mirror of
https://github.com/RGBCube/serenity
synced 2025-07-22 22:37:40 +00:00
LibWeb: Use floats instead of doubles for CSS numbers
Using doubles isn't necessary, and they make things slightly bigger and slower, so let's use floats instead.
This commit is contained in:
parent
1f5b5d3f99
commit
0795b9f7bb
6 changed files with 33 additions and 33 deletions
|
@ -27,7 +27,7 @@ String MediaFeatureValue::to_string() const
|
||||||
[](Length const& length) { return length.to_string(); },
|
[](Length const& length) { return length.to_string(); },
|
||||||
[](Ratio const& ratio) { return ratio.to_string(); },
|
[](Ratio const& ratio) { return ratio.to_string(); },
|
||||||
[](Resolution const& resolution) { return resolution.to_string(); },
|
[](Resolution const& resolution) { return resolution.to_string(); },
|
||||||
[](double number) { return String::number(number); });
|
[](float number) { return String::number(number); });
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
|
bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
|
||||||
|
@ -37,7 +37,7 @@ bool MediaFeatureValue::is_same_type(MediaFeatureValue const& other) const
|
||||||
[&](Length const&) { return other.is_length(); },
|
[&](Length const&) { return other.is_length(); },
|
||||||
[&](Ratio const&) { return other.is_ratio(); },
|
[&](Ratio const&) { return other.is_ratio(); },
|
||||||
[&](Resolution const&) { return other.is_resolution(); },
|
[&](Resolution const&) { return other.is_resolution(); },
|
||||||
[&](double) { return other.is_number(); });
|
[&](float) { return other.is_number(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
String MediaFeature::to_string() const
|
String MediaFeature::to_string() const
|
||||||
|
|
|
@ -42,7 +42,7 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit MediaFeatureValue(double number)
|
explicit MediaFeatureValue(float number)
|
||||||
: m_value(number)
|
: m_value(number)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ public:
|
||||||
|
|
||||||
bool is_ident() const { return m_value.has<ValueID>(); }
|
bool is_ident() const { return m_value.has<ValueID>(); }
|
||||||
bool is_length() const { return m_value.has<Length>(); }
|
bool is_length() const { return m_value.has<Length>(); }
|
||||||
bool is_number() const { return m_value.has<double>(); }
|
bool is_number() const { return m_value.has<float>(); }
|
||||||
bool is_ratio() const { return m_value.has<Ratio>(); }
|
bool is_ratio() const { return m_value.has<Ratio>(); }
|
||||||
bool is_resolution() const { return m_value.has<Resolution>(); }
|
bool is_resolution() const { return m_value.has<Resolution>(); }
|
||||||
bool is_same_type(MediaFeatureValue const& other) const;
|
bool is_same_type(MediaFeatureValue const& other) const;
|
||||||
|
@ -80,14 +80,14 @@ public:
|
||||||
return m_value.get<Resolution>();
|
return m_value.get<Resolution>();
|
||||||
}
|
}
|
||||||
|
|
||||||
double number() const
|
float number() const
|
||||||
{
|
{
|
||||||
VERIFY(is_number());
|
VERIFY(is_number());
|
||||||
return m_value.get<double>();
|
return m_value.get<float>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Variant<ValueID, Length, Ratio, Resolution, double> m_value;
|
Variant<ValueID, Length, Ratio, Resolution, float> m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://www.w3.org/TR/mediaqueries-4/#mq-features
|
// https://www.w3.org/TR/mediaqueries-4/#mq-features
|
||||||
|
|
|
@ -2476,9 +2476,9 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
|
||||||
&& g_val.is(Token::Type::Percentage)
|
&& g_val.is(Token::Type::Percentage)
|
||||||
&& b_val.is(Token::Type::Percentage)) {
|
&& b_val.is(Token::Type::Percentage)) {
|
||||||
|
|
||||||
u8 r = clamp(lroundf(r_val.percentage() * 2.55), 0, 255);
|
u8 r = clamp(lroundf(r_val.percentage() * 2.55f), 0, 255);
|
||||||
u8 g = clamp(lroundf(g_val.percentage() * 2.55), 0, 255);
|
u8 g = clamp(lroundf(g_val.percentage() * 2.55f), 0, 255);
|
||||||
u8 b = clamp(lroundf(b_val.percentage() * 2.55), 0, 255);
|
u8 b = clamp(lroundf(b_val.percentage() * 2.55f), 0, 255);
|
||||||
return Color(r, g, b);
|
return Color(r, g, b);
|
||||||
}
|
}
|
||||||
} else if (function.name().equals_ignoring_case("rgba")) {
|
} else if (function.name().equals_ignoring_case("rgba")) {
|
||||||
|
@ -2498,7 +2498,7 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
|
||||||
auto r = r_val.to_integer();
|
auto r = r_val.to_integer();
|
||||||
auto g = g_val.to_integer();
|
auto g = g_val.to_integer();
|
||||||
auto b = b_val.to_integer();
|
auto b = b_val.to_integer();
|
||||||
auto a = clamp(lroundf(a_val.number_value() * 255.0), 0, 255);
|
auto a = clamp(lroundf(a_val.number_value() * 255.0f), 0, 255);
|
||||||
if (AK::is_within_range<u8>(r) && AK::is_within_range<u8>(g) && AK::is_within_range<u8>(b))
|
if (AK::is_within_range<u8>(r) && AK::is_within_range<u8>(g) && AK::is_within_range<u8>(b))
|
||||||
return Color(r, g, b, a);
|
return Color(r, g, b, a);
|
||||||
|
|
||||||
|
@ -2512,10 +2512,10 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
|
||||||
auto b = b_val.percentage();
|
auto b = b_val.percentage();
|
||||||
auto a = a_val.number_value();
|
auto a = a_val.number_value();
|
||||||
|
|
||||||
u8 r_255 = clamp(lroundf(r * 2.55), 0, 255);
|
u8 r_255 = clamp(lroundf(r * 2.55f), 0, 255);
|
||||||
u8 g_255 = clamp(lroundf(g * 2.55), 0, 255);
|
u8 g_255 = clamp(lroundf(g * 2.55f), 0, 255);
|
||||||
u8 b_255 = clamp(lroundf(b * 2.55), 0, 255);
|
u8 b_255 = clamp(lroundf(b * 2.55f), 0, 255);
|
||||||
u8 a_255 = clamp(lroundf(a * 255.0), 0, 255);
|
u8 a_255 = clamp(lroundf(a * 255.0f), 0, 255);
|
||||||
return Color(r_255, g_255, b_255, a_255);
|
return Color(r_255, g_255, b_255, a_255);
|
||||||
}
|
}
|
||||||
} else if (function.name().equals_ignoring_case("hsl")) {
|
} else if (function.name().equals_ignoring_case("hsl")) {
|
||||||
|
@ -2530,9 +2530,9 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
|
||||||
&& s_val.is(Token::Type::Percentage)
|
&& s_val.is(Token::Type::Percentage)
|
||||||
&& l_val.is(Token::Type::Percentage)) {
|
&& l_val.is(Token::Type::Percentage)) {
|
||||||
|
|
||||||
auto h = static_cast<float>(h_val.number_value());
|
auto h = h_val.number_value();
|
||||||
auto s = static_cast<float>(s_val.percentage() / 100.0f);
|
auto s = s_val.percentage() / 100.0f;
|
||||||
auto l = static_cast<float>(l_val.percentage() / 100.0f);
|
auto l = l_val.percentage() / 100.0f;
|
||||||
return Color::from_hsl(h, s, l);
|
return Color::from_hsl(h, s, l);
|
||||||
}
|
}
|
||||||
} else if (function.name().equals_ignoring_case("hsla")) {
|
} else if (function.name().equals_ignoring_case("hsla")) {
|
||||||
|
@ -2549,10 +2549,10 @@ Optional<Color> Parser::parse_color(StyleComponentValueRule const& component_val
|
||||||
&& l_val.is(Token::Type::Percentage)
|
&& l_val.is(Token::Type::Percentage)
|
||||||
&& a_val.is(Token::Type::Number)) {
|
&& a_val.is(Token::Type::Number)) {
|
||||||
|
|
||||||
auto h = static_cast<float>(h_val.number_value());
|
auto h = h_val.number_value();
|
||||||
auto s = static_cast<float>(s_val.percentage() / 100.0f);
|
auto s = s_val.percentage() / 100.0f;
|
||||||
auto l = static_cast<float>(l_val.percentage() / 100.0f);
|
auto l = l_val.percentage() / 100.0f;
|
||||||
auto a = static_cast<float>(a_val.number_value());
|
auto a = a_val.number_value();
|
||||||
return Color::from_hsla(h, s, l, a);
|
return Color::from_hsla(h, s, l, a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,7 +118,7 @@ public:
|
||||||
VERIFY(m_type == Type::Number);
|
VERIFY(m_type == Type::Number);
|
||||||
return m_value.view();
|
return m_value.view();
|
||||||
}
|
}
|
||||||
double number_value() const
|
float number_value() const
|
||||||
{
|
{
|
||||||
VERIFY(m_type == Type::Number);
|
VERIFY(m_type == Type::Number);
|
||||||
return m_number_value;
|
return m_number_value;
|
||||||
|
@ -135,14 +135,14 @@ public:
|
||||||
VERIFY(m_type == Type::Dimension);
|
VERIFY(m_type == Type::Dimension);
|
||||||
return m_unit.view();
|
return m_unit.view();
|
||||||
}
|
}
|
||||||
double dimension_value() const
|
float dimension_value() const
|
||||||
{
|
{
|
||||||
VERIFY(m_type == Type::Dimension);
|
VERIFY(m_type == Type::Dimension);
|
||||||
return m_number_value;
|
return m_number_value;
|
||||||
}
|
}
|
||||||
i64 dimension_value_int() const { return to_closest_integer(dimension_value()); }
|
i64 dimension_value_int() const { return to_closest_integer(dimension_value()); }
|
||||||
|
|
||||||
double percentage() const
|
float percentage() const
|
||||||
{
|
{
|
||||||
VERIFY(m_type == Type::Percentage);
|
VERIFY(m_type == Type::Percentage);
|
||||||
return m_number_value;
|
return m_number_value;
|
||||||
|
@ -165,22 +165,22 @@ public:
|
||||||
Position const& end_position() const { return m_end_position; }
|
Position const& end_position() const { return m_end_position; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static i64 to_closest_integer(double value)
|
static i64 to_closest_integer(float value)
|
||||||
{
|
{
|
||||||
// https://www.w3.org/TR/css-values-4/#numeric-types
|
// https://www.w3.org/TR/css-values-4/#numeric-types
|
||||||
// When a value cannot be explicitly supported due to range/precision limitations, it must be converted
|
// When a value cannot be explicitly supported due to range/precision limitations, it must be converted
|
||||||
// to the closest value supported by the implementation, but how the implementation defines "closest"
|
// to the closest value supported by the implementation, but how the implementation defines "closest"
|
||||||
// is explicitly undefined as well.
|
// is explicitly undefined as well.
|
||||||
return llround(value);
|
return llroundf(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
|
|
||||||
FlyString m_value;
|
FlyString m_value;
|
||||||
FlyString m_unit;
|
FlyString m_unit;
|
||||||
HashType m_hash_type { HashType::Unrestricted };
|
float m_number_value { 0 };
|
||||||
NumberType m_number_type { NumberType::Integer };
|
NumberType m_number_type { NumberType::Integer };
|
||||||
double m_number_value { 0 };
|
HashType m_hash_type { HashType::Unrestricted };
|
||||||
|
|
||||||
Position m_start_position;
|
Position m_start_position;
|
||||||
Position m_end_position;
|
Position m_end_position;
|
||||||
|
|
|
@ -557,7 +557,7 @@ CSSNumber Tokenizer::consume_a_number()
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.w3.org/TR/css-syntax-3/#convert-string-to-number
|
// https://www.w3.org/TR/css-syntax-3/#convert-string-to-number
|
||||||
double Tokenizer::convert_a_string_to_a_number(StringView string)
|
float Tokenizer::convert_a_string_to_a_number(StringView string)
|
||||||
{
|
{
|
||||||
auto code_point_at = [&](size_t index) -> u32 {
|
auto code_point_at = [&](size_t index) -> u32 {
|
||||||
if (index < string.length())
|
if (index < string.length())
|
||||||
|
@ -630,7 +630,7 @@ double Tokenizer::convert_a_string_to_a_number(StringView string)
|
||||||
VERIFY(position == string.length());
|
VERIFY(position == string.length());
|
||||||
|
|
||||||
// Return the number s·(i + f·10^-d)·10^te.
|
// Return the number s·(i + f·10^-d)·10^te.
|
||||||
return sign * (integer_part + fractional_part * pow(10, -fractional_digits)) * pow(10, exponent_sign * exponent);
|
return sign * (integer_part + fractional_part * powf(10, -fractional_digits)) * powf(10, exponent_sign * exponent);
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.w3.org/TR/css-syntax-3/#consume-name
|
// https://www.w3.org/TR/css-syntax-3/#consume-name
|
||||||
|
|
|
@ -60,7 +60,7 @@ public:
|
||||||
class CSSNumber {
|
class CSSNumber {
|
||||||
public:
|
public:
|
||||||
String string;
|
String string;
|
||||||
double value { 0 };
|
float value { 0 };
|
||||||
Token::NumberType type {};
|
Token::NumberType type {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ private:
|
||||||
[[nodiscard]] Token consume_a_numeric_token();
|
[[nodiscard]] Token consume_a_numeric_token();
|
||||||
[[nodiscard]] Token consume_an_ident_like_token();
|
[[nodiscard]] Token consume_an_ident_like_token();
|
||||||
[[nodiscard]] CSSNumber consume_a_number();
|
[[nodiscard]] CSSNumber consume_a_number();
|
||||||
[[nodiscard]] double convert_a_string_to_a_number(StringView);
|
[[nodiscard]] float convert_a_string_to_a_number(StringView);
|
||||||
[[nodiscard]] String consume_a_name();
|
[[nodiscard]] String consume_a_name();
|
||||||
[[nodiscard]] u32 consume_escaped_code_point();
|
[[nodiscard]] u32 consume_escaped_code_point();
|
||||||
[[nodiscard]] Token consume_a_url_token();
|
[[nodiscard]] Token consume_a_url_token();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue