StyleValue.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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) const
  1286. {
  1287. if (m_bitmap)
  1288. context.painter().draw_scaled_bitmap(dest_rect, *m_bitmap, m_bitmap->rect(), 1.0f, Gfx::Painter::ScalingMode::BilinearBlend);
  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. builder.append("linear-gradient("sv);
  1318. m_direction.visit(
  1319. [&](SideOrCorner side_or_corner) {
  1320. builder.appendff("{}{}, "sv, m_gradient_type == GradientType::Standard ? "to "sv : ""sv, side_or_corner_to_string(side_or_corner));
  1321. },
  1322. [&](Angle const& angle) {
  1323. builder.appendff("{}, "sv, angle.to_string());
  1324. });
  1325. bool first = true;
  1326. for (auto const& element : m_color_stop_list) {
  1327. if (!first)
  1328. builder.append(", "sv);
  1329. if (element.transition_hint.has_value()) {
  1330. builder.appendff("{}, "sv, element.transition_hint->value.to_string());
  1331. }
  1332. serialize_a_srgb_value(builder, element.color_stop.color);
  1333. if (element.color_stop.length.has_value()) {
  1334. builder.appendff(" {}"sv, element.color_stop.length->to_string());
  1335. }
  1336. first = false;
  1337. }
  1338. builder.append(")"sv);
  1339. return builder.to_string();
  1340. }
  1341. static bool operator==(LinearGradientStyleValue::GradientDirection a, LinearGradientStyleValue::GradientDirection b)
  1342. {
  1343. if (a.has<SideOrCorner>() && b.has<SideOrCorner>())
  1344. return a.get<SideOrCorner>() == b.get<SideOrCorner>();
  1345. if (a.has<Angle>() && b.has<Angle>())
  1346. return a.get<Angle>() == b.get<Angle>();
  1347. return false;
  1348. }
  1349. static bool operator==(GradientColorHint a, GradientColorHint b)
  1350. {
  1351. return a.value == b.value;
  1352. }
  1353. static bool operator==(GradientColorStop a, GradientColorStop b)
  1354. {
  1355. return a.color == b.color && a.length == b.length;
  1356. }
  1357. static bool operator==(ColorStopListElement a, ColorStopListElement b)
  1358. {
  1359. return a.transition_hint == b.transition_hint && a.color_stop == b.color_stop;
  1360. }
  1361. static bool operator==(EdgeRect a, EdgeRect b)
  1362. {
  1363. 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;
  1364. }
  1365. bool LinearGradientStyleValue::equals(StyleValue const& other_) const
  1366. {
  1367. if (type() != other_.type())
  1368. return false;
  1369. auto& other = other_.as_linear_gradient();
  1370. if (m_direction != other.m_direction || m_color_stop_list.size() != other.m_color_stop_list.size())
  1371. return false;
  1372. for (size_t i = 0; i < m_color_stop_list.size(); i++) {
  1373. if (m_color_stop_list[i] != other.m_color_stop_list[i])
  1374. return false;
  1375. }
  1376. return true;
  1377. }
  1378. float LinearGradientStyleValue::angle_degrees(Gfx::FloatSize const& gradient_size) const
  1379. {
  1380. auto corner_angle_degrees = [&] {
  1381. return static_cast<float>(atan2(gradient_size.height(), gradient_size.width())) * 180 / AK::Pi<float>;
  1382. };
  1383. return m_direction.visit(
  1384. [&](SideOrCorner side_or_corner) {
  1385. auto angle = [&] {
  1386. switch (side_or_corner) {
  1387. case SideOrCorner::Top:
  1388. return 0.0f;
  1389. case SideOrCorner::Bottom:
  1390. return 180.0f;
  1391. case SideOrCorner::Left:
  1392. return 270.0f;
  1393. case SideOrCorner::Right:
  1394. return 90.0f;
  1395. case SideOrCorner::TopRight:
  1396. return corner_angle_degrees();
  1397. case SideOrCorner::BottomLeft:
  1398. return corner_angle_degrees() + 180.0f;
  1399. case SideOrCorner::TopLeft:
  1400. return -corner_angle_degrees();
  1401. case SideOrCorner::BottomRight:
  1402. return -(corner_angle_degrees() + 180.0f);
  1403. default:
  1404. VERIFY_NOT_REACHED();
  1405. }
  1406. }();
  1407. // Note: For unknowable reasons the angles are opposite on the -webkit- version
  1408. if (m_gradient_type == GradientType::WebKit)
  1409. return angle + 180.0f;
  1410. return angle;
  1411. },
  1412. [&](Angle const& angle) {
  1413. return angle.to_degrees();
  1414. });
  1415. }
  1416. void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, Gfx::FloatSize const& size) const
  1417. {
  1418. m_resolved_data = Painting::resolve_linear_gradient_data(node, size, *this);
  1419. }
  1420. void LinearGradientStyleValue::paint(PaintContext& context, Gfx::IntRect const& dest_rect) const
  1421. {
  1422. VERIFY(m_resolved_data.has_value());
  1423. Painting::paint_linear_gradient(context, dest_rect, *m_resolved_data);
  1424. }
  1425. bool InheritStyleValue::equals(StyleValue const& other) const
  1426. {
  1427. return type() == other.type();
  1428. }
  1429. bool InitialStyleValue::equals(StyleValue const& other) const
  1430. {
  1431. return type() == other.type();
  1432. }
  1433. bool LengthStyleValue::equals(StyleValue const& other) const
  1434. {
  1435. if (type() != other.type())
  1436. return false;
  1437. return m_length == other.as_length().m_length;
  1438. }
  1439. String ListStyleStyleValue::to_string() const
  1440. {
  1441. return String::formatted("{} {} {}", m_position->to_string(), m_image->to_string(), m_style_type->to_string());
  1442. }
  1443. bool ListStyleStyleValue::equals(StyleValue const& other) const
  1444. {
  1445. if (type() != other.type())
  1446. return false;
  1447. auto const& typed_other = other.as_list_style();
  1448. return m_position->equals(typed_other.m_position)
  1449. && m_image->equals(typed_other.m_image)
  1450. && m_style_type->equals(typed_other.m_style_type);
  1451. }
  1452. String NumericStyleValue::to_string() const
  1453. {
  1454. return m_value.visit(
  1455. [](float value) {
  1456. return String::formatted("{}", value);
  1457. },
  1458. [](i64 value) {
  1459. return String::formatted("{}", value);
  1460. });
  1461. }
  1462. bool NumericStyleValue::equals(StyleValue const& other) const
  1463. {
  1464. if (type() != other.type())
  1465. return false;
  1466. if (has_integer() != other.has_integer())
  1467. return false;
  1468. if (has_integer())
  1469. return m_value.get<i64>() == other.as_numeric().m_value.get<i64>();
  1470. return m_value.get<float>() == other.as_numeric().m_value.get<float>();
  1471. }
  1472. String OverflowStyleValue::to_string() const
  1473. {
  1474. return String::formatted("{} {}", m_overflow_x->to_string(), m_overflow_y->to_string());
  1475. }
  1476. bool OverflowStyleValue::equals(StyleValue const& other) const
  1477. {
  1478. if (type() != other.type())
  1479. return false;
  1480. auto const& typed_other = other.as_overflow();
  1481. return m_overflow_x->equals(typed_other.m_overflow_x)
  1482. && m_overflow_y->equals(typed_other.m_overflow_y);
  1483. }
  1484. String PercentageStyleValue::to_string() const
  1485. {
  1486. return m_percentage.to_string();
  1487. }
  1488. bool PercentageStyleValue::equals(StyleValue const& other) const
  1489. {
  1490. if (type() != other.type())
  1491. return false;
  1492. return m_percentage == other.as_percentage().m_percentage;
  1493. }
  1494. String PositionStyleValue::to_string() const
  1495. {
  1496. auto to_string = [](PositionEdge edge) {
  1497. switch (edge) {
  1498. case PositionEdge::Left:
  1499. return "left";
  1500. case PositionEdge::Right:
  1501. return "right";
  1502. case PositionEdge::Top:
  1503. return "top";
  1504. case PositionEdge::Bottom:
  1505. return "bottom";
  1506. }
  1507. VERIFY_NOT_REACHED();
  1508. };
  1509. return String::formatted("{} {} {} {}", to_string(m_edge_x), m_offset_x.to_string(), to_string(m_edge_y), m_offset_y.to_string());
  1510. }
  1511. bool PositionStyleValue::equals(StyleValue const& other) const
  1512. {
  1513. if (type() != other.type())
  1514. return false;
  1515. auto const& typed_other = other.as_position();
  1516. return m_edge_x == typed_other.m_edge_x
  1517. && m_offset_x == typed_other.m_offset_x
  1518. && m_edge_y == typed_other.m_edge_y
  1519. && m_offset_y == typed_other.m_offset_y;
  1520. }
  1521. String RectStyleValue::to_string() const
  1522. {
  1523. return String::formatted("rect({} {} {} {})", m_rect.top_edge, m_rect.right_edge, m_rect.bottom_edge, m_rect.left_edge);
  1524. }
  1525. bool RectStyleValue::equals(StyleValue const& other) const
  1526. {
  1527. if (type() != other.type())
  1528. return false;
  1529. auto const& typed_other = other.as_rect();
  1530. return m_rect == typed_other.rect();
  1531. }
  1532. bool ResolutionStyleValue::equals(StyleValue const& other) const
  1533. {
  1534. if (type() != other.type())
  1535. return false;
  1536. return m_resolution == other.as_resolution().m_resolution;
  1537. }
  1538. String ShadowStyleValue::to_string() const
  1539. {
  1540. StringBuilder builder;
  1541. 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());
  1542. if (m_placement == ShadowPlacement::Inner)
  1543. builder.append(" inset"sv);
  1544. return builder.to_string();
  1545. }
  1546. bool ShadowStyleValue::equals(StyleValue const& other) const
  1547. {
  1548. if (type() != other.type())
  1549. return false;
  1550. auto const& typed_other = other.as_shadow();
  1551. return m_color == typed_other.m_color
  1552. && m_offset_x == typed_other.m_offset_x
  1553. && m_offset_y == typed_other.m_offset_y
  1554. && m_blur_radius == typed_other.m_blur_radius
  1555. && m_spread_distance == typed_other.m_spread_distance
  1556. && m_placement == typed_other.m_placement;
  1557. }
  1558. bool StringStyleValue::equals(StyleValue const& other) const
  1559. {
  1560. if (type() != other.type())
  1561. return false;
  1562. return m_string == other.as_string().m_string;
  1563. }
  1564. String TextDecorationStyleValue::to_string() const
  1565. {
  1566. return String::formatted("{} {} {} {}", m_line->to_string(), m_thickness->to_string(), m_style->to_string(), m_color->to_string());
  1567. }
  1568. bool TextDecorationStyleValue::equals(StyleValue const& other) const
  1569. {
  1570. if (type() != other.type())
  1571. return false;
  1572. auto const& typed_other = other.as_text_decoration();
  1573. return m_line->equals(typed_other.m_line)
  1574. && m_thickness->equals(typed_other.m_thickness)
  1575. && m_style->equals(typed_other.m_style)
  1576. && m_color->equals(typed_other.m_color);
  1577. }
  1578. bool TimeStyleValue::equals(StyleValue const& other) const
  1579. {
  1580. if (type() != other.type())
  1581. return false;
  1582. return m_time == other.as_time().m_time;
  1583. }
  1584. String TransformationStyleValue::to_string() const
  1585. {
  1586. StringBuilder builder;
  1587. builder.append(CSS::to_string(m_transform_function));
  1588. builder.append('(');
  1589. builder.join(", "sv, m_values);
  1590. builder.append(')');
  1591. return builder.to_string();
  1592. }
  1593. bool TransformationStyleValue::equals(StyleValue const& other) const
  1594. {
  1595. if (type() != other.type())
  1596. return false;
  1597. auto const& typed_other = other.as_transformation();
  1598. if (m_transform_function != typed_other.m_transform_function)
  1599. return false;
  1600. if (m_values.size() != typed_other.m_values.size())
  1601. return false;
  1602. for (size_t i = 0; i < m_values.size(); ++i) {
  1603. if (!m_values[i].equals(typed_other.m_values[i]))
  1604. return false;
  1605. }
  1606. return true;
  1607. }
  1608. String UnresolvedStyleValue::to_string() const
  1609. {
  1610. StringBuilder builder;
  1611. for (auto& value : m_values)
  1612. builder.append(value.to_string());
  1613. return builder.to_string();
  1614. }
  1615. bool UnresolvedStyleValue::equals(StyleValue const& other) const
  1616. {
  1617. if (type() != other.type())
  1618. return false;
  1619. // This is a case where comparing the strings actually makes sense.
  1620. return to_string() == other.to_string();
  1621. }
  1622. bool UnsetStyleValue::equals(StyleValue const& other) const
  1623. {
  1624. return type() == other.type();
  1625. }
  1626. String StyleValueList::to_string() const
  1627. {
  1628. String separator = "";
  1629. switch (m_separator) {
  1630. case Separator::Space:
  1631. separator = " ";
  1632. break;
  1633. case Separator::Comma:
  1634. separator = ", ";
  1635. break;
  1636. default:
  1637. VERIFY_NOT_REACHED();
  1638. }
  1639. return String::join(separator, m_values);
  1640. }
  1641. bool StyleValueList::equals(StyleValue const& other) const
  1642. {
  1643. if (type() != other.type())
  1644. return false;
  1645. auto const& typed_other = other.as_value_list();
  1646. if (m_separator != typed_other.m_separator)
  1647. return false;
  1648. if (m_values.size() != typed_other.m_values.size())
  1649. return false;
  1650. for (size_t i = 0; i < m_values.size(); ++i) {
  1651. if (!m_values[i].equals(typed_other.m_values[i]))
  1652. return false;
  1653. }
  1654. return true;
  1655. }
  1656. NonnullRefPtr<ColorStyleValue> ColorStyleValue::create(Color color)
  1657. {
  1658. if (color.value() == 0) {
  1659. static auto transparent = adopt_ref(*new ColorStyleValue(color));
  1660. return transparent;
  1661. }
  1662. if (color == Color::from_rgb(0x000000)) {
  1663. static auto black = adopt_ref(*new ColorStyleValue(color));
  1664. return black;
  1665. }
  1666. if (color == Color::from_rgb(0xffffff)) {
  1667. static auto white = adopt_ref(*new ColorStyleValue(color));
  1668. return white;
  1669. }
  1670. return adopt_ref(*new ColorStyleValue(color));
  1671. }
  1672. NonnullRefPtr<RectStyleValue> RectStyleValue::create(EdgeRect rect)
  1673. {
  1674. return adopt_ref(*new RectStyleValue(rect));
  1675. }
  1676. NonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  1677. {
  1678. if (length.is_auto()) {
  1679. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
  1680. return value;
  1681. }
  1682. if (length.is_px()) {
  1683. if (length.raw_value() == 0) {
  1684. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
  1685. return value;
  1686. }
  1687. if (length.raw_value() == 1) {
  1688. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
  1689. return value;
  1690. }
  1691. }
  1692. return adopt_ref(*new LengthStyleValue(length));
  1693. }
  1694. 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)
  1695. {
  1696. if (length.is_px())
  1697. return {};
  1698. if (length.is_absolute() || length.is_relative()) {
  1699. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size);
  1700. return CSS::Length::make_px(px);
  1701. }
  1702. return {};
  1703. }
  1704. NonnullRefPtr<StyleValue> StyleValue::absolutized(Gfx::IntRect const&, Gfx::FontPixelMetrics const&, float, float) const
  1705. {
  1706. return *this;
  1707. }
  1708. NonnullRefPtr<StyleValue> LengthStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1709. {
  1710. if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size); length.has_value())
  1711. return LengthStyleValue::create(length.release_value());
  1712. return *this;
  1713. }
  1714. NonnullRefPtr<StyleValue> ShadowStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1715. {
  1716. auto absolutized_offset_x = absolutized_length(m_offset_x, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_x);
  1717. auto absolutized_offset_y = absolutized_length(m_offset_y, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_y);
  1718. auto absolutized_blur_radius = absolutized_length(m_blur_radius, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_blur_radius);
  1719. auto absolutized_spread_distance = absolutized_length(m_spread_distance, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_spread_distance);
  1720. return ShadowStyleValue::create(m_color, absolutized_offset_x, absolutized_offset_y, absolutized_blur_radius, absolutized_spread_distance, m_placement);
  1721. }
  1722. NonnullRefPtr<StyleValue> BorderRadiusStyleValue::absolutized(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  1723. {
  1724. if (m_horizontal_radius.is_percentage() && m_vertical_radius.is_percentage())
  1725. return *this;
  1726. auto absolutized_horizontal_radius = m_horizontal_radius;
  1727. auto absolutized_vertical_radius = m_vertical_radius;
  1728. if (!m_horizontal_radius.is_percentage())
  1729. absolutized_horizontal_radius = absolutized_length(m_horizontal_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_horizontal_radius.length());
  1730. if (!m_vertical_radius.is_percentage())
  1731. absolutized_vertical_radius = absolutized_length(m_vertical_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_vertical_radius.length());
  1732. return BorderRadiusStyleValue::create(absolutized_horizontal_radius, absolutized_vertical_radius);
  1733. }
  1734. }