1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:47:35 +00:00

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.
This commit is contained in:
Aliaksandr Kalenik 2024-02-26 09:18:31 +01:00 committed by Andreas Kling
parent c2bc07ef7c
commit 9fa22b60cf
2 changed files with 11 additions and 7 deletions

View file

@ -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()) { if (m_renders_as_alt_text) {
auto const& image_element = verify_cast<HTML::HTMLElement>(*dom_node());
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, m_alt_text, Platform::FontPlugin::the().default_font(), Gfx::TextAlignment::Center, computed_values().color(), Gfx::TextElision::Right);
context.recording_painter().draw_text(enclosing_rect, alt, 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>();

View file

@ -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;
}; };
} }