LayoutState.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. CSSPixels box_baseline(LayoutState const& state, Box const& box)
  90. {
  91. auto const& box_state = state.get(box);
  92. // https://www.w3.org/TR/CSS2/visudet.html#propdef-vertical-align
  93. auto const& vertical_align = box.computed_values().vertical_align();
  94. if (vertical_align.has<CSS::VerticalAlign>()) {
  95. switch (vertical_align.get<CSS::VerticalAlign>()) {
  96. case CSS::VerticalAlign::Top:
  97. // Top: Align the top of the aligned subtree with the top of the line box.
  98. return box_state.border_box_top();
  99. case CSS::VerticalAlign::Bottom:
  100. // Bottom: Align the bottom of the aligned subtree with the bottom of the line box.
  101. return box_state.content_height() + box_state.margin_box_top();
  102. case CSS::VerticalAlign::TextTop:
  103. // TextTop: Align the top of the box with the top of the parent's content area (see 10.6.1).
  104. return box.computed_values().font_size();
  105. case CSS::VerticalAlign::TextBottom:
  106. // TextTop: Align the bottom of the box with the bottom of the parent's content area (see 10.6.1).
  107. return box_state.content_height() - (box.containing_block()->font().pixel_metrics().descent * 2);
  108. default:
  109. break;
  110. }
  111. }
  112. if (!box_state.line_boxes.is_empty())
  113. return box_state.margin_box_top() + box_state.offset.y() + box_state.line_boxes.last().baseline();
  114. if (box.has_children() && !box.children_are_inline()) {
  115. auto const* child_box = box.last_child_of_type<Box>();
  116. VERIFY(child_box);
  117. return box_baseline(state, *child_box);
  118. }
  119. return box_state.margin_box_height();
  120. }
  121. CSSPixelRect margin_box_rect(Box const& box, LayoutState const& state)
  122. {
  123. auto const& box_state = state.get(box);
  124. auto rect = CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  125. rect.set_x(rect.x() - box_state.margin_box_left());
  126. rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
  127. rect.set_y(rect.y() - box_state.margin_box_top());
  128. rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
  129. return rect;
  130. }
  131. CSSPixelRect border_box_rect(Box const& box, LayoutState const& state)
  132. {
  133. auto const& box_state = state.get(box);
  134. auto rect = CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  135. rect.set_x(rect.x() - box_state.border_box_left());
  136. rect.set_width(rect.width() + box_state.border_box_left() + box_state.border_box_right());
  137. rect.set_y(rect.y() - box_state.border_box_top());
  138. rect.set_height(rect.height() + box_state.border_box_top() + box_state.border_box_bottom());
  139. return rect;
  140. }
  141. CSSPixelRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  142. {
  143. auto rect = border_box_rect(box, state);
  144. if (&box == &ancestor_box)
  145. return rect;
  146. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  147. if (current == &ancestor_box)
  148. return rect;
  149. auto const& current_state = state.get(static_cast<Box const&>(*current));
  150. rect.translate_by(current_state.offset);
  151. }
  152. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  153. VERIFY_NOT_REACHED();
  154. }
  155. CSSPixelRect content_box_rect(Box const& box, LayoutState const& state)
  156. {
  157. auto const& box_state = state.get(box);
  158. return CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  159. }
  160. CSSPixelRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  161. {
  162. auto rect = content_box_rect(box, state);
  163. if (&box == &ancestor_box)
  164. return rect;
  165. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  166. if (current == &ancestor_box)
  167. return rect;
  168. auto const& current_state = state.get(static_cast<Box const&>(*current));
  169. rect.translate_by(current_state.offset);
  170. }
  171. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  172. VERIFY_NOT_REACHED();
  173. }
  174. CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
  175. {
  176. auto rect = margin_box_rect(box, state);
  177. if (&box == &ancestor_box)
  178. return rect;
  179. for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
  180. if (current == &ancestor_box)
  181. return rect;
  182. auto const& current_state = state.get(static_cast<Box const&>(*current));
  183. rect.translate_by(current_state.offset);
  184. }
  185. // If we get here, ancestor_box was not a containing block ancestor of `box`!
  186. VERIFY_NOT_REACHED();
  187. }
  188. CSSPixelRect absolute_content_rect(Box const& box, LayoutState const& state)
  189. {
  190. auto const& box_state = state.get(box);
  191. CSSPixelRect rect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
  192. for (auto* block = box.containing_block(); block; block = block->containing_block())
  193. rect.translate_by(state.get(*block).offset);
  194. return rect;
  195. }
  196. void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
  197. {
  198. m_node = &node;
  199. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  200. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  201. // If `node` has definite values for min/max-width or min/max-height and a definite
  202. // preferred size in the same axis, we clamp the preferred size here as well.
  203. //
  204. // There are additional cases where CSS considers values to be definite. We model all of
  205. // those by having our engine consider sizes to be definite *once they are assigned to
  206. // the UsedValues by calling set_content_width() or set_content_height().
  207. auto const& computed_values = node.computed_values();
  208. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  209. // box-sizing: content-box and/or automatic size don't require any adjustment.
  210. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  211. return unadjusted_pixels;
  212. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  213. CSSPixels border_and_padding;
  214. if (width) {
  215. border_and_padding = CSSPixels(computed_values.border_left().width)
  216. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  217. + CSSPixels(computed_values.border_right().width)
  218. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  219. } else {
  220. border_and_padding = CSSPixels(computed_values.border_top().width)
  221. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  222. + CSSPixels(computed_values.border_bottom().width)
  223. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  224. }
  225. return unadjusted_pixels - border_and_padding;
  226. };
  227. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  228. // A size that can be determined without performing layout; that is,
  229. // a <length>,
  230. // a measure of text (without consideration of line-wrapping),
  231. // a size of the initial containing block,
  232. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  233. 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;
  234. if (size.is_auto()) {
  235. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  236. if (width
  237. && !node.is_floating()
  238. && node.display().is_block_outside()
  239. && node.parent()
  240. && !node.parent()->is_floating()
  241. && (node.parent()->display().is_flow_root_inside()
  242. || node.parent()->display().is_flow_inside())) {
  243. if (containing_block_has_definite_size) {
  244. CSSPixels available_width = containing_block_used_values->content_width();
  245. resolved_definite_size = available_width
  246. - margin_left
  247. - margin_right
  248. - padding_left
  249. - padding_right
  250. - border_left
  251. - border_right;
  252. return true;
  253. }
  254. return false;
  255. }
  256. return false;
  257. }
  258. if (size.is_calculated()) {
  259. if (size.calculated().contains_percentage()) {
  260. if (!containing_block_has_definite_size)
  261. return false;
  262. auto containing_block_size_as_length = width
  263. ? CSS::Length::make_px(containing_block_used_values->content_width())
  264. : CSS::Length::make_px(containing_block_used_values->content_height());
  265. 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);
  266. return true;
  267. }
  268. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  269. return true;
  270. }
  271. if (size.is_length()) {
  272. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  273. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  274. return true;
  275. }
  276. if (size.is_percentage()) {
  277. if (containing_block_has_definite_size) {
  278. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  279. resolved_definite_size = adjust_for_box_sizing(containing_block_size * size.percentage().as_fraction(), size, width);
  280. return true;
  281. }
  282. return false;
  283. }
  284. // FIXME: Determine if calc() value is definite.
  285. return false;
  286. };
  287. CSSPixels min_width = 0;
  288. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  289. CSSPixels max_width = 0;
  290. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  291. CSSPixels min_height = 0;
  292. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  293. CSSPixels max_height = 0;
  294. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  295. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  296. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  297. if (m_has_definite_width) {
  298. if (has_definite_min_width)
  299. m_content_width = max(min_width, m_content_width);
  300. if (has_definite_max_width)
  301. m_content_width = min(max_width, m_content_width);
  302. }
  303. if (m_has_definite_height) {
  304. if (has_definite_min_height)
  305. m_content_height = max(min_height, m_content_height);
  306. if (has_definite_max_height)
  307. m_content_height = min(max_height, m_content_height);
  308. }
  309. }
  310. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  311. {
  312. if (width < 0) {
  313. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  314. dbgln("FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  315. width = 0;
  316. }
  317. m_content_width = width;
  318. m_has_definite_width = true;
  319. }
  320. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  321. {
  322. if (height < 0) {
  323. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  324. dbgln("FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  325. height = 0;
  326. }
  327. m_content_height = height;
  328. m_has_definite_height = true;
  329. }
  330. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  331. {
  332. m_content_width = width;
  333. }
  334. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  335. {
  336. m_content_height = height;
  337. }
  338. AvailableSize LayoutState::UsedValues::available_width_inside() const
  339. {
  340. if (width_constraint == SizeConstraint::MinContent)
  341. return AvailableSize::make_min_content();
  342. if (width_constraint == SizeConstraint::MaxContent)
  343. return AvailableSize::make_max_content();
  344. if (has_definite_width())
  345. return AvailableSize::make_definite(m_content_width);
  346. return AvailableSize::make_indefinite();
  347. }
  348. AvailableSize LayoutState::UsedValues::available_height_inside() const
  349. {
  350. if (height_constraint == SizeConstraint::MinContent)
  351. return AvailableSize::make_min_content();
  352. if (height_constraint == SizeConstraint::MaxContent)
  353. return AvailableSize::make_max_content();
  354. if (has_definite_height())
  355. return AvailableSize::make_definite(m_content_height);
  356. return AvailableSize::make_indefinite();
  357. }
  358. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  359. {
  360. auto inner_width = available_width_inside();
  361. auto inner_height = available_height_inside();
  362. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  363. inner_width = outer_space.width;
  364. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  365. inner_height = outer_space.height;
  366. return AvailableSpace(inner_width, inner_height);
  367. }
  368. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  369. {
  370. set_content_x(new_offset.x());
  371. set_content_y(new_offset.y());
  372. }
  373. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  374. {
  375. offset.set_x(x);
  376. }
  377. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  378. {
  379. offset.set_y(y);
  380. }
  381. void LayoutState::UsedValues::set_indefinite_content_width()
  382. {
  383. m_has_definite_width = false;
  384. }
  385. void LayoutState::UsedValues::set_indefinite_content_height()
  386. {
  387. m_has_definite_height = false;
  388. }
  389. }