2
0

LayoutTableDetails.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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 "LayoutTableDetails.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include <algorithm>
  31. #include <float.h>
  32. namespace Rml {
  33. bool TableGrid::Build(Element* element_table)
  34. {
  35. ElementList non_parented_cell_elements;
  36. const int num_table_children = element_table->GetNumChildren();
  37. for (int i = 0; i < num_table_children; i++)
  38. {
  39. using Display = Style::Display;
  40. Element* element = element_table->GetChild(i);
  41. const Display display = element->GetDisplay();
  42. if (display == Display::None)
  43. continue;
  44. if (!non_parented_cell_elements.empty() && display != Display::TableCell)
  45. {
  46. PushRow(nullptr, std::move(non_parented_cell_elements));
  47. non_parented_cell_elements.clear();
  48. }
  49. if (display == Display::TableCell)
  50. {
  51. non_parented_cell_elements.push_back(element);
  52. }
  53. else if (display == Display::TableRow)
  54. {
  55. PushRow(element, {});
  56. }
  57. else if (display == Display::TableRowGroup)
  58. {
  59. const int num_row_group_children = element->GetNumChildren();
  60. const int row_group_index = (int)rows.size();
  61. int num_rows_added = 0;
  62. for (int j = 0; j < num_row_group_children; j++)
  63. {
  64. using Display = Style::Display;
  65. Element* element_row = element->GetChild(j);
  66. const Display display_row = element_row->GetDisplay();
  67. if (display_row != Display::TableRow)
  68. {
  69. if (display_row != Display::None)
  70. {
  71. Log::Message(Log::LT_WARNING, "Only table rows are valid children of table row groups. Ignoring element %s.", element_row->GetAddress().c_str());
  72. }
  73. continue;
  74. }
  75. PushRow(element_row, {});
  76. num_rows_added += 1;
  77. }
  78. if (num_rows_added > 0)
  79. {
  80. rows[row_group_index].element_group = element;
  81. rows[row_group_index].group_span = num_rows_added;
  82. }
  83. }
  84. else if (rows.empty() && display == Display::TableColumn)
  85. {
  86. const int span = Math::Max(1, element->GetAttribute("span", 1));
  87. PushColumn(element, span);
  88. }
  89. else if (rows.empty() && display == Display::TableColumnGroup)
  90. {
  91. PushColumnGroup(element);
  92. }
  93. else
  94. {
  95. if (display == Display::TableColumn || display == Display::TableColumnGroup)
  96. Log::Message(Log::LT_WARNING, "Table columns and column groups must precede any table rows. Ignoring element %s.", element->GetAddress().c_str());
  97. else
  98. Log::Message(Log::LT_WARNING, "Only table columns, column groups, rows, row groups, and cells are valid children of tables. Ignoring element %s.", element->GetAddress().c_str());
  99. }
  100. }
  101. if (!non_parented_cell_elements.empty())
  102. {
  103. PushRow(nullptr, std::move(non_parented_cell_elements));
  104. non_parented_cell_elements.clear();
  105. }
  106. // Sort cells by the last row they span.
  107. std::sort(cells.begin(), cells.end(), [](const Cell& a, const Cell& b) {
  108. return a.row_last < b.row_last;
  109. });
  110. if (!open_cells.empty())
  111. {
  112. 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());
  113. }
  114. open_cells.clear();
  115. open_cells.shrink_to_fit();
  116. return true;
  117. }
  118. void TableGrid::PushColumn(Element* element_column, int span)
  119. {
  120. Column column;
  121. column.element_column = element_column;
  122. column.column_span = span;
  123. columns.push_back(column);
  124. for (int j = 1; j < span; j++)
  125. columns.push_back({});
  126. }
  127. void TableGrid::PushColumnGroup(Element* element_column_group)
  128. {
  129. const int column_begin = (int)columns.size();
  130. int group_span = Math::Max(0, element_column_group->GetAttribute("span", 0));
  131. if (group_span == 0)
  132. {
  133. // Look through the column group to find all its column children.
  134. const int num_column_group_children = element_column_group->GetNumChildren();
  135. for (int j = 0; j < num_column_group_children; j++)
  136. {
  137. Element* child = element_column_group->GetChild(j);
  138. if (child->GetDisplay() == Style::Display::TableColumn)
  139. {
  140. const int column_span = Math::Max(1, child->GetAttribute("span", 1));
  141. PushColumn(child, column_span);
  142. group_span += column_span;
  143. }
  144. }
  145. }
  146. else
  147. {
  148. // Push empty columns, the group properties are filled below.
  149. for (int j = 0; j < group_span; j++)
  150. columns.push_back({});
  151. }
  152. if (group_span > 0)
  153. {
  154. columns[column_begin].element_group = element_column_group;
  155. columns[column_begin].group_span = group_span;
  156. }
  157. }
  158. void TableGrid::PushOrMergeColumnsFromFirstRow(Element* element_cell, int column_begin, int span)
  159. {
  160. for (int column_index = column_begin; column_index < column_begin + span; column_index++)
  161. {
  162. Column* column = nullptr;
  163. if (column_index < (int)columns.size())
  164. {
  165. column = &columns[column_index];
  166. }
  167. else
  168. {
  169. RMLUI_ASSERT(column_index == (int)columns.size());
  170. columns.push_back({});
  171. column = &columns.back();
  172. }
  173. if (column_index == column_begin)
  174. {
  175. column->element_cell = element_cell;
  176. column->cell_span = span;
  177. }
  178. }
  179. }
  180. void TableGrid::PushRow(Element* element_row, ElementList cell_elements)
  181. {
  182. const int row_index = (int)rows.size();
  183. if (element_row)
  184. {
  185. RMLUI_ASSERT(cell_elements.empty());
  186. const int num_row_children = element_row->GetNumChildren();
  187. cell_elements.reserve(num_row_children);
  188. for (int j = 0; j < num_row_children; j++)
  189. {
  190. Element* element_cell = element_row->GetChild(j);
  191. const Style::Display cell_display = element_cell->GetComputedValues().display;
  192. if (cell_display == Style::Display::TableCell)
  193. {
  194. cell_elements.push_back(element_cell);
  195. }
  196. else if (cell_display != Style::Display::None)
  197. {
  198. Log::Message(Log::LT_WARNING, "Only table cells are allowed as children of table rows. %s", element_cell->GetAddress().c_str());
  199. }
  200. }
  201. }
  202. rows.push_back(Row{ element_row, nullptr, 0 });
  203. const int num_cells_spanning_this_row = (int)open_cells.size();
  204. // For all child cell elements of this row, add them to the list of open cells.
  205. for (int j = 0, column = 0; j < (int)cell_elements.size(); j++)
  206. {
  207. Element* element_cell = cell_elements[j];
  208. const int row_span = Math::Max(1, element_cell->GetAttribute("rowspan", 1));
  209. const int col_span = Math::Max(1, element_cell->GetAttribute("colspan", 1));
  210. if (row_index == 0)
  211. {
  212. // This is the first row. The cells of this row along with previous <col> elements define the columns.
  213. PushOrMergeColumnsFromFirstRow(element_cell, column, col_span);
  214. }
  215. // Offset the column if we have any rowspan elements from previous rows overlapping with the current column.
  216. for (bool continue_offset_column = true; continue_offset_column; )
  217. {
  218. continue_offset_column = false;
  219. for (int k = 0; k < num_cells_spanning_this_row; k++)
  220. {
  221. if (column >= open_cells[k].column_begin && column <= open_cells[k].column_last)
  222. {
  223. column = open_cells[k].column_last + 1;
  224. continue_offset_column = true;
  225. break;
  226. }
  227. }
  228. }
  229. const int column_last = column + col_span - 1;
  230. if (column_last >= (int)columns.size())
  231. {
  232. Log::Message(Log::LT_WARNING, "Too many columns in table row %d while encountering cell: %s\nThe number of columns is %d, as determined by the table columns or the first table row.",
  233. row_index + 1, element_cell->GetAddress().c_str(), (int)columns.size());
  234. break;
  235. }
  236. // Add the new cell to our list.
  237. open_cells.emplace_back();
  238. Cell& cell = open_cells.back();
  239. cell.element_cell = element_cell;
  240. cell.row_begin = row_index;
  241. cell.row_last = row_index + row_span - 1;
  242. cell.column_begin = column;
  243. cell.column_last = column_last;
  244. column += col_span;
  245. }
  246. // Partition the cells to determine those who end at this row.
  247. const auto it_cells_in_row_end = std::partition(open_cells.begin(), open_cells.end(), [row_index](const Cell& cell) { return cell.row_last == row_index; });
  248. // Close cells ending at this row.
  249. cells.insert(cells.end(), open_cells.begin(), it_cells_in_row_end);
  250. open_cells.erase(open_cells.begin(), it_cells_in_row_end);
  251. }
  252. ComputedTrackSize BuildComputedColumnSize(const ComputedValues& computed)
  253. {
  254. return ComputedTrackSize{
  255. computed.width,
  256. computed.min_width, computed.max_width,
  257. computed.padding_left, computed.padding_right,
  258. computed.margin_left, computed.margin_right,
  259. computed.border_left_width, computed.border_right_width,
  260. computed.box_sizing
  261. };
  262. }
  263. ComputedTrackSize BuildComputedRowSize(const ComputedValues& computed)
  264. {
  265. return ComputedTrackSize{
  266. computed.height,
  267. computed.min_height, computed.max_height,
  268. computed.padding_top, computed.padding_bottom,
  269. computed.margin_top, computed.margin_bottom,
  270. computed.border_top_width, computed.border_bottom_width,
  271. computed.box_sizing
  272. };
  273. }
  274. void TracksSizing::GetEdgeSizes(float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedTrackSize& computed) const
  275. {
  276. margin_a = ResolveValue(computed.margin_a, table_initial_content_size);
  277. margin_b = ResolveValue(computed.margin_b, table_initial_content_size);
  278. padding_border_a = Math::Max(0.0f, ResolveValue(computed.padding_a, table_initial_content_size)) + Math::Max(0.0f, computed.border_a);
  279. padding_border_b = Math::Max(0.0f, ResolveValue(computed.padding_b, table_initial_content_size)) + Math::Max(0.0f, computed.border_b);
  280. }
  281. void TracksSizing::ApplyGroupElement(const int index, const int span, const ComputedTrackSize& computed)
  282. {
  283. RMLUI_ASSERT(span >= 1 && index + span - 1 < (int)metrics.size());
  284. float margin_a, margin_b;
  285. float padding_border_a, padding_border_b;
  286. GetEdgeSizes(margin_a, margin_b, padding_border_a, padding_border_b, computed);
  287. // Add left/top edges.
  288. TrackMetric& metric_begin = metrics[index];
  289. metric_begin.group_padding_border_a = padding_border_a;
  290. metric_begin.sum_margin_a = margin_a;
  291. // Add right/bottom edges.
  292. TrackMetric& metric_last = metrics[index + span - 1];
  293. metric_last.group_padding_border_b = padding_border_b;
  294. metric_last.sum_margin_b = margin_b;
  295. }
  296. void TracksSizing::ApplyTrackElement(const int index, const int span, const ComputedTrackSize& computed)
  297. {
  298. RMLUI_ASSERT(span >= 1 && index + span - 1 < (int)metrics.size());
  299. float margin_a, margin_b;
  300. float padding_border_a, padding_border_b;
  301. TrackMetric& metric_begin = metrics[index];
  302. // We target the content box because track sizes are defined in terms of the border box of cells, equal to the content size of tracks.
  303. InitializeSize(metric_begin, margin_a, margin_b, padding_border_a, padding_border_b, computed, span, Style::BoxSizing::ContentBox);
  304. // Add left/top edges. Increment the values because we are merging with any previously set edges.
  305. metric_begin.column_padding_border_a += padding_border_a;
  306. metric_begin.sum_margin_a += margin_a;
  307. // Add right/bottom edges.
  308. TrackMetric& metric_last = metrics[index + span - 1];
  309. metric_last.column_padding_border_b += padding_border_b;
  310. metric_last.sum_margin_b += margin_b;
  311. // The size of all spanning tracks are distributed equally.
  312. for (int j = 1; j < span; j++)
  313. {
  314. TrackMetric& metric = metrics[index + j];
  315. metric.sizing_mode = metric_begin.sizing_mode;
  316. metric.fixed_size = metric_begin.fixed_size;
  317. metric.flex_size = metric_begin.flex_size;
  318. metric.min_size = metric_begin.min_size;
  319. metric.max_size = metric_begin.max_size;
  320. }
  321. }
  322. void TracksSizing::ApplyCellElement(const int index, const int span, const ComputedTrackSize& computed)
  323. {
  324. // Merge the metrics of the cell with the existing track: If the existing track
  325. // has auto min-/max-/size, we use the cell's min-/max-/size if it has any.
  326. RMLUI_ASSERT(span >= 1 && index + span - 1 < (int)metrics.size());
  327. float margin_a, margin_b;
  328. float padding_border_a, padding_border_b;
  329. TrackMetric cell_metric;
  330. // We target the border box because track sizes are defined in terms of the border box of cells.
  331. InitializeSize(cell_metric, margin_a, margin_b, padding_border_a, padding_border_b, computed, span, Style::BoxSizing::BorderBox);
  332. cell_metric.sum_margin_a = margin_a;
  333. // Merge the size determined by this cell with any existing track sizes.
  334. for (int j = 0; j < span; j++)
  335. {
  336. if (j == 1)
  337. cell_metric.sum_margin_a = 0;
  338. if (j == span - 1)
  339. cell_metric.sum_margin_b = margin_b;
  340. // Merge the existing track metrics with the cell sizing data.
  341. TrackMetric& destination = metrics[index + j];
  342. if (destination.sizing_mode != TrackSizingMode::Fixed && cell_metric.sizing_mode == TrackSizingMode::Fixed)
  343. {
  344. destination.fixed_size = cell_metric.fixed_size;
  345. destination.flex_size = 0;
  346. destination.sizing_mode = TrackSizingMode::Fixed;
  347. }
  348. if (destination.min_size == 0)
  349. destination.min_size = cell_metric.min_size;
  350. if (destination.max_size == FLT_MAX)
  351. destination.max_size = cell_metric.max_size;
  352. destination.sum_margin_a += cell_metric.sum_margin_a;
  353. destination.sum_margin_b += cell_metric.sum_margin_b;
  354. }
  355. }
  356. void TracksSizing::InitializeSize(TrackMetric& metric, float& margin_a, float& margin_b, float& padding_border_a, float& padding_border_b, const ComputedTrackSize& computed, const int span, const Style::BoxSizing target_box) const
  357. {
  358. RMLUI_ASSERT(span >= 1);
  359. GetEdgeSizes(margin_a, margin_b, padding_border_a, padding_border_b, computed);
  360. const float padding_border_sum = padding_border_a + padding_border_b;
  361. // Find the min/max size.
  362. metric.min_size = ResolveValue(computed.min_size, table_initial_content_size);
  363. metric.max_size = (computed.max_size.value < 0.f ? FLT_MAX : ResolveValue(computed.max_size, table_initial_content_size));
  364. if (target_box == Style::BoxSizing::ContentBox && computed.box_sizing == Style::BoxSizing::BorderBox)
  365. {
  366. metric.min_size = Math::Max(0.0f, metric.min_size - padding_border_sum);
  367. if (metric.max_size < FLT_MAX)
  368. metric.max_size = Math::Max(0.0f, metric.max_size - padding_border_sum);
  369. }
  370. else if (target_box == Style::BoxSizing::BorderBox && computed.box_sizing == Style::BoxSizing::ContentBox)
  371. {
  372. if (metric.min_size > 0)
  373. metric.min_size += padding_border_sum;
  374. if (metric.max_size < FLT_MAX)
  375. metric.max_size += padding_border_sum;
  376. }
  377. // Find fixed and flexible sizes.
  378. if (computed.size.type == Style::LengthPercentageAuto::Auto)
  379. {
  380. metric.sizing_mode = TrackSizingMode::Auto;
  381. }
  382. else if (computed.size.type == Style::LengthPercentageAuto::Percentage && computed.size.value >= 100.f)
  383. {
  384. // Percentages >= 100% are resolved as flexible size.
  385. metric.sizing_mode = TrackSizingMode::Flexible;
  386. metric.flex_size = Math::Max(0.01f * computed.size.value / float(span), 0.f);
  387. }
  388. else
  389. {
  390. float width = ResolveValue(computed.size, table_initial_content_size);
  391. if (target_box == Style::BoxSizing::ContentBox && computed.box_sizing == Style::BoxSizing::BorderBox)
  392. width = Math::Max(0.f, width - padding_border_sum);
  393. else if (target_box == Style::BoxSizing::BorderBox && computed.box_sizing == Style::BoxSizing::ContentBox)
  394. width += padding_border_sum;
  395. metric.sizing_mode = TrackSizingMode::Fixed;
  396. metric.flex_size = 0;
  397. metric.fixed_size = Math::Clamp(width, metric.min_size, metric.max_size);
  398. metric.min_size = metric.fixed_size;
  399. metric.max_size = metric.fixed_size;
  400. }
  401. if (span > 1)
  402. {
  403. // Account for distribution of fixed size over the tracks we are spanning.
  404. const float width_factor = 1.f / float(span);
  405. metric.fixed_size *= width_factor;
  406. metric.min_size *= width_factor;
  407. if (metric.max_size < FLT_MAX)
  408. metric.max_size *= width_factor;
  409. }
  410. }
  411. void TracksSizing::ResolveFlexibleSize()
  412. {
  413. // The fixed spacing includes the table gaps, and the track and track-group elements' padding, border, and margins.
  414. float sum_fixed_spacing = table_gap * float((int)metrics.size() - 1);
  415. for (const TrackMetric& metric : metrics)
  416. {
  417. // Any auto size must have been resolved to either fixed or flexible before running this algorithm.
  418. RMLUI_ASSERT(metric.sizing_mode == TrackSizingMode::Fixed || metric.sizing_mode == TrackSizingMode::Flexible);
  419. sum_fixed_spacing += metric.column_padding_border_a + metric.column_padding_border_b;
  420. sum_fixed_spacing += metric.group_padding_border_a + metric.group_padding_border_a;
  421. sum_fixed_spacing += metric.sum_margin_a + metric.sum_margin_b;
  422. }
  423. float table_available_size = 0.0f;
  424. // Convert any flexible sizes to fixed sizes by filling up the size of the table.
  425. for (bool continue_iteration = true; continue_iteration; )
  426. {
  427. continue_iteration = false;
  428. float fr_to_px_ratio = 0;
  429. // Calculate the fr/px-ratio. [fr] is here the unit for flexible width.
  430. {
  431. float sum_fixed_size = sum_fixed_spacing; // [px]
  432. float sum_flex_size = 0; // [fr]
  433. for (const TrackMetric& metric : metrics)
  434. {
  435. sum_flex_size += metric.flex_size;
  436. sum_fixed_size += (metric.flex_size == 0.f ? metric.fixed_size : 0.0f);
  437. }
  438. sum_flex_size = Math::Max(1.f, sum_flex_size);
  439. table_available_size = table_initial_content_size - sum_fixed_size;
  440. fr_to_px_ratio = Math::Max(0.0f, table_available_size) / sum_flex_size;
  441. }
  442. // Iterate through each track and convert flexible size to fixed size.
  443. for (auto& metric : metrics)
  444. {
  445. if (metric.flex_size > 0)
  446. {
  447. const float fixed_flex_size = metric.flex_size * fr_to_px_ratio;
  448. metric.fixed_size = Math::Clamp(fixed_flex_size, metric.min_size, metric.max_size);
  449. table_available_size -= metric.fixed_size;
  450. if (metric.fixed_size != fixed_flex_size)
  451. {
  452. // We met a min/max-constraint, fix the size of this track. Start over with the procedure once we are done with all the tracks.
  453. metric.flex_size = 0.0f;
  454. continue_iteration = true;
  455. }
  456. }
  457. }
  458. }
  459. // If we have distributed all the flexible space, and there is still space available, then distribute the available space over
  460. // the track sizes while respecting max-widths.
  461. if (table_available_size > 0.5f)
  462. {
  463. const int num_tracks = (int)metrics.size();
  464. struct TrackAvailableSize {
  465. int track;
  466. float available_size;
  467. };
  468. Vector<TrackAvailableSize> track_available_sizes(num_tracks);
  469. // Find the available size of all tracks.
  470. for (int i = 0; i < num_tracks; i++)
  471. {
  472. track_available_sizes[i].track = i;
  473. track_available_sizes[i].available_size = metrics[i].max_size - metrics[i].fixed_size;
  474. }
  475. // Sort the tracks by available size, smallest to largest. This lets us "fill up" the most constrained tracks first.
  476. std::sort(track_available_sizes.begin(), track_available_sizes.end(), [](const TrackAvailableSize& c1, const TrackAvailableSize& c2) {
  477. return c1.available_size < c2.available_size;
  478. });
  479. for (int i = 0; i < num_tracks; i++)
  480. {
  481. const int track = track_available_sizes[i].track;
  482. const int num_tracks_remaining = num_tracks - i;
  483. const float ideal_add_track_size = table_available_size / float(num_tracks_remaining);
  484. const float add_track_size = Math::Min(ideal_add_track_size, track_available_sizes[i].available_size);
  485. if (add_track_size > 0)
  486. {
  487. metrics[track].fixed_size += add_track_size;
  488. table_available_size = Math::Max(0.0f, table_available_size - add_track_size);
  489. }
  490. }
  491. }
  492. }
  493. static float InitializeTrackBoxes(TrackBoxList& boxes, const TrackMetricList& metrics, const float table_gap)
  494. {
  495. boxes.resize(metrics.size());
  496. float cursor = 0;
  497. // Walk through all the metrics and populate the track box accordingly.
  498. for (size_t i = 0; i < metrics.size(); i++)
  499. {
  500. TrackBox& box = boxes[i];
  501. const TrackMetric& metric = metrics[i];
  502. box.group_offset = cursor + metric.sum_margin_a;
  503. box.track_offset = box.group_offset + metric.group_padding_border_a;
  504. box.cell_offset = box.track_offset + metric.column_padding_border_a;
  505. // The group and column width will be extended if they span multiple columns (see next loop).
  506. box.group_size = metric.fixed_size + metric.column_padding_border_a + metric.column_padding_border_b;
  507. box.cell_size = metric.fixed_size;
  508. box.track_size = metric.fixed_size;
  509. cursor = box.cell_offset + metric.fixed_size + metric.column_padding_border_b + metric.group_padding_border_b + metric.sum_margin_b;
  510. if (i != metrics.size() - 1)
  511. cursor += table_gap;
  512. }
  513. return cursor;
  514. }
  515. static void SnapTrackBoxesToPixelGrid(TrackBoxList& boxes)
  516. {
  517. for (TrackBox& box : boxes)
  518. {
  519. Math::SnapToPixelGrid(box.cell_offset, box.cell_size);
  520. Math::SnapToPixelGrid(box.track_offset, box.track_size);
  521. Math::SnapToPixelGrid(box.group_offset, box.group_size);
  522. }
  523. }
  524. float BuildColumnBoxes(TrackBoxList& column_boxes, const TrackMetricList& column_metrics, const TableGrid::ColumnList& grid_columns, const float table_gap_x)
  525. {
  526. const float columns_width = InitializeTrackBoxes(column_boxes, column_metrics, table_gap_x);
  527. const int num_columns = (int)column_metrics.size();
  528. // Extend column and column group widths to cover all the columns they span.
  529. for (int i = 0; i < num_columns; i++)
  530. {
  531. const int column_span = grid_columns[i].column_span;
  532. const int group_span = grid_columns[i].group_span;
  533. if (column_span > 1 && i + column_span - 1 < num_columns)
  534. {
  535. TrackBox& metric = column_boxes[i];
  536. TrackBox& metric_last_span = column_boxes[i + column_span - 1];
  537. metric.track_size = metric_last_span.cell_size + (metric_last_span.cell_offset - metric.cell_offset);
  538. }
  539. if (group_span > 1 && i + group_span - 1 < num_columns)
  540. {
  541. TrackBox& metric = column_boxes[i];
  542. TrackBox& metric_last_span = column_boxes[i + group_span - 1];
  543. metric.group_size = metric_last_span.group_size + (metric_last_span.track_offset - metric.track_offset);
  544. }
  545. }
  546. SnapTrackBoxesToPixelGrid(column_boxes);
  547. return columns_width;
  548. }
  549. float BuildRowBoxes(TrackBoxList& row_boxes, const TrackMetricList& row_metrics, const TableGrid::RowList& grid_rows, const float table_gap_y)
  550. {
  551. const float rows_height = InitializeTrackBoxes(row_boxes, row_metrics, table_gap_y);
  552. const int num_rows = (int)row_metrics.size();
  553. // Extend row group heights to cover the all rows they span.
  554. for (int i = 0; i < num_rows; i++)
  555. {
  556. const int group_span = grid_rows[i].group_span;
  557. if (group_span > 1 && i + group_span - 1 < num_rows)
  558. {
  559. TrackBox& metric = row_boxes[i];
  560. TrackBox& metric_last_span = row_boxes[i + group_span - 1];
  561. metric.group_size = metric_last_span.group_size + (metric_last_span.track_offset - metric.track_offset);
  562. }
  563. }
  564. SnapTrackBoxesToPixelGrid(row_boxes);
  565. return rows_height;
  566. }
  567. } // namespace Rml