LayoutTable.cpp 26 KB

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