TableFormattingDetails.cpp 23 KB

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