LayoutTable.cpp 22 KB

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