TableFormattingContext.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <LibWeb/Layout/FormattingContext.h>
  9. #include <LibWeb/Layout/TableWrapper.h>
  10. namespace Web::Layout {
  11. enum class TableDimension {
  12. Row,
  13. Column
  14. };
  15. class TableFormattingContext final : public FormattingContext {
  16. public:
  17. explicit TableFormattingContext(LayoutState&, Box const&, FormattingContext* parent);
  18. ~TableFormattingContext();
  19. virtual void run(Box const&, LayoutMode, AvailableSpace const&) override;
  20. virtual CSSPixels automatic_content_width() const override;
  21. virtual CSSPixels automatic_content_height() const override;
  22. Box const& table_box() const { return context_box(); }
  23. TableWrapper const& table_wrapper() const
  24. {
  25. return verify_cast<TableWrapper>(*table_box().containing_block());
  26. }
  27. static bool border_is_less_specific(const CSS::BorderData& a, const CSS::BorderData& b);
  28. private:
  29. CSSPixels run_caption_layout(LayoutMode, CSS::CaptionSide);
  30. CSSPixels compute_capmin();
  31. void calculate_row_column_grid(Box const&);
  32. void compute_constrainedness();
  33. void compute_cell_measures(AvailableSpace const& available_space);
  34. template<class RowOrColumn>
  35. void initialize_table_measures();
  36. template<class RowOrColumn>
  37. void compute_table_measures();
  38. void compute_table_width();
  39. void distribute_width_to_columns();
  40. void compute_table_height(LayoutMode layout_mode);
  41. void distribute_height_to_rows();
  42. void position_row_boxes();
  43. void position_cell_boxes();
  44. void border_conflict_resolution();
  45. CSSPixels border_spacing_horizontal() const;
  46. CSSPixels border_spacing_vertical() const;
  47. CSSPixels m_table_height { 0 };
  48. CSSPixels m_automatic_content_height { 0 };
  49. Optional<AvailableSpace> m_available_space;
  50. struct Column {
  51. CSSPixels left_offset { 0 };
  52. CSSPixels min_size { 0 };
  53. CSSPixels max_size { 0 };
  54. CSSPixels used_width { 0 };
  55. bool has_percentage_width { false };
  56. double percentage_width { 0 };
  57. // Store whether the column is constrained: https://www.w3.org/TR/css-tables-3/#constrainedness
  58. bool is_constrained { false };
  59. };
  60. struct Row {
  61. JS::NonnullGCPtr<Box const> box;
  62. CSSPixels base_height { 0 };
  63. CSSPixels reference_height { 0 };
  64. CSSPixels final_height { 0 };
  65. CSSPixels baseline { 0 };
  66. CSSPixels min_size { 0 };
  67. CSSPixels max_size { 0 };
  68. bool has_percentage_height { false };
  69. double percentage_height { 0 };
  70. // Store whether the row is constrained: https://www.w3.org/TR/css-tables-3/#constrainedness
  71. bool is_constrained { false };
  72. };
  73. struct Cell {
  74. JS::NonnullGCPtr<Box const> box;
  75. size_t column_index;
  76. size_t row_index;
  77. size_t column_span;
  78. size_t row_span;
  79. CSSPixels baseline { 0 };
  80. CSSPixels outer_min_width { 0 };
  81. CSSPixels outer_max_width { 0 };
  82. CSSPixels outer_min_height { 0 };
  83. CSSPixels outer_max_height { 0 };
  84. };
  85. // Accessors to enable direction-agnostic table measurement.
  86. template<class RowOrColumn>
  87. static size_t cell_span(Cell const& cell);
  88. template<class RowOrColumn>
  89. static size_t cell_index(Cell const& cell);
  90. template<class RowOrColumn>
  91. static CSSPixels cell_min_size(Cell const& cell);
  92. template<class RowOrColumn>
  93. static CSSPixels cell_max_size(Cell const& cell);
  94. template<class RowOrColumn>
  95. CSSPixels border_spacing();
  96. template<class RowOrColumn>
  97. Vector<RowOrColumn>& table_rows_or_columns();
  98. CSSPixels compute_row_content_height(Cell const& cell) const;
  99. enum class ConflictingSide {
  100. Top,
  101. Bottom,
  102. Left,
  103. Right,
  104. };
  105. struct ConflictingEdge {
  106. Node const* element;
  107. ConflictingSide side;
  108. };
  109. static const CSS::BorderData& border_data_conflicting_edge(ConflictingEdge const& conflicting_edge);
  110. class BorderConflictFinder {
  111. public:
  112. BorderConflictFinder(TableFormattingContext const* context);
  113. Vector<ConflictingEdge> conflicting_edges(Cell const&, ConflictingSide) const;
  114. private:
  115. void collect_conflicting_col_elements();
  116. void collect_conflicting_row_group_elements();
  117. struct RowGroupInfo {
  118. Node const* row_group;
  119. size_t start_index;
  120. size_t row_count;
  121. };
  122. Vector<Node const*> m_col_elements_by_index;
  123. Vector<Optional<RowGroupInfo>> m_row_group_elements_by_index;
  124. TableFormattingContext const* m_context;
  125. };
  126. Vector<Cell> m_cells;
  127. Vector<Vector<Optional<Cell const&>>> m_cells_by_coordinate;
  128. Vector<Column> m_columns;
  129. Vector<Row> m_rows;
  130. };
  131. }