瀏覽代碼

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.
Sam Atkins 1 年之前
父節點
當前提交
1feacd4b52

+ 0 - 6
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)

+ 0 - 1
Userland/Libraries/LibWeb/CSS/Angle.h

@@ -22,7 +22,6 @@ public:
 
     static Optional<Type> 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;

+ 0 - 6
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)

+ 0 - 1
Userland/Libraries/LibWeb/CSS/Frequency.h

@@ -19,7 +19,6 @@ public:
 
     static Optional<Type> 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;

+ 0 - 5
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)

+ 0 - 1
Userland/Libraries/LibWeb/CSS/Length.h

@@ -83,7 +83,6 @@ public:
 
     static Optional<Type> unit_from_name(StringView);
 
-    Length(int value, Type type);
     Length(double value, Type type);
     ~Length();
 

+ 2 - 2
Userland/Libraries/LibWeb/CSS/Parser/GradientParsing.cpp

@@ -200,7 +200,7 @@ RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const&
     if (first_param.is(Token::Type::Dimension)) {
         // <angle>
         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<StyleValue> 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())

+ 20 - 20
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -1755,7 +1755,7 @@ OwnPtr<CalculationNode> Parser::parse_a_calc_function_node(Function const& funct
 Optional<Dimension> 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<Dimension> Parser::parse_dimension(ComponentValue const& component_valu
     }
 
     if (component_value.is(Token::Type::Percentage))
-        return Percentage { static_cast<float>(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<Ratio> Parser::parse_ratio(TokenStream<ComponentValue>& tokens)
     auto transaction = tokens.begin_transaction();
     tokens.skip_whitespace();
 
-    auto read_number_value = [this](ComponentValue const& component_value) -> Optional<float> {
+    auto read_number_value = [this](ComponentValue const& component_value) -> Optional<double> {
         if (component_value.is(Token::Type::Number)) {
             return component_value.token().number_value();
         } else if (component_value.is_function()) {
@@ -2236,9 +2236,9 @@ Optional<Color> 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<Color> 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<Color> 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<Color> 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<Color> 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<StyleValue> Parser::parse_filter_value_list_value(Vector<ComponentValue>
             }
             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<CSS::GridSize> 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);
     }

+ 0 - 5
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)
     {

+ 1 - 1
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)
 {

+ 4 - 4
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<String> 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 };
 };
 
 }

+ 3 - 9
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<String> 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;
     }

+ 3 - 4
Userland/Libraries/LibWeb/CSS/Resolution.h

@@ -21,11 +21,10 @@ public:
 
     static Optional<Type> unit_from_name(StringView);
 
-    Resolution(int value, Type type);
-    Resolution(float value, Type type);
+    Resolution(double value, Type type);
 
     ErrorOr<String> 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 };
 };
 }

+ 0 - 6
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)

+ 0 - 1
Userland/Libraries/LibWeb/CSS/Time.h

@@ -20,7 +20,6 @@ public:
 
     static Optional<Type> 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;

+ 1 - 1
Userland/Libraries/LibWeb/Layout/Box.cpp

@@ -92,7 +92,7 @@ Optional<float> 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<float>(ratio.value()); });
 }
 
 }