TableFormattingDetails.cpp 21 KB

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