From 9fa22b60cfd94b986c8a87fd067f63472458317b Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Mon, 26 Feb 2024 09:18:31 +0100 Subject: [PATCH] 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. --- .../Libraries/LibWeb/Painting/ImagePaintable.cpp | 13 +++++++------ Userland/Libraries/LibWeb/Painting/ImagePaintable.h | 5 ++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp b/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp index 876fce2c9c..063d88921d 100644 --- a/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/ImagePaintable.cpp @@ -20,11 +20,14 @@ namespace Web::Painting { JS::NonnullGCPtr ImagePaintable::create(Layout::ImageBox const& layout_box) { - return layout_box.heap().allocate_without_realm(layout_box); + auto alt = layout_box.dom_node().get_attribute_value(HTML::AttributeNames::alt); + return layout_box.heap().allocate_without_realm(layout_box, move(alt)); } -ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box) +ImagePaintable::ImagePaintable(Layout::ImageBox const& layout_box, String alt_text) : PaintableBox(layout_box) + , m_renders_as_alt_text(layout_box.renders_as_alt_text()) + , m_alt_text(move(alt_text)) { const_cast(layout_box.document()).register_viewport_client(*this); } @@ -52,12 +55,10 @@ void ImagePaintable::paint(PaintContext& context, PaintPhase phase) const if (phase == PaintPhase::Foreground) { auto image_rect = context.rounded_device_rect(absolute_rect()); - if (layout_box().renders_as_alt_text()) { - auto const& image_element = verify_cast(*dom_node()); + if (m_renders_as_alt_text) { auto enclosing_rect = context.enclosing_device_rect(absolute_rect()).to_type(); 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())) { ScopedCornerRadiusClip corner_clip { context, image_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) }; auto image_int_rect = image_rect.to_type(); diff --git a/Userland/Libraries/LibWeb/Painting/ImagePaintable.h b/Userland/Libraries/LibWeb/Painting/ImagePaintable.h index 41569c2203..d7956d735f 100644 --- a/Userland/Libraries/LibWeb/Painting/ImagePaintable.h +++ b/Userland/Libraries/LibWeb/Painting/ImagePaintable.h @@ -30,7 +30,10 @@ private: // ^Document::ViewportClient 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; }; }