123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
- * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
- * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include "CalculatedStyleValue.h"
- #include <LibWeb/CSS/Percentage.h>
- 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;
- }
- 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)
- {
- return value.visit(
- [](Number const& number) { return number.value(); },
- [](Angle const& angle) { return angle.to_degrees(); },
- [](Frequency const& frequency) { return frequency.to_hertz(); },
- [&context](Length const& length) { return length.to_px(*context).to_double(); },
- [](Percentage const& percentage) { return percentage.value(); },
- [](Time const& time) { return time.to_seconds(); });
- };
- static CalculatedStyleValue::CalculationResult to_resolved_type(CalculatedStyleValue::ResolvedType type, double value)
- {
- switch (type) {
- case CalculatedStyleValue::ResolvedType::Integer:
- return { Number(Number::Type::Integer, value) };
- case CalculatedStyleValue::ResolvedType::Number:
- return { Number(Number::Type::Number, value) };
- case CalculatedStyleValue::ResolvedType::Angle:
- return { Angle::make_degrees(value) };
- case CalculatedStyleValue::ResolvedType::Frequency:
- return { Frequency::make_hertz(value) };
- case CalculatedStyleValue::ResolvedType::Length:
- return { Length::make_px(value) };
- case CalculatedStyleValue::ResolvedType::Percentage:
- return { Percentage(value) };
- case CalculatedStyleValue::ResolvedType::Time:
- return { Time::make_seconds(value) };
- }
- VERIFY_NOT_REACHED();
- };
- CalculationNode::CalculationNode(Type type)
- : m_type(type)
- {
- }
- CalculationNode::~CalculationNode() = default;
- ErrorOr<NonnullOwnPtr<NumericCalculationNode>> 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<String> NumericCalculationNode::to_string() const
- {
- return m_value.visit([](auto& value) { return value.to_string(); });
- }
- Optional<CalculatedStyleValue::ResolvedType> 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<Percentage>();
- }
- CalculatedStyleValue::CalculationResult NumericCalculationNode::resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const
- {
- return m_value;
- }
- ErrorOr<void> 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<NonnullOwnPtr<SumCalculationNode>> SumCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) SumCalculationNode(move(values)));
- }
- SumCalculationNode::SumCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
- : CalculationNode(Type::Sum)
- , m_values(move(values))
- {
- VERIFY(!m_values.is_empty());
- }
- SumCalculationNode::~SumCalculationNode() = default;
- ErrorOr<String> 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<CalculatedStyleValue::ResolvedType> 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<CalculatedStyleValue::ResolvedType> 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 <number> and the other is an <integer>.
- // If both sides are the same type, resolve to that type.
- if (value_type == type)
- continue;
- // If one side is a <number> and the other is an <integer>, resolve to <number>.
- if (is_number(*type) && is_number(value_type)) {
- type = CalculatedStyleValue::ResolvedType::Number;
- continue;
- }
- // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
- // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> 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(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- Optional<CalculatedStyleValue::CalculationResult> total;
- for (auto& additional_product : m_values) {
- auto additional_value = additional_product->resolve(context, percentage_basis);
- if (!total.has_value()) {
- total = additional_value;
- continue;
- }
- total->add(additional_value, context, percentage_basis);
- }
- return total.value();
- }
- ErrorOr<void> SumCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- for (auto& item : m_values) {
- TRY(item->for_each_child_node(callback));
- TRY(callback(item));
- }
- return {};
- }
- ErrorOr<void> 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<NonnullOwnPtr<ProductCalculationNode>> ProductCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) ProductCalculationNode(move(values)));
- }
- ProductCalculationNode::ProductCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
- : CalculationNode(Type::Product)
- , m_values(move(values))
- {
- VERIFY(!m_values.is_empty());
- }
- ProductCalculationNode::~ProductCalculationNode() = default;
- ErrorOr<String> 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<CalculatedStyleValue::ResolvedType> 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<CalculatedStyleValue::ResolvedType> 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 <number>.
- if (!(is_number(*type) || is_number(value_type)))
- return {};
- // If both sides are <integer>, resolve to <integer>.
- 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(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- Optional<CalculatedStyleValue::CalculationResult> total;
- for (auto& additional_product : m_values) {
- auto additional_value = additional_product->resolve(context, percentage_basis);
- if (!total.has_value()) {
- total = additional_value;
- continue;
- }
- total->multiply_by(additional_value, context);
- }
- return total.value();
- }
- ErrorOr<void> ProductCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- for (auto& item : m_values) {
- TRY(item->for_each_child_node(callback));
- TRY(callback(item));
- }
- return {};
- }
- ErrorOr<void> 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<NonnullOwnPtr<NegateCalculationNode>> NegateCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) NegateCalculationNode(move(value)));
- }
- NegateCalculationNode::NegateCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Negate)
- , m_value(move(value))
- {
- }
- NegateCalculationNode::~NegateCalculationNode() = default;
- ErrorOr<String> NegateCalculationNode::to_string() const
- {
- return String::formatted("(0 - {})", TRY(m_value->to_string()));
- }
- Optional<CalculatedStyleValue::ResolvedType> NegateCalculationNode::resolved_type() const
- {
- return m_value->resolved_type();
- }
- bool NegateCalculationNode::contains_percentage() const
- {
- return m_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult NegateCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- auto child_value = m_value->resolve(context, percentage_basis);
- child_value.negate();
- return child_value;
- }
- ErrorOr<void> NegateCalculationNode::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> NegateCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}NEGATE:\n", "", indent));
- TRY(m_value->dump(builder, indent + 2));
- return {};
- }
- ErrorOr<NonnullOwnPtr<InvertCalculationNode>> InvertCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) InvertCalculationNode(move(value)));
- }
- InvertCalculationNode::InvertCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Invert)
- , m_value(move(value))
- {
- }
- InvertCalculationNode::~InvertCalculationNode() = default;
- ErrorOr<String> InvertCalculationNode::to_string() const
- {
- return String::formatted("(1 / {})", TRY(m_value->to_string()));
- }
- Optional<CalculatedStyleValue::ResolvedType> 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(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- auto child_value = m_value->resolve(context, percentage_basis);
- child_value.invert();
- return child_value;
- }
- ErrorOr<void> InvertCalculationNode::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> InvertCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}INVERT:\n", "", indent));
- TRY(m_value->dump(builder, indent + 2));
- return {};
- }
- ErrorOr<NonnullOwnPtr<MinCalculationNode>> MinCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) MinCalculationNode(move(values)));
- }
- MinCalculationNode::MinCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
- : CalculationNode(Type::Min)
- , m_values(move(values))
- {
- }
- MinCalculationNode::~MinCalculationNode() = default;
- ErrorOr<String> MinCalculationNode::to_string() const
- {
- StringBuilder builder;
- TRY(builder.try_append("min("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<CalculatedStyleValue::ResolvedType> MinCalculationNode::resolved_type() const
- {
- // NOTE: We check during parsing that all values have the same type.
- return m_values[0]->resolved_type();
- }
- bool MinCalculationNode::contains_percentage() const
- {
- for (auto const& value : m_values) {
- if (value->contains_percentage())
- return true;
- }
- return false;
- }
- CalculatedStyleValue::CalculationResult MinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- CalculatedStyleValue::CalculationResult smallest_node = m_values.first()->resolve(context, percentage_basis);
- auto smallest_value = resolve_value(smallest_node.value(), context);
- for (size_t i = 1; i < m_values.size(); i++) {
- auto child_resolved = m_values[i]->resolve(context, percentage_basis);
- auto child_value = resolve_value(child_resolved.value(), context);
- if (child_value < smallest_value) {
- smallest_value = child_value;
- smallest_node = child_resolved;
- }
- }
- return smallest_node;
- }
- ErrorOr<void> MinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- for (auto& value : m_values) {
- TRY(value->for_each_child_node(callback));
- TRY(callback(value));
- }
- return {};
- }
- ErrorOr<void> MinCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}MIN:\n", "", indent));
- for (auto const& value : m_values)
- TRY(value->dump(builder, indent + 2));
- return {};
- }
- ErrorOr<NonnullOwnPtr<MaxCalculationNode>> MaxCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) MaxCalculationNode(move(values)));
- }
- MaxCalculationNode::MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
- : CalculationNode(Type::Max)
- , m_values(move(values))
- {
- }
- MaxCalculationNode::~MaxCalculationNode() = default;
- ErrorOr<String> MaxCalculationNode::to_string() const
- {
- StringBuilder builder;
- TRY(builder.try_append("max("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<CalculatedStyleValue::ResolvedType> MaxCalculationNode::resolved_type() const
- {
- // NOTE: We check during parsing that all values have the same type.
- return m_values[0]->resolved_type();
- }
- bool MaxCalculationNode::contains_percentage() const
- {
- for (auto const& value : m_values) {
- if (value->contains_percentage())
- return true;
- }
- return false;
- }
- CalculatedStyleValue::CalculationResult MaxCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- CalculatedStyleValue::CalculationResult largest_node = m_values.first()->resolve(context, percentage_basis);
- auto largest_value = resolve_value(largest_node.value(), context);
- for (size_t i = 1; i < m_values.size(); i++) {
- auto child_resolved = m_values[i]->resolve(context, percentage_basis);
- auto child_value = resolve_value(child_resolved.value(), context);
- if (child_value > largest_value) {
- largest_value = child_value;
- largest_node = child_resolved;
- }
- }
- return largest_node;
- }
- ErrorOr<void> MaxCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- for (auto& value : m_values) {
- TRY(value->for_each_child_node(callback));
- TRY(callback(value));
- }
- return {};
- }
- ErrorOr<void> MaxCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}MAX:\n", "", indent));
- for (auto const& value : m_values)
- TRY(value->dump(builder, indent + 2));
- return {};
- }
- ErrorOr<NonnullOwnPtr<ClampCalculationNode>> ClampCalculationNode::create(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) ClampCalculationNode(move(min), move(center), move(max)));
- }
- ClampCalculationNode::ClampCalculationNode(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
- : CalculationNode(Type::Clamp)
- , m_min_value(move(min))
- , m_center_value(move(center))
- , m_max_value(move(max))
- {
- }
- ClampCalculationNode::~ClampCalculationNode() = default;
- ErrorOr<String> ClampCalculationNode::to_string() const
- {
- StringBuilder builder;
- TRY(builder.try_append("clamp("sv));
- TRY(builder.try_append(TRY(m_min_value->to_string())));
- TRY(builder.try_append(", "sv));
- TRY(builder.try_append(TRY(m_center_value->to_string())));
- TRY(builder.try_append(", "sv));
- TRY(builder.try_append(TRY(m_max_value->to_string())));
- TRY(builder.try_append(")"sv));
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> ClampCalculationNode::resolved_type() const
- {
- // NOTE: We check during parsing that all values have the same type.
- return m_min_value->resolved_type();
- }
- bool ClampCalculationNode::contains_percentage() const
- {
- return m_min_value->contains_percentage() || m_center_value->contains_percentage() || m_max_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult ClampCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- auto min_node = m_min_value->resolve(context, percentage_basis);
- auto center_node = m_center_value->resolve(context, percentage_basis);
- auto max_node = m_max_value->resolve(context, percentage_basis);
- auto min_value = resolve_value(min_node.value(), context);
- auto center_value = resolve_value(center_node.value(), context);
- auto max_value = resolve_value(max_node.value(), context);
- // NOTE: The value should be returned as "max(MIN, min(VAL, MAX))"
- auto chosen_value = max(min_value, min(center_value, max_value));
- if (chosen_value == min_value)
- return min_node;
- if (chosen_value == center_value)
- return center_node;
- if (chosen_value == max_value)
- return max_node;
- VERIFY_NOT_REACHED();
- }
- ErrorOr<void> ClampCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- TRY(m_min_value->for_each_child_node(callback));
- TRY(m_center_value->for_each_child_node(callback));
- TRY(m_max_value->for_each_child_node(callback));
- TRY(callback(m_min_value));
- TRY(callback(m_center_value));
- TRY(callback(m_max_value));
- return {};
- }
- ErrorOr<void> ClampCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}CLAMP:\n", "", indent));
- TRY(m_min_value->dump(builder, indent + 2));
- TRY(m_center_value->dump(builder, indent + 2));
- TRY(m_max_value->dump(builder, indent + 2));
- return {};
- }
- ErrorOr<NonnullOwnPtr<AbsCalculationNode>> AbsCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) AbsCalculationNode(move(value)));
- }
- AbsCalculationNode::AbsCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Abs)
- , m_value(move(value))
- {
- }
- AbsCalculationNode::~AbsCalculationNode() = default;
- ErrorOr<String> AbsCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("abs("sv);
- builder.append(TRY(m_value->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> AbsCalculationNode::resolved_type() const
- {
- return m_value->resolved_type();
- }
- bool AbsCalculationNode::contains_percentage() const
- {
- return m_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult AbsCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- auto resolved_type = m_value->resolved_type().value();
- auto node_a = m_value->resolve(context, percentage_basis);
- auto node_a_value = resolve_value(node_a.value(), context);
- if (node_a_value < 0)
- return to_resolved_type(resolved_type, -node_a_value);
- return node_a;
- }
- ErrorOr<void> AbsCalculationNode::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> AbsCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}ABS: {}\n", "", indent, TRY(to_string())));
- return {};
- }
- ErrorOr<NonnullOwnPtr<SignCalculationNode>> SignCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) SignCalculationNode(move(value)));
- }
- SignCalculationNode::SignCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Sign)
- , m_value(move(value))
- {
- }
- SignCalculationNode::~SignCalculationNode() = default;
- ErrorOr<String> SignCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("sign("sv);
- builder.append(TRY(m_value->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> SignCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Integer;
- }
- bool SignCalculationNode::contains_percentage() const
- {
- return m_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult SignCalculationNode::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(node_a.value(), context);
- if (node_a_value < 0)
- return { Number(Number::Type::Integer, -1) };
- if (node_a_value > 0)
- return { Number(Number::Type::Integer, 1) };
- return { Number(Number::Type::Integer, 0) };
- }
- ErrorOr<void> SignCalculationNode::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> SignCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}SIGN: {}\n", "", indent, TRY(to_string())));
- return {};
- }
- ErrorOr<NonnullOwnPtr<ConstantCalculationNode>> ConstantCalculationNode::create(ConstantType constant)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) ConstantCalculationNode(constant));
- }
- ConstantCalculationNode::ConstantCalculationNode(ConstantType constant)
- : CalculationNode(Type::Constant)
- , m_constant(constant)
- {
- }
- ConstantCalculationNode::~ConstantCalculationNode() = default;
- ErrorOr<String> ConstantCalculationNode::to_string() const
- {
- switch (m_constant) {
- case CalculationNode::ConstantType::E:
- return "e"_short_string;
- case CalculationNode::ConstantType::PI:
- return "pi"_short_string;
- case CalculationNode::ConstantType::Infinity:
- return "infinity"_string;
- case CalculationNode::ConstantType::MinusInfinity:
- return "-infinity"_string;
- case CalculationNode::ConstantType::NaN:
- return "NaN"_string;
- }
- VERIFY_NOT_REACHED();
- }
- Optional<CalculatedStyleValue::ResolvedType> ConstantCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Number;
- }
- CalculatedStyleValue::CalculationResult ConstantCalculationNode::resolve([[maybe_unused]] Optional<Length::ResolutionContext const&> context, [[maybe_unused]] CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- switch (m_constant) {
- case CalculationNode::ConstantType::E:
- return { Number(Number::Type::Number, M_E) };
- case CalculationNode::ConstantType::PI:
- return { Number(Number::Type::Number, M_PI) };
- // FIXME: We need to keep track of Infinity and NaN across all nodes, since they require special handling.
- case CalculationNode::ConstantType::Infinity:
- return { Number(Number::Type::Number, NumericLimits<float>::max()) };
- case CalculationNode::ConstantType::MinusInfinity:
- return { Number(Number::Type::Number, NumericLimits<float>::lowest()) };
- case CalculationNode::ConstantType::NaN:
- return { Number(Number::Type::Number, NAN) };
- }
- VERIFY_NOT_REACHED();
- }
- ErrorOr<void> ConstantCalculationNode::for_each_child_node([[maybe_unused]] Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- return {};
- }
- ErrorOr<void> ConstantCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}CONSTANT: {}\n", "", indent, TRY(to_string())));
- 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 {};
- }
- ErrorOr<NonnullOwnPtr<CosCalculationNode>> CosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) CosCalculationNode(move(value)));
- }
- CosCalculationNode::CosCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Cos)
- , m_value(move(value))
- {
- }
- CosCalculationNode::~CosCalculationNode() = default;
- ErrorOr<String> CosCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("cos("sv);
- builder.append(TRY(m_value->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> CosCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Number;
- }
- bool CosCalculationNode::contains_percentage() const
- {
- return m_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult CosCalculationNode::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 = cos(node_a_value);
- return { Number(Number::Type::Number, result) };
- }
- ErrorOr<void> CosCalculationNode::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> CosCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}COS: {}\n", "", indent, TRY(to_string())));
- return {};
- }
- ErrorOr<NonnullOwnPtr<TanCalculationNode>> TanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) TanCalculationNode(move(value)));
- }
- TanCalculationNode::TanCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Tan)
- , m_value(move(value))
- {
- }
- TanCalculationNode::~TanCalculationNode() = default;
- ErrorOr<String> TanCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("tan("sv);
- builder.append(TRY(m_value->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> TanCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Number;
- }
- bool TanCalculationNode::contains_percentage() const
- {
- return m_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult TanCalculationNode::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 = tan(node_a_value);
- return { Number(Number::Type::Number, result) };
- }
- ErrorOr<void> TanCalculationNode::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> TanCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}TAN: {}\n", "", indent, TRY(to_string())));
- return {};
- }
- ErrorOr<NonnullOwnPtr<AsinCalculationNode>> AsinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) AsinCalculationNode(move(value)));
- }
- AsinCalculationNode::AsinCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Asin)
- , m_value(move(value))
- {
- }
- AsinCalculationNode::~AsinCalculationNode() = default;
- ErrorOr<String> AsinCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("asin("sv);
- builder.append(TRY(m_value->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> AsinCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Angle;
- }
- bool AsinCalculationNode::contains_percentage() const
- {
- return m_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult AsinCalculationNode::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(node_a.value(), context);
- auto result = asin(node_a_value);
- return { Angle(result, Angle::Type::Rad) };
- }
- ErrorOr<void> AsinCalculationNode::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> AsinCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}ASIN: {}\n", "", indent, TRY(to_string())));
- return {};
- }
- ErrorOr<NonnullOwnPtr<AcosCalculationNode>> AcosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) AcosCalculationNode(move(value)));
- }
- AcosCalculationNode::AcosCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Acos)
- , m_value(move(value))
- {
- }
- AcosCalculationNode::~AcosCalculationNode() = default;
- ErrorOr<String> AcosCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("acos("sv);
- builder.append(TRY(m_value->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> AcosCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Angle;
- }
- bool AcosCalculationNode::contains_percentage() const
- {
- return m_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult AcosCalculationNode::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(node_a.value(), context);
- auto result = acos(node_a_value);
- return { Angle(result, Angle::Type::Rad) };
- }
- ErrorOr<void> AcosCalculationNode::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> AcosCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}ACOS: {}\n", "", indent, TRY(to_string())));
- return {};
- }
- ErrorOr<NonnullOwnPtr<AtanCalculationNode>> AtanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) AtanCalculationNode(move(value)));
- }
- AtanCalculationNode::AtanCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Atan)
- , m_value(move(value))
- {
- }
- AtanCalculationNode::~AtanCalculationNode() = default;
- ErrorOr<String> AtanCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("atan("sv);
- builder.append(TRY(m_value->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> AtanCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Angle;
- }
- bool AtanCalculationNode::contains_percentage() const
- {
- return m_value->contains_percentage();
- }
- CalculatedStyleValue::CalculationResult AtanCalculationNode::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(node_a.value(), context);
- auto result = atan(node_a_value);
- return { Angle(result, Angle::Type::Rad) };
- }
- ErrorOr<void> AtanCalculationNode::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> AtanCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}ATAN: {}\n", "", indent, TRY(to_string())));
- 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 {};
- }
- ErrorOr<NonnullOwnPtr<PowCalculationNode>> PowCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) PowCalculationNode(move(x), move(y)));
- }
- PowCalculationNode::PowCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
- : CalculationNode(Type::Pow)
- , m_x(move(x))
- , m_y(move(y))
- {
- }
- PowCalculationNode::~PowCalculationNode() = default;
- ErrorOr<String> PowCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("pow("sv);
- builder.append(TRY(m_x->to_string()));
- builder.append(", "sv);
- builder.append(TRY(m_y->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> PowCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Number;
- }
- CalculatedStyleValue::CalculationResult PowCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- auto node_a = m_x->resolve(context, percentage_basis);
- auto node_a_value = resolve_value(node_a.value(), context);
- auto node_b = m_y->resolve(context, percentage_basis);
- auto node_b_value = resolve_value(node_b.value(), context);
- auto result = pow(node_a_value, node_b_value);
- return { Number(Number::Type::Number, result) };
- }
- ErrorOr<void> PowCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- TRY(m_x->for_each_child_node(callback));
- TRY(m_y->for_each_child_node(callback));
- TRY(callback(m_x));
- TRY(callback(m_y));
- return {};
- }
- ErrorOr<void> PowCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}POW: {}\n", "", indent, TRY(to_string())));
- return {};
- }
- ErrorOr<NonnullOwnPtr<SqrtCalculationNode>> SqrtCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) SqrtCalculationNode(move(value)));
- }
- SqrtCalculationNode::SqrtCalculationNode(NonnullOwnPtr<CalculationNode> value)
- : CalculationNode(Type::Sqrt)
- , m_value(move(value))
- {
- }
- SqrtCalculationNode::~SqrtCalculationNode() = default;
- ErrorOr<String> SqrtCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("sqrt("sv);
- builder.append(TRY(m_value->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> SqrtCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Number;
- }
- CalculatedStyleValue::CalculationResult SqrtCalculationNode::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(node_a.value(), context);
- auto result = sqrt(node_a_value);
- return { Number(Number::Type::Number, result) };
- }
- ErrorOr<void> SqrtCalculationNode::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> SqrtCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}SQRT: {}\n", "", indent, TRY(to_string())));
- return {};
- }
- ErrorOr<NonnullOwnPtr<HypotCalculationNode>> HypotCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) HypotCalculationNode(move(values)));
- }
- HypotCalculationNode::HypotCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
- : CalculationNode(Type::Hypot)
- , m_values(move(values))
- {
- }
- HypotCalculationNode::~HypotCalculationNode() = default;
- ErrorOr<String> 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<CalculatedStyleValue::ResolvedType> 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<Length::ResolutionContext const&> 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<void> HypotCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- for (auto& value : m_values) {
- TRY(value->for_each_child_node(callback));
- TRY(callback(value));
- }
- return {};
- }
- ErrorOr<void> 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 {};
- }
- ErrorOr<NonnullOwnPtr<LogCalculationNode>> LogCalculationNode::create(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
- {
- return adopt_nonnull_own_or_enomem(new (nothrow) LogCalculationNode(move(x), move(y)));
- }
- LogCalculationNode::LogCalculationNode(NonnullOwnPtr<CalculationNode> x, NonnullOwnPtr<CalculationNode> y)
- : CalculationNode(Type::Log)
- , m_x(move(x))
- , m_y(move(y))
- {
- }
- LogCalculationNode::~LogCalculationNode() = default;
- ErrorOr<String> LogCalculationNode::to_string() const
- {
- StringBuilder builder;
- builder.append("log("sv);
- builder.append(TRY(m_x->to_string()));
- builder.append(", "sv);
- builder.append(TRY(m_y->to_string()));
- builder.append(")"sv);
- return builder.to_string();
- }
- Optional<CalculatedStyleValue::ResolvedType> LogCalculationNode::resolved_type() const
- {
- return CalculatedStyleValue::ResolvedType::Number;
- }
- CalculatedStyleValue::CalculationResult LogCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
- {
- auto node_a = m_x->resolve(context, percentage_basis);
- auto node_a_value = resolve_value(node_a.value(), context);
- auto node_b = m_y->resolve(context, percentage_basis);
- auto node_b_value = resolve_value(node_b.value(), context);
- auto result = log2(node_a_value) / log2(node_b_value);
- return { Number(Number::Type::Number, result) };
- }
- ErrorOr<void> LogCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
- {
- TRY(m_x->for_each_child_node(callback));
- TRY(m_y->for_each_child_node(callback));
- TRY(callback(m_x));
- TRY(callback(m_y));
- return {};
- }
- ErrorOr<void> LogCalculationNode::dump(StringBuilder& builder, int indent) const
- {
- TRY(builder.try_appendff("{: >{}}LOG: {}\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);
- }
- void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
- {
- add_or_subtract_internal(SumOperation::Subtract, other, context, percentage_basis);
- }
- void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional<Length::ResolutionContext const&> context, 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 <number> and the other is an <integer>".
- // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
- // Note: This is almost identical to ::add()
- m_value.visit(
- [&](Number const& number) {
- auto other_number = other.m_value.get<Number>();
- 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<Angle>()) {
- auto other_degrees = other.m_value.get<Angle>().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<Angle>());
- auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).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<Frequency>()) {
- auto other_hertz = other.m_value.get<Frequency>().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<Frequency>());
- auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).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(*context);
- if (other.m_value.has<Length>()) {
- auto other_px = other.m_value.get<Length>().to_px(*context);
- 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<Length>());
- auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*context);
- 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<Time>()) {
- auto other_seconds = other.m_value.get<Time>().to_seconds();
- if (op == SumOperation::Add)
- m_value = Time::make_seconds(this_seconds + other_seconds);
- else
- m_value = Time::make_seconds(this_seconds - other_seconds);
- } else {
- VERIFY(percentage_basis.has<Time>());
- auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
- if (op == SumOperation::Add)
- m_value = Time::make_seconds(this_seconds + other_seconds);
- else
- m_value = Time::make_seconds(this_seconds - other_seconds);
- }
- },
- [&](Percentage const& percentage) {
- if (other.m_value.has<Percentage>()) {
- if (op == SumOperation::Add)
- m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
- else
- m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
- return;
- }
- // Other side isn't a percentage, so the easiest way to handle it without duplicating all the logic, is just to swap `this` and `other`.
- CalculationResult new_value = other;
- if (op == SumOperation::Add) {
- new_value.add(*this, context, percentage_basis);
- } else {
- // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
- new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, context);
- new_value.add(*this, context, percentage_basis);
- }
- *this = new_value;
- });
- }
- void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
- {
- // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
- // Both of these are represented as a double.
- VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
- bool other_is_number = other.m_value.has<Number>();
- m_value.visit(
- [&](Number const& number) {
- if (other_is_number) {
- m_value = number * other.m_value.get<Number>();
- } else {
- // Avoid duplicating all the logic by swapping `this` and `other`.
- CalculationResult new_value = other;
- new_value.multiply_by(*this, context);
- *this = new_value;
- }
- },
- [&](Angle const& angle) {
- m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
- },
- [&](Frequency const& frequency) {
- m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
- },
- [&](Length const& length) {
- m_value = Length::make_px(length.to_px(*context) * static_cast<double>(other.m_value.get<Number>().value()));
- },
- [&](Time const& time) {
- m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
- },
- [&](Percentage const& percentage) {
- m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
- });
- }
- void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
- {
- // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
- // Both of these are represented as a Number.
- auto denominator = other.m_value.get<Number>().value();
- // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
- VERIFY(denominator != 0.0);
- m_value.visit(
- [&](Number const& number) {
- m_value = Number {
- Number::Type::Number,
- number.value() / denominator
- };
- },
- [&](Angle const& angle) {
- m_value = Angle::make_degrees(angle.to_degrees() / denominator);
- },
- [&](Frequency const& frequency) {
- m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
- },
- [&](Length const& length) {
- m_value = Length::make_px(length.to_px(*context) / static_cast<double>(denominator));
- },
- [&](Time const& time) {
- m_value = Time::make_seconds(time.to_seconds() / denominator);
- },
- [&](Percentage const& percentage) {
- m_value = Percentage { percentage.value() / denominator };
- });
- }
- void CalculatedStyleValue::CalculationResult::negate()
- {
- m_value.visit(
- [&](Number const& number) {
- m_value = Number { number.type(), 0 - number.value() };
- },
- [&](Angle const& angle) {
- m_value = Angle { 0 - angle.raw_value(), angle.type() };
- },
- [&](Frequency const& frequency) {
- m_value = Frequency { 0 - frequency.raw_value(), frequency.type() };
- },
- [&](Length const& length) {
- m_value = Length { 0 - length.raw_value(), length.type() };
- },
- [&](Time const& time) {
- m_value = Time { 0 - time.raw_value(), time.type() };
- },
- [&](Percentage const& percentage) {
- m_value = Percentage { 0 - percentage.value() };
- });
- }
- void CalculatedStyleValue::CalculationResult::invert()
- {
- // FIXME: Correctly handle division by zero.
- m_value.visit(
- [&](Number const& number) {
- m_value = Number { Number::Type::Number, 1 / number.value() };
- },
- [&](Angle const& angle) {
- m_value = Angle { 1 / angle.raw_value(), angle.type() };
- },
- [&](Frequency const& frequency) {
- m_value = Frequency { 1 / frequency.raw_value(), frequency.type() };
- },
- [&](Length const& length) {
- m_value = Length { 1 / length.raw_value(), length.type() };
- },
- [&](Time const& time) {
- m_value = Time { 1 / time.raw_value(), time.type() };
- },
- [&](Percentage const& percentage) {
- m_value = Percentage { 1 / percentage.value() };
- });
- }
- ErrorOr<String> CalculatedStyleValue::to_string() const
- {
- // FIXME: Implement this according to https://www.w3.org/TR/css-values-4/#calc-serialize once that stabilizes.
- return String::formatted("calc({})", TRY(m_calculation->to_string()));
- }
- bool CalculatedStyleValue::equals(StyleValue const& other) const
- {
- if (type() != other.type())
- return false;
- // This is a case where comparing the strings actually makes sense.
- return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
- }
- Optional<Angle> CalculatedStyleValue::resolve_angle() const
- {
- auto result = m_calculation->resolve({}, {});
- if (result.value().has<Angle>())
- return result.value().get<Angle>();
- return {};
- }
- Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
- {
- auto result = m_calculation->resolve({}, percentage_basis);
- return result.value().visit(
- [&](Angle const& angle) -> Optional<Angle> {
- return angle;
- },
- [&](Percentage const& percentage) -> Optional<Angle> {
- return percentage_basis.percentage_of(percentage);
- },
- [&](auto const&) -> Optional<Angle> {
- return {};
- });
- }
- Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
- {
- auto result = m_calculation->resolve({}, {});
- if (result.value().has<Frequency>())
- return result.value().get<Frequency>();
- return {};
- }
- Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
- {
- auto result = m_calculation->resolve({}, percentage_basis);
- return result.value().visit(
- [&](Frequency const& frequency) -> Optional<Frequency> {
- return frequency;
- },
- [&](Percentage const& percentage) -> Optional<Frequency> {
- return percentage_basis.percentage_of(percentage);
- },
- [&](auto const&) -> Optional<Frequency> {
- return {};
- });
- }
- Optional<Length> CalculatedStyleValue::resolve_length(Length::ResolutionContext const& context) const
- {
- auto result = m_calculation->resolve(context, {});
- if (result.value().has<Length>())
- return result.value().get<Length>();
- return {};
- }
- Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
- {
- return resolve_length(Length::ResolutionContext::for_layout_node(layout_node));
- }
- Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
- {
- auto result = m_calculation->resolve(Length::ResolutionContext::for_layout_node(layout_node), percentage_basis);
- return result.value().visit(
- [&](Length const& length) -> Optional<Length> {
- return length;
- },
- [&](Percentage const& percentage) -> Optional<Length> {
- return percentage_basis.percentage_of(percentage);
- },
- [&](auto const&) -> Optional<Length> {
- return {};
- });
- }
- Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
- {
- auto result = m_calculation->resolve({}, {});
- if (result.value().has<Percentage>())
- return result.value().get<Percentage>();
- return {};
- }
- Optional<Time> CalculatedStyleValue::resolve_time() const
- {
- auto result = m_calculation->resolve({}, {});
- if (result.value().has<Time>())
- return result.value().get<Time>();
- return {};
- }
- Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
- {
- auto result = m_calculation->resolve({}, percentage_basis);
- return result.value().visit(
- [&](Time const& time) -> Optional<Time> {
- return time;
- },
- [&](auto const&) -> Optional<Time> {
- return {};
- });
- }
- Optional<double> CalculatedStyleValue::resolve_number() const
- {
- auto result = m_calculation->resolve({}, {});
- if (result.value().has<Number>())
- return result.value().get<Number>().value();
- return {};
- }
- Optional<i64> CalculatedStyleValue::resolve_integer()
- {
- auto result = m_calculation->resolve({}, {});
- if (result.value().has<Number>())
- return result.value().get<Number>().integer_value();
- return {};
- }
- bool CalculatedStyleValue::contains_percentage() const
- {
- return m_calculation->contains_percentage();
- }
- }
|