From 1feacd4b52a884878c510127552c97dc7be0bd7c Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sun, 20 Aug 2023 13:02:41 +0100 Subject: [PATCH] LibWeb: Use doubles for CSS dimension types Avoid unintentionally converting between float and double multiple times by just using double everywhere. Also, remove the unused `int` versions of their constructors. --- Userland/Libraries/LibWeb/CSS/Angle.cpp | 6 --- Userland/Libraries/LibWeb/CSS/Angle.h | 1 - Userland/Libraries/LibWeb/CSS/Frequency.cpp | 6 --- Userland/Libraries/LibWeb/CSS/Frequency.h | 1 - Userland/Libraries/LibWeb/CSS/Length.cpp | 5 --- Userland/Libraries/LibWeb/CSS/Length.h | 1 - .../LibWeb/CSS/Parser/GradientParsing.cpp | 4 +- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 40 +++++++++---------- Userland/Libraries/LibWeb/CSS/Percentage.h | 5 --- Userland/Libraries/LibWeb/CSS/Ratio.cpp | 2 +- Userland/Libraries/LibWeb/CSS/Ratio.h | 8 ++-- Userland/Libraries/LibWeb/CSS/Resolution.cpp | 12 ++---- Userland/Libraries/LibWeb/CSS/Resolution.h | 7 ++-- Userland/Libraries/LibWeb/CSS/Time.cpp | 6 --- Userland/Libraries/LibWeb/CSS/Time.h | 1 - Userland/Libraries/LibWeb/Layout/Box.cpp | 2 +- 16 files changed, 34 insertions(+), 73 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Angle.cpp b/Userland/Libraries/LibWeb/CSS/Angle.cpp index 38a56ee51ed..c20544c65d5 100644 --- a/Userland/Libraries/LibWeb/CSS/Angle.cpp +++ b/Userland/Libraries/LibWeb/CSS/Angle.cpp @@ -10,12 +10,6 @@ namespace Web::CSS { -Angle::Angle(int value, Type type) - : m_type(type) - , m_value(value) -{ -} - Angle::Angle(double value, Type type) : m_type(type) , m_value(value) diff --git a/Userland/Libraries/LibWeb/CSS/Angle.h b/Userland/Libraries/LibWeb/CSS/Angle.h index b9339909827..d400a95625d 100644 --- a/Userland/Libraries/LibWeb/CSS/Angle.h +++ b/Userland/Libraries/LibWeb/CSS/Angle.h @@ -22,7 +22,6 @@ public: static Optional unit_from_name(StringView); - Angle(int value, Type type); Angle(double value, Type type); static Angle make_degrees(double); Angle percentage_of(Percentage const&) const; diff --git a/Userland/Libraries/LibWeb/CSS/Frequency.cpp b/Userland/Libraries/LibWeb/CSS/Frequency.cpp index 9b4b0c569d9..df217f8c91b 100644 --- a/Userland/Libraries/LibWeb/CSS/Frequency.cpp +++ b/Userland/Libraries/LibWeb/CSS/Frequency.cpp @@ -9,12 +9,6 @@ namespace Web::CSS { -Frequency::Frequency(int value, Type type) - : m_type(type) - , m_value(value) -{ -} - Frequency::Frequency(double value, Type type) : m_type(type) , m_value(value) diff --git a/Userland/Libraries/LibWeb/CSS/Frequency.h b/Userland/Libraries/LibWeb/CSS/Frequency.h index 855aa1f7985..14e4f979649 100644 --- a/Userland/Libraries/LibWeb/CSS/Frequency.h +++ b/Userland/Libraries/LibWeb/CSS/Frequency.h @@ -19,7 +19,6 @@ public: static Optional unit_from_name(StringView); - Frequency(int value, Type type); Frequency(double value, Type type); static Frequency make_hertz(double); Frequency percentage_of(Percentage const&) const; diff --git a/Userland/Libraries/LibWeb/CSS/Length.cpp b/Userland/Libraries/LibWeb/CSS/Length.cpp index 5b7e8e4075c..bc8c27a10de 100644 --- a/Userland/Libraries/LibWeb/CSS/Length.cpp +++ b/Userland/Libraries/LibWeb/CSS/Length.cpp @@ -30,11 +30,6 @@ Length::FontMetrics::FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics cons { } -Length::Length(int value, Type type) - : m_type(type) - , m_value(value) -{ -} Length::Length(double value, Type type) : m_type(type) , m_value(value) diff --git a/Userland/Libraries/LibWeb/CSS/Length.h b/Userland/Libraries/LibWeb/CSS/Length.h index 1a63e6d30d2..3c3d03d79a6 100644 --- a/Userland/Libraries/LibWeb/CSS/Length.h +++ b/Userland/Libraries/LibWeb/CSS/Length.h @@ -83,7 +83,6 @@ public: static Optional unit_from_name(StringView); - Length(int value, Type type); Length(double value, Type type); ~Length(); diff --git a/Userland/Libraries/LibWeb/CSS/Parser/GradientParsing.cpp b/Userland/Libraries/LibWeb/CSS/Parser/GradientParsing.cpp index 8eb63ab01f1..2b01c7da740 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/GradientParsing.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/GradientParsing.cpp @@ -200,7 +200,7 @@ RefPtr Parser::parse_linear_gradient_function(ComponentValue const& if (first_param.is(Token::Type::Dimension)) { // tokens.next_token(); - float angle_value = first_param.token().dimension_value(); + auto angle_value = first_param.token().dimension_value(); auto unit_string = first_param.token().dimension_unit(); auto angle_type = Angle::unit_from_name(unit_string); @@ -320,7 +320,7 @@ RefPtr Parser::parse_conic_gradient_function(ComponentValue const& c auto angle_token = tokens.next_token(); if (!angle_token.is(Token::Type::Dimension)) return nullptr; - float angle = angle_token.token().dimension_value(); + auto angle = angle_token.token().dimension_value(); auto angle_unit = angle_token.token().dimension_unit(); auto angle_type = Angle::unit_from_name(angle_unit); if (!angle_type.has_value()) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 07ca26cf444..40e815eac46 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1755,7 +1755,7 @@ OwnPtr Parser::parse_a_calc_function_node(Function const& funct Optional Parser::parse_dimension(ComponentValue const& component_value) { if (component_value.is(Token::Type::Dimension)) { - float numeric_value = component_value.token().dimension_value(); + auto numeric_value = component_value.token().dimension_value(); auto unit_string = component_value.token().dimension_unit(); if (auto length_type = Length::unit_from_name(unit_string); length_type.has_value()) @@ -1775,10 +1775,10 @@ Optional Parser::parse_dimension(ComponentValue const& component_valu } if (component_value.is(Token::Type::Percentage)) - return Percentage { static_cast(component_value.token().percentage()) }; + return Percentage { component_value.token().percentage() }; if (component_value.is(Token::Type::Number)) { - float numeric_value = component_value.token().number_value(); + auto numeric_value = component_value.token().number_value(); if (numeric_value == 0) return Length::make_px(0); if (m_context.in_quirks_mode() && property_has_quirk(m_context.current_property_id(), Quirk::UnitlessLength)) { @@ -1827,7 +1827,7 @@ Optional Parser::parse_ratio(TokenStream& tokens) auto transaction = tokens.begin_transaction(); tokens.skip_whitespace(); - auto read_number_value = [this](ComponentValue const& component_value) -> Optional { + auto read_number_value = [this](ComponentValue const& component_value) -> Optional { if (component_value.is(Token::Type::Number)) { return component_value.token().number_value(); } else if (component_value.is_function()) { @@ -2236,9 +2236,9 @@ Optional Parser::parse_rgb_or_hsl_color(StringView function_name, Vector< u8 a_val = 255; if (params[3].is(Token::Type::Number)) - a_val = clamp(lroundf(params[3].number_value() * 255.0f), 0, 255); + a_val = clamp(lround(params[3].number_value() * 255.0), 0, 255); else if (params[3].is(Token::Type::Percentage)) - a_val = clamp(lroundf(params[3].percentage() * 2.55f), 0, 255); + a_val = clamp(lround(params[3].percentage() * 2.55), 0, 255); if (params[0].is(Token::Type::Number) && params[1].is(Token::Type::Number) @@ -2255,9 +2255,9 @@ Optional Parser::parse_rgb_or_hsl_color(StringView function_name, Vector< && params[1].is(Token::Type::Percentage) && params[2].is(Token::Type::Percentage)) { - u8 r_val = lroundf(clamp(params[0].percentage() * 2.55f, 0, 255)); - u8 g_val = lroundf(clamp(params[1].percentage() * 2.55f, 0, 255)); - u8 b_val = lroundf(clamp(params[2].percentage() * 2.55f, 0, 255)); + u8 r_val = lround(clamp(params[0].percentage() * 2.55, 0, 255)); + u8 g_val = lround(clamp(params[1].percentage() * 2.55, 0, 255)); + u8 b_val = lround(clamp(params[2].percentage() * 2.55, 0, 255)); return Color(r_val, g_val, b_val, a_val); } @@ -2266,17 +2266,17 @@ Optional Parser::parse_rgb_or_hsl_color(StringView function_name, Vector< // https://www.w3.org/TR/css-color-4/#the-hsl-notation - float a_val = 1.0f; + auto a_val = 1.0; if (params[3].is(Token::Type::Number)) a_val = params[3].number_value(); else if (params[3].is(Token::Type::Percentage)) - a_val = params[3].percentage() / 100.0f; + a_val = params[3].percentage() / 100.0; if (params[0].is(Token::Type::Dimension) && params[1].is(Token::Type::Percentage) && params[2].is(Token::Type::Percentage)) { - float numeric_value = params[0].dimension_value(); + auto numeric_value = params[0].dimension_value(); auto unit_string = params[0].dimension_unit(); auto angle_type = Angle::unit_from_name(unit_string); @@ -2285,9 +2285,9 @@ Optional Parser::parse_rgb_or_hsl_color(StringView function_name, Vector< auto angle = Angle { numeric_value, angle_type.release_value() }; - float h_val = fmodf(angle.to_degrees(), 360.0f); - float s_val = params[1].percentage() / 100.0f; - float l_val = params[2].percentage() / 100.0f; + float h_val = fmod(angle.to_degrees(), 360.0); + float s_val = params[1].percentage() / 100.0; + float l_val = params[2].percentage() / 100.0; return Color::from_hsla(h_val, s_val, l_val, a_val); } @@ -2296,9 +2296,9 @@ Optional Parser::parse_rgb_or_hsl_color(StringView function_name, Vector< && params[1].is(Token::Type::Percentage) && params[2].is(Token::Type::Percentage)) { - float h_val = fmodf(params[0].number_value(), 360.0f); - float s_val = params[1].percentage() / 100.0f; - float l_val = params[2].percentage() / 100.0f; + float h_val = fmod(params[0].number_value(), 360.0); + float s_val = params[1].percentage() / 100.0; + float l_val = params[2].percentage() / 100.0; return Color::from_hsla(h_val, s_val, l_val, a_val); } @@ -3882,7 +3882,7 @@ RefPtr Parser::parse_filter_value_list_value(Vector } if (!token.is(Token::Type::Dimension)) return {}; - float angle_value = token.token().dimension_value(); + auto angle_value = token.token().dimension_value(); auto angle_unit_name = token.token().dimension_unit(); auto angle_unit = Angle::unit_from_name(angle_unit_name); if (!angle_unit.has_value()) @@ -5070,7 +5070,7 @@ Optional Parser::parse_grid_size(ComponentValue const& component_ } auto token = component_value.token(); if (token.is(Token::Type::Dimension) && token.dimension_unit().equals_ignoring_ascii_case("fr"sv)) { - float numeric_value = token.dimension_value(); + auto numeric_value = token.dimension_value(); if (numeric_value) return GridSize(numeric_value); } diff --git a/Userland/Libraries/LibWeb/CSS/Percentage.h b/Userland/Libraries/LibWeb/CSS/Percentage.h index 4cd50abf850..5040515fec7 100644 --- a/Userland/Libraries/LibWeb/CSS/Percentage.h +++ b/Userland/Libraries/LibWeb/CSS/Percentage.h @@ -18,11 +18,6 @@ namespace Web::CSS { class Percentage { public: - explicit Percentage(int value) - : m_value(value) - { - } - explicit Percentage(double value) : m_value(value) { diff --git a/Userland/Libraries/LibWeb/CSS/Ratio.cpp b/Userland/Libraries/LibWeb/CSS/Ratio.cpp index d089636f8f5..1895237ca47 100644 --- a/Userland/Libraries/LibWeb/CSS/Ratio.cpp +++ b/Userland/Libraries/LibWeb/CSS/Ratio.cpp @@ -9,7 +9,7 @@ namespace Web::CSS { -Ratio::Ratio(float first, float second) +Ratio::Ratio(double first, double second) : m_first_value(first) , m_second_value(second) { diff --git a/Userland/Libraries/LibWeb/CSS/Ratio.h b/Userland/Libraries/LibWeb/CSS/Ratio.h index 1f3af802784..b6f5c627ddd 100644 --- a/Userland/Libraries/LibWeb/CSS/Ratio.h +++ b/Userland/Libraries/LibWeb/CSS/Ratio.h @@ -13,8 +13,8 @@ namespace Web::CSS { // https://www.w3.org/TR/css-values-4/#ratios class Ratio { public: - Ratio(float first, float second = 1); - float value() const { return m_first_value / m_second_value; } + Ratio(double first, double second = 1); + double value() const { return m_first_value / m_second_value; } bool is_degenerate() const; ErrorOr to_string() const; @@ -37,8 +37,8 @@ public: } private: - float m_first_value { 0 }; - float m_second_value { 1 }; + double m_first_value { 0 }; + double m_second_value { 1 }; }; } diff --git a/Userland/Libraries/LibWeb/CSS/Resolution.cpp b/Userland/Libraries/LibWeb/CSS/Resolution.cpp index 2e0a3568e8c..f1912fd4f19 100644 --- a/Userland/Libraries/LibWeb/CSS/Resolution.cpp +++ b/Userland/Libraries/LibWeb/CSS/Resolution.cpp @@ -8,13 +8,7 @@ namespace Web::CSS { -Resolution::Resolution(int value, Type type) - : m_type(type) - , m_value(value) -{ -} - -Resolution::Resolution(float value, Type type) +Resolution::Resolution(double value, Type type) : m_type(type) , m_value(value) { @@ -25,13 +19,13 @@ ErrorOr Resolution::to_string() const return String::formatted("{}dppx", to_dots_per_pixel()); } -float Resolution::to_dots_per_pixel() const +double Resolution::to_dots_per_pixel() const { switch (m_type) { case Type::Dpi: return m_value * 96; // 1in = 2.54cm = 96px case Type::Dpcm: - return m_value * (96.0f / 2.54f); // 1cm = 96px/2.54 + return m_value * (96.0 / 2.54); // 1cm = 96px/2.54 case Type::Dppx: return m_value; } diff --git a/Userland/Libraries/LibWeb/CSS/Resolution.h b/Userland/Libraries/LibWeb/CSS/Resolution.h index 6a693775d39..8daf32721fb 100644 --- a/Userland/Libraries/LibWeb/CSS/Resolution.h +++ b/Userland/Libraries/LibWeb/CSS/Resolution.h @@ -21,11 +21,10 @@ public: static Optional unit_from_name(StringView); - Resolution(int value, Type type); - Resolution(float value, Type type); + Resolution(double value, Type type); ErrorOr to_string() const; - float to_dots_per_pixel() const; + double to_dots_per_pixel() const; bool operator==(Resolution const& other) const { @@ -48,6 +47,6 @@ private: StringView unit_name() const; Type m_type; - float m_value { 0 }; + double m_value { 0 }; }; } diff --git a/Userland/Libraries/LibWeb/CSS/Time.cpp b/Userland/Libraries/LibWeb/CSS/Time.cpp index a03e4b5c7e9..8f5ec30d845 100644 --- a/Userland/Libraries/LibWeb/CSS/Time.cpp +++ b/Userland/Libraries/LibWeb/CSS/Time.cpp @@ -9,12 +9,6 @@ namespace Web::CSS { -Time::Time(int value, Type type) - : m_type(type) - , m_value(value) -{ -} - Time::Time(double value, Type type) : m_type(type) , m_value(value) diff --git a/Userland/Libraries/LibWeb/CSS/Time.h b/Userland/Libraries/LibWeb/CSS/Time.h index dca75618b51..8fdad6c7885 100644 --- a/Userland/Libraries/LibWeb/CSS/Time.h +++ b/Userland/Libraries/LibWeb/CSS/Time.h @@ -20,7 +20,6 @@ public: static Optional unit_from_name(StringView); - Time(int value, Type type); Time(double value, Type type); static Time make_seconds(double); Time percentage_of(Percentage const&) const; diff --git a/Userland/Libraries/LibWeb/Layout/Box.cpp b/Userland/Libraries/LibWeb/Layout/Box.cpp index 4f25a040cd3..a3be3a9268f 100644 --- a/Userland/Libraries/LibWeb/Layout/Box.cpp +++ b/Userland/Libraries/LibWeb/Layout/Box.cpp @@ -92,7 +92,7 @@ Optional Box::preferred_aspect_ratio() const auto computed_aspect_ratio = computed_values().aspect_ratio(); if (computed_aspect_ratio.use_natural_aspect_ratio_if_available && natural_aspect_ratio().has_value()) return natural_aspect_ratio(); - return computed_aspect_ratio.preferred_ratio.map([](CSS::Ratio const& ratio) { return ratio.value(); }); + return computed_aspect_ratio.preferred_ratio.map([](CSS::Ratio const& ratio) { return static_cast(ratio.value()); }); } }