LayoutTable.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. #include "LayoutTable.h"
  29. #include "LayoutDetails.h"
  30. #include "LayoutEngine.h"
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. #include "../../Include/RmlUi/Core/Types.h"
  34. #include <cstddef>
  35. #include <algorithm>
  36. namespace Rml {
  37. LayoutTable::CloseResult LayoutTable::FormatTable(LayoutBlockBox* table_block_context_box, Element* element_table)
  38. {
  39. const Vector2f content_top_left = table_block_context_box->GetBox().GetPosition();
  40. const Vector2f content_containing_block = Vector2f(table_block_context_box->GetBox().GetSize().x, Math::Max(0.0f, table_block_context_box->GetBox().GetSize().y));
  41. const Vector2f table_gap(10.f, 0.f);
  42. Vector<float> column_border_widths = DetermineColumnWidths(element_table, content_containing_block, table_gap);
  43. // Now that we have the size of each column, we can move on to formatting the elements.
  44. // After we format and size an element, we record its height as well, and keep the maximum_height over all cells in the current row.
  45. // At the end of a row, we then know the height of the row, and we can proceed by positioning the cells.
  46. Vector2f table_content_overflow_size;
  47. Vector2f table_cursor = content_top_left;
  48. table_cursor.y -= table_gap.y;
  49. struct Cell {
  50. Element* element_cell;
  51. int row_last; // The last row the cell spans
  52. int column_begin, column_last;
  53. Box box;
  54. Vector2f table_offset;
  55. float rows_accumulated_height;
  56. };
  57. Vector<Cell> cells;
  58. cells.reserve(column_border_widths.size());
  59. const int num_table_children = element_table->GetNumChildren();
  60. int row = -1;
  61. for (int i = 0; i < num_table_children; i++)
  62. {
  63. Element* element_row = element_table->GetChild(i);
  64. const ComputedValues& computed_row = element_row->GetComputedValues();
  65. if (computed_row.display != Style::Display::TableRow)
  66. continue;
  67. row += 1;
  68. Box row_box;
  69. float row_min_height, row_max_height;
  70. LayoutDetails::BuildBox(row_box, content_containing_block, element_row, false, 0.f);
  71. LayoutDetails::GetMinMaxHeight(row_min_height, row_max_height, &computed_row, row_box, content_containing_block.y);
  72. // Prepare the cursor for this row
  73. table_cursor.x = content_top_left.x;
  74. table_cursor.y += table_gap.y;
  75. const Vector2f row_element_offset = table_cursor + Vector2f(row_box.GetEdge(Box::MARGIN, Box::LEFT), row_box.GetEdge(Box::MARGIN, Box::TOP));
  76. {
  77. const float row_top_offset = row_box.GetEdge(Box::MARGIN, Box::TOP) + row_box.GetEdge(Box::BORDER, Box::TOP) + row_box.GetEdge(Box::PADDING, Box::TOP);
  78. table_cursor.y += row_top_offset;
  79. for (Cell& cell : cells)
  80. cell.rows_accumulated_height += row_top_offset;
  81. }
  82. const int num_cells_spanning_this_row = (int)cells.size();
  83. const int num_row_children = element_row->GetNumChildren();
  84. // For all child cell elements of this row, add them to the list of cells and determine their position.
  85. for (int j = 0, column = 0; j < num_row_children; j++)
  86. {
  87. Element* element_cell = element_row->GetChild(j);
  88. const ComputedValues& computed_cell = element_cell->GetComputedValues();
  89. if (computed_cell.display != Style::Display::TableCell)
  90. continue;
  91. const int row_span = Math::Max(1, element_cell->GetAttribute("rowspan", 1));
  92. const int col_span = Math::Max(1, element_cell->GetAttribute("colspan", 1));
  93. float spanning_column_border_width = 0;
  94. int column_last = -1;
  95. {
  96. // Offset the column if we have any rowspan elements from previous rows overlapping with the current column.
  97. const int column_offset_from = column;
  98. for (bool continue_offset_column = true; continue_offset_column; )
  99. {
  100. continue_offset_column = false;
  101. for (int k = 0; k < num_cells_spanning_this_row; k++)
  102. {
  103. if (column >= cells[k].column_begin && column <= cells[k].column_last)
  104. {
  105. column = cells[k].column_last + 1;
  106. continue_offset_column = true;
  107. break;
  108. }
  109. }
  110. }
  111. column_last = column + col_span - 1;
  112. if (column_last >= (int)column_border_widths.size())
  113. {
  114. Log::Message(Log::LT_WARNING, "Too many columns in table row %d: %s\nThe number of columns is %d, as determined by the first table row.",
  115. row + 1, element_row->GetAddress().c_str(), (int)column_border_widths.size());
  116. break;
  117. }
  118. for (int k = column_offset_from; k < column; k++)
  119. table_cursor.x += column_border_widths[k];
  120. table_cursor.x += table_gap.x * float(column - column_offset_from);
  121. for (int k = column; k <= column_last; k++)
  122. spanning_column_border_width += column_border_widths[k];
  123. spanning_column_border_width += table_gap.x * float(col_span - 1);
  124. }
  125. // Add the new cell to our list.
  126. cells.emplace_back();
  127. Cell& cell = cells.back();
  128. cell.row_last = row + row_span - 1;
  129. cell.column_begin = column;
  130. cell.column_last = column_last;
  131. cell.element_cell = element_cell;
  132. cell.table_offset = table_cursor;
  133. cell.rows_accumulated_height = 0;
  134. // Determine the cell's box for formatting later, we may get an indefinite (-1) vertical content size.
  135. Box& box = cell.box;
  136. LayoutDetails::BuildBox(box, content_containing_block, element_cell, false, 0.f);
  137. // Determine the box width from the current column width.
  138. const float content_width = Math::Max(0.0f, spanning_column_border_width - box.GetSizeAcross(Box::HORIZONTAL, Box::BORDER, Box::PADDING));
  139. box.SetContent(Vector2f(content_width, box.GetSize().y));
  140. table_cursor.x += spanning_column_border_width + table_gap.x;
  141. column += col_span;
  142. }
  143. // Partition the cells to determine those who end at this row.
  144. const auto it_cells_in_row_end = std::partition(cells.begin(), cells.end(), [row](const Cell& cell) { return cell.row_last == row; });
  145. // Determine the row height.
  146. float row_content_height = 0;
  147. if (row_box.GetSize().y >= 0)
  148. {
  149. // The row has a definite size, use that.
  150. row_content_height = row_box.GetSize().y;
  151. }
  152. else
  153. {
  154. // The row does not have a definite size, we will use the maximum height of all its cells to determine the row height.
  155. // For each cell in this row, or spanning onto this row from any previous rows, increase the row height as necessary to make the cell fit.
  156. for (auto it = cells.begin(); it != it_cells_in_row_end; ++it)
  157. {
  158. Cell& cell = *it;
  159. Element* element_cell = cell.element_cell;
  160. Box& box = cell.box;
  161. // If the cell's height is also indefinite, we need to format it to get its height.
  162. if (box.GetSize().y < 0)
  163. {
  164. LayoutEngine::FormatElement(element_cell, content_containing_block, &box);
  165. box.SetContent(element_cell->GetBox().GetSize());
  166. }
  167. row_content_height = Math::Max(row_content_height, box.GetSizeAcross(Box::VERTICAL, Box::BORDER) - cell.rows_accumulated_height);
  168. }
  169. }
  170. row_content_height = Math::Clamp(row_content_height, row_min_height, row_max_height);
  171. for (Cell& cell : cells)
  172. cell.rows_accumulated_height += row_content_height;
  173. // Now we have the height of the row, position and format each cell.
  174. auto FormatCellsInRow = [row, &cells, it_cells_in_row_end, &table_content_overflow_size, element_table, content_containing_block, content_top_left]() {
  175. // Assumes the cells are already partitioned.
  176. for (auto it = cells.begin(); it != it_cells_in_row_end; ++it)
  177. {
  178. Cell& cell = *it;
  179. Element* element_cell = cell.element_cell;
  180. Box& box = cell.box;
  181. Style::VerticalAlign vertical_align = cell.element_cell->GetComputedValues().vertical_align;
  182. if (box.GetSize().y < 0)
  183. {
  184. const bool is_aligned = (vertical_align.type == Style::VerticalAlign::Middle || vertical_align.type == Style::VerticalAlign::Bottom);
  185. if (is_aligned)
  186. {
  187. // The size of the cell is indefinite, we need to get the height by formatting the cell.
  188. LayoutEngine::FormatElement(element_cell, content_containing_block, &box);
  189. box.SetContent(element_cell->GetBox().GetSize());
  190. }
  191. else
  192. {
  193. box.SetContent(Vector2f(box.GetSize().x, Math::Max(0.0f, cell.rows_accumulated_height - box.GetSizeAcross(Box::VERTICAL, Box::BORDER, Box::PADDING))));
  194. }
  195. }
  196. const float available_height = cell.rows_accumulated_height - box.GetSizeAcross(Box::VERTICAL, Box::BORDER);
  197. if (available_height > 0)
  198. {
  199. // Pad the cell for vertical alignment
  200. float add_padding_top;
  201. float add_padding_bottom;
  202. switch (vertical_align.type)
  203. {
  204. case Style::VerticalAlign::Bottom:
  205. add_padding_top = available_height;
  206. add_padding_bottom = 0;
  207. break;
  208. case Style::VerticalAlign::Middle:
  209. add_padding_top = 0.5f * available_height;
  210. add_padding_bottom = 0.5f * available_height;
  211. break;
  212. case Style::VerticalAlign::Top:
  213. default:
  214. add_padding_top = 0.0f;
  215. add_padding_bottom = available_height;
  216. }
  217. box.SetEdge(Box::PADDING, Box::TOP, box.GetEdge(Box::PADDING, Box::TOP) + add_padding_top);
  218. box.SetEdge(Box::PADDING, Box::BOTTOM, box.GetEdge(Box::PADDING, Box::BOTTOM) + add_padding_bottom);
  219. }
  220. {
  221. // Format the cell in a new block formatting context.
  222. Vector2f cell_visible_overflow_size;
  223. LayoutEngine::FormatElement(element_cell, content_containing_block, &box, &cell_visible_overflow_size);
  224. table_content_overflow_size.x = Math::Max(table_content_overflow_size.x, cell.table_offset.x - content_top_left.x + cell_visible_overflow_size.x);
  225. table_content_overflow_size.y = Math::Max(table_content_overflow_size.y, cell.table_offset.y - content_top_left.y + cell_visible_overflow_size.y);
  226. // Set the position of the element within the the table container
  227. element_cell->SetOffset(cell.table_offset, element_table);
  228. }
  229. }
  230. // Remove the formatted cells from pending
  231. cells.erase(cells.begin(), it_cells_in_row_end);
  232. };
  233. FormatCellsInRow();
  234. // TODO: What to do with any remaining row-spanning cells after the last row?
  235. // Position and size the row element
  236. if (row_box.GetSize().y < 0.0f)
  237. row_box.SetContent(Vector2f(row_box.GetSize().x, row_content_height));
  238. element_row->SetOffset(row_element_offset, element_table);
  239. element_row->SetBox(row_box);
  240. const float row_bottom_offset = row_box.GetEdge(Box::MARGIN, Box::BOTTOM) + row_box.GetEdge(Box::BORDER, Box::BOTTOM) + row_box.GetEdge(Box::PADDING, Box::BOTTOM);
  241. table_cursor.y += row_content_height + row_bottom_offset;
  242. for (Cell& cell : cells)
  243. cell.rows_accumulated_height += row_bottom_offset;
  244. }
  245. table_block_context_box->ExtendInnerContentSize(table_content_overflow_size);
  246. CloseResult result = table_block_context_box->Close();
  247. return result;
  248. }
  249. LayoutTable::ColumnWidths LayoutTable::DetermineColumnWidths(Element* element_table, const Vector2f containing_block, const Vector2f table_gap)
  250. {
  251. ColumnWidths column_widths;
  252. // Determine the widths of the cells.
  253. Element* element_first_row = nullptr;
  254. for (int i = 0; i < element_table->GetNumChildren(); i++)
  255. {
  256. element_first_row = element_table->GetChild(i);
  257. if (element_first_row->GetDisplay() != Style::Display::TableRow)
  258. {
  259. Log::Message(Log::LT_WARNING, "Only table rows ('display: table-row') are allowed as children of tables. %s", element_first_row->GetAddress().c_str());
  260. }
  261. else
  262. {
  263. break;
  264. }
  265. }
  266. if (!element_first_row)
  267. {
  268. Log::Message(Log::LT_WARNING, "No rows in table. %s", element_table->GetAddress().c_str());
  269. // TODO: Handle this more gracefully.
  270. return ColumnWidths();
  271. }
  272. const int num_row_children = element_first_row->GetNumChildren();
  273. struct CellMetrics {
  274. float fixed_content_width;
  275. float padding_border_edges_width;
  276. float flex_width;
  277. float min_content_width;
  278. float max_content_width;
  279. };
  280. Vector<CellMetrics> cell_metrics;
  281. cell_metrics.reserve(num_row_children);
  282. for (int i = 0; i < num_row_children; i++)
  283. {
  284. Element* element_cell = element_first_row->GetChild(i);
  285. const ComputedValues& computed = element_cell->GetComputedValues();
  286. if (computed.display != Style::Display::TableCell)
  287. {
  288. Log::Message(Log::LT_WARNING, "Only table cells ('display: table-cell') are allowed as children of table rows. %s", element_cell->GetAddress().c_str());
  289. continue;
  290. }
  291. const float padding_border_edges_width =
  292. Math::Max(0.0f, ResolveValue(computed.padding_left, containing_block.x)) +
  293. Math::Max(0.0f, ResolveValue(computed.padding_right, containing_block.x)) +
  294. Math::Max(0.0f, computed.border_left_width) +
  295. Math::Max(0.0f, computed.border_right_width);
  296. auto min_max_widths = [containing_block_width = containing_block.x](float& min_width, float& max_width, float border_padding_edges_width, const ComputedValues& computed) {
  297. min_width = ResolveValue(computed.min_width, containing_block_width);
  298. max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, containing_block_width));
  299. if (computed.box_sizing == Style::BoxSizing::BorderBox)
  300. {
  301. min_width = Math::Max(0.0f, min_width - border_padding_edges_width);
  302. max_width = Math::Max(0.0f, max_width - border_padding_edges_width);
  303. }
  304. };
  305. CellMetrics metric = {};
  306. metric.padding_border_edges_width = padding_border_edges_width;
  307. min_max_widths(metric.min_content_width, metric.max_content_width, padding_border_edges_width, computed);
  308. if (computed.width.type == Style::Width::Auto)
  309. {
  310. metric.flex_width = 1;
  311. }
  312. else
  313. {
  314. if (computed.box_sizing == Style::BoxSizing::ContentBox)
  315. metric.fixed_content_width = ResolveValue(computed.width, containing_block.x);
  316. else
  317. metric.fixed_content_width = Math::Max(0.f, ResolveValue(computed.width, containing_block.x - padding_border_edges_width));
  318. metric.fixed_content_width = Math::Clamp(metric.fixed_content_width, metric.min_content_width, metric.max_content_width);
  319. }
  320. const int colspan = Math::Max(1, element_cell->GetAttribute("colspan", 1));
  321. if (colspan > 1)
  322. {
  323. const float width_factor = 1.f / float(colspan);
  324. metric.fixed_content_width *= width_factor;
  325. metric.min_content_width *= width_factor;
  326. metric.max_content_width *= width_factor;
  327. }
  328. for(int j = 0; j < colspan; j++)
  329. cell_metrics.push_back(metric);
  330. }
  331. auto calculate_fr_to_px_ratio = [table_content_width = containing_block.x, column_gap = table_gap.x, &cell_metrics]() {
  332. float sum_fixed_width = 0; // [px]
  333. float sum_flex_width = 0; // [fr]
  334. for (const CellMetrics& metric : cell_metrics)
  335. {
  336. sum_flex_width += metric.flex_width;
  337. sum_fixed_width += (metric.flex_width == 0.f ? metric.fixed_content_width : 0.0f) + metric.padding_border_edges_width + column_gap;
  338. }
  339. sum_flex_width = Math::Max(1.f, sum_flex_width);
  340. sum_fixed_width = Math::Max(0.f, sum_fixed_width - column_gap);
  341. const float available_flex_width = table_content_width - sum_fixed_width;
  342. const float fr_to_px_ratio = available_flex_width / sum_flex_width;
  343. return fr_to_px_ratio;
  344. };
  345. for (bool continue_iteration = true; continue_iteration; )
  346. {
  347. continue_iteration = false;
  348. const float fr_to_px_ratio = calculate_fr_to_px_ratio();
  349. for (auto& metric : cell_metrics)
  350. {
  351. if (metric.flex_width > 0)
  352. {
  353. const float fixed_flex_width = metric.flex_width * fr_to_px_ratio;
  354. metric.fixed_content_width = Math::Clamp(fixed_flex_width, metric.min_content_width, metric.max_content_width);
  355. if (metric.fixed_content_width != fixed_flex_width)
  356. {
  357. // We met a min/max-constraint, fix the size of this column.
  358. metric.flex_width = 0.0f;
  359. continue_iteration = true;
  360. }
  361. }
  362. }
  363. }
  364. column_widths.resize(cell_metrics.size());
  365. for (size_t i = 0; i < cell_metrics.size(); i++)
  366. {
  367. column_widths[i] = cell_metrics[i].fixed_content_width + cell_metrics[i].padding_border_edges_width;
  368. }
  369. // TODO: What to do with any left-over space? Distribute evenly across columns, or make table smaller etc.
  370. return column_widths;
  371. }
  372. } // namespace Rml