LayoutTable.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. Vector2f LayoutTable::FormatTable(Box& box, Vector2f min_size, Vector2f max_size, Element* element_table)
  39. {
  40. const ComputedValues& computed_table = element_table->GetComputedValues();
  41. Vector2f table_content_offset = box.GetPosition();
  42. Vector2f table_initial_content_size = Vector2f(box.GetSize().x, Math::Max(0.0f, box.GetSize().y));
  43. // When width or height is set, they act as minimum width or height, just as in CSS.
  44. if (computed_table.width.type != Style::Width::Auto)
  45. min_size.x = Math::Max(min_size.x, table_initial_content_size.x);
  46. if (computed_table.height.type != Style::Height::Auto)
  47. min_size.y = Math::Max(min_size.y, table_initial_content_size.y);
  48. Math::SnapToPixelGrid(table_content_offset, table_initial_content_size);
  49. const Vector2f table_gap = Vector2f(
  50. ResolveValue(computed_table.column_gap, table_initial_content_size.x),
  51. ResolveValue(computed_table.row_gap, table_initial_content_size.y)
  52. );
  53. // Construct the layout object and format the table.
  54. LayoutTable layout_table(element_table, table_gap, table_content_offset, table_initial_content_size, min_size, max_size);
  55. layout_table.FormatTable();
  56. // Update the box size based on the new table size.
  57. box.SetContent(layout_table.table_resulting_content_size);
  58. return layout_table.table_content_overflow_size;
  59. }
  60. LayoutTable::LayoutTable(Element* element_table, Vector2f table_gap, Vector2f table_content_offset, Vector2f table_initial_content_size, Vector2f table_min_size, Vector2f table_max_size)
  61. : element_table(element_table), table_min_size(table_min_size), table_max_size(table_max_size), table_gap(table_gap), table_content_offset(table_content_offset), table_initial_content_size(table_initial_content_size)
  62. {
  63. table_resulting_content_size = table_initial_content_size;
  64. }
  65. void LayoutTable::FormatTable()
  66. {
  67. DetermineColumnWidths();
  68. // Now that we have the size of each column, we can move on to formatting the elements.
  69. // 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.
  70. // At the end of a row, we then know the height of the row, and we can proceed by positioning the cells.
  71. float table_cursor_y = table_content_offset.y;
  72. cells.reserve(columns.size());
  73. const int num_table_children = element_table->GetNumChildren();
  74. // Iterate through the table rows and row groups, and format them.
  75. for (int i = 0, row = -1; i < num_table_children; i++)
  76. {
  77. using Display = Style::Display;
  78. Element* element = element_table->GetChild(i);
  79. const Display display = element->GetDisplay();
  80. if (display != Display::TableRow && display != Display::TableRowGroup)
  81. {
  82. if (row >= 0 && (display == Display::TableColumn || display == Display::TableColumnGroup))
  83. {
  84. Log::Message(Log::LT_WARNING, "Table columns and column groups must precede any table rows. Ignoring element %s.", element->GetAddress().c_str());
  85. }
  86. else if (display != Display::TableColumn && display != Display::TableColumnGroup && display != Display::None)
  87. {
  88. Log::Message(Log::LT_WARNING, "Only table columns, column groups, rows, and row groups are valid children of tables. Ignoring element %s.", element->GetAddress().c_str());
  89. }
  90. continue;
  91. }
  92. if (display == Display::TableRow)
  93. {
  94. row += 1;
  95. table_cursor_y = FormatTableRow(row, element, table_cursor_y);
  96. }
  97. else if (display == Display::TableRowGroup)
  98. {
  99. const int num_row_group_children = element->GetNumChildren();
  100. Box row_group_box;
  101. LayoutDetails::BuildBox(row_group_box, table_initial_content_size, element, false, 0.f);
  102. table_cursor_y += row_group_box.GetEdge(Box::MARGIN, Box::TOP);
  103. const float pos_border_y = table_cursor_y;
  104. table_cursor_y += row_group_box.GetEdge(Box::BORDER, Box::TOP) + row_group_box.GetEdge(Box::PADDING, Box::TOP);
  105. const float pos_content_y = table_cursor_y;
  106. for (int j = 0; j < num_row_group_children; j++)
  107. {
  108. using Display = Style::Display;
  109. Element* element_row = element->GetChild(j);
  110. const Display display_row = element_row->GetDisplay();
  111. if (display_row != Display::TableRow)
  112. {
  113. if (display_row != Display::None)
  114. {
  115. Log::Message(Log::LT_WARNING, "Only table rows are valid children of table row groups. Ignoring element %s.", element_row->GetAddress().c_str());
  116. }
  117. continue;
  118. }
  119. row += 1;
  120. table_cursor_y = FormatTableRow(row, element_row, table_cursor_y);
  121. }
  122. // Size and position the row group element
  123. const Vector2f row_group_element_content_size(
  124. table_resulting_content_size.x - row_group_box.GetSizeAcross(Box::HORIZONTAL, Box::MARGIN, Box::PADDING),
  125. Math::Max(row_group_box.GetSize().y, table_cursor_y - pos_content_y)
  126. );
  127. row_group_box.SetContent(row_group_element_content_size);
  128. const Vector2f row_group_offset = Vector2f(table_content_offset.x + row_group_box.GetEdge(Box::MARGIN, Box::LEFT), pos_border_y);
  129. element->SetOffset(row_group_offset, element_table);
  130. element->SetBox(row_group_box);
  131. table_cursor_y += row_group_box.GetEdge(Box::MARGIN, Box::BOTTOM) + row_group_box.GetEdge(Box::BORDER, Box::BOTTOM) + row_group_box.GetEdge(Box::PADDING, Box::BOTTOM);
  132. }
  133. }
  134. table_resulting_content_size.y = Math::Clamp(table_cursor_y - table_content_offset.y, table_min_size.y, table_max_size.y);
  135. // Size and position the column and column group elements.
  136. auto FormatColumn = [this](Element* element, float content_width, float offset_x) {
  137. Box box;
  138. LayoutDetails::BuildBox(box, table_initial_content_size, element, false, 0.0f);
  139. const Vector2f content_size(
  140. content_width,
  141. table_resulting_content_size.y - box.GetSizeAcross(Box::VERTICAL, Box::MARGIN, Box::PADDING)
  142. );
  143. box.SetContent(content_size);
  144. element->SetBox(box);
  145. element->SetOffset(table_content_offset + Vector2f(offset_x, box.GetEdge(Box::MARGIN, Box::TOP)), element_table);
  146. };
  147. for (const Column& column : columns)
  148. {
  149. if (column.element_column)
  150. FormatColumn(column.element_column, column.column_width, column.column_offset);
  151. if (column.element_group)
  152. FormatColumn(column.element_group, column.group_width, column.group_offset);
  153. }
  154. if (!cells.empty())
  155. {
  156. 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());
  157. }
  158. }
  159. void LayoutTable::DetermineColumnWidths()
  160. {
  161. // The column widths are determined entirely by any <col> elements preceding the first row, and <td> elements in the first row.
  162. // 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.
  163. // All flexible widths are then sized to evenly fill the width of the table.
  164. // Both <col> and <colgroup> can have border/padding which extend beyond the size of <td> and <col>, respectively.
  165. // Margins for <td>, <col>, <colgroup> are merged to produce a single left/right margin for each column, located outside <colgroup>.
  166. struct ColumnMetric {
  167. // All widths are defined in terms of the border width of cells in the column.
  168. bool has_fixed_width = false;
  169. float fixed_width = 0;
  170. float flex_width = 0;
  171. float min_width = 0;
  172. float max_width = 0;
  173. // The following are used for <col> elements.
  174. // If the element spans multiple columns, only the first column it begins at has the element set.
  175. Element* element_column = nullptr;
  176. int column_span = 0;
  177. float column_padding_border_left = 0;
  178. float column_padding_border_right = 0;
  179. // The following are used for <colgroup> elements.
  180. // If the element spans multiple columns, only the first column it begins at has the element set.
  181. Element* element_group = nullptr;
  182. int group_span = 0;
  183. float group_padding_border_left = 0;
  184. float group_padding_border_right = 0;
  185. // The margins are the sum of the margins from all <td>, <col>, <colgroup> elements.
  186. float sum_margin_left = 0;
  187. float sum_margin_right = 0;
  188. };
  189. Vector<ColumnMetric> column_metrics;
  190. Element* element_row = nullptr;
  191. const int num_table_children = element_table->GetNumChildren();
  192. // First, we find any <col> and <colgroup> elements preceding the first table row. For each such element,
  193. // add the corresponding number of entries to 'column_metrics'.
  194. for (int i = 0, column_index = 0; i < num_table_children; i++)
  195. {
  196. Element* element = element_table->GetChild(i);
  197. const Style::Display display = element->GetDisplay();
  198. auto PushGroup = [&column_metrics](Element* element_group, int group_span) {
  199. column_metrics.emplace_back();
  200. ColumnMetric& metric = column_metrics.back();
  201. metric.element_group = element_group;
  202. metric.group_span = group_span;
  203. for (int j = 1; j < group_span; j++)
  204. column_metrics.emplace_back();
  205. };
  206. auto PushColumn = [&column_metrics](Element* element_column, int column_span) {
  207. column_metrics.emplace_back();
  208. ColumnMetric& metric = column_metrics.back();
  209. metric.element_column = element_column;
  210. metric.column_span = column_span;
  211. for (int j = 1; j < column_span; j++)
  212. column_metrics.emplace_back();
  213. };
  214. if (display == Style::Display::TableColumn)
  215. {
  216. const int span = Math::Max(1, element->GetAttribute("span", 1));
  217. PushColumn(element, span);
  218. column_index += span;
  219. }
  220. else if (display == Style::Display::TableColumnGroup)
  221. {
  222. const size_t column_begin = column_metrics.size();
  223. int group_span = Math::Max(0, element->GetAttribute("span", 0));
  224. if (group_span == 0)
  225. {
  226. // Look through the column group to find all its column children.
  227. const int num_column_group_children = element->GetNumChildren();
  228. for (int j = 0; j < num_column_group_children; j++)
  229. {
  230. Element* child = element->GetChild(j);
  231. if (child->GetDisplay() == Style::Display::TableColumn)
  232. {
  233. const int column_span = Math::Max(1, child->GetAttribute("span", 1));
  234. PushColumn(child, column_span);
  235. group_span += column_span;
  236. }
  237. }
  238. }
  239. else
  240. {
  241. PushGroup(element, group_span);
  242. }
  243. if (group_span > 0)
  244. {
  245. RMLUI_ASSERT(column_begin + size_t(group_span - 1) < column_metrics.size());
  246. column_metrics[column_begin].element_group = element;
  247. column_metrics[column_begin].group_span = group_span;
  248. column_index += group_span;
  249. }
  250. }
  251. else if (display == Style::Display::TableRow)
  252. {
  253. // We found the first table row. Any columns found after this are considered invalid, break now.
  254. element_row = element;
  255. break;
  256. }
  257. else if (display == Style::Display::TableRowGroup)
  258. {
  259. // Look through the row group to find the first table row.
  260. const int num_row_group_children = element->GetNumChildren();
  261. for (int j = 0; j < num_row_group_children; j++)
  262. {
  263. Element* child = element->GetChild(j);
  264. if (child->GetDisplay() == Style::Display::TableRow)
  265. {
  266. element_row = child;
  267. break;
  268. }
  269. }
  270. // If we found our first visible row, we are done with processing columns. Otherwise, keep looking.
  271. if (element_row)
  272. break;
  273. else
  274. continue;
  275. }
  276. }
  277. auto GetEdgeWidths = [this](float& margin_left, float& margin_right, float& padding_border_left, float& padding_border_right, const ComputedValues& computed)
  278. {
  279. margin_left = ResolveValue(computed.margin_left, table_initial_content_size.x);
  280. margin_right = ResolveValue(computed.margin_right, table_initial_content_size.x);
  281. padding_border_left = Math::Max(0.0f, ResolveValue(computed.padding_left, table_initial_content_size.x)) + Math::Max(0.0f, computed.border_left_width);
  282. padding_border_right = Math::Max(0.0f, ResolveValue(computed.padding_right, table_initial_content_size.x)) + Math::Max(0.0f, computed.border_right_width);
  283. };
  284. // Fill the column metric with fixed, flexible and min/max widths, based on the element's computed values.
  285. auto InitializeColumnWidths = [this, GetEdgeWidths](ColumnMetric& metric, float& margin_left, float& margin_right, float& padding_border_left, float& padding_border_right, const ComputedValues& computed, int span, Style::BoxSizing column_target_box)
  286. {
  287. RMLUI_ASSERT(span >= 1);
  288. GetEdgeWidths(margin_left, margin_right, padding_border_left, padding_border_right, computed);
  289. const float padding_border_sum = padding_border_left + padding_border_right;
  290. // Find the min/max width.
  291. metric.min_width = ResolveValue(computed.min_width, table_initial_content_size.x);
  292. metric.max_width = (computed.max_width.value < 0.f ? FLT_MAX : ResolveValue(computed.max_width, table_initial_content_size.x));
  293. if (column_target_box == Style::BoxSizing::ContentBox && computed.box_sizing == Style::BoxSizing::BorderBox)
  294. {
  295. metric.min_width = Math::Max(0.0f, metric.min_width - padding_border_sum);
  296. if (metric.max_width < FLT_MAX)
  297. metric.max_width = Math::Max(0.0f, metric.max_width - padding_border_sum);
  298. }
  299. else if (column_target_box == Style::BoxSizing::BorderBox && computed.box_sizing == Style::BoxSizing::ContentBox)
  300. {
  301. if (metric.min_width > 0)
  302. metric.min_width += padding_border_sum;
  303. if (metric.max_width < FLT_MAX)
  304. metric.max_width += padding_border_sum;
  305. }
  306. // Find fixed and flexible widths.
  307. if (computed.width.type == Style::Width::Auto)
  308. {
  309. metric.flex_width = 1;
  310. }
  311. else if (computed.width.type == Style::Width::Percentage && computed.width.value >= 100.f)
  312. {
  313. // Percentages >= 100% are resolved as flexible widths.
  314. metric.flex_width = Math::Max(0.01f * computed.width.value / float(span), 0.f);
  315. }
  316. else
  317. {
  318. float width = ResolveValue(computed.width, table_initial_content_size.x);
  319. if (column_target_box == Style::BoxSizing::ContentBox && computed.box_sizing == Style::BoxSizing::BorderBox)
  320. width = Math::Max(0.f, width - padding_border_sum);
  321. else if (column_target_box == Style::BoxSizing::BorderBox && computed.box_sizing == Style::BoxSizing::ContentBox)
  322. width += padding_border_sum;
  323. metric.flex_width = 0;
  324. metric.fixed_width = Math::Clamp(width, metric.min_width, metric.max_width);
  325. metric.min_width = metric.fixed_width;
  326. metric.max_width = metric.fixed_width;
  327. metric.has_fixed_width = true;
  328. }
  329. if (span > 1)
  330. {
  331. // Account for distribution of fixed widths over the columns we are spanning.
  332. const float width_factor = 1.f / float(span);
  333. metric.fixed_width *= width_factor;
  334. metric.min_width *= width_factor;
  335. if (metric.max_width < FLT_MAX)
  336. metric.max_width *= width_factor;
  337. }
  338. };
  339. // First look for any <col> and <colgroup> elements preceding any <tr> elements, use them for initializing the respective columns.
  340. for (int i = 0; i < (int)column_metrics.size(); i++)
  341. {
  342. if (Element* element_group = column_metrics[i].element_group)
  343. {
  344. // The padding/border/margin of column groups are used, but their widths are ignored.
  345. const ComputedValues& computed = element_group->GetComputedValues();
  346. const int span = column_metrics[i].group_span;
  347. RMLUI_ASSERT(i + span - 1 < (int)column_metrics.size());
  348. float margin_left, margin_right;
  349. float padding_border_left, padding_border_right;
  350. GetEdgeWidths(margin_left, margin_right, padding_border_left, padding_border_right, computed);
  351. // Add left edges.
  352. ColumnMetric& metric_begin = column_metrics[i];
  353. metric_begin.group_padding_border_left = padding_border_left;
  354. metric_begin.sum_margin_left = margin_left;
  355. // Add right edges.
  356. ColumnMetric& metric_last = column_metrics[i + span - 1];
  357. metric_last.group_padding_border_right = padding_border_right;
  358. metric_last.sum_margin_right = margin_right;
  359. // Set the width to flexible by default. This may be overrided later by <col> or <td> elements.
  360. for (int j = 0; j < span; j++)
  361. {
  362. ColumnMetric& metric = column_metrics[i + j];
  363. metric.flex_width = 1;
  364. metric.max_width = FLT_MAX;
  365. }
  366. }
  367. if (Element* element_column = column_metrics[i].element_column)
  368. {
  369. // The padding/border/margin and widths of columns are used.
  370. const ComputedValues& computed = element_column->GetComputedValues();
  371. const int span = column_metrics[i].column_span;
  372. RMLUI_ASSERT(i + span - 1 < (int)column_metrics.size());
  373. float margin_left, margin_right;
  374. float padding_border_left, padding_border_right;
  375. ColumnMetric& metric_begin = column_metrics[i];
  376. InitializeColumnWidths(metric_begin, margin_left, margin_right, padding_border_left, padding_border_right, computed, span, Style::BoxSizing::ContentBox);
  377. // Add left edges. Increment the values because the edges may already have been sized from <colgroup>.
  378. metric_begin.column_padding_border_left += padding_border_left;
  379. metric_begin.sum_margin_left += margin_left;
  380. // Add right edges.
  381. ColumnMetric& metric_last = column_metrics[i + span - 1];
  382. metric_last.column_padding_border_right += padding_border_right;
  383. metric_last.sum_margin_right += margin_right;
  384. // The widths of all spanning columns are distributed equally.
  385. for (int j = 1; j < span; j++)
  386. {
  387. ColumnMetric& metric = column_metrics[i + j];
  388. metric.has_fixed_width = metric_begin.has_fixed_width;
  389. metric.fixed_width = metric_begin.fixed_width;
  390. metric.flex_width = metric_begin.flex_width;
  391. metric.min_width = metric_begin.min_width;
  392. metric.max_width = metric_begin.max_width;
  393. }
  394. }
  395. }
  396. const int num_row_children = (!element_row ? 0 : element_row->GetNumChildren());
  397. column_metrics.reserve(num_row_children);
  398. // Next, walk through the cells in the first table row.
  399. // - We add new columns here if they are not already represented by a <col> or <colgroup> element.
  400. // - Otherwise, we merge the metrics of the cell with the existing column: If the existing column
  401. // has auto min-/max-/width, we use the cell's min-/max-/width if it has any.
  402. // Note: Cells use their border width to line up the column, while <col> use their content width.
  403. for (int i = 0, column = 0; i < num_row_children; i++)
  404. {
  405. Element* element = element_row->GetChild(i);
  406. const ComputedValues& computed = element->GetComputedValues();
  407. if (computed.display != Style::Display::TableCell)
  408. continue;
  409. const int colspan = Math::Max(1, element->GetAttribute("colspan", 1));
  410. float margin_left, margin_right;
  411. float padding_border_left, padding_border_right;
  412. ColumnMetric column_metric;
  413. InitializeColumnWidths(column_metric, margin_left, margin_right, padding_border_left, padding_border_right, computed, colspan, Style::BoxSizing::BorderBox);
  414. column_metric.sum_margin_left = margin_left;
  415. // Merge with existing columns if they exist, or add new columns.
  416. for (int j = 0; j < colspan; j++)
  417. {
  418. if (j == 1)
  419. column_metric.sum_margin_left = 0;
  420. if (j == colspan - 1)
  421. column_metric.sum_margin_right = margin_right;
  422. if (j + column < (int)column_metrics.size())
  423. {
  424. // Merge the existing column with the cell sizing data.
  425. ColumnMetric& destination = column_metrics[j + column];
  426. if (!destination.has_fixed_width && column_metric.has_fixed_width)
  427. {
  428. destination.fixed_width = column_metric.fixed_width;
  429. destination.flex_width = 0;
  430. destination.has_fixed_width = true;
  431. }
  432. if (destination.min_width == 0)
  433. destination.min_width = column_metric.min_width;
  434. if (destination.max_width == FLT_MAX)
  435. destination.max_width = column_metric.max_width;
  436. destination.sum_margin_left += column_metric.sum_margin_left;
  437. destination.sum_margin_right += column_metric.sum_margin_right;
  438. }
  439. else
  440. {
  441. // No existing column, add a new one.
  442. column_metrics.emplace_back(column_metric);
  443. }
  444. }
  445. column += colspan;
  446. }
  447. if (column_metrics.empty())
  448. {
  449. // No columns found in this table.
  450. return;
  451. }
  452. // The fixed spacing includes column gaps and the column and column group elements' padding, border, and margins.
  453. float sum_fixed_spacing = table_gap.x * float((int)column_metrics.size() - 1);
  454. for (const ColumnMetric& metric : column_metrics)
  455. {
  456. sum_fixed_spacing += metric.column_padding_border_left + metric.column_padding_border_right;
  457. sum_fixed_spacing += metric.group_padding_border_left + metric.group_padding_border_left;
  458. sum_fixed_spacing += metric.sum_margin_left + metric.sum_margin_right;
  459. }
  460. float table_available_width = 0.0f;
  461. // Now all the widths are determined in terms of fixed or flexible widths.
  462. // Next, convert any flexible widths to fixed widths by filling up the table width.
  463. for (bool continue_iteration = true; continue_iteration; )
  464. {
  465. continue_iteration = false;
  466. float fr_to_px_ratio = 0;
  467. // Calculate the fr/px-ratio. [fr] is here the unit for flexible width.
  468. {
  469. float sum_fixed_width = sum_fixed_spacing; // [px]
  470. float sum_flex_width = 0; // [fr]
  471. for (const ColumnMetric& metric : column_metrics)
  472. {
  473. sum_flex_width += metric.flex_width;
  474. sum_fixed_width += (metric.flex_width == 0.f ? metric.fixed_width : 0.0f);
  475. }
  476. sum_flex_width = Math::Max(1.f, sum_flex_width);
  477. table_available_width = table_initial_content_size.x - sum_fixed_width;
  478. fr_to_px_ratio = Math::Max(0.0f, table_available_width) / sum_flex_width;
  479. }
  480. // Iterate through the columns and convert flexible widths to fixed widths.
  481. for (auto& metric : column_metrics)
  482. {
  483. if (metric.flex_width > 0)
  484. {
  485. const float fixed_flex_width = metric.flex_width * fr_to_px_ratio;
  486. metric.fixed_width = Math::Clamp(fixed_flex_width, metric.min_width, metric.max_width);
  487. table_available_width -= metric.fixed_width;
  488. if (metric.fixed_width != fixed_flex_width)
  489. {
  490. // 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.
  491. metric.flex_width = 0.0f;
  492. continue_iteration = true;
  493. }
  494. }
  495. }
  496. }
  497. // If we have distributed all the flexible space, and there is still space available, then distribute the available space over
  498. // the column widths while respecting max-widths.
  499. if (table_available_width > 0.5f)
  500. {
  501. const int num_columns = (int)column_metrics.size();
  502. struct ColumnAvailableWidth {
  503. int column;
  504. float available_width;
  505. };
  506. Vector<ColumnAvailableWidth> col_available_widths(num_columns);
  507. // Find the available width of all 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. // Generate the column results based on the metrics.
  531. columns.resize(column_metrics.size());
  532. float cursor_x = 0;
  533. for (size_t i = 0; i < column_metrics.size(); i++)
  534. {
  535. Column& col = columns[i];
  536. const ColumnMetric& metric = column_metrics[i];
  537. col.element_column = metric.element_column;
  538. col.element_group = metric.element_group;
  539. col.group_offset = cursor_x + metric.sum_margin_left;
  540. col.column_offset = col.group_offset + metric.group_padding_border_left;
  541. col.cell_offset = col.column_offset + metric.column_padding_border_left;
  542. // The group and column width will be extended if they span multiple columns (see next loop).
  543. col.group_width = metric.fixed_width + metric.column_padding_border_left + metric.column_padding_border_right;
  544. col.column_width = metric.fixed_width;
  545. col.cell_width = metric.fixed_width;
  546. cursor_x = col.cell_offset + metric.fixed_width + metric.column_padding_border_right + metric.group_padding_border_right + metric.sum_margin_right;
  547. if (i != column_metrics.size() - 1)
  548. cursor_x += table_gap.x;
  549. }
  550. // Adjust the table content width based on the accumulated column widths and spacing.
  551. table_resulting_content_size.x = Math::Clamp(cursor_x, table_min_size.x, table_max_size.x);
  552. // Extend column and column group widths to cover multiple columns for spanning column (group) elements.
  553. for (size_t i = 0; i < column_metrics.size(); i++)
  554. {
  555. const ColumnMetric& metric = column_metrics[i];
  556. if (metric.element_column && metric.column_span > 1 && i + metric.column_span - 1 < column_metrics.size())
  557. {
  558. Column& col = columns[i];
  559. Column& col_last_span = columns[i + metric.column_span - 1];
  560. col.column_width = col_last_span.cell_width + (col_last_span.cell_offset - col.cell_offset);
  561. }
  562. if (metric.element_group && metric.group_span > 1 && i + metric.group_span - 1 < column_metrics.size())
  563. {
  564. Column& col = columns[i];
  565. Column& col_last_span = columns[i + metric.group_span - 1];
  566. col.group_width = col_last_span.group_width + (col_last_span.column_offset - col.column_offset);
  567. }
  568. }
  569. // Finally, snap boxes to the pixel grid.
  570. for (Column& col : columns)
  571. {
  572. Math::SnapToPixelGrid(col.cell_offset, col.cell_width);
  573. Math::SnapToPixelGrid(col.column_offset, col.column_width);
  574. Math::SnapToPixelGrid(col.group_offset, col.group_width);
  575. }
  576. }
  577. float LayoutTable::FormatTableRow(int row_index, Element* element_row, float row_position_y)
  578. {
  579. // First, determine the row height, then format all cells ending at this row.
  580. if (row_index > 0)
  581. row_position_y += table_gap.y;
  582. const ComputedValues& computed_row = element_row->GetComputedValues();
  583. Vector2f table_cursor = Vector2f(table_content_offset.x, row_position_y);
  584. Box row_box;
  585. float row_min_height, row_max_height;
  586. LayoutDetails::BuildBox(row_box, table_initial_content_size, element_row, false, 0.f);
  587. LayoutDetails::GetMinMaxHeight(row_min_height, row_max_height, computed_row, row_box, table_initial_content_size.y);
  588. // Add the row top spacing to the cursor and row-spanning elements.
  589. table_cursor.y += row_box.GetEdge(Box::MARGIN, Box::TOP) + row_box.GetEdge(Box::BORDER, Box::TOP) + row_box.GetEdge(Box::PADDING, Box::TOP);
  590. const int num_cells_spanning_this_row = (int)cells.size();
  591. const int num_row_children = element_row->GetNumChildren();
  592. // For all child cell elements of this row, add them to the list of cells and determine their position.
  593. for (int j = 0, column = 0; j < num_row_children; j++)
  594. {
  595. Element* element_cell = element_row->GetChild(j);
  596. const ComputedValues& computed_cell = element_cell->GetComputedValues();
  597. if (computed_cell.display != Style::Display::TableCell)
  598. {
  599. if (computed_cell.display != Style::Display::None)
  600. Log::Message(Log::LT_WARNING, "Only table cells are allowed as children of table rows. %s", element_cell->GetAddress().c_str());
  601. continue;
  602. }
  603. const int row_span = Math::Max(1, element_cell->GetAttribute("rowspan", 1));
  604. const int col_span = Math::Max(1, element_cell->GetAttribute("colspan", 1));
  605. // Offset the column if we have any rowspan elements from previous rows overlapping with the current column.
  606. for (bool continue_offset_column = true; continue_offset_column; )
  607. {
  608. continue_offset_column = false;
  609. for (int k = 0; k < num_cells_spanning_this_row; k++)
  610. {
  611. if (column >= cells[k].column_begin && column <= cells[k].column_last)
  612. {
  613. column = cells[k].column_last + 1;
  614. continue_offset_column = true;
  615. break;
  616. }
  617. }
  618. }
  619. const int column_last = column + col_span - 1;
  620. if (column_last >= (int)columns.size())
  621. {
  622. 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.",
  623. row_index + 1, element_row->GetAddress().c_str(), (int)columns.size());
  624. break;
  625. }
  626. // Find the horizontal offset for this cell.
  627. table_cursor.x = table_content_offset.x + columns[column].cell_offset;
  628. // Add the new cell to our list.
  629. cells.emplace_back();
  630. Cell& cell = cells.back();
  631. cell.row_last = row_index + row_span - 1;
  632. cell.column_begin = column;
  633. cell.column_last = column_last;
  634. cell.element_cell = element_cell;
  635. cell.table_offset = table_cursor;
  636. // Determine the cell's box for formatting later, we may get an indefinite (-1) vertical content size.
  637. Box& box = cell.box;
  638. LayoutDetails::BuildBox(box, table_initial_content_size, element_cell, false, 0.f);
  639. // Determine the cell's content width. Include any spanning columns in the cell width.
  640. const float cell_border_width = columns[column_last].cell_width + (columns[column_last].cell_offset - columns[column].cell_offset);
  641. const float content_width = Math::Max(0.0f, cell_border_width - box.GetSizeAcross(Box::HORIZONTAL, Box::BORDER, Box::PADDING));
  642. box.SetContent(Vector2f(content_width, box.GetSize().y));
  643. column += col_span;
  644. }
  645. // Partition the cells to determine those who end at this row.
  646. const auto it_cells_in_row_end = std::partition(cells.begin(), cells.end(), [row_index](const Cell& cell) { return cell.row_last == row_index; });
  647. // Determine the row height.
  648. float row_content_height = 0;
  649. if (row_box.GetSize().y >= 0)
  650. {
  651. // The row has a definite size, use that.
  652. row_content_height = row_box.GetSize().y;
  653. }
  654. else
  655. {
  656. // The row does not have a definite size, we will use the maximum height of all its cells to determine the row height.
  657. // 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.
  658. for (auto it = cells.begin(); it != it_cells_in_row_end; ++it)
  659. {
  660. Cell& cell = *it;
  661. Element* element_cell = cell.element_cell;
  662. Box& box = cell.box;
  663. // If the cell's height is also indefinite, we need to format it to get its height.
  664. if (box.GetSize().y < 0)
  665. {
  666. LayoutEngine::FormatElement(element_cell, table_initial_content_size, &box);
  667. box.SetContent(element_cell->GetBox().GetSize());
  668. }
  669. const float cell_inrow_height = box.GetSizeAcross(Box::VERTICAL, Box::BORDER) - (table_cursor.y - cell.table_offset.y);
  670. row_content_height = Math::Max(row_content_height, cell_inrow_height);
  671. }
  672. }
  673. row_content_height = Math::Clamp(row_content_height, row_min_height, row_max_height);
  674. table_cursor.y += row_content_height;
  675. // Now we have the height of the row, position and format each cell.
  676. // Loop through every cell ending at this row.
  677. for (auto it = cells.begin(); it != it_cells_in_row_end; ++it)
  678. {
  679. Cell& cell = *it;
  680. Element* element_cell = cell.element_cell;
  681. Box& box = cell.box;
  682. Style::VerticalAlign vertical_align = cell.element_cell->GetComputedValues().vertical_align;
  683. const float cell_border_height = table_cursor.y - cell.table_offset.y;
  684. if (box.GetSize().y < 0)
  685. {
  686. const bool is_aligned = (vertical_align.type == Style::VerticalAlign::Middle || vertical_align.type == Style::VerticalAlign::Bottom);
  687. if (is_aligned)
  688. {
  689. // The size of the cell is indefinite, we need to get the height by formatting the cell.
  690. LayoutEngine::FormatElement(element_cell, table_initial_content_size, &box);
  691. box.SetContent(element_cell->GetBox().GetSize());
  692. }
  693. else
  694. {
  695. box.SetContent(Vector2f(box.GetSize().x, Math::Max(0.0f, cell_border_height - box.GetSizeAcross(Box::VERTICAL, Box::BORDER, Box::PADDING))));
  696. }
  697. }
  698. const float available_height = cell_border_height - box.GetSizeAcross(Box::VERTICAL, Box::BORDER);
  699. if (available_height > 0)
  700. {
  701. // Pad the cell for vertical alignment
  702. float add_padding_top;
  703. float add_padding_bottom;
  704. switch (vertical_align.type)
  705. {
  706. case Style::VerticalAlign::Bottom:
  707. add_padding_top = available_height;
  708. add_padding_bottom = 0;
  709. break;
  710. case Style::VerticalAlign::Middle:
  711. add_padding_top = 0.5f * available_height;
  712. add_padding_bottom = 0.5f * available_height;
  713. break;
  714. case Style::VerticalAlign::Top:
  715. default:
  716. add_padding_top = 0.0f;
  717. add_padding_bottom = available_height;
  718. }
  719. box.SetEdge(Box::PADDING, Box::TOP, box.GetEdge(Box::PADDING, Box::TOP) + add_padding_top);
  720. box.SetEdge(Box::PADDING, Box::BOTTOM, box.GetEdge(Box::PADDING, Box::BOTTOM) + add_padding_bottom);
  721. }
  722. // Format the cell in a new block formatting context.
  723. // @performance: We may have already formatted the element during the above procedure without the extra padding. In that case, we may
  724. // instead set the new box and offset all descending elements whose offset parent is the cell, to account for the new padding box.
  725. // That should be faster than formatting the element again, but there may be edge-cases not accounted for.
  726. Vector2f cell_visible_overflow_size;
  727. LayoutEngine::FormatElement(element_cell, table_initial_content_size, &box, &cell_visible_overflow_size);
  728. // Set the position of the element within the the table container
  729. element_cell->SetOffset(cell.table_offset, element_table);
  730. // The cell contents may overflow, propagate this to the table.
  731. 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);
  732. 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);
  733. }
  734. // Remove the formatted cells from pending
  735. cells.erase(cells.begin(), it_cells_in_row_end);
  736. // Size and position the row element
  737. const Vector2f row_element_content_size(
  738. table_resulting_content_size.x - row_box.GetSizeAcross(Box::HORIZONTAL, Box::MARGIN, Box::PADDING),
  739. Math::Max(row_box.GetSize().y, row_content_height)
  740. );
  741. row_box.SetContent(row_element_content_size);
  742. const Vector2f row_element_offset = Vector2f(table_content_offset.x, row_position_y) + Vector2f(row_box.GetEdge(Box::MARGIN, Box::LEFT), row_box.GetEdge(Box::MARGIN, Box::TOP));
  743. element_row->SetOffset(row_element_offset, element_table);
  744. element_row->SetBox(row_box);
  745. // Add the row bottom spacing to the cursor and any row-spanning cells.
  746. 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);
  747. table_cursor.y += row_bottom_spacing;
  748. return table_cursor.y;
  749. }
  750. } // namespace Rml