Browse Source

LibWeb: Save alt text in ImagePaintable

By saving string with alt text, image paintable no longer need to reach
into layout and DOM nodes while painting commands recording.

No behaviour change intended.
Aliaksandr Kalenik 1 year ago
parent
commit
9fa22b60cf

+ 7 - 6
Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp

@@ -20,11 +20,14 @@ namespace Web::Painting {
 
 
 JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box)
 JS::NonnullGCPtr<ImagePaintable> ImagePaintable::create(Layout::ImageBox const& layout_box)
 {
 {
-    return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box);
+    auto alt = layout_box.dom_node().get_attribute_value(HTML::AttributeNames::alt);
+    return layout_box.heap().allocate_without_realm<ImagePaintable>(layout_box, move(alt));
 }
 }
 
 
-ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box)
+ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box, String alt_text)
     : PaintableBox(layout_box)
     : PaintableBox(layout_box)
+    , m_renders_as_alt_text(layout_box.renders_as_alt_text())
+    , m_alt_text(move(alt_text))
 {
 {
     const_cast<DOM::Document&>(layout_box.document()).register_viewport_client(*this);
     const_cast<DOM::Document&>(layout_box.document()).register_viewport_client(*this);
 }
 }
@@ -52,12 +55,10 @@ void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const
 
 
     if (phase == PaintPhase::Foreground) {
     if (phase == PaintPhase::Foreground) {
         auto image_rect = context.rounded_device_rect(absolute_rect());
         auto image_rect = context.rounded_device_rect(absolute_rect());
-        if (layout_box().renders_as_alt_text()) {
-            auto const& image_element = verify_cast<HTML::HTMLElement>(*dom_node());
+        if (m_renders_as_alt_text) {
             auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
             auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type<int>();
             context.recording_painter().paint_frame(enclosing_rect, context.palette(), Gfx::FrameStyle::SunkenContainer);
             context.recording_painter().paint_frame(enclosing_rect, context.palette(), Gfx::FrameStyle::SunkenContainer);
-            auto alt = image_element.get_attribute_value(HTML::AttributeNames::alt);
-            context.recording_painter().draw_text(enclosing_rect, alt, Platform::FontPlugin::the().default_font(), Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
+            context.recording_painter().draw_text(enclosing_rect, m_alt_text, Platform::FontPlugin::the().default_font(), Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
         } else if (auto bitmap = layout_box().image_provider().current_image_bitmap(image_rect.size().to_type<int>())) {
         } else if (auto bitmap = layout_box().image_provider().current_image_bitmap(image_rect.size().to_type<int>())) {
             ScopedCornerRadiusClip corner_clip { context, image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
             ScopedCornerRadiusClip corner_clip { context, image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) };
             auto image_int_rect = image_rect.to_type<int>();
             auto image_int_rect = image_rect.to_type<int>();

+ 4 - 1
Userland/Libraries/LibWeb/Painting/ImagePaintable.h

@@ -30,7 +30,10 @@ private:
     // ^Document::ViewportClient
     // ^Document::ViewportClient
     virtual void did_set_viewport_rect(CSSPixelRect const&) final;
     virtual void did_set_viewport_rect(CSSPixelRect const&) final;
 
 
-    ImagePaintable(Layout::ImageBox const&);
+    ImagePaintable(Layout::ImageBox const&, String alt_text);
+
+    bool m_renders_as_alt_text { false };
+    String m_alt_text;
 };
 };
 
 
 }
 }