LayoutTableDetails.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 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_LAYOUTTABLEDETAILS_H
  29. #define RMLUI_CORE_LAYOUTTABLEDETAILS_H
  30. #include "../../Include/RmlUi/Core/Types.h"
  31. #include "../../Include/RmlUi/Core/ComputedValues.h"
  32. #include <float.h>
  33. namespace Rml {
  34. struct ComputedAxisSize;
  35. /*
  36. TableGrid builds the structure of the table, that is a list of rows, columns, and cells, taking
  37. spanning attributes into account to position cells.
  38. */
  39. class TableGrid
  40. {
  41. public:
  42. // Build a list of columns, rows, and cells in this table.
  43. bool Build(Element* element_table);
  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 is spanned from a previous column.
  46. Element* element_group = nullptr; // The '<colgroup>' element which begins at this column, otherwise nullptr.
  47. Element* element_cell = nullptr; // The '<td>' element of the first row which begins at this column, otherwise nullptr.
  48. int column_span = 0; // The span of the '<col>' element.
  49. int group_span = 0; // The span of the '<colgroup>' element.
  50. int cell_span = 0; // The colspan of the '<td>' element.
  51. };
  52. struct Row {
  53. 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 spanned from a previous column.
  54. Element* element_group = nullptr; // The '<tbody>' element which begins at this column, otherwise nullptr.
  55. int group_span = 0; // The span of the '<tbody>' element.
  56. };
  57. struct Cell {
  58. Element* element_cell = nullptr; // The <td> element.
  59. int row_begin = 0, row_last = 0; // The first and last rows the cell spans.
  60. int column_begin = 0, column_last = 0; // The first and last columns the cell spans.
  61. };
  62. using ColumnList = Vector<Column>;
  63. using RowList = Vector<Row>;
  64. using CellList = Vector<Cell>;
  65. ColumnList columns;
  66. RowList rows;
  67. CellList cells;
  68. private:
  69. void PushColumn(Element* element_column, int column_span);
  70. void PushColumnGroup(Element* element_column_group);
  71. void PushOrMergeColumnsFromFirstRow(Element* element_cell, int column_begin, int span);
  72. void PushRow(Element* element_row, ElementList cell_elements);
  73. CellList open_cells;
  74. };
  75. enum class TrackSizingMode { Auto, Fixed, Flexible };
  76. /*
  77. TrackMetric describes the size and the edges of a given track (row or column) in the table.
  78. */
  79. struct TrackMetric {
  80. // All sizes are defined in terms of the border size of cells in the row or column.
  81. TrackSizingMode sizing_mode = TrackSizingMode::Auto;
  82. float fixed_size = 0;
  83. float flex_size = 0;
  84. float min_size = 0;
  85. float max_size = FLT_MAX;
  86. // The following are used for row/column elements.
  87. float column_padding_border_a = 0;
  88. float column_padding_border_b = 0;
  89. // The following are used for group elements.
  90. float group_padding_border_a = 0;
  91. float group_padding_border_b = 0;
  92. // The margins are the sum of the margins from all cells, tracks, and group elements.
  93. float sum_margin_a = 0;
  94. float sum_margin_b = 0;
  95. };
  96. using TrackMetricList = Vector<TrackMetric>;
  97. /*
  98. TracksSizing is a helper class for building the track metrics, with methods applicable to both rows and columns sizing.
  99. */
  100. class TracksSizing {
  101. public:
  102. TracksSizing(TrackMetricList& metrics, float table_initial_content_size, float table_gap) : metrics(metrics), table_initial_content_size(table_initial_content_size), table_gap(table_gap)
  103. {}
  104. // Apply group element. This sets the initial size of edges.
  105. void ApplyGroupElement(const int index, const int span, const ComputedAxisSize& computed);
  106. // Apply track element. This merges its edges, and sets the initial content size.
  107. void ApplyTrackElement(const int index, const int span, const ComputedAxisSize& computed);
  108. // Apply cell element for column sizing. This merges its content size and margins.
  109. void ApplyCellElement(const int index, const int span, const ComputedAxisSize& computed);
  110. // Convert flexible size to fixed size for all tracks.
  111. void ResolveFlexibleSize();
  112. private:
  113. void GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedAxisSize& computed) const;
  114. // Fill the track metric with fixed, flexible and min/max size, based on the element's computed values.
  115. void InitializeSize(TrackMetric& metric, float& margin_a, float& margin_b, float& padding_border_a,
  116. float& padding_border_b, const ComputedAxisSize& computed, const int span, const Style::BoxSizing target_box) const;
  117. TrackMetricList& metrics;
  118. const float table_initial_content_size;
  119. const float table_gap;
  120. };
  121. /*
  122. TrackBox represents the size and offset of any given track (row or column).
  123. Rows: Sizes == Heights. Offsets along vertical axis.
  124. Columns: Sizes == Widths. Offsets along horizontal axis.
  125. */
  126. struct TrackBox {
  127. float cell_size = 0; // The *border* size of cells in this track, does not account for spanning cells.
  128. float cell_offset = 0; // Offset from the table content box to the border box of cells in this track.
  129. float track_size = 0; // The *content* size of the row/column element, which may span multiple tracks.
  130. float track_offset = 0; // Offset from the table content box to the border box of the row/column element.
  131. float group_size = 0; // The *content* size of the group element, which may span multiple tracks.
  132. float group_offset = 0; // Offset from the table content box to the border box of the group element.
  133. };
  134. using TrackBoxList = Vector<TrackBox>;
  135. // Build a list of column boxes from the provided metrics.
  136. // @return The accumulated width of all columns.
  137. float BuildColumnBoxes(TrackBoxList& column_boxes, const TrackMetricList& column_metrics, const TableGrid::ColumnList& grid_columns, float table_gap_x);
  138. // Build a list of row boxes from the provided metrics.
  139. // @return The accumulated height of all rows.
  140. float BuildRowBoxes(TrackBoxList& row_boxes, const TrackMetricList& row_metrics, const TableGrid::RowList& grid_rows, float table_gap_y);
  141. // Return the border size of a cell spanning one or multiple tracks.
  142. inline float GetSpanningCellBorderSize(const TrackBoxList& boxes, const int index, const int index_last_span)
  143. {
  144. RMLUI_ASSERT(index < (int)boxes.size() && index_last_span < (int)boxes.size());
  145. return boxes[index_last_span].cell_size + (boxes[index_last_span].cell_offset - boxes[index].cell_offset);
  146. }
  147. } // namespace Rml
  148. #endif