LayoutState.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Layout/BlockContainer.h>
  7. #include <LibWeb/Layout/LayoutState.h>
  8. #include <LibWeb/Layout/TextNode.h>
  9. namespace Web::Layout {
  10. LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
  11. {
  12. auto serial_id = box.serial_id();
  13. if (used_values_per_layout_node[serial_id])
  14. return *used_values_per_layout_node[serial_id];
  15. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  16. if (ancestor->used_values_per_layout_node[serial_id]) {
  17. auto cow_used_values = adopt_own(*new UsedValues(*ancestor->used_values_per_layout_node[serial_id]));
  18. auto* cow_used_values_ptr = cow_used_values.ptr();
  19. used_values_per_layout_node[serial_id] = move(cow_used_values);
  20. return *cow_used_values_ptr;
  21. }
  22. }
  23. auto const* containing_block_used_values = box.is_initial_containing_block_box() ? nullptr : &get(*box.containing_block());
  24. used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues);
  25. used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  26. return *used_values_per_layout_node[serial_id];
  27. }
  28. LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const
  29. {
  30. auto serial_id = box.serial_id();
  31. if (used_values_per_layout_node[serial_id])
  32. return *used_values_per_layout_node[serial_id];
  33. for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  34. if (ancestor->used_values_per_layout_node[serial_id])
  35. return *ancestor->used_values_per_layout_node[serial_id];
  36. }
  37. auto const* containing_block_used_values = box.is_initial_containing_block_box() ? nullptr : &get(*box.containing_block());
  38. const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues);
  39. const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  40. return *used_values_per_layout_node[serial_id];
  41. }
  42. void LayoutState::commit()
  43. {
  44. // Only the top-level LayoutState should ever be committed.
  45. VERIFY(!m_parent);
  46. HashTable<Layout::TextNode*> text_nodes;
  47. for (auto& used_values_ptr : used_values_per_layout_node) {
  48. if (!used_values_ptr)
  49. continue;
  50. auto& used_values = *used_values_ptr;
  51. auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node());
  52. // Transfer box model metrics.
  53. node.box_model().inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
  54. node.box_model().padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
  55. node.box_model().border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
  56. node.box_model().margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
  57. node.set_paintable(node.create_paintable());
  58. // For boxes, transfer all the state needed for painting.
  59. if (is<Layout::Box>(node)) {
  60. auto& box = static_cast<Layout::Box const&>(node);
  61. auto& paint_box = const_cast<Painting::PaintableBox&>(*box.paint_box());
  62. paint_box.set_offset(used_values.offset);
  63. paint_box.set_content_size(used_values.content_width(), used_values.content_height());
  64. paint_box.set_overflow_data(move(used_values.overflow_data));
  65. paint_box.set_containing_line_box_fragment(used_values.containing_line_box_fragment);
  66. if (is<Layout::BlockContainer>(box)) {
  67. for (auto& line_box : used_values.line_boxes) {
  68. for (auto& fragment : line_box.fragments()) {
  69. if (fragment.layout_node().is_text_node())
  70. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  71. }
  72. }
  73. static_cast<Painting::PaintableWithLines&>(paint_box).set_line_boxes(move(used_values.line_boxes));
  74. }
  75. }
  76. }
  77. for (auto* text_node : text_nodes)
  78. text_node->set_paintable(text_node->create_paintable());
  79. }
  80. Gfx::FloatRect margin_box_rect(Box const& box, LayoutState const& state)
  81. {
  82. auto const& box_state = state.get(box);
  83. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  84. rect.set_x(rect.x() - box_state.margin_box_left());
  85. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  86. rect.set_y(rect.y() - box_state.margin_box_top());
  87. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  88. return rect;
  89. }
  90. Gfx::FloatRect border_box_rect(Box const& box, LayoutState const& state)
  91. {
  92. auto const& box_state = state.get(box);
  93. auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  94. rect.set_x(rect.x() - box_state.border_box_left());
  95. rect.set_width(rect.width() + box_state.border_box_left() + box_state.border_box_right());
  96. rect.set_y(rect.y() - box_state.border_box_top());
  97. rect.set_height(rect.height() + box_state.border_box_top() + box_state.border_box_bottom());
  98. return rect;
  99. }
  100. Gfx::FloatRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  101. {
  102. auto rect = border_box_rect(box, state);
  103. if (&box == &ancestor_box)
  104. return rect;
  105. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  106. if (current == &ancestor_box)
  107. return rect;
  108. auto const& current_state = state.get(static_cast<Box const&>(*current));
  109. rect.translate_by(current_state.offset);
  110. }
  111. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  112. VERIFY_NOT_REACHED();
  113. }
  114. Gfx::FloatRect content_box_rect(Box const& box, LayoutState const& state)
  115. {
  116. auto const& box_state = state.get(box);
  117. return Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  118. }
  119. Gfx::FloatRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  120. {
  121. auto rect = content_box_rect(box, state);
  122. if (&box == &ancestor_box)
  123. return rect;
  124. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  125. if (current == &ancestor_box)
  126. return rect;
  127. auto const& current_state = state.get(static_cast<Box const&>(*current));
  128. rect.translate_by(current_state.offset);
  129. }
  130. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  131. VERIFY_NOT_REACHED();
  132. }
  133. Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  134. {
  135. auto rect = margin_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. Gfx::FloatRect absolute_content_rect(Box const& box, LayoutState const& state)
  148. {
  149. auto const& box_state = state.get(box);
  150. Gfx::FloatRect rect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  151. for (auto* block = box.containing_block(); block; block = block->containing_block())
  152. rect.translate_by(state.get(*block).offset);
  153. return rect;
  154. }
  155. void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
  156. {
  157. m_node = &node;
  158. auto const& computed_values = node.computed_values();
  159. auto is_definite_size = [&](CSS::Size const& size, float& resolved_definite_size, bool width) {
  160. // A size that can be determined without performing layout; that is,
  161. // a <length>,
  162. // a measure of text (without consideration of line-wrapping),
  163. // a size of the initial containing block,
  164. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  165. 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;
  166. if (size.is_auto()) {
  167. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  168. if (width && node.parent() && !node.parent()->computed_values().display().is_flex_inside()) {
  169. if (containing_block_has_definite_size) {
  170. resolved_definite_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  171. return true;
  172. }
  173. return false;
  174. }
  175. return false;
  176. }
  177. if (size.is_length()) {
  178. if (size.length().is_calculated())
  179. return false;
  180. resolved_definite_size = size.length().to_px(node);
  181. return true;
  182. }
  183. if (size.is_percentage()) {
  184. if (containing_block_has_definite_size) {
  185. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  186. resolved_definite_size = containing_block_size * size.percentage().as_fraction();
  187. return true;
  188. }
  189. return false;
  190. }
  191. // FIXME: Determine if calc() value is definite.
  192. return false;
  193. };
  194. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  195. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  196. }
  197. void LayoutState::UsedValues::set_content_width(float width)
  198. {
  199. m_content_width = width;
  200. }
  201. void LayoutState::UsedValues::set_content_height(float height)
  202. {
  203. m_content_height = height;
  204. }
  205. float LayoutState::resolved_definite_width(Box const& box) const
  206. {
  207. auto const& computed_value = box.computed_values().width();
  208. if (computed_value.is_auto())
  209. return get(*box.containing_block()).content_width();
  210. if (computed_value.is_length())
  211. return get(box).content_width();
  212. auto containing_block_size = get(*box.containing_block()).content_width();
  213. return computed_value.resolved(box, CSS::Length::make_px(containing_block_size)).to_px(box);
  214. }
  215. float LayoutState::resolved_definite_height(Box const& box) const
  216. {
  217. auto const& computed_value = box.computed_values().height();
  218. if (computed_value.is_auto())
  219. return get(*box.containing_block()).content_height();
  220. if (computed_value.is_length())
  221. return get(box).content_height();
  222. auto containing_block_size = get(*box.containing_block()).content_height();
  223. return computed_value.resolved(box, CSS::Length::make_px(containing_block_size)).to_px(box);
  224. }
  225. }