LayoutState.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. 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)
  170. {
  171. Painting::BorderRadiusData bottom_left_radius_px {};
  172. Painting::BorderRadiusData bottom_right_radius_px {};
  173. Painting::BorderRadiusData top_left_radius_px {};
  174. Painting::BorderRadiusData top_right_radius_px {};
  175. bottom_left_radius_px.horizontal_radius = bottom_left_radius.horizontal_radius.to_px(node, rect.width());
  176. bottom_right_radius_px.horizontal_radius = bottom_right_radius.horizontal_radius.to_px(node, rect.width());
  177. top_left_radius_px.horizontal_radius = top_left_radius.horizontal_radius.to_px(node, rect.width());
  178. top_right_radius_px.horizontal_radius = top_right_radius.horizontal_radius.to_px(node, rect.width());
  179. bottom_left_radius_px.vertical_radius = bottom_left_radius.vertical_radius.to_px(node, rect.height());
  180. bottom_right_radius_px.vertical_radius = bottom_right_radius.vertical_radius.to_px(node, rect.height());
  181. top_left_radius_px.vertical_radius = top_left_radius.vertical_radius.to_px(node, rect.height());
  182. top_right_radius_px.vertical_radius = top_right_radius.vertical_radius.to_px(node, rect.height());
  183. // Scale overlapping curves according to https://www.w3.org/TR/css-backgrounds-3/#corner-overlap
  184. // Let f = min(Li/Si), where i ∈ {top, right, bottom, left},
  185. // Si is the sum of the two corresponding radii of the corners on side i,
  186. // and Ltop = Lbottom = the width of the box, and Lleft = Lright = the height of the box.
  187. auto l_top = rect.width();
  188. auto l_bottom = l_top;
  189. auto l_left = rect.height();
  190. auto l_right = l_left;
  191. auto s_top = (top_left_radius_px.horizontal_radius + top_right_radius_px.horizontal_radius);
  192. auto s_right = (top_right_radius_px.vertical_radius + bottom_right_radius_px.vertical_radius);
  193. auto s_bottom = (bottom_left_radius_px.horizontal_radius + bottom_right_radius_px.horizontal_radius);
  194. auto s_left = (top_left_radius_px.vertical_radius + bottom_left_radius_px.vertical_radius);
  195. CSSPixelFraction f = 1;
  196. f = (s_top != 0) ? min(f, l_top / s_top) : f;
  197. f = (s_right != 0) ? min(f, l_right / s_right) : f;
  198. f = (s_bottom != 0) ? min(f, l_bottom / s_bottom) : f;
  199. f = (s_left != 0) ? min(f, l_left / s_left) : f;
  200. // If f < 1, then all corner radii are reduced by multiplying them by f.
  201. if (f < 1) {
  202. top_left_radius_px.horizontal_radius *= f;
  203. top_left_radius_px.vertical_radius *= f;
  204. top_right_radius_px.horizontal_radius *= f;
  205. top_right_radius_px.vertical_radius *= f;
  206. bottom_right_radius_px.horizontal_radius *= f;
  207. bottom_right_radius_px.vertical_radius *= f;
  208. bottom_left_radius_px.horizontal_radius *= f;
  209. bottom_left_radius_px.vertical_radius *= f;
  210. }
  211. return Painting::BorderRadiiData { top_left_radius_px, top_right_radius_px, bottom_right_radius_px, bottom_left_radius_px };
  212. }
  213. void LayoutState::resolve_layout_dependent_properties()
  214. {
  215. // Resolves layout-dependent properties not handled during layout and stores them in the paint tree.
  216. // Properties resolved include:
  217. // - Border radii
  218. // - Box shadows
  219. // - Text shadows
  220. // - Transforms
  221. // - Transform origins
  222. for (auto& it : used_values_per_layout_node) {
  223. auto& used_values = *it.value;
  224. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  225. auto* paintable = node.paintable();
  226. auto const is_inline_paintable = paintable && paintable->is_inline_paintable();
  227. auto const is_paintable_box = paintable && paintable->is_paintable_box();
  228. auto const is_paintable_with_lines = paintable && paintable->is_paintable_with_lines();
  229. // Border radii
  230. if (is_inline_paintable) {
  231. auto& inline_paintable = static_cast<Painting::InlinePaintable&>(*paintable);
  232. auto& fragments = inline_paintable.fragments();
  233. auto const& top_left_border_radius = inline_paintable.computed_values().border_top_left_radius();
  234. auto const& top_right_border_radius = inline_paintable.computed_values().border_top_right_radius();
  235. auto const& bottom_right_border_radius = inline_paintable.computed_values().border_bottom_right_radius();
  236. auto const& bottom_left_border_radius = inline_paintable.computed_values().border_bottom_left_radius();
  237. auto containing_block_position_in_absolute_coordinates = inline_paintable.containing_block()->paintable_box()->absolute_position();
  238. for (size_t i = 0; i < fragments.size(); ++i) {
  239. auto is_first_fragment = i == 0;
  240. auto is_last_fragment = i == fragments.size() - 1;
  241. auto& fragment = fragments[i];
  242. CSSPixelRect absolute_fragment_rect { containing_block_position_in_absolute_coordinates.translated(fragment.offset()), fragment.size() };
  243. if (is_first_fragment) {
  244. auto extra_start_width = inline_paintable.box_model().padding.left;
  245. absolute_fragment_rect.translate_by(-extra_start_width, 0);
  246. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
  247. }
  248. if (is_last_fragment) {
  249. auto extra_end_width = inline_paintable.box_model().padding.right;
  250. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
  251. }
  252. 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);
  253. fragment.set_border_radii_data(border_radii_data);
  254. }
  255. }
  256. // Border radii
  257. if (is_paintable_box) {
  258. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  259. CSSPixelRect const border_rect { 0, 0, used_values.border_box_width(), used_values.border_box_height() };
  260. auto const& border_top_left_radius = node.computed_values().border_top_left_radius();
  261. auto const& border_top_right_radius = node.computed_values().border_top_right_radius();
  262. auto const& border_bottom_right_radius = node.computed_values().border_bottom_right_radius();
  263. auto const& border_bottom_left_radius = node.computed_values().border_bottom_left_radius();
  264. 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);
  265. paintable_box.set_border_radii_data(radii_data);
  266. }
  267. // Box shadows
  268. auto const& box_shadow_data = node.computed_values().box_shadow();
  269. if (!box_shadow_data.is_empty()) {
  270. Vector<Painting::ShadowData> resolved_box_shadow_data;
  271. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  272. for (auto const& layer : box_shadow_data) {
  273. resolved_box_shadow_data.empend(
  274. layer.color,
  275. layer.offset_x.to_px(node),
  276. layer.offset_y.to_px(node),
  277. layer.blur_radius.to_px(node),
  278. layer.spread_distance.to_px(node),
  279. layer.placement == CSS::ShadowPlacement::Outer ? Painting::ShadowPlacement::Outer : Painting::ShadowPlacement::Inner);
  280. }
  281. if (paintable && is<Painting::PaintableBox>(*paintable)) {
  282. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  283. paintable_box.set_box_shadow_data(move(resolved_box_shadow_data));
  284. } else if (paintable && is<Painting::InlinePaintable>(*paintable)) {
  285. auto& inline_paintable = static_cast<Painting::InlinePaintable&>(*paintable);
  286. inline_paintable.set_box_shadow_data(move(resolved_box_shadow_data));
  287. } else {
  288. VERIFY_NOT_REACHED();
  289. }
  290. }
  291. // Text shadows
  292. if (is_paintable_with_lines) {
  293. auto const& paintable_with_lines = static_cast<Painting::PaintableWithLines const&>(*paintable);
  294. for (auto const& fragment : paintable_with_lines.fragments()) {
  295. auto const& text_shadow = fragment.m_layout_node->computed_values().text_shadow();
  296. if (!text_shadow.is_empty()) {
  297. Vector<Painting::ShadowData> resolved_shadow_data;
  298. resolved_shadow_data.ensure_capacity(text_shadow.size());
  299. for (auto const& layer : text_shadow) {
  300. resolved_shadow_data.empend(
  301. layer.color,
  302. layer.offset_x.to_px(paintable_with_lines.layout_node()),
  303. layer.offset_y.to_px(paintable_with_lines.layout_node()),
  304. layer.blur_radius.to_px(paintable_with_lines.layout_node()),
  305. layer.spread_distance.to_px(paintable_with_lines.layout_node()),
  306. Painting::ShadowPlacement::Outer);
  307. }
  308. const_cast<Painting::PaintableFragment&>(fragment).set_shadows(move(resolved_shadow_data));
  309. }
  310. }
  311. }
  312. // Transform and transform origin
  313. if (is_paintable_box) {
  314. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  315. auto const& transformations = paintable_box.computed_values().transformations();
  316. if (!transformations.is_empty()) {
  317. auto matrix = Gfx::FloatMatrix4x4::identity();
  318. for (auto const& transform : transformations)
  319. matrix = matrix * transform.to_matrix(paintable_box).release_value();
  320. paintable_box.set_transform(matrix);
  321. }
  322. auto const& transform_origin = paintable_box.computed_values().transform_origin();
  323. // https://www.w3.org/TR/css-transforms-1/#transform-box
  324. auto transform_box = paintable_box.computed_values().transform_box();
  325. // For SVG elements without associated CSS layout box, the used value for content-box is fill-box and for
  326. // border-box is stroke-box.
  327. // FIXME: This currently detects any SVG element except the <svg> one. Is that correct?
  328. // And is it correct to use `else` below?
  329. if (is<Painting::SVGPaintable>(paintable_box)) {
  330. switch (transform_box) {
  331. case CSS::TransformBox::ContentBox:
  332. transform_box = CSS::TransformBox::FillBox;
  333. break;
  334. case CSS::TransformBox::BorderBox:
  335. transform_box = CSS::TransformBox::StrokeBox;
  336. break;
  337. default:
  338. break;
  339. }
  340. }
  341. // For elements with associated CSS layout box, the used value for fill-box is content-box and for
  342. // stroke-box and view-box is border-box.
  343. else {
  344. switch (transform_box) {
  345. case CSS::TransformBox::FillBox:
  346. transform_box = CSS::TransformBox::ContentBox;
  347. break;
  348. case CSS::TransformBox::StrokeBox:
  349. case CSS::TransformBox::ViewBox:
  350. transform_box = CSS::TransformBox::BorderBox;
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. CSSPixelRect reference_box = [&]() {
  357. switch (transform_box) {
  358. case CSS::TransformBox::ContentBox:
  359. // Uses the content box as reference box.
  360. // FIXME: The reference box of a table is the border box of its table wrapper box, not its table box.
  361. return paintable_box.absolute_rect();
  362. case CSS::TransformBox::BorderBox:
  363. // Uses the border box as reference box.
  364. // FIXME: The reference box of a table is the border box of its table wrapper box, not its table box.
  365. return paintable_box.absolute_border_box_rect();
  366. case CSS::TransformBox::FillBox:
  367. // Uses the object bounding box as reference box.
  368. // FIXME: For now we're using the content rect as an approximation.
  369. return paintable_box.absolute_rect();
  370. case CSS::TransformBox::StrokeBox:
  371. // Uses the stroke bounding box as reference box.
  372. // FIXME: For now we're using the border rect as an approximation.
  373. return paintable_box.absolute_border_box_rect();
  374. case CSS::TransformBox::ViewBox:
  375. // Uses the nearest SVG viewport as reference box.
  376. // FIXME: If a viewBox attribute is specified for the SVG viewport creating element:
  377. // - The reference box is positioned at the origin of the coordinate system established by the viewBox attribute.
  378. // - The dimension of the reference box is set to the width and height values of the viewBox attribute.
  379. auto* svg_paintable = paintable_box.first_ancestor_of_type<Painting::SVGSVGPaintable>();
  380. if (!svg_paintable)
  381. return paintable_box.absolute_border_box_rect();
  382. return svg_paintable->absolute_rect();
  383. }
  384. VERIFY_NOT_REACHED();
  385. }();
  386. auto x = reference_box.left() + transform_origin.x.to_px(node, reference_box.width());
  387. auto y = reference_box.top() + transform_origin.y.to_px(node, reference_box.height());
  388. paintable_box.set_transform_origin({ x, y });
  389. paintable_box.set_transform_origin({ x, y });
  390. }
  391. }
  392. }
  393. void LayoutState::commit(Box& root)
  394. {
  395. // Only the top-level LayoutState should ever be committed.
  396. VERIFY(!m_parent);
  397. // NOTE: In case this is a relayout of an existing tree, we start by detaching the old paint tree
  398. // from the layout tree. This is done to ensure that we don't end up with any old-tree pointers
  399. // when text paintables shift around in the tree.
  400. root.for_each_in_inclusive_subtree([&](Layout::Node& node) {
  401. node.set_paintable(nullptr);
  402. return IterationDecision::Continue;
  403. });
  404. HashTable<Layout::TextNode*> text_nodes;
  405. Vector<Painting::PaintableWithLines&> paintables_with_lines;
  406. for (auto& it : used_values_per_layout_node) {
  407. auto& used_values = *it.value;
  408. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  409. if (is<NodeWithStyleAndBoxModelMetrics>(node)) {
  410. // Transfer box model metrics.
  411. auto& box_model = static_cast<NodeWithStyleAndBoxModelMetrics&>(node).box_model();
  412. box_model.inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
  413. box_model.padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
  414. box_model.border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
  415. box_model.margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
  416. }
  417. auto paintable = node.create_paintable();
  418. node.set_paintable(paintable);
  419. // For boxes, transfer all the state needed for painting.
  420. if (paintable && is<Painting::PaintableBox>(*paintable)) {
  421. auto& paintable_box = static_cast<Painting::PaintableBox&>(*paintable);
  422. paintable_box.set_offset(used_values.offset);
  423. paintable_box.set_content_size(used_values.content_width(), used_values.content_height());
  424. if (used_values.override_borders_data().has_value()) {
  425. paintable_box.set_override_borders_data(used_values.override_borders_data().value());
  426. }
  427. if (used_values.table_cell_coordinates().has_value()) {
  428. paintable_box.set_table_cell_coordinates(used_values.table_cell_coordinates().value());
  429. }
  430. if (is<Painting::PaintableWithLines>(paintable_box)) {
  431. auto& paintable_with_lines = static_cast<Painting::PaintableWithLines&>(paintable_box);
  432. for (auto& line_box : used_values.line_boxes) {
  433. for (auto& fragment : line_box.fragments())
  434. paintable_with_lines.add_fragment(fragment);
  435. }
  436. paintables_with_lines.append(paintable_with_lines);
  437. }
  438. if (used_values.computed_svg_transforms().has_value() && is<Painting::SVGGraphicsPaintable>(paintable_box)) {
  439. auto& svg_graphics_paintable = static_cast<Painting::SVGGraphicsPaintable&>(paintable_box);
  440. svg_graphics_paintable.set_computed_transforms(*used_values.computed_svg_transforms());
  441. }
  442. if (used_values.computed_svg_path().has_value() && is<Painting::SVGPathPaintable>(paintable_box)) {
  443. auto& svg_geometry_paintable = static_cast<Painting::SVGPathPaintable&>(paintable_box);
  444. svg_geometry_paintable.set_computed_path(move(*used_values.computed_svg_path()));
  445. }
  446. }
  447. }
  448. // Resolve relative positions for regular boxes (not line box fragments):
  449. // NOTE: This needs to occur before fragments are transferred into the corresponding inline paintables, because
  450. // after this transfer, the containing_line_box_fragment will no longer be valid.
  451. for (auto& it : used_values_per_layout_node) {
  452. auto& used_values = *it.value;
  453. auto& node = const_cast<NodeWithStyle&>(used_values.node());
  454. if (!node.is_box())
  455. continue;
  456. auto& paintable = static_cast<Painting::PaintableBox&>(*node.paintable());
  457. CSSPixelPoint offset;
  458. if (used_values.containing_line_box_fragment.has_value()) {
  459. // Atomic inline case:
  460. // We know that `node` is an atomic inline because `containing_line_box_fragments` refers to the
  461. // line box fragment in the parent block container that contains it.
  462. auto const& containing_line_box_fragment = used_values.containing_line_box_fragment.value();
  463. auto const& containing_block = *node.containing_block();
  464. auto const& containing_block_used_values = get(containing_block);
  465. auto const& fragment = containing_block_used_values.line_boxes[containing_line_box_fragment.line_box_index].fragments()[containing_line_box_fragment.fragment_index];
  466. // The fragment has the final offset for the atomic inline, so we just need to copy it from there.
  467. offset = fragment.offset();
  468. } else {
  469. // Not an atomic inline, much simpler case.
  470. offset = used_values.offset;
  471. }
  472. // Apply relative position inset if appropriate.
  473. if (node.computed_values().position() == CSS::Positioning::Relative && is<NodeWithStyleAndBoxModelMetrics>(node)) {
  474. auto const& inset = static_cast<NodeWithStyleAndBoxModelMetrics const&>(node).box_model().inset;
  475. offset.translate_by(inset.left, inset.top);
  476. }
  477. paintable.set_offset(offset);
  478. }
  479. // Make a pass over all the line boxes to:
  480. // - Collect all text nodes, so we can create paintables for them later.
  481. // - Relocate fragments into matching inline paintables
  482. for (auto& paintable_with_lines : paintables_with_lines) {
  483. Vector<Painting::PaintableFragment> fragments_with_inline_paintables_removed;
  484. for (auto& fragment : paintable_with_lines.fragments()) {
  485. if (fragment.layout_node().is_text_node())
  486. text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
  487. auto find_closest_inline_paintable = [&](auto& fragment) -> Painting::InlinePaintable const* {
  488. for (auto const* parent = fragment.layout_node().parent(); parent; parent = parent->parent()) {
  489. if (is<InlineNode>(*parent))
  490. return static_cast<Painting::InlinePaintable const*>(parent->paintable());
  491. }
  492. return nullptr;
  493. };
  494. if (auto const* inline_paintable = find_closest_inline_paintable(fragment)) {
  495. const_cast<Painting::InlinePaintable*>(inline_paintable)->fragments().append(fragment);
  496. } else {
  497. fragments_with_inline_paintables_removed.append(fragment);
  498. }
  499. }
  500. paintable_with_lines.set_fragments(move(fragments_with_inline_paintables_removed));
  501. }
  502. for (auto* text_node : text_nodes) {
  503. text_node->set_paintable(text_node->create_paintable());
  504. auto* paintable = text_node->paintable();
  505. auto const& font = text_node->first_available_font();
  506. auto const glyph_height = CSSPixels::nearest_value_for(font.pixel_size());
  507. auto const css_line_thickness = [&] {
  508. auto computed_thickness = text_node->computed_values().text_decoration_thickness().resolved(*text_node, CSS::Length(1, CSS::Length::Type::Em).to_px(*text_node));
  509. if (computed_thickness.is_auto())
  510. return max(glyph_height.scaled(0.1), 1);
  511. return computed_thickness.to_px(*text_node);
  512. }();
  513. auto& text_paintable = static_cast<Painting::TextPaintable&>(*paintable);
  514. text_paintable.set_text_decoration_thickness(css_line_thickness);
  515. }
  516. build_paint_tree(root);
  517. resolve_relative_positions();
  518. // Measure overflow in scroll containers.
  519. for (auto& it : used_values_per_layout_node) {
  520. auto& used_values = *it.value;
  521. if (!used_values.node().is_box())
  522. continue;
  523. auto const& box = static_cast<Layout::Box const&>(used_values.node());
  524. measure_scrollable_overflow(box);
  525. }
  526. resolve_layout_dependent_properties();
  527. }
  528. void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* containing_block_used_values)
  529. {
  530. m_node = &node;
  531. m_containing_block_used_values = containing_block_used_values;
  532. // NOTE: In the code below, we decide if `node` has definite width and/or height.
  533. // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
  534. // If `node` has definite values for min/max-width or min/max-height and a definite
  535. // preferred size in the same axis, we clamp the preferred size here as well.
  536. //
  537. // There are additional cases where CSS considers values to be definite. We model all of
  538. // those by having our engine consider sizes to be definite *once they are assigned to
  539. // the UsedValues by calling set_content_width() or set_content_height().
  540. auto const& computed_values = node.computed_values();
  541. auto adjust_for_box_sizing = [&](CSSPixels unadjusted_pixels, CSS::Size const& computed_size, bool width) -> CSSPixels {
  542. // box-sizing: content-box and/or automatic size don't require any adjustment.
  543. if (computed_values.box_sizing() == CSS::BoxSizing::ContentBox || computed_size.is_auto())
  544. return unadjusted_pixels;
  545. // box-sizing: border-box requires us to subtract the relevant border and padding from the size.
  546. CSSPixels border_and_padding;
  547. if (width) {
  548. border_and_padding = computed_values.border_left().width
  549. + computed_values.padding().left().to_px(*m_node, containing_block_used_values->content_width())
  550. + computed_values.border_right().width
  551. + computed_values.padding().right().to_px(*m_node, containing_block_used_values->content_width());
  552. } else {
  553. border_and_padding = computed_values.border_top().width
  554. + computed_values.padding().top().to_px(*m_node, containing_block_used_values->content_width())
  555. + computed_values.border_bottom().width
  556. + computed_values.padding().bottom().to_px(*m_node, containing_block_used_values->content_width());
  557. }
  558. return unadjusted_pixels - border_and_padding;
  559. };
  560. auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
  561. // A size that can be determined without performing layout; that is,
  562. // a <length>,
  563. // a measure of text (without consideration of line-wrapping),
  564. // a size of the initial containing block,
  565. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  566. 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;
  567. if (size.is_auto()) {
  568. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  569. if (width
  570. && !node.is_floating()
  571. && !node.is_absolutely_positioned()
  572. && node.display().is_block_outside()
  573. && node.parent()
  574. && !node.parent()->is_floating()
  575. && (node.parent()->display().is_flow_root_inside()
  576. || node.parent()->display().is_flow_inside())) {
  577. if (containing_block_has_definite_size) {
  578. CSSPixels available_width = containing_block_used_values->content_width();
  579. resolved_definite_size = available_width
  580. - margin_left
  581. - margin_right
  582. - padding_left
  583. - padding_right
  584. - border_left
  585. - border_right;
  586. return true;
  587. }
  588. return false;
  589. }
  590. return false;
  591. }
  592. if (size.is_calculated()) {
  593. if (size.calculated().contains_percentage()) {
  594. if (!containing_block_has_definite_size)
  595. return false;
  596. auto containing_block_size_as_length = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  597. 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);
  598. return true;
  599. }
  600. resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length(node)->to_px(node), size, width);
  601. return true;
  602. }
  603. if (size.is_length()) {
  604. VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
  605. resolved_definite_size = adjust_for_box_sizing(size.length().to_px(node), size, width);
  606. return true;
  607. }
  608. if (size.is_percentage()) {
  609. if (containing_block_has_definite_size) {
  610. auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
  611. resolved_definite_size = adjust_for_box_sizing(containing_block_size.scaled(size.percentage().as_fraction()), size, width);
  612. return true;
  613. }
  614. return false;
  615. }
  616. // FIXME: Determine if calc() value is definite.
  617. return false;
  618. };
  619. CSSPixels min_width = 0;
  620. bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
  621. CSSPixels max_width = 0;
  622. bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
  623. CSSPixels min_height = 0;
  624. bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
  625. CSSPixels max_height = 0;
  626. bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
  627. m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
  628. m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
  629. if (m_has_definite_width) {
  630. if (has_definite_min_width)
  631. m_content_width = max(min_width, m_content_width);
  632. if (has_definite_max_width)
  633. m_content_width = min(max_width, m_content_width);
  634. }
  635. if (m_has_definite_height) {
  636. if (has_definite_min_height)
  637. m_content_height = max(min_height, m_content_height);
  638. if (has_definite_max_height)
  639. m_content_height = min(max_height, m_content_height);
  640. }
  641. }
  642. void LayoutState::UsedValues::set_content_width(CSSPixels width)
  643. {
  644. VERIFY(!width.might_be_saturated());
  645. if (width < 0) {
  646. // Negative widths are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  647. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative width for {}: {}", m_node->debug_description(), width);
  648. width = 0;
  649. }
  650. m_content_width = width;
  651. m_has_definite_width = true;
  652. }
  653. void LayoutState::UsedValues::set_content_height(CSSPixels height)
  654. {
  655. VERIFY(!height.might_be_saturated());
  656. if (height < 0) {
  657. // Negative heights are not allowed in CSS. We have a bug somewhere! Clamp to 0 to avoid doing too much damage.
  658. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Layout calculated a negative height for {}: {}", m_node->debug_description(), height);
  659. height = 0;
  660. }
  661. m_content_height = height;
  662. m_has_definite_height = true;
  663. }
  664. void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
  665. {
  666. m_content_width = width;
  667. }
  668. void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
  669. {
  670. m_content_height = height;
  671. }
  672. AvailableSize LayoutState::UsedValues::available_width_inside() const
  673. {
  674. if (width_constraint == SizeConstraint::MinContent)
  675. return AvailableSize::make_min_content();
  676. if (width_constraint == SizeConstraint::MaxContent)
  677. return AvailableSize::make_max_content();
  678. if (has_definite_width())
  679. return AvailableSize::make_definite(m_content_width);
  680. return AvailableSize::make_indefinite();
  681. }
  682. AvailableSize LayoutState::UsedValues::available_height_inside() const
  683. {
  684. if (height_constraint == SizeConstraint::MinContent)
  685. return AvailableSize::make_min_content();
  686. if (height_constraint == SizeConstraint::MaxContent)
  687. return AvailableSize::make_max_content();
  688. if (has_definite_height())
  689. return AvailableSize::make_definite(m_content_height);
  690. return AvailableSize::make_indefinite();
  691. }
  692. AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
  693. {
  694. auto inner_width = available_width_inside();
  695. auto inner_height = available_height_inside();
  696. if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
  697. inner_width = outer_space.width;
  698. if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
  699. inner_height = outer_space.height;
  700. return AvailableSpace(inner_width, inner_height);
  701. }
  702. void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
  703. {
  704. set_content_x(new_offset.x());
  705. set_content_y(new_offset.y());
  706. }
  707. void LayoutState::UsedValues::set_content_x(CSSPixels x)
  708. {
  709. offset.set_x(x);
  710. }
  711. void LayoutState::UsedValues::set_content_y(CSSPixels y)
  712. {
  713. offset.set_y(y);
  714. }
  715. void LayoutState::UsedValues::set_indefinite_content_width()
  716. {
  717. m_has_definite_width = false;
  718. }
  719. void LayoutState::UsedValues::set_indefinite_content_height()
  720. {
  721. m_has_definite_height = false;
  722. }
  723. }