LayoutState.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibWeb/Layout/AvailableSpace.h>
  8. #include <LibWeb/Layout/BlockContainer.h>
  9. #include <LibWeb/Layout/LayoutState.h>
  10. #include <LibWeb/Layout/Viewport.h>
  11. #include <LibWeb/Painting/InlinePaintable.h>
  12. #include <LibWeb/Painting/SVGPathPaintable.h>
  13. namespace Web::Layout {
  14. LayoutState::LayoutState(LayoutState const* parent)
  15. : m_parent(parent)
  16. , m_root(find_root())
  17. {
  18. }
  19. LayoutState::~LayoutState()
  20. {
  21. }
  22. LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyle const& node)
  23. {
  24. if (auto* used_values = used_values_per_layout_node.get(&node).value_or(nullptr))
  25. return *used_values;
  26. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  27. if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(&node).value_or(nullptr)) {
  28. auto cow_used_values = adopt_own(*new UsedValues(*ancestor_used_values));
  29. auto* cow_used_values_ptr = cow_used_values.ptr();
  30. used_values_per_layout_node.set(&node, move(cow_used_values));
  31. return *cow_used_values_ptr;
  32. }
  33. }
  34. auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
  35. auto new_used_values = adopt_own(*new UsedValues);
  36. auto* new_used_values_ptr = new_used_values.ptr();
  37. new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values);
  38. used_values_per_layout_node.set(&node, move(new_used_values));
  39. return *new_used_values_ptr;
  40. }
  41. LayoutState::UsedValues const& LayoutState::get(NodeWithStyle const& node) const
  42. {
  43. if (auto const* used_values = used_values_per_layout_node.get(&node).value_or(nullptr))
  44. return *used_values;
  45. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  46. if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(&node).value_or(nullptr))
  47. return *ancestor_used_values;
  48. }
  49. auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
  50. auto new_used_values = adopt_own(*new UsedValues);
  51. auto* new_used_values_ptr = new_used_values.ptr();
  52. new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values);
  53. const_cast<LayoutState*>(this)->used_values_per_layout_node.set(&node, move(new_used_values));
  54. return *new_used_values_ptr;
  55. }
  56. // https://www.w3.org/TR/css-overflow-3/#scrollable-overflow
  57. static CSSPixelRect measure_scrollable_overflow(Box const& box)
  58. {
  59. if (!box.paintable_box())
  60. return {};
  61. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  62. if (paintable_box.scrollable_overflow_rect().has_value())
  63. return paintable_box.scrollable_overflow_rect().value();
  64. // The scrollable overflow area is the union of:
  65. // - The scroll container’s own padding box.
  66. auto scrollable_overflow_rect = paintable_box.absolute_padding_box_rect();
  67. // - All line boxes directly contained by the scroll container.
  68. if (is<Painting::PaintableWithLines>(box.paintable())) {
  69. for (auto const& fragment : static_cast<Painting::PaintableWithLines const&>(*box.paintable()).fragments()) {
  70. scrollable_overflow_rect = scrollable_overflow_rect.united(fragment.absolute_rect());
  71. }
  72. }
  73. // - The border boxes of all boxes for which it is the containing block
  74. // and whose border boxes are positioned not wholly in the negative scrollable overflow region,
  75. // FIXME: accounting for transforms by projecting each box onto the plane of the element that establishes its 3D rendering context. [CSS3-TRANSFORMS]
  76. if (!box.children_are_inline()) {
  77. box.for_each_child_of_type<Box>([&box, &scrollable_overflow_rect](Box const& child) {
  78. if (!child.paintable_box())
  79. return IterationDecision::Continue;
  80. auto child_border_box = child.paintable_box()->absolute_border_box_rect();
  81. // NOTE: Here we check that the child is not wholly in the negative scrollable overflow region.
  82. if (child_border_box.bottom() > 0 && child_border_box.right() > 0)
  83. scrollable_overflow_rect = scrollable_overflow_rect.united(child_border_box);
  84. // - The scrollable overflow areas of all of the above boxes
  85. // (including zero-area boxes and accounting for transforms as described above),
  86. // provided they themselves have overflow: visible (i.e. do not themselves trap the overflow)
  87. // and that scrollable overflow is not already clipped (e.g. by the clip property or the contain property).
  88. if (is<Viewport>(box) || child.computed_values().overflow_x() == CSS::Overflow::Visible || child.computed_values().overflow_y() == CSS::Overflow::Visible) {
  89. auto child_scrollable_overflow = measure_scrollable_overflow(child);
  90. if (is<Viewport>(box) || child.computed_values().overflow_x() == CSS::Overflow::Visible)
  91. scrollable_overflow_rect.unite_horizontally(child_scrollable_overflow);
  92. if (is<Viewport>(box) || child.computed_values().overflow_y() == CSS::Overflow::Visible)
  93. scrollable_overflow_rect.unite_vertically(child_scrollable_overflow);
  94. }
  95. return IterationDecision::Continue;
  96. });
  97. }
  98. // FIXME: - The margin areas of grid item and flex item boxes for which the box establishes a containing block.
  99. // FIXME: - Additional padding added to the end-side of the scrollable overflow rectangle as necessary
  100. // to enable a scroll position that satisfies the requirements of place-content: end alignment.
  101. paintable_box.set_overflow_data(Painting::PaintableBox::OverflowData {
  102. .scrollable_overflow_rect = scrollable_overflow_rect,
  103. .has_scrollable_overflow = !paintable_box.absolute_padding_box_rect().contains(scrollable_overflow_rect),
  104. });
  105. return scrollable_overflow_rect;
  106. }
  107. void LayoutState::resolve_relative_positions(Vector<Painting::PaintableWithLines&> const& paintables_with_lines)
  108. {
  109. // This function resolves relative position offsets of all the boxes & fragments in the paint tree.
  110. // It runs *after* the paint tree has been constructed, so it modifies paintable node & fragment offsets directly.
  111. // Regular boxes (not line box fragments):
  112. for (auto& it : used_values_per_layout_node) {
  113. auto& used_values = *it.value;
  114. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  115. if (!node.is_box())
  116. continue;
  117. auto& paintable = static_cast<Painting::PaintableBox&>(*node.paintable());
  118. CSSPixelPoint offset;
  119. if (used_values.containing_line_box_fragment.has_value()) {
  120. // Atomic inline case:
  121. // We know that `node` is an atomic inline because `containing_line_box_fragments` refers to the
  122. // line box fragment in the parent block container that contains it.
  123. auto const& containing_line_box_fragment = used_values.containing_line_box_fragment.value();
  124. auto const& containing_block = *node.containing_block();
  125. auto const& containing_block_used_values = get(containing_block);
  126. auto const& fragment = containing_block_used_values.line_boxes[containing_line_box_fragment.line_box_index].fragments()[containing_line_box_fragment.fragment_index];
  127. // The fragment has the final offset for the atomic inline, so we just need to copy it from there.
  128. offset = fragment.offset();
  129. } else {
  130. // Not an atomic inline, much simpler case.
  131. offset = used_values.offset;
  132. }
  133. // Apply relative position inset if appropriate.
  134. if (node.computed_values().position() == CSS::Positioning::Relative && is<NodeWithStyleAndBoxModelMetrics>(node)) {
  135. auto& inset = static_cast<NodeWithStyleAndBoxModelMetrics const&>(node).box_model().inset;
  136. offset.translate_by(inset.left, inset.top);
  137. }
  138. paintable.set_offset(offset);
  139. }
  140. // Line box fragments:
  141. for (auto const& paintable_with_lines : paintables_with_lines) {
  142. for (auto& fragment : paintable_with_lines.fragments()) {
  143. auto const& fragment_node = fragment.layout_node();
  144. if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*fragment_node.parent()))
  145. continue;
  146. // Collect effective relative position offset from inline-flow parent chain.
  147. CSSPixelPoint offset;
  148. for (auto* ancestor = fragment_node.parent(); ancestor; ancestor = ancestor->parent()) {
  149. if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*ancestor))
  150. break;
  151. if (!ancestor->display().is_inline_outside() || !ancestor->display().is_flow_inside())
  152. break;
  153. if (ancestor->computed_values().position() == CSS::Positioning::Relative) {
  154. auto const& ancestor_node = static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*ancestor);
  155. auto const& inset = ancestor_node.box_model().inset;
  156. offset.translate_by(inset.left, inset.top);
  157. }
  158. }
  159. const_cast<Painting::PaintableFragment&>(fragment).set_offset(fragment.offset().translated(offset));
  160. }
  161. }
  162. }
  163. static void build_paint_tree(Node& node, Painting::Paintable* parent_paintable = nullptr)
  164. {
  165. Painting::Paintable* paintable = nullptr;
  166. if (node.paintable()) {
  167. paintable = const_cast<Painting::Paintable*>(node.paintable());
  168. if (parent_paintable && !paintable->forms_unconnected_subtree()) {
  169. VERIFY(!paintable->parent());
  170. parent_paintable->append_child(*paintable);
  171. }
  172. paintable->set_dom_node(node.dom_node());
  173. if (node.dom_node())
  174. node.dom_node()->set_paintable(paintable);
  175. }
  176. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  177. build_paint_tree(*child, paintable);
  178. }
  179. }
  180. static Painting::BorderRadiiData normalized_border_radii_data(Layout::Node const& node, CSSPixelRect const& rect, CSS::BorderRadiusData top_left_radius, CSS::BorderRadiusData top_right_radius, CSS::BorderRadiusData bottom_right_radius, CSS::BorderRadiusData bottom_left_radius)
  181. {
  182. Painting::BorderRadiusData bottom_left_radius_px {};
  183. Painting::BorderRadiusData bottom_right_radius_px {};
  184. Painting::BorderRadiusData top_left_radius_px {};
  185. Painting::BorderRadiusData top_right_radius_px {};
  186. bottom_left_radius_px.horizontal_radius = bottom_left_radius.horizontal_radius.to_px(node, rect.width());
  187. bottom_right_radius_px.horizontal_radius = bottom_right_radius.horizontal_radius.to_px(node, rect.width());
  188. top_left_radius_px.horizontal_radius = top_left_radius.horizontal_radius.to_px(node, rect.width());
  189. top_right_radius_px.horizontal_radius = top_right_radius.horizontal_radius.to_px(node, rect.width());
  190. bottom_left_radius_px.vertical_radius = bottom_left_radius.vertical_radius.to_px(node, rect.height());
  191. bottom_right_radius_px.vertical_radius = bottom_right_radius.vertical_radius.to_px(node, rect.height());
  192. top_left_radius_px.vertical_radius = top_left_radius.vertical_radius.to_px(node, rect.height());
  193. top_right_radius_px.vertical_radius = top_right_radius.vertical_radius.to_px(node, rect.height());
  194. // Scale overlapping curves according to https://www.w3.org/TR/css-backgrounds-3/#corner-overlap
  195. // Let f = min(Li/Si), where i ∈ {top, right, bottom, left},
  196. // Si is the sum of the two corresponding radii of the corners on side i,
  197. // and Ltop = Lbottom = the width of the box, and Lleft = Lright = the height of the box.
  198. auto l_top = rect.width();
  199. auto l_bottom = l_top;
  200. auto l_left = rect.height();
  201. auto l_right = l_left;
  202. auto s_top = (top_left_radius_px.horizontal_radius + top_right_radius_px.horizontal_radius);
  203. auto s_right = (top_right_radius_px.vertical_radius + bottom_right_radius_px.vertical_radius);
  204. auto s_bottom = (bottom_left_radius_px.horizontal_radius + bottom_right_radius_px.horizontal_radius);
  205. auto s_left = (top_left_radius_px.vertical_radius + bottom_left_radius_px.vertical_radius);
  206. CSSPixelFraction f = 1;
  207. f = min(f, l_top / s_top);
  208. f = min(f, l_right / s_right);
  209. f = min(f, l_bottom / s_bottom);
  210. f = min(f, l_left / s_left);
  211. // If f < 1, then all corner radii are reduced by multiplying them by f.
  212. if (f < 1) {
  213. top_left_radius_px.horizontal_radius *= f;
  214. top_left_radius_px.vertical_radius *= f;
  215. top_right_radius_px.horizontal_radius *= f;
  216. top_right_radius_px.vertical_radius *= f;
  217. bottom_right_radius_px.horizontal_radius *= f;
  218. bottom_right_radius_px.vertical_radius *= f;
  219. bottom_left_radius_px.horizontal_radius *= f;
  220. bottom_left_radius_px.vertical_radius *= f;
  221. }
  222. return Painting::BorderRadiiData { top_left_radius_px, top_right_radius_px, bottom_right_radius_px, bottom_left_radius_px };
  223. }
  224. void LayoutState::resolve_border_radii()
  225. {
  226. Vector<Painting::InlinePaintable&> inline_paintables;
  227. for (auto& it : used_values_per_layout_node) {
  228. auto& used_values = *it.value;
  229. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  230. auto* paintable = node.paintable();
  231. if (paintable && is<Painting::InlinePaintable>(*paintable)) {
  232. auto& inline_paintable = static_cast<Painting::InlinePaintable&>(*paintable);
  233. inline_paintables.append(inline_paintable);
  234. }
  235. if (paintable && is<Painting::PaintableBox>(*paintable)) {
  236. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  237. CSSPixelRect const border_rect { 0, 0, used_values.border_box_width(), used_values.border_box_height() };
  238. auto const& border_top_left_radius = node.computed_values().border_top_left_radius();
  239. auto const& border_top_right_radius = node.computed_values().border_top_right_radius();
  240. auto const& border_bottom_right_radius = node.computed_values().border_bottom_right_radius();
  241. auto const& border_bottom_left_radius = node.computed_values().border_bottom_left_radius();
  242. auto radii_data = normalized_border_radii_data(node, border_rect, border_top_left_radius, border_top_right_radius, border_bottom_right_radius, border_bottom_left_radius);
  243. paintable_box.set_border_radii_data(radii_data);
  244. }
  245. }
  246. for (auto& inline_paintable : inline_paintables) {
  247. Vector<Painting::PaintableFragment&> fragments;
  248. verify_cast<Painting::PaintableWithLines>(*inline_paintable.containing_block()->paintable_box()).for_each_fragment([&](auto& fragment) {
  249. if (inline_paintable.layout_node().is_inclusive_ancestor_of(fragment.layout_node()))
  250. fragments.append(const_cast<Painting::PaintableFragment&>(fragment));
  251. return IterationDecision::Continue;
  252. });
  253. auto const& top_left_border_radius = inline_paintable.computed_values().border_top_left_radius();
  254. auto const& top_right_border_radius = inline_paintable.computed_values().border_top_right_radius();
  255. auto const& bottom_right_border_radius = inline_paintable.computed_values().border_bottom_right_radius();
  256. auto const& bottom_left_border_radius = inline_paintable.computed_values().border_bottom_left_radius();
  257. auto containing_block_position_in_absolute_coordinates = inline_paintable.containing_block()->paintable_box()->absolute_position();
  258. for (size_t i = 0; i < fragments.size(); ++i) {
  259. auto is_first_fragment = i == 0;
  260. auto is_last_fragment = i == fragments.size() - 1;
  261. auto& fragment = fragments[i];
  262. CSSPixelRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  263. if (is_first_fragment) {
  264. auto extra_start_width = inline_paintable.box_model().padding.left;
  265. absolute_fragment_rect.translate_by(-extra_start_width, 0);
  266. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
  267. }
  268. if (is_last_fragment) {
  269. auto extra_end_width = inline_paintable.box_model().padding.right;
  270. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
  271. }
  272. auto border_radii_data = normalized_border_radii_data(inline_paintable.layout_node(), absolute_fragment_rect, top_left_border_radius, top_right_border_radius, bottom_right_border_radius, bottom_left_border_radius);
  273. fragment.set_border_radii_data(border_radii_data);
  274. }
  275. }
  276. }
  277. void LayoutState::resolve_box_shadow_data()
  278. {
  279. for (auto& it : used_values_per_layout_node) {
  280. auto& used_values = *it.value;
  281. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  282. auto* paintable = node.paintable();
  283. if (paintable && is<Painting::PaintableBox>(*paintable)) {
  284. auto box_shadow_data = node.computed_values().box_shadow();
  285. if (box_shadow_data.is_empty())
  286. continue;
  287. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  288. Vector<Painting::ShadowData> resolved_box_shadow_data;
  289. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  290. for (auto const& layer : box_shadow_data) {
  291. resolved_box_shadow_data.empend(
  292. layer.color,
  293. layer.offset_x.to_px(node),
  294. layer.offset_y.to_px(node),
  295. layer.blur_radius.to_px(node),
  296. layer.spread_distance.to_px(node),
  297. layer.placement == CSS::ShadowPlacement::Outer ? Painting::ShadowPlacement::Outer : Painting::ShadowPlacement::Inner);
  298. }
  299. paintable_box.set_box_shadow_data(move(resolved_box_shadow_data));
  300. }
  301. }
  302. }
  303. void LayoutState::resolve_text_shadows(Vector<Painting::PaintableWithLines&> const& paintables_with_lines)
  304. {
  305. for (auto const& paintable_with_lines : paintables_with_lines) {
  306. for (auto const& fragment : paintable_with_lines.fragments()) {
  307. auto const& text_shadow = fragment.m_layout_node->computed_values().text_shadow();
  308. if (!text_shadow.is_empty()) {
  309. Vector<Painting::ShadowData> resolved_shadow_data;
  310. resolved_shadow_data.ensure_capacity(text_shadow.size());
  311. for (auto const& layer : text_shadow) {
  312. resolved_shadow_data.empend(
  313. layer.color,
  314. layer.offset_x.to_px(paintable_with_lines.layout_node()),
  315. layer.offset_y.to_px(paintable_with_lines.layout_node()),
  316. layer.blur_radius.to_px(paintable_with_lines.layout_node()),
  317. layer.spread_distance.to_px(paintable_with_lines.layout_node()),
  318. Painting::ShadowPlacement::Outer);
  319. }
  320. const_cast<Painting::PaintableFragment&>(fragment).set_shadows(move(resolved_shadow_data));
  321. }
  322. }
  323. }
  324. }
  325. void LayoutState::commit(Box& root)
  326. {
  327. // Only the top-level LayoutState should ever be committed.
  328. VERIFY(!m_parent);
  329. // NOTE: In case this is a relayout of an existing tree, we start by detaching the old paint tree
  330. // from the layout tree. This is done to ensure that we don't end up with any old-tree pointers
  331. // when text paintables shift around in the tree.
  332. root.for_each_in_inclusive_subtree([&](Layout::Node& node) {
  333. node.set_paintable(nullptr);
  334. return IterationDecision::Continue;
  335. });
  336. HashTable<Layout::TextNode*> text_nodes;
  337. Vector<Painting::PaintableWithLines&> paintables_with_lines;
  338. for (auto& it : used_values_per_layout_node) {
  339. auto& used_values = *it.value;
  340. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  341. if (is<NodeWithStyleAndBoxModelMetrics>(node)) {
  342. // Transfer box model metrics.
  343. auto& box_model = static_cast<NodeWithStyleAndBoxModelMetrics&>(node).box_model();
  344. box_model.inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
  345. box_model.padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
  346. box_model.border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
  347. box_model.margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
  348. }
  349. auto paintable = node.create_paintable();
  350. node.set_paintable(paintable);
  351. // For boxes, transfer all the state needed for painting.
  352. if (paintable && is<Painting::PaintableBox>(*paintable)) {
  353. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  354. paintable_box.set_offset(used_values.offset);
  355. paintable_box.set_content_size(used_values.content_width(), used_values.content_height());
  356. if (used_values.override_borders_data().has_value()) {
  357. paintable_box.set_override_borders_data(used_values.override_borders_data().value());
  358. }
  359. if (used_values.table_cell_coordinates().has_value()) {
  360. paintable_box.set_table_cell_coordinates(used_values.table_cell_coordinates().value());
  361. }
  362. if (is<Painting::PaintableWithLines>(paintable_box)) {
  363. auto& paintable_with_lines = static_cast<Painting::PaintableWithLines&>(paintable_box);
  364. for (auto& line_box : used_values.line_boxes) {
  365. for (auto& fragment : line_box.fragments())
  366. paintable_with_lines.add_fragment(fragment);
  367. }
  368. paintables_with_lines.append(paintable_with_lines);
  369. }
  370. if (used_values.computed_svg_transforms().has_value() && is<Painting::SVGGraphicsPaintable>(paintable_box)) {
  371. auto& svg_graphics_paintable = static_cast<Painting::SVGGraphicsPaintable&>(paintable_box);
  372. svg_graphics_paintable.set_computed_transforms(*used_values.computed_svg_transforms());
  373. }
  374. if (used_values.computed_svg_path().has_value() && is<Painting::SVGPathPaintable>(paintable_box)) {
  375. auto& svg_geometry_paintable = static_cast<Painting::SVGPathPaintable&>(paintable_box);
  376. svg_geometry_paintable.set_computed_path(move(*used_values.computed_svg_path()));
  377. }
  378. }
  379. }
  380. resolve_relative_positions(paintables_with_lines);
  381. // Make a pass over all the line boxes to:
  382. // - Measure absolute rect of each line box.
  383. // - Collect all text nodes, so we can create paintables for them later.
  384. for (auto& paintable_with_lines : paintables_with_lines) {
  385. for (auto& fragment : paintable_with_lines.fragments()) {
  386. if (fragment.layout_node().is_text_node())
  387. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  388. }
  389. }
  390. for (auto* text_node : text_nodes)
  391. text_node->set_paintable(text_node->create_paintable());
  392. build_paint_tree(root);
  393. // Measure overflow in scroll containers.
  394. for (auto& it : used_values_per_layout_node) {
  395. auto& used_values = *it.value;
  396. if (!used_values.node().is_box())
  397. continue;
  398. auto const& box = static_cast<Layout::Box const&>(used_values.node());
  399. measure_scrollable_overflow(box);
  400. }
  401. resolve_border_radii();
  402. resolve_box_shadow_data();
  403. resolve_text_shadows(paintables_with_lines);
  404. for (auto& it : used_values_per_layout_node) {
  405. auto& used_values = *it.value;
  406. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  407. auto* paintable = node.paintable();
  408. if (paintable && is<Painting::InlinePaintable>(*paintable)) {
  409. auto& inline_paintable = static_cast<Painting::InlinePaintable&>(*paintable);
  410. // FIXME: Marking fragments contained by inline node is a hack required to skip them while painting
  411. // PaintableWithLines content.
  412. inline_paintable.mark_contained_fragments();
  413. }
  414. }
  415. }
  416. void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* containing_block_used_values)
  417. {
  418. m_node = &node;
  419. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  420. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  421. // If `node` has definite values for min/max-width or min/max-height and a definite
  422. // preferred size in the same axis, we clamp the preferred size here as well.
  423. //
  424. // There are additional cases where CSS considers values to be definite. We model all of
  425. // those by having our engine consider sizes to be definite *once they are assigned to
  426. // the UsedValues by calling set_content_width() or set_content_height().
  427. auto const& computed_values = node.computed_values();
  428. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  429. // box-sizing: content-box and/or automatic size don't require any adjustment.
  430. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  431. return unadjusted_pixels;
  432. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  433. CSSPixels border_and_padding;
  434. if (width) {
  435. border_and_padding = computed_values.border_left().width
  436. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  437. + computed_values.border_right().width
  438. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  439. } else {
  440. border_and_padding = computed_values.border_top().width
  441. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  442. + computed_values.border_bottom().width
  443. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  444. }
  445. return unadjusted_pixels - border_and_padding;
  446. };
  447. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  448. // A size that can be determined without performing layout; that is,
  449. // a <length>,
  450. // a measure of text (without consideration of line-wrapping),
  451. // a size of the initial containing block,
  452. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  453. auto containing_block_has_definite_size = containing_block_used_values ? (width ? containing_block_used_values->has_definite_width() : containing_block_used_values->has_definite_height()) : false;
  454. if (size.is_auto()) {
  455. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  456. if (width
  457. && !node.is_floating()
  458. && !node.is_absolutely_positioned()
  459. && node.display().is_block_outside()
  460. && node.parent()
  461. && !node.parent()->is_floating()
  462. && (node.parent()->display().is_flow_root_inside()
  463. || node.parent()->display().is_flow_inside())) {
  464. if (containing_block_has_definite_size) {
  465. CSSPixels available_width = containing_block_used_values->content_width();
  466. resolved_definite_size = available_width
  467. - margin_left
  468. - margin_right
  469. - padding_left
  470. - padding_right
  471. - border_left
  472. - border_right;
  473. return true;
  474. }
  475. return false;
  476. }
  477. return false;
  478. }
  479. if (size.is_calculated()) {
  480. if (size.calculated().contains_percentage()) {
  481. if (!containing_block_has_definite_size)
  482. return false;
  483. auto containing_block_size_as_length = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  484. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node), size, width);
  485. return true;
  486. }
  487. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  488. return true;
  489. }
  490. if (size.is_length()) {
  491. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  492. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  493. return true;
  494. }
  495. if (size.is_percentage()) {
  496. if (containing_block_has_definite_size) {
  497. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  498. resolved_definite_size = adjust_for_box_sizing(containing_block_size.scaled(size.percentage().as_fraction()), size, width);
  499. return true;
  500. }
  501. return false;
  502. }
  503. // FIXME: Determine if calc() value is definite.
  504. return false;
  505. };
  506. CSSPixels min_width = 0;
  507. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  508. CSSPixels max_width = 0;
  509. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  510. CSSPixels min_height = 0;
  511. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  512. CSSPixels max_height = 0;
  513. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  514. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  515. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  516. if (m_has_definite_width) {
  517. if (has_definite_min_width)
  518. m_content_width = max(min_width, m_content_width);
  519. if (has_definite_max_width)
  520. m_content_width = min(max_width, m_content_width);
  521. }
  522. if (m_has_definite_height) {
  523. if (has_definite_min_height)
  524. m_content_height = max(min_height, m_content_height);
  525. if (has_definite_max_height)
  526. m_content_height = min(max_height, m_content_height);
  527. }
  528. }
  529. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  530. {
  531. VERIFY(!width.might_be_saturated());
  532. if (width < 0) {
  533. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  534. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  535. width = 0;
  536. }
  537. m_content_width = width;
  538. m_has_definite_width = true;
  539. }
  540. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  541. {
  542. VERIFY(!height.might_be_saturated());
  543. if (height < 0) {
  544. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  545. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  546. height = 0;
  547. }
  548. m_content_height = height;
  549. m_has_definite_height = true;
  550. }
  551. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  552. {
  553. m_content_width = width;
  554. }
  555. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  556. {
  557. m_content_height = height;
  558. }
  559. AvailableSize LayoutState::UsedValues::available_width_inside() const
  560. {
  561. if (width_constraint == SizeConstraint::MinContent)
  562. return AvailableSize::make_min_content();
  563. if (width_constraint == SizeConstraint::MaxContent)
  564. return AvailableSize::make_max_content();
  565. if (has_definite_width())
  566. return AvailableSize::make_definite(m_content_width);
  567. return AvailableSize::make_indefinite();
  568. }
  569. AvailableSize LayoutState::UsedValues::available_height_inside() const
  570. {
  571. if (height_constraint == SizeConstraint::MinContent)
  572. return AvailableSize::make_min_content();
  573. if (height_constraint == SizeConstraint::MaxContent)
  574. return AvailableSize::make_max_content();
  575. if (has_definite_height())
  576. return AvailableSize::make_definite(m_content_height);
  577. return AvailableSize::make_indefinite();
  578. }
  579. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  580. {
  581. auto inner_width = available_width_inside();
  582. auto inner_height = available_height_inside();
  583. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  584. inner_width = outer_space.width;
  585. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  586. inner_height = outer_space.height;
  587. return AvailableSpace(inner_width, inner_height);
  588. }
  589. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  590. {
  591. set_content_x(new_offset.x());
  592. set_content_y(new_offset.y());
  593. }
  594. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  595. {
  596. offset.set_x(x);
  597. }
  598. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  599. {
  600. offset.set_y(y);
  601. }
  602. void LayoutState::UsedValues::set_indefinite_content_width()
  603. {
  604. m_has_definite_width = false;
  605. }
  606. void LayoutState::UsedValues::set_indefinite_content_height()
  607. {
  608. m_has_definite_height = false;
  609. }
  610. }