TableFormattingDetails.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_LAYOUT_TABLEFORMATTINGDETAILS_H
  29. #define RMLUI_CORE_LAYOUT_TABLEFORMATTINGDETAILS_H
  30. #include "../../../Include/RmlUi/Core/StyleTypes.h"
  31. #include "../../../Include/RmlUi/Core/Types.h"
  32. #include <float.h>
  33. namespace Rml {
  34. class TableWrapper;
  35. struct ComputedAxisSize;
  36. /*
  37. TableGrid builds the structure of the table, that is a list of rows, columns, and cells, taking
  38. spanning attributes into account to position cells.
  39. */
  40. class TableGrid {
  41. public:
  42. // Build a list of columns, rows, and cells in this table.
  43. bool Build(Element* element_table, TableWrapper& table_wrapper);
  44. struct Column {
  45. Element* element_column = nullptr; // The '<col>' element which begins at this column, or nullptr if there is no such element or if the column
  46. // is spanned from a previous column.
  47. Element* element_group = nullptr; // The '<colgroup>' element which begins at this column, otherwise nullptr.
  48. Element* element_cell = nullptr; // The '<td>' element of the first row which begins at this column, otherwise nullptr.
  49. int column_span = 0; // The span of the '<col>' element.
  50. int group_span = 0; // The span of the '<colgroup>' element.
  51. int cell_span = 0; // The colspan of the '<td>' element.
  52. };
  53. struct Row {
  54. Element* element_row = nullptr; // The '<tr>' element which begins at this column, or nullptr if there is no such element or if the column is
  55. // spanned from a previous column.
  56. Element* element_group = nullptr; // The '<tbody>' element which begins at this column, otherwise nullptr.
  57. int group_span = 0; // The span of the '<tbody>' element.
  58. };
  59. struct Cell {
  60. Element* element_cell = nullptr; // The <td> element.
  61. int row_begin = 0, row_last = 0; // The first and last rows the cell spans.
  62. int column_begin = 0, column_last = 0; // The first and last columns the cell spans.
  63. };
  64. using ColumnList = Vector<Column>;
  65. using RowList = Vector<Row>;
  66. using CellList = Vector<Cell>;
  67. ColumnList columns;
  68. RowList rows;
  69. CellList cells;
  70. private:
  71. void PushColumn(Element* element_column, int column_span);
  72. void PushColumnGroup(Element* element_column_group);
  73. void PushOrMergeColumnsFromFirstRow(Element* element_cell, int column_begin, int span);
  74. void PushRow(Element* element_row, ElementList cell_elements, TableWrapper& table_wrapper);
  75. CellList open_cells;
  76. };
  77. enum class TrackSizingMode { Auto, Fixed, Flexible };
  78. /*
  79. TrackMetric describes the size and the edges of a given track (row or column) in the table.
  80. */
  81. struct TrackMetric {
  82. // All sizes are defined in terms of the border size of cells in the row or column.
  83. TrackSizingMode sizing_mode = TrackSizingMode::Auto;
  84. float fixed_size = 0;
  85. float flex_size = 0;
  86. float min_size = 0;
  87. float max_size = FLT_MAX;
  88. // The following are used for row/column elements.
  89. float column_padding_border_a = 0;
  90. float column_padding_border_b = 0;
  91. // The following are used for group elements.
  92. float group_padding_border_a = 0;
  93. float group_padding_border_b = 0;
  94. // The margins are the sum of the margins from all cells, tracks, and group elements.
  95. float sum_margin_a = 0;
  96. float sum_margin_b = 0;
  97. };
  98. using TrackMetricList = Vector<TrackMetric>;
  99. /*
  100. TracksSizing is a helper class for building the track metrics, with methods applicable to both rows and columns sizing.
  101. */
  102. class TracksSizing {
  103. public:
  104. TracksSizing(TrackMetricList& metrics, float table_initial_content_size, float table_gap) :
  105. metrics(metrics), table_initial_content_size(table_initial_content_size), table_gap(table_gap)
  106. {}
  107. // Apply group element. This sets the initial size of edges.
  108. void ApplyGroupElement(const int index, const int span, const ComputedAxisSize& computed);
  109. // Apply track element. This merges its edges, and sets the initial content size.
  110. void ApplyTrackElement(const int index, const int span, const ComputedAxisSize& computed);
  111. // Apply cell element for column sizing. This merges its content size and margins.
  112. void ApplyCellElement(const int index, const int span, const ComputedAxisSize& computed);
  113. // Convert flexible size to fixed size for all tracks.
  114. void ResolveFlexibleSize();
  115. private:
  116. void GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedAxisSize& computed) const;
  117. // Fill the track metric with fixed, flexible and min/max size, based on the element's computed values.
  118. void InitializeSize(TrackMetric& metric, float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b,
  119. const ComputedAxisSize& computed, const int span, const Style::BoxSizing target_box) const;
  120. TrackMetricList& metrics;
  121. const float table_initial_content_size;
  122. const float table_gap;
  123. };
  124. /*
  125. TrackBox represents the size and offset of any given track (row or column).
  126. Rows: Sizes == Heights. Offsets along vertical axis.
  127. Columns: Sizes == Widths. Offsets along horizontal axis.
  128. */
  129. struct TrackBox {
  130. float cell_size = 0; // The *border* size of cells in this track, does not account for spanning cells.
  131. float cell_offset = 0; // Offset from the table content box to the border box of cells in this track.
  132. float track_size = 0; // The *content* size of the row/column element, which may span multiple tracks.
  133. float track_offset = 0; // Offset from the table content box to the border box of the row/column element.
  134. float group_size = 0; // The *content* size of the group element, which may span multiple tracks.
  135. float group_offset = 0; // Offset from the table content box to the border box of the group element.
  136. };
  137. using TrackBoxList = Vector<TrackBox>;
  138. // Build a list of column boxes from the provided metrics.
  139. // @return The accumulated width of all columns.
  140. float BuildColumnBoxes(TrackBoxList& column_boxes, const TrackMetricList& column_metrics, const TableGrid::ColumnList& grid_columns,
  141. float table_gap_x);
  142. // Build a list of row boxes from the provided metrics.
  143. // @return The accumulated height of all rows.
  144. float BuildRowBoxes(TrackBoxList& row_boxes, const TrackMetricList& row_metrics, const TableGrid::RowList& grid_rows, float table_gap_y);
  145. // Return the border size of a cell spanning one or multiple tracks.
  146. inline float GetSpanningCellBorderSize(const TrackBoxList& boxes, const int index, const int index_last_span)
  147. {
  148. RMLUI_ASSERT(index < (int)boxes.size() && index_last_span < (int)boxes.size());
  149. return boxes[index_last_span].cell_size + (boxes[index_last_span].cell_offset - boxes[index].cell_offset);
  150. }
  151. } // namespace Rml
  152. #endif