LayoutState.cpp 31 KB

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