/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Tobias Christiansen * Copyright (c) 2021-2023, Sam Atkins * Copyright (c) 2022-2023, MacDue * * SPDX-License-Identifier: BSD-2-Clause */ #include "CalculatedStyleValue.h" #include namespace Web::CSS { static bool is_number(CalculatedStyleValue::ResolvedType type) { return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer; } static bool is_dimension(CalculatedStyleValue::ResolvedType type) { return type != CalculatedStyleValue::ResolvedType::Number && type != CalculatedStyleValue::ResolvedType::Integer && type != CalculatedStyleValue::ResolvedType::Percentage; } CalculationNode::CalculationNode(Type type) : m_type(type) { } CalculationNode::~CalculationNode() = default; ErrorOr> NumericCalculationNode::create(NumericValue value) { return adopt_nonnull_own_or_enomem(new (nothrow) NumericCalculationNode(move(value))); } NumericCalculationNode::NumericCalculationNode(NumericValue value) : CalculationNode(Type::Numeric) , m_value(move(value)) { } NumericCalculationNode::~NumericCalculationNode() = default; ErrorOr NumericCalculationNode::to_string() const { return m_value.visit([](auto& value) { return value.to_string(); }); } Optional NumericCalculationNode::resolved_type() const { return m_value.visit( [](Number const&) { return CalculatedStyleValue::ResolvedType::Number; }, [](Angle const&) { return CalculatedStyleValue::ResolvedType::Angle; }, [](Frequency const&) { return CalculatedStyleValue::ResolvedType::Frequency; }, [](Length const&) { return CalculatedStyleValue::ResolvedType::Length; }, [](Percentage const&) { return CalculatedStyleValue::ResolvedType::Percentage; }, [](Time const&) { return CalculatedStyleValue::ResolvedType::Time; }); } bool NumericCalculationNode::contains_percentage() const { return m_value.has(); } CalculatedStyleValue::CalculationResult NumericCalculationNode::resolve(Layout::Node const*, CalculatedStyleValue::PercentageBasis const&) const { return m_value; } ErrorOr NumericCalculationNode::dump(StringBuilder& builder, int indent) const { return builder.try_appendff("{: >{}}NUMERIC({})\n", "", indent, TRY(m_value.visit([](auto& it) { return it.to_string(); }))); } ErrorOr> SumCalculationNode::create(Vector> values) { return adopt_nonnull_own_or_enomem(new (nothrow) SumCalculationNode(move(values))); } SumCalculationNode::SumCalculationNode(Vector> values) : CalculationNode(Type::Sum) , m_values(move(values)) { VERIFY(!m_values.is_empty()); } SumCalculationNode::~SumCalculationNode() = default; ErrorOr SumCalculationNode::to_string() const { bool first = true; StringBuilder builder; for (auto& value : m_values) { if (!first) TRY(builder.try_append(" + "sv)); TRY(builder.try_append(TRY(value->to_string()))); first = false; } return builder.to_string(); } Optional SumCalculationNode::resolved_type() const { // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation // For now, this is just ad-hoc, based on the old implementation. Optional type; for (auto const& value : m_values) { auto maybe_value_type = value->resolved_type(); if (!maybe_value_type.has_value()) return {}; auto value_type = maybe_value_type.value(); if (!type.has_value()) { type = value_type; continue; } // At + or -, check that both sides have the same type, or that one side is a and the other is an . // If both sides are the same type, resolve to that type. if (value_type == type) continue; // If one side is a and the other is an , resolve to . if (is_number(*type) && is_number(value_type)) { type = CalculatedStyleValue::ResolvedType::Number; continue; } // FIXME: calc() handles by allowing them to pretend to be whatever type is allowed at this location. // Since we can't easily check what that type is, we just allow to combine with any other type. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(value_type)) { type = value_type; continue; } if (is_dimension(*type) && value_type == CalculatedStyleValue::ResolvedType::Percentage) continue; return {}; } return type; } bool SumCalculationNode::contains_percentage() const { for (auto const& value : m_values) { if (value->contains_percentage()) return true; } return false; } CalculatedStyleValue::CalculationResult SumCalculationNode::resolve(Layout::Node const* layout_node, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { Optional total; for (auto& additional_product : m_values) { auto additional_value = additional_product->resolve(layout_node, percentage_basis); if (!total.has_value()) { total = additional_value; continue; } total->add(additional_value, layout_node, percentage_basis); } return total.value(); } ErrorOr SumCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { for (auto& item : m_values) { TRY(item->for_each_child_node(callback)); TRY(callback(item)); } return {}; } ErrorOr SumCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}SUM:\n", "", indent)); for (auto const& item : m_values) TRY(item->dump(builder, indent + 2)); return {}; } ErrorOr> ProductCalculationNode::create(Vector> values) { return adopt_nonnull_own_or_enomem(new (nothrow) ProductCalculationNode(move(values))); } ProductCalculationNode::ProductCalculationNode(Vector> values) : CalculationNode(Type::Product) , m_values(move(values)) { VERIFY(!m_values.is_empty()); } ProductCalculationNode::~ProductCalculationNode() = default; ErrorOr ProductCalculationNode::to_string() const { bool first = true; StringBuilder builder; for (auto& value : m_values) { if (!first) TRY(builder.try_append(" * "sv)); TRY(builder.try_append(TRY(value->to_string()))); first = false; } return builder.to_string(); } Optional ProductCalculationNode::resolved_type() const { // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation // For now, this is just ad-hoc, based on the old implementation. Optional type; for (auto const& value : m_values) { auto maybe_value_type = value->resolved_type(); if (!maybe_value_type.has_value()) return {}; auto value_type = maybe_value_type.value(); if (!type.has_value()) { type = value_type; continue; } // At *, check that at least one side is . if (!(is_number(*type) || is_number(value_type))) return {}; // If both sides are , resolve to . if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) { type = CalculatedStyleValue::ResolvedType::Integer; } else { // Otherwise, resolve to the type of the other side. if (is_number(*type)) type = value_type; } } return type; } bool ProductCalculationNode::contains_percentage() const { for (auto const& value : m_values) { if (value->contains_percentage()) return true; } return false; } CalculatedStyleValue::CalculationResult ProductCalculationNode::resolve(Layout::Node const* layout_node, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { Optional total; for (auto& additional_product : m_values) { auto additional_value = additional_product->resolve(layout_node, percentage_basis); if (!total.has_value()) { total = additional_value; continue; } total->multiply_by(additional_value, layout_node); } return total.value(); } ErrorOr ProductCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { for (auto& item : m_values) { TRY(item->for_each_child_node(callback)); TRY(callback(item)); } return {}; } ErrorOr ProductCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}PRODUCT:\n", "", indent)); for (auto const& item : m_values) TRY(item->dump(builder, indent + 2)); return {}; } ErrorOr> NegateCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) NegateCalculationNode(move(value))); } NegateCalculationNode::NegateCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Negate) , m_value(move(value)) { } NegateCalculationNode::~NegateCalculationNode() = default; ErrorOr NegateCalculationNode::to_string() const { return String::formatted("(0 - {})", TRY(m_value->to_string())); } Optional NegateCalculationNode::resolved_type() const { return m_value->resolved_type(); } bool NegateCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult NegateCalculationNode::resolve(Layout::Node const* layout_node, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { auto child_value = m_value->resolve(layout_node, percentage_basis); child_value.negate(); return child_value; } ErrorOr NegateCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr NegateCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}NEGATE:\n", "", indent)); TRY(m_value->dump(builder, indent + 2)); return {}; } ErrorOr> InvertCalculationNode::create(NonnullOwnPtr value) { return adopt_nonnull_own_or_enomem(new (nothrow) InvertCalculationNode(move(value))); } InvertCalculationNode::InvertCalculationNode(NonnullOwnPtr value) : CalculationNode(Type::Invert) , m_value(move(value)) { } InvertCalculationNode::~InvertCalculationNode() = default; ErrorOr InvertCalculationNode::to_string() const { return String::formatted("(1 / {})", TRY(m_value->to_string())); } Optional InvertCalculationNode::resolved_type() const { auto type = m_value->resolved_type(); if (type == CalculatedStyleValue::ResolvedType::Integer) return CalculatedStyleValue::ResolvedType::Number; return type; } bool InvertCalculationNode::contains_percentage() const { return m_value->contains_percentage(); } CalculatedStyleValue::CalculationResult InvertCalculationNode::resolve(Layout::Node const* layout_node, CalculatedStyleValue::PercentageBasis const& percentage_basis) const { auto child_value = m_value->resolve(layout_node, percentage_basis); child_value.invert(); return child_value; } ErrorOr InvertCalculationNode::for_each_child_node(Function(NonnullOwnPtr&)> const& callback) { TRY(m_value->for_each_child_node(callback)); TRY(callback(m_value)); return {}; } ErrorOr InvertCalculationNode::dump(StringBuilder& builder, int indent) const { TRY(builder.try_appendff("{: >{}}INVERT:\n", "", indent)); TRY(m_value->dump(builder, indent + 2)); return {}; } void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis) { add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis); } void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis) { add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis); } void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis) { // We know from validation when resolving the type, that "both sides have the same type, or that one side is a and the other is an ". // Though, having the same type may mean that one side is a and the other a . // Note: This is almost identical to ::add() m_value.visit( [&](Number const& number) { auto other_number = other.m_value.get(); if (op == SumOperation::Add) { m_value = number + other_number; } else { m_value = number - other_number; } }, [&](Angle const& angle) { auto this_degrees = angle.to_degrees(); if (other.m_value.has()) { auto other_degrees = other.m_value.get().to_degrees(); if (op == SumOperation::Add) m_value = Angle::make_degrees(this_degrees + other_degrees); else m_value = Angle::make_degrees(this_degrees - other_degrees); } else { VERIFY(percentage_basis.has()); auto other_degrees = percentage_basis.get().percentage_of(other.m_value.get()).to_degrees(); if (op == SumOperation::Add) m_value = Angle::make_degrees(this_degrees + other_degrees); else m_value = Angle::make_degrees(this_degrees - other_degrees); } }, [&](Frequency const& frequency) { auto this_hertz = frequency.to_hertz(); if (other.m_value.has()) { auto other_hertz = other.m_value.get().to_hertz(); if (op == SumOperation::Add) m_value = Frequency::make_hertz(this_hertz + other_hertz); else m_value = Frequency::make_hertz(this_hertz - other_hertz); } else { VERIFY(percentage_basis.has()); auto other_hertz = percentage_basis.get().percentage_of(other.m_value.get()).to_hertz(); if (op == SumOperation::Add) m_value = Frequency::make_hertz(this_hertz + other_hertz); else m_value = Frequency::make_hertz(this_hertz - other_hertz); } }, [&](Length const& length) { auto this_px = length.to_px(*layout_node); if (other.m_value.has()) { auto other_px = other.m_value.get().to_px(*layout_node); if (op == SumOperation::Add) m_value = Length::make_px(this_px + other_px); else m_value = Length::make_px(this_px - other_px); } else { VERIFY(percentage_basis.has()); auto other_px = percentage_basis.get().percentage_of(other.m_value.get()).to_px(*layout_node); if (op == SumOperation::Add) m_value = Length::make_px(this_px + other_px); else m_value = Length::make_px(this_px - other_px); } }, [&](Time const& time) { auto this_seconds = time.to_seconds(); if (other.m_value.has