LayoutState.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. auto const& computed_values = node.computed_values();
  160. auto is_definite_size = [&](CSS::Size const& size, float& resolved_definite_size, bool width) {
  161. // A size that can be determined without performing layout; that is,
  162. // a <length>,
  163. // a measure of text (without consideration of line-wrapping),
  164. // a size of the initial containing block,
  165. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  166. 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;
  167. if (size.is_auto()) {
  168. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  169. if (width
  170. && node.display().is_block_outside()
  171. && node.parent()
  172. && !node.parent()->is_floating()
  173. && (node.parent()->display().is_flow_root_inside()
  174. || node.parent()->display().is_flow_inside())) {
  175. if (containing_block_has_definite_size) {
  176. float available_width = containing_block_used_values->content_width();
  177. resolved_definite_size = available_width
  178. - margin_left
  179. - margin_right
  180. - padding_left
  181. - padding_right
  182. - border_left
  183. - border_right;
  184. return true;
  185. }
  186. return false;
  187. }
  188. return false;
  189. }
  190. if (size.is_length() && size.length().is_calculated()) {
  191. if (width && size.length().calculated_style_value()->contains_percentage() && containing_block_has_definite_size) {
  192. auto& calc_value = *size.length().calculated_style_value();
  193. auto containing_block_width_as_length = CSS::Length::make_px(containing_block_used_values->content_width());
  194. resolved_definite_size = calc_value.resolve_length_percentage(node, containing_block_width_as_length).value_or(CSS::Length::make_auto()).to_px(node);
  195. return false;
  196. }
  197. if (size.length().calculated_style_value()->contains_percentage())
  198. return false;
  199. resolved_definite_size = size.length().to_px(node);
  200. return true;
  201. }
  202. if (size.is_length()) {
  203. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  204. VERIFY(!size.length().is_calculated()); // Covered above.
  205. resolved_definite_size = size.length().to_px(node);
  206. return true;
  207. }
  208. if (size.is_percentage()) {
  209. if (containing_block_has_definite_size) {
  210. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  211. resolved_definite_size = containing_block_size * size.percentage().as_fraction();
  212. return true;
  213. }
  214. return false;
  215. }
  216. // FIXME: Determine if calc() value is definite.
  217. return false;
  218. };
  219. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  220. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  221. }
  222. void LayoutState::UsedValues::set_content_width(float width)
  223. {
  224. m_content_width = width;
  225. m_has_definite_width = true;
  226. }
  227. void LayoutState::UsedValues::set_content_height(float height)
  228. {
  229. m_content_height = height;
  230. m_has_definite_height = true;
  231. }
  232. float LayoutState::resolved_definite_width(Box const& box) const
  233. {
  234. return get(box).content_width();
  235. }
  236. float LayoutState::resolved_definite_height(Box const& box) const
  237. {
  238. return get(box).content_height();
  239. }
  240. AvailableSize LayoutState::UsedValues::available_width_inside() const
  241. {
  242. if (width_constraint == SizeConstraint::MinContent)
  243. return AvailableSize::make_min_content();
  244. if (width_constraint == SizeConstraint::MaxContent)
  245. return AvailableSize::make_max_content();
  246. if (has_definite_width())
  247. return AvailableSize::make_definite(m_content_width);
  248. return AvailableSize::make_indefinite();
  249. }
  250. AvailableSize LayoutState::UsedValues::available_height_inside() const
  251. {
  252. if (height_constraint == SizeConstraint::MinContent)
  253. return AvailableSize::make_min_content();
  254. if (height_constraint == SizeConstraint::MaxContent)
  255. return AvailableSize::make_max_content();
  256. if (has_definite_height())
  257. return AvailableSize::make_definite(m_content_height);
  258. return AvailableSize::make_indefinite();
  259. }
  260. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  261. {
  262. auto inner_width = available_width_inside();
  263. auto inner_height = available_height_inside();
  264. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  265. inner_width = outer_space.width;
  266. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  267. inner_height = outer_space.height;
  268. return AvailableSpace(inner_width, inner_height);
  269. }
  270. void LayoutState::UsedValues::set_content_offset(Gfx::FloatPoint offset)
  271. {
  272. set_content_x(offset.x());
  273. set_content_y(offset.y());
  274. }
  275. void LayoutState::UsedValues::set_content_x(float x)
  276. {
  277. offset.set_x(x);
  278. }
  279. void LayoutState::UsedValues::set_content_y(float y)
  280. {
  281. offset.set_y(y);
  282. }
  283. }