LayoutState.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
  55. node.box_model().padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
  56. node.box_model().border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
  57. node.box_model().margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
  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. Gfx::FloatRect margin_box_rect(Box const& box, LayoutState const& state)
  82. {
  83. auto const& box_state = state.get(box);
  84. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  85. rect.set_x(rect.x() - box_state.margin_box_left());
  86. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  87. rect.set_y(rect.y() - box_state.margin_box_top());
  88. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  89. return rect;
  90. }
  91. Gfx::FloatRect border_box_rect(Box const& box, LayoutState const& state)
  92. {
  93. auto const& box_state = state.get(box);
  94. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  95. rect.set_x(rect.x() - box_state.border_box_left());
  96. rect.set_width(rect.width() + box_state.border_box_left() + box_state.border_box_right());
  97. rect.set_y(rect.y() - box_state.border_box_top());
  98. rect.set_height(rect.height() + box_state.border_box_top() + box_state.border_box_bottom());
  99. return rect;
  100. }
  101. Gfx::FloatRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  102. {
  103. auto rect = border_box_rect(box, state);
  104. if (&box == &ancestor_box)
  105. return rect;
  106. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  107. if (current == &ancestor_box)
  108. return rect;
  109. auto const& current_state = state.get(static_cast<Box const&>(*current));
  110. rect.translate_by(current_state.offset);
  111. }
  112. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  113. VERIFY_NOT_REACHED();
  114. }
  115. Gfx::FloatRect content_box_rect(Box const& box, LayoutState const& state)
  116. {
  117. auto const& box_state = state.get(box);
  118. return Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  119. }
  120. Gfx::FloatRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  121. {
  122. auto rect = content_box_rect(box, state);
  123. if (&box == &ancestor_box)
  124. return rect;
  125. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  126. if (current == &ancestor_box)
  127. return rect;
  128. auto const& current_state = state.get(static_cast<Box const&>(*current));
  129. rect.translate_by(current_state.offset);
  130. }
  131. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  132. VERIFY_NOT_REACHED();
  133. }
  134. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  135. {
  136. auto rect = margin_box_rect(box, state);
  137. if (&box == &ancestor_box)
  138. return rect;
  139. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  140. if (current == &ancestor_box)
  141. return rect;
  142. auto const& current_state = state.get(static_cast<Box const&>(*current));
  143. rect.translate_by(current_state.offset);
  144. }
  145. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  146. VERIFY_NOT_REACHED();
  147. }
  148. Gfx::FloatRect absolute_content_rect(Box const& box, LayoutState const& state)
  149. {
  150. auto const& box_state = state.get(box);
  151. Gfx::FloatRect rect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  152. for (auto* block = box.containing_block(); block; block = block->containing_block())
  153. rect.translate_by(state.get(*block).offset);
  154. return rect;
  155. }
  156. void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
  157. {
  158. m_node = &node;
  159. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  160. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  161. // If `node` has definite values for min/max-width or min/max-height and a definite
  162. // preferred size in the same axis, we clamp the preferred size here as well.
  163. //
  164. // There are additional cases where CSS considers values to be definite. We model all of
  165. // those by having our engine consider sizes to be definite *once they are assigned to
  166. // the UsedValues by calling set_content_width() or set_content_height().
  167. auto const& computed_values = node.computed_values();
  168. auto is_definite_size = [&](CSS::Size const& size, float& resolved_definite_size, bool width) {
  169. // A size that can be determined without performing layout; that is,
  170. // a <length>,
  171. // a measure of text (without consideration of line-wrapping),
  172. // a size of the initial containing block,
  173. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  174. 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;
  175. if (size.is_auto()) {
  176. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  177. if (width
  178. && !node.is_floating()
  179. && node.display().is_block_outside()
  180. && node.parent()
  181. && !node.parent()->is_floating()
  182. && (node.parent()->display().is_flow_root_inside()
  183. || node.parent()->display().is_flow_inside())) {
  184. if (containing_block_has_definite_size) {
  185. float available_width = containing_block_used_values->content_width();
  186. resolved_definite_size = available_width
  187. - margin_left
  188. - margin_right
  189. - padding_left
  190. - padding_right
  191. - border_left
  192. - border_right;
  193. return true;
  194. }
  195. return false;
  196. }
  197. return false;
  198. }
  199. if (size.is_length() && size.length().is_calculated()) {
  200. if (size.length().calculated_style_value()->contains_percentage()) {
  201. if (!containing_block_has_definite_size)
  202. return false;
  203. auto& calc_value = *size.length().calculated_style_value();
  204. auto containing_block_size_as_length = width
  205. ? CSS::Length::make_px(containing_block_used_values->content_width())
  206. : CSS::Length::make_px(containing_block_used_values->content_height());
  207. resolved_definite_size = calc_value.resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node);
  208. return true;
  209. }
  210. resolved_definite_size = size.length().to_px(node);
  211. return true;
  212. }
  213. if (size.is_length()) {
  214. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  215. VERIFY(!size.length().is_calculated()); // Covered above.
  216. resolved_definite_size = size.length().to_px(node);
  217. return true;
  218. }
  219. if (size.is_percentage()) {
  220. if (containing_block_has_definite_size) {
  221. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  222. resolved_definite_size = containing_block_size * size.percentage().as_fraction();
  223. return true;
  224. }
  225. return false;
  226. }
  227. // FIXME: Determine if calc() value is definite.
  228. return false;
  229. };
  230. float min_width = 0;
  231. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  232. float max_width = 0;
  233. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  234. float min_height = 0;
  235. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  236. float max_height = 0;
  237. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  238. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  239. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  240. if (m_has_definite_width) {
  241. if (has_definite_min_width)
  242. m_content_width = max(min_width, m_content_width);
  243. if (has_definite_max_width)
  244. m_content_width = min(max_width, m_content_width);
  245. }
  246. if (m_has_definite_height) {
  247. if (has_definite_min_height)
  248. m_content_height = max(min_height, m_content_height);
  249. if (has_definite_max_height)
  250. m_content_height = min(max_height, m_content_height);
  251. }
  252. }
  253. void LayoutState::UsedValues::set_content_width(float width)
  254. {
  255. m_content_width = width;
  256. m_has_definite_width = true;
  257. }
  258. void LayoutState::UsedValues::set_content_height(float height)
  259. {
  260. m_content_height = height;
  261. m_has_definite_height = true;
  262. }
  263. float LayoutState::resolved_definite_width(Box const& box) const
  264. {
  265. return get(box).content_width();
  266. }
  267. float LayoutState::resolved_definite_height(Box const& box) const
  268. {
  269. return get(box).content_height();
  270. }
  271. AvailableSize LayoutState::UsedValues::available_width_inside() const
  272. {
  273. if (width_constraint == SizeConstraint::MinContent)
  274. return AvailableSize::make_min_content();
  275. if (width_constraint == SizeConstraint::MaxContent)
  276. return AvailableSize::make_max_content();
  277. if (has_definite_width())
  278. return AvailableSize::make_definite(m_content_width);
  279. return AvailableSize::make_indefinite();
  280. }
  281. AvailableSize LayoutState::UsedValues::available_height_inside() const
  282. {
  283. if (height_constraint == SizeConstraint::MinContent)
  284. return AvailableSize::make_min_content();
  285. if (height_constraint == SizeConstraint::MaxContent)
  286. return AvailableSize::make_max_content();
  287. if (has_definite_height())
  288. return AvailableSize::make_definite(m_content_height);
  289. return AvailableSize::make_indefinite();
  290. }
  291. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  292. {
  293. auto inner_width = available_width_inside();
  294. auto inner_height = available_height_inside();
  295. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  296. inner_width = outer_space.width;
  297. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  298. inner_height = outer_space.height;
  299. return AvailableSpace(inner_width, inner_height);
  300. }
  301. void LayoutState::UsedValues::set_content_offset(Gfx::FloatPoint offset)
  302. {
  303. set_content_x(offset.x());
  304. set_content_y(offset.y());
  305. }
  306. void LayoutState::UsedValues::set_content_x(float x)
  307. {
  308. offset.set_x(x);
  309. }
  310. void LayoutState::UsedValues::set_content_y(float y)
  311. {
  312. offset.set_y(y);
  313. }
  314. }