LayoutState.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.value(), used_values.inset_right.value(), used_values.inset_bottom.value(), used_values.inset_left.value() };
  93. node.box_model().padding = { used_values.padding_top.value(), used_values.padding_right.value(), used_values.padding_bottom.value(), used_values.padding_left.value() };
  94. node.box_model().border = { used_values.border_top.value(), used_values.border_right.value(), used_values.border_bottom.value(), used_values.border_left.value() };
  95. node.box_model().margin = { used_values.margin_top.value(), used_values.margin_right.value(), used_values.margin_bottom.value(), used_values.margin_left.value() };
  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 (is<Layout::BlockContainer>(box)) {
  108. for (auto& line_box : used_values.line_boxes) {
  109. for (auto& fragment : line_box.fragments()) {
  110. if (fragment.layout_node().is_text_node())
  111. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  112. }
  113. }
  114. static_cast<Painting::PaintableWithLines&>(paintable_box).set_line_boxes(move(used_values.line_boxes));
  115. }
  116. }
  117. }
  118. // Measure overflow in scroll containers.
  119. for (auto& it : used_values_per_layout_node) {
  120. auto& used_values = *it.value;
  121. if (!used_values.node().is_box())
  122. continue;
  123. auto const& box = static_cast<Layout::Box const&>(used_values.node());
  124. if (!box.is_scroll_container())
  125. continue;
  126. CSSPixels bottom_edge = 0;
  127. CSSPixels right_edge = 0;
  128. measure_scrollable_overflow(*this, box, bottom_edge, right_edge);
  129. auto padding_box = box.paintable_box()->absolute_padding_box_rect();
  130. if (bottom_edge > padding_box.height() || right_edge > padding_box.width()) {
  131. Painting::PaintableBox::OverflowData overflow_data;
  132. overflow_data.scrollable_overflow_rect = padding_box;
  133. overflow_data.scrollable_overflow_rect.set_size(right_edge, bottom_edge);
  134. const_cast<Painting::PaintableBox&>(*box.paintable_box()).set_overflow_data(overflow_data);
  135. }
  136. }
  137. for (auto* text_node : text_nodes)
  138. text_node->set_paintable(text_node->create_paintable());
  139. }
  140. void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
  141. {
  142. m_node = &node;
  143. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  144. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  145. // If `node` has definite values for min/max-width or min/max-height and a definite
  146. // preferred size in the same axis, we clamp the preferred size here as well.
  147. //
  148. // There are additional cases where CSS considers values to be definite. We model all of
  149. // those by having our engine consider sizes to be definite *once they are assigned to
  150. // the UsedValues by calling set_content_width() or set_content_height().
  151. auto const& computed_values = node.computed_values();
  152. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  153. // box-sizing: content-box and/or automatic size don't require any adjustment.
  154. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  155. return unadjusted_pixels;
  156. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  157. CSSPixels border_and_padding;
  158. if (width) {
  159. border_and_padding = CSSPixels(computed_values.border_left().width)
  160. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  161. + CSSPixels(computed_values.border_right().width)
  162. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  163. } else {
  164. border_and_padding = CSSPixels(computed_values.border_top().width)
  165. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  166. + CSSPixels(computed_values.border_bottom().width)
  167. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  168. }
  169. return unadjusted_pixels - border_and_padding;
  170. };
  171. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  172. // A size that can be determined without performing layout; that is,
  173. // a <length>,
  174. // a measure of text (without consideration of line-wrapping),
  175. // a size of the initial containing block,
  176. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  177. 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;
  178. if (size.is_auto()) {
  179. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  180. if (width
  181. && !node.is_floating()
  182. && node.display().is_block_outside()
  183. && node.parent()
  184. && !node.parent()->is_floating()
  185. && (node.parent()->display().is_flow_root_inside()
  186. || node.parent()->display().is_flow_inside())) {
  187. if (containing_block_has_definite_size) {
  188. CSSPixels available_width = containing_block_used_values->content_width();
  189. resolved_definite_size = available_width
  190. - margin_left
  191. - margin_right
  192. - padding_left
  193. - padding_right
  194. - border_left
  195. - border_right;
  196. return true;
  197. }
  198. return false;
  199. }
  200. return false;
  201. }
  202. if (size.is_calculated()) {
  203. if (size.calculated().contains_percentage()) {
  204. if (!containing_block_has_definite_size)
  205. return false;
  206. auto containing_block_size_as_length = width
  207. ? CSS::Length::make_px(containing_block_used_values->content_width())
  208. : CSS::Length::make_px(containing_block_used_values->content_height());
  209. 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);
  210. return true;
  211. }
  212. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  213. return true;
  214. }
  215. if (size.is_length()) {
  216. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  217. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  218. return true;
  219. }
  220. if (size.is_percentage()) {
  221. if (containing_block_has_definite_size) {
  222. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  223. resolved_definite_size = adjust_for_box_sizing(containing_block_size * static_cast<double>(size.percentage().as_fraction()), size, width);
  224. return true;
  225. }
  226. return false;
  227. }
  228. // FIXME: Determine if calc() value is definite.
  229. return false;
  230. };
  231. CSSPixels min_width = 0;
  232. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  233. CSSPixels max_width = 0;
  234. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  235. CSSPixels min_height = 0;
  236. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  237. CSSPixels max_height = 0;
  238. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  239. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  240. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  241. if (m_has_definite_width) {
  242. if (has_definite_min_width)
  243. m_content_width = max(min_width, m_content_width);
  244. if (has_definite_max_width)
  245. m_content_width = min(max_width, m_content_width);
  246. }
  247. if (m_has_definite_height) {
  248. if (has_definite_min_height)
  249. m_content_height = max(min_height, m_content_height);
  250. if (has_definite_max_height)
  251. m_content_height = min(max_height, m_content_height);
  252. }
  253. }
  254. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  255. {
  256. VERIFY(isfinite(width.value()));
  257. if (width < 0) {
  258. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  259. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  260. width = 0;
  261. }
  262. m_content_width = width;
  263. m_has_definite_width = true;
  264. }
  265. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  266. {
  267. VERIFY(isfinite(height.value()));
  268. if (height < 0) {
  269. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  270. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  271. height = 0;
  272. }
  273. m_content_height = height;
  274. m_has_definite_height = true;
  275. }
  276. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  277. {
  278. m_content_width = width;
  279. }
  280. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  281. {
  282. m_content_height = height;
  283. }
  284. AvailableSize LayoutState::UsedValues::available_width_inside() const
  285. {
  286. if (width_constraint == SizeConstraint::MinContent)
  287. return AvailableSize::make_min_content();
  288. if (width_constraint == SizeConstraint::MaxContent)
  289. return AvailableSize::make_max_content();
  290. if (has_definite_width())
  291. return AvailableSize::make_definite(m_content_width);
  292. return AvailableSize::make_indefinite();
  293. }
  294. AvailableSize LayoutState::UsedValues::available_height_inside() const
  295. {
  296. if (height_constraint == SizeConstraint::MinContent)
  297. return AvailableSize::make_min_content();
  298. if (height_constraint == SizeConstraint::MaxContent)
  299. return AvailableSize::make_max_content();
  300. if (has_definite_height())
  301. return AvailableSize::make_definite(m_content_height);
  302. return AvailableSize::make_indefinite();
  303. }
  304. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  305. {
  306. auto inner_width = available_width_inside();
  307. auto inner_height = available_height_inside();
  308. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  309. inner_width = outer_space.width;
  310. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  311. inner_height = outer_space.height;
  312. return AvailableSpace(inner_width, inner_height);
  313. }
  314. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  315. {
  316. set_content_x(new_offset.x());
  317. set_content_y(new_offset.y());
  318. }
  319. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  320. {
  321. offset.set_x(x);
  322. }
  323. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  324. {
  325. offset.set_y(y);
  326. }
  327. void LayoutState::UsedValues::set_indefinite_content_width()
  328. {
  329. m_has_definite_width = false;
  330. }
  331. void LayoutState::UsedValues::set_indefinite_content_height()
  332. {
  333. m_has_definite_height = false;
  334. }
  335. void LayoutState::UsedValues::set_min_content_width()
  336. {
  337. width_constraint = SizeConstraint::MinContent;
  338. m_content_width = 0;
  339. m_has_definite_height = false;
  340. }
  341. void LayoutState::UsedValues::set_max_content_width()
  342. {
  343. width_constraint = SizeConstraint::MaxContent;
  344. m_content_width = INFINITY;
  345. m_has_definite_height = false;
  346. }
  347. }