LayoutState.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. namespace Web::Layout {
  12. LayoutState::LayoutState(LayoutState const* parent)
  13. : m_parent(parent)
  14. , m_root(find_root())
  15. {
  16. }
  17. LayoutState::~LayoutState()
  18. {
  19. }
  20. LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
  21. {
  22. if (auto* used_values = used_values_per_layout_node.get(&box).value_or(nullptr))
  23. return *used_values;
  24. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  25. if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(&box).value_or(nullptr)) {
  26. auto cow_used_values = adopt_own(*new UsedValues(*ancestor_used_values));
  27. auto* cow_used_values_ptr = cow_used_values.ptr();
  28. used_values_per_layout_node.set(&box, move(cow_used_values));
  29. return *cow_used_values_ptr;
  30. }
  31. }
  32. auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
  33. auto new_used_values = adopt_own(*new UsedValues);
  34. auto* new_used_values_ptr = new_used_values.ptr();
  35. new_used_values->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  36. used_values_per_layout_node.set(&box, move(new_used_values));
  37. return *new_used_values_ptr;
  38. }
  39. LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const
  40. {
  41. if (auto const* used_values = used_values_per_layout_node.get(&box).value_or(nullptr))
  42. return *used_values;
  43. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  44. if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(&box).value_or(nullptr))
  45. return *ancestor_used_values;
  46. }
  47. auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
  48. auto new_used_values = adopt_own(*new UsedValues);
  49. auto* new_used_values_ptr = new_used_values.ptr();
  50. new_used_values->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  51. const_cast<LayoutState*>(this)->used_values_per_layout_node.set(&box, move(new_used_values));
  52. return *new_used_values_ptr;
  53. }
  54. static void measure_scrollable_overflow(LayoutState const& state, Box const& box, CSSPixels& bottom_edge, CSSPixels& right_edge)
  55. {
  56. auto const* maybe_box_state = state.used_values_per_layout_node.get(&box).value_or(nullptr);
  57. if (!maybe_box_state)
  58. return;
  59. auto const& box_state = *maybe_box_state;
  60. auto scroll_container_border_box = CSSPixelRect {
  61. box_state.offset.translated(-box_state.border_box_left(), -box_state.border_box_top()),
  62. { box_state.border_box_width(), box_state.border_box_height() }
  63. };
  64. bottom_edge = max(bottom_edge, scroll_container_border_box.bottom());
  65. right_edge = max(right_edge, scroll_container_border_box.right());
  66. if (box.children_are_inline()) {
  67. if (!box_state.line_boxes.is_empty()) {
  68. bottom_edge = max(bottom_edge, scroll_container_border_box.y() + box_state.line_boxes.last().bottom());
  69. for (auto& line_box : box_state.line_boxes) {
  70. if (line_box.fragments().is_empty())
  71. continue;
  72. right_edge = max(right_edge, scroll_container_border_box.x() + line_box.fragments().last().width());
  73. }
  74. }
  75. } else {
  76. // FIXME: Only check boxes for whom `box` is the containing block.
  77. box.for_each_child_of_type<Box>([&](Box const& child) {
  78. measure_scrollable_overflow(state, child, bottom_edge, right_edge);
  79. return IterationDecision::Continue;
  80. });
  81. }
  82. }
  83. void LayoutState::commit()
  84. {
  85. // Only the top-level LayoutState should ever be committed.
  86. VERIFY(!m_parent);
  87. HashTable<Layout::TextNode*> text_nodes;
  88. for (auto& it : used_values_per_layout_node) {
  89. auto& used_values = *it.value;
  90. auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node());
  91. // Transfer box model metrics.
  92. node.box_model().inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
  93. node.box_model().padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
  94. node.box_model().border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
  95. node.box_model().margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
  96. node.set_paintable(node.create_paintable());
  97. // For boxes, transfer all the state needed for painting.
  98. if (is<Layout::Box>(node)) {
  99. auto& box = static_cast<Layout::Box const&>(node);
  100. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  101. paintable_box.set_offset(used_values.offset);
  102. paintable_box.set_content_size(used_values.content_width(), used_values.content_height());
  103. paintable_box.set_containing_line_box_fragment(used_values.containing_line_box_fragment);
  104. if (used_values.override_borders_data().has_value()) {
  105. paintable_box.set_override_borders_data(used_values.override_borders_data().value());
  106. }
  107. if (used_values.table_cell_coordinates().has_value()) {
  108. paintable_box.set_table_cell_coordinates(used_values.table_cell_coordinates().value());
  109. }
  110. if (is<Layout::BlockContainer>(box)) {
  111. for (auto& line_box : used_values.line_boxes) {
  112. for (auto& fragment : line_box.fragments()) {
  113. if (fragment.layout_node().is_text_node())
  114. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  115. }
  116. }
  117. static_cast<Painting::PaintableWithLines&>(paintable_box).set_line_boxes(move(used_values.line_boxes));
  118. }
  119. }
  120. }
  121. // Measure overflow in scroll containers.
  122. for (auto& it : used_values_per_layout_node) {
  123. auto& used_values = *it.value;
  124. if (!used_values.node().is_box())
  125. continue;
  126. auto const& box = static_cast<Layout::Box const&>(used_values.node());
  127. if (!box.is_scroll_container())
  128. continue;
  129. CSSPixels bottom_edge = 0;
  130. CSSPixels right_edge = 0;
  131. measure_scrollable_overflow(*this, box, bottom_edge, right_edge);
  132. auto padding_box = box.paintable_box()->absolute_padding_box_rect();
  133. if (bottom_edge > padding_box.height() || right_edge > padding_box.width()) {
  134. Painting::PaintableBox::OverflowData overflow_data;
  135. overflow_data.scrollable_overflow_rect = padding_box;
  136. overflow_data.scrollable_overflow_rect.set_size(right_edge, bottom_edge);
  137. const_cast<Painting::PaintableBox&>(*box.paintable_box()).set_overflow_data(overflow_data);
  138. }
  139. }
  140. for (auto* text_node : text_nodes)
  141. text_node->set_paintable(text_node->create_paintable());
  142. }
  143. void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
  144. {
  145. m_node = &node;
  146. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  147. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  148. // If `node` has definite values for min/max-width or min/max-height and a definite
  149. // preferred size in the same axis, we clamp the preferred size here as well.
  150. //
  151. // There are additional cases where CSS considers values to be definite. We model all of
  152. // those by having our engine consider sizes to be definite *once they are assigned to
  153. // the UsedValues by calling set_content_width() or set_content_height().
  154. auto const& computed_values = node.computed_values();
  155. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  156. // box-sizing: content-box and/or automatic size don't require any adjustment.
  157. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  158. return unadjusted_pixels;
  159. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  160. CSSPixels border_and_padding;
  161. if (width) {
  162. border_and_padding = CSSPixels(computed_values.border_left().width)
  163. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  164. + CSSPixels(computed_values.border_right().width)
  165. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  166. } else {
  167. border_and_padding = CSSPixels(computed_values.border_top().width)
  168. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  169. + CSSPixels(computed_values.border_bottom().width)
  170. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  171. }
  172. return unadjusted_pixels - border_and_padding;
  173. };
  174. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  175. // A size that can be determined without performing layout; that is,
  176. // a <length>,
  177. // a measure of text (without consideration of line-wrapping),
  178. // a size of the initial containing block,
  179. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  180. 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;
  181. if (size.is_auto()) {
  182. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  183. if (width
  184. && !node.is_floating()
  185. && !node.is_absolutely_positioned()
  186. && node.display().is_block_outside()
  187. && node.parent()
  188. && !node.parent()->is_floating()
  189. && (node.parent()->display().is_flow_root_inside()
  190. || node.parent()->display().is_flow_inside())) {
  191. if (containing_block_has_definite_size) {
  192. CSSPixels available_width = containing_block_used_values->content_width();
  193. resolved_definite_size = available_width
  194. - margin_left
  195. - margin_right
  196. - padding_left
  197. - padding_right
  198. - border_left
  199. - border_right;
  200. return true;
  201. }
  202. return false;
  203. }
  204. return false;
  205. }
  206. if (size.is_calculated()) {
  207. if (size.calculated().contains_percentage()) {
  208. if (!containing_block_has_definite_size)
  209. return false;
  210. auto containing_block_size_as_length = width
  211. ? CSS::Length::make_px(containing_block_used_values->content_width())
  212. : CSS::Length::make_px(containing_block_used_values->content_height());
  213. 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);
  214. return true;
  215. }
  216. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  217. return true;
  218. }
  219. if (size.is_length()) {
  220. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  221. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  222. return true;
  223. }
  224. if (size.is_percentage()) {
  225. if (containing_block_has_definite_size) {
  226. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  227. resolved_definite_size = adjust_for_box_sizing(containing_block_size * static_cast<double>(size.percentage().as_fraction()), size, width);
  228. return true;
  229. }
  230. return false;
  231. }
  232. // FIXME: Determine if calc() value is definite.
  233. return false;
  234. };
  235. CSSPixels min_width = 0;
  236. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  237. CSSPixels max_width = 0;
  238. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  239. CSSPixels min_height = 0;
  240. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  241. CSSPixels max_height = 0;
  242. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  243. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  244. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  245. if (m_has_definite_width) {
  246. if (has_definite_min_width)
  247. m_content_width = max(min_width, m_content_width);
  248. if (has_definite_max_width)
  249. m_content_width = min(max_width, m_content_width);
  250. }
  251. if (m_has_definite_height) {
  252. if (has_definite_min_height)
  253. m_content_height = max(min_height, m_content_height);
  254. if (has_definite_max_height)
  255. m_content_height = min(max_height, m_content_height);
  256. }
  257. }
  258. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  259. {
  260. VERIFY(isfinite(width.to_double()));
  261. if (width < 0) {
  262. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  263. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  264. width = 0;
  265. }
  266. m_content_width = width;
  267. m_has_definite_width = true;
  268. }
  269. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  270. {
  271. VERIFY(isfinite(height.to_double()));
  272. if (height < 0) {
  273. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  274. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  275. height = 0;
  276. }
  277. m_content_height = height;
  278. m_has_definite_height = true;
  279. }
  280. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  281. {
  282. m_content_width = width;
  283. }
  284. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  285. {
  286. m_content_height = height;
  287. }
  288. AvailableSize LayoutState::UsedValues::available_width_inside() const
  289. {
  290. if (width_constraint == SizeConstraint::MinContent)
  291. return AvailableSize::make_min_content();
  292. if (width_constraint == SizeConstraint::MaxContent)
  293. return AvailableSize::make_max_content();
  294. if (has_definite_width())
  295. return AvailableSize::make_definite(m_content_width);
  296. return AvailableSize::make_indefinite();
  297. }
  298. AvailableSize LayoutState::UsedValues::available_height_inside() const
  299. {
  300. if (height_constraint == SizeConstraint::MinContent)
  301. return AvailableSize::make_min_content();
  302. if (height_constraint == SizeConstraint::MaxContent)
  303. return AvailableSize::make_max_content();
  304. if (has_definite_height())
  305. return AvailableSize::make_definite(m_content_height);
  306. return AvailableSize::make_indefinite();
  307. }
  308. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  309. {
  310. auto inner_width = available_width_inside();
  311. auto inner_height = available_height_inside();
  312. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  313. inner_width = outer_space.width;
  314. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  315. inner_height = outer_space.height;
  316. return AvailableSpace(inner_width, inner_height);
  317. }
  318. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  319. {
  320. set_content_x(new_offset.x());
  321. set_content_y(new_offset.y());
  322. }
  323. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  324. {
  325. offset.set_x(x);
  326. }
  327. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  328. {
  329. offset.set_y(y);
  330. }
  331. void LayoutState::UsedValues::set_indefinite_content_width()
  332. {
  333. m_has_definite_width = false;
  334. }
  335. void LayoutState::UsedValues::set_indefinite_content_height()
  336. {
  337. m_has_definite_height = false;
  338. }
  339. void LayoutState::UsedValues::set_min_content_width()
  340. {
  341. width_constraint = SizeConstraint::MinContent;
  342. m_content_width = 0;
  343. m_has_definite_height = false;
  344. }
  345. void LayoutState::UsedValues::set_max_content_width()
  346. {
  347. width_constraint = SizeConstraint::MaxContent;
  348. m_content_width = INFINITY;
  349. m_has_definite_width = false;
  350. }
  351. }