CalculatedStyleValue.cpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. static bool is_number(CalculatedStyleValue::ResolvedType type)
  13. {
  14. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  15. }
  16. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  17. {
  18. return type != CalculatedStyleValue::ResolvedType::Number
  19. && type != CalculatedStyleValue::ResolvedType::Integer
  20. && type != CalculatedStyleValue::ResolvedType::Percentage;
  21. }
  22. static double resolve_value_radians(CalculatedStyleValue::CalculationResult::Value value)
  23. {
  24. return value.visit(
  25. [](Number const& number) { return number.value(); },
  26. [](Angle const& angle) { return angle.to_radians(); },
  27. [](auto const&) { VERIFY_NOT_REACHED(); return 0.0; });
  28. };
  29. static double resolve_value(CalculatedStyleValue::CalculationResult::Value value, Optional<Length::ResolutionContext const&> context)
  30. {
  31. return value.visit(
  32. [](Number const& number) { return number.value(); },
  33. [](Angle const& angle) { return angle.to_degrees(); },
  34. [](Frequency const& frequency) { return frequency.to_hertz(); },
  35. [&context](Length const& length) { return length.to_px(*context).to_double(); },
  36. [](Percentage const& percentage) { return percentage.value(); },
  37. [](Time const& time) { return time.to_seconds(); });
  38. };
  39. static CalculatedStyleValue::CalculationResult to_resolved_type(CalculatedStyleValue::ResolvedType type, double value)
  40. {
  41. switch (type) {
  42. case CalculatedStyleValue::ResolvedType::Integer:
  43. return { Number(Number::Type::Integer, value) };
  44. case CalculatedStyleValue::ResolvedType::Number:
  45. return { Number(Number::Type::Number, value) };
  46. case CalculatedStyleValue::ResolvedType::Angle:
  47. return { Angle::make_degrees(value) };
  48. case CalculatedStyleValue::ResolvedType::Frequency:
  49. return { Frequency::make_hertz(value) };
  50. case CalculatedStyleValue::ResolvedType::Length:
  51. return { Length::make_px(value) };
  52. case CalculatedStyleValue::ResolvedType::Percentage:
  53. return { Percentage(value) };
  54. case CalculatedStyleValue::ResolvedType::Time:
  55. return { Time::make_seconds(value) };
  56. }
  57. VERIFY_NOT_REACHED();
  58. };
  59. CalculationNode::CalculationNode(Type type)
  60. : m_type(type)
  61. {
  62. }
  63. CalculationNode::~CalculationNode() = default;
  64. ErrorOr<NonnullOwnPtr<NumericCalculationNode>> NumericCalculationNode::create(NumericValue value)
  65. {
  66. return adopt_nonnull_own_or_enomem(new (nothrow) NumericCalculationNode(move(value)));
  67. }
  68. NumericCalculationNode::NumericCalculationNode(NumericValue value)
  69. : CalculationNode(Type::Numeric)
  70. , m_value(move(value))
  71. {
  72. }
  73. NumericCalculationNode::~NumericCalculationNode() = default;
  74. ErrorOr<String> NumericCalculationNode::to_string() const
  75. {
  76. return m_value.visit([](auto& value) { return value.to_string(); });
  77. }
  78. Optional<CalculatedStyleValue::ResolvedType> NumericCalculationNode::resolved_type() const
  79. {
  80. return m_value.visit(
  81. [](Number const&) { return CalculatedStyleValue::ResolvedType::Number; },
  82. [](Angle const&) { return CalculatedStyleValue::ResolvedType::Angle; },
  83. [](Frequency const&) { return CalculatedStyleValue::ResolvedType::Frequency; },
  84. [](Length const&) { return CalculatedStyleValue::ResolvedType::Length; },
  85. [](Percentage const&) { return CalculatedStyleValue::ResolvedType::Percentage; },
  86. [](Time const&) { return CalculatedStyleValue::ResolvedType::Time; });
  87. }
  88. bool NumericCalculationNode::contains_percentage() const
  89. {
  90. return m_value.has<Percentage>();
  91. }
  92. CalculatedStyleValue::CalculationResult NumericCalculationNode::resolve(Optional<Length::ResolutionContext const&>, CalculatedStyleValue::PercentageBasis const&) const
  93. {
  94. return m_value;
  95. }
  96. ErrorOr<void> NumericCalculationNode::dump(StringBuilder& builder, int indent) const
  97. {
  98. return builder.try_appendff("{: >{}}NUMERIC({})\n", "", indent, TRY(m_value.visit([](auto& it) { return it.to_string(); })));
  99. }
  100. ErrorOr<NonnullOwnPtr<SumCalculationNode>> SumCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  101. {
  102. return adopt_nonnull_own_or_enomem(new (nothrow) SumCalculationNode(move(values)));
  103. }
  104. SumCalculationNode::SumCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  105. : CalculationNode(Type::Sum)
  106. , m_values(move(values))
  107. {
  108. VERIFY(!m_values.is_empty());
  109. }
  110. SumCalculationNode::~SumCalculationNode() = default;
  111. ErrorOr<String> SumCalculationNode::to_string() const
  112. {
  113. bool first = true;
  114. StringBuilder builder;
  115. for (auto& value : m_values) {
  116. if (!first)
  117. TRY(builder.try_append(" + "sv));
  118. TRY(builder.try_append(TRY(value->to_string())));
  119. first = false;
  120. }
  121. return builder.to_string();
  122. }
  123. Optional<CalculatedStyleValue::ResolvedType> SumCalculationNode::resolved_type() const
  124. {
  125. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  126. // For now, this is just ad-hoc, based on the old implementation.
  127. Optional<CalculatedStyleValue::ResolvedType> type;
  128. for (auto const& value : m_values) {
  129. auto maybe_value_type = value->resolved_type();
  130. if (!maybe_value_type.has_value())
  131. return {};
  132. auto value_type = maybe_value_type.value();
  133. if (!type.has_value()) {
  134. type = value_type;
  135. continue;
  136. }
  137. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  138. // If both sides are the same type, resolve to that type.
  139. if (value_type == type)
  140. continue;
  141. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  142. if (is_number(*type) && is_number(value_type)) {
  143. type = CalculatedStyleValue::ResolvedType::Number;
  144. continue;
  145. }
  146. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  147. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  148. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(value_type)) {
  149. type = value_type;
  150. continue;
  151. }
  152. if (is_dimension(*type) && value_type == CalculatedStyleValue::ResolvedType::Percentage)
  153. continue;
  154. return {};
  155. }
  156. return type;
  157. }
  158. bool SumCalculationNode::contains_percentage() const
  159. {
  160. for (auto const& value : m_values) {
  161. if (value->contains_percentage())
  162. return true;
  163. }
  164. return false;
  165. }
  166. CalculatedStyleValue::CalculationResult SumCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  167. {
  168. Optional<CalculatedStyleValue::CalculationResult> total;
  169. for (auto& additional_product : m_values) {
  170. auto additional_value = additional_product->resolve(context, percentage_basis);
  171. if (!total.has_value()) {
  172. total = additional_value;
  173. continue;
  174. }
  175. total->add(additional_value, context, percentage_basis);
  176. }
  177. return total.value();
  178. }
  179. ErrorOr<void> SumCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  180. {
  181. for (auto& item : m_values) {
  182. TRY(item->for_each_child_node(callback));
  183. TRY(callback(item));
  184. }
  185. return {};
  186. }
  187. ErrorOr<void> SumCalculationNode::dump(StringBuilder& builder, int indent) const
  188. {
  189. TRY(builder.try_appendff("{: >{}}SUM:\n", "", indent));
  190. for (auto const& item : m_values)
  191. TRY(item->dump(builder, indent + 2));
  192. return {};
  193. }
  194. ErrorOr<NonnullOwnPtr<ProductCalculationNode>> ProductCalculationNode::create(Vector<NonnullOwnPtr<CalculationNode>> values)
  195. {
  196. return adopt_nonnull_own_or_enomem(new (nothrow) ProductCalculationNode(move(values)));
  197. }
  198. ProductCalculationNode::ProductCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  199. : CalculationNode(Type::Product)
  200. , m_values(move(values))
  201. {
  202. VERIFY(!m_values.is_empty());
  203. }
  204. ProductCalculationNode::~ProductCalculationNode() = default;
  205. ErrorOr<String> ProductCalculationNode::to_string() const
  206. {
  207. bool first = true;
  208. StringBuilder builder;
  209. for (auto& value : m_values) {
  210. if (!first)
  211. TRY(builder.try_append(" * "sv));
  212. TRY(builder.try_append(TRY(value->to_string())));
  213. first = false;
  214. }
  215. return builder.to_string();
  216. }
  217. Optional<CalculatedStyleValue::ResolvedType> ProductCalculationNode::resolved_type() const
  218. {
  219. // FIXME: Implement https://www.w3.org/TR/css-values-4/#determine-the-type-of-a-calculation
  220. // For now, this is just ad-hoc, based on the old implementation.
  221. Optional<CalculatedStyleValue::ResolvedType> type;
  222. for (auto const& value : m_values) {
  223. auto maybe_value_type = value->resolved_type();
  224. if (!maybe_value_type.has_value())
  225. return {};
  226. auto value_type = maybe_value_type.value();
  227. if (!type.has_value()) {
  228. type = value_type;
  229. continue;
  230. }
  231. // At *, check that at least one side is <number>.
  232. if (!(is_number(*type) || is_number(value_type)))
  233. return {};
  234. // If both sides are <integer>, resolve to <integer>.
  235. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  236. type = CalculatedStyleValue::ResolvedType::Integer;
  237. } else {
  238. // Otherwise, resolve to the type of the other side.
  239. if (is_number(*type))
  240. type = value_type;
  241. }
  242. }
  243. return type;
  244. }
  245. bool ProductCalculationNode::contains_percentage() const
  246. {
  247. for (auto const& value : m_values) {
  248. if (value->contains_percentage())
  249. return true;
  250. }
  251. return false;
  252. }
  253. CalculatedStyleValue::CalculationResult ProductCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  254. {
  255. Optional<CalculatedStyleValue::CalculationResult> total;
  256. for (auto& additional_product : m_values) {
  257. auto additional_value = additional_product->resolve(context, percentage_basis);
  258. if (!total.has_value()) {
  259. total = additional_value;
  260. continue;
  261. }
  262. total->multiply_by(additional_value, context);
  263. }
  264. return total.value();
  265. }
  266. ErrorOr<void> ProductCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  267. {
  268. for (auto& item : m_values) {
  269. TRY(item->for_each_child_node(callback));
  270. TRY(callback(item));
  271. }
  272. return {};
  273. }
  274. ErrorOr<void> ProductCalculationNode::dump(StringBuilder& builder, int indent) const
  275. {
  276. TRY(builder.try_appendff("{: >{}}PRODUCT:\n", "", indent));
  277. for (auto const& item : m_values)
  278. TRY(item->dump(builder, indent + 2));
  279. return {};
  280. }
  281. ErrorOr<NonnullOwnPtr<NegateCalculationNode>> NegateCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  282. {
  283. return adopt_nonnull_own_or_enomem(new (nothrow) NegateCalculationNode(move(value)));
  284. }
  285. NegateCalculationNode::NegateCalculationNode(NonnullOwnPtr<CalculationNode> value)
  286. : CalculationNode(Type::Negate)
  287. , m_value(move(value))
  288. {
  289. }
  290. NegateCalculationNode::~NegateCalculationNode() = default;
  291. ErrorOr<String> NegateCalculationNode::to_string() const
  292. {
  293. return String::formatted("(0 - {})", TRY(m_value->to_string()));
  294. }
  295. Optional<CalculatedStyleValue::ResolvedType> NegateCalculationNode::resolved_type() const
  296. {
  297. return m_value->resolved_type();
  298. }
  299. bool NegateCalculationNode::contains_percentage() const
  300. {
  301. return m_value->contains_percentage();
  302. }
  303. CalculatedStyleValue::CalculationResult NegateCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  304. {
  305. auto child_value = m_value->resolve(context, percentage_basis);
  306. child_value.negate();
  307. return child_value;
  308. }
  309. ErrorOr<void> NegateCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  310. {
  311. TRY(m_value->for_each_child_node(callback));
  312. TRY(callback(m_value));
  313. return {};
  314. }
  315. ErrorOr<void> NegateCalculationNode::dump(StringBuilder& builder, int indent) const
  316. {
  317. TRY(builder.try_appendff("{: >{}}NEGATE:\n", "", indent));
  318. TRY(m_value->dump(builder, indent + 2));
  319. return {};
  320. }
  321. ErrorOr<NonnullOwnPtr<InvertCalculationNode>> InvertCalculationNode::create(NonnullOwnPtr<Web::CSS::CalculationNode> value)
  322. {
  323. return adopt_nonnull_own_or_enomem(new (nothrow) InvertCalculationNode(move(value)));
  324. }
  325. InvertCalculationNode::InvertCalculationNode(NonnullOwnPtr<CalculationNode> value)
  326. : CalculationNode(Type::Invert)
  327. , m_value(move(value))
  328. {
  329. }
  330. InvertCalculationNode::~InvertCalculationNode() = default;
  331. ErrorOr<String> InvertCalculationNode::to_string() const
  332. {
  333. return String::formatted("(1 / {})", TRY(m_value->to_string()));
  334. }
  335. Optional<CalculatedStyleValue::ResolvedType> InvertCalculationNode::resolved_type() const
  336. {
  337. auto type = m_value->resolved_type();
  338. if (type == CalculatedStyleValue::ResolvedType::Integer)
  339. return CalculatedStyleValue::ResolvedType::Number;
  340. return type;
  341. }
  342. bool InvertCalculationNode::contains_percentage() const
  343. {
  344. return m_value->contains_percentage();
  345. }
  346. CalculatedStyleValue::CalculationResult InvertCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  347. {
  348. auto child_value = m_value->resolve(context, percentage_basis);
  349. child_value.invert();
  350. return child_value;
  351. }
  352. ErrorOr<void> InvertCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  353. {
  354. TRY(m_value->for_each_child_node(callback));
  355. TRY(callback(m_value));
  356. return {};
  357. }
  358. ErrorOr<void> InvertCalculationNode::dump(StringBuilder& builder, int indent) const
  359. {
  360. TRY(builder.try_appendff("{: >{}}INVERT:\n", "", indent));
  361. TRY(m_value->dump(builder, indent + 2));
  362. return {};
  363. }
  364. ErrorOr<NonnullOwnPtr<MinCalculationNode>> MinCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  365. {
  366. return adopt_nonnull_own_or_enomem(new (nothrow) MinCalculationNode(move(values)));
  367. }
  368. MinCalculationNode::MinCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  369. : CalculationNode(Type::Min)
  370. , m_values(move(values))
  371. {
  372. }
  373. MinCalculationNode::~MinCalculationNode() = default;
  374. ErrorOr<String> MinCalculationNode::to_string() const
  375. {
  376. StringBuilder builder;
  377. TRY(builder.try_append("min("sv));
  378. for (size_t i = 0; i < m_values.size(); ++i) {
  379. if (i != 0)
  380. TRY(builder.try_append(", "sv));
  381. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  382. }
  383. TRY(builder.try_append(")"sv));
  384. return builder.to_string();
  385. }
  386. Optional<CalculatedStyleValue::ResolvedType> MinCalculationNode::resolved_type() const
  387. {
  388. // NOTE: We check during parsing that all values have the same type.
  389. return m_values[0]->resolved_type();
  390. }
  391. bool MinCalculationNode::contains_percentage() const
  392. {
  393. for (auto const& value : m_values) {
  394. if (value->contains_percentage())
  395. return true;
  396. }
  397. return false;
  398. }
  399. CalculatedStyleValue::CalculationResult MinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  400. {
  401. CalculatedStyleValue::CalculationResult smallest_node = m_values.first()->resolve(context, percentage_basis);
  402. auto smallest_value = resolve_value(smallest_node.value(), context);
  403. for (size_t i = 1; i < m_values.size(); i++) {
  404. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  405. auto child_value = resolve_value(child_resolved.value(), context);
  406. if (child_value < smallest_value) {
  407. smallest_value = child_value;
  408. smallest_node = child_resolved;
  409. }
  410. }
  411. return smallest_node;
  412. }
  413. ErrorOr<void> MinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  414. {
  415. for (auto& value : m_values) {
  416. TRY(value->for_each_child_node(callback));
  417. TRY(callback(value));
  418. }
  419. return {};
  420. }
  421. ErrorOr<void> MinCalculationNode::dump(StringBuilder& builder, int indent) const
  422. {
  423. TRY(builder.try_appendff("{: >{}}MIN:\n", "", indent));
  424. for (auto const& value : m_values)
  425. TRY(value->dump(builder, indent + 2));
  426. return {};
  427. }
  428. ErrorOr<NonnullOwnPtr<MaxCalculationNode>> MaxCalculationNode::create(Vector<NonnullOwnPtr<Web::CSS::CalculationNode>> values)
  429. {
  430. return adopt_nonnull_own_or_enomem(new (nothrow) MaxCalculationNode(move(values)));
  431. }
  432. MaxCalculationNode::MaxCalculationNode(Vector<NonnullOwnPtr<CalculationNode>> values)
  433. : CalculationNode(Type::Max)
  434. , m_values(move(values))
  435. {
  436. }
  437. MaxCalculationNode::~MaxCalculationNode() = default;
  438. ErrorOr<String> MaxCalculationNode::to_string() const
  439. {
  440. StringBuilder builder;
  441. TRY(builder.try_append("max("sv));
  442. for (size_t i = 0; i < m_values.size(); ++i) {
  443. if (i != 0)
  444. TRY(builder.try_append(", "sv));
  445. TRY(builder.try_append(TRY(m_values[i]->to_string())));
  446. }
  447. TRY(builder.try_append(")"sv));
  448. return builder.to_string();
  449. }
  450. Optional<CalculatedStyleValue::ResolvedType> MaxCalculationNode::resolved_type() const
  451. {
  452. // NOTE: We check during parsing that all values have the same type.
  453. return m_values[0]->resolved_type();
  454. }
  455. bool MaxCalculationNode::contains_percentage() const
  456. {
  457. for (auto const& value : m_values) {
  458. if (value->contains_percentage())
  459. return true;
  460. }
  461. return false;
  462. }
  463. CalculatedStyleValue::CalculationResult MaxCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  464. {
  465. CalculatedStyleValue::CalculationResult largest_node = m_values.first()->resolve(context, percentage_basis);
  466. auto largest_value = resolve_value(largest_node.value(), context);
  467. for (size_t i = 1; i < m_values.size(); i++) {
  468. auto child_resolved = m_values[i]->resolve(context, percentage_basis);
  469. auto child_value = resolve_value(child_resolved.value(), context);
  470. if (child_value > largest_value) {
  471. largest_value = child_value;
  472. largest_node = child_resolved;
  473. }
  474. }
  475. return largest_node;
  476. }
  477. ErrorOr<void> MaxCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  478. {
  479. for (auto& value : m_values) {
  480. TRY(value->for_each_child_node(callback));
  481. TRY(callback(value));
  482. }
  483. return {};
  484. }
  485. ErrorOr<void> MaxCalculationNode::dump(StringBuilder& builder, int indent) const
  486. {
  487. TRY(builder.try_appendff("{: >{}}MAX:\n", "", indent));
  488. for (auto const& value : m_values)
  489. TRY(value->dump(builder, indent + 2));
  490. return {};
  491. }
  492. ErrorOr<NonnullOwnPtr<ClampCalculationNode>> ClampCalculationNode::create(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  493. {
  494. return adopt_nonnull_own_or_enomem(new (nothrow) ClampCalculationNode(move(min), move(center), move(max)));
  495. }
  496. ClampCalculationNode::ClampCalculationNode(NonnullOwnPtr<CalculationNode> min, NonnullOwnPtr<CalculationNode> center, NonnullOwnPtr<CalculationNode> max)
  497. : CalculationNode(Type::Clamp)
  498. , m_min_value(move(min))
  499. , m_center_value(move(center))
  500. , m_max_value(move(max))
  501. {
  502. }
  503. ClampCalculationNode::~ClampCalculationNode() = default;
  504. ErrorOr<String> ClampCalculationNode::to_string() const
  505. {
  506. StringBuilder builder;
  507. TRY(builder.try_append("clamp("sv));
  508. TRY(builder.try_append(TRY(m_min_value->to_string())));
  509. TRY(builder.try_append(", "sv));
  510. TRY(builder.try_append(TRY(m_center_value->to_string())));
  511. TRY(builder.try_append(", "sv));
  512. TRY(builder.try_append(TRY(m_max_value->to_string())));
  513. TRY(builder.try_append(")"sv));
  514. return builder.to_string();
  515. }
  516. Optional<CalculatedStyleValue::ResolvedType> ClampCalculationNode::resolved_type() const
  517. {
  518. // NOTE: We check during parsing that all values have the same type.
  519. return m_min_value->resolved_type();
  520. }
  521. bool ClampCalculationNode::contains_percentage() const
  522. {
  523. return m_min_value->contains_percentage() || m_center_value->contains_percentage() || m_max_value->contains_percentage();
  524. }
  525. CalculatedStyleValue::CalculationResult ClampCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  526. {
  527. auto min_node = m_min_value->resolve(context, percentage_basis);
  528. auto center_node = m_center_value->resolve(context, percentage_basis);
  529. auto max_node = m_max_value->resolve(context, percentage_basis);
  530. auto min_value = resolve_value(min_node.value(), context);
  531. auto center_value = resolve_value(center_node.value(), context);
  532. auto max_value = resolve_value(max_node.value(), context);
  533. // NOTE: The value should be returned as "max(MIN, min(VAL, MAX))"
  534. auto chosen_value = max(min_value, min(center_value, max_value));
  535. if (chosen_value == min_value)
  536. return min_node;
  537. if (chosen_value == center_value)
  538. return center_node;
  539. if (chosen_value == max_value)
  540. return max_node;
  541. VERIFY_NOT_REACHED();
  542. }
  543. ErrorOr<void> ClampCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  544. {
  545. TRY(m_min_value->for_each_child_node(callback));
  546. TRY(m_center_value->for_each_child_node(callback));
  547. TRY(m_max_value->for_each_child_node(callback));
  548. TRY(callback(m_min_value));
  549. TRY(callback(m_center_value));
  550. TRY(callback(m_max_value));
  551. return {};
  552. }
  553. ErrorOr<void> ClampCalculationNode::dump(StringBuilder& builder, int indent) const
  554. {
  555. TRY(builder.try_appendff("{: >{}}CLAMP:\n", "", indent));
  556. TRY(m_min_value->dump(builder, indent + 2));
  557. TRY(m_center_value->dump(builder, indent + 2));
  558. TRY(m_max_value->dump(builder, indent + 2));
  559. return {};
  560. }
  561. ErrorOr<NonnullOwnPtr<AbsCalculationNode>> AbsCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  562. {
  563. return adopt_nonnull_own_or_enomem(new (nothrow) AbsCalculationNode(move(value)));
  564. }
  565. AbsCalculationNode::AbsCalculationNode(NonnullOwnPtr<CalculationNode> value)
  566. : CalculationNode(Type::Abs)
  567. , m_value(move(value))
  568. {
  569. }
  570. AbsCalculationNode::~AbsCalculationNode() = default;
  571. ErrorOr<String> AbsCalculationNode::to_string() const
  572. {
  573. StringBuilder builder;
  574. builder.append("abs("sv);
  575. builder.append(TRY(m_value->to_string()));
  576. builder.append(")"sv);
  577. return builder.to_string();
  578. }
  579. Optional<CalculatedStyleValue::ResolvedType> AbsCalculationNode::resolved_type() const
  580. {
  581. return m_value->resolved_type();
  582. }
  583. bool AbsCalculationNode::contains_percentage() const
  584. {
  585. return m_value->contains_percentage();
  586. }
  587. CalculatedStyleValue::CalculationResult AbsCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  588. {
  589. auto resolved_type = m_value->resolved_type().value();
  590. auto node_a = m_value->resolve(context, percentage_basis);
  591. auto node_a_value = resolve_value(node_a.value(), context);
  592. if (node_a_value < 0)
  593. return to_resolved_type(resolved_type, -node_a_value);
  594. return node_a;
  595. }
  596. ErrorOr<void> AbsCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  597. {
  598. TRY(m_value->for_each_child_node(callback));
  599. TRY(callback(m_value));
  600. return {};
  601. }
  602. ErrorOr<void> AbsCalculationNode::dump(StringBuilder& builder, int indent) const
  603. {
  604. TRY(builder.try_appendff("{: >{}}ABS: {}\n", "", indent, TRY(to_string())));
  605. return {};
  606. }
  607. ErrorOr<NonnullOwnPtr<SignCalculationNode>> SignCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  608. {
  609. return adopt_nonnull_own_or_enomem(new (nothrow) SignCalculationNode(move(value)));
  610. }
  611. SignCalculationNode::SignCalculationNode(NonnullOwnPtr<CalculationNode> value)
  612. : CalculationNode(Type::Sign)
  613. , m_value(move(value))
  614. {
  615. }
  616. SignCalculationNode::~SignCalculationNode() = default;
  617. ErrorOr<String> SignCalculationNode::to_string() const
  618. {
  619. StringBuilder builder;
  620. builder.append("sign("sv);
  621. builder.append(TRY(m_value->to_string()));
  622. builder.append(")"sv);
  623. return builder.to_string();
  624. }
  625. Optional<CalculatedStyleValue::ResolvedType> SignCalculationNode::resolved_type() const
  626. {
  627. return CalculatedStyleValue::ResolvedType::Integer;
  628. }
  629. bool SignCalculationNode::contains_percentage() const
  630. {
  631. return m_value->contains_percentage();
  632. }
  633. CalculatedStyleValue::CalculationResult SignCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  634. {
  635. auto node_a = m_value->resolve(context, percentage_basis);
  636. auto node_a_value = resolve_value(node_a.value(), context);
  637. if (node_a_value < 0)
  638. return { Number(Number::Type::Integer, -1) };
  639. if (node_a_value > 0)
  640. return { Number(Number::Type::Integer, 1) };
  641. return { Number(Number::Type::Integer, 0) };
  642. }
  643. ErrorOr<void> SignCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  644. {
  645. TRY(m_value->for_each_child_node(callback));
  646. TRY(callback(m_value));
  647. return {};
  648. }
  649. ErrorOr<void> SignCalculationNode::dump(StringBuilder& builder, int indent) const
  650. {
  651. TRY(builder.try_appendff("{: >{}}SIGN: {}\n", "", indent, TRY(to_string())));
  652. return {};
  653. }
  654. ErrorOr<NonnullOwnPtr<ConstantCalculationNode>> ConstantCalculationNode::create(ConstantType constant)
  655. {
  656. return adopt_nonnull_own_or_enomem(new (nothrow) ConstantCalculationNode(constant));
  657. }
  658. ConstantCalculationNode::ConstantCalculationNode(ConstantType constant)
  659. : CalculationNode(Type::Constant)
  660. , m_constant(constant)
  661. {
  662. }
  663. ConstantCalculationNode::~ConstantCalculationNode() = default;
  664. ErrorOr<String> ConstantCalculationNode::to_string() const
  665. {
  666. switch (m_constant) {
  667. case CalculationNode::ConstantType::E:
  668. return "e"_short_string;
  669. case CalculationNode::ConstantType::PI:
  670. return "pi"_short_string;
  671. case CalculationNode::ConstantType::Infinity:
  672. return "infinity"_string;
  673. case CalculationNode::ConstantType::MinusInfinity:
  674. return "-infinity"_string;
  675. case CalculationNode::ConstantType::NaN:
  676. return "NaN"_string;
  677. }
  678. VERIFY_NOT_REACHED();
  679. }
  680. Optional<CalculatedStyleValue::ResolvedType> ConstantCalculationNode::resolved_type() const
  681. {
  682. return CalculatedStyleValue::ResolvedType::Number;
  683. }
  684. CalculatedStyleValue::CalculationResult ConstantCalculationNode::resolve([[maybe_unused]] Optional<Length::ResolutionContext const&> context, [[maybe_unused]] CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  685. {
  686. switch (m_constant) {
  687. case CalculationNode::ConstantType::E:
  688. return { Number(Number::Type::Number, M_E) };
  689. case CalculationNode::ConstantType::PI:
  690. return { Number(Number::Type::Number, M_PI) };
  691. // FIXME: We need to keep track of Infinity and NaN across all nodes, since they require special handling.
  692. case CalculationNode::ConstantType::Infinity:
  693. return { Number(Number::Type::Number, NumericLimits<float>::max()) };
  694. case CalculationNode::ConstantType::MinusInfinity:
  695. return { Number(Number::Type::Number, NumericLimits<float>::lowest()) };
  696. case CalculationNode::ConstantType::NaN:
  697. return { Number(Number::Type::Number, NAN) };
  698. }
  699. VERIFY_NOT_REACHED();
  700. }
  701. ErrorOr<void> ConstantCalculationNode::for_each_child_node([[maybe_unused]] Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  702. {
  703. return {};
  704. }
  705. ErrorOr<void> ConstantCalculationNode::dump(StringBuilder& builder, int indent) const
  706. {
  707. TRY(builder.try_appendff("{: >{}}CONSTANT: {}\n", "", indent, TRY(to_string())));
  708. return {};
  709. }
  710. ErrorOr<NonnullOwnPtr<SinCalculationNode>> SinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  711. {
  712. return adopt_nonnull_own_or_enomem(new (nothrow) SinCalculationNode(move(value)));
  713. }
  714. SinCalculationNode::SinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  715. : CalculationNode(Type::Sin)
  716. , m_value(move(value))
  717. {
  718. }
  719. SinCalculationNode::~SinCalculationNode() = default;
  720. ErrorOr<String> SinCalculationNode::to_string() const
  721. {
  722. StringBuilder builder;
  723. builder.append("sin("sv);
  724. builder.append(TRY(m_value->to_string()));
  725. builder.append(")"sv);
  726. return builder.to_string();
  727. }
  728. Optional<CalculatedStyleValue::ResolvedType> SinCalculationNode::resolved_type() const
  729. {
  730. return CalculatedStyleValue::ResolvedType::Number;
  731. }
  732. bool SinCalculationNode::contains_percentage() const
  733. {
  734. return m_value->contains_percentage();
  735. }
  736. CalculatedStyleValue::CalculationResult SinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  737. {
  738. auto node_a = m_value->resolve(context, percentage_basis);
  739. auto node_a_value = resolve_value_radians(node_a.value());
  740. auto result = sin(node_a_value);
  741. return { Number(Number::Type::Number, result) };
  742. }
  743. ErrorOr<void> SinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  744. {
  745. TRY(m_value->for_each_child_node(callback));
  746. TRY(callback(m_value));
  747. return {};
  748. }
  749. ErrorOr<void> SinCalculationNode::dump(StringBuilder& builder, int indent) const
  750. {
  751. TRY(builder.try_appendff("{: >{}}SIN: {}\n", "", indent, TRY(to_string())));
  752. return {};
  753. }
  754. ErrorOr<NonnullOwnPtr<CosCalculationNode>> CosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  755. {
  756. return adopt_nonnull_own_or_enomem(new (nothrow) CosCalculationNode(move(value)));
  757. }
  758. CosCalculationNode::CosCalculationNode(NonnullOwnPtr<CalculationNode> value)
  759. : CalculationNode(Type::Cos)
  760. , m_value(move(value))
  761. {
  762. }
  763. CosCalculationNode::~CosCalculationNode() = default;
  764. ErrorOr<String> CosCalculationNode::to_string() const
  765. {
  766. StringBuilder builder;
  767. builder.append("cos("sv);
  768. builder.append(TRY(m_value->to_string()));
  769. builder.append(")"sv);
  770. return builder.to_string();
  771. }
  772. Optional<CalculatedStyleValue::ResolvedType> CosCalculationNode::resolved_type() const
  773. {
  774. return CalculatedStyleValue::ResolvedType::Number;
  775. }
  776. bool CosCalculationNode::contains_percentage() const
  777. {
  778. return m_value->contains_percentage();
  779. }
  780. CalculatedStyleValue::CalculationResult CosCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  781. {
  782. auto node_a = m_value->resolve(context, percentage_basis);
  783. auto node_a_value = resolve_value_radians(node_a.value());
  784. auto result = cos(node_a_value);
  785. return { Number(Number::Type::Number, result) };
  786. }
  787. ErrorOr<void> CosCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  788. {
  789. TRY(m_value->for_each_child_node(callback));
  790. TRY(callback(m_value));
  791. return {};
  792. }
  793. ErrorOr<void> CosCalculationNode::dump(StringBuilder& builder, int indent) const
  794. {
  795. TRY(builder.try_appendff("{: >{}}COS: {}\n", "", indent, TRY(to_string())));
  796. return {};
  797. }
  798. ErrorOr<NonnullOwnPtr<TanCalculationNode>> TanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  799. {
  800. return adopt_nonnull_own_or_enomem(new (nothrow) TanCalculationNode(move(value)));
  801. }
  802. TanCalculationNode::TanCalculationNode(NonnullOwnPtr<CalculationNode> value)
  803. : CalculationNode(Type::Tan)
  804. , m_value(move(value))
  805. {
  806. }
  807. TanCalculationNode::~TanCalculationNode() = default;
  808. ErrorOr<String> TanCalculationNode::to_string() const
  809. {
  810. StringBuilder builder;
  811. builder.append("tan("sv);
  812. builder.append(TRY(m_value->to_string()));
  813. builder.append(")"sv);
  814. return builder.to_string();
  815. }
  816. Optional<CalculatedStyleValue::ResolvedType> TanCalculationNode::resolved_type() const
  817. {
  818. return CalculatedStyleValue::ResolvedType::Number;
  819. }
  820. bool TanCalculationNode::contains_percentage() const
  821. {
  822. return m_value->contains_percentage();
  823. }
  824. CalculatedStyleValue::CalculationResult TanCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  825. {
  826. auto node_a = m_value->resolve(context, percentage_basis);
  827. auto node_a_value = resolve_value_radians(node_a.value());
  828. auto result = tan(node_a_value);
  829. return { Number(Number::Type::Number, result) };
  830. }
  831. ErrorOr<void> TanCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  832. {
  833. TRY(m_value->for_each_child_node(callback));
  834. TRY(callback(m_value));
  835. return {};
  836. }
  837. ErrorOr<void> TanCalculationNode::dump(StringBuilder& builder, int indent) const
  838. {
  839. TRY(builder.try_appendff("{: >{}}TAN: {}\n", "", indent, TRY(to_string())));
  840. return {};
  841. }
  842. ErrorOr<NonnullOwnPtr<AsinCalculationNode>> AsinCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  843. {
  844. return adopt_nonnull_own_or_enomem(new (nothrow) AsinCalculationNode(move(value)));
  845. }
  846. AsinCalculationNode::AsinCalculationNode(NonnullOwnPtr<CalculationNode> value)
  847. : CalculationNode(Type::Asin)
  848. , m_value(move(value))
  849. {
  850. }
  851. AsinCalculationNode::~AsinCalculationNode() = default;
  852. ErrorOr<String> AsinCalculationNode::to_string() const
  853. {
  854. StringBuilder builder;
  855. builder.append("asin("sv);
  856. builder.append(TRY(m_value->to_string()));
  857. builder.append(")"sv);
  858. return builder.to_string();
  859. }
  860. Optional<CalculatedStyleValue::ResolvedType> AsinCalculationNode::resolved_type() const
  861. {
  862. return CalculatedStyleValue::ResolvedType::Angle;
  863. }
  864. bool AsinCalculationNode::contains_percentage() const
  865. {
  866. return m_value->contains_percentage();
  867. }
  868. CalculatedStyleValue::CalculationResult AsinCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  869. {
  870. auto node_a = m_value->resolve(context, percentage_basis);
  871. auto node_a_value = resolve_value(node_a.value(), context);
  872. auto result = asin(node_a_value);
  873. return { Angle(result, Angle::Type::Rad) };
  874. }
  875. ErrorOr<void> AsinCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  876. {
  877. TRY(m_value->for_each_child_node(callback));
  878. TRY(callback(m_value));
  879. return {};
  880. }
  881. ErrorOr<void> AsinCalculationNode::dump(StringBuilder& builder, int indent) const
  882. {
  883. TRY(builder.try_appendff("{: >{}}ASIN: {}\n", "", indent, TRY(to_string())));
  884. return {};
  885. }
  886. ErrorOr<NonnullOwnPtr<AcosCalculationNode>> AcosCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  887. {
  888. return adopt_nonnull_own_or_enomem(new (nothrow) AcosCalculationNode(move(value)));
  889. }
  890. AcosCalculationNode::AcosCalculationNode(NonnullOwnPtr<CalculationNode> value)
  891. : CalculationNode(Type::Acos)
  892. , m_value(move(value))
  893. {
  894. }
  895. AcosCalculationNode::~AcosCalculationNode() = default;
  896. ErrorOr<String> AcosCalculationNode::to_string() const
  897. {
  898. StringBuilder builder;
  899. builder.append("acos("sv);
  900. builder.append(TRY(m_value->to_string()));
  901. builder.append(")"sv);
  902. return builder.to_string();
  903. }
  904. Optional<CalculatedStyleValue::ResolvedType> AcosCalculationNode::resolved_type() const
  905. {
  906. return CalculatedStyleValue::ResolvedType::Angle;
  907. }
  908. bool AcosCalculationNode::contains_percentage() const
  909. {
  910. return m_value->contains_percentage();
  911. }
  912. CalculatedStyleValue::CalculationResult AcosCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  913. {
  914. auto node_a = m_value->resolve(context, percentage_basis);
  915. auto node_a_value = resolve_value(node_a.value(), context);
  916. auto result = acos(node_a_value);
  917. return { Angle(result, Angle::Type::Rad) };
  918. }
  919. ErrorOr<void> AcosCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  920. {
  921. TRY(m_value->for_each_child_node(callback));
  922. TRY(callback(m_value));
  923. return {};
  924. }
  925. ErrorOr<void> AcosCalculationNode::dump(StringBuilder& builder, int indent) const
  926. {
  927. TRY(builder.try_appendff("{: >{}}ACOS: {}\n", "", indent, TRY(to_string())));
  928. return {};
  929. }
  930. ErrorOr<NonnullOwnPtr<AtanCalculationNode>> AtanCalculationNode::create(NonnullOwnPtr<CalculationNode> value)
  931. {
  932. return adopt_nonnull_own_or_enomem(new (nothrow) AtanCalculationNode(move(value)));
  933. }
  934. AtanCalculationNode::AtanCalculationNode(NonnullOwnPtr<CalculationNode> value)
  935. : CalculationNode(Type::Atan)
  936. , m_value(move(value))
  937. {
  938. }
  939. AtanCalculationNode::~AtanCalculationNode() = default;
  940. ErrorOr<String> AtanCalculationNode::to_string() const
  941. {
  942. StringBuilder builder;
  943. builder.append("atan("sv);
  944. builder.append(TRY(m_value->to_string()));
  945. builder.append(")"sv);
  946. return builder.to_string();
  947. }
  948. Optional<CalculatedStyleValue::ResolvedType> AtanCalculationNode::resolved_type() const
  949. {
  950. return CalculatedStyleValue::ResolvedType::Angle;
  951. }
  952. bool AtanCalculationNode::contains_percentage() const
  953. {
  954. return m_value->contains_percentage();
  955. }
  956. CalculatedStyleValue::CalculationResult AtanCalculationNode::resolve(Optional<Length::ResolutionContext const&> context, CalculatedStyleValue::PercentageBasis const& percentage_basis) const
  957. {
  958. auto node_a = m_value->resolve(context, percentage_basis);
  959. auto node_a_value = resolve_value(node_a.value(), context);
  960. auto result = atan(node_a_value);
  961. return { Angle(result, Angle::Type::Rad) };
  962. }
  963. ErrorOr<void> AtanCalculationNode::for_each_child_node(Function<ErrorOr<void>(NonnullOwnPtr<CalculationNode>&)> const& callback)
  964. {
  965. TRY(m_value->for_each_child_node(callback));
  966. TRY(callback(m_value));
  967. return {};
  968. }
  969. ErrorOr<void> AtanCalculationNode::dump(StringBuilder& builder, int indent) const
  970. {
  971. TRY(builder.try_appendff("{: >{}}ATAN: {}\n", "", indent, TRY(to_string())));
  972. return {};
  973. }
  974. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  975. {
  976. add_or_subtract_internal(SumOperation::Add, other, context, percentage_basis);
  977. }
  978. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  979. {
  980. add_or_subtract_internal(SumOperation::Subtract, other, context, percentage_basis);
  981. }
  982. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Optional<Length::ResolutionContext const&> context, PercentageBasis const& percentage_basis)
  983. {
  984. // 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>".
  985. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  986. // Note: This is almost identical to ::add()
  987. m_value.visit(
  988. [&](Number const& number) {
  989. auto other_number = other.m_value.get<Number>();
  990. if (op == SumOperation::Add) {
  991. m_value = number + other_number;
  992. } else {
  993. m_value = number - other_number;
  994. }
  995. },
  996. [&](Angle const& angle) {
  997. auto this_degrees = angle.to_degrees();
  998. if (other.m_value.has<Angle>()) {
  999. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  1000. if (op == SumOperation::Add)
  1001. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1002. else
  1003. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1004. } else {
  1005. VERIFY(percentage_basis.has<Angle>());
  1006. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  1007. if (op == SumOperation::Add)
  1008. m_value = Angle::make_degrees(this_degrees + other_degrees);
  1009. else
  1010. m_value = Angle::make_degrees(this_degrees - other_degrees);
  1011. }
  1012. },
  1013. [&](Frequency const& frequency) {
  1014. auto this_hertz = frequency.to_hertz();
  1015. if (other.m_value.has<Frequency>()) {
  1016. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  1017. if (op == SumOperation::Add)
  1018. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1019. else
  1020. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1021. } else {
  1022. VERIFY(percentage_basis.has<Frequency>());
  1023. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  1024. if (op == SumOperation::Add)
  1025. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  1026. else
  1027. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  1028. }
  1029. },
  1030. [&](Length const& length) {
  1031. auto this_px = length.to_px(*context);
  1032. if (other.m_value.has<Length>()) {
  1033. auto other_px = other.m_value.get<Length>().to_px(*context);
  1034. if (op == SumOperation::Add)
  1035. m_value = Length::make_px(this_px + other_px);
  1036. else
  1037. m_value = Length::make_px(this_px - other_px);
  1038. } else {
  1039. VERIFY(percentage_basis.has<Length>());
  1040. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*context);
  1041. if (op == SumOperation::Add)
  1042. m_value = Length::make_px(this_px + other_px);
  1043. else
  1044. m_value = Length::make_px(this_px - other_px);
  1045. }
  1046. },
  1047. [&](Time const& time) {
  1048. auto this_seconds = time.to_seconds();
  1049. if (other.m_value.has<Time>()) {
  1050. auto other_seconds = other.m_value.get<Time>().to_seconds();
  1051. if (op == SumOperation::Add)
  1052. m_value = Time::make_seconds(this_seconds + other_seconds);
  1053. else
  1054. m_value = Time::make_seconds(this_seconds - other_seconds);
  1055. } else {
  1056. VERIFY(percentage_basis.has<Time>());
  1057. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  1058. if (op == SumOperation::Add)
  1059. m_value = Time::make_seconds(this_seconds + other_seconds);
  1060. else
  1061. m_value = Time::make_seconds(this_seconds - other_seconds);
  1062. }
  1063. },
  1064. [&](Percentage const& percentage) {
  1065. if (other.m_value.has<Percentage>()) {
  1066. if (op == SumOperation::Add)
  1067. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  1068. else
  1069. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  1070. return;
  1071. }
  1072. // 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`.
  1073. CalculationResult new_value = other;
  1074. if (op == SumOperation::Add) {
  1075. new_value.add(*this, context, percentage_basis);
  1076. } else {
  1077. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  1078. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, context);
  1079. new_value.add(*this, context, percentage_basis);
  1080. }
  1081. *this = new_value;
  1082. });
  1083. }
  1084. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1085. {
  1086. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  1087. // Both of these are represented as a double.
  1088. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  1089. bool other_is_number = other.m_value.has<Number>();
  1090. m_value.visit(
  1091. [&](Number const& number) {
  1092. if (other_is_number) {
  1093. m_value = number * other.m_value.get<Number>();
  1094. } else {
  1095. // Avoid duplicating all the logic by swapping `this` and `other`.
  1096. CalculationResult new_value = other;
  1097. new_value.multiply_by(*this, context);
  1098. *this = new_value;
  1099. }
  1100. },
  1101. [&](Angle const& angle) {
  1102. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  1103. },
  1104. [&](Frequency const& frequency) {
  1105. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  1106. },
  1107. [&](Length const& length) {
  1108. m_value = Length::make_px(length.to_px(*context) * static_cast<double>(other.m_value.get<Number>().value()));
  1109. },
  1110. [&](Time const& time) {
  1111. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  1112. },
  1113. [&](Percentage const& percentage) {
  1114. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  1115. });
  1116. }
  1117. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Optional<Length::ResolutionContext const&> context)
  1118. {
  1119. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  1120. // Both of these are represented as a Number.
  1121. auto denominator = other.m_value.get<Number>().value();
  1122. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  1123. VERIFY(denominator != 0.0);
  1124. m_value.visit(
  1125. [&](Number const& number) {
  1126. m_value = Number {
  1127. Number::Type::Number,
  1128. number.value() / denominator
  1129. };
  1130. },
  1131. [&](Angle const& angle) {
  1132. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  1133. },
  1134. [&](Frequency const& frequency) {
  1135. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  1136. },
  1137. [&](Length const& length) {
  1138. m_value = Length::make_px(length.to_px(*context) / static_cast<double>(denominator));
  1139. },
  1140. [&](Time const& time) {
  1141. m_value = Time::make_seconds(time.to_seconds() / denominator);
  1142. },
  1143. [&](Percentage const& percentage) {
  1144. m_value = Percentage { percentage.value() / denominator };
  1145. });
  1146. }
  1147. void CalculatedStyleValue::CalculationResult::negate()
  1148. {
  1149. m_value.visit(
  1150. [&](Number const& number) {
  1151. m_value = Number { number.type(), 0 - number.value() };
  1152. },
  1153. [&](Angle const& angle) {
  1154. m_value = Angle { 0 - angle.raw_value(), angle.type() };
  1155. },
  1156. [&](Frequency const& frequency) {
  1157. m_value = Frequency { 0 - frequency.raw_value(), frequency.type() };
  1158. },
  1159. [&](Length const& length) {
  1160. m_value = Length { 0 - length.raw_value(), length.type() };
  1161. },
  1162. [&](Time const& time) {
  1163. m_value = Time { 0 - time.raw_value(), time.type() };
  1164. },
  1165. [&](Percentage const& percentage) {
  1166. m_value = Percentage { 0 - percentage.value() };
  1167. });
  1168. }
  1169. void CalculatedStyleValue::CalculationResult::invert()
  1170. {
  1171. // FIXME: Correctly handle division by zero.
  1172. m_value.visit(
  1173. [&](Number const& number) {
  1174. m_value = Number { Number::Type::Number, 1 / number.value() };
  1175. },
  1176. [&](Angle const& angle) {
  1177. m_value = Angle { 1 / angle.raw_value(), angle.type() };
  1178. },
  1179. [&](Frequency const& frequency) {
  1180. m_value = Frequency { 1 / frequency.raw_value(), frequency.type() };
  1181. },
  1182. [&](Length const& length) {
  1183. m_value = Length { 1 / length.raw_value(), length.type() };
  1184. },
  1185. [&](Time const& time) {
  1186. m_value = Time { 1 / time.raw_value(), time.type() };
  1187. },
  1188. [&](Percentage const& percentage) {
  1189. m_value = Percentage { 1 / percentage.value() };
  1190. });
  1191. }
  1192. ErrorOr<String> CalculatedStyleValue::to_string() const
  1193. {
  1194. // FIXME: Implement this according to https://www.w3.org/TR/css-values-4/#calc-serialize once that stabilizes.
  1195. return String::formatted("calc({})", TRY(m_calculation->to_string()));
  1196. }
  1197. bool CalculatedStyleValue::equals(StyleValue const& other) const
  1198. {
  1199. if (type() != other.type())
  1200. return false;
  1201. // This is a case where comparing the strings actually makes sense.
  1202. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  1203. }
  1204. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  1205. {
  1206. auto result = m_calculation->resolve({}, {});
  1207. if (result.value().has<Angle>())
  1208. return result.value().get<Angle>();
  1209. return {};
  1210. }
  1211. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  1212. {
  1213. auto result = m_calculation->resolve({}, percentage_basis);
  1214. return result.value().visit(
  1215. [&](Angle const& angle) -> Optional<Angle> {
  1216. return angle;
  1217. },
  1218. [&](Percentage const& percentage) -> Optional<Angle> {
  1219. return percentage_basis.percentage_of(percentage);
  1220. },
  1221. [&](auto const&) -> Optional<Angle> {
  1222. return {};
  1223. });
  1224. }
  1225. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  1226. {
  1227. auto result = m_calculation->resolve({}, {});
  1228. if (result.value().has<Frequency>())
  1229. return result.value().get<Frequency>();
  1230. return {};
  1231. }
  1232. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  1233. {
  1234. auto result = m_calculation->resolve({}, percentage_basis);
  1235. return result.value().visit(
  1236. [&](Frequency const& frequency) -> Optional<Frequency> {
  1237. return frequency;
  1238. },
  1239. [&](Percentage const& percentage) -> Optional<Frequency> {
  1240. return percentage_basis.percentage_of(percentage);
  1241. },
  1242. [&](auto const&) -> Optional<Frequency> {
  1243. return {};
  1244. });
  1245. }
  1246. Optional<Length> CalculatedStyleValue::resolve_length(Length::ResolutionContext const& context) const
  1247. {
  1248. auto result = m_calculation->resolve(context, {});
  1249. if (result.value().has<Length>())
  1250. return result.value().get<Length>();
  1251. return {};
  1252. }
  1253. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  1254. {
  1255. return resolve_length(Length::ResolutionContext::for_layout_node(layout_node));
  1256. }
  1257. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  1258. {
  1259. auto result = m_calculation->resolve(Length::ResolutionContext::for_layout_node(layout_node), percentage_basis);
  1260. return result.value().visit(
  1261. [&](Length const& length) -> Optional<Length> {
  1262. return length;
  1263. },
  1264. [&](Percentage const& percentage) -> Optional<Length> {
  1265. return percentage_basis.percentage_of(percentage);
  1266. },
  1267. [&](auto const&) -> Optional<Length> {
  1268. return {};
  1269. });
  1270. }
  1271. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  1272. {
  1273. auto result = m_calculation->resolve({}, {});
  1274. if (result.value().has<Percentage>())
  1275. return result.value().get<Percentage>();
  1276. return {};
  1277. }
  1278. Optional<Time> CalculatedStyleValue::resolve_time() const
  1279. {
  1280. auto result = m_calculation->resolve({}, {});
  1281. if (result.value().has<Time>())
  1282. return result.value().get<Time>();
  1283. return {};
  1284. }
  1285. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  1286. {
  1287. auto result = m_calculation->resolve({}, percentage_basis);
  1288. return result.value().visit(
  1289. [&](Time const& time) -> Optional<Time> {
  1290. return time;
  1291. },
  1292. [&](auto const&) -> Optional<Time> {
  1293. return {};
  1294. });
  1295. }
  1296. Optional<double> CalculatedStyleValue::resolve_number() const
  1297. {
  1298. auto result = m_calculation->resolve({}, {});
  1299. if (result.value().has<Number>())
  1300. return result.value().get<Number>().value();
  1301. return {};
  1302. }
  1303. Optional<i64> CalculatedStyleValue::resolve_integer()
  1304. {
  1305. auto result = m_calculation->resolve({}, {});
  1306. if (result.value().has<Number>())
  1307. return result.value().get<Number>().integer_value();
  1308. return {};
  1309. }
  1310. bool CalculatedStyleValue::contains_percentage() const
  1311. {
  1312. return m_calculation->contains_percentage();
  1313. }
  1314. }