LayoutState.cpp 36 KB

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