| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2022-12-04 21:57:21 +03:00
										 |  |  | #include <LibWeb/Layout/FormattingContext.h>
 | 
					
						
							| 
									
										
										
										
											2023-01-14 15:05:08 +01:00
										 |  |  | #include <LibWeb/Layout/TableWrapper.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Layout { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 03:37:26 +00:00
										 |  |  | enum class TableDimension { | 
					
						
							|  |  |  |     Row, | 
					
						
							|  |  |  |     Column | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 21:57:21 +03:00
										 |  |  | class TableFormattingContext final : public FormattingContext { | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-05-29 14:11:19 +03:00
										 |  |  |     explicit TableFormattingContext(LayoutState&, Box const&, FormattingContext* parent); | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  |     ~TableFormattingContext(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-27 15:29:17 +02:00
										 |  |  |     virtual void run(Box const&, LayoutMode, AvailableSpace const&) override; | 
					
						
							| 
									
										
										
										
											2023-03-19 09:57:31 +01:00
										 |  |  |     virtual CSSPixels automatic_content_width() const override; | 
					
						
							| 
									
										
										
										
											2022-11-23 17:46:10 +00:00
										 |  |  |     virtual CSSPixels automatic_content_height() const override; | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 14:11:19 +03:00
										 |  |  |     Box const& table_box() const { return context_box(); } | 
					
						
							| 
									
										
										
										
											2023-01-14 15:05:08 +01:00
										 |  |  |     TableWrapper const& table_wrapper() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return verify_cast<TableWrapper>(*table_box().containing_block()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-08 01:54:25 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-01 03:06:21 +00:00
										 |  |  |     static bool border_is_less_specific(const CSS::BorderData& a, const CSS::BorderData& b); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-06-07 02:10:55 +00:00
										 |  |  |     CSSPixels run_caption_layout(LayoutMode, CSS::CaptionSide); | 
					
						
							|  |  |  |     CSSPixels compute_capmin(); | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |     void calculate_row_column_grid(Box const&); | 
					
						
							| 
									
										
										
										
											2023-07-15 01:12:10 +00:00
										 |  |  |     void compute_constrainedness(); | 
					
						
							| 
									
										
										
										
											2023-06-08 03:37:26 +00:00
										 |  |  |     void compute_cell_measures(AvailableSpace const& available_space); | 
					
						
							| 
									
										
										
										
											2023-07-15 01:41:42 +00:00
										 |  |  |     void compute_outer_content_sizes(); | 
					
						
							| 
									
										
										
										
											2023-06-08 03:37:26 +00:00
										 |  |  |     template<class RowOrColumn> | 
					
						
							| 
									
										
										
										
											2023-06-26 02:48:09 +00:00
										 |  |  |     void initialize_table_measures(); | 
					
						
							|  |  |  |     template<class RowOrColumn> | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |     void compute_table_measures(); | 
					
						
							| 
									
										
										
										
											2023-01-05 18:56:34 +03:00
										 |  |  |     void compute_table_width(); | 
					
						
							|  |  |  |     void distribute_width_to_columns(); | 
					
						
							| 
									
										
										
										
											2023-07-15 02:46:47 +00:00
										 |  |  |     void distribute_excess_width_to_columns(CSSPixels available_width); | 
					
						
							| 
									
										
										
										
											2023-04-29 00:43:05 +03:00
										 |  |  |     void compute_table_height(LayoutMode layout_mode); | 
					
						
							|  |  |  |     void distribute_height_to_rows(); | 
					
						
							| 
									
										
										
										
											2023-06-22 17:22:12 +00:00
										 |  |  |     void position_row_boxes(); | 
					
						
							| 
									
										
										
										
											2023-01-08 00:57:57 +03:00
										 |  |  |     void position_cell_boxes(); | 
					
						
							| 
									
										
										
										
											2023-06-04 14:21:20 +00:00
										 |  |  |     void border_conflict_resolution(); | 
					
						
							| 
									
										
										
										
											2023-06-16 02:48:56 +00:00
										 |  |  |     CSSPixels border_spacing_horizontal() const; | 
					
						
							|  |  |  |     CSSPixels border_spacing_vertical() const; | 
					
						
							| 
									
										
										
										
											2022-09-24 13:39:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-15 02:46:47 +00:00
										 |  |  |     CSSPixels compute_columns_total_used_width() const; | 
					
						
							|  |  |  |     void commit_candidate_column_widths(Vector<CSSPixels> const& candidate_widths); | 
					
						
							|  |  |  |     void assign_columns_width_linear_combination(Vector<CSSPixels> const& candidate_widths, CSSPixels available_width); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<class ColumnFilter> | 
					
						
							|  |  |  |     bool distribute_excess_width_proportionally_to_max_width(CSSPixels excess_width, ColumnFilter column_filter); | 
					
						
							|  |  |  |     template<class ColumnFilter> | 
					
						
							|  |  |  |     bool distribute_excess_width_equally(CSSPixels excess_width, ColumnFilter column_filter); | 
					
						
							|  |  |  |     template<class ColumnFilter> | 
					
						
							|  |  |  |     bool distribute_excess_width_by_intrinsic_percentage(CSSPixels excess_width, ColumnFilter column_filter); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-29 00:43:05 +03:00
										 |  |  |     CSSPixels m_table_height { 0 }; | 
					
						
							| 
									
										
										
										
											2022-12-15 12:57:36 +00:00
										 |  |  |     CSSPixels m_automatic_content_height { 0 }; | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-05 18:44:04 +03:00
										 |  |  |     Optional<AvailableSpace> m_available_space; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |     struct Column { | 
					
						
							| 
									
										
										
										
											2022-12-15 12:57:36 +00:00
										 |  |  |         CSSPixels left_offset { 0 }; | 
					
						
							| 
									
										
										
										
											2023-06-08 03:37:26 +00:00
										 |  |  |         CSSPixels min_size { 0 }; | 
					
						
							|  |  |  |         CSSPixels max_size { 0 }; | 
					
						
							| 
									
										
										
										
											2022-12-15 12:57:36 +00:00
										 |  |  |         CSSPixels used_width { 0 }; | 
					
						
							| 
									
										
										
										
											2023-07-14 06:57:58 +00:00
										 |  |  |         bool has_percentage_width { false }; | 
					
						
							| 
									
										
										
										
											2023-05-24 10:50:57 +02:00
										 |  |  |         double percentage_width { 0 }; | 
					
						
							| 
									
										
										
										
											2023-06-28 03:06:03 +00:00
										 |  |  |         // Store whether the column is constrained: https://www.w3.org/TR/css-tables-3/#constrainedness
 | 
					
						
							|  |  |  |         bool is_constrained { false }; | 
					
						
							| 
									
										
										
										
											2023-07-15 02:46:47 +00:00
										 |  |  |         // Store whether the column has originating cells, defined in https://www.w3.org/TR/css-tables-3/#terminology.
 | 
					
						
							|  |  |  |         bool has_originating_cells { false }; | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct Row { | 
					
						
							| 
									
										
										
										
											2023-05-29 16:07:28 +03:00
										 |  |  |         JS::NonnullGCPtr<Box const> box; | 
					
						
							| 
									
										
										
										
											2023-04-29 00:43:05 +03:00
										 |  |  |         CSSPixels base_height { 0 }; | 
					
						
							|  |  |  |         CSSPixels reference_height { 0 }; | 
					
						
							|  |  |  |         CSSPixels final_height { 0 }; | 
					
						
							| 
									
										
										
										
											2022-12-15 12:57:36 +00:00
										 |  |  |         CSSPixels baseline { 0 }; | 
					
						
							| 
									
										
										
										
											2023-06-08 03:37:26 +00:00
										 |  |  |         CSSPixels min_size { 0 }; | 
					
						
							|  |  |  |         CSSPixels max_size { 0 }; | 
					
						
							| 
									
										
										
										
											2023-07-14 06:57:58 +00:00
										 |  |  |         bool has_percentage_height { false }; | 
					
						
							| 
									
										
										
										
											2023-06-08 03:37:26 +00:00
										 |  |  |         double percentage_height { 0 }; | 
					
						
							| 
									
										
										
										
											2023-07-13 03:33:11 +00:00
										 |  |  |         // Store whether the row is constrained: https://www.w3.org/TR/css-tables-3/#constrainedness
 | 
					
						
							|  |  |  |         bool is_constrained { false }; | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct Cell { | 
					
						
							| 
									
										
										
										
											2023-05-29 16:07:28 +03:00
										 |  |  |         JS::NonnullGCPtr<Box const> box; | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |         size_t column_index; | 
					
						
							|  |  |  |         size_t row_index; | 
					
						
							|  |  |  |         size_t column_span; | 
					
						
							| 
									
										
										
										
											2022-12-09 13:36:56 +03:00
										 |  |  |         size_t row_span; | 
					
						
							| 
									
										
										
										
											2022-12-15 12:57:36 +00:00
										 |  |  |         CSSPixels baseline { 0 }; | 
					
						
							| 
									
										
										
										
											2023-07-13 02:44:47 +00:00
										 |  |  |         CSSPixels outer_min_width { 0 }; | 
					
						
							|  |  |  |         CSSPixels outer_max_width { 0 }; | 
					
						
							|  |  |  |         CSSPixels outer_min_height { 0 }; | 
					
						
							|  |  |  |         CSSPixels outer_max_height { 0 }; | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 03:37:26 +00:00
										 |  |  |     // Accessors to enable direction-agnostic table measurement.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<class RowOrColumn> | 
					
						
							|  |  |  |     static size_t cell_span(Cell const& cell); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<class RowOrColumn> | 
					
						
							|  |  |  |     static size_t cell_index(Cell const& cell); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<class RowOrColumn> | 
					
						
							|  |  |  |     static CSSPixels cell_min_size(Cell const& cell); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<class RowOrColumn> | 
					
						
							|  |  |  |     static CSSPixels cell_max_size(Cell const& cell); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-16 02:48:56 +00:00
										 |  |  |     template<class RowOrColumn> | 
					
						
							|  |  |  |     CSSPixels border_spacing(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-08 03:37:26 +00:00
										 |  |  |     template<class RowOrColumn> | 
					
						
							|  |  |  |     Vector<RowOrColumn>& table_rows_or_columns(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 03:55:39 +00:00
										 |  |  |     CSSPixels compute_row_content_height(Cell const& cell) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 03:39:06 +00:00
										 |  |  |     enum class ConflictingSide { | 
					
						
							| 
									
										
										
										
											2023-06-04 14:21:20 +00:00
										 |  |  |         Top, | 
					
						
							|  |  |  |         Bottom, | 
					
						
							|  |  |  |         Left, | 
					
						
							| 
									
										
										
										
											2023-07-02 03:39:06 +00:00
										 |  |  |         Right, | 
					
						
							| 
									
										
										
										
											2023-06-04 14:21:20 +00:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 03:39:06 +00:00
										 |  |  |     struct ConflictingEdge { | 
					
						
							|  |  |  |         Node const* element; | 
					
						
							|  |  |  |         ConflictingSide side; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 03:02:54 +00:00
										 |  |  |     static const CSS::BorderData& border_data_conflicting_edge(ConflictingEdge const& conflicting_edge); | 
					
						
							| 
									
										
										
										
											2023-07-02 03:39:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-04 14:21:20 +00:00
										 |  |  |     class BorderConflictFinder { | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         BorderConflictFinder(TableFormattingContext const* context); | 
					
						
							| 
									
										
										
										
											2023-07-02 03:39:06 +00:00
										 |  |  |         Vector<ConflictingEdge> conflicting_edges(Cell const&, ConflictingSide) const; | 
					
						
							| 
									
										
										
										
											2023-06-04 14:21:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private: | 
					
						
							|  |  |  |         void collect_conflicting_col_elements(); | 
					
						
							| 
									
										
										
										
											2023-07-09 23:04:24 +00:00
										 |  |  |         void collect_conflicting_row_group_elements(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         struct RowGroupInfo { | 
					
						
							|  |  |  |             Node const* row_group; | 
					
						
							|  |  |  |             size_t start_index; | 
					
						
							|  |  |  |             size_t row_count; | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2023-06-04 14:21:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Vector<Node const*> m_col_elements_by_index; | 
					
						
							| 
									
										
										
										
											2023-07-09 23:04:24 +00:00
										 |  |  |         Vector<Optional<RowGroupInfo>> m_row_group_elements_by_index; | 
					
						
							| 
									
										
										
										
											2023-06-04 14:21:20 +00:00
										 |  |  |         TableFormattingContext const* m_context; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |     Vector<Cell> m_cells; | 
					
						
							| 
									
										
										
										
											2023-07-09 03:02:54 +00:00
										 |  |  |     Vector<Vector<Optional<Cell const&>>> m_cells_by_coordinate; | 
					
						
							| 
									
										
										
										
											2022-12-04 22:39:38 +03:00
										 |  |  |     Vector<Column> m_columns; | 
					
						
							|  |  |  |     Vector<Row> m_rows; | 
					
						
							| 
									
										
										
										
											2020-11-22 13:38:18 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |