mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
LibWeb: Implement CSS atan2()
This commit is contained in:
parent
1aa84dfddd
commit
a9a62ad8c9
Notes:
sideshowbarker
2024-07-17 07:25:39 +09:00
Author: https://github.com/stelar7 Commit: https://github.com/SerenityOS/serenity/commit/a9a62ad8c9 Pull-request: https://github.com/SerenityOS/serenity/pull/19165 Reviewed-by: https://github.com/AtkinsSJ ✅
4 changed files with 126 additions and 0 deletions
|
@ -3738,6 +3738,46 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_atan_function(Function const& fun
|
|||
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)
|
||||
{
|
||||
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))
|
||||
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());
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -301,6 +301,7 @@ private:
|
|||
ErrorOr<OwnPtr<CalculationNode>> parse_asin_function(Function const&);
|
||||
ErrorOr<OwnPtr<CalculationNode>> parse_acos_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_integer_value(TokenStream<ComponentValue>&);
|
||||
ErrorOr<RefPtr<StyleValue>> parse_number_value(TokenStream<ComponentValue>&);
|
||||
|
|
|
@ -1174,6 +1174,69 @@ ErrorOr<void> AtanCalculationNode::dump(StringBuilder& builder, int indent) cons
|
|||
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)
|
||||
{
|
||||
add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
|
||||
|
|
|
@ -504,4 +504,23 @@ private:
|
|||
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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue