summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-03-30 15:01:23 +0100
committerAndreas Kling <kling@serenityos.org>2023-03-30 21:29:50 +0200
commit16e3a863936baeafe62d8c89992d826e43f0da07 (patch)
treef16f852640a0ca319bed45edd73a267c116c2102 /Userland
parent7d29262b8bbabd43b45b296df0e6f4af1b36d562 (diff)
downloadserenity-16e3a863936baeafe62d8c89992d826e43f0da07.zip
LibWeb: Make `absolutized_length()` helper a Length method
There were a mix of users between those who want to know if the Length changed, and those that just want an absolute Length. So, we now have two methods: Length::absolutize() returns an empty Optional if nothing changed, and Length::absolutized() always returns a value.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/CSS/Length.cpp16
-rw-r--r--Userland/Libraries/LibWeb/CSS/Length.h4
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValue.cpp11
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValue.h2
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValues/BorderRadiusStyleValue.cpp4
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp2
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp8
7 files changed, 27 insertions, 20 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Length.cpp b/Userland/Libraries/LibWeb/CSS/Length.cpp
index e937719f66..d9197d7720 100644
--- a/Userland/Libraries/LibWeb/CSS/Length.cpp
+++ b/Userland/Libraries/LibWeb/CSS/Length.cpp
@@ -193,4 +193,20 @@ Optional<Length::Type> Length::unit_from_name(StringView name)
return {};
}
+Optional<Length> Length::absolutize(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
+{
+ if (is_px())
+ return {};
+ if (is_absolute() || is_relative()) {
+ auto px = to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
+ return CSS::Length::make_px(px);
+ }
+ return {};
+}
+
+Length Length::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
+{
+ return absolutize(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(*this);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/CSS/Length.h b/Userland/Libraries/LibWeb/CSS/Length.h
index baa2b2e87f..d9f5a0af2d 100644
--- a/Userland/Libraries/LibWeb/CSS/Length.h
+++ b/Userland/Libraries/LibWeb/CSS/Length.h
@@ -125,6 +125,10 @@ public:
CSSPixels relative_length_to_px(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const;
+ // Returns empty optional if it's already absolute.
+ Optional<Length> absolutize(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const;
+ Length absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const;
+
private:
char const* unit_name() const;
diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp
index a563c12b76..60122d7c26 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp
+++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp
@@ -1021,17 +1021,6 @@ CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartW
return value->resolve(layout_node, percentage_basis);
}
-Optional<CSS::Length> absolutized_length(CSS::Length const& length, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height)
-{
- if (length.is_px())
- return {};
- if (length.is_absolute() || length.is_relative()) {
- auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
- return CSS::Length::make_px(px);
- }
- return {};
-}
-
ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Gfx::FontPixelMetrics const&, CSSPixels, CSSPixels, CSSPixels, CSSPixels) const
{
return *this;
diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h
index 546882aabf..8ee7ad6591 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleValue.h
+++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h
@@ -90,8 +90,6 @@ private:
using StyleValueVector = Vector<ValueComparingNonnullRefPtr<StyleValue const>>;
-Optional<CSS::Length> absolutized_length(CSS::Length const&, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const&, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height);
-
class StyleValue : public RefCounted<StyleValue> {
public:
virtual ~StyleValue() = default;
diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/BorderRadiusStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/BorderRadiusStyleValue.cpp
index 51ed3004a0..3001f95899 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleValues/BorderRadiusStyleValue.cpp
+++ b/Userland/Libraries/LibWeb/CSS/StyleValues/BorderRadiusStyleValue.cpp
@@ -25,9 +25,9 @@ ValueComparingNonnullRefPtr<StyleValue const> BorderRadiusStyleValue::absolutize
auto absolutized_horizontal_radius = m_properties.horizontal_radius;
auto absolutized_vertical_radius = m_properties.vertical_radius;
if (!m_properties.horizontal_radius.is_percentage())
- absolutized_horizontal_radius = absolutized_length(m_properties.horizontal_radius.length(), viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.horizontal_radius.length());
+ absolutized_horizontal_radius = m_properties.horizontal_radius.length().absolutized(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
if (!m_properties.vertical_radius.is_percentage())
- absolutized_vertical_radius = absolutized_length(m_properties.vertical_radius.length(), viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.vertical_radius.length());
+ absolutized_vertical_radius = m_properties.vertical_radius.length().absolutized(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
return BorderRadiusStyleValue::create(absolutized_horizontal_radius, absolutized_vertical_radius);
}
diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp
index 99fbe547de..15f24b324e 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp
+++ b/Userland/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp
@@ -32,7 +32,7 @@ ValueComparingNonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length co
ValueComparingNonnullRefPtr<StyleValue const> LengthStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
{
- if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height); length.has_value())
+ if (auto length = m_length.absolutize(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height); length.has_value())
return LengthStyleValue::create(length.release_value());
return *this;
}
diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp
index ee75a2366e..feb9535510 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp
+++ b/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp
@@ -22,10 +22,10 @@ ErrorOr<String> ShadowStyleValue::to_string() const
ValueComparingNonnullRefPtr<StyleValue const> ShadowStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
{
- auto absolutized_offset_x = absolutized_length(m_properties.offset_x, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.offset_x);
- auto absolutized_offset_y = absolutized_length(m_properties.offset_y, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.offset_y);
- auto absolutized_blur_radius = absolutized_length(m_properties.blur_radius, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.blur_radius);
- auto absolutized_spread_distance = absolutized_length(m_properties.spread_distance, viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(m_properties.spread_distance);
+ auto absolutized_offset_x = m_properties.offset_x.absolutized(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
+ auto absolutized_offset_y = m_properties.offset_y.absolutized(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
+ auto absolutized_blur_radius = m_properties.blur_radius.absolutized(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
+ auto absolutized_spread_distance = m_properties.spread_distance.absolutized(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
return ShadowStyleValue::create(m_properties.color, absolutized_offset_x, absolutized_offset_y, absolutized_blur_radius, absolutized_spread_distance, m_properties.placement);
}