LayoutState.cpp 19 KB

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