StyleValue.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/ByteBuffer.h>
  9. #include <LibGfx/Palette.h>
  10. #include <LibWeb/CSS/Serialize.h>
  11. #include <LibWeb/CSS/StyleValue.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/BrowsingContext.h>
  14. #include <LibWeb/Loader/LoadRequest.h>
  15. #include <LibWeb/Loader/ResourceLoader.h>
  16. #include <LibWeb/Page/Page.h>
  17. #include <LibWeb/Painting/GradientPainting.h>
  18. namespace Web::CSS {
  19. StyleValue::StyleValue(Type type)
  20. : m_type(type)
  21. {
  22. }
  23. AbstractImageStyleValue const& StyleValue::as_abstract_image() const
  24. {
  25. VERIFY(is_abstract_image());
  26. return static_cast<AbstractImageStyleValue const&>(*this);
  27. }
  28. AngleStyleValue const& StyleValue::as_angle() const
  29. {
  30. VERIFY(is_angle());
  31. return static_cast<AngleStyleValue const&>(*this);
  32. }
  33. BackgroundStyleValue const& StyleValue::as_background() const
  34. {
  35. VERIFY(is_background());
  36. return static_cast<BackgroundStyleValue const&>(*this);
  37. }
  38. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  39. {
  40. VERIFY(is_background_repeat());
  41. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  42. }
  43. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  44. {
  45. VERIFY(is_background_size());
  46. return static_cast<BackgroundSizeStyleValue const&>(*this);
  47. }
  48. BorderStyleValue const& StyleValue::as_border() const
  49. {
  50. VERIFY(is_border());
  51. return static_cast<BorderStyleValue const&>(*this);
  52. }
  53. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  54. {
  55. VERIFY(is_border_radius());
  56. return static_cast<BorderRadiusStyleValue const&>(*this);
  57. }
  58. BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const
  59. {
  60. VERIFY(is_border_radius_shorthand());
  61. return static_cast<BorderRadiusShorthandStyleValue const&>(*this);
  62. }
  63. ShadowStyleValue const& StyleValue::as_shadow() const
  64. {
  65. VERIFY(is_shadow());
  66. return static_cast<ShadowStyleValue const&>(*this);
  67. }
  68. CalculatedStyleValue const& StyleValue::as_calculated() const
  69. {
  70. VERIFY(is_calculated());
  71. return static_cast<CalculatedStyleValue const&>(*this);
  72. }
  73. ColorStyleValue const& StyleValue::as_color() const
  74. {
  75. VERIFY(is_color());
  76. return static_cast<ColorStyleValue const&>(*this);
  77. }
  78. ContentStyleValue const& StyleValue::as_content() const
  79. {
  80. VERIFY(is_content());
  81. return static_cast<ContentStyleValue const&>(*this);
  82. }
  83. FlexStyleValue const& StyleValue::as_flex() const
  84. {
  85. VERIFY(is_flex());
  86. return static_cast<FlexStyleValue const&>(*this);
  87. }
  88. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  89. {
  90. VERIFY(is_flex_flow());
  91. return static_cast<FlexFlowStyleValue const&>(*this);
  92. }
  93. FontStyleValue const& StyleValue::as_font() const
  94. {
  95. VERIFY(is_font());
  96. return static_cast<FontStyleValue const&>(*this);
  97. }
  98. FrequencyStyleValue const& StyleValue::as_frequency() const
  99. {
  100. VERIFY(is_frequency());
  101. return static_cast<FrequencyStyleValue const&>(*this);
  102. }
  103. IdentifierStyleValue const& StyleValue::as_identifier() const
  104. {
  105. VERIFY(is_identifier());
  106. return static_cast<IdentifierStyleValue const&>(*this);
  107. }
  108. ImageStyleValue const& StyleValue::as_image() const
  109. {
  110. VERIFY(is_image());
  111. return static_cast<ImageStyleValue const&>(*this);
  112. }
  113. InheritStyleValue const& StyleValue::as_inherit() const
  114. {
  115. VERIFY(is_inherit());
  116. return static_cast<InheritStyleValue const&>(*this);
  117. }
  118. InitialStyleValue const& StyleValue::as_initial() const
  119. {
  120. VERIFY(is_initial());
  121. return static_cast<InitialStyleValue const&>(*this);
  122. }
  123. LengthStyleValue const& StyleValue::as_length() const
  124. {
  125. VERIFY(is_length());
  126. return static_cast<LengthStyleValue const&>(*this);
  127. }
  128. LinearGradientStyleValue const& StyleValue::as_linear_gradient() const
  129. {
  130. VERIFY(is_linear_gradient());
  131. return static_cast<LinearGradientStyleValue const&>(*this);
  132. }
  133. ListStyleStyleValue const& StyleValue::as_list_style() const
  134. {
  135. VERIFY(is_list_style());
  136. return static_cast<ListStyleStyleValue const&>(*this);
  137. }
  138. NumericStyleValue const& StyleValue::as_numeric() const
  139. {
  140. VERIFY(is_numeric());
  141. return static_cast<NumericStyleValue const&>(*this);
  142. }
  143. OverflowStyleValue const& StyleValue::as_overflow() const
  144. {
  145. VERIFY(is_overflow());
  146. return static_cast<OverflowStyleValue const&>(*this);
  147. }
  148. PercentageStyleValue const& StyleValue::as_percentage() const
  149. {
  150. VERIFY(is_percentage());
  151. return static_cast<PercentageStyleValue const&>(*this);
  152. }
  153. PositionStyleValue const& StyleValue::as_position() const
  154. {
  155. VERIFY(is_position());
  156. return static_cast<PositionStyleValue const&>(*this);
  157. }
  158. RectStyleValue const& StyleValue::as_rect() const
  159. {
  160. VERIFY(is_rect());
  161. return static_cast<RectStyleValue const&>(*this);
  162. }
  163. ResolutionStyleValue const& StyleValue::as_resolution() const
  164. {
  165. VERIFY(is_resolution());
  166. return static_cast<ResolutionStyleValue const&>(*this);
  167. }
  168. StringStyleValue const& StyleValue::as_string() const
  169. {
  170. VERIFY(is_string());
  171. return static_cast<StringStyleValue const&>(*this);
  172. }
  173. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  174. {
  175. VERIFY(is_text_decoration());
  176. return static_cast<TextDecorationStyleValue const&>(*this);
  177. }
  178. TimeStyleValue const& StyleValue::as_time() const
  179. {
  180. VERIFY(is_time());
  181. return static_cast<TimeStyleValue const&>(*this);
  182. }
  183. TransformationStyleValue const& StyleValue::as_transformation() const
  184. {
  185. VERIFY(is_transformation());
  186. return static_cast<TransformationStyleValue const&>(*this);
  187. }
  188. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  189. {
  190. VERIFY(is_unresolved());
  191. return static_cast<UnresolvedStyleValue const&>(*this);
  192. }
  193. UnsetStyleValue const& StyleValue::as_unset() const
  194. {
  195. VERIFY(is_unset());
  196. return static_cast<UnsetStyleValue const&>(*this);
  197. }
  198. StyleValueList const& StyleValue::as_value_list() const
  199. {
  200. VERIFY(is_value_list());
  201. return static_cast<StyleValueList const&>(*this);
  202. }
  203. BackgroundStyleValue::BackgroundStyleValue(
  204. NonnullRefPtr<StyleValue> color,
  205. NonnullRefPtr<StyleValue> image,
  206. NonnullRefPtr<StyleValue> position,
  207. NonnullRefPtr<StyleValue> size,
  208. NonnullRefPtr<StyleValue> repeat,
  209. NonnullRefPtr<StyleValue> attachment,
  210. NonnullRefPtr<StyleValue> origin,
  211. NonnullRefPtr<StyleValue> clip)
  212. : StyleValue(Type::Background)
  213. , m_color(color)
  214. , m_image(image)
  215. , m_position(position)
  216. , m_size(size)
  217. , m_repeat(repeat)
  218. , m_attachment(attachment)
  219. , m_origin(origin)
  220. , m_clip(clip)
  221. {
  222. auto layer_count = [](auto style_value) -> size_t {
  223. if (style_value->is_value_list())
  224. return style_value->as_value_list().size();
  225. else
  226. return 1;
  227. };
  228. m_layer_count = max(layer_count(m_image), layer_count(m_position));
  229. m_layer_count = max(m_layer_count, layer_count(m_size));
  230. m_layer_count = max(m_layer_count, layer_count(m_repeat));
  231. m_layer_count = max(m_layer_count, layer_count(m_attachment));
  232. m_layer_count = max(m_layer_count, layer_count(m_origin));
  233. m_layer_count = max(m_layer_count, layer_count(m_clip));
  234. VERIFY(!m_color->is_value_list());
  235. }
  236. String BackgroundStyleValue::to_string() const
  237. {
  238. if (m_layer_count == 1) {
  239. return String::formatted("{} {} {} {} {} {} {} {}", m_color->to_string(), m_image->to_string(), m_position->to_string(), m_size->to_string(), m_repeat->to_string(), m_attachment->to_string(), m_origin->to_string(), m_clip->to_string());
  240. }
  241. auto get_layer_value_string = [](NonnullRefPtr<StyleValue> const& style_value, size_t index) {
  242. if (style_value->is_value_list())
  243. return style_value->as_value_list().value_at(index, true)->to_string();
  244. return style_value->to_string();
  245. };
  246. StringBuilder builder;
  247. for (size_t i = 0; i < m_layer_count; i++) {
  248. if (i)
  249. builder.append(", "sv);
  250. if (i == m_layer_count - 1)
  251. builder.appendff("{} ", m_color->to_string());
  252. builder.appendff("{} {} {} {} {} {} {}", get_layer_value_string(m_image, i), get_layer_value_string(m_position, i), get_layer_value_string(m_size, i), get_layer_value_string(m_repeat, i), get_layer_value_string(m_attachment, i), get_layer_value_string(m_origin, i), get_layer_value_string(m_clip, i));
  253. }
  254. return builder.to_string();
  255. }
  256. bool BackgroundStyleValue::equals(StyleValue const& other) const
  257. {
  258. if (type() != other.type())
  259. return false;
  260. auto const& typed_other = other.as_background();
  261. return m_color->equals(typed_other.m_color)
  262. && m_image->equals(typed_other.m_image)
  263. && m_position->equals(typed_other.m_position)
  264. && m_size->equals(typed_other.m_size)
  265. && m_repeat->equals(typed_other.m_repeat)
  266. && m_attachment->equals(typed_other.m_attachment)
  267. && m_origin->equals(typed_other.m_origin)
  268. && m_clip->equals(typed_other.m_clip);
  269. }
  270. String BackgroundRepeatStyleValue::to_string() const
  271. {
  272. return String::formatted("{} {}", CSS::to_string(m_repeat_x), CSS::to_string(m_repeat_y));
  273. }
  274. bool BackgroundRepeatStyleValue::equals(StyleValue const& other) const
  275. {
  276. if (type() != other.type())
  277. return false;
  278. auto const& typed_other = other.as_background_repeat();
  279. return m_repeat_x == typed_other.m_repeat_x && m_repeat_y == typed_other.m_repeat_y;
  280. }
  281. String BackgroundSizeStyleValue::to_string() const
  282. {
  283. return String::formatted("{} {}", m_size_x.to_string(), m_size_y.to_string());
  284. }
  285. bool BackgroundSizeStyleValue::equals(StyleValue const& other) const
  286. {
  287. if (type() != other.type())
  288. return false;
  289. auto const& typed_other = other.as_background_size();
  290. return m_size_x == typed_other.m_size_x && m_size_y == typed_other.m_size_y;
  291. }
  292. String BorderStyleValue::to_string() const
  293. {
  294. return String::formatted("{} {} {}", m_border_width->to_string(), m_border_style->to_string(), m_border_color->to_string());
  295. }
  296. bool BorderStyleValue::equals(StyleValue const& other) const
  297. {
  298. if (type() != other.type())
  299. return false;
  300. auto const& typed_other = other.as_border();
  301. return m_border_width->equals(typed_other.m_border_width)
  302. && m_border_style->equals(typed_other.m_border_style)
  303. && m_border_color->equals(typed_other.m_border_color);
  304. }
  305. String BorderRadiusStyleValue::to_string() const
  306. {
  307. if (m_horizontal_radius == m_vertical_radius)
  308. return m_horizontal_radius.to_string();
  309. return String::formatted("{} / {}", m_horizontal_radius.to_string(), m_vertical_radius.to_string());
  310. }
  311. bool BorderRadiusStyleValue::equals(StyleValue const& other) const
  312. {
  313. if (type() != other.type())
  314. return false;
  315. auto const& typed_other = other.as_border_radius();
  316. return m_is_elliptical == typed_other.m_is_elliptical
  317. && m_horizontal_radius == typed_other.m_horizontal_radius
  318. && m_vertical_radius == typed_other.m_vertical_radius;
  319. }
  320. String BorderRadiusShorthandStyleValue::to_string() const
  321. {
  322. return String::formatted("{} {} {} {} / {} {} {} {}", m_top_left->horizontal_radius().to_string(), m_top_right->horizontal_radius().to_string(), m_bottom_right->horizontal_radius().to_string(), m_bottom_left->horizontal_radius().to_string(), m_top_left->vertical_radius().to_string(), m_top_right->vertical_radius().to_string(), m_bottom_right->vertical_radius().to_string(), m_bottom_left->vertical_radius().to_string());
  323. }
  324. bool BorderRadiusShorthandStyleValue::equals(StyleValue const& other) const
  325. {
  326. if (type() != other.type())
  327. return false;
  328. auto const& typed_other = other.as_border_radius_shorthand();
  329. return m_top_left->equals(typed_other.m_top_left)
  330. && m_top_right->equals(typed_other.m_top_right)
  331. && m_bottom_right->equals(typed_other.m_bottom_right)
  332. && m_bottom_left->equals(typed_other.m_bottom_left);
  333. }
  334. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  335. {
  336. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  337. }
  338. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  339. {
  340. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  341. }
  342. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  343. {
  344. // 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>".
  345. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  346. // Note: This is almost identical to ::add()
  347. m_value.visit(
  348. [&](Number const& number) {
  349. auto other_number = other.m_value.get<Number>();
  350. if (op == SumOperation::Add) {
  351. m_value = number + other_number;
  352. } else {
  353. m_value = number - other_number;
  354. }
  355. },
  356. [&](Angle const& angle) {
  357. auto this_degrees = angle.to_degrees();
  358. if (other.m_value.has<Angle>()) {
  359. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  360. if (op == SumOperation::Add)
  361. m_value = Angle::make_degrees(this_degrees + other_degrees);
  362. else
  363. m_value = Angle::make_degrees(this_degrees - other_degrees);
  364. } else {
  365. VERIFY(percentage_basis.has<Angle>());
  366. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  367. if (op == SumOperation::Add)
  368. m_value = Angle::make_degrees(this_degrees + other_degrees);
  369. else
  370. m_value = Angle::make_degrees(this_degrees - other_degrees);
  371. }
  372. },
  373. [&](Frequency const& frequency) {
  374. auto this_hertz = frequency.to_hertz();
  375. if (other.m_value.has<Frequency>()) {
  376. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  377. if (op == SumOperation::Add)
  378. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  379. else
  380. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  381. } else {
  382. VERIFY(percentage_basis.has<Frequency>());
  383. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  384. if (op == SumOperation::Add)
  385. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  386. else
  387. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  388. }
  389. },
  390. [&](Length const& length) {
  391. auto this_px = length.to_px(*layout_node);
  392. if (other.m_value.has<Length>()) {
  393. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  394. if (op == SumOperation::Add)
  395. m_value = Length::make_px(this_px + other_px);
  396. else
  397. m_value = Length::make_px(this_px - other_px);
  398. } else {
  399. VERIFY(percentage_basis.has<Length>());
  400. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  401. if (op == SumOperation::Add)
  402. m_value = Length::make_px(this_px + other_px);
  403. else
  404. m_value = Length::make_px(this_px - other_px);
  405. }
  406. },
  407. [&](Time const& time) {
  408. auto this_seconds = time.to_seconds();
  409. if (other.m_value.has<Time>()) {
  410. auto other_seconds = other.m_value.get<Time>().to_seconds();
  411. if (op == SumOperation::Add)
  412. m_value = Time::make_seconds(this_seconds + other_seconds);
  413. else
  414. m_value = Time::make_seconds(this_seconds - other_seconds);
  415. } else {
  416. VERIFY(percentage_basis.has<Time>());
  417. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  418. if (op == SumOperation::Add)
  419. m_value = Time::make_seconds(this_seconds + other_seconds);
  420. else
  421. m_value = Time::make_seconds(this_seconds - other_seconds);
  422. }
  423. },
  424. [&](Percentage const& percentage) {
  425. if (other.m_value.has<Percentage>()) {
  426. if (op == SumOperation::Add)
  427. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  428. else
  429. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  430. return;
  431. }
  432. // 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`.
  433. CalculationResult new_value = other;
  434. if (op == SumOperation::Add) {
  435. new_value.add(*this, layout_node, percentage_basis);
  436. } else {
  437. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  438. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node);
  439. new_value.add(*this, layout_node, percentage_basis);
  440. }
  441. *this = new_value;
  442. });
  443. }
  444. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  445. {
  446. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  447. // Both of these are represented as a float.
  448. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  449. bool other_is_number = other.m_value.has<Number>();
  450. m_value.visit(
  451. [&](Number const& number) {
  452. if (other_is_number) {
  453. m_value = number * other.m_value.get<Number>();
  454. } else {
  455. // Avoid duplicating all the logic by swapping `this` and `other`.
  456. CalculationResult new_value = other;
  457. new_value.multiply_by(*this, layout_node);
  458. *this = new_value;
  459. }
  460. },
  461. [&](Angle const& angle) {
  462. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  463. },
  464. [&](Frequency const& frequency) {
  465. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  466. },
  467. [&](Length const& length) {
  468. VERIFY(layout_node);
  469. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  470. },
  471. [&](Time const& time) {
  472. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  473. },
  474. [&](Percentage const& percentage) {
  475. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  476. });
  477. }
  478. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  479. {
  480. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  481. // Both of these are represented as a Number.
  482. auto denominator = other.m_value.get<Number>().value();
  483. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  484. VERIFY(denominator != 0.0f);
  485. m_value.visit(
  486. [&](Number const& number) {
  487. m_value = Number {
  488. Number::Type::Number,
  489. number.value() / denominator
  490. };
  491. },
  492. [&](Angle const& angle) {
  493. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  494. },
  495. [&](Frequency const& frequency) {
  496. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  497. },
  498. [&](Length const& length) {
  499. VERIFY(layout_node);
  500. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  501. },
  502. [&](Time const& time) {
  503. m_value = Time::make_seconds(time.to_seconds() / denominator);
  504. },
  505. [&](Percentage const& percentage) {
  506. m_value = Percentage { percentage.value() / denominator };
  507. });
  508. }
  509. String CalculatedStyleValue::to_string() const
  510. {
  511. return String::formatted("calc({})", m_expression->to_string());
  512. }
  513. bool CalculatedStyleValue::equals(StyleValue const& other) const
  514. {
  515. if (type() != other.type())
  516. return false;
  517. // This is a case where comparing the strings actually makes sense.
  518. return to_string() == other.to_string();
  519. }
  520. String CalculatedStyleValue::CalcNumberValue::to_string() const
  521. {
  522. return value.visit(
  523. [](Number const& number) { return String::number(number.value()); },
  524. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return String::formatted("({})", sum->to_string()); });
  525. }
  526. String CalculatedStyleValue::CalcValue::to_string() const
  527. {
  528. return value.visit(
  529. [](Number const& number) { return String::number(number.value()); },
  530. [](NonnullOwnPtr<CalcSum> const& sum) { return String::formatted("({})", sum->to_string()); },
  531. [](auto const& v) { return v.to_string(); });
  532. }
  533. String CalculatedStyleValue::CalcSum::to_string() const
  534. {
  535. StringBuilder builder;
  536. builder.append(first_calc_product->to_string());
  537. for (auto const& item : zero_or_more_additional_calc_products)
  538. builder.append(item.to_string());
  539. return builder.to_string();
  540. }
  541. String CalculatedStyleValue::CalcNumberSum::to_string() const
  542. {
  543. StringBuilder builder;
  544. builder.append(first_calc_number_product->to_string());
  545. for (auto const& item : zero_or_more_additional_calc_number_products)
  546. builder.append(item.to_string());
  547. return builder.to_string();
  548. }
  549. String CalculatedStyleValue::CalcProduct::to_string() const
  550. {
  551. StringBuilder builder;
  552. builder.append(first_calc_value.to_string());
  553. for (auto const& item : zero_or_more_additional_calc_values)
  554. builder.append(item.to_string());
  555. return builder.to_string();
  556. }
  557. String CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  558. {
  559. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, value->to_string());
  560. }
  561. String CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  562. {
  563. auto value_string = value.visit(
  564. [](CalcValue const& v) { return v.to_string(); },
  565. [](CalcNumberValue const& v) { return v.to_string(); });
  566. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  567. }
  568. String CalculatedStyleValue::CalcNumberProduct::to_string() const
  569. {
  570. StringBuilder builder;
  571. builder.append(first_calc_number_value.to_string());
  572. for (auto const& item : zero_or_more_additional_calc_number_values)
  573. builder.append(item.to_string());
  574. return builder.to_string();
  575. }
  576. String CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  577. {
  578. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value.to_string());
  579. }
  580. String CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  581. {
  582. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, value->to_string());
  583. }
  584. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  585. {
  586. auto result = m_expression->resolve(nullptr, {});
  587. if (result.value().has<Angle>())
  588. return result.value().get<Angle>();
  589. return {};
  590. }
  591. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  592. {
  593. auto result = m_expression->resolve(nullptr, percentage_basis);
  594. return result.value().visit(
  595. [&](Angle const& angle) -> Optional<Angle> {
  596. return angle;
  597. },
  598. [&](Percentage const& percentage) -> Optional<Angle> {
  599. return percentage_basis.percentage_of(percentage);
  600. },
  601. [&](auto const&) -> Optional<Angle> {
  602. return {};
  603. });
  604. }
  605. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  606. {
  607. auto result = m_expression->resolve(nullptr, {});
  608. if (result.value().has<Frequency>())
  609. return result.value().get<Frequency>();
  610. return {};
  611. }
  612. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  613. {
  614. auto result = m_expression->resolve(nullptr, percentage_basis);
  615. return result.value().visit(
  616. [&](Frequency const& frequency) -> Optional<Frequency> {
  617. return frequency;
  618. },
  619. [&](Percentage const& percentage) -> Optional<Frequency> {
  620. return percentage_basis.percentage_of(percentage);
  621. },
  622. [&](auto const&) -> Optional<Frequency> {
  623. return {};
  624. });
  625. }
  626. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  627. {
  628. auto result = m_expression->resolve(&layout_node, {});
  629. if (result.value().has<Length>())
  630. return result.value().get<Length>();
  631. return {};
  632. }
  633. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  634. {
  635. auto result = m_expression->resolve(&layout_node, percentage_basis);
  636. return result.value().visit(
  637. [&](Length const& length) -> Optional<Length> {
  638. return length;
  639. },
  640. [&](Percentage const& percentage) -> Optional<Length> {
  641. return percentage_basis.percentage_of(percentage);
  642. },
  643. [&](auto const&) -> Optional<Length> {
  644. return {};
  645. });
  646. }
  647. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  648. {
  649. auto result = m_expression->resolve(nullptr, {});
  650. if (result.value().has<Percentage>())
  651. return result.value().get<Percentage>();
  652. return {};
  653. }
  654. Optional<Time> CalculatedStyleValue::resolve_time() const
  655. {
  656. auto result = m_expression->resolve(nullptr, {});
  657. if (result.value().has<Time>())
  658. return result.value().get<Time>();
  659. return {};
  660. }
  661. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  662. {
  663. auto result = m_expression->resolve(nullptr, percentage_basis);
  664. return result.value().visit(
  665. [&](Time const& time) -> Optional<Time> {
  666. return time;
  667. },
  668. [&](auto const&) -> Optional<Time> {
  669. return {};
  670. });
  671. }
  672. Optional<float> CalculatedStyleValue::resolve_number()
  673. {
  674. auto result = m_expression->resolve(nullptr, {});
  675. if (result.value().has<Number>())
  676. return result.value().get<Number>().value();
  677. return {};
  678. }
  679. Optional<i64> CalculatedStyleValue::resolve_integer()
  680. {
  681. auto result = m_expression->resolve(nullptr, {});
  682. if (result.value().has<Number>())
  683. return result.value().get<Number>().integer_value();
  684. return {};
  685. }
  686. static bool is_number(CalculatedStyleValue::ResolvedType type)
  687. {
  688. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  689. }
  690. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  691. {
  692. return type != CalculatedStyleValue::ResolvedType::Number
  693. && type != CalculatedStyleValue::ResolvedType::Integer
  694. && type != CalculatedStyleValue::ResolvedType::Percentage;
  695. }
  696. template<typename SumWithOperator>
  697. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<SumWithOperator> const& zero_or_more_additional_products)
  698. {
  699. auto type = first_type;
  700. for (auto const& product : zero_or_more_additional_products) {
  701. auto maybe_product_type = product.resolved_type();
  702. if (!maybe_product_type.has_value())
  703. return {};
  704. auto product_type = maybe_product_type.value();
  705. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  706. // If both sides are the same type, resolve to that type.
  707. if (product_type == type)
  708. continue;
  709. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  710. if (is_number(type) && is_number(product_type)) {
  711. type = CalculatedStyleValue::ResolvedType::Number;
  712. continue;
  713. }
  714. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  715. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  716. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  717. type = product_type;
  718. continue;
  719. }
  720. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  721. continue;
  722. return {};
  723. }
  724. return type;
  725. }
  726. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  727. {
  728. auto maybe_type = first_calc_product->resolved_type();
  729. if (!maybe_type.has_value())
  730. return {};
  731. auto type = maybe_type.value();
  732. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  733. }
  734. // https://www.w3.org/TR/CSS2/visufx.html#value-def-shape
  735. Gfx::FloatRect EdgeRect::resolved(Layout::Node const& layout_node, Gfx::FloatRect border_box) const
  736. {
  737. // In CSS 2.1, the only valid <shape> value is: rect(<top>, <right>, <bottom>, <left>) where
  738. // <top> and <bottom> specify offsets from the top border edge of the box, and <right>, and
  739. // <left> specify offsets from the left border edge of the box.
  740. // The value 'auto' means that a given edge of the clipping region will be the same as the edge
  741. // of the element's generated border box (i.e., 'auto' means the same as '0' for <top> and
  742. // <left>, the same as the used value of the height plus the sum of vertical padding and border
  743. // widths for <bottom>, and the same as the used value of the width plus the sum of the
  744. // horizontal padding and border widths for <right>, such that four 'auto' values result in the
  745. // clipping region being the same as the element's border box).
  746. auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node));
  747. auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node));
  748. auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node));
  749. auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node));
  750. return Gfx::FloatRect {
  751. left,
  752. top,
  753. right - left,
  754. bottom - top
  755. };
  756. }
  757. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  758. {
  759. auto maybe_type = first_calc_number_product->resolved_type();
  760. if (!maybe_type.has_value())
  761. return {};
  762. auto type = maybe_type.value();
  763. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  764. }
  765. template<typename ProductWithOperator>
  766. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<ProductWithOperator> const& zero_or_more_additional_values)
  767. {
  768. auto type = first_type;
  769. for (auto const& value : zero_or_more_additional_values) {
  770. auto maybe_value_type = value.resolved_type();
  771. if (!maybe_value_type.has_value())
  772. return {};
  773. auto value_type = maybe_value_type.value();
  774. if (value.op == CalculatedStyleValue::ProductOperation::Multiply) {
  775. // At *, check that at least one side is <number>.
  776. if (!(is_number(type) || is_number(value_type)))
  777. return {};
  778. // If both sides are <integer>, resolve to <integer>.
  779. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  780. type = CalculatedStyleValue::ResolvedType::Integer;
  781. } else {
  782. // Otherwise, resolve to the type of the other side.
  783. if (is_number(type))
  784. type = value_type;
  785. }
  786. continue;
  787. } else {
  788. VERIFY(value.op == CalculatedStyleValue::ProductOperation::Divide);
  789. // At /, check that the right side is <number>.
  790. if (!is_number(value_type))
  791. return {};
  792. // If the left side is <integer>, resolve to <number>.
  793. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  794. type = CalculatedStyleValue::ResolvedType::Number;
  795. } else {
  796. // Otherwise, resolve to the type of the left side.
  797. }
  798. // FIXME: Division by zero makes the whole calc() expression invalid.
  799. }
  800. }
  801. return type;
  802. }
  803. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  804. {
  805. auto maybe_type = first_calc_value.resolved_type();
  806. if (!maybe_type.has_value())
  807. return {};
  808. auto type = maybe_type.value();
  809. return resolve_product_type(type, zero_or_more_additional_calc_values);
  810. }
  811. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  812. {
  813. return value->resolved_type();
  814. }
  815. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  816. {
  817. auto maybe_type = first_calc_number_value.resolved_type();
  818. if (!maybe_type.has_value())
  819. return {};
  820. auto type = maybe_type.value();
  821. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  822. }
  823. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  824. {
  825. return value.resolved_type();
  826. }
  827. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  828. {
  829. return value->resolved_type();
  830. }
  831. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  832. {
  833. return value.visit(
  834. [](CalcValue const& calc_value) {
  835. return calc_value.resolved_type();
  836. },
  837. [](CalcNumberValue const& calc_number_value) {
  838. return calc_number_value.resolved_type();
  839. });
  840. }
  841. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  842. {
  843. return value.visit(
  844. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  845. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  846. },
  847. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  848. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  849. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  850. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  851. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  852. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  853. }
  854. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  855. {
  856. return value.visit(
  857. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  858. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  859. },
  860. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  861. }
  862. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  863. {
  864. return value.visit(
  865. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  866. return CalculatedStyleValue::CalculationResult { number };
  867. },
  868. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  869. return sum->resolve(layout_node, percentage_basis);
  870. });
  871. }
  872. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  873. {
  874. return value.visit(
  875. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  876. return sum->resolve(layout_node, percentage_basis);
  877. },
  878. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  879. return CalculatedStyleValue::CalculationResult { v };
  880. });
  881. }
  882. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  883. {
  884. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  885. for (auto& additional_product : zero_or_more_additional_calc_products) {
  886. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  887. if (additional_product.op == CalculatedStyleValue::SumOperation::Add)
  888. value.add(additional_value, layout_node, percentage_basis);
  889. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  890. value.subtract(additional_value, layout_node, percentage_basis);
  891. else
  892. VERIFY_NOT_REACHED();
  893. }
  894. return value;
  895. }
  896. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  897. {
  898. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  899. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  900. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  901. if (additional_product.op == CSS::CalculatedStyleValue::SumOperation::Add)
  902. value.add(additional_value, layout_node, percentage_basis);
  903. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  904. value.subtract(additional_value, layout_node, percentage_basis);
  905. else
  906. VERIFY_NOT_REACHED();
  907. }
  908. return value;
  909. }
  910. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  911. {
  912. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  913. for (auto& additional_value : zero_or_more_additional_calc_values) {
  914. additional_value.value.visit(
  915. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  916. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Multiply);
  917. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  918. value.multiply_by(resolved_value, layout_node);
  919. },
  920. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  921. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Divide);
  922. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  923. // FIXME: Checking for division by 0 should happen during parsing.
  924. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  925. value.divide_by(resolved_calc_number_value, layout_node);
  926. });
  927. }
  928. return value;
  929. }
  930. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  931. {
  932. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  933. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  934. auto additional_value = additional_number_value.resolve(layout_node, percentage_basis);
  935. if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Multiply)
  936. value.multiply_by(additional_value, layout_node);
  937. else if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Divide)
  938. value.divide_by(additional_value, layout_node);
  939. else
  940. VERIFY_NOT_REACHED();
  941. }
  942. return value;
  943. }
  944. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  945. {
  946. return value.visit(
  947. [&](CalcValue const& calc_value) {
  948. return calc_value.resolve(layout_node, percentage_basis);
  949. },
  950. [&](CalcNumberValue const& calc_number_value) {
  951. return calc_number_value.resolve(layout_node, percentage_basis);
  952. });
  953. }
  954. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  955. {
  956. return value->resolve(layout_node, percentage_basis);
  957. }
  958. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  959. {
  960. return value.resolve(layout_node, percentage_basis);
  961. }
  962. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  963. {
  964. return value->resolve(layout_node, percentage_basis);
  965. }
  966. String ColorStyleValue::to_string() const
  967. {
  968. return serialize_a_srgb_value(m_color);
  969. }
  970. bool ColorStyleValue::equals(StyleValue const& other) const
  971. {
  972. if (type() != other.type())
  973. return false;
  974. return m_color == other.as_color().m_color;
  975. }
  976. String ContentStyleValue::to_string() const
  977. {
  978. if (has_alt_text())
  979. return String::formatted("{} / {}", m_content->to_string(), m_alt_text->to_string());
  980. return m_content->to_string();
  981. }
  982. bool ContentStyleValue::equals(StyleValue const& other) const
  983. {
  984. if (type() != other.type())
  985. return false;
  986. auto const& typed_other = other.as_content();
  987. if (!m_content->equals(typed_other.m_content))
  988. return false;
  989. if (m_alt_text.is_null() != typed_other.m_alt_text.is_null())
  990. return false;
  991. if (!m_alt_text.is_null())
  992. return m_alt_text->equals(*typed_other.m_alt_text);
  993. return true;
  994. }
  995. String FlexStyleValue::to_string() const
  996. {
  997. return String::formatted("{} {} {}", m_grow->to_string(), m_shrink->to_string(), m_basis->to_string());
  998. }
  999. bool FlexStyleValue::equals(StyleValue const& other) const
  1000. {
  1001. if (type() != other.type())
  1002. return false;
  1003. auto const& typed_other = other.as_flex();
  1004. return m_grow->equals(typed_other.m_grow)
  1005. && m_shrink->equals(typed_other.m_shrink)
  1006. && m_basis->equals(typed_other.m_basis);
  1007. }
  1008. String FlexFlowStyleValue::to_string() const
  1009. {
  1010. return String::formatted("{} {}", m_flex_direction->to_string(), m_flex_wrap->to_string());
  1011. }
  1012. bool FlexFlowStyleValue::equals(StyleValue const& other) const
  1013. {
  1014. if (type() != other.type())
  1015. return false;
  1016. auto const& typed_other = other.as_flex_flow();
  1017. return m_flex_direction->equals(typed_other.m_flex_direction)
  1018. && m_flex_wrap->equals(typed_other.m_flex_wrap);
  1019. }
  1020. String FontStyleValue::to_string() const
  1021. {
  1022. return String::formatted("{} {} {} / {} {}", m_font_style->to_string(), m_font_weight->to_string(), m_font_size->to_string(), m_line_height->to_string(), m_font_families->to_string());
  1023. }
  1024. bool FontStyleValue::equals(StyleValue const& other) const
  1025. {
  1026. if (type() != other.type())
  1027. return false;
  1028. auto const& typed_other = other.as_font();
  1029. return m_font_style->equals(typed_other.m_font_style)
  1030. && m_font_weight->equals(typed_other.m_font_weight)
  1031. && m_font_size->equals(typed_other.m_font_size)
  1032. && m_line_height->equals(typed_other.m_line_height)
  1033. && m_font_families->equals(typed_other.m_font_families);
  1034. }
  1035. bool FrequencyStyleValue::equals(StyleValue const& other) const
  1036. {
  1037. if (type() != other.type())
  1038. return false;
  1039. return m_frequency == other.as_frequency().m_frequency;
  1040. }
  1041. String IdentifierStyleValue::to_string() const
  1042. {
  1043. return CSS::string_from_value_id(m_id);
  1044. }
  1045. bool IdentifierStyleValue::equals(StyleValue const& other) const
  1046. {
  1047. if (type() != other.type())
  1048. return false;
  1049. return m_id == other.as_identifier().m_id;
  1050. }
  1051. bool IdentifierStyleValue::has_color() const
  1052. {
  1053. switch (m_id) {
  1054. case ValueID::Currentcolor:
  1055. case ValueID::LibwebLink:
  1056. case ValueID::LibwebPaletteActiveLink:
  1057. case ValueID::LibwebPaletteActiveWindowBorder1:
  1058. case ValueID::LibwebPaletteActiveWindowBorder2:
  1059. case ValueID::LibwebPaletteActiveWindowTitle:
  1060. case ValueID::LibwebPaletteBase:
  1061. case ValueID::LibwebPaletteBaseText:
  1062. case ValueID::LibwebPaletteButton:
  1063. case ValueID::LibwebPaletteButtonText:
  1064. case ValueID::LibwebPaletteDesktopBackground:
  1065. case ValueID::LibwebPaletteFocusOutline:
  1066. case ValueID::LibwebPaletteHighlightWindowBorder1:
  1067. case ValueID::LibwebPaletteHighlightWindowBorder2:
  1068. case ValueID::LibwebPaletteHighlightWindowTitle:
  1069. case ValueID::LibwebPaletteHoverHighlight:
  1070. case ValueID::LibwebPaletteInactiveSelection:
  1071. case ValueID::LibwebPaletteInactiveSelectionText:
  1072. case ValueID::LibwebPaletteInactiveWindowBorder1:
  1073. case ValueID::LibwebPaletteInactiveWindowBorder2:
  1074. case ValueID::LibwebPaletteInactiveWindowTitle:
  1075. case ValueID::LibwebPaletteLink:
  1076. case ValueID::LibwebPaletteMenuBase:
  1077. case ValueID::LibwebPaletteMenuBaseText:
  1078. case ValueID::LibwebPaletteMenuSelection:
  1079. case ValueID::LibwebPaletteMenuSelectionText:
  1080. case ValueID::LibwebPaletteMenuStripe:
  1081. case ValueID::LibwebPaletteMovingWindowBorder1:
  1082. case ValueID::LibwebPaletteMovingWindowBorder2:
  1083. case ValueID::LibwebPaletteMovingWindowTitle:
  1084. case ValueID::LibwebPaletteRubberBandBorder:
  1085. case ValueID::LibwebPaletteRubberBandFill:
  1086. case ValueID::LibwebPaletteRuler:
  1087. case ValueID::LibwebPaletteRulerActiveText:
  1088. case ValueID::LibwebPaletteRulerBorder:
  1089. case ValueID::LibwebPaletteRulerInactiveText:
  1090. case ValueID::LibwebPaletteSelection:
  1091. case ValueID::LibwebPaletteSelectionText:
  1092. case ValueID::LibwebPaletteSyntaxComment:
  1093. case ValueID::LibwebPaletteSyntaxControlKeyword:
  1094. case ValueID::LibwebPaletteSyntaxIdentifier:
  1095. case ValueID::LibwebPaletteSyntaxKeyword:
  1096. case ValueID::LibwebPaletteSyntaxNumber:
  1097. case ValueID::LibwebPaletteSyntaxOperator:
  1098. case ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1099. case ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1100. case ValueID::LibwebPaletteSyntaxPunctuation:
  1101. case ValueID::LibwebPaletteSyntaxString:
  1102. case ValueID::LibwebPaletteSyntaxType:
  1103. case ValueID::LibwebPaletteTextCursor:
  1104. case ValueID::LibwebPaletteThreedHighlight:
  1105. case ValueID::LibwebPaletteThreedShadow1:
  1106. case ValueID::LibwebPaletteThreedShadow2:
  1107. case ValueID::LibwebPaletteVisitedLink:
  1108. case ValueID::LibwebPaletteWindow:
  1109. case ValueID::LibwebPaletteWindowText:
  1110. return true;
  1111. default:
  1112. return false;
  1113. }
  1114. }
  1115. Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
  1116. {
  1117. if (id() == CSS::ValueID::Currentcolor) {
  1118. if (!node.has_style())
  1119. return Color::Black;
  1120. return node.computed_values().color();
  1121. }
  1122. auto& document = node.document();
  1123. if (id() == CSS::ValueID::LibwebLink)
  1124. return document.link_color();
  1125. if (!document.page())
  1126. return {};
  1127. auto palette = document.page()->palette();
  1128. switch (id()) {
  1129. case CSS::ValueID::LibwebPaletteDesktopBackground:
  1130. return palette.color(ColorRole::DesktopBackground);
  1131. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  1132. return palette.color(ColorRole::ActiveWindowBorder1);
  1133. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  1134. return palette.color(ColorRole::ActiveWindowBorder2);
  1135. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  1136. return palette.color(ColorRole::ActiveWindowTitle);
  1137. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  1138. return palette.color(ColorRole::InactiveWindowBorder1);
  1139. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  1140. return palette.color(ColorRole::InactiveWindowBorder2);
  1141. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  1142. return palette.color(ColorRole::InactiveWindowTitle);
  1143. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  1144. return palette.color(ColorRole::MovingWindowBorder1);
  1145. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  1146. return palette.color(ColorRole::MovingWindowBorder2);
  1147. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  1148. return palette.color(ColorRole::MovingWindowTitle);
  1149. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  1150. return palette.color(ColorRole::HighlightWindowBorder1);
  1151. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  1152. return palette.color(ColorRole::HighlightWindowBorder2);
  1153. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  1154. return palette.color(ColorRole::HighlightWindowTitle);
  1155. case CSS::ValueID::LibwebPaletteMenuStripe:
  1156. return palette.color(ColorRole::MenuStripe);
  1157. case CSS::ValueID::LibwebPaletteMenuBase:
  1158. return palette.color(ColorRole::MenuBase);
  1159. case CSS::ValueID::LibwebPaletteMenuBaseText:
  1160. return palette.color(ColorRole::MenuBaseText);
  1161. case CSS::ValueID::LibwebPaletteMenuSelection:
  1162. return palette.color(ColorRole::MenuSelection);
  1163. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  1164. return palette.color(ColorRole::MenuSelectionText);
  1165. case CSS::ValueID::LibwebPaletteWindow:
  1166. return palette.color(ColorRole::Window);
  1167. case CSS::ValueID::LibwebPaletteWindowText:
  1168. return palette.color(ColorRole::WindowText);
  1169. case CSS::ValueID::LibwebPaletteButton:
  1170. return palette.color(ColorRole::Button);
  1171. case CSS::ValueID::LibwebPaletteButtonText:
  1172. return palette.color(ColorRole::ButtonText);
  1173. case CSS::ValueID::LibwebPaletteBase:
  1174. return palette.color(ColorRole::Base);
  1175. case CSS::ValueID::LibwebPaletteBaseText:
  1176. return palette.color(ColorRole::BaseText);
  1177. case CSS::ValueID::LibwebPaletteThreedHighlight:
  1178. return palette.color(ColorRole::ThreedHighlight);
  1179. case CSS::ValueID::LibwebPaletteThreedShadow1:
  1180. return palette.color(ColorRole::ThreedShadow1);
  1181. case CSS::ValueID::LibwebPaletteThreedShadow2:
  1182. return palette.color(ColorRole::ThreedShadow2);
  1183. case CSS::ValueID::LibwebPaletteHoverHighlight:
  1184. return palette.color(ColorRole::HoverHighlight);
  1185. case CSS::ValueID::LibwebPaletteSelection:
  1186. return palette.color(ColorRole::Selection);
  1187. case CSS::ValueID::LibwebPaletteSelectionText:
  1188. return palette.color(ColorRole::SelectionText);
  1189. case CSS::ValueID::LibwebPaletteInactiveSelection:
  1190. return palette.color(ColorRole::InactiveSelection);
  1191. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  1192. return palette.color(ColorRole::InactiveSelectionText);
  1193. case CSS::ValueID::LibwebPaletteRubberBandFill:
  1194. return palette.color(ColorRole::RubberBandFill);
  1195. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  1196. return palette.color(ColorRole::RubberBandBorder);
  1197. case CSS::ValueID::LibwebPaletteLink:
  1198. return palette.color(ColorRole::Link);
  1199. case CSS::ValueID::LibwebPaletteActiveLink:
  1200. return palette.color(ColorRole::ActiveLink);
  1201. case CSS::ValueID::LibwebPaletteVisitedLink:
  1202. return palette.color(ColorRole::VisitedLink);
  1203. case CSS::ValueID::LibwebPaletteRuler:
  1204. return palette.color(ColorRole::Ruler);
  1205. case CSS::ValueID::LibwebPaletteRulerBorder:
  1206. return palette.color(ColorRole::RulerBorder);
  1207. case CSS::ValueID::LibwebPaletteRulerActiveText:
  1208. return palette.color(ColorRole::RulerActiveText);
  1209. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  1210. return palette.color(ColorRole::RulerInactiveText);
  1211. case CSS::ValueID::LibwebPaletteTextCursor:
  1212. return palette.color(ColorRole::TextCursor);
  1213. case CSS::ValueID::LibwebPaletteFocusOutline:
  1214. return palette.color(ColorRole::FocusOutline);
  1215. case CSS::ValueID::LibwebPaletteSyntaxComment:
  1216. return palette.color(ColorRole::SyntaxComment);
  1217. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  1218. return palette.color(ColorRole::SyntaxNumber);
  1219. case CSS::ValueID::LibwebPaletteSyntaxString:
  1220. return palette.color(ColorRole::SyntaxString);
  1221. case CSS::ValueID::LibwebPaletteSyntaxType:
  1222. return palette.color(ColorRole::SyntaxType);
  1223. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  1224. return palette.color(ColorRole::SyntaxPunctuation);
  1225. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  1226. return palette.color(ColorRole::SyntaxOperator);
  1227. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  1228. return palette.color(ColorRole::SyntaxKeyword);
  1229. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  1230. return palette.color(ColorRole::SyntaxControlKeyword);
  1231. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  1232. return palette.color(ColorRole::SyntaxIdentifier);
  1233. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1234. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  1235. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1236. return palette.color(ColorRole::SyntaxPreprocessorValue);
  1237. default:
  1238. return {};
  1239. }
  1240. }
  1241. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  1242. : AbstractImageStyleValue(Type::Image)
  1243. , m_url(url)
  1244. {
  1245. }
  1246. void ImageStyleValue::load_any_resources(DOM::Document& document)
  1247. {
  1248. if (m_bitmap)
  1249. return;
  1250. m_document = &document;
  1251. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  1252. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  1253. }
  1254. void ImageStyleValue::resource_did_load()
  1255. {
  1256. if (!m_document)
  1257. return;
  1258. m_bitmap = resource()->bitmap();
  1259. // FIXME: Do less than a full repaint if possible?
  1260. if (m_document && m_document->browsing_context())
  1261. m_document->browsing_context()->set_needs_display();
  1262. }
  1263. String ImageStyleValue::to_string() const
  1264. {
  1265. return serialize_a_url(m_url.to_string());
  1266. }
  1267. bool ImageStyleValue::equals(StyleValue const& other) const
  1268. {
  1269. if (type() != other.type())
  1270. return false;
  1271. return m_url == other.as_image().m_url;
  1272. }
  1273. Optional<int> ImageStyleValue::natural_width() const
  1274. {
  1275. if (m_bitmap)
  1276. return m_bitmap->width();
  1277. return {};
  1278. }
  1279. Optional<int> ImageStyleValue::natural_height() const
  1280. {
  1281. if (m_bitmap)
  1282. return m_bitmap->height();
  1283. return {};
  1284. }
  1285. void ImageStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering image_rendering) const
  1286. {
  1287. if (m_bitmap)
  1288. context.painter().draw_scaled_bitmap(dest_rect, *m_bitmap, m_bitmap->rect(), 1.0f, to_gfx_scaling_mode(image_rendering));
  1289. }
  1290. String LinearGradientStyleValue::to_string() const
  1291. {
  1292. StringBuilder builder;
  1293. auto side_or_corner_to_string = [](SideOrCorner value) {
  1294. switch (value) {
  1295. case SideOrCorner::Top:
  1296. return "top"sv;
  1297. case SideOrCorner::Bottom:
  1298. return "bottom"sv;
  1299. case SideOrCorner::Left:
  1300. return "left"sv;
  1301. case SideOrCorner::Right:
  1302. return "right"sv;
  1303. case SideOrCorner::TopLeft:
  1304. return "top left"sv;
  1305. case SideOrCorner::TopRight:
  1306. return "top right"sv;
  1307. case SideOrCorner::BottomLeft:
  1308. return "bottom left"sv;
  1309. case SideOrCorner::BottomRight:
  1310. return "bottom right"sv;
  1311. default:
  1312. VERIFY_NOT_REACHED();
  1313. }
  1314. };
  1315. if (m_gradient_type == GradientType::WebKit)
  1316. builder.append("-webkit-"sv);
  1317. if (m_repeating == Repeating::Yes)
  1318. builder.append("repeating-"sv);
  1319. builder.append("linear-gradient("sv);
  1320. m_direction.visit(
  1321. [&](SideOrCorner side_or_corner) {
  1322. builder.appendff("{}{}, "sv, m_gradient_type == GradientType::Standard ? "to "sv : ""sv, side_or_corner_to_string(side_or_corner));
  1323. },
  1324. [&](Angle const& angle) {
  1325. builder.appendff("{}, "sv, angle.to_string());
  1326. });
  1327. bool first = true;
  1328. for (auto const& element : m_color_stop_list) {
  1329. if (!first)
  1330. builder.append(", "sv);
  1331. if (element.transition_hint.has_value()) {
  1332. builder.appendff("{}, "sv, element.transition_hint->value.to_string());
  1333. }
  1334. serialize_a_srgb_value(builder, element.color_stop.color);
  1335. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  1336. if (position->has_value())
  1337. builder.appendff(" {}"sv, (*position)->to_string());
  1338. }
  1339. first = false;
  1340. }
  1341. builder.append(")"sv);
  1342. return builder.to_string();
  1343. }
  1344. static bool operator==(LinearGradientStyleValue::GradientDirection a, LinearGradientStyleValue::GradientDirection b)
  1345. {
  1346. if (a.has<SideOrCorner>() && b.has<SideOrCorner>())
  1347. return a.get<SideOrCorner>() == b.get<SideOrCorner>();
  1348. if (a.has<Angle>() && b.has<Angle>())
  1349. return a.get<Angle>() == b.get<Angle>();
  1350. return false;
  1351. }
  1352. static bool operator==(GradientColorHint a, GradientColorHint b)
  1353. {
  1354. return a.value == b.value;
  1355. }
  1356. static bool operator==(GradientColorStop a, GradientColorStop b)
  1357. {
  1358. return a.color == b.color && a.position == b.position && a.second_position == b.second_position;
  1359. }
  1360. static bool operator==(ColorStopListElement a, ColorStopListElement b)
  1361. {
  1362. return a.transition_hint == b.transition_hint && a.color_stop == b.color_stop;
  1363. }
  1364. static bool operator==(EdgeRect a, EdgeRect b)
  1365. {
  1366. return a.top_edge == b.top_edge && a.right_edge == b.right_edge && a.bottom_edge == b.bottom_edge && a.left_edge == b.left_edge;
  1367. }
  1368. bool LinearGradientStyleValue::equals(StyleValue const& other_) const
  1369. {
  1370. if (type() != other_.type())
  1371. return false;
  1372. auto& other = other_.as_linear_gradient();
  1373. if (m_gradient_type != other.m_gradient_type
  1374. || m_repeating != other.m_repeating
  1375. || m_direction != other.m_direction
  1376. || m_color_stop_list.size() != other.m_color_stop_list.size()) {
  1377. return false;
  1378. }
  1379. for (size_t i = 0; i < m_color_stop_list.size(); i++) {
  1380. if (m_color_stop_list[i] != other.m_color_stop_list[i])
  1381. return false;
  1382. }
  1383. return true;
  1384. }
  1385. float LinearGradientStyleValue::angle_degrees(Gfx::FloatSize const& gradient_size) const
  1386. {
  1387. auto corner_angle_degrees = [&] {
  1388. return static_cast<float>(atan2(gradient_size.height(), gradient_size.width())) * 180 / AK::Pi<float>;
  1389. };
  1390. return m_direction.visit(
  1391. [&](SideOrCorner side_or_corner) {
  1392. auto angle = [&] {
  1393. switch (side_or_corner) {
  1394. case SideOrCorner::Top:
  1395. return 0.0f;
  1396. case SideOrCorner::Bottom:
  1397. return 180.0f;
  1398. case SideOrCorner::Left:
  1399. return 270.0f;
  1400. case SideOrCorner::Right:
  1401. return 90.0f;
  1402. case SideOrCorner::TopRight:
  1403. return corner_angle_degrees();
  1404. case SideOrCorner::BottomLeft:
  1405. return corner_angle_degrees() + 180.0f;
  1406. case SideOrCorner::TopLeft:
  1407. return -corner_angle_degrees();
  1408. case SideOrCorner::BottomRight:
  1409. return -(corner_angle_degrees() + 180.0f);
  1410. default:
  1411. VERIFY_NOT_REACHED();
  1412. }
  1413. }();
  1414. // Note: For unknowable reasons the angles are opposite on the -webkit- version
  1415. if (m_gradient_type == GradientType::WebKit)
  1416. return angle + 180.0f;
  1417. return angle;
  1418. },
  1419. [&](Angle const& angle) {
  1420. return angle.to_degrees();
  1421. });
  1422. }
  1423. void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, Gfx::FloatSize const& size) const
  1424. {
  1425. m_resolved_data = Painting::resolve_linear_gradient_data(node, size, *this);
  1426. }
  1427. void LinearGradientStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering) const
  1428. {
  1429. VERIFY(m_resolved_data.has_value());
  1430. Painting::paint_linear_gradient(context, dest_rect, *m_resolved_data);
  1431. }
  1432. bool InheritStyleValue::equals(StyleValue const& other) const
  1433. {
  1434. return type() == other.type();
  1435. }
  1436. bool InitialStyleValue::equals(StyleValue const& other) const
  1437. {
  1438. return type() == other.type();
  1439. }
  1440. bool LengthStyleValue::equals(StyleValue const& other) const
  1441. {
  1442. if (type() != other.type())
  1443. return false;
  1444. return m_length == other.as_length().m_length;
  1445. }
  1446. String ListStyleStyleValue::to_string() const
  1447. {
  1448. return String::formatted("{} {} {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
  1449. }
  1450. bool ListStyleStyleValue::equals(StyleValue const& other) const
  1451. {
  1452. if (type() != other.type())
  1453. return false;
  1454. auto const& typed_other = other.as_list_style();
  1455. return m_position->equals(typed_other.m_position)
  1456. && m_image->equals(typed_other.m_image)
  1457. && m_style_type->equals(typed_other.m_style_type);
  1458. }
  1459. String NumericStyleValue::to_string() const
  1460. {
  1461. return m_value.visit(
  1462. [](float value) {
  1463. return String::formatted("{}", value);
  1464. },
  1465. [](i64 value) {
  1466. return String::formatted("{}", value);
  1467. });
  1468. }
  1469. bool NumericStyleValue::equals(StyleValue const& other) const
  1470. {
  1471. if (type() != other.type())
  1472. return false;
  1473. if (has_integer() != other.has_integer())
  1474. return false;
  1475. if (has_integer())
  1476. return m_value.get<i64>() == other.as_numeric().m_value.get<i64>();
  1477. return m_value.get<float>() == other.as_numeric().m_value.get<float>();
  1478. }
  1479. String OverflowStyleValue::to_string() const
  1480. {
  1481. return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
  1482. }
  1483. bool OverflowStyleValue::equals(StyleValue const& other) const
  1484. {
  1485. if (type() != other.type())
  1486. return false;
  1487. auto const& typed_other = other.as_overflow();
  1488. return m_overflow_x->equals(typed_other.m_overflow_x)
  1489. && m_overflow_y->equals(typed_other.m_overflow_y);
  1490. }
  1491. String PercentageStyleValue::to_string() const
  1492. {
  1493. return m_percentage.to_string();
  1494. }
  1495. bool PercentageStyleValue::equals(StyleValue const& other) const
  1496. {
  1497. if (type() != other.type())
  1498. return false;
  1499. return m_percentage == other.as_percentage().m_percentage;
  1500. }
  1501. String PositionStyleValue::to_string() const
  1502. {
  1503. auto to_string = [](PositionEdge edge) {
  1504. switch (edge) {
  1505. case PositionEdge::Left:
  1506. return "left";
  1507. case PositionEdge::Right:
  1508. return "right";
  1509. case PositionEdge::Top:
  1510. return "top";
  1511. case PositionEdge::Bottom:
  1512. return "bottom";
  1513. }
  1514. VERIFY_NOT_REACHED();
  1515. };
  1516. return String::formatted("{} {} {} {}", to_string(m_edge_x), m_offset_x.to_string(), to_string(m_edge_y), m_offset_y.to_string());
  1517. }
  1518. bool PositionStyleValue::equals(StyleValue const& other) const
  1519. {
  1520. if (type() != other.type())
  1521. return false;
  1522. auto const& typed_other = other.as_position();
  1523. return m_edge_x == typed_other.m_edge_x
  1524. && m_offset_x == typed_other.m_offset_x
  1525. && m_edge_y == typed_other.m_edge_y
  1526. && m_offset_y == typed_other.m_offset_y;
  1527. }
  1528. String RectStyleValue::to_string() const
  1529. {
  1530. return String::formatted("rect({} {} {} {})", m_rect.top_edge, m_rect.right_edge, m_rect.bottom_edge, m_rect.left_edge);
  1531. }
  1532. bool RectStyleValue::equals(StyleValue const& other) const
  1533. {
  1534. if (type() != other.type())
  1535. return false;
  1536. auto const& typed_other = other.as_rect();
  1537. return m_rect == typed_other.rect();
  1538. }
  1539. bool ResolutionStyleValue::equals(StyleValue const& other) const
  1540. {
  1541. if (type() != other.type())
  1542. return false;
  1543. return m_resolution == other.as_resolution().m_resolution;
  1544. }
  1545. String ShadowStyleValue::to_string() const
  1546. {
  1547. StringBuilder builder;
  1548. builder.appendff("{} {} {} {} {}", m_color.to_string(), m_offset_x.to_string(), m_offset_y.to_string(), m_blur_radius.to_string(), m_spread_distance.to_string());
  1549. if (m_placement == ShadowPlacement::Inner)
  1550. builder.append(" inset"sv);
  1551. return builder.to_string();
  1552. }
  1553. bool ShadowStyleValue::equals(StyleValue const& other) const
  1554. {
  1555. if (type() != other.type())
  1556. return false;
  1557. auto const& typed_other = other.as_shadow();
  1558. return m_color == typed_other.m_color
  1559. && m_offset_x == typed_other.m_offset_x
  1560. && m_offset_y == typed_other.m_offset_y
  1561. && m_blur_radius == typed_other.m_blur_radius
  1562. && m_spread_distance == typed_other.m_spread_distance
  1563. && m_placement == typed_other.m_placement;
  1564. }
  1565. bool StringStyleValue::equals(StyleValue const& other) const
  1566. {
  1567. if (type() != other.type())
  1568. return false;
  1569. return m_string == other.as_string().m_string;
  1570. }
  1571. String TextDecorationStyleValue::to_string() const
  1572. {
  1573. return String::formatted("{} {} {} {}", m_line->to_string(), m_thickness->to_string(), m_style->to_string(), m_color->to_string());
  1574. }
  1575. bool TextDecorationStyleValue::equals(StyleValue const& other) const
  1576. {
  1577. if (type() != other.type())
  1578. return false;
  1579. auto const& typed_other = other.as_text_decoration();
  1580. return m_line->equals(typed_other.m_line)
  1581. && m_thickness->equals(typed_other.m_thickness)
  1582. && m_style->equals(typed_other.m_style)
  1583. && m_color->equals(typed_other.m_color);
  1584. }
  1585. bool TimeStyleValue::equals(StyleValue const& other) const
  1586. {
  1587. if (type() != other.type())
  1588. return false;
  1589. return m_time == other.as_time().m_time;
  1590. }
  1591. String TransformationStyleValue::to_string() const
  1592. {
  1593. StringBuilder builder;
  1594. builder.append(CSS::to_string(m_transform_function));
  1595. builder.append('(');
  1596. builder.join(", "sv, m_values);
  1597. builder.append(')');
  1598. return builder.to_string();
  1599. }
  1600. bool TransformationStyleValue::equals(StyleValue const& other) const
  1601. {
  1602. if (type() != other.type())
  1603. return false;
  1604. auto const& typed_other = other.as_transformation();
  1605. if (m_transform_function != typed_other.m_transform_function)
  1606. return false;
  1607. if (m_values.size() != typed_other.m_values.size())
  1608. return false;
  1609. for (size_t i = 0; i < m_values.size(); ++i) {
  1610. if (!m_values[i].equals(typed_other.m_values[i]))
  1611. return false;
  1612. }
  1613. return true;
  1614. }
  1615. String UnresolvedStyleValue::to_string() const
  1616. {
  1617. StringBuilder builder;
  1618. for (auto& value : m_values)
  1619. builder.append(value.to_string());
  1620. return builder.to_string();
  1621. }
  1622. bool UnresolvedStyleValue::equals(StyleValue const& other) const
  1623. {
  1624. if (type() != other.type())
  1625. return false;
  1626. // This is a case where comparing the strings actually makes sense.
  1627. return to_string() == other.to_string();
  1628. }
  1629. bool UnsetStyleValue::equals(StyleValue const& other) const
  1630. {
  1631. return type() == other.type();
  1632. }
  1633. String StyleValueList::to_string() const
  1634. {
  1635. String separator = "";
  1636. switch (m_separator) {
  1637. case Separator::Space:
  1638. separator = " ";
  1639. break;
  1640. case Separator::Comma:
  1641. separator = ", ";
  1642. break;
  1643. default:
  1644. VERIFY_NOT_REACHED();
  1645. }
  1646. return String::join(separator, m_values);
  1647. }
  1648. bool StyleValueList::equals(StyleValue const& other) const
  1649. {
  1650. if (type() != other.type())
  1651. return false;
  1652. auto const& typed_other = other.as_value_list();
  1653. if (m_separator != typed_other.m_separator)
  1654. return false;
  1655. if (m_values.size() != typed_other.m_values.size())
  1656. return false;
  1657. for (size_t i = 0; i < m_values.size(); ++i) {
  1658. if (!m_values[i].equals(typed_other.m_values[i]))
  1659. return false;
  1660. }
  1661. return true;
  1662. }
  1663. NonnullRefPtr<ColorStyleValue> ColorStyleValue::create(Color color)
  1664. {
  1665. if (color.value() == 0) {
  1666. static auto transparent = adopt_ref(*new ColorStyleValue(color));
  1667. return transparent;
  1668. }
  1669. if (color == Color::from_rgb(0x000000)) {
  1670. static auto black = adopt_ref(*new ColorStyleValue(color));
  1671. return black;
  1672. }
  1673. if (color == Color::from_rgb(0xffffff)) {
  1674. static auto white = adopt_ref(*new ColorStyleValue(color));
  1675. return white;
  1676. }
  1677. return adopt_ref(*new ColorStyleValue(color));
  1678. }
  1679. NonnullRefPtr<RectStyleValue> RectStyleValue::create(EdgeRect rect)
  1680. {
  1681. return adopt_ref(*new RectStyleValue(rect));
  1682. }
  1683. NonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  1684. {
  1685. if (length.is_auto()) {
  1686. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
  1687. return value;
  1688. }
  1689. if (length.is_px()) {
  1690. if (length.raw_value() == 0) {
  1691. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
  1692. return value;
  1693. }
  1694. if (length.raw_value() == 1) {
  1695. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
  1696. return value;
  1697. }
  1698. }
  1699. return adopt_ref(*new LengthStyleValue(length));
  1700. }
  1701. static Optional<CSS::Length> absolutized_length(CSS::Length const& length, Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size)
  1702. {
  1703. if (length.is_px())
  1704. return {};
  1705. if (length.is_absolute() || length.is_relative()) {
  1706. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size);
  1707. return CSS::Length::make_px(px);
  1708. }
  1709. return {};
  1710. }
  1711. NonnullRefPtr<StyleValue> StyleValue::absolutized(Gfx::IntRect const&, Gfx::FontPixelMetrics const&, float, float) const
  1712. {
  1713. return *this;
  1714. }
  1715. NonnullRefPtr<StyleValue> LengthStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1716. {
  1717. if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size); length.has_value())
  1718. return LengthStyleValue::create(length.release_value());
  1719. return *this;
  1720. }
  1721. NonnullRefPtr<StyleValue> ShadowStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1722. {
  1723. auto absolutized_offset_x = absolutized_length(m_offset_x, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_x);
  1724. auto absolutized_offset_y = absolutized_length(m_offset_y, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_y);
  1725. auto absolutized_blur_radius = absolutized_length(m_blur_radius, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_blur_radius);
  1726. auto absolutized_spread_distance = absolutized_length(m_spread_distance, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_spread_distance);
  1727. return ShadowStyleValue::create(m_color, absolutized_offset_x, absolutized_offset_y, absolutized_blur_radius, absolutized_spread_distance, m_placement);
  1728. }
  1729. NonnullRefPtr<StyleValue> BorderRadiusStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1730. {
  1731. if (m_horizontal_radius.is_percentage() && m_vertical_radius.is_percentage())
  1732. return *this;
  1733. auto absolutized_horizontal_radius = m_horizontal_radius;
  1734. auto absolutized_vertical_radius = m_vertical_radius;
  1735. if (!m_horizontal_radius.is_percentage())
  1736. absolutized_horizontal_radius = absolutized_length(m_horizontal_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_horizontal_radius.length());
  1737. if (!m_vertical_radius.is_percentage())
  1738. absolutized_vertical_radius = absolutized_length(m_vertical_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_vertical_radius.length());
  1739. return BorderRadiusStyleValue::create(absolutized_horizontal_radius, absolutized_vertical_radius);
  1740. }
  1741. }