LayoutState.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/AvailableSpace.h>
  7. #include <LibWeb/Layout/BlockContainer.h>
  8. #include <LibWeb/Layout/LayoutState.h>
  9. #include <LibWeb/Layout/TextNode.h>
  10. namespace Web::Layout {
  11. LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
  12. {
  13. auto serial_id = box.serial_id();
  14. if (used_values_per_layout_node[serial_id])
  15. return *used_values_per_layout_node[serial_id];
  16. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  17. if (ancestor->used_values_per_layout_node[serial_id]) {
  18. auto cow_used_values = adopt_own(*new UsedValues(*ancestor->used_values_per_layout_node[serial_id]));
  19. auto* cow_used_values_ptr = cow_used_values.ptr();
  20. used_values_per_layout_node[serial_id] = move(cow_used_values);
  21. return *cow_used_values_ptr;
  22. }
  23. }
  24. auto const* containing_block_used_values = box.is_initial_containing_block_box() ? nullptr : &get(*box.containing_block());
  25. used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues);
  26. used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  27. return *used_values_per_layout_node[serial_id];
  28. }
  29. LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const
  30. {
  31. auto serial_id = box.serial_id();
  32. if (used_values_per_layout_node[serial_id])
  33. return *used_values_per_layout_node[serial_id];
  34. for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  35. if (ancestor->used_values_per_layout_node[serial_id])
  36. return *ancestor->used_values_per_layout_node[serial_id];
  37. }
  38. auto const* containing_block_used_values = box.is_initial_containing_block_box() ? nullptr : &get(*box.containing_block());
  39. const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues);
  40. const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  41. return *used_values_per_layout_node[serial_id];
  42. }
  43. void LayoutState::commit()
  44. {
  45. // Only the top-level LayoutState should ever be committed.
  46. VERIFY(!m_parent);
  47. HashTable<Layout::TextNode*> text_nodes;
  48. for (auto& used_values_ptr : used_values_per_layout_node) {
  49. if (!used_values_ptr)
  50. continue;
  51. auto& used_values = *used_values_ptr;
  52. auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node());
  53. // Transfer box model metrics.
  54. node.box_model().inset = { used_values.inset_top.value(), used_values.inset_right.value(), used_values.inset_bottom.value(), used_values.inset_left.value() };
  55. node.box_model().padding = { used_values.padding_top.value(), used_values.padding_right.value(), used_values.padding_bottom.value(), used_values.padding_left.value() };
  56. node.box_model().border = { used_values.border_top.value(), used_values.border_right.value(), used_values.border_bottom.value(), used_values.border_left.value() };
  57. node.box_model().margin = { used_values.margin_top.value(), used_values.margin_right.value(), used_values.margin_bottom.value(), used_values.margin_left.value() };
  58. node.set_paintable(node.create_paintable());
  59. // For boxes, transfer all the state needed for painting.
  60. if (is<Layout::Box>(node)) {
  61. auto& box = static_cast<Layout::Box const&>(node);
  62. auto& paint_box = const_cast<Painting::PaintableBox&>(*box.paint_box());
  63. paint_box.set_offset(used_values.offset);
  64. paint_box.set_content_size(used_values.content_width(), used_values.content_height());
  65. paint_box.set_overflow_data(move(used_values.overflow_data));
  66. paint_box.set_containing_line_box_fragment(used_values.containing_line_box_fragment);
  67. if (is<Layout::BlockContainer>(box)) {
  68. for (auto& line_box : used_values.line_boxes) {
  69. for (auto& fragment : line_box.fragments()) {
  70. if (fragment.layout_node().is_text_node())
  71. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  72. }
  73. }
  74. static_cast<Painting::PaintableWithLines&>(paint_box).set_line_boxes(move(used_values.line_boxes));
  75. }
  76. }
  77. }
  78. for (auto* text_node : text_nodes)
  79. text_node->set_paintable(text_node->create_paintable());
  80. }
  81. CSSPixels box_baseline(LayoutState const& state, Box const& box)
  82. {
  83. auto const& box_state = state.get(box);
  84. // https://www.w3.org/TR/CSS2/visudet.html#propdef-vertical-align
  85. auto const& vertical_align = box.computed_values().vertical_align();
  86. if (vertical_align.has<CSS::VerticalAlign>()) {
  87. switch (vertical_align.get<CSS::VerticalAlign>()) {
  88. case CSS::VerticalAlign::Top:
  89. // Top: Align the top of the aligned subtree with the top of the line box.
  90. return box_state.border_box_top();
  91. case CSS::VerticalAlign::Bottom:
  92. // Bottom: Align the bottom of the aligned subtree with the bottom of the line box.
  93. return box_state.content_height() + box_state.border_box_bottom();
  94. case CSS::VerticalAlign::TextTop:
  95. // TextTop: Align the top of the box with the top of the parent's content area (see 10.6.1).
  96. return box.computed_values().font_size();
  97. case CSS::VerticalAlign::TextBottom:
  98. // TextTop: Align the bottom of the box with the bottom of the parent's content area (see 10.6.1).
  99. return box_state.content_height() - (box.containing_block()->font().pixel_metrics().descent * 2);
  100. default:
  101. break;
  102. }
  103. }
  104. if (!box_state.line_boxes.is_empty())
  105. return box_state.border_box_top() + box_state.offset.y() + box_state.line_boxes.last().baseline();
  106. if (box.has_children() && !box.children_are_inline()) {
  107. auto const* child_box = box.last_child_of_type<Box>();
  108. VERIFY(child_box);
  109. return box_baseline(state, *child_box);
  110. }
  111. return box_state.border_box_height();
  112. }
  113. CSSPixelRect margin_box_rect(Box const& box, LayoutState const& state)
  114. {
  115. auto const& box_state = state.get(box);
  116. auto rect = CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  117. rect.set_x(rect.x() - box_state.margin_box_left());
  118. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  119. rect.set_y(rect.y() - box_state.margin_box_top());
  120. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  121. return rect;
  122. }
  123. CSSPixelRect border_box_rect(Box const& box, LayoutState const& state)
  124. {
  125. auto const& box_state = state.get(box);
  126. auto rect = CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  127. rect.set_x(rect.x() - box_state.border_box_left());
  128. rect.set_width(rect.width() + box_state.border_box_left() + box_state.border_box_right());
  129. rect.set_y(rect.y() - box_state.border_box_top());
  130. rect.set_height(rect.height() + box_state.border_box_top() + box_state.border_box_bottom());
  131. return rect;
  132. }
  133. CSSPixelRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  134. {
  135. auto rect = border_box_rect(box, state);
  136. if (&box == &ancestor_box)
  137. return rect;
  138. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  139. if (current == &ancestor_box)
  140. return rect;
  141. auto const& current_state = state.get(static_cast<Box const&>(*current));
  142. rect.translate_by(current_state.offset);
  143. }
  144. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  145. VERIFY_NOT_REACHED();
  146. }
  147. CSSPixelRect content_box_rect(Box const& box, LayoutState const& state)
  148. {
  149. auto const& box_state = state.get(box);
  150. return CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  151. }
  152. CSSPixelRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  153. {
  154. auto rect = content_box_rect(box, state);
  155. if (&box == &ancestor_box)
  156. return rect;
  157. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  158. if (current == &ancestor_box)
  159. return rect;
  160. auto const& current_state = state.get(static_cast<Box const&>(*current));
  161. rect.translate_by(current_state.offset);
  162. }
  163. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  164. VERIFY_NOT_REACHED();
  165. }
  166. CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  167. {
  168. auto rect = margin_box_rect(box, state);
  169. if (&box == &ancestor_box)
  170. return rect;
  171. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  172. if (current == &ancestor_box)
  173. return rect;
  174. auto const& current_state = state.get(static_cast<Box const&>(*current));
  175. rect.translate_by(current_state.offset);
  176. }
  177. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  178. VERIFY_NOT_REACHED();
  179. }
  180. CSSPixelRect absolute_content_rect(Box const& box, LayoutState const& state)
  181. {
  182. auto const& box_state = state.get(box);
  183. CSSPixelRect rect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  184. for (auto* block = box.containing_block(); block; block = block->containing_block())
  185. rect.translate_by(state.get(*block).offset);
  186. return rect;
  187. }
  188. void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
  189. {
  190. m_node = &node;
  191. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  192. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  193. // If `node` has definite values for min/max-width or min/max-height and a definite
  194. // preferred size in the same axis, we clamp the preferred size here as well.
  195. //
  196. // There are additional cases where CSS considers values to be definite. We model all of
  197. // those by having our engine consider sizes to be definite *once they are assigned to
  198. // the UsedValues by calling set_content_width() or set_content_height().
  199. auto const& computed_values = node.computed_values();
  200. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  201. // A size that can be determined without performing layout; that is,
  202. // a <length>,
  203. // a measure of text (without consideration of line-wrapping),
  204. // a size of the initial containing block,
  205. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  206. 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;
  207. if (size.is_auto()) {
  208. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  209. if (width
  210. && !node.is_floating()
  211. && node.display().is_block_outside()
  212. && node.parent()
  213. && !node.parent()->is_floating()
  214. && (node.parent()->display().is_flow_root_inside()
  215. || node.parent()->display().is_flow_inside())) {
  216. if (containing_block_has_definite_size) {
  217. CSSPixels available_width = containing_block_used_values->content_width();
  218. resolved_definite_size = available_width
  219. - margin_left
  220. - margin_right
  221. - padding_left
  222. - padding_right
  223. - border_left
  224. - border_right;
  225. return true;
  226. }
  227. return false;
  228. }
  229. return false;
  230. }
  231. if (size.is_length() && size.length().is_calculated()) {
  232. if (size.length().calculated_style_value()->contains_percentage()) {
  233. if (!containing_block_has_definite_size)
  234. return false;
  235. auto& calc_value = *size.length().calculated_style_value();
  236. auto containing_block_size_as_length = width
  237. ? CSS::Length::make_px(containing_block_used_values->content_width())
  238. : CSS::Length::make_px(containing_block_used_values->content_height());
  239. resolved_definite_size = calc_value.resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node);
  240. return true;
  241. }
  242. resolved_definite_size = size.length().to_px(node);
  243. return true;
  244. }
  245. if (size.is_length()) {
  246. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  247. VERIFY(!size.length().is_calculated()); // Covered above.
  248. resolved_definite_size = size.length().to_px(node);
  249. return true;
  250. }
  251. if (size.is_percentage()) {
  252. if (containing_block_has_definite_size) {
  253. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  254. resolved_definite_size = containing_block_size * size.percentage().as_fraction();
  255. return true;
  256. }
  257. return false;
  258. }
  259. // FIXME: Determine if calc() value is definite.
  260. return false;
  261. };
  262. CSSPixels min_width = 0;
  263. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  264. CSSPixels max_width = 0;
  265. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  266. CSSPixels min_height = 0;
  267. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  268. CSSPixels max_height = 0;
  269. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  270. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  271. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  272. if (m_has_definite_width) {
  273. if (has_definite_min_width)
  274. m_content_width = max(min_width, m_content_width);
  275. if (has_definite_max_width)
  276. m_content_width = min(max_width, m_content_width);
  277. }
  278. if (m_has_definite_height) {
  279. if (has_definite_min_height)
  280. m_content_height = max(min_height, m_content_height);
  281. if (has_definite_max_height)
  282. m_content_height = min(max_height, m_content_height);
  283. }
  284. }
  285. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  286. {
  287. m_content_width = width;
  288. m_has_definite_width = true;
  289. }
  290. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  291. {
  292. m_content_height = height;
  293. m_has_definite_height = true;
  294. }
  295. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  296. {
  297. m_content_width = width;
  298. }
  299. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  300. {
  301. m_content_height = height;
  302. }
  303. CSSPixels LayoutState::resolved_definite_width(Box const& box) const
  304. {
  305. return get(box).content_width();
  306. }
  307. CSSPixels LayoutState::resolved_definite_height(Box const& box) const
  308. {
  309. return get(box).content_height();
  310. }
  311. AvailableSize LayoutState::UsedValues::available_width_inside() const
  312. {
  313. if (width_constraint == SizeConstraint::MinContent)
  314. return AvailableSize::make_min_content();
  315. if (width_constraint == SizeConstraint::MaxContent)
  316. return AvailableSize::make_max_content();
  317. if (has_definite_width())
  318. return AvailableSize::make_definite(m_content_width);
  319. return AvailableSize::make_indefinite();
  320. }
  321. AvailableSize LayoutState::UsedValues::available_height_inside() const
  322. {
  323. if (height_constraint == SizeConstraint::MinContent)
  324. return AvailableSize::make_min_content();
  325. if (height_constraint == SizeConstraint::MaxContent)
  326. return AvailableSize::make_max_content();
  327. if (has_definite_height())
  328. return AvailableSize::make_definite(m_content_height);
  329. return AvailableSize::make_indefinite();
  330. }
  331. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  332. {
  333. auto inner_width = available_width_inside();
  334. auto inner_height = available_height_inside();
  335. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  336. inner_width = outer_space.width;
  337. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  338. inner_height = outer_space.height;
  339. return AvailableSpace(inner_width, inner_height);
  340. }
  341. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  342. {
  343. set_content_x(new_offset.x());
  344. set_content_y(new_offset.y());
  345. }
  346. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  347. {
  348. offset.set_x(x);
  349. }
  350. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  351. {
  352. offset.set_y(y);
  353. }
  354. }