LayoutState.cpp 27 KB

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