LayoutState.cpp 17 KB

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