From fa37bb8b766cd8b620cf8ab4c050b6c3631c883f Mon Sep 17 00:00:00 2001 From: stelar7 Date: Sun, 28 May 2023 11:43:04 +0200 Subject: [PATCH] LibWeb: Implement CSS `hypot()` --- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 50 ++++++++++++ Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 1 + .../CSS/StyleValues/CalculatedStyleValue.cpp | 76 +++++++++++++++++++ .../CSS/StyleValues/CalculatedStyleValue.h | 18 +++++ 4 files changed, 145 insertions(+) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 4be59f2e8c..88c57396ce 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -3844,6 +3844,53 @@ ErrorOr> Parser::parse_sqrt_function(Function const& fun return TRY(SqrtCalculationNode::create(node.release_nonnull())); } +ErrorOr> Parser::parse_hypot_function(Function const& function) +{ + TokenStream stream { function.values() }; + auto parameters = parse_a_comma_separated_list_of_component_values(stream); + + if (parameters.size() == 0) { + dbgln_if(CSS_PARSER_DEBUG, "hypot() must have at least 1 parameter"sv); + return nullptr; + } + + Vector> calculated_parameters; + calculated_parameters.ensure_capacity(parameters.size()); + + CalculatedStyleValue::ResolvedType type; + bool first = true; + for (auto& parameter : parameters) { + auto calculation_node = TRY(parse_a_calculation(parameter)); + + if (!calculation_node) { + dbgln_if(CSS_PARSER_DEBUG, "hypot() parameters must be valid calculations"sv); + return nullptr; + } + + auto maybe_parameter_type = calculation_node->resolved_type(); + if (!maybe_parameter_type.has_value()) { + dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for hypot() parameter #{}"sv, calculated_parameters.size() + 1); + return nullptr; + } + + auto resolved_type = maybe_parameter_type.value(); + + if (first) { + type = resolved_type; + first = false; + } + + if (resolved_type != type) { + dbgln_if(CSS_PARSER_DEBUG, "hypot() parameters must all be of the same type"sv); + return nullptr; + } + + calculated_parameters.append(calculation_node.release_nonnull()); + } + + return TRY(HypotCalculationNode::create(move(calculated_parameters))); +} + ErrorOr> Parser::parse_dynamic_value(ComponentValue const& component_value) { if (component_value.is_function()) { @@ -3915,6 +3962,9 @@ ErrorOr> Parser::parse_a_calc_function_node(Function con if (function.name().equals_ignoring_ascii_case("sqrt"sv)) return TRY(parse_sqrt_function(function)); + if (function.name().equals_ignoring_ascii_case("hypot"sv)) + return TRY(parse_hypot_function(function)); + dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name()); return nullptr; } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 6eec793496..d5d0a93588 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -304,6 +304,7 @@ private: ErrorOr> parse_atan2_function(Function const&); ErrorOr> parse_pow_function(Function const&); ErrorOr> parse_sqrt_function(Function const&); + ErrorOr> parse_hypot_function(Function const&); ErrorOr> parse_dimension_value(ComponentValue const&); ErrorOr> parse_integer_value(TokenStream&); ErrorOr> parse_number_value(TokenStream&); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp index 35166d698b..864c0ad6e0 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp @@ -1344,6 +1344,82 @@ ErrorOr SqrtCalculationNode::dump(StringBuilder& builder, int indent) cons return {}; } +ErrorOr> HypotCalculationNode::create(Vector> values) +{ + return adopt_nonnull_own_or_enomem(new (nothrow) HypotCalculationNode(move(values))); +} + +HypotCalculationNode::HypotCalculationNode(Vector> values) + : CalculationNode(Type::Hypot) + , m_values(move(values)) +{ +} + +HypotCalculationNode::~HypotCalculationNode() = default; + +ErrorOr HypotCalculationNode::to_string() const +{ + StringBuilder builder; + TRY(builder.try_append("hypot("sv)); + for (size_t i = 0; i < m_values.size(); ++i) { + if (i != 0) + TRY(builder.try_append(", "sv)); + TRY(builder.try_append(TRY(m_values[i]->to_string()))); + } + TRY(builder.try_append(")"sv)); + return builder.to_string(); +} + +Optional HypotCalculationNode::resolved_type() const +{ + // NOTE: We check during parsing that all values have the same type. + return m_values[0]->resolved_type(); +} + +bool HypotCalculationNode::contains_percentage() const +{ + for (auto const& value : m_values) { + if (value->contains_percentage()) + return true; + } + + return false; +} + +CalculatedStyleValue::CalculationResult HypotCalculationNode::resolve(Optional context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const +{ + double square_sum = 0.0; + + for (auto const& value : m_values) { + auto child_resolved = value->resolve(context, percentage_basis); + auto child_value = resolve_value(child_resolved.value(), context); + + square_sum += child_value * child_value; + } + + auto result = sqrt(square_sum); + + return to_resolved_type(resolved_type().value(), result); +} + +ErrorOr HypotCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) +{ + for (auto& value : m_values) { + TRY(value->for_each_child_node(callback)); + TRY(callback(value)); + } + + return {}; +} + +ErrorOr HypotCalculationNode::dump(StringBuilder& builder, int indent) const +{ + TRY(builder.try_appendff("{: >{}}HYPOT:\n", "", indent)); + for (auto const& value : m_values) + TRY(value->dump(builder, indent + 2)); + return {}; +} + void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional context, PercentageBasis const& percentage_basis) { add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h index 2c0a8782f4..151b47d20e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h @@ -568,4 +568,22 @@ private: NonnullOwnPtr m_value; }; +class HypotCalculationNode final : public CalculationNode { +public: + static ErrorOr> create(Vector>); + ~HypotCalculationNode(); + + virtual ErrorOr to_string() const override; + virtual Optional resolved_type() const override; + virtual bool contains_percentage() const override; + virtual CalculatedStyleValue::CalculationResult resolve(Optional, CalculatedStyleValue::PercentageBasis const&) const override; + virtual ErrorOr for_each_child_node(Function(NonnullOwnPtr&)> const&) override; + + virtual ErrorOr dump(StringBuilder&, int indent) const override; + +private: + explicit HypotCalculationNode(Vector>); + Vector> m_values; +}; + }