CalculatedStyleValue.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "CalculatedStyleValue.h"
  10. #include <LibWeb/CSS/Percentage.h>
  11. namespace Web::CSS {
  12. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  13. {
  14. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  15. }
  16. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  17. {
  18. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  19. }
  20. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  21. {
  22. // 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>".
  23. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  24. // Note: This is almost identical to ::add()
  25. m_value.visit(
  26. [&](Number const& number) {
  27. auto other_number = other.m_value.get<Number>();
  28. if (op == SumOperation::Add) {
  29. m_value = number + other_number;
  30. } else {
  31. m_value = number - other_number;
  32. }
  33. },
  34. [&](Angle const& angle) {
  35. auto this_degrees = angle.to_degrees();
  36. if (other.m_value.has<Angle>()) {
  37. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  38. if (op == SumOperation::Add)
  39. m_value = Angle::make_degrees(this_degrees + other_degrees);
  40. else
  41. m_value = Angle::make_degrees(this_degrees - other_degrees);
  42. } else {
  43. VERIFY(percentage_basis.has<Angle>());
  44. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  45. if (op == SumOperation::Add)
  46. m_value = Angle::make_degrees(this_degrees + other_degrees);
  47. else
  48. m_value = Angle::make_degrees(this_degrees - other_degrees);
  49. }
  50. },
  51. [&](Frequency const& frequency) {
  52. auto this_hertz = frequency.to_hertz();
  53. if (other.m_value.has<Frequency>()) {
  54. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  55. if (op == SumOperation::Add)
  56. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  57. else
  58. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  59. } else {
  60. VERIFY(percentage_basis.has<Frequency>());
  61. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  62. if (op == SumOperation::Add)
  63. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  64. else
  65. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  66. }
  67. },
  68. [&](Length const& length) {
  69. auto this_px = length.to_px(*layout_node);
  70. if (other.m_value.has<Length>()) {
  71. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  72. if (op == SumOperation::Add)
  73. m_value = Length::make_px(this_px + other_px);
  74. else
  75. m_value = Length::make_px(this_px - other_px);
  76. } else {
  77. VERIFY(percentage_basis.has<Length>());
  78. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  79. if (op == SumOperation::Add)
  80. m_value = Length::make_px(this_px + other_px);
  81. else
  82. m_value = Length::make_px(this_px - other_px);
  83. }
  84. },
  85. [&](Time const& time) {
  86. auto this_seconds = time.to_seconds();
  87. if (other.m_value.has<Time>()) {
  88. auto other_seconds = other.m_value.get<Time>().to_seconds();
  89. if (op == SumOperation::Add)
  90. m_value = Time::make_seconds(this_seconds + other_seconds);
  91. else
  92. m_value = Time::make_seconds(this_seconds - other_seconds);
  93. } else {
  94. VERIFY(percentage_basis.has<Time>());
  95. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  96. if (op == SumOperation::Add)
  97. m_value = Time::make_seconds(this_seconds + other_seconds);
  98. else
  99. m_value = Time::make_seconds(this_seconds - other_seconds);
  100. }
  101. },
  102. [&](Percentage const& percentage) {
  103. if (other.m_value.has<Percentage>()) {
  104. if (op == SumOperation::Add)
  105. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  106. else
  107. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  108. return;
  109. }
  110. // 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`.
  111. CalculationResult new_value = other;
  112. if (op == SumOperation::Add) {
  113. new_value.add(*this, layout_node, percentage_basis);
  114. } else {
  115. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  116. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node);
  117. new_value.add(*this, layout_node, percentage_basis);
  118. }
  119. *this = new_value;
  120. });
  121. }
  122. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  123. {
  124. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  125. // Both of these are represented as a float.
  126. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  127. bool other_is_number = other.m_value.has<Number>();
  128. m_value.visit(
  129. [&](Number const& number) {
  130. if (other_is_number) {
  131. m_value = number * other.m_value.get<Number>();
  132. } else {
  133. // Avoid duplicating all the logic by swapping `this` and `other`.
  134. CalculationResult new_value = other;
  135. new_value.multiply_by(*this, layout_node);
  136. *this = new_value;
  137. }
  138. },
  139. [&](Angle const& angle) {
  140. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  141. },
  142. [&](Frequency const& frequency) {
  143. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  144. },
  145. [&](Length const& length) {
  146. VERIFY(layout_node);
  147. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  148. },
  149. [&](Time const& time) {
  150. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  151. },
  152. [&](Percentage const& percentage) {
  153. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  154. });
  155. }
  156. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  157. {
  158. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  159. // Both of these are represented as a Number.
  160. auto denominator = other.m_value.get<Number>().value();
  161. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  162. VERIFY(denominator != 0.0f);
  163. m_value.visit(
  164. [&](Number const& number) {
  165. m_value = Number {
  166. Number::Type::Number,
  167. number.value() / denominator
  168. };
  169. },
  170. [&](Angle const& angle) {
  171. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  172. },
  173. [&](Frequency const& frequency) {
  174. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  175. },
  176. [&](Length const& length) {
  177. VERIFY(layout_node);
  178. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  179. },
  180. [&](Time const& time) {
  181. m_value = Time::make_seconds(time.to_seconds() / denominator);
  182. },
  183. [&](Percentage const& percentage) {
  184. m_value = Percentage { percentage.value() / denominator };
  185. });
  186. }
  187. ErrorOr<String> CalculatedStyleValue::to_string() const
  188. {
  189. return String::formatted("calc({})", TRY(m_expression->to_string()));
  190. }
  191. bool CalculatedStyleValue::equals(StyleValue const& other) const
  192. {
  193. if (type() != other.type())
  194. return false;
  195. // This is a case where comparing the strings actually makes sense.
  196. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  197. }
  198. ErrorOr<String> CalculatedStyleValue::CalcNumberValue::to_string() const
  199. {
  200. return value.visit(
  201. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  202. [](NonnullOwnPtr<CalcNumberSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); });
  203. }
  204. ErrorOr<String> CalculatedStyleValue::CalcValue::to_string() const
  205. {
  206. return value.visit(
  207. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  208. [](NonnullOwnPtr<CalcSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); },
  209. [](auto const& v) -> ErrorOr<String> { return v.to_string(); });
  210. }
  211. ErrorOr<String> CalculatedStyleValue::CalcSum::to_string() const
  212. {
  213. StringBuilder builder;
  214. TRY(builder.try_append(TRY(first_calc_product->to_string())));
  215. for (auto const& item : zero_or_more_additional_calc_products)
  216. TRY(builder.try_append(TRY(item->to_string())));
  217. return builder.to_string();
  218. }
  219. ErrorOr<String> CalculatedStyleValue::CalcNumberSum::to_string() const
  220. {
  221. StringBuilder builder;
  222. TRY(builder.try_append(TRY(first_calc_number_product->to_string())));
  223. for (auto const& item : zero_or_more_additional_calc_number_products)
  224. TRY(builder.try_append(TRY(item->to_string())));
  225. return builder.to_string();
  226. }
  227. ErrorOr<String> CalculatedStyleValue::CalcProduct::to_string() const
  228. {
  229. StringBuilder builder;
  230. TRY(builder.try_append(TRY(first_calc_value.to_string())));
  231. for (auto const& item : zero_or_more_additional_calc_values)
  232. TRY(builder.try_append(TRY(item->to_string())));
  233. return builder.to_string();
  234. }
  235. ErrorOr<String> CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  236. {
  237. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  238. }
  239. ErrorOr<String> CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  240. {
  241. auto value_string = TRY(value.visit(
  242. [](CalcValue const& v) { return v.to_string(); },
  243. [](CalcNumberValue const& v) { return v.to_string(); }));
  244. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  245. }
  246. ErrorOr<String> CalculatedStyleValue::CalcNumberProduct::to_string() const
  247. {
  248. StringBuilder builder;
  249. TRY(builder.try_append(TRY(first_calc_number_value.to_string())));
  250. for (auto const& item : zero_or_more_additional_calc_number_values)
  251. TRY(builder.try_append(TRY(item->to_string())));
  252. return builder.to_string();
  253. }
  254. ErrorOr<String> CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  255. {
  256. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, TRY(value.to_string()));
  257. }
  258. ErrorOr<String> CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  259. {
  260. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  261. }
  262. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  263. {
  264. auto result = m_expression->resolve(nullptr, {});
  265. if (result.value().has<Angle>())
  266. return result.value().get<Angle>();
  267. return {};
  268. }
  269. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  270. {
  271. auto result = m_expression->resolve(nullptr, percentage_basis);
  272. return result.value().visit(
  273. [&](Angle const& angle) -> Optional<Angle> {
  274. return angle;
  275. },
  276. [&](Percentage const& percentage) -> Optional<Angle> {
  277. return percentage_basis.percentage_of(percentage);
  278. },
  279. [&](auto const&) -> Optional<Angle> {
  280. return {};
  281. });
  282. }
  283. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  284. {
  285. auto result = m_expression->resolve(nullptr, {});
  286. if (result.value().has<Frequency>())
  287. return result.value().get<Frequency>();
  288. return {};
  289. }
  290. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  291. {
  292. auto result = m_expression->resolve(nullptr, percentage_basis);
  293. return result.value().visit(
  294. [&](Frequency const& frequency) -> Optional<Frequency> {
  295. return frequency;
  296. },
  297. [&](Percentage const& percentage) -> Optional<Frequency> {
  298. return percentage_basis.percentage_of(percentage);
  299. },
  300. [&](auto const&) -> Optional<Frequency> {
  301. return {};
  302. });
  303. }
  304. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  305. {
  306. auto result = m_expression->resolve(&layout_node, {});
  307. if (result.value().has<Length>())
  308. return result.value().get<Length>();
  309. return {};
  310. }
  311. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  312. {
  313. auto result = m_expression->resolve(&layout_node, percentage_basis);
  314. return result.value().visit(
  315. [&](Length const& length) -> Optional<Length> {
  316. return length;
  317. },
  318. [&](Percentage const& percentage) -> Optional<Length> {
  319. return percentage_basis.percentage_of(percentage);
  320. },
  321. [&](auto const&) -> Optional<Length> {
  322. return {};
  323. });
  324. }
  325. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  326. {
  327. auto result = m_expression->resolve(nullptr, {});
  328. if (result.value().has<Percentage>())
  329. return result.value().get<Percentage>();
  330. return {};
  331. }
  332. Optional<Time> CalculatedStyleValue::resolve_time() const
  333. {
  334. auto result = m_expression->resolve(nullptr, {});
  335. if (result.value().has<Time>())
  336. return result.value().get<Time>();
  337. return {};
  338. }
  339. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  340. {
  341. auto result = m_expression->resolve(nullptr, percentage_basis);
  342. return result.value().visit(
  343. [&](Time const& time) -> Optional<Time> {
  344. return time;
  345. },
  346. [&](auto const&) -> Optional<Time> {
  347. return {};
  348. });
  349. }
  350. Optional<float> CalculatedStyleValue::resolve_number()
  351. {
  352. auto result = m_expression->resolve(nullptr, {});
  353. if (result.value().has<Number>())
  354. return result.value().get<Number>().value();
  355. return {};
  356. }
  357. Optional<i64> CalculatedStyleValue::resolve_integer()
  358. {
  359. auto result = m_expression->resolve(nullptr, {});
  360. if (result.value().has<Number>())
  361. return result.value().get<Number>().integer_value();
  362. return {};
  363. }
  364. static bool is_number(CalculatedStyleValue::ResolvedType type)
  365. {
  366. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  367. }
  368. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  369. {
  370. return type != CalculatedStyleValue::ResolvedType::Number
  371. && type != CalculatedStyleValue::ResolvedType::Integer
  372. && type != CalculatedStyleValue::ResolvedType::Percentage;
  373. }
  374. template<typename SumWithOperator>
  375. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<SumWithOperator>> const& zero_or_more_additional_products)
  376. {
  377. auto type = first_type;
  378. for (auto const& product : zero_or_more_additional_products) {
  379. auto maybe_product_type = product->resolved_type();
  380. if (!maybe_product_type.has_value())
  381. return {};
  382. auto product_type = maybe_product_type.value();
  383. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  384. // If both sides are the same type, resolve to that type.
  385. if (product_type == type)
  386. continue;
  387. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  388. if (is_number(type) && is_number(product_type)) {
  389. type = CalculatedStyleValue::ResolvedType::Number;
  390. continue;
  391. }
  392. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  393. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  394. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  395. type = product_type;
  396. continue;
  397. }
  398. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  399. continue;
  400. return {};
  401. }
  402. return type;
  403. }
  404. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  405. {
  406. auto maybe_type = first_calc_product->resolved_type();
  407. if (!maybe_type.has_value())
  408. return {};
  409. auto type = maybe_type.value();
  410. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  411. }
  412. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  413. {
  414. auto maybe_type = first_calc_number_product->resolved_type();
  415. if (!maybe_type.has_value())
  416. return {};
  417. auto type = maybe_type.value();
  418. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  419. }
  420. template<typename ProductWithOperator>
  421. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, Vector<NonnullOwnPtr<ProductWithOperator>> const& zero_or_more_additional_values)
  422. {
  423. auto type = first_type;
  424. for (auto const& value : zero_or_more_additional_values) {
  425. auto maybe_value_type = value->resolved_type();
  426. if (!maybe_value_type.has_value())
  427. return {};
  428. auto value_type = maybe_value_type.value();
  429. if (value->op == CalculatedStyleValue::ProductOperation::Multiply) {
  430. // At *, check that at least one side is <number>.
  431. if (!(is_number(type) || is_number(value_type)))
  432. return {};
  433. // If both sides are <integer>, resolve to <integer>.
  434. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  435. type = CalculatedStyleValue::ResolvedType::Integer;
  436. } else {
  437. // Otherwise, resolve to the type of the other side.
  438. if (is_number(type))
  439. type = value_type;
  440. }
  441. continue;
  442. } else {
  443. VERIFY(value->op == CalculatedStyleValue::ProductOperation::Divide);
  444. // At /, check that the right side is <number>.
  445. if (!is_number(value_type))
  446. return {};
  447. // If the left side is <integer>, resolve to <number>.
  448. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  449. type = CalculatedStyleValue::ResolvedType::Number;
  450. } else {
  451. // Otherwise, resolve to the type of the left side.
  452. }
  453. // FIXME: Division by zero makes the whole calc() expression invalid.
  454. }
  455. }
  456. return type;
  457. }
  458. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  459. {
  460. auto maybe_type = first_calc_value.resolved_type();
  461. if (!maybe_type.has_value())
  462. return {};
  463. auto type = maybe_type.value();
  464. return resolve_product_type(type, zero_or_more_additional_calc_values);
  465. }
  466. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  467. {
  468. return value->resolved_type();
  469. }
  470. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  471. {
  472. auto maybe_type = first_calc_number_value.resolved_type();
  473. if (!maybe_type.has_value())
  474. return {};
  475. auto type = maybe_type.value();
  476. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  477. }
  478. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  479. {
  480. return value.resolved_type();
  481. }
  482. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  483. {
  484. return value->resolved_type();
  485. }
  486. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  487. {
  488. return value.visit(
  489. [](CalcValue const& calc_value) {
  490. return calc_value.resolved_type();
  491. },
  492. [](CalcNumberValue const& calc_number_value) {
  493. return calc_number_value.resolved_type();
  494. });
  495. }
  496. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  497. {
  498. return value.visit(
  499. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  500. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  501. },
  502. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  503. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  504. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  505. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  506. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  507. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  508. }
  509. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  510. {
  511. return value.visit(
  512. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  513. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  514. },
  515. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  516. }
  517. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  518. {
  519. return value.visit(
  520. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  521. return CalculatedStyleValue::CalculationResult { number };
  522. },
  523. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  524. return sum->resolve(layout_node, percentage_basis);
  525. });
  526. }
  527. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  528. {
  529. return value.visit(
  530. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  531. return sum->resolve(layout_node, percentage_basis);
  532. },
  533. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  534. return CalculatedStyleValue::CalculationResult { v };
  535. });
  536. }
  537. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  538. {
  539. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  540. for (auto& additional_product : zero_or_more_additional_calc_products) {
  541. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  542. if (additional_product->op == CalculatedStyleValue::SumOperation::Add)
  543. value.add(additional_value, layout_node, percentage_basis);
  544. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  545. value.subtract(additional_value, layout_node, percentage_basis);
  546. else
  547. VERIFY_NOT_REACHED();
  548. }
  549. return value;
  550. }
  551. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  552. {
  553. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  554. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  555. auto additional_value = additional_product->resolve(layout_node, percentage_basis);
  556. if (additional_product->op == CSS::CalculatedStyleValue::SumOperation::Add)
  557. value.add(additional_value, layout_node, percentage_basis);
  558. else if (additional_product->op == CalculatedStyleValue::SumOperation::Subtract)
  559. value.subtract(additional_value, layout_node, percentage_basis);
  560. else
  561. VERIFY_NOT_REACHED();
  562. }
  563. return value;
  564. }
  565. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  566. {
  567. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  568. for (auto& additional_value : zero_or_more_additional_calc_values) {
  569. additional_value->value.visit(
  570. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  571. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Multiply);
  572. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  573. value.multiply_by(resolved_value, layout_node);
  574. },
  575. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  576. VERIFY(additional_value->op == CalculatedStyleValue::ProductOperation::Divide);
  577. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  578. // FIXME: Checking for division by 0 should happen during parsing.
  579. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  580. value.divide_by(resolved_calc_number_value, layout_node);
  581. });
  582. }
  583. return value;
  584. }
  585. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  586. {
  587. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  588. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  589. auto additional_value = additional_number_value->resolve(layout_node, percentage_basis);
  590. if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Multiply)
  591. value.multiply_by(additional_value, layout_node);
  592. else if (additional_number_value->op == CalculatedStyleValue::ProductOperation::Divide)
  593. value.divide_by(additional_value, layout_node);
  594. else
  595. VERIFY_NOT_REACHED();
  596. }
  597. return value;
  598. }
  599. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  600. {
  601. return value.visit(
  602. [&](CalcValue const& calc_value) {
  603. return calc_value.resolve(layout_node, percentage_basis);
  604. },
  605. [&](CalcNumberValue const& calc_number_value) {
  606. return calc_number_value.resolve(layout_node, percentage_basis);
  607. });
  608. }
  609. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  610. {
  611. return value->resolve(layout_node, percentage_basis);
  612. }
  613. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  614. {
  615. return value.resolve(layout_node, percentage_basis);
  616. }
  617. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  618. {
  619. return value->resolve(layout_node, percentage_basis);
  620. }
  621. ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Gfx::FontPixelMetrics const&, CSSPixels, CSSPixels, CSSPixels, CSSPixels) const
  622. {
  623. return *this;
  624. }
  625. bool CalculatedStyleValue::contains_percentage() const
  626. {
  627. return m_expression->contains_percentage();
  628. }
  629. bool CalculatedStyleValue::CalcSum::contains_percentage() const
  630. {
  631. if (first_calc_product->contains_percentage())
  632. return true;
  633. for (auto& part : zero_or_more_additional_calc_products) {
  634. if (part->contains_percentage())
  635. return true;
  636. }
  637. return false;
  638. }
  639. bool CalculatedStyleValue::CalcSumPartWithOperator::contains_percentage() const
  640. {
  641. return value->contains_percentage();
  642. }
  643. bool CalculatedStyleValue::CalcProduct::contains_percentage() const
  644. {
  645. if (first_calc_value.contains_percentage())
  646. return true;
  647. for (auto& part : zero_or_more_additional_calc_values) {
  648. if (part->contains_percentage())
  649. return true;
  650. }
  651. return false;
  652. }
  653. bool CalculatedStyleValue::CalcProductPartWithOperator::contains_percentage() const
  654. {
  655. return value.visit(
  656. [](CalcValue const& value) { return value.contains_percentage(); },
  657. [](CalcNumberValue const&) { return false; });
  658. }
  659. bool CalculatedStyleValue::CalcValue::contains_percentage() const
  660. {
  661. return value.visit(
  662. [](Percentage const&) { return true; },
  663. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->contains_percentage(); },
  664. [](auto const&) { return false; });
  665. }
  666. }