LayoutTable.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 ComputedValues& computed_table = element_table->GetComputedValues();
  42. const Vector2f table_gap(
  43. ResolveValue(computed_table.column_gap, content_containing_block.x),
  44. ResolveValue(computed_table.row_gap, content_containing_block.y)
  45. );
  46. Vector2f table_resulting_content_size = content_containing_block;
  47. Vector<Column> columns = DetermineColumnWidths(element_table, table_gap.x, table_resulting_content_size.x);
  48. // Now that we have the size of each column, we can move on to formatting the elements.
  49. // 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.
  50. // At the end of a row, we then know the height of the row, and we can proceed by positioning the cells.
  51. Vector2f table_content_overflow_size;
  52. Vector2f table_cursor = content_top_left;
  53. table_cursor.y -= table_gap.y;
  54. struct Cell {
  55. Element* element_cell;
  56. int row_last; // The last row the cell spans
  57. int column_begin, column_last;
  58. Box box;
  59. Vector2f table_offset;
  60. float rows_accumulated_height;
  61. };
  62. Vector<Cell> cells;
  63. cells.reserve(columns.size());
  64. const int num_table_children = element_table->GetNumChildren();
  65. for (int i = 0, row = -1; i < num_table_children; i++)
  66. {
  67. Element* element_row = element_table->GetChild(i);
  68. const ComputedValues& computed_row = element_row->GetComputedValues();
  69. if (computed_row.display != Style::Display::TableRow)
  70. continue;
  71. row += 1;
  72. Box row_box;
  73. float row_min_height, row_max_height;
  74. LayoutDetails::BuildBox(row_box, content_containing_block, element_row, false, 0.f);
  75. LayoutDetails::GetMinMaxHeight(row_min_height, row_max_height, &computed_row, row_box, content_containing_block.y);
  76. // Prepare the cursor for this row
  77. table_cursor.x = content_top_left.x;
  78. const Vector2f row_element_offset = table_cursor + Vector2f(0.0f, table_gap.y) + Vector2f(row_box.GetEdge(Box::MARGIN, Box::LEFT), row_box.GetEdge(Box::MARGIN, Box::TOP));
  79. {
  80. const float row_top_offset = table_gap.y + row_box.GetEdge(Box::MARGIN, Box::TOP) + row_box.GetEdge(Box::BORDER, Box::TOP) + row_box.GetEdge(Box::PADDING, Box::TOP);
  81. table_cursor.y += row_top_offset;
  82. for (Cell& cell : cells)
  83. cell.rows_accumulated_height += row_top_offset;
  84. }
  85. const int num_cells_spanning_this_row = (int)cells.size();
  86. const int num_row_children = element_row->GetNumChildren();
  87. // For all child cell elements of this row, add them to the list of cells and determine their position.
  88. for (int j = 0, column = 0; j < num_row_children; j++)
  89. {
  90. Element* element_cell = element_row->GetChild(j);
  91. const ComputedValues& computed_cell = element_cell->GetComputedValues();
  92. if (computed_cell.display != Style::Display::TableCell)
  93. continue;
  94. const int row_span = Math::Max(1, element_cell->GetAttribute("rowspan", 1));
  95. const int col_span = Math::Max(1, element_cell->GetAttribute("colspan", 1));
  96. float spanning_column_border_width = 0;
  97. int column_last = -1;
  98. {
  99. // Offset the column if we have any rowspan elements from previous rows overlapping with the current column.
  100. for (bool continue_offset_column = true; continue_offset_column; )
  101. {
  102. continue_offset_column = false;
  103. for (int k = 0; k < num_cells_spanning_this_row; k++)
  104. {
  105. if (column >= cells[k].column_begin && column <= cells[k].column_last)
  106. {
  107. column = cells[k].column_last + 1;
  108. continue_offset_column = true;
  109. break;
  110. }
  111. }
  112. }
  113. column_last = column + col_span - 1;
  114. if (column_last >= (int)columns.size())
  115. {
  116. Log::Message(Log::LT_WARNING, "Too many columns in table row %d: %s\nThe number of columns is %d, as determined by the table columns or first table row.",
  117. row + 1, element_row->GetAddress().c_str(), (int)columns.size());
  118. break;
  119. }
  120. spanning_column_border_width = columns[column_last].cell_width + (columns[column_last].cell_offset - columns[column].cell_offset);
  121. }
  122. table_cursor.x = content_top_left.x + columns[column].cell_offset;
  123. // Add the new cell to our list.
  124. cells.emplace_back();
  125. Cell& cell = cells.back();
  126. cell.row_last = row + row_span - 1;
  127. cell.column_begin = column;
  128. cell.column_last = column_last;
  129. cell.element_cell = element_cell;
  130. cell.table_offset = table_cursor;
  131. cell.rows_accumulated_height = 0;
  132. // Determine the cell's box for formatting later, we may get an indefinite (-1) vertical content size.
  133. Box& box = cell.box;
  134. LayoutDetails::BuildBox(box, content_containing_block, element_cell, false, 0.f);
  135. // Determine the box width from the current column width.
  136. const float content_width = Math::Max(0.0f, spanning_column_border_width - box.GetSizeAcross(Box::HORIZONTAL, Box::BORDER, Box::PADDING));
  137. box.SetContent(Vector2f(content_width, box.GetSize().y));
  138. column += col_span;
  139. }
  140. // Partition the cells to determine those who end at this row.
  141. const auto it_cells_in_row_end = std::partition(cells.begin(), cells.end(), [row](const Cell& cell) { return cell.row_last == row; });
  142. // Determine the row height.
  143. float row_content_height = 0;
  144. if (row_box.GetSize().y >= 0)
  145. {
  146. // The row has a definite size, use that.
  147. row_content_height = row_box.GetSize().y;
  148. }
  149. else
  150. {
  151. // The row does not have a definite size, we will use the maximum height of all its cells to determine the row height.
  152. // 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.
  153. for (auto it = cells.begin(); it != it_cells_in_row_end; ++it)
  154. {
  155. Cell& cell = *it;
  156. Element* element_cell = cell.element_cell;
  157. Box& box = cell.box;
  158. // If the cell's height is also indefinite, we need to format it to get its height.
  159. if (box.GetSize().y < 0)
  160. {
  161. LayoutEngine::FormatElement(element_cell, content_containing_block, &box);
  162. box.SetContent(element_cell->GetBox().GetSize());
  163. }
  164. row_content_height = Math::Max(row_content_height, box.GetSizeAcross(Box::VERTICAL, Box::BORDER) - cell.rows_accumulated_height);
  165. }
  166. }
  167. row_content_height = Math::Clamp(row_content_height, row_min_height, row_max_height);
  168. for (Cell& cell : cells)
  169. cell.rows_accumulated_height += row_content_height;
  170. // Now we have the height of the row, position and format each cell.
  171. auto FormatCellsInRow = [row, &cells, it_cells_in_row_end, &table_content_overflow_size, element_table, content_containing_block, content_top_left]() {
  172. // Assumes the cells are already partitioned.
  173. for (auto it = cells.begin(); it != it_cells_in_row_end; ++it)
  174. {
  175. Cell& cell = *it;
  176. Element* element_cell = cell.element_cell;
  177. Box& box = cell.box;
  178. Style::VerticalAlign vertical_align = cell.element_cell->GetComputedValues().vertical_align;
  179. if (box.GetSize().y < 0)
  180. {
  181. const bool is_aligned = (vertical_align.type == Style::VerticalAlign::Middle || vertical_align.type == Style::VerticalAlign::Bottom);
  182. if (is_aligned)
  183. {
  184. // The size of the cell is indefinite, we need to get the height by formatting the cell.
  185. LayoutEngine::FormatElement(element_cell, content_containing_block, &box);
  186. box.SetContent(element_cell->GetBox().GetSize());
  187. }
  188. else
  189. {
  190. box.SetContent(Vector2f(box.GetSize().x, Math::Max(0.0f, cell.rows_accumulated_height - box.GetSizeAcross(Box::VERTICAL, Box::BORDER, Box::PADDING))));
  191. }
  192. }
  193. const float available_height = cell.rows_accumulated_height - box.GetSizeAcross(Box::VERTICAL, Box::BORDER);
  194. if (available_height > 0)
  195. {
  196. // Pad the cell for vertical alignment
  197. float add_padding_top;
  198. float add_padding_bottom;
  199. switch (vertical_align.type)
  200. {
  201. case Style::VerticalAlign::Bottom:
  202. add_padding_top = available_height;
  203. add_padding_bottom = 0;
  204. break;
  205. case Style::VerticalAlign::Middle:
  206. add_padding_top = 0.5f * available_height;
  207. add_padding_bottom = 0.5f * available_height;
  208. break;
  209. case Style::VerticalAlign::Top:
  210. default:
  211. add_padding_top = 0.0f;
  212. add_padding_bottom = available_height;
  213. }
  214. box.SetEdge(Box::PADDING, Box::TOP, box.GetEdge(Box::PADDING, Box::TOP) + add_padding_top);
  215. box.SetEdge(Box::PADDING, Box::BOTTOM, box.GetEdge(Box::PADDING, Box::BOTTOM) + add_padding_bottom);
  216. }
  217. {
  218. // Format the cell in a new block formatting context.
  219. Vector2f cell_visible_overflow_size;
  220. LayoutEngine::FormatElement(element_cell, content_containing_block, &box, &cell_visible_overflow_size);
  221. 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);
  222. 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);
  223. // Set the position of the element within the the table container
  224. element_cell->SetOffset(cell.table_offset, element_table);
  225. }
  226. }
  227. // Remove the formatted cells from pending
  228. cells.erase(cells.begin(), it_cells_in_row_end);
  229. };
  230. FormatCellsInRow();
  231. // TODO: What to do with any remaining row-spanning cells after the last row?
  232. // Position and size the row element
  233. if (row_box.GetSize().y < 0.0f)
  234. row_box.SetContent(Vector2f(row_box.GetSize().x, row_content_height));
  235. element_row->SetOffset(row_element_offset, element_table);
  236. element_row->SetBox(row_box);
  237. 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);
  238. table_cursor.y += row_content_height + row_bottom_offset;
  239. for (Cell& cell : cells)
  240. cell.rows_accumulated_height += row_bottom_offset;
  241. }
  242. table_resulting_content_size.y = table_cursor.y - content_top_left.y;
  243. // Size and position the column elements.
  244. for (const Column& column : columns)
  245. {
  246. if (Element* element = column.element_column)
  247. {
  248. Box box;
  249. LayoutDetails::BuildBox(box, content_containing_block, element, false, 0.0f);
  250. box.SetContent(Vector2f(column.column_width, table_resulting_content_size.y));
  251. element->SetBox(box);
  252. element->SetOffset(content_top_left + Vector2f(column.column_offset, 0.0f), element_table);
  253. }
  254. }
  255. if (table_resulting_content_size != content_containing_block)
  256. {
  257. table_block_context_box->GetBox().SetContent(table_resulting_content_size);
  258. }
  259. table_block_context_box->ExtendInnerContentSize(table_content_overflow_size);
  260. CloseResult result = table_block_context_box->Close();
  261. return result;
  262. }
  263. LayoutTable::Columns LayoutTable::DetermineColumnWidths(Element* const element_table, const float column_gap, float& table_content_width)
  264. {
  265. // The column widths are determined entirely by any <col> elements preceding the first row, and <td> elements in the first row.
  266. // If <col> has a fixed width, that is used. Otherwise, if <td> has a fixed width, that is used. Otherwise the column is 'flexible' width.
  267. // All flexible widths are then sized to evenly fill the content width of the table.
  268. struct ColumnMetric {
  269. // All widths are defined in terms of the border width of cells in the column. The column element can add padding,
  270. // and borders which extends beyond the cell's border width, and is instead added to 'sum_fixed_spacing'.
  271. float fixed_width = 0;
  272. float flex_width = 0;
  273. float min_width = 0;
  274. float max_width = 0;
  275. // The following are only used for <col> elements.
  276. Element* element_column = nullptr;
  277. bool has_fixed_size = false;
  278. int column_span = 0;
  279. float column_padding_border_left = 0;
  280. float column_padding_border_right = 0;
  281. };
  282. Vector<ColumnMetric> column_metrics;
  283. float sum_fixed_spacing = 0; // Includes column gaps and the column elements' padding, and border.
  284. const int num_table_children = element_table->GetNumChildren();
  285. Element* element_row = nullptr;
  286. // First look for any <col> elements preceding any <tr> elements, use them for defining the width of the respective columns.
  287. for (int i = 0; i < num_table_children; i++)
  288. {
  289. Element* element = element_table->GetChild(i);
  290. const ComputedValues& computed = element->GetComputedValues();
  291. if (computed.display == Style::Display::TableRow)
  292. {
  293. // End of column elements.
  294. element_row = element;
  295. break;
  296. }
  297. else if (computed.display != Style::Display::TableColumn)
  298. {
  299. continue;
  300. }
  301. const float padding_border_left = Math::Max(0.0f, ResolveValue(computed.padding_left, table_content_width)) + Math::Max(0.0f, computed.border_left_width);
  302. const float padding_border_right = Math::Max(0.0f, ResolveValue(computed.padding_right, table_content_width)) + Math::Max(0.0f, computed.border_right_width);
  303. const float padding_border_sum = padding_border_left + padding_border_right;
  304. sum_fixed_spacing += padding_border_sum;
  305. ColumnMetric column_metric;
  306. // Find the min/max width.
  307. column_metric.min_width = ResolveValue(computed.min_width, table_content_width);
  308. column_metric.max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, table_content_width));
  309. if (computed.box_sizing == Style::BoxSizing::BorderBox)
  310. {
  311. column_metric.min_width = Math::Max(0.0f, column_metric.min_width - padding_border_sum);
  312. column_metric.max_width = Math::Max(0.0f, column_metric.max_width - padding_border_sum);
  313. }
  314. if (computed.width.type == Style::Width::Auto)
  315. {
  316. column_metric.flex_width = 1;
  317. }
  318. else
  319. {
  320. float width = ResolveValue(computed.width, table_content_width);
  321. if (computed.box_sizing == Style::BoxSizing::BorderBox)
  322. width = Math::Max(0.f, ResolveValue(computed.width, table_content_width) - padding_border_sum);
  323. column_metric.fixed_width = Math::Clamp(width, column_metric.min_width, column_metric.max_width);
  324. column_metric.has_fixed_size = true;
  325. }
  326. const int span = Math::Max(1, element->GetAttribute("span", 1));
  327. if (span > 1)
  328. {
  329. // Distribute any fixed widths over the columns we are spanning.
  330. const float width_factor = 1.f / float(span);
  331. column_metric.fixed_width *= width_factor;
  332. column_metric.min_width *= width_factor;
  333. column_metric.max_width *= width_factor;
  334. }
  335. column_metric.element_column = element;
  336. column_metric.column_span = span;
  337. column_metric.column_padding_border_left = padding_border_left;
  338. for (int j = 0; j < span; j++)
  339. {
  340. if (j == 1)
  341. {
  342. column_metric.element_column = nullptr;
  343. column_metric.column_span = 0;
  344. column_metric.column_padding_border_left = 0;
  345. }
  346. if (j == span - 1)
  347. column_metric.column_padding_border_right = padding_border_right;
  348. column_metrics.emplace_back(column_metric);
  349. }
  350. }
  351. const int num_row_children = (!element_row ? 0 : element_row->GetNumChildren());
  352. column_metrics.reserve(num_row_children);
  353. // Next, walk through the cells in the first table row. This procedure is subtly different from the <col>-iteration above:
  354. // (1) Cells use their border width to line up the column, while <col> use their content width.
  355. // (2a) We only add new columns here if they are not already represented by a <col> element.
  356. // (2b) Otherwise, if the <col> element has auto (min-/max-) width, we use the cell's (min-/max-) width if it has any.
  357. for (int i = 0, column = 0; i < num_row_children; i++)
  358. {
  359. Element* element = element_row->GetChild(i);
  360. const ComputedValues& computed = element->GetComputedValues();
  361. if (computed.display != Style::Display::TableCell)
  362. {
  363. Log::Message(Log::LT_WARNING, "Only table cells ('display: table-cell') are allowed as children of table rows. %s", element->GetAddress().c_str());
  364. continue;
  365. }
  366. const float padding_border_sum =
  367. Math::Max(0.0f, ResolveValue(computed.padding_left, table_content_width)) +
  368. Math::Max(0.0f, ResolveValue(computed.padding_right, table_content_width)) +
  369. Math::Max(0.0f, computed.border_left_width) +
  370. Math::Max(0.0f, computed.border_right_width);
  371. ColumnMetric column_metric;
  372. // Find the min/max width.
  373. column_metric.min_width = ResolveValue(computed.min_width, table_content_width);
  374. column_metric.max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, table_content_width));
  375. if (computed.box_sizing == Style::BoxSizing::ContentBox)
  376. {
  377. if (column_metric.min_width > 0)
  378. column_metric.min_width += padding_border_sum;
  379. if (column_metric.max_width < FLT_MAX)
  380. column_metric.max_width += padding_border_sum;
  381. }
  382. if (computed.width.type == Style::Width::Auto)
  383. {
  384. column_metric.flex_width = 1;
  385. }
  386. else
  387. {
  388. float width = ResolveValue(computed.width, table_content_width);
  389. if (computed.box_sizing == Style::BoxSizing::ContentBox)
  390. width += padding_border_sum;
  391. column_metric.fixed_width = Math::Clamp(width, column_metric.min_width, column_metric.max_width);
  392. column_metric.has_fixed_size = true;
  393. }
  394. const int colspan = Math::Max(1, element->GetAttribute("colspan", 1));
  395. if (colspan > 1)
  396. {
  397. const float width_factor = 1.f / float(colspan);
  398. column_metric.fixed_width *= width_factor;
  399. column_metric.min_width *= width_factor;
  400. column_metric.max_width *= width_factor;
  401. }
  402. for (int j = 0; j < colspan; j++)
  403. {
  404. if (j + column < (int)column_metrics.size())
  405. {
  406. ColumnMetric& destination = column_metrics[j + column];
  407. if (!destination.has_fixed_size && column_metric.has_fixed_size)
  408. {
  409. destination.fixed_width = column_metric.fixed_width;
  410. destination.has_fixed_size = true;
  411. }
  412. if (destination.min_width == 0)
  413. destination.min_width = column_metric.min_width;
  414. if (destination.max_width == FLT_MAX)
  415. destination.max_width = column_metric.max_width;
  416. }
  417. else
  418. {
  419. column_metrics.emplace_back(column_metric);
  420. }
  421. }
  422. column += colspan;
  423. }
  424. if (column_metrics.empty())
  425. {
  426. Log::Message(Log::LT_WARNING, "No columns or rows in table %s", element_table->GetAddress().c_str());
  427. // TODO: Handle this more gracefully.
  428. return Columns();
  429. }
  430. sum_fixed_spacing += column_gap * float((int)column_metrics.size() - 1);
  431. // Now all the widths are determined in terms of fixed or flexible widths.
  432. // Next, convert any flexible widths to fixed widths by filling up the table width.
  433. for (bool continue_iteration = true; continue_iteration; )
  434. {
  435. continue_iteration = false;
  436. float table_available_width = 0.0f;
  437. float fr_to_px_ratio = 0;
  438. // Calculate the fr/px-ratio.
  439. {
  440. float sum_fixed_width = sum_fixed_spacing; // [px]
  441. float sum_flex_width = 0; // [fr]
  442. for (const ColumnMetric& metric : column_metrics)
  443. {
  444. sum_flex_width += metric.flex_width;
  445. sum_fixed_width += (metric.flex_width == 0.f ? metric.fixed_width : 0.0f);
  446. }
  447. sum_flex_width = Math::Max(1.f, sum_flex_width);
  448. table_available_width = table_content_width - sum_fixed_width;
  449. fr_to_px_ratio = Math::Max(0.0f, table_available_width) / sum_flex_width;
  450. }
  451. // Iterate through the columns and convert flexible widths to fixed widths.
  452. for (auto& metric : column_metrics)
  453. {
  454. if (metric.flex_width > 0)
  455. {
  456. const float fixed_flex_width = metric.flex_width * fr_to_px_ratio;
  457. metric.fixed_width = Math::Clamp(fixed_flex_width, metric.min_width, metric.max_width);
  458. table_available_width -= metric.fixed_width;
  459. if (metric.fixed_width != fixed_flex_width)
  460. {
  461. // We met a min/max-constraint, fix the size of this column. Start over with the procedure once we are done with all the columns.
  462. metric.flex_width = 0.0f;
  463. continue_iteration = true;
  464. }
  465. }
  466. }
  467. // If we have distributed all the flexible space, and there is still space available, then distribute the available space over the column widths while respecting max-widths.
  468. if (!continue_iteration && table_available_width > 0.5f)
  469. {
  470. const int num_columns = (int)column_metrics.size();
  471. struct ColumnAvailableWidth {
  472. int column;
  473. float available_width;
  474. };
  475. Vector<ColumnAvailableWidth> col_available_widths(num_columns);
  476. for (int i = 0; i < num_columns; i++)
  477. {
  478. col_available_widths[i].column = i;
  479. col_available_widths[i].available_width = column_metrics[i].max_width - column_metrics[i].fixed_width;
  480. }
  481. // Sort the columns by available width, smallest to largest. This lets us "fill up" the most constrained columns first.
  482. std::sort(col_available_widths.begin(), col_available_widths.end(), [](const ColumnAvailableWidth& c1, const ColumnAvailableWidth& c2) {
  483. return c1.available_width < c2.available_width;
  484. });
  485. for (int i = 0; i < num_columns; i++)
  486. {
  487. const int column = col_available_widths[i].column;
  488. const int num_columns_remaining = num_columns - i;
  489. const float ideal_add_column_width = table_available_width / float(num_columns_remaining);
  490. const float add_column_width = Math::Min(ideal_add_column_width, col_available_widths[i].available_width);
  491. if (add_column_width > 0)
  492. {
  493. column_metrics[column].fixed_width += add_column_width;
  494. table_available_width = Math::Max(0.0f, table_available_width - add_column_width);
  495. }
  496. }
  497. }
  498. }
  499. // Fill in the resulting columns.
  500. Columns columns;
  501. columns.resize(column_metrics.size());
  502. float cursor_x = 0;
  503. for (size_t i = 0; i < column_metrics.size(); i++)
  504. {
  505. Column& col = columns[i];
  506. const ColumnMetric& metric = column_metrics[i];
  507. col.element_column = metric.element_column;
  508. col.cell_width = metric.fixed_width;
  509. col.cell_offset = cursor_x + metric.column_padding_border_left;
  510. col.column_width = col.cell_width; // Column content width is the cell border width, unless there is a spanning column element (see next loop).
  511. col.column_offset = cursor_x;
  512. cursor_x += metric.fixed_width + metric.column_padding_border_left + metric.column_padding_border_right;
  513. if (i != column_metrics.size() - 1)
  514. cursor_x += column_gap;
  515. }
  516. // Extend the table content width if the summed column widths and spacing is larger. Include some margin for floating-point imprecision.
  517. // TODO: What if the content width is smaller? (This could possibly happen if all columns have a max-width)
  518. if (cursor_x > table_content_width + 0.5f)
  519. table_content_width = cursor_x;
  520. // Extend column widths to cover multiple columns for spanning column elements.
  521. for (size_t i = 0; i < column_metrics.size(); i++)
  522. {
  523. const ColumnMetric& metric = column_metrics[i];
  524. if (metric.column_span > 1 && metric.element_column && i + metric.column_span - 1 < column_metrics.size())
  525. {
  526. Column& col = columns[i];
  527. Column& col_last_span = columns[i + metric.column_span - 1];
  528. col.column_width = col_last_span.cell_width + (col_last_span.cell_offset - col.cell_offset);
  529. }
  530. }
  531. return columns;
  532. }
  533. } // namespace Rml