StyleProperties.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/TypeCasts.h>
  8. #include <LibCore/DirIterator.h>
  9. #include <LibGfx/Font/FontDatabase.h>
  10. #include <LibWeb/CSS/StyleProperties.h>
  11. #include <LibWeb/FontCache.h>
  12. #include <LibWeb/Layout/BlockContainer.h>
  13. #include <LibWeb/Layout/Node.h>
  14. namespace Web::CSS {
  15. StyleProperties::StyleProperties(StyleProperties const& other)
  16. : m_property_values(other.m_property_values)
  17. {
  18. if (other.m_font) {
  19. m_font = other.m_font->clone();
  20. } else {
  21. m_font = nullptr;
  22. }
  23. }
  24. NonnullRefPtr<StyleProperties> StyleProperties::clone() const
  25. {
  26. return adopt_ref(*new StyleProperties(*this));
  27. }
  28. void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value)
  29. {
  30. m_property_values[to_underlying(id)] = move(value);
  31. }
  32. NonnullRefPtr<StyleValue> StyleProperties::property(CSS::PropertyID property_id) const
  33. {
  34. auto value = m_property_values[to_underlying(property_id)];
  35. // By the time we call this method, all properties have values assigned.
  36. VERIFY(!value.is_null());
  37. return value.release_nonnull();
  38. }
  39. Length StyleProperties::length_or_fallback(CSS::PropertyID id, Length const& fallback) const
  40. {
  41. auto value = property(id);
  42. if (value->is_calculated())
  43. return Length::make_calculated(value->as_calculated());
  44. if (value->has_length())
  45. return value->to_length();
  46. return fallback;
  47. }
  48. LengthPercentage StyleProperties::length_percentage_or_fallback(CSS::PropertyID id, LengthPercentage const& fallback) const
  49. {
  50. return length_percentage(id).value_or(fallback);
  51. }
  52. Optional<LengthPercentage> StyleProperties::length_percentage(CSS::PropertyID id) const
  53. {
  54. auto value = property(id);
  55. if (value->is_calculated())
  56. return LengthPercentage { value->as_calculated() };
  57. if (value->is_percentage())
  58. return value->as_percentage().percentage();
  59. if (value->has_length())
  60. return value->to_length();
  61. return {};
  62. }
  63. LengthBox StyleProperties::length_box(CSS::PropertyID left_id, CSS::PropertyID top_id, CSS::PropertyID right_id, CSS::PropertyID bottom_id, const CSS::Length& default_value) const
  64. {
  65. LengthBox box;
  66. box.left = length_percentage_or_fallback(left_id, default_value);
  67. box.top = length_percentage_or_fallback(top_id, default_value);
  68. box.right = length_percentage_or_fallback(right_id, default_value);
  69. box.bottom = length_percentage_or_fallback(bottom_id, default_value);
  70. return box;
  71. }
  72. Color StyleProperties::color_or_fallback(CSS::PropertyID id, Layout::NodeWithStyle const& node, Color fallback) const
  73. {
  74. auto value = property(id);
  75. if (!value->has_color())
  76. return fallback;
  77. return value->to_color(node);
  78. }
  79. NonnullRefPtr<Gfx::Font> StyleProperties::font_fallback(bool monospace, bool bold)
  80. {
  81. if (monospace && bold)
  82. return Gfx::FontDatabase::default_fixed_width_font().bold_variant();
  83. if (monospace)
  84. return Gfx::FontDatabase::default_fixed_width_font();
  85. if (bold)
  86. return Gfx::FontDatabase::default_font().bold_variant();
  87. return Gfx::FontDatabase::default_font();
  88. }
  89. float StyleProperties::line_height(Layout::Node const& layout_node) const
  90. {
  91. auto line_height = property(CSS::PropertyID::LineHeight);
  92. if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
  93. return layout_node.font().pixel_metrics().line_spacing();
  94. if (line_height->is_length()) {
  95. auto line_height_length = line_height->to_length();
  96. if (!line_height_length.is_auto())
  97. return line_height_length.to_px(layout_node);
  98. }
  99. if (line_height->is_numeric())
  100. return Length(line_height->to_number(), Length::Type::Em).to_px(layout_node);
  101. if (line_height->is_percentage()) {
  102. // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
  103. auto& percentage = line_height->as_percentage().percentage();
  104. return Length(percentage.as_fraction(), Length::Type::Em).to_px(layout_node);
  105. }
  106. return layout_node.font().pixel_metrics().line_spacing();
  107. }
  108. Optional<int> StyleProperties::z_index() const
  109. {
  110. auto value = property(CSS::PropertyID::ZIndex);
  111. if (value->has_auto())
  112. return {};
  113. if (value->has_integer())
  114. return value->to_integer();
  115. return {};
  116. }
  117. float StyleProperties::opacity() const
  118. {
  119. auto value = property(CSS::PropertyID::Opacity);
  120. float unclamped_opacity = 1.0f;
  121. if (value->has_number()) {
  122. unclamped_opacity = value->to_number();
  123. } else if (value->is_calculated()) {
  124. auto& calculated = value->as_calculated();
  125. if (calculated.resolved_type() == CalculatedStyleValue::ResolvedType::Percentage) {
  126. auto maybe_percentage = value->as_calculated().resolve_percentage();
  127. if (maybe_percentage.has_value())
  128. unclamped_opacity = maybe_percentage->as_fraction();
  129. else
  130. dbgln("Unable to resolve calc() as opacity (percentage): {}", value->to_string());
  131. } else {
  132. auto maybe_number = value->as_calculated().resolve_number();
  133. if (maybe_number.has_value())
  134. unclamped_opacity = maybe_number.value();
  135. else
  136. dbgln("Unable to resolve calc() as opacity (number): {}", value->to_string());
  137. }
  138. } else if (value->is_percentage()) {
  139. unclamped_opacity = value->as_percentage().percentage().as_fraction();
  140. }
  141. return clamp(unclamped_opacity, 0.0f, 1.0f);
  142. }
  143. Optional<CSS::FlexDirection> StyleProperties::flex_direction() const
  144. {
  145. auto value = property(CSS::PropertyID::FlexDirection);
  146. return value_id_to_flex_direction(value->to_identifier());
  147. }
  148. Optional<CSS::FlexWrap> StyleProperties::flex_wrap() const
  149. {
  150. auto value = property(CSS::PropertyID::FlexWrap);
  151. return value_id_to_flex_wrap(value->to_identifier());
  152. }
  153. Optional<CSS::FlexBasisData> StyleProperties::flex_basis() const
  154. {
  155. auto value = property(CSS::PropertyID::FlexBasis);
  156. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::Content)
  157. return { { CSS::FlexBasis::Content, {} } };
  158. if (value->has_auto())
  159. return { { CSS::FlexBasis::Auto, {} } };
  160. if (value->is_percentage())
  161. return { { CSS::FlexBasis::LengthPercentage, value->as_percentage().percentage() } };
  162. if (value->has_length())
  163. return { { CSS::FlexBasis::LengthPercentage, value->to_length() } };
  164. return {};
  165. }
  166. float StyleProperties::flex_grow() const
  167. {
  168. auto value = property(CSS::PropertyID::FlexGrow);
  169. if (!value->has_number())
  170. return 0;
  171. return value->to_number();
  172. }
  173. float StyleProperties::flex_shrink() const
  174. {
  175. auto value = property(CSS::PropertyID::FlexShrink);
  176. if (!value->has_number())
  177. return 1;
  178. return value->to_number();
  179. }
  180. int StyleProperties::order() const
  181. {
  182. auto value = property(CSS::PropertyID::Order);
  183. if (!value->has_integer())
  184. return 0;
  185. return value->to_integer();
  186. }
  187. Optional<CSS::ImageRendering> StyleProperties::image_rendering() const
  188. {
  189. auto value = property(CSS::PropertyID::ImageRendering);
  190. return value_id_to_image_rendering(value->to_identifier());
  191. }
  192. Optional<CSS::JustifyContent> StyleProperties::justify_content() const
  193. {
  194. auto value = property(CSS::PropertyID::JustifyContent);
  195. return value_id_to_justify_content(value->to_identifier());
  196. }
  197. Vector<CSS::Transformation> StyleProperties::transformations() const
  198. {
  199. auto value = property(CSS::PropertyID::Transform);
  200. if (value->is_identifier() && value->to_identifier() == CSS::ValueID::None)
  201. return {};
  202. if (!value->is_value_list())
  203. return {};
  204. auto& list = value->as_value_list();
  205. Vector<CSS::Transformation> transformations;
  206. for (auto& it : list.values()) {
  207. if (!it.is_transformation())
  208. return {};
  209. auto& transformation_style_value = it.as_transformation();
  210. CSS::Transformation transformation;
  211. transformation.function = transformation_style_value.transform_function();
  212. Vector<TransformValue> values;
  213. for (auto& transformation_value : transformation_style_value.values()) {
  214. if (transformation_value.is_length()) {
  215. values.append({ transformation_value.to_length() });
  216. } else if (transformation_value.is_percentage()) {
  217. values.append({ transformation_value.as_percentage().percentage() });
  218. } else if (transformation_value.is_numeric()) {
  219. values.append({ transformation_value.to_number() });
  220. } else if (transformation_value.is_angle()) {
  221. values.append({ transformation_value.as_angle().angle() });
  222. } else {
  223. dbgln("FIXME: Unsupported value in transform!");
  224. }
  225. }
  226. transformation.values = move(values);
  227. transformations.append(move(transformation));
  228. }
  229. return transformations;
  230. }
  231. static Optional<LengthPercentage> length_percentage_for_style_value(StyleValue const& value)
  232. {
  233. if (value.is_length())
  234. return value.to_length();
  235. if (value.is_percentage())
  236. return value.as_percentage().percentage();
  237. return {};
  238. }
  239. CSS::TransformOrigin StyleProperties::transform_origin() const
  240. {
  241. auto value = property(CSS::PropertyID::TransformOrigin);
  242. if (!value->is_value_list() || value->as_value_list().size() != 2)
  243. return {};
  244. auto const& list = value->as_value_list();
  245. auto x_value = length_percentage_for_style_value(list.values()[0]);
  246. auto y_value = length_percentage_for_style_value(list.values()[1]);
  247. if (!x_value.has_value() || !y_value.has_value()) {
  248. return {};
  249. }
  250. return { x_value.value(), y_value.value() };
  251. }
  252. Optional<CSS::AlignItems> StyleProperties::align_items() const
  253. {
  254. auto value = property(CSS::PropertyID::AlignItems);
  255. return value_id_to_align_items(value->to_identifier());
  256. }
  257. Optional<CSS::AlignSelf> StyleProperties::align_self() const
  258. {
  259. auto value = property(CSS::PropertyID::AlignSelf);
  260. return value_id_to_align_self(value->to_identifier());
  261. }
  262. Optional<CSS::Appearance> StyleProperties::appearance() const
  263. {
  264. auto value = property(CSS::PropertyID::Appearance);
  265. auto appearance = value_id_to_appearance(value->to_identifier());
  266. if (appearance.has_value()) {
  267. switch (*appearance) {
  268. // Note: All these compatibility values can be treated as 'auto'
  269. case CSS::Appearance::Textfield:
  270. case CSS::Appearance::MenulistButton:
  271. case CSS::Appearance::Searchfield:
  272. case CSS::Appearance::Textarea:
  273. case CSS::Appearance::PushButton:
  274. case CSS::Appearance::SliderHorizontal:
  275. case CSS::Appearance::Checkbox:
  276. case CSS::Appearance::Radio:
  277. case CSS::Appearance::SquareButton:
  278. case CSS::Appearance::Menulist:
  279. case CSS::Appearance::Listbox:
  280. case CSS::Appearance::Meter:
  281. case CSS::Appearance::ProgressBar:
  282. case CSS::Appearance::Button:
  283. appearance = CSS::Appearance::Auto;
  284. break;
  285. default:
  286. break;
  287. }
  288. }
  289. return appearance;
  290. }
  291. Optional<CSS::Position> StyleProperties::position() const
  292. {
  293. auto value = property(CSS::PropertyID::Position);
  294. return value_id_to_position(value->to_identifier());
  295. }
  296. bool StyleProperties::operator==(StyleProperties const& other) const
  297. {
  298. if (m_property_values.size() != other.m_property_values.size())
  299. return false;
  300. for (size_t i = 0; i < m_property_values.size(); ++i) {
  301. auto const& my_ptr = m_property_values[i];
  302. auto const& other_ptr = other.m_property_values[i];
  303. if (!my_ptr) {
  304. if (other_ptr)
  305. return false;
  306. continue;
  307. }
  308. if (!other_ptr)
  309. return false;
  310. auto const& my_value = *my_ptr;
  311. auto const& other_value = *other_ptr;
  312. if (my_value.type() != other_value.type())
  313. return false;
  314. if (my_value != other_value)
  315. return false;
  316. }
  317. return true;
  318. }
  319. Optional<CSS::TextAlign> StyleProperties::text_align() const
  320. {
  321. auto value = property(CSS::PropertyID::TextAlign);
  322. return value_id_to_text_align(value->to_identifier());
  323. }
  324. Optional<CSS::TextJustify> StyleProperties::text_justify() const
  325. {
  326. auto value = property(CSS::PropertyID::TextJustify);
  327. return value_id_to_text_justify(value->to_identifier());
  328. }
  329. Optional<CSS::PointerEvents> StyleProperties::pointer_events() const
  330. {
  331. auto value = property(CSS::PropertyID::PointerEvents);
  332. return value_id_to_pointer_events(value->to_identifier());
  333. }
  334. Optional<CSS::WhiteSpace> StyleProperties::white_space() const
  335. {
  336. auto value = property(CSS::PropertyID::WhiteSpace);
  337. return value_id_to_white_space(value->to_identifier());
  338. }
  339. Optional<CSS::LineStyle> StyleProperties::line_style(CSS::PropertyID property_id) const
  340. {
  341. auto value = property(property_id);
  342. return value_id_to_line_style(value->to_identifier());
  343. }
  344. Optional<CSS::Float> StyleProperties::float_() const
  345. {
  346. auto value = property(CSS::PropertyID::Float);
  347. return value_id_to_float(value->to_identifier());
  348. }
  349. Optional<CSS::Clear> StyleProperties::clear() const
  350. {
  351. auto value = property(CSS::PropertyID::Clear);
  352. return value_id_to_clear(value->to_identifier());
  353. }
  354. CSS::ContentData StyleProperties::content() const
  355. {
  356. auto value = property(CSS::PropertyID::Content);
  357. if (value->is_content()) {
  358. auto& content_style_value = value->as_content();
  359. CSS::ContentData content_data;
  360. // FIXME: The content is a list of things: strings, identifiers or functions that return strings, and images.
  361. // So it can't always be represented as a single String, but may have to be multiple boxes.
  362. // For now, we'll just assume strings since that is easiest.
  363. StringBuilder builder;
  364. for (auto const& item : content_style_value.content().values()) {
  365. if (item.is_string()) {
  366. builder.append(item.to_string());
  367. } else {
  368. // TODO: Implement quotes, counters, images, and other things.
  369. }
  370. }
  371. content_data.type = ContentData::Type::String;
  372. content_data.data = builder.to_string();
  373. if (content_style_value.has_alt_text()) {
  374. StringBuilder alt_text_builder;
  375. for (auto const& item : content_style_value.alt_text()->values()) {
  376. if (item.is_string()) {
  377. alt_text_builder.append(item.to_string());
  378. } else {
  379. // TODO: Implement counters
  380. }
  381. }
  382. content_data.alt_text = alt_text_builder.to_string();
  383. }
  384. return content_data;
  385. }
  386. switch (value->to_identifier()) {
  387. case ValueID::None:
  388. return { ContentData::Type::None };
  389. case ValueID::Normal:
  390. return { ContentData::Type::Normal };
  391. default:
  392. break;
  393. }
  394. return CSS::ContentData {};
  395. }
  396. Optional<CSS::Cursor> StyleProperties::cursor() const
  397. {
  398. auto value = property(CSS::PropertyID::Cursor);
  399. return value_id_to_cursor(value->to_identifier());
  400. }
  401. Optional<CSS::Visibility> StyleProperties::visibility() const
  402. {
  403. auto value = property(CSS::PropertyID::Visibility);
  404. if (!value->is_identifier())
  405. return {};
  406. return value_id_to_visibility(value->to_identifier());
  407. }
  408. CSS::Display StyleProperties::display() const
  409. {
  410. auto value = property(CSS::PropertyID::Display);
  411. if (!value->is_identifier())
  412. return CSS::Display::from_short(CSS::Display::Short::Inline);
  413. switch (value->to_identifier()) {
  414. case CSS::ValueID::None:
  415. return CSS::Display::from_short(CSS::Display::Short::None);
  416. case CSS::ValueID::Block:
  417. return CSS::Display::from_short(CSS::Display::Short::Block);
  418. case CSS::ValueID::Inline:
  419. return CSS::Display::from_short(CSS::Display::Short::Inline);
  420. case CSS::ValueID::InlineBlock:
  421. return CSS::Display::from_short(CSS::Display::Short::InlineBlock);
  422. case CSS::ValueID::ListItem:
  423. return CSS::Display::from_short(CSS::Display::Short::ListItem);
  424. case CSS::ValueID::Table:
  425. return CSS::Display::from_short(CSS::Display::Short::Table);
  426. case CSS::ValueID::TableRow:
  427. return CSS::Display { CSS::Display::Internal::TableRow };
  428. case CSS::ValueID::TableCell:
  429. return CSS::Display { CSS::Display::Internal::TableCell };
  430. case CSS::ValueID::TableColumn:
  431. return CSS::Display { CSS::Display::Internal::TableColumn };
  432. case CSS::ValueID::TableColumnGroup:
  433. return CSS::Display { CSS::Display::Internal::TableColumnGroup };
  434. case CSS::ValueID::TableCaption:
  435. return CSS::Display { CSS::Display::Internal::TableCaption };
  436. case CSS::ValueID::TableRowGroup:
  437. return CSS::Display { CSS::Display::Internal::TableRowGroup };
  438. case CSS::ValueID::TableHeaderGroup:
  439. return CSS::Display { CSS::Display::Internal::TableHeaderGroup };
  440. case CSS::ValueID::TableFooterGroup:
  441. return CSS::Display { CSS::Display::Internal::TableFooterGroup };
  442. case CSS::ValueID::Flex:
  443. return CSS::Display::from_short(CSS::Display::Short::Flex);
  444. case CSS::ValueID::InlineFlex:
  445. return CSS::Display::from_short(CSS::Display::Short::InlineFlex);
  446. default:
  447. return CSS::Display::from_short(CSS::Display::Short::Block);
  448. }
  449. }
  450. Vector<CSS::TextDecorationLine> StyleProperties::text_decoration_line() const
  451. {
  452. auto value = property(CSS::PropertyID::TextDecorationLine);
  453. if (value->is_value_list()) {
  454. Vector<CSS::TextDecorationLine> lines;
  455. auto& values = value->as_value_list().values();
  456. for (auto const& item : values) {
  457. lines.append(value_id_to_text_decoration_line(item.to_identifier()).value());
  458. }
  459. return lines;
  460. }
  461. if (value->is_identifier() && value->to_identifier() == ValueID::None)
  462. return {};
  463. VERIFY_NOT_REACHED();
  464. }
  465. Optional<CSS::TextDecorationStyle> StyleProperties::text_decoration_style() const
  466. {
  467. auto value = property(CSS::PropertyID::TextDecorationStyle);
  468. return value_id_to_text_decoration_style(value->to_identifier());
  469. }
  470. Optional<CSS::TextTransform> StyleProperties::text_transform() const
  471. {
  472. auto value = property(CSS::PropertyID::TextTransform);
  473. return value_id_to_text_transform(value->to_identifier());
  474. }
  475. Optional<CSS::ListStyleType> StyleProperties::list_style_type() const
  476. {
  477. auto value = property(CSS::PropertyID::ListStyleType);
  478. return value_id_to_list_style_type(value->to_identifier());
  479. }
  480. Optional<CSS::Overflow> StyleProperties::overflow_x() const
  481. {
  482. return overflow(CSS::PropertyID::OverflowX);
  483. }
  484. Optional<CSS::Overflow> StyleProperties::overflow_y() const
  485. {
  486. return overflow(CSS::PropertyID::OverflowY);
  487. }
  488. Optional<CSS::Overflow> StyleProperties::overflow(CSS::PropertyID property_id) const
  489. {
  490. auto value = property(property_id);
  491. return value_id_to_overflow(value->to_identifier());
  492. }
  493. Vector<ShadowData> StyleProperties::shadow(PropertyID property_id) const
  494. {
  495. auto value = property(property_id);
  496. auto make_shadow_data = [](ShadowStyleValue const& value) {
  497. return ShadowData { value.color(), value.offset_x(), value.offset_y(), value.blur_radius(), value.spread_distance(), value.placement() };
  498. };
  499. if (value->is_value_list()) {
  500. auto& value_list = value->as_value_list();
  501. Vector<ShadowData> shadow_data;
  502. shadow_data.ensure_capacity(value_list.size());
  503. for (auto const& layer_value : value_list.values())
  504. shadow_data.append(make_shadow_data(layer_value.as_shadow()));
  505. return shadow_data;
  506. }
  507. if (value->is_shadow()) {
  508. auto& box = value->as_shadow();
  509. return { make_shadow_data(box) };
  510. }
  511. return {};
  512. }
  513. Vector<ShadowData> StyleProperties::box_shadow() const
  514. {
  515. return shadow(PropertyID::BoxShadow);
  516. }
  517. Vector<ShadowData> StyleProperties::text_shadow() const
  518. {
  519. return shadow(PropertyID::TextShadow);
  520. }
  521. Optional<CSS::BoxSizing> StyleProperties::box_sizing() const
  522. {
  523. auto value = property(CSS::PropertyID::BoxSizing);
  524. return value_id_to_box_sizing(value->to_identifier());
  525. }
  526. Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_align() const
  527. {
  528. auto value = property(CSS::PropertyID::VerticalAlign);
  529. if (value->is_identifier())
  530. return value_id_to_vertical_align(value->to_identifier()).release_value();
  531. if (value->is_length())
  532. return CSS::LengthPercentage(value->to_length());
  533. if (value->is_percentage())
  534. return CSS::LengthPercentage(value->as_percentage().percentage());
  535. VERIFY_NOT_REACHED();
  536. }
  537. Optional<CSS::FontVariant> StyleProperties::font_variant() const
  538. {
  539. auto value = property(CSS::PropertyID::FontVariant);
  540. return value_id_to_font_variant(value->to_identifier());
  541. }
  542. }