StyleValue.cpp 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/ByteBuffer.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibWeb/CSS/Serialize.h>
  12. #include <LibWeb/CSS/StyleValue.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/HTML/BrowsingContext.h>
  15. #include <LibWeb/Loader/LoadRequest.h>
  16. #include <LibWeb/Loader/ResourceLoader.h>
  17. #include <LibWeb/Page/Page.h>
  18. #include <LibWeb/Painting/GradientPainting.h>
  19. #include <LibWeb/Platform/Timer.h>
  20. namespace Web::CSS {
  21. StyleValue::StyleValue(Type type)
  22. : m_type(type)
  23. {
  24. }
  25. AbstractImageStyleValue const& StyleValue::as_abstract_image() const
  26. {
  27. VERIFY(is_abstract_image());
  28. return static_cast<AbstractImageStyleValue const&>(*this);
  29. }
  30. AngleStyleValue const& StyleValue::as_angle() const
  31. {
  32. VERIFY(is_angle());
  33. return static_cast<AngleStyleValue const&>(*this);
  34. }
  35. BackgroundStyleValue const& StyleValue::as_background() const
  36. {
  37. VERIFY(is_background());
  38. return static_cast<BackgroundStyleValue const&>(*this);
  39. }
  40. BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const
  41. {
  42. VERIFY(is_background_repeat());
  43. return static_cast<BackgroundRepeatStyleValue const&>(*this);
  44. }
  45. BackgroundSizeStyleValue const& StyleValue::as_background_size() const
  46. {
  47. VERIFY(is_background_size());
  48. return static_cast<BackgroundSizeStyleValue const&>(*this);
  49. }
  50. BorderStyleValue const& StyleValue::as_border() const
  51. {
  52. VERIFY(is_border());
  53. return static_cast<BorderStyleValue const&>(*this);
  54. }
  55. BorderRadiusStyleValue const& StyleValue::as_border_radius() const
  56. {
  57. VERIFY(is_border_radius());
  58. return static_cast<BorderRadiusStyleValue const&>(*this);
  59. }
  60. BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const
  61. {
  62. VERIFY(is_border_radius_shorthand());
  63. return static_cast<BorderRadiusShorthandStyleValue const&>(*this);
  64. }
  65. ShadowStyleValue const& StyleValue::as_shadow() const
  66. {
  67. VERIFY(is_shadow());
  68. return static_cast<ShadowStyleValue const&>(*this);
  69. }
  70. CalculatedStyleValue const& StyleValue::as_calculated() const
  71. {
  72. VERIFY(is_calculated());
  73. return static_cast<CalculatedStyleValue const&>(*this);
  74. }
  75. ColorStyleValue const& StyleValue::as_color() const
  76. {
  77. VERIFY(is_color());
  78. return static_cast<ColorStyleValue const&>(*this);
  79. }
  80. ConicGradientStyleValue const& StyleValue::as_conic_gradient() const
  81. {
  82. VERIFY(is_conic_gradient());
  83. return static_cast<ConicGradientStyleValue const&>(*this);
  84. }
  85. ContentStyleValue const& StyleValue::as_content() const
  86. {
  87. VERIFY(is_content());
  88. return static_cast<ContentStyleValue const&>(*this);
  89. }
  90. FilterValueListStyleValue const& StyleValue::as_filter_value_list() const
  91. {
  92. VERIFY(is_filter_value_list());
  93. return static_cast<FilterValueListStyleValue const&>(*this);
  94. }
  95. FlexStyleValue const& StyleValue::as_flex() const
  96. {
  97. VERIFY(is_flex());
  98. return static_cast<FlexStyleValue const&>(*this);
  99. }
  100. FlexFlowStyleValue const& StyleValue::as_flex_flow() const
  101. {
  102. VERIFY(is_flex_flow());
  103. return static_cast<FlexFlowStyleValue const&>(*this);
  104. }
  105. FontStyleValue const& StyleValue::as_font() const
  106. {
  107. VERIFY(is_font());
  108. return static_cast<FontStyleValue const&>(*this);
  109. }
  110. FrequencyStyleValue const& StyleValue::as_frequency() const
  111. {
  112. VERIFY(is_frequency());
  113. return static_cast<FrequencyStyleValue const&>(*this);
  114. }
  115. GridTrackPlacementShorthandStyleValue const& StyleValue::as_grid_track_placement_shorthand() const
  116. {
  117. VERIFY(is_grid_track_placement_shorthand());
  118. return static_cast<GridTrackPlacementShorthandStyleValue const&>(*this);
  119. }
  120. GridAreaShorthandStyleValue const& StyleValue::as_grid_area_shorthand() const
  121. {
  122. VERIFY(is_grid_area_shorthand());
  123. return static_cast<GridAreaShorthandStyleValue const&>(*this);
  124. }
  125. GridTemplateAreaStyleValue const& StyleValue::as_grid_template_area() const
  126. {
  127. VERIFY(is_grid_template_area());
  128. return static_cast<GridTemplateAreaStyleValue const&>(*this);
  129. }
  130. GridTrackPlacementStyleValue const& StyleValue::as_grid_track_placement() const
  131. {
  132. VERIFY(is_grid_track_placement());
  133. return static_cast<GridTrackPlacementStyleValue const&>(*this);
  134. }
  135. IdentifierStyleValue const& StyleValue::as_identifier() const
  136. {
  137. VERIFY(is_identifier());
  138. return static_cast<IdentifierStyleValue const&>(*this);
  139. }
  140. ImageStyleValue const& StyleValue::as_image() const
  141. {
  142. VERIFY(is_image());
  143. return static_cast<ImageStyleValue const&>(*this);
  144. }
  145. InheritStyleValue const& StyleValue::as_inherit() const
  146. {
  147. VERIFY(is_inherit());
  148. return static_cast<InheritStyleValue const&>(*this);
  149. }
  150. InitialStyleValue const& StyleValue::as_initial() const
  151. {
  152. VERIFY(is_initial());
  153. return static_cast<InitialStyleValue const&>(*this);
  154. }
  155. LengthStyleValue const& StyleValue::as_length() const
  156. {
  157. VERIFY(is_length());
  158. return static_cast<LengthStyleValue const&>(*this);
  159. }
  160. GridTrackSizeStyleValue const& StyleValue::as_grid_track_size_list() const
  161. {
  162. VERIFY(is_grid_track_size_list());
  163. return static_cast<GridTrackSizeStyleValue const&>(*this);
  164. }
  165. LinearGradientStyleValue const& StyleValue::as_linear_gradient() const
  166. {
  167. VERIFY(is_linear_gradient());
  168. return static_cast<LinearGradientStyleValue const&>(*this);
  169. }
  170. ListStyleStyleValue const& StyleValue::as_list_style() const
  171. {
  172. VERIFY(is_list_style());
  173. return static_cast<ListStyleStyleValue const&>(*this);
  174. }
  175. NumericStyleValue const& StyleValue::as_numeric() const
  176. {
  177. VERIFY(is_numeric());
  178. return static_cast<NumericStyleValue const&>(*this);
  179. }
  180. OverflowStyleValue const& StyleValue::as_overflow() const
  181. {
  182. VERIFY(is_overflow());
  183. return static_cast<OverflowStyleValue const&>(*this);
  184. }
  185. PercentageStyleValue const& StyleValue::as_percentage() const
  186. {
  187. VERIFY(is_percentage());
  188. return static_cast<PercentageStyleValue const&>(*this);
  189. }
  190. PositionStyleValue const& StyleValue::as_position() const
  191. {
  192. VERIFY(is_position());
  193. return static_cast<PositionStyleValue const&>(*this);
  194. }
  195. RadialGradientStyleValue const& StyleValue::as_radial_gradient() const
  196. {
  197. VERIFY(is_radial_gradient());
  198. return static_cast<RadialGradientStyleValue const&>(*this);
  199. }
  200. RectStyleValue const& StyleValue::as_rect() const
  201. {
  202. VERIFY(is_rect());
  203. return static_cast<RectStyleValue const&>(*this);
  204. }
  205. ResolutionStyleValue const& StyleValue::as_resolution() const
  206. {
  207. VERIFY(is_resolution());
  208. return static_cast<ResolutionStyleValue const&>(*this);
  209. }
  210. StringStyleValue const& StyleValue::as_string() const
  211. {
  212. VERIFY(is_string());
  213. return static_cast<StringStyleValue const&>(*this);
  214. }
  215. TextDecorationStyleValue const& StyleValue::as_text_decoration() const
  216. {
  217. VERIFY(is_text_decoration());
  218. return static_cast<TextDecorationStyleValue const&>(*this);
  219. }
  220. TimeStyleValue const& StyleValue::as_time() const
  221. {
  222. VERIFY(is_time());
  223. return static_cast<TimeStyleValue const&>(*this);
  224. }
  225. TransformationStyleValue const& StyleValue::as_transformation() const
  226. {
  227. VERIFY(is_transformation());
  228. return static_cast<TransformationStyleValue const&>(*this);
  229. }
  230. UnresolvedStyleValue const& StyleValue::as_unresolved() const
  231. {
  232. VERIFY(is_unresolved());
  233. return static_cast<UnresolvedStyleValue const&>(*this);
  234. }
  235. UnsetStyleValue const& StyleValue::as_unset() const
  236. {
  237. VERIFY(is_unset());
  238. return static_cast<UnsetStyleValue const&>(*this);
  239. }
  240. StyleValueList const& StyleValue::as_value_list() const
  241. {
  242. VERIFY(is_value_list());
  243. return static_cast<StyleValueList const&>(*this);
  244. }
  245. BackgroundStyleValue::BackgroundStyleValue(
  246. NonnullRefPtr<StyleValue> color,
  247. NonnullRefPtr<StyleValue> image,
  248. NonnullRefPtr<StyleValue> position,
  249. NonnullRefPtr<StyleValue> size,
  250. NonnullRefPtr<StyleValue> repeat,
  251. NonnullRefPtr<StyleValue> attachment,
  252. NonnullRefPtr<StyleValue> origin,
  253. NonnullRefPtr<StyleValue> clip)
  254. : StyleValue(Type::Background)
  255. , m_color(color)
  256. , m_image(image)
  257. , m_position(position)
  258. , m_size(size)
  259. , m_repeat(repeat)
  260. , m_attachment(attachment)
  261. , m_origin(origin)
  262. , m_clip(clip)
  263. {
  264. auto layer_count = [](auto style_value) -> size_t {
  265. if (style_value->is_value_list())
  266. return style_value->as_value_list().size();
  267. else
  268. return 1;
  269. };
  270. m_layer_count = max(layer_count(m_image), layer_count(m_position));
  271. m_layer_count = max(m_layer_count, layer_count(m_size));
  272. m_layer_count = max(m_layer_count, layer_count(m_repeat));
  273. m_layer_count = max(m_layer_count, layer_count(m_attachment));
  274. m_layer_count = max(m_layer_count, layer_count(m_origin));
  275. m_layer_count = max(m_layer_count, layer_count(m_clip));
  276. VERIFY(!m_color->is_value_list());
  277. }
  278. ErrorOr<String> BackgroundStyleValue::to_string() const
  279. {
  280. if (m_layer_count == 1) {
  281. return String::formatted("{} {} {} {} {} {} {} {}", TRY(m_color->to_string()), TRY(m_image->to_string()), TRY(m_position->to_string()), TRY(m_size->to_string()), TRY(m_repeat->to_string()), TRY(m_attachment->to_string()), TRY(m_origin->to_string()), TRY(m_clip->to_string()));
  282. }
  283. auto get_layer_value_string = [](NonnullRefPtr<StyleValue> const& style_value, size_t index) {
  284. if (style_value->is_value_list())
  285. return style_value->as_value_list().value_at(index, true)->to_string();
  286. return style_value->to_string();
  287. };
  288. StringBuilder builder;
  289. for (size_t i = 0; i < m_layer_count; i++) {
  290. if (i)
  291. TRY(builder.try_append(", "sv));
  292. if (i == m_layer_count - 1)
  293. TRY(builder.try_appendff("{} ", TRY(m_color->to_string())));
  294. TRY(builder.try_appendff("{} {} {} {} {} {} {}", TRY(get_layer_value_string(m_image, i)), TRY(get_layer_value_string(m_position, i)), TRY(get_layer_value_string(m_size, i)), TRY(get_layer_value_string(m_repeat, i)), TRY(get_layer_value_string(m_attachment, i)), TRY(get_layer_value_string(m_origin, i)), TRY(get_layer_value_string(m_clip, i))));
  295. }
  296. return builder.to_string();
  297. }
  298. bool BackgroundStyleValue::equals(StyleValue const& other) const
  299. {
  300. if (type() != other.type())
  301. return false;
  302. auto const& typed_other = other.as_background();
  303. return m_color->equals(typed_other.m_color)
  304. && m_image->equals(typed_other.m_image)
  305. && m_position->equals(typed_other.m_position)
  306. && m_size->equals(typed_other.m_size)
  307. && m_repeat->equals(typed_other.m_repeat)
  308. && m_attachment->equals(typed_other.m_attachment)
  309. && m_origin->equals(typed_other.m_origin)
  310. && m_clip->equals(typed_other.m_clip);
  311. }
  312. ErrorOr<String> BackgroundRepeatStyleValue::to_string() const
  313. {
  314. return String::formatted("{} {}", CSS::to_string(m_repeat_x), CSS::to_string(m_repeat_y));
  315. }
  316. bool BackgroundRepeatStyleValue::equals(StyleValue const& other) const
  317. {
  318. if (type() != other.type())
  319. return false;
  320. auto const& typed_other = other.as_background_repeat();
  321. return m_repeat_x == typed_other.m_repeat_x && m_repeat_y == typed_other.m_repeat_y;
  322. }
  323. ErrorOr<String> BackgroundSizeStyleValue::to_string() const
  324. {
  325. return String::formatted("{} {}", TRY(m_size_x.to_string()), TRY(m_size_y.to_string()));
  326. }
  327. bool BackgroundSizeStyleValue::equals(StyleValue const& other) const
  328. {
  329. if (type() != other.type())
  330. return false;
  331. auto const& typed_other = other.as_background_size();
  332. return m_size_x == typed_other.m_size_x && m_size_y == typed_other.m_size_y;
  333. }
  334. ErrorOr<String> BorderStyleValue::to_string() const
  335. {
  336. return String::formatted("{} {} {}", TRY(m_border_width->to_string()), TRY(m_border_style->to_string()), TRY(m_border_color->to_string()));
  337. }
  338. bool BorderStyleValue::equals(StyleValue const& other) const
  339. {
  340. if (type() != other.type())
  341. return false;
  342. auto const& typed_other = other.as_border();
  343. return m_border_width->equals(typed_other.m_border_width)
  344. && m_border_style->equals(typed_other.m_border_style)
  345. && m_border_color->equals(typed_other.m_border_color);
  346. }
  347. ErrorOr<String> BorderRadiusStyleValue::to_string() const
  348. {
  349. if (m_horizontal_radius == m_vertical_radius)
  350. return m_horizontal_radius.to_string();
  351. return String::formatted("{} / {}", TRY(m_horizontal_radius.to_string()), TRY(m_vertical_radius.to_string()));
  352. }
  353. bool BorderRadiusStyleValue::equals(StyleValue const& other) const
  354. {
  355. if (type() != other.type())
  356. return false;
  357. auto const& typed_other = other.as_border_radius();
  358. return m_is_elliptical == typed_other.m_is_elliptical
  359. && m_horizontal_radius == typed_other.m_horizontal_radius
  360. && m_vertical_radius == typed_other.m_vertical_radius;
  361. }
  362. ErrorOr<String> BorderRadiusShorthandStyleValue::to_string() const
  363. {
  364. return String::formatted("{} {} {} {} / {} {} {} {}", TRY(m_top_left->horizontal_radius().to_string()), TRY(m_top_right->horizontal_radius().to_string()), TRY(m_bottom_right->horizontal_radius().to_string()), TRY(m_bottom_left->horizontal_radius().to_string()), TRY(m_top_left->vertical_radius().to_string()), TRY(m_top_right->vertical_radius().to_string()), TRY(m_bottom_right->vertical_radius().to_string()), TRY(m_bottom_left->vertical_radius().to_string()));
  365. }
  366. bool BorderRadiusShorthandStyleValue::equals(StyleValue const& other) const
  367. {
  368. if (type() != other.type())
  369. return false;
  370. auto const& typed_other = other.as_border_radius_shorthand();
  371. return m_top_left->equals(typed_other.m_top_left)
  372. && m_top_right->equals(typed_other.m_top_right)
  373. && m_bottom_right->equals(typed_other.m_bottom_right)
  374. && m_bottom_left->equals(typed_other.m_bottom_left);
  375. }
  376. void CalculatedStyleValue::CalculationResult::add(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  377. {
  378. add_or_subtract_internal(SumOperation::Add, other, layout_node, percentage_basis);
  379. }
  380. void CalculatedStyleValue::CalculationResult::subtract(CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  381. {
  382. add_or_subtract_internal(SumOperation::Subtract, other, layout_node, percentage_basis);
  383. }
  384. void CalculatedStyleValue::CalculationResult::add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const* layout_node, PercentageBasis const& percentage_basis)
  385. {
  386. // 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>".
  387. // Though, having the same type may mean that one side is a <dimension> and the other a <percentage>.
  388. // Note: This is almost identical to ::add()
  389. m_value.visit(
  390. [&](Number const& number) {
  391. auto other_number = other.m_value.get<Number>();
  392. if (op == SumOperation::Add) {
  393. m_value = number + other_number;
  394. } else {
  395. m_value = number - other_number;
  396. }
  397. },
  398. [&](Angle const& angle) {
  399. auto this_degrees = angle.to_degrees();
  400. if (other.m_value.has<Angle>()) {
  401. auto other_degrees = other.m_value.get<Angle>().to_degrees();
  402. if (op == SumOperation::Add)
  403. m_value = Angle::make_degrees(this_degrees + other_degrees);
  404. else
  405. m_value = Angle::make_degrees(this_degrees - other_degrees);
  406. } else {
  407. VERIFY(percentage_basis.has<Angle>());
  408. auto other_degrees = percentage_basis.get<Angle>().percentage_of(other.m_value.get<Percentage>()).to_degrees();
  409. if (op == SumOperation::Add)
  410. m_value = Angle::make_degrees(this_degrees + other_degrees);
  411. else
  412. m_value = Angle::make_degrees(this_degrees - other_degrees);
  413. }
  414. },
  415. [&](Frequency const& frequency) {
  416. auto this_hertz = frequency.to_hertz();
  417. if (other.m_value.has<Frequency>()) {
  418. auto other_hertz = other.m_value.get<Frequency>().to_hertz();
  419. if (op == SumOperation::Add)
  420. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  421. else
  422. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  423. } else {
  424. VERIFY(percentage_basis.has<Frequency>());
  425. auto other_hertz = percentage_basis.get<Frequency>().percentage_of(other.m_value.get<Percentage>()).to_hertz();
  426. if (op == SumOperation::Add)
  427. m_value = Frequency::make_hertz(this_hertz + other_hertz);
  428. else
  429. m_value = Frequency::make_hertz(this_hertz - other_hertz);
  430. }
  431. },
  432. [&](Length const& length) {
  433. auto this_px = length.to_px(*layout_node);
  434. if (other.m_value.has<Length>()) {
  435. auto other_px = other.m_value.get<Length>().to_px(*layout_node);
  436. if (op == SumOperation::Add)
  437. m_value = Length::make_px(this_px + other_px);
  438. else
  439. m_value = Length::make_px(this_px - other_px);
  440. } else {
  441. VERIFY(percentage_basis.has<Length>());
  442. auto other_px = percentage_basis.get<Length>().percentage_of(other.m_value.get<Percentage>()).to_px(*layout_node);
  443. if (op == SumOperation::Add)
  444. m_value = Length::make_px(this_px + other_px);
  445. else
  446. m_value = Length::make_px(this_px - other_px);
  447. }
  448. },
  449. [&](Time const& time) {
  450. auto this_seconds = time.to_seconds();
  451. if (other.m_value.has<Time>()) {
  452. auto other_seconds = other.m_value.get<Time>().to_seconds();
  453. if (op == SumOperation::Add)
  454. m_value = Time::make_seconds(this_seconds + other_seconds);
  455. else
  456. m_value = Time::make_seconds(this_seconds - other_seconds);
  457. } else {
  458. VERIFY(percentage_basis.has<Time>());
  459. auto other_seconds = percentage_basis.get<Time>().percentage_of(other.m_value.get<Percentage>()).to_seconds();
  460. if (op == SumOperation::Add)
  461. m_value = Time::make_seconds(this_seconds + other_seconds);
  462. else
  463. m_value = Time::make_seconds(this_seconds - other_seconds);
  464. }
  465. },
  466. [&](Percentage const& percentage) {
  467. if (other.m_value.has<Percentage>()) {
  468. if (op == SumOperation::Add)
  469. m_value = Percentage { percentage.value() + other.m_value.get<Percentage>().value() };
  470. else
  471. m_value = Percentage { percentage.value() - other.m_value.get<Percentage>().value() };
  472. return;
  473. }
  474. // 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`.
  475. CalculationResult new_value = other;
  476. if (op == SumOperation::Add) {
  477. new_value.add(*this, layout_node, percentage_basis);
  478. } else {
  479. // Turn 'this - other' into '-other + this', as 'A + B == B + A', but 'A - B != B - A'
  480. new_value.multiply_by({ Number { Number::Type::Integer, -1.0f } }, layout_node);
  481. new_value.add(*this, layout_node, percentage_basis);
  482. }
  483. *this = new_value;
  484. });
  485. }
  486. void CalculatedStyleValue::CalculationResult::multiply_by(CalculationResult const& other, Layout::Node const* layout_node)
  487. {
  488. // We know from validation when resolving the type, that at least one side must be a <number> or <integer>.
  489. // Both of these are represented as a float.
  490. VERIFY(m_value.has<Number>() || other.m_value.has<Number>());
  491. bool other_is_number = other.m_value.has<Number>();
  492. m_value.visit(
  493. [&](Number const& number) {
  494. if (other_is_number) {
  495. m_value = number * other.m_value.get<Number>();
  496. } else {
  497. // Avoid duplicating all the logic by swapping `this` and `other`.
  498. CalculationResult new_value = other;
  499. new_value.multiply_by(*this, layout_node);
  500. *this = new_value;
  501. }
  502. },
  503. [&](Angle const& angle) {
  504. m_value = Angle::make_degrees(angle.to_degrees() * other.m_value.get<Number>().value());
  505. },
  506. [&](Frequency const& frequency) {
  507. m_value = Frequency::make_hertz(frequency.to_hertz() * other.m_value.get<Number>().value());
  508. },
  509. [&](Length const& length) {
  510. VERIFY(layout_node);
  511. m_value = Length::make_px(length.to_px(*layout_node) * other.m_value.get<Number>().value());
  512. },
  513. [&](Time const& time) {
  514. m_value = Time::make_seconds(time.to_seconds() * other.m_value.get<Number>().value());
  515. },
  516. [&](Percentage const& percentage) {
  517. m_value = Percentage { percentage.value() * other.m_value.get<Number>().value() };
  518. });
  519. }
  520. void CalculatedStyleValue::CalculationResult::divide_by(CalculationResult const& other, Layout::Node const* layout_node)
  521. {
  522. // We know from validation when resolving the type, that `other` must be a <number> or <integer>.
  523. // Both of these are represented as a Number.
  524. auto denominator = other.m_value.get<Number>().value();
  525. // FIXME: Dividing by 0 is invalid, and should be caught during parsing.
  526. VERIFY(denominator != 0.0f);
  527. m_value.visit(
  528. [&](Number const& number) {
  529. m_value = Number {
  530. Number::Type::Number,
  531. number.value() / denominator
  532. };
  533. },
  534. [&](Angle const& angle) {
  535. m_value = Angle::make_degrees(angle.to_degrees() / denominator);
  536. },
  537. [&](Frequency const& frequency) {
  538. m_value = Frequency::make_hertz(frequency.to_hertz() / denominator);
  539. },
  540. [&](Length const& length) {
  541. VERIFY(layout_node);
  542. m_value = Length::make_px(length.to_px(*layout_node) / denominator);
  543. },
  544. [&](Time const& time) {
  545. m_value = Time::make_seconds(time.to_seconds() / denominator);
  546. },
  547. [&](Percentage const& percentage) {
  548. m_value = Percentage { percentage.value() / denominator };
  549. });
  550. }
  551. ErrorOr<String> CalculatedStyleValue::to_string() const
  552. {
  553. return String::formatted("calc({})", TRY(m_expression->to_string()));
  554. }
  555. bool CalculatedStyleValue::equals(StyleValue const& other) const
  556. {
  557. if (type() != other.type())
  558. return false;
  559. // This is a case where comparing the strings actually makes sense.
  560. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  561. }
  562. ErrorOr<String> CalculatedStyleValue::CalcNumberValue::to_string() const
  563. {
  564. return value.visit(
  565. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  566. [](NonnullOwnPtr<CalcNumberSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); });
  567. }
  568. ErrorOr<String> CalculatedStyleValue::CalcValue::to_string() const
  569. {
  570. return value.visit(
  571. [](Number const& number) -> ErrorOr<String> { return String::number(number.value()); },
  572. [](NonnullOwnPtr<CalcSum> const& sum) -> ErrorOr<String> { return String::formatted("({})", TRY(sum->to_string())); },
  573. [](auto const& v) -> ErrorOr<String> { return v.to_string(); });
  574. }
  575. ErrorOr<String> CalculatedStyleValue::CalcSum::to_string() const
  576. {
  577. StringBuilder builder;
  578. TRY(builder.try_append(TRY(first_calc_product->to_string())));
  579. for (auto const& item : zero_or_more_additional_calc_products)
  580. TRY(builder.try_append(TRY(item.to_string())));
  581. return builder.to_string();
  582. }
  583. ErrorOr<String> CalculatedStyleValue::CalcNumberSum::to_string() const
  584. {
  585. StringBuilder builder;
  586. TRY(builder.try_append(TRY(first_calc_number_product->to_string())));
  587. for (auto const& item : zero_or_more_additional_calc_number_products)
  588. TRY(builder.try_append(TRY(item.to_string())));
  589. return builder.to_string();
  590. }
  591. ErrorOr<String> CalculatedStyleValue::CalcProduct::to_string() const
  592. {
  593. StringBuilder builder;
  594. TRY(builder.try_append(TRY(first_calc_value.to_string())));
  595. for (auto const& item : zero_or_more_additional_calc_values)
  596. TRY(builder.try_append(TRY(item.to_string())));
  597. return builder.to_string();
  598. }
  599. ErrorOr<String> CalculatedStyleValue::CalcSumPartWithOperator::to_string() const
  600. {
  601. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  602. }
  603. ErrorOr<String> CalculatedStyleValue::CalcProductPartWithOperator::to_string() const
  604. {
  605. auto value_string = TRY(value.visit(
  606. [](CalcValue const& v) { return v.to_string(); },
  607. [](CalcNumberValue const& v) { return v.to_string(); }));
  608. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, value_string);
  609. }
  610. ErrorOr<String> CalculatedStyleValue::CalcNumberProduct::to_string() const
  611. {
  612. StringBuilder builder;
  613. TRY(builder.try_append(TRY(first_calc_number_value.to_string())));
  614. for (auto const& item : zero_or_more_additional_calc_number_values)
  615. TRY(builder.try_append(TRY(item.to_string())));
  616. return builder.to_string();
  617. }
  618. ErrorOr<String> CalculatedStyleValue::CalcNumberProductPartWithOperator::to_string() const
  619. {
  620. return String::formatted(" {} {}", op == ProductOperation::Multiply ? "*"sv : "/"sv, TRY(value.to_string()));
  621. }
  622. ErrorOr<String> CalculatedStyleValue::CalcNumberSumPartWithOperator::to_string() const
  623. {
  624. return String::formatted(" {} {}", op == SumOperation::Add ? "+"sv : "-"sv, TRY(value->to_string()));
  625. }
  626. Optional<Angle> CalculatedStyleValue::resolve_angle() const
  627. {
  628. auto result = m_expression->resolve(nullptr, {});
  629. if (result.value().has<Angle>())
  630. return result.value().get<Angle>();
  631. return {};
  632. }
  633. Optional<Angle> CalculatedStyleValue::resolve_angle_percentage(Angle const& percentage_basis) const
  634. {
  635. auto result = m_expression->resolve(nullptr, percentage_basis);
  636. return result.value().visit(
  637. [&](Angle const& angle) -> Optional<Angle> {
  638. return angle;
  639. },
  640. [&](Percentage const& percentage) -> Optional<Angle> {
  641. return percentage_basis.percentage_of(percentage);
  642. },
  643. [&](auto const&) -> Optional<Angle> {
  644. return {};
  645. });
  646. }
  647. Optional<Frequency> CalculatedStyleValue::resolve_frequency() const
  648. {
  649. auto result = m_expression->resolve(nullptr, {});
  650. if (result.value().has<Frequency>())
  651. return result.value().get<Frequency>();
  652. return {};
  653. }
  654. Optional<Frequency> CalculatedStyleValue::resolve_frequency_percentage(Frequency const& percentage_basis) const
  655. {
  656. auto result = m_expression->resolve(nullptr, percentage_basis);
  657. return result.value().visit(
  658. [&](Frequency const& frequency) -> Optional<Frequency> {
  659. return frequency;
  660. },
  661. [&](Percentage const& percentage) -> Optional<Frequency> {
  662. return percentage_basis.percentage_of(percentage);
  663. },
  664. [&](auto const&) -> Optional<Frequency> {
  665. return {};
  666. });
  667. }
  668. Optional<Length> CalculatedStyleValue::resolve_length(Layout::Node const& layout_node) const
  669. {
  670. auto result = m_expression->resolve(&layout_node, {});
  671. if (result.value().has<Length>())
  672. return result.value().get<Length>();
  673. return {};
  674. }
  675. Optional<Length> CalculatedStyleValue::resolve_length_percentage(Layout::Node const& layout_node, Length const& percentage_basis) const
  676. {
  677. auto result = m_expression->resolve(&layout_node, percentage_basis);
  678. return result.value().visit(
  679. [&](Length const& length) -> Optional<Length> {
  680. return length;
  681. },
  682. [&](Percentage const& percentage) -> Optional<Length> {
  683. return percentage_basis.percentage_of(percentage);
  684. },
  685. [&](auto const&) -> Optional<Length> {
  686. return {};
  687. });
  688. }
  689. Optional<Percentage> CalculatedStyleValue::resolve_percentage() const
  690. {
  691. auto result = m_expression->resolve(nullptr, {});
  692. if (result.value().has<Percentage>())
  693. return result.value().get<Percentage>();
  694. return {};
  695. }
  696. Optional<Time> CalculatedStyleValue::resolve_time() const
  697. {
  698. auto result = m_expression->resolve(nullptr, {});
  699. if (result.value().has<Time>())
  700. return result.value().get<Time>();
  701. return {};
  702. }
  703. Optional<Time> CalculatedStyleValue::resolve_time_percentage(Time const& percentage_basis) const
  704. {
  705. auto result = m_expression->resolve(nullptr, percentage_basis);
  706. return result.value().visit(
  707. [&](Time const& time) -> Optional<Time> {
  708. return time;
  709. },
  710. [&](auto const&) -> Optional<Time> {
  711. return {};
  712. });
  713. }
  714. Optional<float> CalculatedStyleValue::resolve_number()
  715. {
  716. auto result = m_expression->resolve(nullptr, {});
  717. if (result.value().has<Number>())
  718. return result.value().get<Number>().value();
  719. return {};
  720. }
  721. Optional<i64> CalculatedStyleValue::resolve_integer()
  722. {
  723. auto result = m_expression->resolve(nullptr, {});
  724. if (result.value().has<Number>())
  725. return result.value().get<Number>().integer_value();
  726. return {};
  727. }
  728. static bool is_number(CalculatedStyleValue::ResolvedType type)
  729. {
  730. return type == CalculatedStyleValue::ResolvedType::Number || type == CalculatedStyleValue::ResolvedType::Integer;
  731. }
  732. static bool is_dimension(CalculatedStyleValue::ResolvedType type)
  733. {
  734. return type != CalculatedStyleValue::ResolvedType::Number
  735. && type != CalculatedStyleValue::ResolvedType::Integer
  736. && type != CalculatedStyleValue::ResolvedType::Percentage;
  737. }
  738. template<typename SumWithOperator>
  739. static Optional<CalculatedStyleValue::ResolvedType> resolve_sum_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<SumWithOperator> const& zero_or_more_additional_products)
  740. {
  741. auto type = first_type;
  742. for (auto const& product : zero_or_more_additional_products) {
  743. auto maybe_product_type = product.resolved_type();
  744. if (!maybe_product_type.has_value())
  745. return {};
  746. auto product_type = maybe_product_type.value();
  747. // At + or -, check that both sides have the same type, or that one side is a <number> and the other is an <integer>.
  748. // If both sides are the same type, resolve to that type.
  749. if (product_type == type)
  750. continue;
  751. // If one side is a <number> and the other is an <integer>, resolve to <number>.
  752. if (is_number(type) && is_number(product_type)) {
  753. type = CalculatedStyleValue::ResolvedType::Number;
  754. continue;
  755. }
  756. // FIXME: calc() handles <percentage> by allowing them to pretend to be whatever <dimension> type is allowed at this location.
  757. // Since we can't easily check what that type is, we just allow <percentage> to combine with any other <dimension> type.
  758. if (type == CalculatedStyleValue::ResolvedType::Percentage && is_dimension(product_type)) {
  759. type = product_type;
  760. continue;
  761. }
  762. if (is_dimension(type) && product_type == CalculatedStyleValue::ResolvedType::Percentage)
  763. continue;
  764. return {};
  765. }
  766. return type;
  767. }
  768. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSum::resolved_type() const
  769. {
  770. auto maybe_type = first_calc_product->resolved_type();
  771. if (!maybe_type.has_value())
  772. return {};
  773. auto type = maybe_type.value();
  774. return resolve_sum_type(type, zero_or_more_additional_calc_products);
  775. }
  776. // https://www.w3.org/TR/CSS2/visufx.html#value-def-shape
  777. Gfx::FloatRect EdgeRect::resolved(Layout::Node const& layout_node, Gfx::FloatRect border_box) const
  778. {
  779. // In CSS 2.1, the only valid <shape> value is: rect(<top>, <right>, <bottom>, <left>) where
  780. // <top> and <bottom> specify offsets from the top border edge of the box, and <right>, and
  781. // <left> specify offsets from the left border edge of the box.
  782. // The value 'auto' means that a given edge of the clipping region will be the same as the edge
  783. // of the element's generated border box (i.e., 'auto' means the same as '0' for <top> and
  784. // <left>, the same as the used value of the height plus the sum of vertical padding and border
  785. // widths for <bottom>, and the same as the used value of the width plus the sum of the
  786. // horizontal padding and border widths for <right>, such that four 'auto' values result in the
  787. // clipping region being the same as the element's border box).
  788. auto left = border_box.left() + (left_edge.is_auto() ? 0 : left_edge.to_px(layout_node)).value();
  789. auto top = border_box.top() + (top_edge.is_auto() ? 0 : top_edge.to_px(layout_node)).value();
  790. auto right = border_box.left() + (right_edge.is_auto() ? border_box.width() : right_edge.to_px(layout_node)).value();
  791. auto bottom = border_box.top() + (bottom_edge.is_auto() ? border_box.height() : bottom_edge.to_px(layout_node)).value();
  792. return Gfx::FloatRect {
  793. left,
  794. top,
  795. right - left,
  796. bottom - top
  797. };
  798. }
  799. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSum::resolved_type() const
  800. {
  801. auto maybe_type = first_calc_number_product->resolved_type();
  802. if (!maybe_type.has_value())
  803. return {};
  804. auto type = maybe_type.value();
  805. return resolve_sum_type(type, zero_or_more_additional_calc_number_products);
  806. }
  807. template<typename ProductWithOperator>
  808. static Optional<CalculatedStyleValue::ResolvedType> resolve_product_type(CalculatedStyleValue::ResolvedType first_type, NonnullOwnPtrVector<ProductWithOperator> const& zero_or_more_additional_values)
  809. {
  810. auto type = first_type;
  811. for (auto const& value : zero_or_more_additional_values) {
  812. auto maybe_value_type = value.resolved_type();
  813. if (!maybe_value_type.has_value())
  814. return {};
  815. auto value_type = maybe_value_type.value();
  816. if (value.op == CalculatedStyleValue::ProductOperation::Multiply) {
  817. // At *, check that at least one side is <number>.
  818. if (!(is_number(type) || is_number(value_type)))
  819. return {};
  820. // If both sides are <integer>, resolve to <integer>.
  821. if (type == CalculatedStyleValue::ResolvedType::Integer && value_type == CalculatedStyleValue::ResolvedType::Integer) {
  822. type = CalculatedStyleValue::ResolvedType::Integer;
  823. } else {
  824. // Otherwise, resolve to the type of the other side.
  825. if (is_number(type))
  826. type = value_type;
  827. }
  828. continue;
  829. } else {
  830. VERIFY(value.op == CalculatedStyleValue::ProductOperation::Divide);
  831. // At /, check that the right side is <number>.
  832. if (!is_number(value_type))
  833. return {};
  834. // If the left side is <integer>, resolve to <number>.
  835. if (type == CalculatedStyleValue::ResolvedType::Integer) {
  836. type = CalculatedStyleValue::ResolvedType::Number;
  837. } else {
  838. // Otherwise, resolve to the type of the left side.
  839. }
  840. // FIXME: Division by zero makes the whole calc() expression invalid.
  841. }
  842. }
  843. return type;
  844. }
  845. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProduct::resolved_type() const
  846. {
  847. auto maybe_type = first_calc_value.resolved_type();
  848. if (!maybe_type.has_value())
  849. return {};
  850. auto type = maybe_type.value();
  851. return resolve_product_type(type, zero_or_more_additional_calc_values);
  852. }
  853. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcSumPartWithOperator::resolved_type() const
  854. {
  855. return value->resolved_type();
  856. }
  857. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProduct::resolved_type() const
  858. {
  859. auto maybe_type = first_calc_number_value.resolved_type();
  860. if (!maybe_type.has_value())
  861. return {};
  862. auto type = maybe_type.value();
  863. return resolve_product_type(type, zero_or_more_additional_calc_number_values);
  864. }
  865. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberProductPartWithOperator::resolved_type() const
  866. {
  867. return value.resolved_type();
  868. }
  869. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberSumPartWithOperator::resolved_type() const
  870. {
  871. return value->resolved_type();
  872. }
  873. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcProductPartWithOperator::resolved_type() const
  874. {
  875. return value.visit(
  876. [](CalcValue const& calc_value) {
  877. return calc_value.resolved_type();
  878. },
  879. [](CalcNumberValue const& calc_number_value) {
  880. return calc_number_value.resolved_type();
  881. });
  882. }
  883. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcValue::resolved_type() const
  884. {
  885. return value.visit(
  886. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  887. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  888. },
  889. [](Angle const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Angle }; },
  890. [](Frequency const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Frequency }; },
  891. [](Length const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Length }; },
  892. [](Percentage const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Percentage }; },
  893. [](Time const&) -> Optional<CalculatedStyleValue::ResolvedType> { return { ResolvedType::Time }; },
  894. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->resolved_type(); });
  895. }
  896. Optional<CalculatedStyleValue::ResolvedType> CalculatedStyleValue::CalcNumberValue::resolved_type() const
  897. {
  898. return value.visit(
  899. [](Number const& number) -> Optional<CalculatedStyleValue::ResolvedType> {
  900. return { number.is_integer() ? ResolvedType::Integer : ResolvedType::Number };
  901. },
  902. [](NonnullOwnPtr<CalcNumberSum> const& sum) { return sum->resolved_type(); });
  903. }
  904. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  905. {
  906. return value.visit(
  907. [&](Number const& number) -> CalculatedStyleValue::CalculationResult {
  908. return CalculatedStyleValue::CalculationResult { number };
  909. },
  910. [&](NonnullOwnPtr<CalcNumberSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  911. return sum->resolve(layout_node, percentage_basis);
  912. });
  913. }
  914. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcValue::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  915. {
  916. return value.visit(
  917. [&](NonnullOwnPtr<CalcSum> const& sum) -> CalculatedStyleValue::CalculationResult {
  918. return sum->resolve(layout_node, percentage_basis);
  919. },
  920. [&](auto const& v) -> CalculatedStyleValue::CalculationResult {
  921. return CalculatedStyleValue::CalculationResult { v };
  922. });
  923. }
  924. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  925. {
  926. auto value = first_calc_product->resolve(layout_node, percentage_basis);
  927. for (auto& additional_product : zero_or_more_additional_calc_products) {
  928. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  929. if (additional_product.op == CalculatedStyleValue::SumOperation::Add)
  930. value.add(additional_value, layout_node, percentage_basis);
  931. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  932. value.subtract(additional_value, layout_node, percentage_basis);
  933. else
  934. VERIFY_NOT_REACHED();
  935. }
  936. return value;
  937. }
  938. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSum::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  939. {
  940. auto value = first_calc_number_product->resolve(layout_node, percentage_basis);
  941. for (auto& additional_product : zero_or_more_additional_calc_number_products) {
  942. auto additional_value = additional_product.resolve(layout_node, percentage_basis);
  943. if (additional_product.op == CSS::CalculatedStyleValue::SumOperation::Add)
  944. value.add(additional_value, layout_node, percentage_basis);
  945. else if (additional_product.op == CalculatedStyleValue::SumOperation::Subtract)
  946. value.subtract(additional_value, layout_node, percentage_basis);
  947. else
  948. VERIFY_NOT_REACHED();
  949. }
  950. return value;
  951. }
  952. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  953. {
  954. auto value = first_calc_value.resolve(layout_node, percentage_basis);
  955. for (auto& additional_value : zero_or_more_additional_calc_values) {
  956. additional_value.value.visit(
  957. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  958. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Multiply);
  959. auto resolved_value = calc_value.resolve(layout_node, percentage_basis);
  960. value.multiply_by(resolved_value, layout_node);
  961. },
  962. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  963. VERIFY(additional_value.op == CalculatedStyleValue::ProductOperation::Divide);
  964. auto resolved_calc_number_value = calc_number_value.resolve(layout_node, percentage_basis);
  965. // FIXME: Checking for division by 0 should happen during parsing.
  966. VERIFY(resolved_calc_number_value.value().get<Number>().value() != 0.0f);
  967. value.divide_by(resolved_calc_number_value, layout_node);
  968. });
  969. }
  970. return value;
  971. }
  972. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProduct::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  973. {
  974. auto value = first_calc_number_value.resolve(layout_node, percentage_basis);
  975. for (auto& additional_number_value : zero_or_more_additional_calc_number_values) {
  976. auto additional_value = additional_number_value.resolve(layout_node, percentage_basis);
  977. if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Multiply)
  978. value.multiply_by(additional_value, layout_node);
  979. else if (additional_number_value.op == CalculatedStyleValue::ProductOperation::Divide)
  980. value.divide_by(additional_value, layout_node);
  981. else
  982. VERIFY_NOT_REACHED();
  983. }
  984. return value;
  985. }
  986. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  987. {
  988. return value.visit(
  989. [&](CalcValue const& calc_value) {
  990. return calc_value.resolve(layout_node, percentage_basis);
  991. },
  992. [&](CalcNumberValue const& calc_number_value) {
  993. return calc_number_value.resolve(layout_node, percentage_basis);
  994. });
  995. }
  996. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  997. {
  998. return value->resolve(layout_node, percentage_basis);
  999. }
  1000. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberProductPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  1001. {
  1002. return value.resolve(layout_node, percentage_basis);
  1003. }
  1004. CalculatedStyleValue::CalculationResult CalculatedStyleValue::CalcNumberSumPartWithOperator::resolve(Layout::Node const* layout_node, PercentageBasis const& percentage_basis) const
  1005. {
  1006. return value->resolve(layout_node, percentage_basis);
  1007. }
  1008. ErrorOr<String> ColorStyleValue::to_string() const
  1009. {
  1010. return serialize_a_srgb_value(m_color);
  1011. }
  1012. bool ColorStyleValue::equals(StyleValue const& other) const
  1013. {
  1014. if (type() != other.type())
  1015. return false;
  1016. return m_color == other.as_color().m_color;
  1017. }
  1018. ErrorOr<String> ContentStyleValue::to_string() const
  1019. {
  1020. if (has_alt_text())
  1021. return String::formatted("{} / {}", TRY(m_content->to_string()), TRY(m_alt_text->to_string()));
  1022. return m_content->to_string();
  1023. }
  1024. bool ContentStyleValue::equals(StyleValue const& other) const
  1025. {
  1026. if (type() != other.type())
  1027. return false;
  1028. auto const& typed_other = other.as_content();
  1029. if (!m_content->equals(typed_other.m_content))
  1030. return false;
  1031. if (m_alt_text.is_null() != typed_other.m_alt_text.is_null())
  1032. return false;
  1033. if (!m_alt_text.is_null())
  1034. return m_alt_text->equals(*typed_other.m_alt_text);
  1035. return true;
  1036. }
  1037. float Filter::Blur::resolved_radius(Layout::Node const& node) const
  1038. {
  1039. // Default value when omitted is 0px.
  1040. auto sigma = 0;
  1041. if (radius.has_value())
  1042. sigma = radius->resolved(node).to_px(node).value();
  1043. // Note: The radius/sigma of the blur needs to be doubled for LibGfx's blur functions.
  1044. return sigma * 2;
  1045. }
  1046. Filter::DropShadow::Resolved Filter::DropShadow::resolved(Layout::Node const& node) const
  1047. {
  1048. // The default value for omitted values is missing length values set to 0
  1049. // and the missing used color is taken from the color property.
  1050. return Resolved {
  1051. offset_x.resolved(node).to_px(node).value(),
  1052. offset_y.resolved(node).to_px(node).value(),
  1053. radius.has_value() ? radius->resolved(node).to_px(node).value() : 0.0f,
  1054. color.has_value() ? *color : node.computed_values().color()
  1055. };
  1056. }
  1057. float Filter::HueRotate::angle_degrees() const
  1058. {
  1059. // Default value when omitted is 0deg.
  1060. if (!angle.has_value())
  1061. return 0.0f;
  1062. return angle->visit([&](Angle const& angle) { return angle.to_degrees(); }, [&](auto) { return 0.0f; });
  1063. }
  1064. float Filter::Color::resolved_amount() const
  1065. {
  1066. if (amount.has_value()) {
  1067. if (amount->is_percentage())
  1068. return amount->percentage().as_fraction();
  1069. return amount->number().value();
  1070. }
  1071. // All color filters (brightness, sepia, etc) have a default amount of 1.
  1072. return 1.0f;
  1073. }
  1074. ErrorOr<String> FilterValueListStyleValue::to_string() const
  1075. {
  1076. StringBuilder builder {};
  1077. bool first = true;
  1078. for (auto& filter_function : filter_value_list()) {
  1079. if (!first)
  1080. TRY(builder.try_append(' '));
  1081. TRY(filter_function.visit(
  1082. [&](Filter::Blur const& blur) -> ErrorOr<void> {
  1083. TRY(builder.try_append("blur("sv));
  1084. if (blur.radius.has_value())
  1085. TRY(builder.try_append(TRY(blur.radius->to_string())));
  1086. return {};
  1087. },
  1088. [&](Filter::DropShadow const& drop_shadow) -> ErrorOr<void> {
  1089. TRY(builder.try_appendff("drop-shadow({} {}"sv,
  1090. drop_shadow.offset_x, drop_shadow.offset_y));
  1091. if (drop_shadow.radius.has_value())
  1092. TRY(builder.try_appendff(" {}", TRY(drop_shadow.radius->to_string())));
  1093. if (drop_shadow.color.has_value()) {
  1094. TRY(builder.try_append(' '));
  1095. serialize_a_srgb_value(builder, *drop_shadow.color);
  1096. }
  1097. return {};
  1098. },
  1099. [&](Filter::HueRotate const& hue_rotate) -> ErrorOr<void> {
  1100. TRY(builder.try_append("hue-rotate("sv));
  1101. if (hue_rotate.angle.has_value()) {
  1102. TRY(hue_rotate.angle->visit(
  1103. [&](Angle const& angle) -> ErrorOr<void> {
  1104. return builder.try_append(TRY(angle.to_string()));
  1105. },
  1106. [&](auto&) -> ErrorOr<void> {
  1107. return builder.try_append('0');
  1108. }));
  1109. }
  1110. return {};
  1111. },
  1112. [&](Filter::Color const& color) -> ErrorOr<void> {
  1113. TRY(builder.try_appendff("{}(",
  1114. [&] {
  1115. switch (color.operation) {
  1116. case Filter::Color::Operation::Brightness:
  1117. return "brightness"sv;
  1118. case Filter::Color::Operation::Contrast:
  1119. return "contrast"sv;
  1120. case Filter::Color::Operation::Grayscale:
  1121. return "grayscale"sv;
  1122. case Filter::Color::Operation::Invert:
  1123. return "invert"sv;
  1124. case Filter::Color::Operation::Opacity:
  1125. return "opacity"sv;
  1126. case Filter::Color::Operation::Saturate:
  1127. return "saturate"sv;
  1128. case Filter::Color::Operation::Sepia:
  1129. return "sepia"sv;
  1130. default:
  1131. VERIFY_NOT_REACHED();
  1132. }
  1133. }()));
  1134. if (color.amount.has_value())
  1135. TRY(builder.try_append(TRY(color.amount->to_string())));
  1136. return {};
  1137. }));
  1138. TRY(builder.try_append(')'));
  1139. first = false;
  1140. }
  1141. return builder.to_string();
  1142. }
  1143. static bool operator==(Filter::Blur const& a, Filter::Blur const& b)
  1144. {
  1145. return a.radius == b.radius;
  1146. }
  1147. static bool operator==(Filter::DropShadow const& a, Filter::DropShadow const& b)
  1148. {
  1149. return a.offset_x == b.offset_x && a.offset_y == b.offset_y && a.radius == b.radius && a.color == b.color;
  1150. }
  1151. static bool operator==(Filter::HueRotate::Zero const&, Filter::HueRotate::Zero const&)
  1152. {
  1153. return true;
  1154. }
  1155. static bool operator==(Filter::Color const& a, Filter::Color const& b)
  1156. {
  1157. return a.operation == b.operation && a.amount == b.amount;
  1158. }
  1159. static bool operator==(Filter::HueRotate const& a, Filter::HueRotate const& b)
  1160. {
  1161. return a.angle == b.angle;
  1162. }
  1163. static bool variant_equals(auto const& a, auto const& b)
  1164. {
  1165. return a.visit([&](auto const& held_value) {
  1166. using HeldType = AK::Detail::Decay<decltype(held_value)>;
  1167. bool other_holds_same_type = b.template has<HeldType>();
  1168. return other_holds_same_type && held_value == b.template get<HeldType>();
  1169. });
  1170. }
  1171. static bool operator==(Filter::HueRotate::AngleOrZero const& a, Filter::HueRotate::AngleOrZero const& b)
  1172. {
  1173. return variant_equals(a, b);
  1174. }
  1175. static bool operator==(FilterFunction const& a, FilterFunction const& b)
  1176. {
  1177. return variant_equals(a, b);
  1178. }
  1179. bool FilterValueListStyleValue::equals(StyleValue const& other) const
  1180. {
  1181. if (type() != other.type())
  1182. return false;
  1183. auto const& typed_other = other.as_filter_value_list();
  1184. if (m_filter_value_list.size() != typed_other.m_filter_value_list.size())
  1185. return false;
  1186. for (size_t i = 0; i < m_filter_value_list.size(); i++) {
  1187. if (m_filter_value_list[i] != typed_other.m_filter_value_list[i])
  1188. return false;
  1189. }
  1190. return true;
  1191. }
  1192. ErrorOr<String> FlexStyleValue::to_string() const
  1193. {
  1194. return String::formatted("{} {} {}", TRY(m_grow->to_string()), TRY(m_shrink->to_string()), TRY(m_basis->to_string()));
  1195. }
  1196. bool FlexStyleValue::equals(StyleValue const& other) const
  1197. {
  1198. if (type() != other.type())
  1199. return false;
  1200. auto const& typed_other = other.as_flex();
  1201. return m_grow->equals(typed_other.m_grow)
  1202. && m_shrink->equals(typed_other.m_shrink)
  1203. && m_basis->equals(typed_other.m_basis);
  1204. }
  1205. ErrorOr<String> FlexFlowStyleValue::to_string() const
  1206. {
  1207. return String::formatted("{} {}", TRY(m_flex_direction->to_string()), TRY(m_flex_wrap->to_string()));
  1208. }
  1209. bool FlexFlowStyleValue::equals(StyleValue const& other) const
  1210. {
  1211. if (type() != other.type())
  1212. return false;
  1213. auto const& typed_other = other.as_flex_flow();
  1214. return m_flex_direction->equals(typed_other.m_flex_direction)
  1215. && m_flex_wrap->equals(typed_other.m_flex_wrap);
  1216. }
  1217. ErrorOr<String> FontStyleValue::to_string() const
  1218. {
  1219. return String::formatted("{} {} {} / {} {}", TRY(m_font_style->to_string()), TRY(m_font_weight->to_string()), TRY(m_font_size->to_string()), TRY(m_line_height->to_string()), TRY(m_font_families->to_string()));
  1220. }
  1221. bool FontStyleValue::equals(StyleValue const& other) const
  1222. {
  1223. if (type() != other.type())
  1224. return false;
  1225. auto const& typed_other = other.as_font();
  1226. return m_font_style->equals(typed_other.m_font_style)
  1227. && m_font_weight->equals(typed_other.m_font_weight)
  1228. && m_font_size->equals(typed_other.m_font_size)
  1229. && m_line_height->equals(typed_other.m_line_height)
  1230. && m_font_families->equals(typed_other.m_font_families);
  1231. }
  1232. bool FrequencyStyleValue::equals(StyleValue const& other) const
  1233. {
  1234. if (type() != other.type())
  1235. return false;
  1236. return m_frequency == other.as_frequency().m_frequency;
  1237. }
  1238. ErrorOr<String> GridTrackPlacementShorthandStyleValue::to_string() const
  1239. {
  1240. if (m_end->grid_track_placement().is_auto())
  1241. return String::formatted("{}", TRY(m_start->grid_track_placement().to_string()));
  1242. return String::formatted("{} / {}", TRY(m_start->grid_track_placement().to_string()), TRY(m_end->grid_track_placement().to_string()));
  1243. }
  1244. bool GridTrackPlacementShorthandStyleValue::equals(StyleValue const& other) const
  1245. {
  1246. if (type() != other.type())
  1247. return false;
  1248. auto const& typed_other = other.as_grid_track_placement_shorthand();
  1249. return m_start->equals(typed_other.m_start)
  1250. && m_end->equals(typed_other.m_end);
  1251. }
  1252. ErrorOr<String> GridAreaShorthandStyleValue::to_string() const
  1253. {
  1254. StringBuilder builder;
  1255. if (!m_row_start->as_grid_track_placement().grid_track_placement().is_auto())
  1256. TRY(builder.try_appendff("{}", TRY(m_row_start->as_grid_track_placement().grid_track_placement().to_string())));
  1257. if (!m_column_start->as_grid_track_placement().grid_track_placement().is_auto())
  1258. TRY(builder.try_appendff(" / {}", TRY(m_column_start->as_grid_track_placement().grid_track_placement().to_string())));
  1259. if (!m_row_end->as_grid_track_placement().grid_track_placement().is_auto())
  1260. TRY(builder.try_appendff(" / {}", TRY(m_row_end->as_grid_track_placement().grid_track_placement().to_string())));
  1261. if (!m_column_end->as_grid_track_placement().grid_track_placement().is_auto())
  1262. TRY(builder.try_appendff(" / {}", TRY(m_column_end->as_grid_track_placement().grid_track_placement().to_string())));
  1263. return builder.to_string();
  1264. }
  1265. bool GridAreaShorthandStyleValue::equals(StyleValue const& other) const
  1266. {
  1267. if (type() != other.type())
  1268. return false;
  1269. auto const& typed_other = other.as_grid_area_shorthand();
  1270. return m_row_start->equals(typed_other.m_row_start)
  1271. && m_column_start->equals(typed_other.m_column_start)
  1272. && m_row_end->equals(typed_other.m_row_end)
  1273. && m_column_end->equals(typed_other.m_column_end);
  1274. }
  1275. ErrorOr<String> GridTrackPlacementStyleValue::to_string() const
  1276. {
  1277. return m_grid_track_placement.to_string();
  1278. }
  1279. bool GridTrackPlacementStyleValue::equals(StyleValue const& other) const
  1280. {
  1281. if (type() != other.type())
  1282. return false;
  1283. auto const& typed_other = other.as_grid_track_placement();
  1284. return m_grid_track_placement == typed_other.grid_track_placement();
  1285. }
  1286. ErrorOr<String> GridTemplateAreaStyleValue::to_string() const
  1287. {
  1288. StringBuilder builder;
  1289. for (size_t y = 0; y < m_grid_template_area.size(); ++y) {
  1290. for (size_t x = 0; x < m_grid_template_area[y].size(); ++x) {
  1291. TRY(builder.try_appendff("{}", m_grid_template_area[y][x]));
  1292. if (x < m_grid_template_area[y].size() - 1)
  1293. TRY(builder.try_append(" "sv));
  1294. }
  1295. if (y < m_grid_template_area.size() - 1)
  1296. TRY(builder.try_append(", "sv));
  1297. }
  1298. return builder.to_string();
  1299. }
  1300. bool GridTemplateAreaStyleValue::equals(StyleValue const& other) const
  1301. {
  1302. if (type() != other.type())
  1303. return false;
  1304. auto const& typed_other = other.as_grid_template_area();
  1305. return m_grid_template_area == typed_other.grid_template_area();
  1306. }
  1307. ErrorOr<String> GridTrackSizeStyleValue::to_string() const
  1308. {
  1309. return m_grid_track_size_list.to_string();
  1310. }
  1311. bool GridTrackSizeStyleValue::equals(StyleValue const& other) const
  1312. {
  1313. if (type() != other.type())
  1314. return false;
  1315. auto const& typed_other = other.as_grid_track_size_list();
  1316. return m_grid_track_size_list == typed_other.grid_track_size_list();
  1317. }
  1318. ErrorOr<String> IdentifierStyleValue::to_string() const
  1319. {
  1320. return String::from_utf8(CSS::string_from_value_id(m_id));
  1321. }
  1322. bool IdentifierStyleValue::equals(StyleValue const& other) const
  1323. {
  1324. if (type() != other.type())
  1325. return false;
  1326. return m_id == other.as_identifier().m_id;
  1327. }
  1328. bool IdentifierStyleValue::has_color() const
  1329. {
  1330. switch (m_id) {
  1331. case ValueID::Currentcolor:
  1332. case ValueID::LibwebLink:
  1333. case ValueID::LibwebPaletteActiveLink:
  1334. case ValueID::LibwebPaletteActiveWindowBorder1:
  1335. case ValueID::LibwebPaletteActiveWindowBorder2:
  1336. case ValueID::LibwebPaletteActiveWindowTitle:
  1337. case ValueID::LibwebPaletteBase:
  1338. case ValueID::LibwebPaletteBaseText:
  1339. case ValueID::LibwebPaletteButton:
  1340. case ValueID::LibwebPaletteButtonText:
  1341. case ValueID::LibwebPaletteDesktopBackground:
  1342. case ValueID::LibwebPaletteFocusOutline:
  1343. case ValueID::LibwebPaletteHighlightWindowBorder1:
  1344. case ValueID::LibwebPaletteHighlightWindowBorder2:
  1345. case ValueID::LibwebPaletteHighlightWindowTitle:
  1346. case ValueID::LibwebPaletteHoverHighlight:
  1347. case ValueID::LibwebPaletteInactiveSelection:
  1348. case ValueID::LibwebPaletteInactiveSelectionText:
  1349. case ValueID::LibwebPaletteInactiveWindowBorder1:
  1350. case ValueID::LibwebPaletteInactiveWindowBorder2:
  1351. case ValueID::LibwebPaletteInactiveWindowTitle:
  1352. case ValueID::LibwebPaletteLink:
  1353. case ValueID::LibwebPaletteMenuBase:
  1354. case ValueID::LibwebPaletteMenuBaseText:
  1355. case ValueID::LibwebPaletteMenuSelection:
  1356. case ValueID::LibwebPaletteMenuSelectionText:
  1357. case ValueID::LibwebPaletteMenuStripe:
  1358. case ValueID::LibwebPaletteMovingWindowBorder1:
  1359. case ValueID::LibwebPaletteMovingWindowBorder2:
  1360. case ValueID::LibwebPaletteMovingWindowTitle:
  1361. case ValueID::LibwebPaletteRubberBandBorder:
  1362. case ValueID::LibwebPaletteRubberBandFill:
  1363. case ValueID::LibwebPaletteRuler:
  1364. case ValueID::LibwebPaletteRulerActiveText:
  1365. case ValueID::LibwebPaletteRulerBorder:
  1366. case ValueID::LibwebPaletteRulerInactiveText:
  1367. case ValueID::LibwebPaletteSelection:
  1368. case ValueID::LibwebPaletteSelectionText:
  1369. case ValueID::LibwebPaletteSyntaxComment:
  1370. case ValueID::LibwebPaletteSyntaxControlKeyword:
  1371. case ValueID::LibwebPaletteSyntaxIdentifier:
  1372. case ValueID::LibwebPaletteSyntaxKeyword:
  1373. case ValueID::LibwebPaletteSyntaxNumber:
  1374. case ValueID::LibwebPaletteSyntaxOperator:
  1375. case ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1376. case ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1377. case ValueID::LibwebPaletteSyntaxPunctuation:
  1378. case ValueID::LibwebPaletteSyntaxString:
  1379. case ValueID::LibwebPaletteSyntaxType:
  1380. case ValueID::LibwebPaletteTextCursor:
  1381. case ValueID::LibwebPaletteThreedHighlight:
  1382. case ValueID::LibwebPaletteThreedShadow1:
  1383. case ValueID::LibwebPaletteThreedShadow2:
  1384. case ValueID::LibwebPaletteVisitedLink:
  1385. case ValueID::LibwebPaletteWindow:
  1386. case ValueID::LibwebPaletteWindowText:
  1387. return true;
  1388. default:
  1389. return false;
  1390. }
  1391. }
  1392. Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
  1393. {
  1394. if (id() == CSS::ValueID::Currentcolor) {
  1395. if (!node.has_style())
  1396. return Color::Black;
  1397. return node.computed_values().color();
  1398. }
  1399. auto& document = node.document();
  1400. if (id() == CSS::ValueID::LibwebLink)
  1401. return document.link_color();
  1402. if (!document.page())
  1403. return {};
  1404. auto palette = document.page()->palette();
  1405. switch (id()) {
  1406. case CSS::ValueID::LibwebPaletteDesktopBackground:
  1407. return palette.color(ColorRole::DesktopBackground);
  1408. case CSS::ValueID::LibwebPaletteActiveWindowBorder1:
  1409. return palette.color(ColorRole::ActiveWindowBorder1);
  1410. case CSS::ValueID::LibwebPaletteActiveWindowBorder2:
  1411. return palette.color(ColorRole::ActiveWindowBorder2);
  1412. case CSS::ValueID::LibwebPaletteActiveWindowTitle:
  1413. return palette.color(ColorRole::ActiveWindowTitle);
  1414. case CSS::ValueID::LibwebPaletteInactiveWindowBorder1:
  1415. return palette.color(ColorRole::InactiveWindowBorder1);
  1416. case CSS::ValueID::LibwebPaletteInactiveWindowBorder2:
  1417. return palette.color(ColorRole::InactiveWindowBorder2);
  1418. case CSS::ValueID::LibwebPaletteInactiveWindowTitle:
  1419. return palette.color(ColorRole::InactiveWindowTitle);
  1420. case CSS::ValueID::LibwebPaletteMovingWindowBorder1:
  1421. return palette.color(ColorRole::MovingWindowBorder1);
  1422. case CSS::ValueID::LibwebPaletteMovingWindowBorder2:
  1423. return palette.color(ColorRole::MovingWindowBorder2);
  1424. case CSS::ValueID::LibwebPaletteMovingWindowTitle:
  1425. return palette.color(ColorRole::MovingWindowTitle);
  1426. case CSS::ValueID::LibwebPaletteHighlightWindowBorder1:
  1427. return palette.color(ColorRole::HighlightWindowBorder1);
  1428. case CSS::ValueID::LibwebPaletteHighlightWindowBorder2:
  1429. return palette.color(ColorRole::HighlightWindowBorder2);
  1430. case CSS::ValueID::LibwebPaletteHighlightWindowTitle:
  1431. return palette.color(ColorRole::HighlightWindowTitle);
  1432. case CSS::ValueID::LibwebPaletteMenuStripe:
  1433. return palette.color(ColorRole::MenuStripe);
  1434. case CSS::ValueID::LibwebPaletteMenuBase:
  1435. return palette.color(ColorRole::MenuBase);
  1436. case CSS::ValueID::LibwebPaletteMenuBaseText:
  1437. return palette.color(ColorRole::MenuBaseText);
  1438. case CSS::ValueID::LibwebPaletteMenuSelection:
  1439. return palette.color(ColorRole::MenuSelection);
  1440. case CSS::ValueID::LibwebPaletteMenuSelectionText:
  1441. return palette.color(ColorRole::MenuSelectionText);
  1442. case CSS::ValueID::LibwebPaletteWindow:
  1443. return palette.color(ColorRole::Window);
  1444. case CSS::ValueID::LibwebPaletteWindowText:
  1445. return palette.color(ColorRole::WindowText);
  1446. case CSS::ValueID::LibwebPaletteButton:
  1447. return palette.color(ColorRole::Button);
  1448. case CSS::ValueID::LibwebPaletteButtonText:
  1449. return palette.color(ColorRole::ButtonText);
  1450. case CSS::ValueID::LibwebPaletteBase:
  1451. return palette.color(ColorRole::Base);
  1452. case CSS::ValueID::LibwebPaletteBaseText:
  1453. return palette.color(ColorRole::BaseText);
  1454. case CSS::ValueID::LibwebPaletteThreedHighlight:
  1455. return palette.color(ColorRole::ThreedHighlight);
  1456. case CSS::ValueID::LibwebPaletteThreedShadow1:
  1457. return palette.color(ColorRole::ThreedShadow1);
  1458. case CSS::ValueID::LibwebPaletteThreedShadow2:
  1459. return palette.color(ColorRole::ThreedShadow2);
  1460. case CSS::ValueID::LibwebPaletteHoverHighlight:
  1461. return palette.color(ColorRole::HoverHighlight);
  1462. case CSS::ValueID::LibwebPaletteSelection:
  1463. return palette.color(ColorRole::Selection);
  1464. case CSS::ValueID::LibwebPaletteSelectionText:
  1465. return palette.color(ColorRole::SelectionText);
  1466. case CSS::ValueID::LibwebPaletteInactiveSelection:
  1467. return palette.color(ColorRole::InactiveSelection);
  1468. case CSS::ValueID::LibwebPaletteInactiveSelectionText:
  1469. return palette.color(ColorRole::InactiveSelectionText);
  1470. case CSS::ValueID::LibwebPaletteRubberBandFill:
  1471. return palette.color(ColorRole::RubberBandFill);
  1472. case CSS::ValueID::LibwebPaletteRubberBandBorder:
  1473. return palette.color(ColorRole::RubberBandBorder);
  1474. case CSS::ValueID::LibwebPaletteLink:
  1475. return palette.color(ColorRole::Link);
  1476. case CSS::ValueID::LibwebPaletteActiveLink:
  1477. return palette.color(ColorRole::ActiveLink);
  1478. case CSS::ValueID::LibwebPaletteVisitedLink:
  1479. return palette.color(ColorRole::VisitedLink);
  1480. case CSS::ValueID::LibwebPaletteRuler:
  1481. return palette.color(ColorRole::Ruler);
  1482. case CSS::ValueID::LibwebPaletteRulerBorder:
  1483. return palette.color(ColorRole::RulerBorder);
  1484. case CSS::ValueID::LibwebPaletteRulerActiveText:
  1485. return palette.color(ColorRole::RulerActiveText);
  1486. case CSS::ValueID::LibwebPaletteRulerInactiveText:
  1487. return palette.color(ColorRole::RulerInactiveText);
  1488. case CSS::ValueID::LibwebPaletteTextCursor:
  1489. return palette.color(ColorRole::TextCursor);
  1490. case CSS::ValueID::LibwebPaletteFocusOutline:
  1491. return palette.color(ColorRole::FocusOutline);
  1492. case CSS::ValueID::LibwebPaletteSyntaxComment:
  1493. return palette.color(ColorRole::SyntaxComment);
  1494. case CSS::ValueID::LibwebPaletteSyntaxNumber:
  1495. return palette.color(ColorRole::SyntaxNumber);
  1496. case CSS::ValueID::LibwebPaletteSyntaxString:
  1497. return palette.color(ColorRole::SyntaxString);
  1498. case CSS::ValueID::LibwebPaletteSyntaxType:
  1499. return palette.color(ColorRole::SyntaxType);
  1500. case CSS::ValueID::LibwebPaletteSyntaxPunctuation:
  1501. return palette.color(ColorRole::SyntaxPunctuation);
  1502. case CSS::ValueID::LibwebPaletteSyntaxOperator:
  1503. return palette.color(ColorRole::SyntaxOperator);
  1504. case CSS::ValueID::LibwebPaletteSyntaxKeyword:
  1505. return palette.color(ColorRole::SyntaxKeyword);
  1506. case CSS::ValueID::LibwebPaletteSyntaxControlKeyword:
  1507. return palette.color(ColorRole::SyntaxControlKeyword);
  1508. case CSS::ValueID::LibwebPaletteSyntaxIdentifier:
  1509. return palette.color(ColorRole::SyntaxIdentifier);
  1510. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorStatement:
  1511. return palette.color(ColorRole::SyntaxPreprocessorStatement);
  1512. case CSS::ValueID::LibwebPaletteSyntaxPreprocessorValue:
  1513. return palette.color(ColorRole::SyntaxPreprocessorValue);
  1514. default:
  1515. return {};
  1516. }
  1517. }
  1518. ImageStyleValue::ImageStyleValue(AK::URL const& url)
  1519. : AbstractImageStyleValue(Type::Image)
  1520. , m_url(url)
  1521. {
  1522. }
  1523. void ImageStyleValue::load_any_resources(DOM::Document& document)
  1524. {
  1525. if (resource())
  1526. return;
  1527. m_document = &document;
  1528. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  1529. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  1530. }
  1531. void ImageStyleValue::resource_did_load()
  1532. {
  1533. if (!m_document)
  1534. return;
  1535. // FIXME: Do less than a full repaint if possible?
  1536. if (m_document && m_document->browsing_context())
  1537. m_document->browsing_context()->set_needs_display();
  1538. if (resource()->is_animated() && resource()->frame_count() > 1) {
  1539. m_timer = Platform::Timer::create();
  1540. m_timer->set_interval(resource()->frame_duration(0));
  1541. m_timer->on_timeout = [this] { animate(); };
  1542. m_timer->start();
  1543. }
  1544. }
  1545. void ImageStyleValue::animate()
  1546. {
  1547. m_current_frame_index = (m_current_frame_index + 1) % resource()->frame_count();
  1548. auto current_frame_duration = resource()->frame_duration(m_current_frame_index);
  1549. if (current_frame_duration != m_timer->interval())
  1550. m_timer->restart(current_frame_duration);
  1551. if (m_current_frame_index == resource()->frame_count() - 1) {
  1552. ++m_loops_completed;
  1553. if (m_loops_completed > 0 && m_loops_completed == resource()->loop_count())
  1554. m_timer->stop();
  1555. }
  1556. if (on_animate)
  1557. on_animate();
  1558. }
  1559. Gfx::Bitmap const* ImageStyleValue::bitmap(size_t frame_index) const
  1560. {
  1561. if (!resource())
  1562. return nullptr;
  1563. return resource()->bitmap(frame_index);
  1564. }
  1565. ErrorOr<String> ImageStyleValue::to_string() const
  1566. {
  1567. return String::from_deprecated_string(serialize_a_url(m_url.to_deprecated_string()));
  1568. }
  1569. bool ImageStyleValue::equals(StyleValue const& other) const
  1570. {
  1571. if (type() != other.type())
  1572. return false;
  1573. return m_url == other.as_image().m_url;
  1574. }
  1575. Optional<CSSPixels> ImageStyleValue::natural_width() const
  1576. {
  1577. if (auto* b = bitmap(0); b != nullptr)
  1578. return b->width();
  1579. return {};
  1580. }
  1581. Optional<CSSPixels> ImageStyleValue::natural_height() const
  1582. {
  1583. if (auto* b = bitmap(0); b != nullptr)
  1584. return b->height();
  1585. return {};
  1586. }
  1587. void ImageStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering) const
  1588. {
  1589. if (auto* b = bitmap(m_current_frame_index); b != nullptr)
  1590. context.painter().draw_scaled_bitmap(dest_rect.to_type<int>(), *b, bitmap(0)->rect(), 1.0f, to_gfx_scaling_mode(image_rendering));
  1591. }
  1592. static ErrorOr<void> serialize_color_stop_list(StringBuilder& builder, auto const& color_stop_list)
  1593. {
  1594. bool first = true;
  1595. for (auto const& element : color_stop_list) {
  1596. if (!first)
  1597. TRY(builder.try_append(", "sv));
  1598. if (element.transition_hint.has_value())
  1599. TRY(builder.try_appendff("{}, "sv, TRY(element.transition_hint->value.to_string())));
  1600. serialize_a_srgb_value(builder, element.color_stop.color);
  1601. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  1602. if (position->has_value())
  1603. TRY(builder.try_appendff(" {}"sv, TRY((*position)->to_string())));
  1604. }
  1605. first = false;
  1606. }
  1607. return {};
  1608. }
  1609. ErrorOr<String> LinearGradientStyleValue::to_string() const
  1610. {
  1611. StringBuilder builder;
  1612. auto side_or_corner_to_string = [](SideOrCorner value) {
  1613. switch (value) {
  1614. case SideOrCorner::Top:
  1615. return "top"sv;
  1616. case SideOrCorner::Bottom:
  1617. return "bottom"sv;
  1618. case SideOrCorner::Left:
  1619. return "left"sv;
  1620. case SideOrCorner::Right:
  1621. return "right"sv;
  1622. case SideOrCorner::TopLeft:
  1623. return "top left"sv;
  1624. case SideOrCorner::TopRight:
  1625. return "top right"sv;
  1626. case SideOrCorner::BottomLeft:
  1627. return "bottom left"sv;
  1628. case SideOrCorner::BottomRight:
  1629. return "bottom right"sv;
  1630. default:
  1631. VERIFY_NOT_REACHED();
  1632. }
  1633. };
  1634. if (m_gradient_type == GradientType::WebKit)
  1635. TRY(builder.try_append("-webkit-"sv));
  1636. if (is_repeating())
  1637. TRY(builder.try_append("repeating-"sv));
  1638. TRY(builder.try_append("linear-gradient("sv));
  1639. TRY(m_direction.visit(
  1640. [&](SideOrCorner side_or_corner) -> ErrorOr<void> {
  1641. return builder.try_appendff("{}{}, "sv, m_gradient_type == GradientType::Standard ? "to "sv : ""sv, side_or_corner_to_string(side_or_corner));
  1642. },
  1643. [&](Angle const& angle) -> ErrorOr<void> {
  1644. return builder.try_appendff("{}, "sv, TRY(angle.to_string()));
  1645. }));
  1646. TRY(serialize_color_stop_list(builder, m_color_stop_list));
  1647. TRY(builder.try_append(")"sv));
  1648. return builder.to_string();
  1649. }
  1650. static bool operator==(LinearGradientStyleValue::GradientDirection const& a, LinearGradientStyleValue::GradientDirection const& b)
  1651. {
  1652. if (a.has<SideOrCorner>() && b.has<SideOrCorner>())
  1653. return a.get<SideOrCorner>() == b.get<SideOrCorner>();
  1654. if (a.has<Angle>() && b.has<Angle>())
  1655. return a.get<Angle>() == b.get<Angle>();
  1656. return false;
  1657. }
  1658. static bool operator==(EdgeRect const& a, EdgeRect const& b)
  1659. {
  1660. 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;
  1661. }
  1662. bool LinearGradientStyleValue::equals(StyleValue const& other_) const
  1663. {
  1664. if (type() != other_.type())
  1665. return false;
  1666. auto& other = other_.as_linear_gradient();
  1667. return (m_gradient_type == other.m_gradient_type
  1668. && m_repeating == other.m_repeating
  1669. && m_direction == other.m_direction
  1670. && m_color_stop_list == other.m_color_stop_list);
  1671. }
  1672. float LinearGradientStyleValue::angle_degrees(CSSPixelSize gradient_size) const
  1673. {
  1674. auto corner_angle_degrees = [&] {
  1675. return static_cast<float>(atan2(gradient_size.height().value(), gradient_size.width().value())) * 180 / AK::Pi<float>;
  1676. };
  1677. return m_direction.visit(
  1678. [&](SideOrCorner side_or_corner) {
  1679. auto angle = [&] {
  1680. switch (side_or_corner) {
  1681. case SideOrCorner::Top:
  1682. return 0.0f;
  1683. case SideOrCorner::Bottom:
  1684. return 180.0f;
  1685. case SideOrCorner::Left:
  1686. return 270.0f;
  1687. case SideOrCorner::Right:
  1688. return 90.0f;
  1689. case SideOrCorner::TopRight:
  1690. return corner_angle_degrees();
  1691. case SideOrCorner::BottomLeft:
  1692. return corner_angle_degrees() + 180.0f;
  1693. case SideOrCorner::TopLeft:
  1694. return -corner_angle_degrees();
  1695. case SideOrCorner::BottomRight:
  1696. return -(corner_angle_degrees() + 180.0f);
  1697. default:
  1698. VERIFY_NOT_REACHED();
  1699. }
  1700. }();
  1701. // Note: For unknowable reasons the angles are opposite on the -webkit- version
  1702. if (m_gradient_type == GradientType::WebKit)
  1703. return angle + 180.0f;
  1704. return angle;
  1705. },
  1706. [&](Angle const& angle) {
  1707. return angle.to_degrees();
  1708. });
  1709. }
  1710. void LinearGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const
  1711. {
  1712. if (m_resolved.has_value() && m_resolved->size == size)
  1713. return;
  1714. m_resolved = ResolvedData { Painting::resolve_linear_gradient_data(node, size, *this), size };
  1715. }
  1716. void LinearGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  1717. {
  1718. VERIFY(m_resolved.has_value());
  1719. Painting::paint_linear_gradient(context, dest_rect, m_resolved->data);
  1720. }
  1721. CSSPixelPoint PositionValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const
  1722. {
  1723. // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense)
  1724. CSSPixels x = horizontal_position.visit(
  1725. [&](HorizontalPreset preset) -> CSSPixels {
  1726. return rect.width() * [&] {
  1727. switch (preset) {
  1728. case HorizontalPreset::Left:
  1729. return 0.0f;
  1730. case HorizontalPreset::Center:
  1731. return 0.5f;
  1732. case HorizontalPreset::Right:
  1733. return 1.0f;
  1734. default:
  1735. VERIFY_NOT_REACHED();
  1736. }
  1737. }();
  1738. },
  1739. [&](LengthPercentage length_percentage) -> CSSPixels {
  1740. return length_percentage.resolved(node, Length::make_px(rect.width())).to_px(node);
  1741. });
  1742. CSSPixels y = vertical_position.visit(
  1743. [&](VerticalPreset preset) -> CSSPixels {
  1744. return rect.height() * [&] {
  1745. switch (preset) {
  1746. case VerticalPreset::Top:
  1747. return 0.0f;
  1748. case VerticalPreset::Center:
  1749. return 0.5f;
  1750. case VerticalPreset::Bottom:
  1751. return 1.0f;
  1752. default:
  1753. VERIFY_NOT_REACHED();
  1754. }
  1755. }();
  1756. },
  1757. [&](LengthPercentage length_percentage) -> CSSPixels {
  1758. return length_percentage.resolved(node, Length::make_px(rect.height())).to_px(node);
  1759. });
  1760. if (x_relative_to == HorizontalEdge::Right)
  1761. x = rect.width() - x;
  1762. if (y_relative_to == VerticalEdge::Bottom)
  1763. y = rect.height() - y;
  1764. return CSSPixelPoint { rect.x() + x, rect.y() + y };
  1765. }
  1766. ErrorOr<void> PositionValue::serialize(StringBuilder& builder) const
  1767. {
  1768. // Note: This means our serialization with simplify any with explicit edges that are just `top left`.
  1769. bool has_relative_edges = x_relative_to == HorizontalEdge::Right || y_relative_to == VerticalEdge::Bottom;
  1770. if (has_relative_edges)
  1771. TRY(builder.try_append(x_relative_to == HorizontalEdge::Left ? "left "sv : "right "sv));
  1772. TRY(horizontal_position.visit(
  1773. [&](HorizontalPreset preset) -> ErrorOr<void> {
  1774. return builder.try_append([&] {
  1775. switch (preset) {
  1776. case HorizontalPreset::Left:
  1777. return "left"sv;
  1778. case HorizontalPreset::Center:
  1779. return "center"sv;
  1780. case HorizontalPreset::Right:
  1781. return "right"sv;
  1782. default:
  1783. VERIFY_NOT_REACHED();
  1784. }
  1785. }());
  1786. },
  1787. [&](LengthPercentage length_percentage) -> ErrorOr<void> {
  1788. return builder.try_appendff(TRY(length_percentage.to_string()));
  1789. }));
  1790. TRY(builder.try_append(' '));
  1791. if (has_relative_edges)
  1792. TRY(builder.try_append(y_relative_to == VerticalEdge::Top ? "top "sv : "bottom "sv));
  1793. TRY(vertical_position.visit(
  1794. [&](VerticalPreset preset) -> ErrorOr<void> {
  1795. return builder.try_append([&] {
  1796. switch (preset) {
  1797. case VerticalPreset::Top:
  1798. return "top"sv;
  1799. case VerticalPreset::Center:
  1800. return "center"sv;
  1801. case VerticalPreset::Bottom:
  1802. return "bottom"sv;
  1803. default:
  1804. VERIFY_NOT_REACHED();
  1805. }
  1806. }());
  1807. },
  1808. [&](LengthPercentage length_percentage) -> ErrorOr<void> {
  1809. return builder.try_append(TRY(length_percentage.to_string()));
  1810. }));
  1811. return {};
  1812. }
  1813. bool PositionValue::operator==(PositionValue const& other) const
  1814. {
  1815. return (
  1816. x_relative_to == other.x_relative_to
  1817. && y_relative_to == other.y_relative_to
  1818. && variant_equals(horizontal_position, other.horizontal_position)
  1819. && variant_equals(vertical_position, other.vertical_position));
  1820. }
  1821. ErrorOr<String> RadialGradientStyleValue::to_string() const
  1822. {
  1823. StringBuilder builder;
  1824. if (is_repeating())
  1825. TRY(builder.try_append("repeating-"sv));
  1826. TRY(builder.try_appendff("radial-gradient({} "sv,
  1827. m_ending_shape == EndingShape::Circle ? "circle"sv : "ellipse"sv));
  1828. TRY(m_size.visit(
  1829. [&](Extent extent) -> ErrorOr<void> {
  1830. return builder.try_append([&] {
  1831. switch (extent) {
  1832. case Extent::ClosestCorner:
  1833. return "closest-corner"sv;
  1834. case Extent::ClosestSide:
  1835. return "closest-side"sv;
  1836. case Extent::FarthestCorner:
  1837. return "farthest-corner"sv;
  1838. case Extent::FarthestSide:
  1839. return "farthest-side"sv;
  1840. default:
  1841. VERIFY_NOT_REACHED();
  1842. }
  1843. }());
  1844. },
  1845. [&](CircleSize const& circle_size) -> ErrorOr<void> {
  1846. return builder.try_append(TRY(circle_size.radius.to_string()));
  1847. },
  1848. [&](EllipseSize const& ellipse_size) -> ErrorOr<void> {
  1849. return builder.try_appendff("{} {}", TRY(ellipse_size.radius_a.to_string()), TRY(ellipse_size.radius_b.to_string()));
  1850. }));
  1851. if (m_position != PositionValue::center()) {
  1852. TRY(builder.try_appendff(" at "sv));
  1853. TRY(m_position.serialize(builder));
  1854. }
  1855. TRY(builder.try_append(", "sv));
  1856. TRY(serialize_color_stop_list(builder, m_color_stop_list));
  1857. TRY(builder.try_append(')'));
  1858. return builder.to_string();
  1859. }
  1860. Gfx::FloatSize RadialGradientStyleValue::resolve_size(Layout::Node const& node, Gfx::FloatPoint center, Gfx::FloatRect const& size) const
  1861. {
  1862. auto const side_shape = [&](auto distance_function) {
  1863. auto const distance_from = [&](float v, float a, float b, auto distance_function) {
  1864. return distance_function(fabs(a - v), fabs(b - v));
  1865. };
  1866. auto x_dist = distance_from(center.x(), size.left(), size.right(), distance_function);
  1867. auto y_dist = distance_from(center.y(), size.top(), size.bottom(), distance_function);
  1868. if (m_ending_shape == EndingShape::Circle) {
  1869. auto dist = distance_function(x_dist, y_dist);
  1870. return Gfx::FloatSize { dist, dist };
  1871. } else {
  1872. return Gfx::FloatSize { x_dist, y_dist };
  1873. }
  1874. };
  1875. auto const closest_side_shape = [&] {
  1876. return side_shape(AK::min<float>);
  1877. };
  1878. auto const farthest_side_shape = [&] {
  1879. return side_shape(AK::max<float>);
  1880. };
  1881. auto const corner_distance = [&](auto distance_compare, Gfx::FloatPoint& corner) {
  1882. auto top_left_distance = size.top_left().distance_from(center);
  1883. auto top_right_distance = size.top_right().distance_from(center);
  1884. auto bottom_right_distance = size.bottom_right().distance_from(center);
  1885. auto bottom_left_distance = size.bottom_left().distance_from(center);
  1886. auto distance = top_left_distance;
  1887. if (distance_compare(top_right_distance, distance)) {
  1888. corner = size.top_right();
  1889. distance = top_right_distance;
  1890. }
  1891. if (distance_compare(bottom_right_distance, distance)) {
  1892. corner = size.top_right();
  1893. distance = bottom_right_distance;
  1894. }
  1895. if (distance_compare(bottom_left_distance, distance)) {
  1896. corner = size.top_right();
  1897. distance = bottom_left_distance;
  1898. }
  1899. return distance;
  1900. };
  1901. auto const closest_corner_distance = [&](Gfx::FloatPoint& corner) {
  1902. return corner_distance([](float a, float b) { return a < b; }, corner);
  1903. };
  1904. auto const farthest_corner_distance = [&](Gfx::FloatPoint& corner) {
  1905. return corner_distance([](float a, float b) { return a > b; }, corner);
  1906. };
  1907. auto const corner_shape = [&](auto corner_distance, auto get_shape) {
  1908. Gfx::FloatPoint corner {};
  1909. auto distance = corner_distance(corner);
  1910. if (m_ending_shape == EndingShape::Ellipse) {
  1911. auto shape = get_shape();
  1912. auto aspect_ratio = shape.width() / shape.height();
  1913. auto p = corner - center;
  1914. auto radius_a = AK::sqrt(p.y() * p.y() * aspect_ratio * aspect_ratio + p.x() * p.x());
  1915. auto radius_b = radius_a / aspect_ratio;
  1916. return Gfx::FloatSize { radius_a, radius_b };
  1917. }
  1918. return Gfx::FloatSize { distance, distance };
  1919. };
  1920. // https://w3c.github.io/csswg-drafts/css-images/#radial-gradient-syntax
  1921. auto resolved_size = m_size.visit(
  1922. [&](Extent extent) {
  1923. switch (extent) {
  1924. case Extent::ClosestSide:
  1925. // The ending shape is sized so that it exactly meets the side of the gradient box closest to the gradient’s center.
  1926. // If the shape is an ellipse, it exactly meets the closest side in each dimension.
  1927. return closest_side_shape();
  1928. case Extent::ClosestCorner:
  1929. // The ending shape is sized so that it passes through the corner of the gradient box closest to the gradient’s center.
  1930. // If the shape is an ellipse, the ending shape is given the same aspect-ratio it would have if closest-side were specified
  1931. return corner_shape(closest_corner_distance, closest_side_shape);
  1932. case Extent::FarthestCorner:
  1933. // Same as closest-corner, except the ending shape is sized based on the farthest corner.
  1934. // If the shape is an ellipse, the ending shape is given the same aspect ratio it would have if farthest-side were specified.
  1935. return corner_shape(farthest_corner_distance, farthest_side_shape);
  1936. case Extent::FarthestSide:
  1937. // Same as closest-side, except the ending shape is sized based on the farthest side(s).
  1938. return farthest_side_shape();
  1939. default:
  1940. VERIFY_NOT_REACHED();
  1941. }
  1942. },
  1943. [&](CircleSize const& circle_size) {
  1944. auto radius = circle_size.radius.to_px(node);
  1945. return Gfx::FloatSize { radius, radius };
  1946. },
  1947. [&](EllipseSize const& ellipse_size) {
  1948. auto radius_a = ellipse_size.radius_a.resolved(node, CSS::Length::make_px(size.width())).to_px(node);
  1949. auto radius_b = ellipse_size.radius_b.resolved(node, CSS::Length::make_px(size.height())).to_px(node);
  1950. return Gfx::FloatSize { radius_a, radius_b };
  1951. });
  1952. // Handle degenerate cases
  1953. // https://w3c.github.io/csswg-drafts/css-images/#degenerate-radials
  1954. constexpr auto arbitrary_small_number = 1e-10;
  1955. constexpr auto arbitrary_large_number = 1e10;
  1956. // If the ending shape is a circle with zero radius:
  1957. if (m_ending_shape == EndingShape::Circle && resolved_size.is_empty()) {
  1958. // Render as if the ending shape was a circle whose radius was an arbitrary very small number greater than zero.
  1959. // This will make the gradient continue to look like a circle.
  1960. return Gfx::FloatSize { arbitrary_small_number, arbitrary_small_number };
  1961. }
  1962. // If the ending shape has zero width (regardless of the height):
  1963. if (resolved_size.width() <= 0) {
  1964. // Render as if the ending shape was an ellipse whose height was an arbitrary very large number
  1965. // and whose width was an arbitrary very small number greater than zero.
  1966. // This will make the gradient look similar to a horizontal linear gradient that is mirrored across the center of the ellipse.
  1967. // It also means that all color-stop positions specified with a percentage resolve to 0px.
  1968. return Gfx::FloatSize { arbitrary_small_number, arbitrary_large_number };
  1969. }
  1970. // Otherwise, if the ending shape has zero height:
  1971. if (resolved_size.height() <= 0) {
  1972. // Render as if the ending shape was an ellipse whose width was an arbitrary very large number and whose height
  1973. // was an arbitrary very small number greater than zero. This will make the gradient look like a solid-color image equal
  1974. // to the color of the last color-stop, or equal to the average color of the gradient if it’s repeating.
  1975. return Gfx::FloatSize { arbitrary_large_number, arbitrary_small_number };
  1976. }
  1977. return resolved_size;
  1978. }
  1979. void RadialGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize paint_size) const
  1980. {
  1981. CSSPixelRect gradient_box { { 0, 0 }, paint_size };
  1982. auto center = m_position.resolved(node, gradient_box).to_type<float>();
  1983. auto gradient_size = resolve_size(node, center, gradient_box.to_type<float>());
  1984. if (m_resolved.has_value() && m_resolved->gradient_size == gradient_size)
  1985. return;
  1986. m_resolved = ResolvedData {
  1987. Painting::resolve_radial_gradient_data(node, gradient_size.to_type<CSSPixels>(), *this),
  1988. gradient_size,
  1989. center,
  1990. };
  1991. }
  1992. bool RadialGradientStyleValue::equals(StyleValue const& other) const
  1993. {
  1994. if (type() != other.type())
  1995. return false;
  1996. auto& other_gradient = other.as_radial_gradient();
  1997. return (m_ending_shape == other_gradient.m_ending_shape
  1998. && variant_equals(m_size, other_gradient.m_size)
  1999. && m_position == other_gradient.m_position
  2000. && m_color_stop_list == other_gradient.m_color_stop_list);
  2001. }
  2002. void RadialGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  2003. {
  2004. VERIFY(m_resolved.has_value());
  2005. Painting::paint_radial_gradient(context, dest_rect, m_resolved->data,
  2006. context.rounded_device_point(m_resolved->center.to_type<CSSPixels>()),
  2007. context.rounded_device_size(m_resolved->gradient_size.to_type<CSSPixels>()));
  2008. }
  2009. ErrorOr<String> ConicGradientStyleValue::to_string() const
  2010. {
  2011. StringBuilder builder;
  2012. if (is_repeating())
  2013. TRY(builder.try_append("repeating-"sv));
  2014. TRY(builder.try_append("conic-gradient("sv));
  2015. bool has_from_angle = false;
  2016. bool has_at_position = false;
  2017. if ((has_from_angle = m_from_angle.to_degrees() != 0))
  2018. TRY(builder.try_appendff("from {}", TRY(m_from_angle.to_string())));
  2019. if ((has_at_position = m_position != PositionValue::center())) {
  2020. if (has_from_angle)
  2021. TRY(builder.try_append(' '));
  2022. TRY(builder.try_appendff("at "sv));
  2023. TRY(m_position.serialize(builder));
  2024. }
  2025. if (has_from_angle || has_at_position)
  2026. TRY(builder.try_append(", "sv));
  2027. TRY(serialize_color_stop_list(builder, m_color_stop_list));
  2028. TRY(builder.try_append(')'));
  2029. return builder.to_string();
  2030. }
  2031. void ConicGradientStyleValue::resolve_for_size(Layout::Node const& node, CSSPixelSize size) const
  2032. {
  2033. if (!m_resolved.has_value())
  2034. m_resolved = ResolvedData { Painting::resolve_conic_gradient_data(node, *this), {} };
  2035. m_resolved->position = m_position.resolved(node, CSSPixelRect { { 0, 0 }, size });
  2036. }
  2037. void ConicGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  2038. {
  2039. VERIFY(m_resolved.has_value());
  2040. Painting::paint_conic_gradient(context, dest_rect, m_resolved->data, context.rounded_device_point(m_resolved->position));
  2041. }
  2042. bool ConicGradientStyleValue::equals(StyleValue const& other) const
  2043. {
  2044. if (type() != other.type())
  2045. return false;
  2046. auto& other_gradient = other.as_conic_gradient();
  2047. return (m_from_angle == other_gradient.m_from_angle
  2048. && m_position == other_gradient.m_position
  2049. && m_color_stop_list == other_gradient.m_color_stop_list
  2050. && m_repeating == other_gradient.m_repeating);
  2051. }
  2052. float ConicGradientStyleValue::angle_degrees() const
  2053. {
  2054. return m_from_angle.to_degrees();
  2055. }
  2056. bool InheritStyleValue::equals(StyleValue const& other) const
  2057. {
  2058. return type() == other.type();
  2059. }
  2060. bool InitialStyleValue::equals(StyleValue const& other) const
  2061. {
  2062. return type() == other.type();
  2063. }
  2064. bool LengthStyleValue::equals(StyleValue const& other) const
  2065. {
  2066. if (type() != other.type())
  2067. return false;
  2068. return m_length == other.as_length().m_length;
  2069. }
  2070. ErrorOr<String> ListStyleStyleValue::to_string() const
  2071. {
  2072. return String::formatted("{} {} {}", TRY(m_position->to_string()), TRY(m_image->to_string()), TRY(m_style_type->to_string()));
  2073. }
  2074. bool ListStyleStyleValue::equals(StyleValue const& other) const
  2075. {
  2076. if (type() != other.type())
  2077. return false;
  2078. auto const& typed_other = other.as_list_style();
  2079. return m_position->equals(typed_other.m_position)
  2080. && m_image->equals(typed_other.m_image)
  2081. && m_style_type->equals(typed_other.m_style_type);
  2082. }
  2083. ErrorOr<String> NumericStyleValue::to_string() const
  2084. {
  2085. return m_value.visit(
  2086. [](float value) {
  2087. return String::formatted("{}", value);
  2088. },
  2089. [](i64 value) {
  2090. return String::formatted("{}", value);
  2091. });
  2092. }
  2093. bool NumericStyleValue::equals(StyleValue const& other) const
  2094. {
  2095. if (type() != other.type())
  2096. return false;
  2097. if (has_integer() != other.has_integer())
  2098. return false;
  2099. if (has_integer())
  2100. return m_value.get<i64>() == other.as_numeric().m_value.get<i64>();
  2101. return m_value.get<float>() == other.as_numeric().m_value.get<float>();
  2102. }
  2103. ErrorOr<String> OverflowStyleValue::to_string() const
  2104. {
  2105. return String::formatted("{} {}", TRY(m_overflow_x->to_string()), TRY(m_overflow_y->to_string()));
  2106. }
  2107. bool OverflowStyleValue::equals(StyleValue const& other) const
  2108. {
  2109. if (type() != other.type())
  2110. return false;
  2111. auto const& typed_other = other.as_overflow();
  2112. return m_overflow_x->equals(typed_other.m_overflow_x)
  2113. && m_overflow_y->equals(typed_other.m_overflow_y);
  2114. }
  2115. ErrorOr<String> PercentageStyleValue::to_string() const
  2116. {
  2117. return m_percentage.to_string();
  2118. }
  2119. bool PercentageStyleValue::equals(StyleValue const& other) const
  2120. {
  2121. if (type() != other.type())
  2122. return false;
  2123. return m_percentage == other.as_percentage().m_percentage;
  2124. }
  2125. ErrorOr<String> PositionStyleValue::to_string() const
  2126. {
  2127. auto to_string = [](PositionEdge edge) {
  2128. switch (edge) {
  2129. case PositionEdge::Left:
  2130. return "left";
  2131. case PositionEdge::Right:
  2132. return "right";
  2133. case PositionEdge::Top:
  2134. return "top";
  2135. case PositionEdge::Bottom:
  2136. return "bottom";
  2137. }
  2138. VERIFY_NOT_REACHED();
  2139. };
  2140. return String::formatted("{} {} {} {}", to_string(m_edge_x), TRY(m_offset_x.to_string()), to_string(m_edge_y), TRY(m_offset_y.to_string()));
  2141. }
  2142. bool PositionStyleValue::equals(StyleValue const& other) const
  2143. {
  2144. if (type() != other.type())
  2145. return false;
  2146. auto const& typed_other = other.as_position();
  2147. return m_edge_x == typed_other.m_edge_x
  2148. && m_offset_x == typed_other.m_offset_x
  2149. && m_edge_y == typed_other.m_edge_y
  2150. && m_offset_y == typed_other.m_offset_y;
  2151. }
  2152. ErrorOr<String> RectStyleValue::to_string() const
  2153. {
  2154. return String::formatted("rect({} {} {} {})", m_rect.top_edge, m_rect.right_edge, m_rect.bottom_edge, m_rect.left_edge);
  2155. }
  2156. bool RectStyleValue::equals(StyleValue const& other) const
  2157. {
  2158. if (type() != other.type())
  2159. return false;
  2160. auto const& typed_other = other.as_rect();
  2161. return m_rect == typed_other.rect();
  2162. }
  2163. bool ResolutionStyleValue::equals(StyleValue const& other) const
  2164. {
  2165. if (type() != other.type())
  2166. return false;
  2167. return m_resolution == other.as_resolution().m_resolution;
  2168. }
  2169. ErrorOr<String> ShadowStyleValue::to_string() const
  2170. {
  2171. StringBuilder builder;
  2172. TRY(builder.try_appendff("{} {} {} {} {}", m_color.to_deprecated_string(), TRY(m_offset_x.to_string()), TRY(m_offset_y.to_string()), TRY(m_blur_radius.to_string()), TRY(m_spread_distance.to_string())));
  2173. if (m_placement == ShadowPlacement::Inner)
  2174. TRY(builder.try_append(" inset"sv));
  2175. return builder.to_string();
  2176. }
  2177. bool ShadowStyleValue::equals(StyleValue const& other) const
  2178. {
  2179. if (type() != other.type())
  2180. return false;
  2181. auto const& typed_other = other.as_shadow();
  2182. return m_color == typed_other.m_color
  2183. && m_offset_x == typed_other.m_offset_x
  2184. && m_offset_y == typed_other.m_offset_y
  2185. && m_blur_radius == typed_other.m_blur_radius
  2186. && m_spread_distance == typed_other.m_spread_distance
  2187. && m_placement == typed_other.m_placement;
  2188. }
  2189. bool StringStyleValue::equals(StyleValue const& other) const
  2190. {
  2191. if (type() != other.type())
  2192. return false;
  2193. return m_string == other.as_string().m_string;
  2194. }
  2195. ErrorOr<String> TextDecorationStyleValue::to_string() const
  2196. {
  2197. return String::formatted("{} {} {} {}", TRY(m_line->to_string()), TRY(m_thickness->to_string()), TRY(m_style->to_string()), TRY(m_color->to_string()));
  2198. }
  2199. bool TextDecorationStyleValue::equals(StyleValue const& other) const
  2200. {
  2201. if (type() != other.type())
  2202. return false;
  2203. auto const& typed_other = other.as_text_decoration();
  2204. return m_line->equals(typed_other.m_line)
  2205. && m_thickness->equals(typed_other.m_thickness)
  2206. && m_style->equals(typed_other.m_style)
  2207. && m_color->equals(typed_other.m_color);
  2208. }
  2209. bool TimeStyleValue::equals(StyleValue const& other) const
  2210. {
  2211. if (type() != other.type())
  2212. return false;
  2213. return m_time == other.as_time().m_time;
  2214. }
  2215. ErrorOr<String> TransformationStyleValue::to_string() const
  2216. {
  2217. StringBuilder builder;
  2218. TRY(builder.try_append(CSS::to_string(m_transform_function)));
  2219. TRY(builder.try_append('('));
  2220. TRY(builder.try_join(", "sv, m_values));
  2221. TRY(builder.try_append(')'));
  2222. return builder.to_string();
  2223. }
  2224. bool TransformationStyleValue::equals(StyleValue const& other) const
  2225. {
  2226. if (type() != other.type())
  2227. return false;
  2228. auto const& typed_other = other.as_transformation();
  2229. if (m_transform_function != typed_other.m_transform_function)
  2230. return false;
  2231. if (m_values.size() != typed_other.m_values.size())
  2232. return false;
  2233. for (size_t i = 0; i < m_values.size(); ++i) {
  2234. if (!m_values[i].equals(typed_other.m_values[i]))
  2235. return false;
  2236. }
  2237. return true;
  2238. }
  2239. ErrorOr<String> UnresolvedStyleValue::to_string() const
  2240. {
  2241. StringBuilder builder;
  2242. for (auto& value : m_values)
  2243. TRY(builder.try_append(value.to_deprecated_string()));
  2244. return builder.to_string();
  2245. }
  2246. bool UnresolvedStyleValue::equals(StyleValue const& other) const
  2247. {
  2248. if (type() != other.type())
  2249. return false;
  2250. // This is a case where comparing the strings actually makes sense.
  2251. return to_string().release_value_but_fixme_should_propagate_errors() == other.to_string().release_value_but_fixme_should_propagate_errors();
  2252. }
  2253. bool UnsetStyleValue::equals(StyleValue const& other) const
  2254. {
  2255. return type() == other.type();
  2256. }
  2257. ErrorOr<String> StyleValueList::to_string() const
  2258. {
  2259. auto separator = ""sv;
  2260. switch (m_separator) {
  2261. case Separator::Space:
  2262. separator = " "sv;
  2263. break;
  2264. case Separator::Comma:
  2265. separator = ", "sv;
  2266. break;
  2267. default:
  2268. VERIFY_NOT_REACHED();
  2269. }
  2270. return String::from_deprecated_string(DeprecatedString::join(separator, m_values));
  2271. }
  2272. bool StyleValueList::equals(StyleValue const& other) const
  2273. {
  2274. if (type() != other.type())
  2275. return false;
  2276. auto const& typed_other = other.as_value_list();
  2277. if (m_separator != typed_other.m_separator)
  2278. return false;
  2279. if (m_values.size() != typed_other.m_values.size())
  2280. return false;
  2281. for (size_t i = 0; i < m_values.size(); ++i) {
  2282. if (!m_values[i].equals(typed_other.m_values[i]))
  2283. return false;
  2284. }
  2285. return true;
  2286. }
  2287. NonnullRefPtr<ColorStyleValue> ColorStyleValue::create(Color color)
  2288. {
  2289. if (color.value() == 0) {
  2290. static auto transparent = adopt_ref(*new ColorStyleValue(color));
  2291. return transparent;
  2292. }
  2293. if (color == Color::from_rgb(0x000000)) {
  2294. static auto black = adopt_ref(*new ColorStyleValue(color));
  2295. return black;
  2296. }
  2297. if (color == Color::from_rgb(0xffffff)) {
  2298. static auto white = adopt_ref(*new ColorStyleValue(color));
  2299. return white;
  2300. }
  2301. return adopt_ref(*new ColorStyleValue(color));
  2302. }
  2303. NonnullRefPtr<GridTemplateAreaStyleValue> GridTemplateAreaStyleValue::create(Vector<Vector<String>> grid_template_area)
  2304. {
  2305. return adopt_ref(*new GridTemplateAreaStyleValue(grid_template_area));
  2306. }
  2307. NonnullRefPtr<GridTrackPlacementStyleValue> GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement grid_track_placement)
  2308. {
  2309. return adopt_ref(*new GridTrackPlacementStyleValue(grid_track_placement));
  2310. }
  2311. NonnullRefPtr<GridTrackSizeStyleValue> GridTrackSizeStyleValue::create(CSS::GridTrackSizeList grid_track_size_list)
  2312. {
  2313. return adopt_ref(*new GridTrackSizeStyleValue(grid_track_size_list));
  2314. }
  2315. NonnullRefPtr<GridTrackSizeStyleValue> GridTrackSizeStyleValue::make_auto()
  2316. {
  2317. return adopt_ref(*new GridTrackSizeStyleValue(CSS::GridTrackSizeList()));
  2318. }
  2319. NonnullRefPtr<RectStyleValue> RectStyleValue::create(EdgeRect rect)
  2320. {
  2321. return adopt_ref(*new RectStyleValue(rect));
  2322. }
  2323. NonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  2324. {
  2325. if (length.is_auto()) {
  2326. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_auto()));
  2327. return value;
  2328. }
  2329. if (length.is_px()) {
  2330. if (length.raw_value() == 0) {
  2331. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(0)));
  2332. return value;
  2333. }
  2334. if (length.raw_value() == 1) {
  2335. static auto value = adopt_ref(*new LengthStyleValue(CSS::Length::make_px(1)));
  2336. return value;
  2337. }
  2338. }
  2339. return adopt_ref(*new LengthStyleValue(length));
  2340. }
  2341. static Optional<CSS::Length> absolutized_length(CSS::Length const& length, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size)
  2342. {
  2343. if (length.is_px())
  2344. return {};
  2345. if (length.is_absolute() || length.is_relative()) {
  2346. auto px = length.to_px(viewport_rect, font_metrics, font_size, root_font_size);
  2347. return CSS::Length::make_px(px);
  2348. }
  2349. return {};
  2350. }
  2351. NonnullRefPtr<StyleValue> StyleValue::absolutized(CSSPixelRect const&, Gfx::FontPixelMetrics const&, CSSPixels, CSSPixels) const
  2352. {
  2353. return *this;
  2354. }
  2355. NonnullRefPtr<StyleValue> LengthStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size) const
  2356. {
  2357. if (auto length = absolutized_length(m_length, viewport_rect, font_metrics, font_size, root_font_size); length.has_value())
  2358. return LengthStyleValue::create(length.release_value());
  2359. return *this;
  2360. }
  2361. NonnullRefPtr<StyleValue> ShadowStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size) const
  2362. {
  2363. auto absolutized_offset_x = absolutized_length(m_offset_x, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_x);
  2364. auto absolutized_offset_y = absolutized_length(m_offset_y, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_offset_y);
  2365. auto absolutized_blur_radius = absolutized_length(m_blur_radius, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_blur_radius);
  2366. auto absolutized_spread_distance = absolutized_length(m_spread_distance, viewport_rect, font_metrics, font_size, root_font_size).value_or(m_spread_distance);
  2367. return ShadowStyleValue::create(m_color, absolutized_offset_x, absolutized_offset_y, absolutized_blur_radius, absolutized_spread_distance, m_placement);
  2368. }
  2369. NonnullRefPtr<StyleValue> BorderRadiusStyleValue::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size) const
  2370. {
  2371. if (m_horizontal_radius.is_percentage() && m_vertical_radius.is_percentage())
  2372. return *this;
  2373. auto absolutized_horizontal_radius = m_horizontal_radius;
  2374. auto absolutized_vertical_radius = m_vertical_radius;
  2375. if (!m_horizontal_radius.is_percentage())
  2376. absolutized_horizontal_radius = absolutized_length(m_horizontal_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_horizontal_radius.length());
  2377. if (!m_vertical_radius.is_percentage())
  2378. absolutized_vertical_radius = absolutized_length(m_vertical_radius.length(), viewport_rect, font_metrics, font_size, root_font_size).value_or(m_vertical_radius.length());
  2379. return BorderRadiusStyleValue::create(absolutized_horizontal_radius, absolutized_vertical_radius);
  2380. }
  2381. bool CalculatedStyleValue::contains_percentage() const
  2382. {
  2383. return m_expression->contains_percentage();
  2384. }
  2385. bool CalculatedStyleValue::CalcSum::contains_percentage() const
  2386. {
  2387. if (first_calc_product->contains_percentage())
  2388. return true;
  2389. for (auto& part : zero_or_more_additional_calc_products) {
  2390. if (part.contains_percentage())
  2391. return true;
  2392. }
  2393. return false;
  2394. }
  2395. bool CalculatedStyleValue::CalcSumPartWithOperator::contains_percentage() const
  2396. {
  2397. return value->contains_percentage();
  2398. }
  2399. bool CalculatedStyleValue::CalcProduct::contains_percentage() const
  2400. {
  2401. if (first_calc_value.contains_percentage())
  2402. return true;
  2403. for (auto& part : zero_or_more_additional_calc_values) {
  2404. if (part.contains_percentage())
  2405. return true;
  2406. }
  2407. return false;
  2408. }
  2409. bool CalculatedStyleValue::CalcProductPartWithOperator::contains_percentage() const
  2410. {
  2411. return value.visit(
  2412. [](CalcValue const& value) { return value.contains_percentage(); },
  2413. [](CalcNumberValue const&) { return false; });
  2414. }
  2415. bool CalculatedStyleValue::CalcValue::contains_percentage() const
  2416. {
  2417. return value.visit(
  2418. [](Percentage const&) { return true; },
  2419. [](NonnullOwnPtr<CalcSum> const& sum) { return sum->contains_percentage(); },
  2420. [](auto const&) { return false; });
  2421. }
  2422. bool calculated_style_value_contains_percentage(CalculatedStyleValue const& value)
  2423. {
  2424. return value.contains_percentage();
  2425. }
  2426. }