mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 13:17:36 +00:00
LibWeb: Implement CSS sin()
This commit is contained in:
parent
ba7af82c5c
commit
c73f476915
6 changed files with 125 additions and 0 deletions
|
@ -52,6 +52,11 @@ double Angle::to_degrees() const
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double Angle::to_radians() const
|
||||||
|
{
|
||||||
|
return to_degrees() * (AK::Pi<double> / 180.0);
|
||||||
|
}
|
||||||
|
|
||||||
StringView Angle::unit_name() const
|
StringView Angle::unit_name() const
|
||||||
{
|
{
|
||||||
switch (m_type) {
|
switch (m_type) {
|
||||||
|
|
|
@ -28,7 +28,9 @@ public:
|
||||||
Angle percentage_of(Percentage const&) const;
|
Angle percentage_of(Percentage const&) const;
|
||||||
|
|
||||||
ErrorOr<String> to_string() const;
|
ErrorOr<String> to_string() const;
|
||||||
|
|
||||||
double to_degrees() const;
|
double to_degrees() const;
|
||||||
|
double to_radians() const;
|
||||||
|
|
||||||
Type type() const { return m_type; }
|
Type type() const { return m_type; }
|
||||||
double raw_value() const { return m_value; }
|
double raw_value() const { return m_value; }
|
||||||
|
|
|
@ -3594,6 +3594,30 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_sign_function(Function const& fun
|
||||||
return TRY(SignCalculationNode::create(calculation_node.release_nonnull()));
|
return TRY(SignCalculationNode::create(calculation_node.release_nonnull()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> Parser::parse_sin_function(Function const& function)
|
||||||
|
{
|
||||||
|
auto calculation_node = TRY(parse_a_calculation(function.values()));
|
||||||
|
|
||||||
|
if (!calculation_node) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "sin() parameter must be a valid calculation"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 sin() parameter"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto resolved_type = maybe_parameter_type.value();
|
||||||
|
if (resolved_type != CalculatedStyleValue::ResolvedType::Number && resolved_type != CalculatedStyleValue::ResolvedType::Angle) {
|
||||||
|
dbgln_if(CSS_PARSER_DEBUG, "sin() parameter must be a number or angle"sv);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRY(SinCalculationNode::create(calculation_node.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()) {
|
||||||
|
@ -3638,6 +3662,9 @@ ErrorOr<OwnPtr<CalculationNode>> Parser::parse_a_calc_function_node(Function con
|
||||||
if (function.name().equals_ignoring_ascii_case("sign"sv))
|
if (function.name().equals_ignoring_ascii_case("sign"sv))
|
||||||
return TRY(parse_sign_function(function));
|
return TRY(parse_sign_function(function));
|
||||||
|
|
||||||
|
if (function.name().equals_ignoring_ascii_case("sin"sv))
|
||||||
|
return TRY(parse_sin_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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -295,6 +295,7 @@ private:
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_clamp_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_clamp_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_abs_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_abs_function(Function const&);
|
||||||
ErrorOr<OwnPtr<CalculationNode>> parse_sign_function(Function const&);
|
ErrorOr<OwnPtr<CalculationNode>> parse_sign_function(Function const&);
|
||||||
|
ErrorOr<OwnPtr<CalculationNode>> parse_sin_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>&);
|
||||||
|
|
|
@ -24,6 +24,14 @@ static bool is_dimension(CalculatedStyleValue::ResolvedType type)
|
||||||
&& type != CalculatedStyleValue::ResolvedType::Percentage;
|
&& type != CalculatedStyleValue::ResolvedType::Percentage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static double resolve_value_radians(CalculatedStyleValue::CalculationResult::Value value)
|
||||||
|
{
|
||||||
|
return value.visit(
|
||||||
|
[](Number const& number) { return number.value(); },
|
||||||
|
[](Angle const& angle) { return angle.to_radians(); },
|
||||||
|
[](auto const&) { VERIFY_NOT_REACHED(); return 0.0; });
|
||||||
|
};
|
||||||
|
|
||||||
static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Optional<Length::ResolutionContext const&> context)
|
static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Optional<Length::ResolutionContext const&> context)
|
||||||
{
|
{
|
||||||
return value.visit(
|
return value.visit(
|
||||||
|
@ -842,6 +850,60 @@ ErrorOr<void> ConstantCalculationNode::dump(StringBuilder& builder, int indent)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullOwnPtr<SinCalculationNode>> SinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_own_or_enomem(new (nothrow) SinCalculationNode(move(value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
SinCalculationNode::SinCalculationNode(NonnullOwnPtr<CalculationNode> value)
|
||||||
|
: CalculationNode(Type::Sin)
|
||||||
|
, m_value(move(value))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
SinCalculationNode::~SinCalculationNode() = default;
|
||||||
|
|
||||||
|
ErrorOr<String> SinCalculationNode::to_string() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
builder.append("sin("sv);
|
||||||
|
builder.append(TRY(m_value->to_string()));
|
||||||
|
builder.append(")"sv);
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<CalculatedStyleValue::ResolvedType> SinCalculationNode::resolved_type() const
|
||||||
|
{
|
||||||
|
return CalculatedStyleValue::ResolvedType::Number;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SinCalculationNode::contains_percentage() const
|
||||||
|
{
|
||||||
|
return m_value->contains_percentage();
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculatedStyleValue::CalculationResult SinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
|
||||||
|
{
|
||||||
|
auto node_a = m_value->resolve(context, percentage_basis);
|
||||||
|
auto node_a_value = resolve_value_radians(node_a.value());
|
||||||
|
auto result = sin(node_a_value);
|
||||||
|
|
||||||
|
return { Number(Number::Type::Number, result) };
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> SinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
|
||||||
|
{
|
||||||
|
TRY(m_value->for_each_child_node(callback));
|
||||||
|
TRY(callback(m_value));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> SinCalculationNode::dump(StringBuilder& builder, int indent) const
|
||||||
|
{
|
||||||
|
TRY(builder.try_appendff("{: >{}}SIN: {}\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);
|
||||||
|
|
|
@ -152,6 +152,16 @@ public:
|
||||||
// https://drafts.csswg.org/css-values-4/#calc-constants
|
// https://drafts.csswg.org/css-values-4/#calc-constants
|
||||||
Constant,
|
Constant,
|
||||||
|
|
||||||
|
// Trigonometric functions, a sub-type of operator node
|
||||||
|
// https://drafts.csswg.org/css-values-4/#trig-funcs
|
||||||
|
Sin,
|
||||||
|
Cos,
|
||||||
|
Tan,
|
||||||
|
Asin,
|
||||||
|
Acos,
|
||||||
|
Atan,
|
||||||
|
Atan2,
|
||||||
|
|
||||||
// This only exists during parsing.
|
// This only exists during parsing.
|
||||||
Unparsed,
|
Unparsed,
|
||||||
};
|
};
|
||||||
|
@ -386,4 +396,22 @@ private:
|
||||||
CalculationNode::ConstantType m_constant;
|
CalculationNode::ConstantType m_constant;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SinCalculationNode final : public CalculationNode {
|
||||||
|
public:
|
||||||
|
static ErrorOr<NonnullOwnPtr<SinCalculationNode>> create(NonnullOwnPtr<CalculationNode>);
|
||||||
|
~SinCalculationNode();
|
||||||
|
|
||||||
|
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:
|
||||||
|
SinCalculationNode(NonnullOwnPtr<CalculationNode>);
|
||||||
|
NonnullOwnPtr<CalculationNode> m_value;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue