From 0d76a9da17e00029c9b7848a5bfd73a3ee481e94 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Thu, 25 Jan 2024 19:21:56 +0100 Subject: [PATCH] LibWeb: Resolve `text-decoration-thickness` during layout commit Refactoring towards stop resolving CSS lengths during paintable commands recording. --- Userland/Libraries/LibWeb/Layout/LayoutState.cpp | 15 ++++++++++++++- .../Libraries/LibWeb/Painting/PaintableBox.cpp | 12 +++--------- .../Libraries/LibWeb/Painting/TextPaintable.h | 5 +++++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/LayoutState.cpp b/Userland/Libraries/LibWeb/Layout/LayoutState.cpp index 4b87c6db2e..c2b19b2c34 100644 --- a/Userland/Libraries/LibWeb/Layout/LayoutState.cpp +++ b/Userland/Libraries/LibWeb/Layout/LayoutState.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace Web::Layout { @@ -507,8 +508,20 @@ void LayoutState::commit(Box& root) paintable_with_lines.set_fragments(move(fragments_with_inline_paintables_removed)); } - for (auto* text_node : text_nodes) + for (auto* text_node : text_nodes) { text_node->set_paintable(text_node->create_paintable()); + auto* paintable = text_node->paintable(); + auto const& font = text_node->first_available_font(); + auto const glyph_height = CSSPixels::nearest_value_for(font.pixel_size()); + auto const css_line_thickness = [&] { + auto computed_thickness = text_node->computed_values().text_decoration_thickness().resolved(*text_node, CSS::Length(1, CSS::Length::Type::Em).to_px(*text_node)); + if (computed_thickness.is_auto()) + return max(glyph_height.scaled(0.1), 1); + return computed_thickness.to_px(*text_node); + }(); + auto& text_paintable = static_cast(*paintable); + text_paintable.set_text_decoration_thickness(css_line_thickness); + } build_paint_tree(root); diff --git a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp index 085a4df13b..4664c47e13 100644 --- a/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp +++ b/Userland/Libraries/LibWeb/Painting/PaintableBox.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -508,15 +509,8 @@ void paint_text_decoration(PaintContext& context, Layout::Node const& text_node, auto baseline = fragment.baseline(); auto line_color = text_node.computed_values().text_decoration_color(); - - CSSPixels css_line_thickness = [&] { - CSS::Length computed_thickness = text_node.computed_values().text_decoration_thickness().resolved(text_node, CSS::Length(1, CSS::Length::Type::Em).to_px(text_node)); - if (computed_thickness.is_auto()) - return max(glyph_height.scaled(0.1), 1); - - return computed_thickness.to_px(text_node); - }(); - auto device_line_thickness = context.rounded_device_pixels(css_line_thickness); + auto const& text_paintable = static_cast(fragment.paintable()); + auto device_line_thickness = context.rounded_device_pixels(text_paintable.text_decoration_thickness()); auto text_decoration_lines = text_node.computed_values().text_decoration_line(); for (auto line : text_decoration_lines) { diff --git a/Userland/Libraries/LibWeb/Painting/TextPaintable.h b/Userland/Libraries/LibWeb/Painting/TextPaintable.h index 9213d6b4a4..d67da894ab 100644 --- a/Userland/Libraries/LibWeb/Painting/TextPaintable.h +++ b/Userland/Libraries/LibWeb/Painting/TextPaintable.h @@ -24,8 +24,13 @@ public: virtual DispatchEventOfSameName handle_mouseup(Badge, CSSPixelPoint, unsigned button, unsigned modifiers) override; virtual DispatchEventOfSameName handle_mousemove(Badge, CSSPixelPoint, unsigned button, unsigned modifiers) override; + void set_text_decoration_thickness(CSSPixels thickness) { m_text_decoration_thickness = thickness; } + CSSPixels text_decoration_thickness() const { return m_text_decoration_thickness; } + private: explicit TextPaintable(Layout::TextNode const&); + + CSSPixels m_text_decoration_thickness { 0 }; }; }