TableFormattingContext.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. private:
  28. CSSPixels run_caption_layout(LayoutMode, CSS::CaptionSide);
  29. CSSPixels compute_capmin();
  30. void calculate_row_column_grid(Box const&);
  31. void compute_cell_measures(AvailableSpace const& available_space);
  32. template<class RowOrColumn>
  33. void initialize_table_measures();
  34. template<class RowOrColumn>
  35. void compute_table_measures();
  36. void compute_table_width();
  37. void distribute_width_to_columns();
  38. void compute_table_height(LayoutMode layout_mode);
  39. void distribute_height_to_rows();
  40. void position_row_boxes();
  41. void position_cell_boxes();
  42. void border_conflict_resolution();
  43. CSSPixels border_spacing_horizontal() const;
  44. CSSPixels border_spacing_vertical() const;
  45. CSSPixels m_table_height { 0 };
  46. CSSPixels m_automatic_content_height { 0 };
  47. Optional<AvailableSpace> m_available_space;
  48. enum class SizeType {
  49. Percent,
  50. Pixel,
  51. Auto
  52. };
  53. struct Column {
  54. SizeType type { SizeType::Auto };
  55. CSSPixels left_offset { 0 };
  56. CSSPixels min_size { 0 };
  57. CSSPixels max_size { 0 };
  58. CSSPixels used_width { 0 };
  59. double percentage_width { 0 };
  60. // Store whether the column is constrained: https://www.w3.org/TR/css-tables-3/#constrainedness
  61. bool is_constrained { false };
  62. };
  63. struct Row {
  64. JS::NonnullGCPtr<Box const> box;
  65. CSSPixels base_height { 0 };
  66. CSSPixels reference_height { 0 };
  67. CSSPixels final_height { 0 };
  68. CSSPixels baseline { 0 };
  69. SizeType type { SizeType::Auto };
  70. CSSPixels min_size { 0 };
  71. CSSPixels max_size { 0 };
  72. double percentage_height { 0 };
  73. };
  74. struct Cell {
  75. JS::NonnullGCPtr<Box const> box;
  76. size_t column_index;
  77. size_t row_index;
  78. size_t column_span;
  79. size_t row_span;
  80. CSSPixels baseline { 0 };
  81. CSSPixels min_width { 0 };
  82. CSSPixels max_width { 0 };
  83. CSSPixels min_height { 0 };
  84. CSSPixels max_height { 0 };
  85. };
  86. // Accessors to enable direction-agnostic table measurement.
  87. template<class RowOrColumn>
  88. static size_t cell_span(Cell const& cell);
  89. template<class RowOrColumn>
  90. static size_t cell_index(Cell const& cell);
  91. template<class RowOrColumn>
  92. static CSSPixels cell_min_size(Cell const& cell);
  93. template<class RowOrColumn>
  94. static CSSPixels cell_max_size(Cell const& cell);
  95. template<class RowOrColumn>
  96. CSSPixels border_spacing();
  97. template<class RowOrColumn>
  98. Vector<RowOrColumn>& table_rows_or_columns();
  99. CSSPixels compute_row_content_height(Cell const& cell) const;
  100. enum class ConflictingEdge {
  101. Top,
  102. Right,
  103. Bottom,
  104. Left,
  105. };
  106. class BorderConflictFinder {
  107. public:
  108. BorderConflictFinder(TableFormattingContext const* context);
  109. Vector<Node const*> conflicting_elements(Cell const&, ConflictingEdge) const;
  110. private:
  111. void collect_conflicting_col_elements();
  112. Vector<Node const*> m_col_elements_by_index;
  113. TableFormattingContext const* m_context;
  114. };
  115. Vector<Cell> m_cells;
  116. Vector<Column> m_columns;
  117. Vector<Row> m_rows;
  118. };
  119. }