Browse Source

LibWeb: Implement CSS `atan2()`

stelar7 2 years ago
parent
commit
a9a62ad8c9

+ 43 - 0
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -3738,6 +3738,46 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_atan_function(Function const& fun
     return TRY(AtanCalculationNode::create(calculation_node.release_nonnull()));
     return TRY(AtanCalculationNode::create(calculation_node.release_nonnull()));
 }
 }
 
 
+ErrorOr<OwnPtr<CalculationNode>> Parser::parse_atan2_function(Function const& function)
+{
+    TokenStream stream { function.values() };
+    auto parameters = parse_a_comma_separated_list_of_component_values(stream);
+
+    if (parameters.size() != 2) {
+        dbgln_if(CSS_PARSER_DEBUG, "atan2() must have exactly two parameter"sv);
+        return nullptr;
+    }
+
+    auto node_a = TRY(parse_a_calculation(parameters[0]));
+    auto node_b = TRY(parse_a_calculation(parameters[1]));
+
+    if (!node_a || !node_b) {
+        dbgln_if(CSS_PARSER_DEBUG, "atan2() parameters must be valid calculations"sv);
+        return nullptr;
+    }
+
+    auto node_a_maybe_parameter_type = node_a->resolved_type();
+    if (!node_a_maybe_parameter_type.has_value()) {
+        dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for atan() parameter 1"sv);
+        return nullptr;
+    }
+
+    auto node_b_maybe_parameter_type = node_b->resolved_type();
+    if (!node_b_maybe_parameter_type.has_value()) {
+        dbgln_if(CSS_PARSER_DEBUG, "Failed to resolve type for atan() parameter 2"sv);
+        return nullptr;
+    }
+
+    auto node_a_resolved_type = node_a_maybe_parameter_type.value();
+    auto node_b_resolved_type = node_b_maybe_parameter_type.value();
+    if (node_a_resolved_type != node_b_resolved_type) {
+        dbgln_if(CSS_PARSER_DEBUG, "atan2() parameters must be of the same type"sv);
+        return nullptr;
+    }
+
+    return TRY(Atan2CalculationNode::create(node_a.release_nonnull(), node_b.release_nonnull()));
+}
+
 ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
 ErrorOr<RefPtr<StyleValue>> Parser::parse_dynamic_value(ComponentValue const& component_value)
 {
 {
     if (component_value.is_function()) {
     if (component_value.is_function()) {
@@ -3800,6 +3840,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
     if (function.name().equals_ignoring_ascii_case("atan"sv))
     if (function.name().equals_ignoring_ascii_case("atan"sv))
         return TRY(parse_atan_function(function));
         return TRY(parse_atan_function(function));
 
 
+    if (function.name().equals_ignoring_ascii_case("atan2"sv))
+        return TRY(parse_atan2_function(function));
+
     dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
     dbgln_if(CSS_PARSER_DEBUG, "We didn't implement `{}` function yet", function.name());
     return nullptr;
     return nullptr;
 }
 }

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

@@ -301,6 +301,7 @@ private:
     ErrorOr<OwnPtr<CalculationNode>> parse_asin_function(Function const&);
     ErrorOr<OwnPtr<CalculationNode>> parse_asin_function(Function const&);
     ErrorOr<OwnPtr<CalculationNode>> parse_acos_function(Function const&);
     ErrorOr<OwnPtr<CalculationNode>> parse_acos_function(Function const&);
     ErrorOr<OwnPtr<CalculationNode>> parse_atan_function(Function const&);
     ErrorOr<OwnPtr<CalculationNode>> parse_atan_function(Function const&);
+    ErrorOr<OwnPtr<CalculationNode>> parse_atan2_function(Function const&);
     ErrorOr<RefPtr<StyleValue>> parse_dimension_value(ComponentValue const&);
     ErrorOr<RefPtr<StyleValue>> parse_dimension_value(ComponentValue const&);
     ErrorOr<RefPtr<StyleValue>> parse_integer_value(TokenStream<ComponentValue>&);
     ErrorOr<RefPtr<StyleValue>> parse_integer_value(TokenStream<ComponentValue>&);
     ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&);
     ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&);

+ 63 - 0
Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.cpp

@@ -1174,6 +1174,69 @@ ErrorOr<void> AtanCalculationNode::dump(StringBuilder& builder, int indent) cons
     return {};
     return {};
 }
 }
 
 
+ErrorOr<NonnullOwnPtr<Atan2CalculationNode>> Atan2CalculationNode::create(NonnullOwnPtr<CalculationNode> y, NonnullOwnPtr<CalculationNode> x)
+{
+    return adopt_nonnull_own_or_enomem(new (nothrow) Atan2CalculationNode(move(y), move(x)));
+}
+
+Atan2CalculationNode::Atan2CalculationNode(NonnullOwnPtr<CalculationNode> y, NonnullOwnPtr<CalculationNode> x)
+    : CalculationNode(Type::Atan2)
+    , m_y(move(y))
+    , m_x(move(x))
+{
+}
+
+Atan2CalculationNode::~Atan2CalculationNode() = default;
+
+ErrorOr<String> Atan2CalculationNode::to_string() const
+{
+    StringBuilder builder;
+    builder.append("atan2("sv);
+    builder.append(TRY(m_y->to_string()));
+    builder.append(", "sv);
+    builder.append(TRY(m_x->to_string()));
+    builder.append(")"sv);
+    return builder.to_string();
+}
+
+Optional<CalculatedStyleValue::ResolvedType> Atan2CalculationNode::resolved_type() const
+{
+    return CalculatedStyleValue::ResolvedType::Angle;
+}
+
+bool Atan2CalculationNode::contains_percentage() const
+{
+    return m_y->contains_percentage() || m_x->contains_percentage();
+}
+
+CalculatedStyleValue::CalculationResult Atan2CalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
+{
+    auto node_a = m_y->resolve(context, percentage_basis);
+    auto node_a_value = resolve_value(node_a.value(), context);
+
+    auto node_b = m_x->resolve(context, percentage_basis);
+    auto node_b_value = resolve_value(node_b.value(), context);
+
+    auto result = atan2(node_a_value, node_b_value);
+
+    return { Angle(result, Angle::Type::Rad) };
+}
+
+ErrorOr<void> Atan2CalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
+{
+    TRY(m_y->for_each_child_node(callback));
+    TRY(m_x->for_each_child_node(callback));
+    TRY(callback(m_y));
+    TRY(callback(m_x));
+    return {};
+}
+
+ErrorOr<void> Atan2CalculationNode::dump(StringBuilder& builder, int indent) const
+{
+    TRY(builder.try_appendff("{: >{}}ATAN2: {}\n", "", indent, TRY(to_string())));
+    return {};
+}
+
 void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
 void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
 {
 {
     add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
     add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);

+ 19 - 0
Userland/Libraries/LibWeb/CSS/StyleValues/CalculatedStyleValue.h

@@ -504,4 +504,23 @@ private:
     NonnullOwnPtr<CalculationNode> m_value;
     NonnullOwnPtr<CalculationNode> m_value;
 };
 };
 
 
+class Atan2CalculationNode final : public CalculationNode {
+public:
+    static ErrorOr<NonnullOwnPtr<Atan2CalculationNode>> create(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
+    ~Atan2CalculationNode();
+
+    virtual ErrorOr<String> to_string() const override;
+    virtual Optional<CalculatedStyleValue::ResolvedType> resolved_type() const override;
+    virtual bool contains_percentage() const override;
+    virtual CalculatedStyleValue::CalculationResult resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const override;
+    virtual ErrorOr<void> for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const&) override;
+
+    virtual ErrorOr<void> dump(StringBuilder&, int indent) const override;
+
+private:
+    Atan2CalculationNode(NonnullOwnPtr<CalculationNode>, NonnullOwnPtr<CalculationNode>);
+    NonnullOwnPtr<CalculationNode> m_y;
+    NonnullOwnPtr<CalculationNode> m_x;
+};
+
 }
 }