| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/BlockContainer.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  | #include <LibWeb/Layout/LayoutState.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  | #include <LibWeb/Layout/TextNode.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Layout { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  | LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box) | 
					
						
							| 
									
										
										
										
											2022-02-28 12:32:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |     auto serial_id = box.serial_id(); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     if (used_values_per_layout_node[serial_id]) | 
					
						
							|  |  |  |         return *used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) { | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |         if (ancestor->used_values_per_layout_node[serial_id]) { | 
					
						
							|  |  |  |             auto cow_used_values = adopt_own(*new UsedValues(*ancestor->used_values_per_layout_node[serial_id])); | 
					
						
							|  |  |  |             auto* cow_used_values_ptr = cow_used_values.ptr(); | 
					
						
							|  |  |  |             used_values_per_layout_node[serial_id] = move(cow_used_values); | 
					
						
							|  |  |  |             return *cow_used_values_ptr; | 
					
						
							| 
									
										
										
										
											2022-03-12 16:33:11 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-12 16:33:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |     auto const* containing_block_used_values = box.is_initial_containing_block_box() ? nullptr : &get(*box.containing_block()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |     used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     return *used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-02-28 12:32:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  | LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const | 
					
						
							| 
									
										
										
										
											2022-02-28 12:32:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |     auto serial_id = box.serial_id(); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     if (used_values_per_layout_node[serial_id]) | 
					
						
							|  |  |  |         return *used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) { | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |         if (ancestor->used_values_per_layout_node[serial_id]) | 
					
						
							|  |  |  |             return *ancestor->used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto const* containing_block_used_values = box.is_initial_containing_block_box() ? nullptr : &get(*box.containing_block()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id] = adopt_own(*new UsedValues); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |     const_cast<LayoutState*>(this)->used_values_per_layout_node[serial_id]->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     return *used_values_per_layout_node[serial_id]; | 
					
						
							| 
									
										
										
										
											2022-02-28 12:32:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  | void LayoutState::commit() | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  |     // Only the top-level LayoutState should ever be committed.
 | 
					
						
							| 
									
										
										
										
											2022-03-12 16:33:11 +01:00
										 |  |  |     VERIFY(!m_parent); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  |     HashTable<Layout::TextNode*> text_nodes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |     for (auto& used_values_ptr : used_values_per_layout_node) { | 
					
						
							|  |  |  |         if (!used_values_ptr) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:12:58 +02:00
										 |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |         auto& used_values = *used_values_ptr; | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |         auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node()); | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Transfer box model metrics.
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |         node.box_model().inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left }; | 
					
						
							|  |  |  |         node.box_model().padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left }; | 
					
						
							|  |  |  |         node.box_model().border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left }; | 
					
						
							|  |  |  |         node.box_model().margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left }; | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:57 +01:00
										 |  |  |         node.set_paintable(node.create_paintable()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-09 23:53:41 +01:00
										 |  |  |         // For boxes, transfer all the state needed for painting.
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |         if (is<Layout::Box>(node)) { | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |             auto& box = static_cast<Layout::Box const&>(node); | 
					
						
							| 
									
										
										
										
											2022-03-10 15:50:57 +01:00
										 |  |  |             auto& paint_box = const_cast<Painting::PaintableBox&>(*box.paint_box()); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |             paint_box.set_offset(used_values.offset); | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  |             paint_box.set_content_size(used_values.content_width(), used_values.content_height()); | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |             paint_box.set_overflow_data(move(used_values.overflow_data)); | 
					
						
							|  |  |  |             paint_box.set_containing_line_box_fragment(used_values.containing_line_box_fragment); | 
					
						
							| 
									
										
										
										
											2022-03-10 11:12:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  |             if (is<Layout::BlockContainer>(box)) { | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |                 for (auto& line_box : used_values.line_boxes) { | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  |                     for (auto& fragment : line_box.fragments()) { | 
					
						
							|  |  |  |                         if (fragment.layout_node().is_text_node()) | 
					
						
							|  |  |  |                             text_nodes.set(static_cast<Layout::TextNode*>(const_cast<Layout::Node*>(&fragment.layout_node()))); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-07-16 23:43:48 +02:00
										 |  |  |                 static_cast<Painting::PaintableWithLines&>(paint_box).set_line_boxes(move(used_values.line_boxes)); | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-10 16:46:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto* text_node : text_nodes) | 
					
						
							|  |  |  |         text_node->set_paintable(text_node->create_paintable()); | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  | Gfx::FloatRect margin_box_rect(Box const& box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  |     auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } }; | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |     rect.set_x(rect.x() - box_state.margin_box_left()); | 
					
						
							|  |  |  |     rect.set_width(rect.width() + box_state.margin_box_left() + box_state.margin_box_right()); | 
					
						
							|  |  |  |     rect.set_y(rect.y() - box_state.margin_box_top()); | 
					
						
							|  |  |  |     rect.set_height(rect.height() + box_state.margin_box_top() + box_state.margin_box_bottom()); | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 13:29:58 +02:00
										 |  |  | Gfx::FloatRect border_box_rect(Box const& box, LayoutState const& state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							|  |  |  |     auto rect = Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } }; | 
					
						
							|  |  |  |     rect.set_x(rect.x() - box_state.border_box_left()); | 
					
						
							|  |  |  |     rect.set_width(rect.width() + box_state.border_box_left() + box_state.border_box_right()); | 
					
						
							|  |  |  |     rect.set_y(rect.y() - box_state.border_box_top()); | 
					
						
							|  |  |  |     rect.set_height(rect.height() + box_state.border_box_top() + box_state.border_box_bottom()); | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Gfx::FloatRect border_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto rect = border_box_rect(box, state); | 
					
						
							|  |  |  |     for (auto const* current = box.parent(); current; current = current->parent()) { | 
					
						
							|  |  |  |         if (current == &ancestor_box) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         if (is<Box>(*current)) { | 
					
						
							|  |  |  |             auto const& current_state = state.get(static_cast<Box const&>(*current)); | 
					
						
							|  |  |  |             rect.translate_by(current_state.offset); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 13:21:57 +02:00
										 |  |  | Gfx::FloatRect content_box_rect(Box const& box, LayoutState const& state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							|  |  |  |     return Gfx::FloatRect { box_state.offset, { box_state.content_width(), box_state.content_height() } }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Gfx::FloatRect content_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto rect = content_box_rect(box, state); | 
					
						
							|  |  |  |     for (auto const* current = box.parent(); current; current = current->parent()) { | 
					
						
							|  |  |  |         if (current == &ancestor_box) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         if (is<Box>(*current)) { | 
					
						
							|  |  |  |             auto const& current_state = state.get(static_cast<Box const&>(*current)); | 
					
						
							|  |  |  |             rect.translate_by(current_state.offset); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  | Gfx::FloatRect margin_box_rect_in_ancestor_coordinate_space(Box const& box, Box const& ancestor_box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto rect = margin_box_rect(box, state); | 
					
						
							|  |  |  |     for (auto const* current = box.parent(); current; current = current->parent()) { | 
					
						
							|  |  |  |         if (current == &ancestor_box) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         if (is<Box>(*current)) { | 
					
						
							|  |  |  |             auto const& current_state = state.get(static_cast<Box const&>(*current)); | 
					
						
							|  |  |  |             rect.translate_by(current_state.offset); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  | Gfx::FloatRect absolute_content_rect(Box const& box, LayoutState const& state) | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto const& box_state = state.get(box); | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  |     Gfx::FloatRect rect { box_state.offset, { box_state.content_width(), box_state.content_height() } }; | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |     for (auto* block = box.containing_block(); block; block = block->containing_block()) | 
					
						
							|  |  |  |         rect.translate_by(state.get(*block).offset); | 
					
						
							|  |  |  |     return rect; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  | void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_node = &node; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto const& computed_values = node.computed_values(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |     auto is_definite_size = [&](CSS::LengthPercentage const& size, float& resolved_definite_size, bool width) { | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |         // A size that can be determined without performing layout; that is,
 | 
					
						
							|  |  |  |         // a <length>,
 | 
					
						
							|  |  |  |         // a measure of text (without consideration of line-wrapping),
 | 
					
						
							|  |  |  |         // a size of the initial containing block,
 | 
					
						
							|  |  |  |         // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (size.is_auto()) { | 
					
						
							|  |  |  |             // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
 | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |             if (width && node.parent() && !node.parent()->computed_values().display().is_flex_inside()) { | 
					
						
							|  |  |  |                 if (containing_block_has_definite_size) { | 
					
						
							|  |  |  |                     resolved_definite_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height(); | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |         if (size.is_length()) { | 
					
						
							|  |  |  |             resolved_definite_size = size.length().to_px(node); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (size.is_percentage()) { | 
					
						
							|  |  |  |             if (containing_block_has_definite_size) { | 
					
						
							|  |  |  |                 auto containing_block_size = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height(); | 
					
						
							|  |  |  |                 resolved_definite_size = containing_block_size * size.percentage().as_fraction(); | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  |         // FIXME: Determine if calc() value is definite.
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-19 13:46:27 +02:00
										 |  |  |     m_has_definite_width = is_definite_size(computed_values.width(), m_content_width, true); | 
					
						
							|  |  |  |     m_has_definite_height = is_definite_size(computed_values.height(), m_content_height, false); | 
					
						
							| 
									
										
										
										
											2022-07-17 18:46:38 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 17:59:02 +02:00
										 |  |  | void LayoutState::UsedValues::set_content_width(float width) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_content_width = width; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LayoutState::UsedValues::set_content_height(float height) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_content_height = height; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  | } |