1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

LibWeb: Distinguish between integer and float in NumericStyleValue

We have this information when parsing, and some properties specifically
only allow integers, so it makes sense to keep that around.
This commit is contained in:
Sam Atkins 2021-10-19 16:43:56 +01:00 committed by Andreas Kling
parent 450b782c18
commit 78e57096e2
Notes: sideshowbarker 2024-07-18 02:09:44 +09:00
7 changed files with 55 additions and 30 deletions

View file

@ -2060,11 +2060,11 @@ RefPtr<StyleValue> Parser::parse_numeric_value(ParsingContext const&, StyleCompo
if (component_value.is(Token::Type::Number)) {
auto number = component_value.token();
if (number.m_number_type == Token::NumberType::Integer) {
return NumericStyleValue::create(number.to_integer());
return NumericStyleValue::create_integer(number.to_integer());
} else {
auto float_value = try_parse_float(number.m_value.string_view());
if (float_value.has_value())
return NumericStyleValue::create(float_value.value());
return NumericStyleValue::create_float(float_value.value());
}
}
@ -2626,11 +2626,11 @@ RefPtr<StyleValue> Parser::parse_flex_value(ParsingContext const& context, Vecto
switch (value->to_identifier()) {
case ValueID::Auto: {
auto one = NumericStyleValue::create(1);
auto one = NumericStyleValue::create_integer(1);
return FlexStyleValue::create(one, one, IdentifierStyleValue::create(ValueID::Auto));
}
case ValueID::None: {
auto zero = NumericStyleValue::create(0);
auto zero = NumericStyleValue::create_integer(0);
return FlexStyleValue::create(zero, zero, IdentifierStyleValue::create(ValueID::Auto));
}
default:

View file

@ -105,7 +105,7 @@ public:
VERIFY(m_type == Type::Number);
return m_value.string_view();
}
int to_integer() const
i64 to_integer() const
{
VERIFY(m_type == Type::Number && m_number_type == NumberType::Integer);
return number_string_value().to_int().value();
@ -122,7 +122,7 @@ public:
VERIFY(m_type == Type::Dimension);
return m_value.string_view();
}
int dimension_value_int() const { return dimension_value().to_int().value(); }
i64 dimension_value_int() const { return dimension_value().to_int().value(); }
NumberType number_type() const
{

View file

@ -469,7 +469,7 @@ RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout:
auto maybe_z_index = layout_node.computed_values().z_index();
if (!maybe_z_index.has_value())
return {};
return NumericStyleValue::create(maybe_z_index.release_value());
return NumericStyleValue::create_integer(maybe_z_index.release_value());
}
case CSS::PropertyID::TextAlign:
return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().text_align()));
@ -501,19 +501,19 @@ RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout:
auto maybe_grow_factor = layout_node.computed_values().flex_grow_factor();
if (!maybe_grow_factor.has_value())
return {};
return NumericStyleValue::create(maybe_grow_factor.release_value());
return NumericStyleValue::create_float(maybe_grow_factor.release_value());
}
case CSS::PropertyID::FlexShrink: {
auto maybe_shrink_factor = layout_node.computed_values().flex_shrink_factor();
if (!maybe_shrink_factor.has_value())
return {};
return NumericStyleValue::create(maybe_shrink_factor.release_value());
return NumericStyleValue::create_float(maybe_shrink_factor.release_value());
}
case CSS::PropertyID::Opacity: {
auto maybe_opacity = layout_node.computed_values().opacity();
if (!maybe_opacity.has_value())
return {};
return NumericStyleValue::create(maybe_opacity.release_value());
return NumericStyleValue::create_float(maybe_opacity.release_value());
}
case CSS::PropertyID::JustifyContent:
return IdentifierStyleValue::create(to_css_value_id(layout_node.computed_values().justify_content()));

View file

@ -665,8 +665,8 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
default:
break;
}
} else if (font_weight->is_numeric()) {
int font_weight_integer = roundf(static_cast<NumericStyleValue const&>(*font_weight).value());
} else if (font_weight->has_integer()) {
int font_weight_integer = font_weight->to_integer();
if (font_weight_integer <= Gfx::FontWeight::Regular)
weight = Gfx::FontWeight::Regular;
else if (font_weight_integer <= Gfx::FontWeight::Bold)

View file

@ -116,8 +116,8 @@ Optional<int> StyleProperties::z_index() const
if (value->has_auto())
return 0;
if (value->is_numeric())
return value->as_numeric().int_value();
if (value->has_integer())
return value->to_integer();
return {};
}

View file

@ -333,11 +333,13 @@ public:
virtual bool has_identifier() const { return false; }
virtual bool has_length() const { return false; }
virtual bool has_number() const { return false; }
virtual bool has_integer() const { return false; }
virtual Color to_color(Layout::NodeWithStyle const&) const { return {}; }
virtual CSS::ValueID to_identifier() const { return ValueID::Invalid; }
virtual Length to_length() const { return {}; }
virtual float to_number() const { return {}; }
virtual float to_number() const { return 0; }
virtual float to_integer() const { return 0; }
virtual String to_string() const = 0;
bool operator==(StyleValue const& other) const { return equals(other); }
@ -1021,37 +1023,60 @@ private:
class NumericStyleValue : public StyleValue {
public:
static NonnullRefPtr<NumericStyleValue> create(float value)
static NonnullRefPtr<NumericStyleValue> create_float(float value)
{
return adopt_ref(*new NumericStyleValue(value));
}
virtual bool has_length() const override { return m_value == 0; }
static NonnullRefPtr<NumericStyleValue> create_integer(i64 value)
{
return adopt_ref(*new NumericStyleValue(value));
}
virtual bool has_length() const override { return to_number() == 0; }
virtual Length to_length() const override { return Length(0, Length::Type::Px); }
virtual bool has_number() const override { return true; }
virtual float to_number() const override { return m_value; }
virtual float to_number() const override
{
return m_value.visit(
[](float value) { return value; },
[](i64 value) { return (float)value; });
}
float value() const { return m_value; }
// FIXME: Store integer values separately
i64 int_value() const { return roundf(m_value); }
String to_string() const override { return String::formatted("{}", m_value); }
virtual bool has_integer() const override { return m_value.has<i64>(); }
virtual float to_integer() const override { return m_value.get<i64>(); }
String to_string() const override
{
return m_value.visit(
[](float value) {
return String::formatted("{}", value);
},
[](i64 value) {
return String::formatted("{}", value);
});
}
virtual bool equals(StyleValue const& other) const override
{
if (type() != other.type())
return false;
return m_value == static_cast<NumericStyleValue const&>(other).m_value;
if (has_integer() != other.has_integer())
return false;
if (has_integer())
return m_value.get<i64>() == static_cast<NumericStyleValue const&>(other).m_value.get<i64>();
return m_value.get<float>() == static_cast<NumericStyleValue const&>(other).m_value.get<float>();
}
private:
explicit NumericStyleValue(float value)
explicit NumericStyleValue(Variant<float, i64> value)
: StyleValue(Type::Numeric)
, m_value(value)
, m_value(move(value))
{
}
float m_value { 0 };
Variant<float, i64> m_value { (i64)0 };
};
class OverflowStyleValue final : public StyleValue {

View file

@ -301,22 +301,22 @@ RefPtr<CSS::StyleValue> Window::query_media_feature(FlyString const& name) const
return CSS::IdentifierStyleValue::create(CSS::ValueID::Fine);
// FIXME: aspect-ratio
if (name.equals_ignoring_case("color"sv))
return CSS::NumericStyleValue::create(32);
return CSS::NumericStyleValue::create_integer(32);
if (name.equals_ignoring_case("color-gamut"sv))
return CSS::IdentifierStyleValue::create(CSS::ValueID::Srgb);
if (name.equals_ignoring_case("color-index"sv))
return CSS::NumericStyleValue::create(0);
return CSS::NumericStyleValue::create_integer(0);
// FIXME: device-aspect-ratio
// FIXME: device-height
// FIXME: device-width
if (name.equals_ignoring_case("grid"sv))
return CSS::NumericStyleValue::create(0);
return CSS::NumericStyleValue::create_integer(0);
if (name.equals_ignoring_case("height"sv))
return CSS::LengthStyleValue::create(CSS::Length::make_px(inner_height()));
if (name.equals_ignoring_case("hover"sv))
return CSS::IdentifierStyleValue::create(CSS::ValueID::Hover);
if (name.equals_ignoring_case("monochrome"sv))
return CSS::NumericStyleValue::create(0);
return CSS::NumericStyleValue::create_integer(0);
if (name.equals_ignoring_case("hover"sv))
return CSS::IdentifierStyleValue::create(inner_height() >= inner_width() ? CSS::ValueID::Portrait : CSS::ValueID::Landscape);
if (name.equals_ignoring_case("overflow-block"sv))