From 1a3da3d825d5644196c55e0edb2e28770064d0dc Mon Sep 17 00:00:00 2001 From: Callum Law Date: Mon, 25 Aug 2025 13:20:20 +1200 Subject: [PATCH] LibWeb: Don't store separate computed value for font_size The StyleValue stored in m_property_values is already in it's computed form and it's trivial to pull the underlying value out so there is no need to store this separately. Also removes unnecessary handling of percentage values in `absolutize_values` - this is already handled within `compute_font`. --- Libraries/LibWeb/CSS/ComputedProperties.cpp | 5 +++++ Libraries/LibWeb/CSS/ComputedProperties.h | 4 +--- Libraries/LibWeb/CSS/StyleComputer.cpp | 24 ++++++--------------- Libraries/LibWeb/CSS/StyleComputer.h | 2 +- Libraries/LibWeb/DOM/Element.cpp | 2 +- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/Libraries/LibWeb/CSS/ComputedProperties.cpp b/Libraries/LibWeb/CSS/ComputedProperties.cpp index 042fec30407..406cfa92557 100644 --- a/Libraries/LibWeb/CSS/ComputedProperties.cpp +++ b/Libraries/LibWeb/CSS/ComputedProperties.cpp @@ -1977,4 +1977,9 @@ WillChange ComputedProperties::will_change() const return WillChange::make_auto(); } +CSSPixels ComputedProperties::font_size() const +{ + return property(PropertyID::FontSize).as_length().length().absolute_length_to_px(); +} + } diff --git a/Libraries/LibWeb/CSS/ComputedProperties.h b/Libraries/LibWeb/CSS/ComputedProperties.h index 5e838ab421e..f32e70a6bd4 100644 --- a/Libraries/LibWeb/CSS/ComputedProperties.h +++ b/Libraries/LibWeb/CSS/ComputedProperties.h @@ -227,8 +227,7 @@ public: [[nodiscard]] CSSPixels line_height() const { return *m_line_height; } void set_line_height(Badge const&, CSSPixels line_height) { m_line_height = line_height; } - [[nodiscard]] CSSPixels font_size() const { return *m_font_size; } - void set_font_size(Badge const&, CSSPixels font_size) { m_font_size = font_size; } + [[nodiscard]] CSSPixels font_size() const; bool operator==(ComputedProperties const&) const; @@ -283,7 +282,6 @@ private: RefPtr m_first_available_computed_font; Optional m_line_height; - Optional m_font_size; PseudoClassBitmap m_attempted_pseudo_class_matches; }; diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp index 5f6ece5d9bd..a6b48ccd78b 100644 --- a/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -1098,7 +1098,7 @@ void StyleComputer::collect_animation_into(DOM::Element& element, Optional element) const +void StyleComputer::absolutize_values(ComputedProperties& style) const { Length::FontMetrics font_metrics { - root_element_font_metrics_for_element(element).font_size, + style.font_size(), style.first_available_computed_font().pixel_metrics() }; - // "A percentage value specifies an absolute font size relative to the parent element’s computed font-size. Negative percentages are invalid." - auto& font_size_value_slot = style.m_property_values[to_underlying(CSS::PropertyID::FontSize)]; - if (font_size_value_slot && font_size_value_slot->is_percentage()) { - auto parent_font_size = get_inherit_value(CSS::PropertyID::FontSize, element)->as_length().length().to_px(viewport_rect(), font_metrics, m_root_element_font_metrics); - font_size_value_slot = LengthStyleValue::create( - Length::make_px(CSSPixels::nearest_value_for(parent_font_size * font_size_value_slot->as_percentage().percentage().as_fraction()))); - } - - auto font_size = font_size_value_slot->as_length().length().to_px(viewport_rect(), font_metrics, m_root_element_font_metrics); - font_metrics.font_size = font_size; - style.set_font_size({}, font_size); - // NOTE: Percentage line-height values are relative to the font-size of the element. // We have to resolve them right away, so that the *computed* line-height is ready for inheritance. // We can't simply absolutize *all* percentage values against the font size, @@ -2162,7 +2150,7 @@ void StyleComputer::absolutize_values(ComputedProperties& style, GC::Ptris_percentage()) { line_height_value_slot = LengthStyleValue::create( - Length::make_px(CSSPixels::nearest_value_for(font_size * static_cast(line_height_value_slot->as_percentage().percentage().as_fraction())))); + Length::make_px(CSSPixels::nearest_value_for(style.font_size() * static_cast(line_height_value_slot->as_percentage().percentage().as_fraction())))); } auto line_height = style.compute_line_height(viewport_rect(), font_metrics, m_root_element_font_metrics); @@ -2396,7 +2384,7 @@ GC::Ref StyleComputer::create_document_style() const compute_math_depth(style, {}); compute_font(style, nullptr, {}); - absolutize_values(style, nullptr); + absolutize_values(style); style->set_property(CSS::PropertyID::Width, CSS::LengthStyleValue::create(CSS::Length::make_px(viewport_rect().width()))); style->set_property(CSS::PropertyID::Height, CSS::LengthStyleValue::create(CSS::Length::make_px(viewport_rect().height()))); style->set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::Block))); @@ -2708,7 +2696,7 @@ GC::Ref StyleComputer::compute_properties(DOM::Element& elem compute_font(computed_style, &element, pseudo_element); // 4. Absolutize values, turning font/viewport relative lengths into absolute lengths - absolutize_values(computed_style, element); + absolutize_values(computed_style); // 5. Run automatic box type transformations transform_box_type_if_needed(computed_style, element, pseudo_element); diff --git a/Libraries/LibWeb/CSS/StyleComputer.h b/Libraries/LibWeb/CSS/StyleComputer.h index 13351705e7f..a68e6e8fc78 100644 --- a/Libraries/LibWeb/CSS/StyleComputer.h +++ b/Libraries/LibWeb/CSS/StyleComputer.h @@ -193,7 +193,7 @@ public: [[nodiscard]] GC::Ref compute_properties(DOM::Element&, Optional, CascadedProperties&) const; - void absolutize_values(ComputedProperties&, GC::Ptr) const; + void absolutize_values(ComputedProperties&) const; void compute_font(ComputedProperties&, DOM::Element const*, Optional) const; [[nodiscard]] inline bool should_reject_with_ancestor_filter(Selector const&) const; diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp index 5224ebd3c4a..d6905a63e16 100644 --- a/Libraries/LibWeb/DOM/Element.cpp +++ b/Libraries/LibWeb/DOM/Element.cpp @@ -839,7 +839,7 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_inherited_style() return invalidation; document().style_computer().compute_font(*computed_properties, this, {}); - document().style_computer().absolutize_values(*computed_properties, this); + document().style_computer().absolutize_values(*computed_properties); for (auto [property_id, old_value] : old_values_with_relative_units) { auto new_value = computed_properties->maybe_null_property(static_cast(property_id));