LayoutTable.cpp 26 KB

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