| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-02-21 01:43:37 +01:00
										 |  |  |  * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-05 20:10:39 +01:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-06 21:53:25 +02:00
										 |  |  | #include <LibWeb/Layout/BlockContainer.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | #include <LibWeb/Layout/FormattingContext.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Layout { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-22 19:18:05 +01:00
										 |  |  | class LineBuilder; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 19:52:10 +01:00
										 |  |  | // https://www.w3.org/TR/css-display/#block-formatting-context
 | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | class BlockFormattingContext : public FormattingContext { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  |     explicit BlockFormattingContext(LayoutState&, BlockContainer const&, FormattingContext* parent); | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  |     ~BlockFormattingContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     virtual void run(Box const&, LayoutMode, AvailableSpace const&) override; | 
					
						
							| 
									
										
										
										
											2022-09-24 13:39:43 +02:00
										 |  |  |     virtual float automatic_content_height() const override; | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool is_initial() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 15:19:18 +01:00
										 |  |  |     auto const& left_side_floats() const { return m_left_floats; } | 
					
						
							|  |  |  |     auto const& right_side_floats() const { return m_right_floats; } | 
					
						
							| 
									
										
										
										
											2020-12-05 20:10:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     void compute_width(Box const&, AvailableSpace const&, LayoutMode = LayoutMode::Normal); | 
					
						
							| 
									
										
										
										
											2021-05-30 21:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 19:52:10 +01:00
										 |  |  |     // https://www.w3.org/TR/css-display/#block-formatting-context-root
 | 
					
						
							| 
									
										
										
										
											2021-10-06 21:53:25 +02:00
										 |  |  |     BlockContainer const& root() const { return static_cast<BlockContainer const&>(context_box()); } | 
					
						
							| 
									
										
										
										
											2021-10-06 19:47:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 19:54:09 +01:00
										 |  |  |     virtual void parent_context_did_dimension_child_root_box() override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     void compute_height(Box const&, AvailableSpace const&); | 
					
						
							| 
									
										
										
										
											2022-02-12 19:54:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |     void add_absolutely_positioned_box(Box const& box) { m_absolutely_positioned_boxes.append(box); } | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 14:44:36 +01:00
										 |  |  |     SpaceUsedByFloats space_used_by_floats(float y) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual float greatest_child_width(Box const&) override; | 
					
						
							| 
									
										
										
										
											2022-03-17 12:30:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-25 17:13:21 +03:00
										 |  |  |     void layout_floating_box(Box const& child, BlockContainer const& containing_block, LayoutMode, AvailableSpace const&, float y, LineBuilder* = nullptr); | 
					
						
							| 
									
										
										
										
											2022-03-22 19:18:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-25 17:13:21 +03:00
										 |  |  |     void layout_block_level_box(Box const&, BlockContainer const&, LayoutMode, float& bottom_of_lowest_margin_box, AvailableSpace const&, float& current_y); | 
					
						
							| 
									
										
										
										
											2022-07-09 20:14:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     virtual bool can_determine_size_of_child() const override { return true; } | 
					
						
							|  |  |  |     virtual void determine_width_of_child(Box const&, AvailableSpace const&) override; | 
					
						
							|  |  |  |     virtual void determine_height_of_child(Box const&, AvailableSpace const&) override; | 
					
						
							| 
									
										
										
										
											2022-09-13 10:47:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-12-05 20:10:39 +01:00
										 |  |  |     virtual bool is_block_formatting_context() const final { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     void compute_width_for_floating_box(Box const&, AvailableSpace const&); | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     void compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox const&, AvailableSpace const&); | 
					
						
							| 
									
										
										
										
											2021-03-08 22:49:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     void layout_initial_containing_block(LayoutMode, AvailableSpace const&); | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     void layout_block_level_children(BlockContainer const&, LayoutMode, AvailableSpace const&); | 
					
						
							|  |  |  |     void layout_inline_children(BlockContainer const&, LayoutMode, AvailableSpace const&); | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-08 17:09:25 +02:00
										 |  |  |     static void resolve_vertical_box_model_metrics(Box const& box, LayoutState&); | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     void place_block_level_element_in_normal_flow_horizontally(Box const& child_box, AvailableSpace const&); | 
					
						
							| 
									
										
										
										
											2022-12-25 17:13:21 +03:00
										 |  |  |     void place_block_level_element_in_normal_flow_vertically(Box const&, float y); | 
					
						
							| 
									
										
										
										
											2020-12-06 19:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 01:43:37 +01:00
										 |  |  |     void layout_list_item_marker(ListItemBox const&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-23 01:19:28 +01:00
										 |  |  |     enum class FloatSide { | 
					
						
							|  |  |  |         Left, | 
					
						
							|  |  |  |         Right, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-18 14:44:36 +01:00
										 |  |  |     struct FloatingBox { | 
					
						
							|  |  |  |         Box const& box; | 
					
						
							|  |  |  |         // Offset from left/right edge to the left content edge of `box`.
 | 
					
						
							|  |  |  |         float offset_from_edge { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Top margin edge of `box`.
 | 
					
						
							|  |  |  |         float top_margin_edge { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Bottom margin edge of `box`.
 | 
					
						
							|  |  |  |         float bottom_margin_edge { 0 }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 15:19:18 +01:00
										 |  |  |     struct FloatSideData { | 
					
						
							| 
									
										
										
										
											2022-03-18 14:44:36 +01:00
										 |  |  |         // Floating boxes currently accumulating on this side.
 | 
					
						
							|  |  |  |         Vector<FloatingBox&> current_boxes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Combined width of boxes currently accumulating on this side.
 | 
					
						
							|  |  |  |         // This is the innermost margin of the innermost floating box.
 | 
					
						
							|  |  |  |         float current_width { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Highest value of `m_current_width` we've seen.
 | 
					
						
							|  |  |  |         float max_width { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // All floating boxes encountered thus far within this BFC.
 | 
					
						
							|  |  |  |         Vector<NonnullOwnPtr<FloatingBox>> all_boxes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Current Y offset from BFC root top.
 | 
					
						
							| 
									
										
										
										
											2022-01-23 01:19:28 +01:00
										 |  |  |         float y_offset { 0 }; | 
					
						
							| 
									
										
										
										
											2022-03-18 14:44:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         void clear() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             current_boxes.clear(); | 
					
						
							|  |  |  |             current_width = 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-22 15:19:18 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-25 17:13:21 +03:00
										 |  |  |     struct BlockMarginState { | 
					
						
							|  |  |  |         Vector<float> current_collapsible_margins; | 
					
						
							| 
									
										
										
										
											2022-12-25 14:01:14 +03:00
										 |  |  |         Function<void(float)> block_container_y_position_update_callback; | 
					
						
							| 
									
										
										
										
											2022-12-25 17:13:21 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         void add_margin(float margin) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             current_collapsible_margins.append(margin); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-25 14:01:14 +03:00
										 |  |  |         void register_block_container_y_position_update_callback(Function<void(float)> callback) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             block_container_y_position_update_callback = move(callback); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-25 17:13:21 +03:00
										 |  |  |         float current_collapsed_margin() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-25 14:01:14 +03:00
										 |  |  |         bool has_block_container_waiting_for_final_y_position() const | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return static_cast<bool>(block_container_y_position_update_callback); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         void update_block_waiting_for_final_y_position() const | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (block_container_y_position_update_callback) { | 
					
						
							|  |  |  |                 float collapsed_margin = current_collapsed_margin(); | 
					
						
							|  |  |  |                 block_container_y_position_update_callback(collapsed_margin); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-25 17:13:21 +03:00
										 |  |  |         void reset() | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-12-25 14:01:14 +03:00
										 |  |  |             block_container_y_position_update_callback = {}; | 
					
						
							| 
									
										
										
										
											2022-12-25 17:13:21 +03:00
										 |  |  |             current_collapsible_margins.clear(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     BlockMarginState m_margin_state; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 15:19:18 +01:00
										 |  |  |     FloatSideData m_left_floats; | 
					
						
							|  |  |  |     FloatSideData m_right_floats; | 
					
						
							| 
									
										
										
										
											2022-02-12 19:54:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 15:51:24 +01:00
										 |  |  |     Vector<Box const&> m_absolutely_positioned_boxes; | 
					
						
							| 
									
										
										
										
											2022-02-19 16:42:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_was_notified_after_parent_dimensioned_my_root_box { false }; | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |