LayoutState.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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::LayoutState(LayoutState const* parent)
  12. : m_parent(parent)
  13. , m_root(find_root())
  14. {
  15. }
  16. LayoutState::~LayoutState()
  17. {
  18. }
  19. LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
  20. {
  21. if (auto* used_values = used_values_per_layout_node.get(&box).value_or(nullptr))
  22. return *used_values;
  23. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  24. if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(&box).value_or(nullptr)) {
  25. auto cow_used_values = adopt_own(*new UsedValues(*ancestor_used_values));
  26. auto* cow_used_values_ptr = cow_used_values.ptr();
  27. used_values_per_layout_node.set(&box, move(cow_used_values));
  28. return *cow_used_values_ptr;
  29. }
  30. }
  31. auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
  32. auto new_used_values = adopt_own(*new UsedValues);
  33. auto* new_used_values_ptr = new_used_values.ptr();
  34. new_used_values->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  35. used_values_per_layout_node.set(&box, move(new_used_values));
  36. return *new_used_values_ptr;
  37. }
  38. LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const
  39. {
  40. if (auto const* used_values = used_values_per_layout_node.get(&box).value_or(nullptr))
  41. return *used_values;
  42. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  43. if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(&box).value_or(nullptr))
  44. return *ancestor_used_values;
  45. }
  46. auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
  47. auto new_used_values = adopt_own(*new UsedValues);
  48. auto* new_used_values_ptr = new_used_values.ptr();
  49. new_used_values->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
  50. const_cast<LayoutState*>(this)->used_values_per_layout_node.set(&box, move(new_used_values));
  51. return *new_used_values_ptr;
  52. }
  53. void LayoutState::commit()
  54. {
  55. // Only the top-level LayoutState should ever be committed.
  56. VERIFY(!m_parent);
  57. HashTable<Layout::TextNode*> text_nodes;
  58. for (auto& it : used_values_per_layout_node) {
  59. auto& used_values = *it.value;
  60. auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node());
  61. // Transfer box model metrics.
  62. node.box_model().inset = { used_values.inset_top.value(), used_values.inset_right.value(), used_values.inset_bottom.value(), used_values.inset_left.value() };
  63. node.box_model().padding = { used_values.padding_top.value(), used_values.padding_right.value(), used_values.padding_bottom.value(), used_values.padding_left.value() };
  64. node.box_model().border = { used_values.border_top.value(), used_values.border_right.value(), used_values.border_bottom.value(), used_values.border_left.value() };
  65. node.box_model().margin = { used_values.margin_top.value(), used_values.margin_right.value(), used_values.margin_bottom.value(), used_values.margin_left.value() };
  66. node.set_paintable(node.create_paintable());
  67. // For boxes, transfer all the state needed for painting.
  68. if (is<Layout::Box>(node)) {
  69. auto& box = static_cast<Layout::Box const&>(node);
  70. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  71. paintable_box.set_offset(used_values.offset);
  72. paintable_box.set_content_size(used_values.content_width(), used_values.content_height());
  73. paintable_box.set_overflow_data(move(used_values.overflow_data));
  74. paintable_box.set_containing_line_box_fragment(used_values.containing_line_box_fragment);
  75. if (is<Layout::BlockContainer>(box)) {
  76. for (auto& line_box : used_values.line_boxes) {
  77. for (auto& fragment : line_box.fragments()) {
  78. if (fragment.layout_node().is_text_node())
  79. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  80. }
  81. }
  82. static_cast<Painting::PaintableWithLines&>(paintable_box).set_line_boxes(move(used_values.line_boxes));
  83. }
  84. }
  85. }
  86. for (auto* text_node : text_nodes)
  87. text_node->set_paintable(text_node->create_paintable());
  88. }
  89. void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
  90. {
  91. m_node = &node;
  92. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  93. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  94. // If `node` has definite values for min/max-width or min/max-height and a definite
  95. // preferred size in the same axis, we clamp the preferred size here as well.
  96. //
  97. // There are additional cases where CSS considers values to be definite. We model all of
  98. // those by having our engine consider sizes to be definite *once they are assigned to
  99. // the UsedValues by calling set_content_width() or set_content_height().
  100. auto const& computed_values = node.computed_values();
  101. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  102. // box-sizing: content-box and/or automatic size don't require any adjustment.
  103. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  104. return unadjusted_pixels;
  105. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  106. CSSPixels border_and_padding;
  107. if (width) {
  108. border_and_padding = CSSPixels(computed_values.border_left().width)
  109. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  110. + CSSPixels(computed_values.border_right().width)
  111. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  112. } else {
  113. border_and_padding = CSSPixels(computed_values.border_top().width)
  114. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  115. + CSSPixels(computed_values.border_bottom().width)
  116. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  117. }
  118. return unadjusted_pixels - border_and_padding;
  119. };
  120. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  121. // A size that can be determined without performing layout; that is,
  122. // a <length>,
  123. // a measure of text (without consideration of line-wrapping),
  124. // a size of the initial containing block,
  125. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  126. 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;
  127. if (size.is_auto()) {
  128. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  129. if (width
  130. && !node.is_floating()
  131. && node.display().is_block_outside()
  132. && node.parent()
  133. && !node.parent()->is_floating()
  134. && (node.parent()->display().is_flow_root_inside()
  135. || node.parent()->display().is_flow_inside())) {
  136. if (containing_block_has_definite_size) {
  137. CSSPixels available_width = containing_block_used_values->content_width();
  138. resolved_definite_size = available_width
  139. - margin_left
  140. - margin_right
  141. - padding_left
  142. - padding_right
  143. - border_left
  144. - border_right;
  145. return true;
  146. }
  147. return false;
  148. }
  149. return false;
  150. }
  151. if (size.is_calculated()) {
  152. if (size.calculated().contains_percentage()) {
  153. if (!containing_block_has_definite_size)
  154. return false;
  155. auto containing_block_size_as_length = width
  156. ? CSS::Length::make_px(containing_block_used_values->content_width())
  157. : CSS::Length::make_px(containing_block_used_values->content_height());
  158. 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);
  159. return true;
  160. }
  161. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  162. return true;
  163. }
  164. if (size.is_length()) {
  165. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  166. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  167. return true;
  168. }
  169. if (size.is_percentage()) {
  170. if (containing_block_has_definite_size) {
  171. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  172. resolved_definite_size = adjust_for_box_sizing(containing_block_size * static_cast<double>(size.percentage().as_fraction()), size, width);
  173. return true;
  174. }
  175. return false;
  176. }
  177. // FIXME: Determine if calc() value is definite.
  178. return false;
  179. };
  180. CSSPixels min_width = 0;
  181. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  182. CSSPixels max_width = 0;
  183. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  184. CSSPixels min_height = 0;
  185. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  186. CSSPixels max_height = 0;
  187. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  188. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  189. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  190. if (m_has_definite_width) {
  191. if (has_definite_min_width)
  192. m_content_width = max(min_width, m_content_width);
  193. if (has_definite_max_width)
  194. m_content_width = min(max_width, m_content_width);
  195. }
  196. if (m_has_definite_height) {
  197. if (has_definite_min_height)
  198. m_content_height = max(min_height, m_content_height);
  199. if (has_definite_max_height)
  200. m_content_height = min(max_height, m_content_height);
  201. }
  202. }
  203. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  204. {
  205. if (width < 0) {
  206. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  207. dbgln("FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  208. width = 0;
  209. }
  210. m_content_width = width;
  211. m_has_definite_width = true;
  212. }
  213. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  214. {
  215. if (height < 0) {
  216. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  217. dbgln("FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  218. height = 0;
  219. }
  220. m_content_height = height;
  221. m_has_definite_height = true;
  222. }
  223. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  224. {
  225. m_content_width = width;
  226. }
  227. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  228. {
  229. m_content_height = height;
  230. }
  231. AvailableSize LayoutState::UsedValues::available_width_inside() const
  232. {
  233. if (width_constraint == SizeConstraint::MinContent)
  234. return AvailableSize::make_min_content();
  235. if (width_constraint == SizeConstraint::MaxContent)
  236. return AvailableSize::make_max_content();
  237. if (has_definite_width())
  238. return AvailableSize::make_definite(m_content_width);
  239. return AvailableSize::make_indefinite();
  240. }
  241. AvailableSize LayoutState::UsedValues::available_height_inside() const
  242. {
  243. if (height_constraint == SizeConstraint::MinContent)
  244. return AvailableSize::make_min_content();
  245. if (height_constraint == SizeConstraint::MaxContent)
  246. return AvailableSize::make_max_content();
  247. if (has_definite_height())
  248. return AvailableSize::make_definite(m_content_height);
  249. return AvailableSize::make_indefinite();
  250. }
  251. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  252. {
  253. auto inner_width = available_width_inside();
  254. auto inner_height = available_height_inside();
  255. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  256. inner_width = outer_space.width;
  257. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  258. inner_height = outer_space.height;
  259. return AvailableSpace(inner_width, inner_height);
  260. }
  261. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  262. {
  263. set_content_x(new_offset.x());
  264. set_content_y(new_offset.y());
  265. }
  266. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  267. {
  268. offset.set_x(x);
  269. }
  270. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  271. {
  272. offset.set_y(y);
  273. }
  274. void LayoutState::UsedValues::set_indefinite_content_width()
  275. {
  276. m_has_definite_width = false;
  277. }
  278. void LayoutState::UsedValues::set_indefinite_content_height()
  279. {
  280. m_has_definite_height = false;
  281. }
  282. }