From 1052ee20b0a8ffeb6dad717de741ab89aa780e2e Mon Sep 17 00:00:00 2001 From: Callum Law Date: Thu, 28 Aug 2025 14:12:35 +1200 Subject: [PATCH] LibWeb: Move `snap_a_length_as_a_border_width` to `StyleComputer.cpp` All users are in this file so it makes more sense to have it here --- Libraries/LibWeb/CSS/StyleComputer.cpp | 24 +++++++++++++++++++++++- Libraries/LibWeb/Layout/Node.cpp | 22 ---------------------- Libraries/LibWeb/Layout/Node.h | 3 --- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp index 20f0dbf6a9b..df9f83e4fd8 100644 --- a/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -3220,6 +3220,28 @@ static CSSPixels line_width_keyword_to_css_pixels(Keyword keyword) } } +// https://www.w3.org/TR/css-values-4/#snap-a-length-as-a-border-width +static CSSPixels snap_a_length_as_a_border_width(double device_pixels_per_css_pixel, CSSPixels length) +{ + // 1. Assert: len is non-negative. + VERIFY(length >= 0); + + // 2. If len is an integer number of device pixels, do nothing. + auto device_pixels = length.to_double() * device_pixels_per_css_pixel; + if (device_pixels == trunc(device_pixels)) + return length; + + // 3. If len is greater than zero, but less than 1 device pixel, round len up to 1 device pixel. + if (device_pixels > 0 && device_pixels < 1) + return CSSPixels::nearest_value_for(1 / device_pixels_per_css_pixel); + + // 4. If len is greater than 1 device pixel, round it down to the nearest integer number of device pixels. + if (device_pixels > 1) + return CSSPixels::nearest_value_for(floor(device_pixels) / device_pixels_per_css_pixel); + + return length; +} + NonnullRefPtr StyleComputer::compute_value_of_property(PropertyID property_id, NonnullRefPtr const& specified_value, Function(PropertyID)> const& get_property_specified_value, PropertyValueComputationContext const& computation_context) { switch (property_id) { @@ -3261,7 +3283,7 @@ NonnullRefPtr StyleComputer::compute_border_or_outline_width(N VERIFY_NOT_REACHED(); }(); - return LengthStyleValue::create(Length::make_px(Layout::NodeWithStyle::snap_a_length_as_a_border_width(computation_context.device_pixels_per_css_pixel, absolute_length))); + return LengthStyleValue::create(Length::make_px(snap_a_length_as_a_border_width(computation_context.device_pixels_per_css_pixel, absolute_length))); } void StyleComputer::compute_math_depth(ComputedProperties& style, Optional element) const diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cpp index c9adfc53ace..c84c40b6aee 100644 --- a/Libraries/LibWeb/Layout/Node.cpp +++ b/Libraries/LibWeb/Layout/Node.cpp @@ -368,28 +368,6 @@ void NodeWithStyle::visit_edges(Visitor& visitor) m_list_style_image->as_image().visit_edges(visitor); } -// https://www.w3.org/TR/css-values-4/#snap-a-length-as-a-border-width -CSSPixels NodeWithStyle::snap_a_length_as_a_border_width(double device_pixels_per_css_pixel, CSSPixels length) -{ - // 1. Assert: len is non-negative. - VERIFY(length >= 0); - - // 2. If len is an integer number of device pixels, do nothing. - auto device_pixels = length.to_double() * device_pixels_per_css_pixel; - if (device_pixels == trunc(device_pixels)) - return length; - - // 3. If len is greater than zero, but less than 1 device pixel, round len up to 1 device pixel. - if (device_pixels > 0 && device_pixels < 1) - return CSSPixels::nearest_value_for(1 / device_pixels_per_css_pixel); - - // 4. If len is greater than 1 device pixel, round it down to the nearest integer number of device pixels. - if (device_pixels > 1) - return CSSPixels::nearest_value_for(floor(device_pixels) / device_pixels_per_css_pixel); - - return length; -} - void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style) { auto& computed_values = mutable_computed_values(); diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.h index 7778b196d6c..6fd5fc9f12c 100644 --- a/Libraries/LibWeb/Layout/Node.h +++ b/Libraries/LibWeb/Layout/Node.h @@ -243,9 +243,6 @@ public: CSS::ImmutableComputedValues const& computed_values() const { return static_cast(*m_computed_values); } CSS::MutableComputedValues& mutable_computed_values() { return static_cast(*m_computed_values); } - // FIXME: Move this to StyleComputer once all users are migrated - static CSSPixels snap_a_length_as_a_border_width(double device_pixels_per_css_pixel, CSSPixels length); - void apply_style(CSS::ComputedProperties const&); Gfx::Font const& first_available_font() const;