StyleValue.cpp 73 KB

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