Serenity Operating System
1/*
2 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
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
12namespace Web::Layout {
13
14LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
15{
16 auto serial_id = box.serial_id();
17 if (used_values_per_layout_node[serial_id])
18 return *used_values_per_layout_node[serial_id];
19
20 for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
21 if (ancestor->used_values_per_layout_node[serial_id]) {
22 auto cow_used_values = adopt_own(*new UsedValues(*ancestor->used_values_per_layout_node[serial_id]));
23 auto* cow_used_values_ptr = cow_used_values.ptr();
24 used_values_per_layout_node[serial_id] = move(cow_used_values);
25 return *cow_used_values_ptr;
26 }
27 }
28
29 auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
30
31 used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues);
32 used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
33 return *used_values_per_layout_node[serial_id];
34}
35
36LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const
37{
38 auto serial_id = box.serial_id();
39 if (used_values_per_layout_node[serial_id])
40 return *used_values_per_layout_node[serial_id];
41
42 for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
43 if (ancestor->used_values_per_layout_node[serial_id])
44 return *ancestor->used_values_per_layout_node[serial_id];
45 }
46
47 auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
48
49 const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues);
50 const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
51 return *used_values_per_layout_node[serial_id];
52}
53
54void LayoutState::commit()
55{
56 // Only the top-level LayoutState should ever be committed.
57 VERIFY(!m_parent);
58
59 HashTable<Layout::TextNode*> text_nodes;
60
61 for (auto& used_values_ptr : used_values_per_layout_node) {
62 if (!used_values_ptr)
63 continue;
64 auto& used_values = *used_values_ptr;
65 auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node());
66
67 // Transfer box model metrics.
68 node.box_model().inset = { used_values.inset_top.value(), used_values.inset_right.value(), used_values.inset_bottom.value(), used_values.inset_left.value() };
69 node.box_model().padding = { used_values.padding_top.value(), used_values.padding_right.value(), used_values.padding_bottom.value(), used_values.padding_left.value() };
70 node.box_model().border = { used_values.border_top.value(), used_values.border_right.value(), used_values.border_bottom.value(), used_values.border_left.value() };
71 node.box_model().margin = { used_values.margin_top.value(), used_values.margin_right.value(), used_values.margin_bottom.value(), used_values.margin_left.value() };
72
73 node.set_paintable(node.create_paintable());
74
75 // For boxes, transfer all the state needed for painting.
76 if (is<Layout::Box>(node)) {
77 auto& box = static_cast<Layout::Box const&>(node);
78 auto& paint_box = const_cast<Painting::PaintableBox&>(*box.paint_box());
79 paint_box.set_offset(used_values.offset);
80 paint_box.set_content_size(used_values.content_width(), used_values.content_height());
81 paint_box.set_overflow_data(move(used_values.overflow_data));
82 paint_box.set_containing_line_box_fragment(used_values.containing_line_box_fragment);
83
84 if (is<Layout::BlockContainer>(box)) {
85 for (auto& line_box : used_values.line_boxes) {
86 for (auto& fragment : line_box.fragments()) {
87 if (fragment.layout_node().is_text_node())
88 text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node())));
89 }
90 }
91 static_cast<Painting::PaintableWithLines&>(paint_box).set_line_boxes(move(used_values.line_boxes));
92 }
93 }
94 }
95
96 for (auto* text_node : text_nodes)
97 text_node->set_paintable(text_node->create_paintable());
98}
99
100CSSPixels box_baseline(LayoutState const& state, Box const& box)
101{
102 auto const& box_state = state.get(box);
103
104 // https://www.w3.org/TR/CSS2/visudet.html#propdef-vertical-align
105 auto const& vertical_align = box.computed_values().vertical_align();
106 if (vertical_align.has<CSS::VerticalAlign>()) {
107 switch (vertical_align.get<CSS::VerticalAlign>()) {
108 case CSS::VerticalAlign::Top:
109 // Top: Align the top of the aligned subtree with the top of the line box.
110 return box_state.border_box_top();
111 case CSS::VerticalAlign::Bottom:
112 // Bottom: Align the bottom of the aligned subtree with the bottom of the line box.
113 return box_state.content_height() + box_state.border_box_bottom();
114 case CSS::VerticalAlign::TextTop:
115 // TextTop: Align the top of the box with the top of the parent's content area (see 10.6.1).
116 return box.computed_values().font_size();
117 case CSS::VerticalAlign::TextBottom:
118 // TextTop: Align the bottom of the box with the bottom of the parent's content area (see 10.6.1).
119 return box_state.content_height() - (box.containing_block()->font().pixel_metrics().descent * 2);
120 default:
121 break;
122 }
123 }
124
125 if (!box_state.line_boxes.is_empty())
126 return box_state.border_box_top() + box_state.offset.y() + box_state.line_boxes.last().baseline();
127 if (box.has_children() && !box.children_are_inline()) {
128 auto const* child_box = box.last_child_of_type<Box>();
129 VERIFY(child_box);
130 return box_baseline(state, *child_box);
131 }
132 return box_state.border_box_height();
133}
134
135CSSPixelRect margin_box_rect(Box const& box, LayoutState const& state)
136{
137 auto const& box_state = state.get(box);
138 auto rect = CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
139 rect.set_x(rect.x() - box_state.margin_box_left());
140 rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right());
141 rect.set_y(rect.y() - box_state.margin_box_top());
142 rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom());
143 return rect;
144}
145
146CSSPixelRect border_box_rect(Box const& box, LayoutState const& state)
147{
148 auto const& box_state = state.get(box);
149 auto rect = CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
150 rect.set_x(rect.x() - box_state.border_box_left());
151 rect.set_width(rect.width() + box_state.border_box_left() + box_state.border_box_right());
152 rect.set_y(rect.y() - box_state.border_box_top());
153 rect.set_height(rect.height() + box_state.border_box_top() + box_state.border_box_bottom());
154 return rect;
155}
156
157CSSPixelRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
158{
159 auto rect = border_box_rect(box, state);
160 if (&box == &ancestor_box)
161 return rect;
162 for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
163 if (current == &ancestor_box)
164 return rect;
165 auto const& current_state = state.get(static_cast<Box const&>(*current));
166 rect.translate_by(current_state.offset);
167 }
168 // If we get here, ancestor_box was not a containing block ancestor of `box`!
169 VERIFY_NOT_REACHED();
170}
171
172CSSPixelRect content_box_rect(Box const& box, LayoutState const& state)
173{
174 auto const& box_state = state.get(box);
175 return CSSPixelRect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
176}
177
178CSSPixelRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
179{
180 auto rect = content_box_rect(box, state);
181 if (&box == &ancestor_box)
182 return rect;
183 for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
184 if (current == &ancestor_box)
185 return rect;
186 auto const& current_state = state.get(static_cast<Box const&>(*current));
187 rect.translate_by(current_state.offset);
188 }
189 // If we get here, ancestor_box was not a containing block ancestor of `box`!
190 VERIFY_NOT_REACHED();
191}
192
193CSSPixelRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state)
194{
195 auto rect = margin_box_rect(box, state);
196 if (&box == &ancestor_box)
197 return rect;
198 for (auto const* current = box.containing_block(); current; current = current->containing_block()) {
199 if (current == &ancestor_box)
200 return rect;
201 auto const& current_state = state.get(static_cast<Box const&>(*current));
202 rect.translate_by(current_state.offset);
203 }
204 // If we get here, ancestor_box was not a containing block ancestor of `box`!
205 VERIFY_NOT_REACHED();
206}
207
208CSSPixelRect absolute_content_rect(Box const& box, LayoutState const& state)
209{
210 auto const& box_state = state.get(box);
211 CSSPixelRect rect { box_state.offset, { box_state.content_width(), box_state.content_height() } };
212 for (auto* block = box.containing_block(); block; block = block->containing_block())
213 rect.translate_by(state.get(*block).offset);
214 return rect;
215}
216
217void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
218{
219 m_node = &node;
220
221 // NOTE: In the code below, we decide if `node` has definite width and/or height.
222 // This attempts to cover all the *general* cases where CSS considers sizes to be definite.
223 // If `node` has definite values for min/max-width or min/max-height and a definite
224 // preferred size in the same axis, we clamp the preferred size here as well.
225 //
226 // There are additional cases where CSS considers values to be definite. We model all of
227 // those by having our engine consider sizes to be definite *once they are assigned to
228 // the UsedValues by calling set_content_width() or set_content_height().
229
230 auto const& computed_values = node.computed_values();
231
232 auto is_definite_size = [&](CSS::Size const& size, CSSPixels& resolved_definite_size, bool width) {
233 // A size that can be determined without performing layout; that is,
234 // a <length>,
235 // a measure of text (without consideration of line-wrapping),
236 // a size of the initial containing block,
237 // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
238
239 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;
240
241 if (size.is_auto()) {
242 // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
243 if (width
244 && !node.is_floating()
245 && node.display().is_block_outside()
246 && node.parent()
247 && !node.parent()->is_floating()
248 && (node.parent()->display().is_flow_root_inside()
249 || node.parent()->display().is_flow_inside())) {
250 if (containing_block_has_definite_size) {
251 CSSPixels available_width = containing_block_used_values->content_width();
252 resolved_definite_size = available_width
253 - margin_left
254 - margin_right
255 - padding_left
256 - padding_right
257 - border_left
258 - border_right;
259 return true;
260 }
261 return false;
262 }
263 return false;
264 }
265
266 if (size.is_length() && size.length().is_calculated()) {
267 if (size.length().calculated_style_value()->contains_percentage()) {
268 if (!containing_block_has_definite_size)
269 return false;
270 auto& calc_value = *size.length().calculated_style_value();
271 auto containing_block_size_as_length = width
272 ? CSS::Length::make_px(containing_block_used_values->content_width())
273 : CSS::Length::make_px(containing_block_used_values->content_height());
274 resolved_definite_size = calc_value.resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node);
275 return true;
276 }
277 resolved_definite_size = size.length().to_px(node);
278 return true;
279 }
280
281 if (size.is_length()) {
282 VERIFY(!size.is_auto()); // This should have been covered by the Size::is_auto() branch above.
283 VERIFY(!size.length().is_calculated()); // Covered above.
284 resolved_definite_size = size.length().to_px(node);
285 return true;
286 }
287 if (size.is_percentage()) {
288 if (containing_block_has_definite_size) {
289 auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
290 resolved_definite_size = containing_block_size * size.percentage().as_fraction();
291 return true;
292 }
293 return false;
294 }
295 // FIXME: Determine if calc() value is definite.
296 return false;
297 };
298
299 CSSPixels min_width = 0;
300 bool has_definite_min_width = is_definite_size(computed_values.min_width(), min_width, true);
301 CSSPixels max_width = 0;
302 bool has_definite_max_width = is_definite_size(computed_values.max_width(), max_width, true);
303
304 CSSPixels min_height = 0;
305 bool has_definite_min_height = is_definite_size(computed_values.min_height(), min_height, false);
306 CSSPixels max_height = 0;
307 bool has_definite_max_height = is_definite_size(computed_values.max_height(), max_height, false);
308
309 m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true);
310 m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false);
311
312 if (m_has_definite_width) {
313 if (has_definite_min_width)
314 m_content_width = max(min_width, m_content_width);
315 if (has_definite_max_width)
316 m_content_width = min(max_width, m_content_width);
317 }
318
319 if (m_has_definite_height) {
320 if (has_definite_min_height)
321 m_content_height = max(min_height, m_content_height);
322 if (has_definite_max_height)
323 m_content_height = min(max_height, m_content_height);
324 }
325}
326
327void LayoutState::UsedValues::set_content_width(CSSPixels width)
328{
329 m_content_width = width;
330 m_has_definite_width = true;
331}
332
333void LayoutState::UsedValues::set_content_height(CSSPixels height)
334{
335 m_content_height = height;
336 m_has_definite_height = true;
337}
338
339void LayoutState::UsedValues::set_temporary_content_width(CSSPixels width)
340{
341 m_content_width = width;
342}
343
344void LayoutState::UsedValues::set_temporary_content_height(CSSPixels height)
345{
346 m_content_height = height;
347}
348
349CSSPixels LayoutState::resolved_definite_width(Box const& box) const
350{
351 return get(box).content_width();
352}
353
354CSSPixels LayoutState::resolved_definite_height(Box const& box) const
355{
356 return get(box).content_height();
357}
358
359AvailableSize LayoutState::UsedValues::available_width_inside() const
360{
361 if (width_constraint == SizeConstraint::MinContent)
362 return AvailableSize::make_min_content();
363 if (width_constraint == SizeConstraint::MaxContent)
364 return AvailableSize::make_max_content();
365 if (has_definite_width())
366 return AvailableSize::make_definite(m_content_width);
367 return AvailableSize::make_indefinite();
368}
369
370AvailableSize LayoutState::UsedValues::available_height_inside() const
371{
372 if (height_constraint == SizeConstraint::MinContent)
373 return AvailableSize::make_min_content();
374 if (height_constraint == SizeConstraint::MaxContent)
375 return AvailableSize::make_max_content();
376 if (has_definite_height())
377 return AvailableSize::make_definite(m_content_height);
378 return AvailableSize::make_indefinite();
379}
380
381AvailableSpace LayoutState::UsedValues::available_inner_space_or_constraints_from(AvailableSpace const& outer_space) const
382{
383 auto inner_width = available_width_inside();
384 auto inner_height = available_height_inside();
385
386 if (inner_width.is_indefinite() && outer_space.width.is_intrinsic_sizing_constraint())
387 inner_width = outer_space.width;
388 if (inner_height.is_indefinite() && outer_space.height.is_intrinsic_sizing_constraint())
389 inner_height = outer_space.height;
390 return AvailableSpace(inner_width, inner_height);
391}
392
393void LayoutState::UsedValues::set_content_offset(CSSPixelPoint new_offset)
394{
395 set_content_x(new_offset.x());
396 set_content_y(new_offset.y());
397}
398
399void LayoutState::UsedValues::set_content_x(CSSPixels x)
400{
401 offset.set_x(x);
402}
403
404void LayoutState::UsedValues::set_content_y(CSSPixels y)
405{
406 offset.set_y(y);
407}
408
409}