LayoutState.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * Copyright (c) 2022-2024, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <LibWeb/DOM/ShadowRoot.h>
  9. #include <LibWeb/Layout/AvailableSpace.h>
  10. #include <LibWeb/Layout/BlockContainer.h>
  11. #include <LibWeb/Layout/InlineNode.h>
  12. #include <LibWeb/Layout/LayoutState.h>
  13. #include <LibWeb/Layout/Viewport.h>
  14. #include <LibWeb/Painting/InlinePaintable.h>
  15. #include <LibWeb/Painting/SVGPathPaintable.h>
  16. #include <LibWeb/Painting/SVGSVGPaintable.h>
  17. #include <LibWeb/Painting/TextPaintable.h>
  18. namespace Web::Layout {
  19. LayoutState::LayoutState(LayoutState const* parent)
  20. : m_parent(parent)
  21. , m_root(find_root())
  22. {
  23. }
  24. LayoutState::~LayoutState()
  25. {
  26. }
  27. LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyle const& node)
  28. {
  29. if (auto* used_values = used_values_per_layout_node.get(node).value_or(nullptr))
  30. return *used_values;
  31. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  32. if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(node).value_or(nullptr)) {
  33. auto cow_used_values = adopt_own(*new UsedValues(*ancestor_used_values));
  34. auto* cow_used_values_ptr = cow_used_values.ptr();
  35. used_values_per_layout_node.set(node, move(cow_used_values));
  36. return *cow_used_values_ptr;
  37. }
  38. }
  39. auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
  40. auto new_used_values = adopt_own(*new UsedValues);
  41. auto* new_used_values_ptr = new_used_values.ptr();
  42. new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values);
  43. used_values_per_layout_node.set(node, move(new_used_values));
  44. return *new_used_values_ptr;
  45. }
  46. LayoutState::UsedValues const& LayoutState::get(NodeWithStyle const& node) const
  47. {
  48. if (auto const* used_values = used_values_per_layout_node.get(node).value_or(nullptr))
  49. return *used_values;
  50. for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  51. if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(node).value_or(nullptr))
  52. return *ancestor_used_values;
  53. }
  54. auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
  55. auto new_used_values = adopt_own(*new UsedValues);
  56. auto* new_used_values_ptr = new_used_values.ptr();
  57. new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values);
  58. const_cast<LayoutState*>(this)->used_values_per_layout_node.set(node, move(new_used_values));
  59. return *new_used_values_ptr;
  60. }
  61. // https://www.w3.org/TR/css-overflow-3/#scrollable-overflow
  62. static CSSPixelRect measure_scrollable_overflow(Box const& box)
  63. {
  64. if (!box.paintable_box())
  65. return {};
  66. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  67. if (paintable_box.scrollable_overflow_rect().has_value())
  68. return paintable_box.scrollable_overflow_rect().value();
  69. // The scrollable overflow area is the union of:
  70. // - The scroll container’s own padding box.
  71. auto scrollable_overflow_rect = paintable_box.absolute_padding_box_rect();
  72. // - All line boxes directly contained by the scroll container.
  73. if (is<Painting::PaintableWithLines>(box.paintable())) {
  74. for (auto const& fragment : static_cast<Painting::PaintableWithLines const&>(*box.paintable()).fragments()) {
  75. scrollable_overflow_rect = scrollable_overflow_rect.united(fragment.absolute_rect());
  76. }
  77. }
  78. // - The border boxes of all boxes for which it is the containing block
  79. // and whose border boxes are positioned not wholly in the negative scrollable overflow region,
  80. // FIXME: accounting for transforms by projecting each box onto the plane of the element that establishes its 3D rendering context. [CSS3-TRANSFORMS]
  81. if (!box.children_are_inline()) {
  82. box.for_each_child_of_type<Box>([&box, &scrollable_overflow_rect](Box const& child) {
  83. if (!child.paintable_box())
  84. return IterationDecision::Continue;
  85. auto child_border_box = child.paintable_box()->absolute_border_box_rect();
  86. // NOTE: Here we check that the child is not wholly in the negative scrollable overflow region.
  87. if (child_border_box.bottom() > 0 && child_border_box.right() > 0)
  88. scrollable_overflow_rect = scrollable_overflow_rect.united(child_border_box);
  89. // - The scrollable overflow areas of all of the above boxes
  90. // (including zero-area boxes and accounting for transforms as described above),
  91. // provided they themselves have overflow: visible (i.e. do not themselves trap the overflow)
  92. // and that scrollable overflow is not already clipped (e.g. by the clip property or the contain property).
  93. if (is<Viewport>(box) || child.computed_values().overflow_x() == CSS::Overflow::Visible || child.computed_values().overflow_y() == CSS::Overflow::Visible) {
  94. auto child_scrollable_overflow = measure_scrollable_overflow(child);
  95. if (is<Viewport>(box) || child.computed_values().overflow_x() == CSS::Overflow::Visible)
  96. scrollable_overflow_rect.unite_horizontally(child_scrollable_overflow);
  97. if (is<Viewport>(box) || child.computed_values().overflow_y() == CSS::Overflow::Visible)
  98. scrollable_overflow_rect.unite_vertically(child_scrollable_overflow);
  99. }
  100. return IterationDecision::Continue;
  101. });
  102. } else {
  103. box.for_each_child([&scrollable_overflow_rect](Node const& child) {
  104. if (child.paintable() && child.paintable()->is_inline_paintable()) {
  105. for (auto const& fragment : static_cast<Painting::InlinePaintable const&>(*child.paintable()).fragments())
  106. scrollable_overflow_rect = scrollable_overflow_rect.united(fragment.absolute_rect());
  107. }
  108. return IterationDecision::Continue;
  109. });
  110. }
  111. // FIXME: - The margin areas of grid item and flex item boxes for which the box establishes a containing block.
  112. // FIXME: - Additional padding added to the end-side of the scrollable overflow rectangle as necessary
  113. // to enable a scroll position that satisfies the requirements of place-content: end alignment.
  114. paintable_box.set_overflow_data(Painting::PaintableBox::OverflowData {
  115. .scrollable_overflow_rect = scrollable_overflow_rect,
  116. .has_scrollable_overflow = !paintable_box.absolute_padding_box_rect().contains(scrollable_overflow_rect),
  117. });
  118. return scrollable_overflow_rect;
  119. }
  120. void LayoutState::resolve_relative_positions()
  121. {
  122. // This function resolves relative position offsets of fragments that belong to inline paintables.
  123. // It runs *after* the paint tree has been constructed, so it modifies paintable node & fragment offsets directly.
  124. for (auto& it : used_values_per_layout_node) {
  125. auto& used_values = *it.value;
  126. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  127. auto* paintable = node.paintable();
  128. if (!paintable)
  129. continue;
  130. if (!is<Painting::InlinePaintable>(*paintable))
  131. continue;
  132. auto const& inline_paintable = static_cast<Painting::InlinePaintable&>(*paintable);
  133. for (auto& fragment : inline_paintable.fragments()) {
  134. auto const& fragment_node = fragment.layout_node();
  135. if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*fragment_node.parent()))
  136. continue;
  137. // Collect effective relative position offset from inline-flow parent chain.
  138. CSSPixelPoint offset;
  139. for (auto* ancestor = fragment_node.parent(); ancestor; ancestor = ancestor->parent()) {
  140. if (!is<Layout::NodeWithStyleAndBoxModelMetrics>(*ancestor))
  141. break;
  142. if (!ancestor->display().is_inline_outside() || !ancestor->display().is_flow_inside())
  143. break;
  144. if (ancestor->computed_values().position() == CSS::Positioning::Relative) {
  145. auto const& ancestor_node = static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*ancestor);
  146. auto const& inset = ancestor_node.box_model().inset;
  147. offset.translate_by(inset.left, inset.top);
  148. }
  149. }
  150. const_cast<Painting::PaintableFragment&>(fragment).set_offset(fragment.offset().translated(offset));
  151. }
  152. }
  153. }
  154. static void build_paint_tree(Node& node, Painting::Paintable* parent_paintable = nullptr)
  155. {
  156. Painting::Paintable* paintable = nullptr;
  157. if (node.paintable()) {
  158. paintable = const_cast<Painting::Paintable*>(node.paintable());
  159. if (parent_paintable && !paintable->forms_unconnected_subtree()) {
  160. VERIFY(!paintable->parent());
  161. parent_paintable->append_child(*paintable);
  162. }
  163. paintable->set_dom_node(node.dom_node());
  164. if (node.dom_node())
  165. node.dom_node()->set_paintable(paintable);
  166. }
  167. for (auto* child = node.first_child(); child; child = child->next_sibling()) {
  168. build_paint_tree(*child, paintable);
  169. }
  170. }
  171. void LayoutState::commit(Box& root)
  172. {
  173. // Only the top-level LayoutState should ever be committed.
  174. VERIFY(!m_parent);
  175. // NOTE: In case this is a relayout of an existing tree, we start by detaching the old paint tree
  176. // from the layout tree. This is done to ensure that we don't end up with any old-tree pointers
  177. // when text paintables shift around in the tree.
  178. root.for_each_in_inclusive_subtree([&](Layout::Node& node) {
  179. node.set_paintable(nullptr);
  180. return TraversalDecision::Continue;
  181. });
  182. root.document().for_each_shadow_including_inclusive_descendant([&](DOM::Node& node) {
  183. node.set_paintable(nullptr);
  184. return TraversalDecision::Continue;
  185. });
  186. HashTable<Layout::TextNode*> text_nodes;
  187. Vector<Painting::PaintableWithLines&> paintables_with_lines;
  188. for (auto& it : used_values_per_layout_node) {
  189. auto& used_values = *it.value;
  190. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  191. if (is<NodeWithStyleAndBoxModelMetrics>(node)) {
  192. // Transfer box model metrics.
  193. auto& box_model = static_cast<NodeWithStyleAndBoxModelMetrics&>(node).box_model();
  194. box_model.inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
  195. box_model.padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
  196. box_model.border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
  197. box_model.margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
  198. }
  199. auto paintable = node.create_paintable();
  200. node.set_paintable(paintable);
  201. // For boxes, transfer all the state needed for painting.
  202. if (paintable && is<Painting::PaintableBox>(*paintable)) {
  203. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  204. paintable_box.set_offset(used_values.offset);
  205. paintable_box.set_content_size(used_values.content_width(), used_values.content_height());
  206. if (used_values.override_borders_data().has_value()) {
  207. paintable_box.set_override_borders_data(used_values.override_borders_data().value());
  208. }
  209. if (used_values.table_cell_coordinates().has_value()) {
  210. paintable_box.set_table_cell_coordinates(used_values.table_cell_coordinates().value());
  211. }
  212. if (is<Painting::PaintableWithLines>(paintable_box)) {
  213. auto& paintable_with_lines = static_cast<Painting::PaintableWithLines&>(paintable_box);
  214. for (auto& line_box : used_values.line_boxes) {
  215. for (auto& fragment : line_box.fragments())
  216. paintable_with_lines.add_fragment(fragment);
  217. }
  218. paintables_with_lines.append(paintable_with_lines);
  219. }
  220. if (used_values.computed_svg_transforms().has_value() && is<Painting::SVGGraphicsPaintable>(paintable_box)) {
  221. auto& svg_graphics_paintable = static_cast<Painting::SVGGraphicsPaintable&>(paintable_box);
  222. svg_graphics_paintable.set_computed_transforms(*used_values.computed_svg_transforms());
  223. }
  224. if (used_values.computed_svg_path().has_value() && is<Painting::SVGPathPaintable>(paintable_box)) {
  225. auto& svg_geometry_paintable = static_cast<Painting::SVGPathPaintable&>(paintable_box);
  226. svg_geometry_paintable.set_computed_path(move(*used_values.computed_svg_path()));
  227. }
  228. }
  229. }
  230. // Resolve relative positions for regular boxes (not line box fragments):
  231. // NOTE: This needs to occur before fragments are transferred into the corresponding inline paintables, because
  232. // after this transfer, the containing_line_box_fragment will no longer be valid.
  233. for (auto& it : used_values_per_layout_node) {
  234. auto& used_values = *it.value;
  235. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  236. if (!node.is_box())
  237. continue;
  238. auto& paintable = static_cast<Painting::PaintableBox&>(*node.paintable());
  239. CSSPixelPoint offset;
  240. if (used_values.containing_line_box_fragment.has_value()) {
  241. // Atomic inline case:
  242. // We know that `node` is an atomic inline because `containing_line_box_fragments` refers to the
  243. // line box fragment in the parent block container that contains it.
  244. auto const& containing_line_box_fragment = used_values.containing_line_box_fragment.value();
  245. auto const& containing_block = *node.containing_block();
  246. auto const& containing_block_used_values = get(containing_block);
  247. auto const& fragment = containing_block_used_values.line_boxes[containing_line_box_fragment.line_box_index].fragments()[containing_line_box_fragment.fragment_index];
  248. // The fragment has the final offset for the atomic inline, so we just need to copy it from there.
  249. offset = fragment.offset();
  250. } else {
  251. // Not an atomic inline, much simpler case.
  252. offset = used_values.offset;
  253. }
  254. // Apply relative position inset if appropriate.
  255. if (node.computed_values().position() == CSS::Positioning::Relative && is<NodeWithStyleAndBoxModelMetrics>(node)) {
  256. auto const& inset = static_cast<NodeWithStyleAndBoxModelMetrics const&>(node).box_model().inset;
  257. offset.translate_by(inset.left, inset.top);
  258. }
  259. paintable.set_offset(offset);
  260. }
  261. // Make a pass over all the line boxes to:
  262. // - Collect all text nodes, so we can create paintables for them later.
  263. // - Relocate fragments into matching inline paintables
  264. for (auto& paintable_with_lines : paintables_with_lines) {
  265. Vector<Painting::PaintableFragment> fragments_with_inline_paintables_removed;
  266. for (auto& fragment : paintable_with_lines.fragments()) {
  267. if (fragment.layout_node().is_text_node())
  268. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  269. auto find_closest_inline_paintable = [&](auto& fragment) -> Painting::InlinePaintable const* {
  270. for (auto const* parent = fragment.layout_node().parent(); parent; parent = parent->parent()) {
  271. if (is<InlineNode>(*parent))
  272. return static_cast<Painting::InlinePaintable const*>(parent->paintable());
  273. }
  274. return nullptr;
  275. };
  276. if (auto const* inline_paintable = find_closest_inline_paintable(fragment)) {
  277. const_cast<Painting::InlinePaintable*>(inline_paintable)->fragments().append(fragment);
  278. } else {
  279. fragments_with_inline_paintables_removed.append(fragment);
  280. }
  281. }
  282. paintable_with_lines.set_fragments(move(fragments_with_inline_paintables_removed));
  283. }
  284. for (auto* text_node : text_nodes) {
  285. text_node->set_paintable(text_node->create_paintable());
  286. auto* paintable = text_node->paintable();
  287. auto const& font = text_node->first_available_font();
  288. auto const glyph_height = CSSPixels::nearest_value_for(font.pixel_size());
  289. auto const css_line_thickness = [&] {
  290. auto computed_thickness = text_node->computed_values().text_decoration_thickness().resolved(*text_node, CSS::Length(1, CSS::Length::Type::Em).to_px(*text_node));
  291. if (computed_thickness.is_auto())
  292. return max(glyph_height.scaled(0.1), 1);
  293. return computed_thickness.to_px(*text_node);
  294. }();
  295. auto& text_paintable = static_cast<Painting::TextPaintable&>(*paintable);
  296. text_paintable.set_text_decoration_thickness(css_line_thickness);
  297. }
  298. build_paint_tree(root);
  299. resolve_relative_positions();
  300. // Measure overflow in scroll containers.
  301. for (auto& it : used_values_per_layout_node) {
  302. auto& used_values = *it.value;
  303. if (!used_values.node().is_box())
  304. continue;
  305. auto const& box = static_cast<Layout::Box const&>(used_values.node());
  306. measure_scrollable_overflow(box);
  307. // The scroll offset can become invalid if the scrollable overflow rectangle has changed after layout.
  308. // For example, if the scroll container has been scrolled to the very end and is then resized to become larger
  309. // (scrollable overflow rect become smaller), the scroll offset would be out of bounds.
  310. auto& paintable_box = const_cast<Painting::PaintableBox&>(*box.paintable_box());
  311. if (!paintable_box.scroll_offset().is_zero())
  312. paintable_box.set_scroll_offset(paintable_box.scroll_offset());
  313. }
  314. }
  315. void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* containing_block_used_values)
  316. {
  317. m_node = &node;
  318. m_containing_block_used_values = containing_block_used_values;
  319. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  320. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  321. // If `node` has definite values for min/max-width or min/max-height and a definite
  322. // preferred size in the same axis, we clamp the preferred size here as well.
  323. //
  324. // There are additional cases where CSS considers values to be definite. We model all of
  325. // those by having our engine consider sizes to be definite *once they are assigned to
  326. // the UsedValues by calling set_content_width() or set_content_height().
  327. auto const& computed_values = node.computed_values();
  328. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  329. // box-sizing: content-box and/or automatic size don't require any adjustment.
  330. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  331. return unadjusted_pixels;
  332. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  333. CSSPixels border_and_padding;
  334. if (width) {
  335. border_and_padding = computed_values.border_left().width
  336. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  337. + computed_values.border_right().width
  338. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  339. } else {
  340. border_and_padding = computed_values.border_top().width
  341. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  342. + computed_values.border_bottom().width
  343. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  344. }
  345. return unadjusted_pixels - border_and_padding;
  346. };
  347. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  348. // A size that can be determined without performing layout; that is,
  349. // a <length>,
  350. // a measure of text (without consideration of line-wrapping),
  351. // a size of the initial containing block,
  352. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  353. 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;
  354. if (size.is_auto()) {
  355. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  356. if (width
  357. && !node.is_floating()
  358. && !node.is_absolutely_positioned()
  359. && node.display().is_block_outside()
  360. && node.parent()
  361. && !node.parent()->is_floating()
  362. && (node.parent()->display().is_flow_root_inside()
  363. || node.parent()->display().is_flow_inside())) {
  364. if (containing_block_has_definite_size) {
  365. CSSPixels available_width = containing_block_used_values->content_width();
  366. resolved_definite_size = available_width
  367. - margin_left
  368. - margin_right
  369. - padding_left
  370. - padding_right
  371. - border_left
  372. - border_right;
  373. return true;
  374. }
  375. return false;
  376. }
  377. return false;
  378. }
  379. if (size.is_calculated()) {
  380. if (size.calculated().contains_percentage()) {
  381. if (!containing_block_has_definite_size)
  382. return false;
  383. auto containing_block_size_as_length = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  384. 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);
  385. return true;
  386. }
  387. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  388. return true;
  389. }
  390. if (size.is_length()) {
  391. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  392. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  393. return true;
  394. }
  395. if (size.is_percentage()) {
  396. if (containing_block_has_definite_size) {
  397. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  398. resolved_definite_size = adjust_for_box_sizing(containing_block_size.scaled(size.percentage().as_fraction()), size, width);
  399. return true;
  400. }
  401. return false;
  402. }
  403. return false;
  404. };
  405. CSSPixels min_width = 0;
  406. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  407. CSSPixels max_width = 0;
  408. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  409. CSSPixels min_height = 0;
  410. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  411. CSSPixels max_height = 0;
  412. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  413. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  414. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  415. // For boxes with a preferred aspect ratio and one definite size, we can infer the other size
  416. // and consider it definite since this did not require performing layout.
  417. if (is<Box>(node)) {
  418. auto const& box = static_cast<Box const&>(node);
  419. if (auto aspect_ratio = box.preferred_aspect_ratio(); aspect_ratio.has_value()) {
  420. if (m_has_definite_width && m_has_definite_height) {
  421. // Both width and height are definite.
  422. } else if (m_has_definite_width) {
  423. m_content_height = m_content_width / *aspect_ratio;
  424. m_has_definite_height = true;
  425. } else if (m_has_definite_height) {
  426. m_content_width = m_content_height * *aspect_ratio;
  427. m_has_definite_width = true;
  428. }
  429. }
  430. }
  431. if (m_has_definite_width) {
  432. if (has_definite_min_width)
  433. m_content_width = max(min_width, m_content_width);
  434. if (has_definite_max_width)
  435. m_content_width = min(max_width, m_content_width);
  436. }
  437. if (m_has_definite_height) {
  438. if (has_definite_min_height)
  439. m_content_height = max(min_height, m_content_height);
  440. if (has_definite_max_height)
  441. m_content_height = min(max_height, m_content_height);
  442. }
  443. }
  444. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  445. {
  446. VERIFY(!width.might_be_saturated());
  447. if (width < 0) {
  448. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  449. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  450. width = 0;
  451. }
  452. m_content_width = width;
  453. // FIXME: We should not do this! Definiteness of widths should be determined early,
  454. // and not changed later (except for some special cases in flex layout..)
  455. m_has_definite_width = true;
  456. }
  457. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  458. {
  459. VERIFY(!height.might_be_saturated());
  460. if (height < 0) {
  461. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  462. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  463. height = 0;
  464. }
  465. m_content_height = height;
  466. }
  467. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  468. {
  469. m_content_width = width;
  470. }
  471. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  472. {
  473. m_content_height = height;
  474. }
  475. AvailableSize LayoutState::UsedValues::available_width_inside() const
  476. {
  477. if (width_constraint == SizeConstraint::MinContent)
  478. return AvailableSize::make_min_content();
  479. if (width_constraint == SizeConstraint::MaxContent)
  480. return AvailableSize::make_max_content();
  481. if (has_definite_width())
  482. return AvailableSize::make_definite(m_content_width);
  483. return AvailableSize::make_indefinite();
  484. }
  485. AvailableSize LayoutState::UsedValues::available_height_inside() const
  486. {
  487. if (height_constraint == SizeConstraint::MinContent)
  488. return AvailableSize::make_min_content();
  489. if (height_constraint == SizeConstraint::MaxContent)
  490. return AvailableSize::make_max_content();
  491. if (has_definite_height())
  492. return AvailableSize::make_definite(m_content_height);
  493. return AvailableSize::make_indefinite();
  494. }
  495. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  496. {
  497. auto inner_width = available_width_inside();
  498. auto inner_height = available_height_inside();
  499. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  500. inner_width = outer_space.width;
  501. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  502. inner_height = outer_space.height;
  503. return AvailableSpace(inner_width, inner_height);
  504. }
  505. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  506. {
  507. set_content_x(new_offset.x());
  508. set_content_y(new_offset.y());
  509. }
  510. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  511. {
  512. offset.set_x(x);
  513. }
  514. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  515. {
  516. offset.set_y(y);
  517. }
  518. void LayoutState::UsedValues::set_indefinite_content_width()
  519. {
  520. m_has_definite_width = false;
  521. }
  522. void LayoutState::UsedValues::set_indefinite_content_height()
  523. {
  524. m_has_definite_height = false;
  525. }
  526. }