LayoutState.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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/TextNode.h>
  11. #include <LibWeb/Layout/Viewport.h>
  12. namespace Web::Layout {
  13. LayoutState::LayoutState(LayoutState const* parent)
  14. : m_parent(parent)
  15. , m_root(find_root())
  16. {
  17. }
  18. LayoutState::~LayoutState()
  19. {
  20. }
  21. LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
  22. {
  23. if (auto* used_values = used_values_per_layout_node.get(&box).value_or(nullptr))
  24. return *used_values;
  25. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  26. if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(&box).value_or(nullptr)) {
  27. auto cow_used_values = adopt_own(*new UsedValues(*ancestor_used_values));
  28. auto* cow_used_values_ptr = cow_used_values.ptr();
  29. used_values_per_layout_node.set(&box, move(cow_used_values));
  30. return *cow_used_values_ptr;
  31. }
  32. }
  33. auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
  34. auto new_used_values = adopt_own(*new UsedValues);
  35. auto* new_used_values_ptr = new_used_values.ptr();
  36. new_used_values->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  37. used_values_per_layout_node.set(&box, move(new_used_values));
  38. return *new_used_values_ptr;
  39. }
  40. LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const
  41. {
  42. if (auto const* used_values = used_values_per_layout_node.get(&box).value_or(nullptr))
  43. return *used_values;
  44. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  45. if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(&box).value_or(nullptr))
  46. return *ancestor_used_values;
  47. }
  48. auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
  49. auto new_used_values = adopt_own(*new UsedValues);
  50. auto* new_used_values_ptr = new_used_values.ptr();
  51. new_used_values->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  52. const_cast<LayoutState*>(this)->used_values_per_layout_node.set(&box, move(new_used_values));
  53. return *new_used_values_ptr;
  54. }
  55. // https://www.w3.org/TR/css-overflow-3/#scrollable-overflow
  56. static CSSPixelRect measure_scrollable_overflow(Box const& box)
  57. {
  58. if (!box.paintable_box())
  59. return {};
  60. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  61. if (paintable_box.scrollable_overflow_rect().has_value())
  62. return paintable_box.scrollable_overflow_rect().value();
  63. // The scrollable overflow area is the union of:
  64. // - The scroll container’s own padding box.
  65. auto scrollable_overflow_rect = paintable_box.absolute_padding_box_rect();
  66. // - All line boxes directly contained by the scroll container.
  67. if (box.is_block_container() && box.children_are_inline()) {
  68. auto const& line_boxes = verify_cast<Painting::PaintableWithLines>(*box.paintable_box()).line_boxes();
  69. for (auto const& line_box : line_boxes) {
  70. scrollable_overflow_rect = scrollable_overflow_rect.united(line_box.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<NodeWithStyleAndBoxModelMetrics&>(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_paintable = verify_cast<Painting::PaintableWithLines>(*containing_block.paintable_box());
  126. auto const& fragment = containing_block_paintable.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::Position::Relative) {
  135. auto& inset = 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 const& line_box : paintable_with_lines.line_boxes()) {
  143. for (auto& fragment : line_box.fragments()) {
  144. auto const& fragment_node = fragment.layout_node();
  145. if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*fragment_node.parent()))
  146. continue;
  147. // Collect effective relative position offset from inline-flow parent chain.
  148. CSSPixelPoint offset;
  149. for (auto* ancestor = fragment_node.parent(); ancestor; ancestor = ancestor->parent()) {
  150. if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*ancestor))
  151. break;
  152. if (!ancestor->display().is_inline_outside() || !ancestor->display().is_flow_inside())
  153. break;
  154. if (ancestor->computed_values().position() == CSS::Position::Relative) {
  155. auto const& ancestor_node = static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*ancestor);
  156. auto const& inset = ancestor_node.box_model().inset;
  157. offset.translate_by(inset.left, inset.top);
  158. }
  159. }
  160. const_cast<LineBoxFragment&>(fragment).set_offset(fragment.offset().translated(offset));
  161. }
  162. }
  163. }
  164. }
  165. void LayoutState::commit()
  166. {
  167. // Only the top-level LayoutState should ever be committed.
  168. VERIFY(!m_parent);
  169. HashTable<Layout::TextNode*> text_nodes;
  170. Vector<Painting::PaintableWithLines&> paintables_with_lines;
  171. for (auto& it : used_values_per_layout_node) {
  172. auto& used_values = *it.value;
  173. auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node());
  174. // Transfer box model metrics.
  175. node.box_model().inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
  176. node.box_model().padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
  177. node.box_model().border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
  178. node.box_model().margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
  179. node.set_paintable(node.create_paintable());
  180. // For boxes, transfer all the state needed for painting.
  181. if (is<Layout::Box>(node)) {
  182. auto& box = static_cast<Layout::Box const&>(node);
  183. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  184. paintable_box.set_offset(used_values.offset);
  185. paintable_box.set_content_size(used_values.content_width(), used_values.content_height());
  186. if (used_values.override_borders_data().has_value()) {
  187. paintable_box.set_override_borders_data(used_values.override_borders_data().value());
  188. }
  189. if (used_values.table_cell_coordinates().has_value()) {
  190. paintable_box.set_table_cell_coordinates(used_values.table_cell_coordinates().value());
  191. }
  192. if (is<Layout::BlockContainer>(box)) {
  193. auto& paintable_with_lines = static_cast<Painting::PaintableWithLines&>(paintable_box);
  194. paintable_with_lines.set_line_boxes(move(used_values.line_boxes));
  195. paintables_with_lines.append(paintable_with_lines);
  196. }
  197. }
  198. }
  199. resolve_relative_positions(paintables_with_lines);
  200. // Make a pass over all the line boxes to:
  201. // - Measure absolute rect of each line box.
  202. // - Collect all text nodes, so we can create paintables for them later.
  203. for (auto& paintable_with_lines : paintables_with_lines) {
  204. for (auto& line_box : paintable_with_lines.line_boxes()) {
  205. CSSPixelRect line_box_absolute_rect;
  206. for (auto const& fragment : line_box.fragments()) {
  207. line_box_absolute_rect = line_box_absolute_rect.united(fragment.absolute_rect());
  208. if (fragment.layout_node().is_text_node())
  209. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  210. }
  211. const_cast<LineBox&>(line_box).set_absolute_rect(line_box_absolute_rect);
  212. }
  213. }
  214. // Measure overflow in scroll containers.
  215. for (auto& it : used_values_per_layout_node) {
  216. auto& used_values = *it.value;
  217. if (!used_values.node().is_box())
  218. continue;
  219. auto const& box = static_cast<Layout::Box const&>(used_values.node());
  220. measure_scrollable_overflow(box);
  221. }
  222. for (auto* text_node : text_nodes)
  223. text_node->set_paintable(text_node->create_paintable());
  224. }
  225. void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
  226. {
  227. m_node = &node;
  228. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  229. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  230. // If `node` has definite values for min/max-width or min/max-height and a definite
  231. // preferred size in the same axis, we clamp the preferred size here as well.
  232. //
  233. // There are additional cases where CSS considers values to be definite. We model all of
  234. // those by having our engine consider sizes to be definite *once they are assigned to
  235. // the UsedValues by calling set_content_width() or set_content_height().
  236. auto const& computed_values = node.computed_values();
  237. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  238. // box-sizing: content-box and/or automatic size don't require any adjustment.
  239. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  240. return unadjusted_pixels;
  241. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  242. CSSPixels border_and_padding;
  243. if (width) {
  244. border_and_padding = computed_values.border_left().width
  245. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  246. + computed_values.border_right().width
  247. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  248. } else {
  249. border_and_padding = computed_values.border_top().width
  250. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  251. + computed_values.border_bottom().width
  252. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  253. }
  254. return unadjusted_pixels - border_and_padding;
  255. };
  256. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  257. // A size that can be determined without performing layout; that is,
  258. // a <length>,
  259. // a measure of text (without consideration of line-wrapping),
  260. // a size of the initial containing block,
  261. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  262. 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;
  263. if (size.is_auto()) {
  264. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  265. if (width
  266. && !node.is_floating()
  267. && !node.is_absolutely_positioned()
  268. && node.display().is_block_outside()
  269. && node.parent()
  270. && !node.parent()->is_floating()
  271. && (node.parent()->display().is_flow_root_inside()
  272. || node.parent()->display().is_flow_inside())) {
  273. if (containing_block_has_definite_size) {
  274. CSSPixels available_width = containing_block_used_values->content_width();
  275. resolved_definite_size = available_width
  276. - margin_left
  277. - margin_right
  278. - padding_left
  279. - padding_right
  280. - border_left
  281. - border_right;
  282. return true;
  283. }
  284. return false;
  285. }
  286. return false;
  287. }
  288. if (size.is_calculated()) {
  289. if (size.calculated().contains_percentage()) {
  290. if (!containing_block_has_definite_size)
  291. return false;
  292. auto containing_block_size_as_length = width
  293. ? CSS::Length::make_px(containing_block_used_values->content_width())
  294. : CSS::Length::make_px(containing_block_used_values->content_height());
  295. 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);
  296. return true;
  297. }
  298. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  299. return true;
  300. }
  301. if (size.is_length()) {
  302. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  303. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  304. return true;
  305. }
  306. if (size.is_percentage()) {
  307. if (containing_block_has_definite_size) {
  308. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  309. resolved_definite_size = adjust_for_box_sizing(containing_block_size * static_cast<double>(size.percentage().as_fraction()), size, width);
  310. return true;
  311. }
  312. return false;
  313. }
  314. // FIXME: Determine if calc() value is definite.
  315. return false;
  316. };
  317. CSSPixels min_width = 0;
  318. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  319. CSSPixels max_width = 0;
  320. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  321. CSSPixels min_height = 0;
  322. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  323. CSSPixels max_height = 0;
  324. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  325. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  326. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  327. if (m_has_definite_width) {
  328. if (has_definite_min_width)
  329. m_content_width = max(min_width, m_content_width);
  330. if (has_definite_max_width)
  331. m_content_width = min(max_width, m_content_width);
  332. }
  333. if (m_has_definite_height) {
  334. if (has_definite_min_height)
  335. m_content_height = max(min_height, m_content_height);
  336. if (has_definite_max_height)
  337. m_content_height = min(max_height, m_content_height);
  338. }
  339. }
  340. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  341. {
  342. VERIFY(!width.might_be_saturated());
  343. if (width < 0) {
  344. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  345. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  346. width = 0;
  347. }
  348. m_content_width = width;
  349. m_has_definite_width = true;
  350. }
  351. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  352. {
  353. VERIFY(!height.might_be_saturated());
  354. if (height < 0) {
  355. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  356. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  357. height = 0;
  358. }
  359. m_content_height = height;
  360. m_has_definite_height = true;
  361. }
  362. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  363. {
  364. m_content_width = width;
  365. }
  366. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  367. {
  368. m_content_height = height;
  369. }
  370. AvailableSize LayoutState::UsedValues::available_width_inside() const
  371. {
  372. if (width_constraint == SizeConstraint::MinContent)
  373. return AvailableSize::make_min_content();
  374. if (width_constraint == SizeConstraint::MaxContent)
  375. return AvailableSize::make_max_content();
  376. if (has_definite_width())
  377. return AvailableSize::make_definite(m_content_width);
  378. return AvailableSize::make_indefinite();
  379. }
  380. AvailableSize LayoutState::UsedValues::available_height_inside() const
  381. {
  382. if (height_constraint == SizeConstraint::MinContent)
  383. return AvailableSize::make_min_content();
  384. if (height_constraint == SizeConstraint::MaxContent)
  385. return AvailableSize::make_max_content();
  386. if (has_definite_height())
  387. return AvailableSize::make_definite(m_content_height);
  388. return AvailableSize::make_indefinite();
  389. }
  390. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  391. {
  392. auto inner_width = available_width_inside();
  393. auto inner_height = available_height_inside();
  394. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  395. inner_width = outer_space.width;
  396. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  397. inner_height = outer_space.height;
  398. return AvailableSpace(inner_width, inner_height);
  399. }
  400. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  401. {
  402. set_content_x(new_offset.x());
  403. set_content_y(new_offset.y());
  404. }
  405. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  406. {
  407. offset.set_x(x);
  408. }
  409. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  410. {
  411. offset.set_y(y);
  412. }
  413. void LayoutState::UsedValues::set_indefinite_content_width()
  414. {
  415. m_has_definite_width = false;
  416. }
  417. void LayoutState::UsedValues::set_indefinite_content_height()
  418. {
  419. m_has_definite_height = false;
  420. }
  421. }