1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 19:47:42 +00:00

LibWeb: Remove CalculatedStyleValue from Angle

...and replace it with AngleOrCalculated.

This has the nice bonus effect of actually handling `calc()` for angles
in a transform function. :^) (Previously we just would have asserted.)
This commit is contained in:
Sam Atkins 2023-03-30 15:33:37 +01:00 committed by Andreas Kling
parent fa90a3bb4f
commit 7a1a97f153
6 changed files with 9 additions and 37 deletions

View file

@ -1,12 +1,12 @@
/* /*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include "Angle.h" #include "Angle.h"
#include <AK/Math.h> #include <AK/Math.h>
#include <LibWeb/CSS/StyleValue.h> #include <LibWeb/CSS/Percentage.h>
namespace Web::CSS { namespace Web::CSS {
@ -22,13 +22,6 @@ Angle::Angle(float value, Type type)
{ {
} }
Angle Angle::make_calculated(NonnullRefPtr<CalculatedStyleValue> calculated_style_value)
{
Angle angle { 0, Type::Calculated };
angle.m_calculated_style = move(calculated_style_value);
return angle;
}
Angle Angle::make_degrees(float value) Angle Angle::make_degrees(float value)
{ {
return { value, Type::Deg }; return { value, Type::Deg };
@ -36,23 +29,17 @@ Angle Angle::make_degrees(float value)
Angle Angle::percentage_of(Percentage const& percentage) const Angle Angle::percentage_of(Percentage const& percentage) const
{ {
VERIFY(!is_calculated());
return Angle { percentage.as_fraction() * m_value, m_type }; return Angle { percentage.as_fraction() * m_value, m_type };
} }
ErrorOr<String> Angle::to_string() const ErrorOr<String> Angle::to_string() const
{ {
if (is_calculated())
return m_calculated_style->to_string();
return String::formatted("{}{}", m_value, unit_name()); return String::formatted("{}{}", m_value, unit_name());
} }
float Angle::to_degrees() const float Angle::to_degrees() const
{ {
switch (m_type) { switch (m_type) {
case Type::Calculated:
return m_calculated_style->resolve_angle()->to_degrees();
case Type::Deg: case Type::Deg:
return m_value; return m_value;
case Type::Grad: case Type::Grad:
@ -68,8 +55,6 @@ float Angle::to_degrees() const
StringView Angle::unit_name() const StringView Angle::unit_name() const
{ {
switch (m_type) { switch (m_type) {
case Type::Calculated:
return "calculated"sv;
case Type::Deg: case Type::Deg:
return "deg"sv; return "deg"sv;
case Type::Grad: case Type::Grad:
@ -99,10 +84,4 @@ Optional<Angle::Type> Angle::unit_from_name(StringView name)
return {}; return {};
} }
NonnullRefPtr<CalculatedStyleValue> Angle::calculated_style_value() const
{
VERIFY(!m_calculated_style.is_null());
return *m_calculated_style;
}
} }

View file

@ -1,12 +1,11 @@
/* /*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#pragma once #pragma once
#include <AK/RefPtr.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
@ -15,7 +14,6 @@ namespace Web::CSS {
class Angle { class Angle {
public: public:
enum class Type { enum class Type {
Calculated,
Deg, Deg,
Grad, Grad,
Rad, Rad,
@ -26,20 +24,14 @@ public:
Angle(int value, Type type); Angle(int value, Type type);
Angle(float value, Type type); Angle(float value, Type type);
static Angle make_calculated(NonnullRefPtr<CalculatedStyleValue>);
static Angle make_degrees(float); static Angle make_degrees(float);
Angle percentage_of(Percentage const&) const; Angle percentage_of(Percentage const&) const;
bool is_calculated() const { return m_type == Type::Calculated; }
NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
ErrorOr<String> to_string() const; ErrorOr<String> to_string() const;
float to_degrees() const; float to_degrees() const;
bool operator==(Angle const& other) const bool operator==(Angle const& other) const
{ {
if (is_calculated())
return m_calculated_style == other.m_calculated_style;
return m_type == other.m_type && m_value == other.m_value; return m_type == other.m_type && m_value == other.m_value;
} }
@ -48,7 +40,6 @@ private:
Type m_type; Type m_type;
float m_value { 0 }; float m_value { 0 };
RefPtr<CalculatedStyleValue> m_calculated_style;
}; };
} }

View file

@ -105,7 +105,7 @@ public:
float width { 0 }; float width { 0 };
}; };
using TransformValue = Variant<CSS::Angle, CSS::LengthPercentage, float>; using TransformValue = Variant<CSS::AngleOrCalculated, CSS::LengthPercentage, float>;
struct Transformation { struct Transformation {
CSS::TransformFunction function; CSS::TransformFunction function;

View file

@ -20,6 +20,7 @@
#include <LibWeb/CSS/CSSStyleRule.h> #include <LibWeb/CSS/CSSStyleRule.h>
#include <LibWeb/CSS/CSSStyleSheet.h> #include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/CSS/CSSSupportsRule.h> #include <LibWeb/CSS/CSSSupportsRule.h>
#include <LibWeb/CSS/CalculatedOr.h>
#include <LibWeb/CSS/MediaList.h> #include <LibWeb/CSS/MediaList.h>
#include <LibWeb/CSS/Parser/Block.h> #include <LibWeb/CSS/Parser/Block.h>
#include <LibWeb/CSS/Parser/ComponentValue.h> #include <LibWeb/CSS/Parser/ComponentValue.h>
@ -5780,7 +5781,7 @@ RefPtr<StyleValue> Parser::parse_transform_value(Vector<ComponentValue> const& c
case TransformFunctionParameterType::Angle: { case TransformFunctionParameterType::Angle: {
// These are `<angle> | <zero>` in the spec, so we have to check for both kinds. // These are `<angle> | <zero>` in the spec, so we have to check for both kinds.
if (maybe_calc_value && maybe_calc_value->resolves_to_angle()) { if (maybe_calc_value && maybe_calc_value->resolves_to_angle()) {
values.append(AngleStyleValue::create(Angle::make_calculated(maybe_calc_value.release_nonnull()))); values.append(maybe_calc_value.release_nonnull());
} else if (value.is(Token::Type::Number) && value.token().number_value() == 0) { } else if (value.is(Token::Type::Number) && value.token().number_value() == 0) {
values.append(AngleStyleValue::create(Angle::make_degrees(0))); values.append(AngleStyleValue::create(Angle::make_degrees(0)));
} else { } else {

View file

@ -23,6 +23,7 @@
#include <AK/WeakPtr.h> #include <AK/WeakPtr.h>
#include <LibGfx/Painter.h> #include <LibGfx/Painter.h>
#include <LibWeb/CSS/Angle.h> #include <LibWeb/CSS/Angle.h>
#include <LibWeb/CSS/CalculatedOr.h>
#include <LibWeb/CSS/Enums.h> #include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/Frequency.h> #include <LibWeb/CSS/Frequency.h>
#include <LibWeb/CSS/Length.h> #include <LibWeb/CSS/Length.h>

View file

@ -239,8 +239,8 @@ Gfx::FloatMatrix4x4 StackingContext::get_transformation_matrix(CSS::Transformati
return value.length().to_px(m_box).value(); return value.length().to_px(m_box).value();
}, },
[](CSS::Angle const& value) { [this](CSS::AngleOrCalculated const& value) {
return value.to_degrees() * static_cast<float>(M_DEG2RAD); return value.resolved(m_box).to_degrees() * static_cast<float>(M_DEG2RAD);
}, },
[](float value) { [](float value) {
return value; return value;