GridFormattingContext.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Node.h>
  7. #include <LibWeb/Layout/Box.h>
  8. #include <LibWeb/Layout/GridFormattingContext.h>
  9. namespace Web::Layout {
  10. GridFormattingContext::GridFormattingContext(LayoutState& state, BlockContainer const& block_container, FormattingContext* parent)
  11. : BlockFormattingContext(state, block_container, parent)
  12. {
  13. }
  14. GridFormattingContext::~GridFormattingContext() = default;
  15. void GridFormattingContext::run(Box const& box, LayoutMode, [[maybe_unused]] AvailableSpace const& available_width, [[maybe_unused]] AvailableSpace const& available_height)
  16. {
  17. auto should_skip_is_anonymous_text_run = [&](Box& child_box) -> bool {
  18. if (child_box.is_anonymous() && !child_box.first_child_of_type<BlockContainer>()) {
  19. bool contains_only_white_space = true;
  20. child_box.for_each_in_subtree([&](auto const& node) {
  21. if (!is<TextNode>(node) || !static_cast<TextNode const&>(node).dom_node().data().is_whitespace()) {
  22. contains_only_white_space = false;
  23. return IterationDecision::Break;
  24. }
  25. return IterationDecision::Continue;
  26. });
  27. if (contains_only_white_space)
  28. return true;
  29. }
  30. return false;
  31. };
  32. auto maybe_add_column_to_occupation_grid = [](int needed_number_of_columns, Vector<Vector<bool>>& occupation_grid) -> void {
  33. int current_column_count = (int)occupation_grid[0].size();
  34. if (needed_number_of_columns <= current_column_count)
  35. return;
  36. for (auto& occupation_grid_row : occupation_grid)
  37. for (int idx = 0; idx < (needed_number_of_columns + 1) - current_column_count; idx++)
  38. occupation_grid_row.append(false);
  39. };
  40. auto maybe_add_row_to_occupation_grid = [](int needed_number_of_rows, Vector<Vector<bool>>& occupation_grid) -> void {
  41. if (needed_number_of_rows <= (int)occupation_grid.size())
  42. return;
  43. Vector<bool> new_occupation_grid_row;
  44. for (int idx = 0; idx < (int)occupation_grid[0].size(); idx++)
  45. new_occupation_grid_row.append(false);
  46. for (int idx = 0; idx < needed_number_of_rows - (int)occupation_grid.size(); idx++)
  47. occupation_grid.append(new_occupation_grid_row);
  48. };
  49. auto set_occupied_cells = [](int row_start, int row_end, int column_start, int column_end, Vector<Vector<bool>>& occupation_grid) -> void {
  50. for (int row_index = 0; row_index < (int)occupation_grid.size(); row_index++) {
  51. if (row_index >= row_start && row_index < row_end) {
  52. for (int column_index = 0; column_index < (int)occupation_grid[0].size(); column_index++) {
  53. if (column_index >= column_start && column_index < column_end) {
  54. occupation_grid[row_index][column_index] = true;
  55. }
  56. }
  57. }
  58. }
  59. };
  60. // https://drafts.csswg.org/css-grid/#overview-placement
  61. // 2.2. Placing Items
  62. // The contents of the grid container are organized into individual grid items (analogous to
  63. // flex items), which are then assigned to predefined areas in the grid. They can be explicitly
  64. // placed using coordinates through the grid-placement properties or implicitly placed into
  65. // empty areas using auto-placement.
  66. struct PositionedBox {
  67. Box const& box;
  68. int row { 0 };
  69. int row_span { 1 };
  70. int column { 0 };
  71. int column_span { 1 };
  72. float computed_height { 0 };
  73. };
  74. Vector<PositionedBox> positioned_boxes;
  75. Vector<Vector<bool>> occupation_grid;
  76. Vector<bool> occupation_grid_row;
  77. for (int column_index = 0; column_index < max((int)box.computed_values().grid_template_columns().size(), 1); column_index++)
  78. occupation_grid_row.append(false);
  79. for (int row_index = 0; row_index < max((int)box.computed_values().grid_template_rows().size(), 1); row_index++)
  80. occupation_grid.append(occupation_grid_row);
  81. Vector<Box const&> boxes_to_place;
  82. box.for_each_child_of_type<Box>([&](Box& child_box) {
  83. if (should_skip_is_anonymous_text_run(child_box))
  84. return IterationDecision::Continue;
  85. boxes_to_place.append(child_box);
  86. return IterationDecision::Continue;
  87. });
  88. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  89. // 8.5. Grid Item Placement Algorithm
  90. // FIXME: 0. Generate anonymous grid items
  91. // 1. Position anything that's not auto-positioned.
  92. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  93. auto const& child_box = boxes_to_place[i];
  94. if (child_box.computed_values().grid_row_start().is_auto()
  95. || child_box.computed_values().grid_row_end().is_auto()
  96. || child_box.computed_values().grid_column_start().is_auto()
  97. || child_box.computed_values().grid_column_end().is_auto())
  98. continue;
  99. int row_start = child_box.computed_values().grid_row_start().position();
  100. int row_end = child_box.computed_values().grid_row_end().position();
  101. int column_start = child_box.computed_values().grid_column_start().position();
  102. int column_end = child_box.computed_values().grid_column_end().position();
  103. int row_span = 1;
  104. int column_span = 1;
  105. // https://drafts.csswg.org/css-grid/#grid-placement-int
  106. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  107. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  108. // instead counts in reverse, starting from the end edge of the explicit grid.
  109. if (row_end < 0)
  110. row_end = static_cast<int>(occupation_grid.size()) + row_end + 2;
  111. if (column_end < 0)
  112. column_end = static_cast<int>(occupation_grid[0].size()) + column_end + 2;
  113. // FIXME: If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  114. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  115. // of finding this position.
  116. // FIXME: An <integer> value of zero makes the declaration invalid.
  117. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  118. // 8.3.1. Grid Placement Conflict Handling
  119. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  120. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  121. // line.
  122. if (row_start > row_end) {
  123. auto temp = row_end;
  124. row_end = row_start;
  125. row_start = temp;
  126. }
  127. if (column_start > column_end) {
  128. auto temp = column_end;
  129. column_end = column_start;
  130. column_start = temp;
  131. }
  132. if (row_start != row_end)
  133. row_span = row_end - row_start;
  134. if (column_start != column_end)
  135. column_span = column_end - column_start;
  136. // FIXME: If the placement contains two spans, remove the one contributed by the end grid-placement
  137. // property.
  138. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  139. row_start -= 1;
  140. column_start -= 1;
  141. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  142. maybe_add_row_to_occupation_grid(row_start + row_span, occupation_grid);
  143. maybe_add_column_to_occupation_grid(column_start + column_span, occupation_grid);
  144. set_occupied_cells(row_start, row_start + row_span, column_start, column_start + column_span, occupation_grid);
  145. boxes_to_place.remove(i);
  146. i--;
  147. }
  148. // 2. Process the items locked to a given row.
  149. // FIXME: Do "dense" packing
  150. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  151. auto const& child_box = boxes_to_place[i];
  152. if (child_box.computed_values().grid_row_start().is_auto()
  153. || child_box.computed_values().grid_row_end().is_auto())
  154. continue;
  155. int row_start = child_box.computed_values().grid_row_start().position();
  156. int row_end = child_box.computed_values().grid_row_end().position();
  157. int row_span = 1;
  158. // https://drafts.csswg.org/css-grid/#grid-placement-int
  159. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  160. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  161. // instead counts in reverse, starting from the end edge of the explicit grid.
  162. if (row_end < 0)
  163. row_end = static_cast<int>(occupation_grid.size()) + row_end + 2;
  164. // FIXME: If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  165. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  166. // of finding this position.
  167. // FIXME: An <integer> value of zero makes the declaration invalid.
  168. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  169. // 8.3.1. Grid Placement Conflict Handling
  170. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  171. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  172. // line.
  173. if (row_start > row_end) {
  174. auto temp = row_end;
  175. row_end = row_start;
  176. row_start = temp;
  177. }
  178. if (row_start != row_end)
  179. row_span = row_end - row_start;
  180. // FIXME: If the placement contains two spans, remove the one contributed by the end grid-placement
  181. // property.
  182. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  183. row_start -= 1;
  184. maybe_add_row_to_occupation_grid(row_start + row_span, occupation_grid);
  185. int column_start = 0;
  186. int column_span = 1;
  187. bool found_available_column = false;
  188. for (int column_index = column_start; column_index < (int)occupation_grid[0].size(); column_index++) {
  189. if (!occupation_grid[0][column_index]) {
  190. found_available_column = true;
  191. column_start = column_index;
  192. break;
  193. }
  194. }
  195. if (!found_available_column) {
  196. column_start = occupation_grid[0].size();
  197. maybe_add_column_to_occupation_grid(column_start + column_span, occupation_grid);
  198. }
  199. set_occupied_cells(row_start, row_start + row_span, column_start, column_start + column_span, occupation_grid);
  200. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  201. boxes_to_place.remove(i);
  202. i--;
  203. }
  204. // 3. Determine the columns in the implicit grid.
  205. // NOTE: "implicit grid" here is the same as the occupation_grid
  206. // 3.1. Start with the columns from the explicit grid.
  207. // NOTE: Done in step 1.
  208. // 3.2. Among all the items with a definite column position (explicitly positioned items, items
  209. // positioned in the previous step, and items not yet positioned but with a definite column) add
  210. // columns to the beginning and end of the implicit grid as necessary to accommodate those items.
  211. // NOTE: "Explicitly positioned items" and "items positioned in the previous step" done in step 1
  212. // and 2, respectively. Adding columns for "items not yet positioned but with a definite column"
  213. // will be done in step 4.
  214. // 3.3. If the largest column span among all the items without a definite column position is larger
  215. // than the width of the implicit grid, add columns to the end of the implicit grid to accommodate
  216. // that column span.
  217. // NOTE: Done in step 1, 2, and will be done in step 4.
  218. // 4. Position the remaining grid items.
  219. // For each grid item that hasn't been positioned by the previous steps, in order-modified document
  220. // order:
  221. auto auto_placement_cursor_x = 0;
  222. auto auto_placement_cursor_y = 0;
  223. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  224. auto const& child_box = boxes_to_place[i];
  225. // 4.1. For sparse packing:
  226. // FIXME: no distinction made. See #4.2
  227. // 4.1.1. If the item has a definite column position:
  228. if (!child_box.computed_values().grid_column_start().is_auto()) {
  229. int column_start = child_box.computed_values().grid_column_start().position();
  230. int column_end = child_box.computed_values().grid_column_end().position();
  231. int column_span = 1;
  232. // https://drafts.csswg.org/css-grid/#grid-placement-int
  233. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  234. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  235. // instead counts in reverse, starting from the end edge of the explicit grid.
  236. if (column_end < 0)
  237. column_end = static_cast<int>(occupation_grid[0].size()) + column_end + 2;
  238. // FIXME: If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  239. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  240. // of finding this position.
  241. // FIXME: An <integer> value of zero makes the declaration invalid.
  242. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  243. // 8.3.1. Grid Placement Conflict Handling
  244. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  245. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  246. // line.
  247. if (!child_box.computed_values().grid_column_end().is_auto()) {
  248. if (column_start > column_end) {
  249. auto temp = column_end;
  250. column_end = column_start;
  251. column_start = temp;
  252. }
  253. if (column_start != column_end)
  254. column_span = column_end - column_start;
  255. }
  256. // FIXME: If the placement contains two spans, remove the one contributed by the end grid-placement
  257. // property.
  258. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  259. column_start -= 1;
  260. // 4.1.1.1. Set the column position of the cursor to the grid item's column-start line. If this is
  261. // less than the previous column position of the cursor, increment the row position by 1.
  262. auto_placement_cursor_x = column_start;
  263. if (column_start < auto_placement_cursor_x)
  264. auto_placement_cursor_y++;
  265. maybe_add_column_to_occupation_grid(auto_placement_cursor_x + 1, occupation_grid);
  266. maybe_add_row_to_occupation_grid(auto_placement_cursor_y + 1, occupation_grid);
  267. // 4.1.1.2. Increment the cursor's row position until a value is found where the grid item does not
  268. // overlap any occupied grid cells (creating new rows in the implicit grid as necessary).
  269. while (true) {
  270. if (!occupation_grid[auto_placement_cursor_y][column_start]) {
  271. break;
  272. }
  273. auto_placement_cursor_y++;
  274. maybe_add_row_to_occupation_grid(auto_placement_cursor_y + 1, occupation_grid);
  275. }
  276. // 4.1.1.3. Set the item's row-start line to the cursor's row position, and set the item's row-end
  277. // line according to its span from that position.
  278. set_occupied_cells(auto_placement_cursor_y, auto_placement_cursor_y + 1, column_start, column_start + column_span, occupation_grid);
  279. positioned_boxes.append({ child_box, auto_placement_cursor_y, 1, column_start, column_span });
  280. }
  281. // 4.1.2. If the item has an automatic grid position in both axes:
  282. else {
  283. // 4.1.2.1. Increment the column position of the auto-placement cursor until either this item's grid
  284. // area does not overlap any occupied grid cells, or the cursor's column position, plus the item's
  285. // column span, overflow the number of columns in the implicit grid, as determined earlier in this
  286. // algorithm.
  287. auto column_start = 0;
  288. auto column_span = 1;
  289. auto row_start = 0;
  290. auto row_span = 1;
  291. auto found_unoccupied_cell = false;
  292. for (int row_index = auto_placement_cursor_y; row_index < (int)occupation_grid.size(); row_index++) {
  293. for (int column_index = auto_placement_cursor_x; column_index < (int)occupation_grid[0].size(); column_index++) {
  294. if (!occupation_grid[row_index][column_index]) {
  295. found_unoccupied_cell = true;
  296. column_start = column_index;
  297. row_start = row_index;
  298. goto finish;
  299. }
  300. auto_placement_cursor_x = 0;
  301. }
  302. auto_placement_cursor_x = 0;
  303. auto_placement_cursor_y++;
  304. }
  305. finish:
  306. // 4.1.2.2. If a non-overlapping position was found in the previous step, set the item's row-start
  307. // and column-start lines to the cursor's position. Otherwise, increment the auto-placement cursor's
  308. // row position (creating new rows in the implicit grid as necessary), set its column position to the
  309. // start-most column line in the implicit grid, and return to the previous step.
  310. if (!found_unoccupied_cell) {
  311. row_start = (int)occupation_grid.size();
  312. maybe_add_row_to_occupation_grid((int)occupation_grid.size() + 1, occupation_grid);
  313. }
  314. set_occupied_cells(row_start, row_start + row_span, column_start, column_start + column_span, occupation_grid);
  315. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  316. }
  317. boxes_to_place.remove(i);
  318. i--;
  319. // FIXME: 4.2. For dense packing:
  320. }
  321. auto& box_state = m_state.get_mutable(box);
  322. for (auto& positioned_box : positioned_boxes) {
  323. auto& child_box_state = m_state.get_mutable(positioned_box.box);
  324. if (child_box_state.content_height() > positioned_box.computed_height)
  325. positioned_box.computed_height = child_box_state.content_height();
  326. (void)layout_inside(positioned_box.box, LayoutMode::Normal);
  327. if (child_box_state.content_height() > positioned_box.computed_height)
  328. positioned_box.computed_height = child_box_state.content_height();
  329. }
  330. // https://drafts.csswg.org/css-grid/#overview-sizing
  331. // 2.3. Sizing the Grid
  332. // Once the grid items have been placed, the sizes of the grid tracks (rows and columns) are
  333. // calculated, accounting for the sizes of their contents and/or available space as specified in
  334. // the grid definition.
  335. // https://drafts.csswg.org/css-grid/#layout-algorithm
  336. // 12. Grid Sizing
  337. // This section defines the grid sizing algorithm, which determines the size of all grid tracks and,
  338. // by extension, the entire grid.
  339. // Each track has specified minimum and maximum sizing functions (which may be the same). Each
  340. // sizing function is either:
  341. // - A fixed sizing function (<length> or resolvable <percentage>).
  342. // - An intrinsic sizing function (min-content, max-content, auto, fit-content()).
  343. // - A flexible sizing function (<flex>).
  344. // The grid sizing algorithm defines how to resolve these sizing constraints into used track sizes.
  345. struct GridTrack {
  346. CSS::GridTrackSize min_track_sizing_function;
  347. CSS::GridTrackSize max_track_sizing_function;
  348. float base_size { 0 };
  349. float growth_limit { 0 };
  350. };
  351. Vector<GridTrack> grid_rows;
  352. Vector<GridTrack> grid_columns;
  353. for (auto& column_size : box.computed_values().grid_template_columns())
  354. grid_columns.append({ column_size, column_size });
  355. for (auto& row_size : box.computed_values().grid_template_rows())
  356. grid_rows.append({ row_size, row_size });
  357. for (int column_index = grid_columns.size(); column_index < static_cast<int>(occupation_grid[0].size()); column_index++)
  358. grid_columns.append({ CSS::GridTrackSize::make_auto(), CSS::GridTrackSize::make_auto() });
  359. for (int row_index = grid_rows.size(); row_index < static_cast<int>(occupation_grid.size()); row_index++)
  360. grid_rows.append({ CSS::GridTrackSize::make_auto(), CSS::GridTrackSize::make_auto() });
  361. // https://drafts.csswg.org/css-grid/#algo-overview
  362. // 12.1. Grid Sizing Algorithm
  363. // FIXME: Deals with subgrids, min-content, and justify-content.. not implemented yet
  364. // https://drafts.csswg.org/css-grid/#algo-track-sizing
  365. // 12.3. Track Sizing Algorithm
  366. // The remainder of this section is the track sizing algorithm, which calculates from the min and
  367. // max track sizing functions the used track size. Each track has a base size, a <length> which
  368. // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
  369. // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:
  370. // 1. Initialize Track Sizes
  371. // 2. Resolve Intrinsic Track Sizes
  372. // 3. Maximize Tracks
  373. // 4. Expand Flexible Tracks
  374. // 5. [[#algo-stretch|Expand Stretched auto Tracks]]
  375. // https://drafts.csswg.org/css-grid/#algo-init
  376. // 12.4. Initialize Track Sizes
  377. // Initialize each track’s base size and growth limit.
  378. for (auto& grid_column : grid_columns) {
  379. // For each track, if the track’s min track sizing function is:
  380. switch (grid_column.min_track_sizing_function.type()) {
  381. // - A fixed sizing function
  382. // Resolve to an absolute length and use that size as the track’s initial base size.
  383. // Indefinite lengths cannot occur, as they’re treated as auto.
  384. case CSS::GridTrackSize::Type::Length:
  385. if (!grid_column.min_track_sizing_function.length().is_auto())
  386. grid_column.base_size = grid_column.min_track_sizing_function.length().to_px(box);
  387. break;
  388. case CSS::GridTrackSize::Type::Percentage:
  389. grid_column.base_size = grid_column.min_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  390. break;
  391. // - An intrinsic sizing function
  392. // Use an initial base size of zero.
  393. case CSS::GridTrackSize::Type::FlexibleLength:
  394. break;
  395. default:
  396. VERIFY_NOT_REACHED();
  397. }
  398. // For each track, if the track’s max track sizing function is:
  399. switch (grid_column.max_track_sizing_function.type()) {
  400. // - A fixed sizing function
  401. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  402. case CSS::GridTrackSize::Type::Length:
  403. if (!grid_column.max_track_sizing_function.length().is_auto())
  404. grid_column.growth_limit = grid_column.max_track_sizing_function.length().to_px(box);
  405. else
  406. // - An intrinsic sizing function
  407. // Use an initial growth limit of infinity.
  408. grid_column.growth_limit = -1;
  409. break;
  410. case CSS::GridTrackSize::Type::Percentage:
  411. grid_column.growth_limit = grid_column.max_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  412. break;
  413. // - A flexible sizing function
  414. // Use an initial growth limit of infinity.
  415. case CSS::GridTrackSize::Type::FlexibleLength:
  416. grid_column.growth_limit = -1;
  417. break;
  418. default:
  419. VERIFY_NOT_REACHED();
  420. }
  421. }
  422. // Initialize each track’s base size and growth limit.
  423. for (auto& grid_row : grid_rows) {
  424. // For each track, if the track’s min track sizing function is:
  425. switch (grid_row.min_track_sizing_function.type()) {
  426. // - A fixed sizing function
  427. // Resolve to an absolute length and use that size as the track’s initial base size.
  428. // Indefinite lengths cannot occur, as they’re treated as auto.
  429. case CSS::GridTrackSize::Type::Length:
  430. if (!grid_row.min_track_sizing_function.length().is_auto())
  431. grid_row.base_size = grid_row.min_track_sizing_function.length().to_px(box);
  432. break;
  433. case CSS::GridTrackSize::Type::Percentage:
  434. grid_row.base_size = grid_row.min_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  435. break;
  436. // - An intrinsic sizing function
  437. // Use an initial base size of zero.
  438. case CSS::GridTrackSize::Type::FlexibleLength:
  439. break;
  440. default:
  441. VERIFY_NOT_REACHED();
  442. }
  443. // For each track, if the track’s max track sizing function is:
  444. switch (grid_row.max_track_sizing_function.type()) {
  445. // - A fixed sizing function
  446. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  447. case CSS::GridTrackSize::Type::Length:
  448. if (!grid_row.max_track_sizing_function.length().is_auto())
  449. grid_row.growth_limit = grid_row.max_track_sizing_function.length().to_px(box);
  450. else
  451. // - An intrinsic sizing function
  452. // Use an initial growth limit of infinity.
  453. grid_row.growth_limit = -1;
  454. break;
  455. case CSS::GridTrackSize::Type::Percentage:
  456. grid_row.growth_limit = grid_row.max_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  457. break;
  458. // - A flexible sizing function
  459. // Use an initial growth limit of infinity.
  460. case CSS::GridTrackSize::Type::FlexibleLength:
  461. grid_row.growth_limit = -1;
  462. break;
  463. default:
  464. VERIFY_NOT_REACHED();
  465. }
  466. }
  467. // FIXME: In all cases, if the growth limit is less than the base size, increase the growth limit to match
  468. // the base size.
  469. // https://drafts.csswg.org/css-grid/#algo-content
  470. // 12.5. Resolve Intrinsic Track Sizes
  471. // This step resolves intrinsic track sizing functions to absolute lengths. First it resolves those
  472. // sizes based on items that are contained wholly within a single track. Then it gradually adds in
  473. // the space requirements of items that span multiple tracks, evenly distributing the extra space
  474. // across those tracks insofar as possible.
  475. // FIXME: 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline
  476. // alignment. For the items in each baseline-sharing group, add a “shim” (effectively, additional
  477. // margin) on the start/end side (for first/last-baseline alignment) of each item so that, when
  478. // start/end-aligned together their baselines align as specified.
  479. // Consider these “shims” as part of the items’ intrinsic size contribution for the purpose of track
  480. // sizing, below. If an item uses multiple intrinsic size contributions, it can have different shims
  481. // for each one.
  482. // 2. Size tracks to fit non-spanning items: For each track with an intrinsic track sizing function and
  483. // not a flexible sizing function, consider the items in it with a span of 1:
  484. int index = 0;
  485. for (auto& grid_column : grid_columns) {
  486. if (!grid_column.min_track_sizing_function.is_intrinsic_track_sizing()) {
  487. ++index;
  488. continue;
  489. }
  490. Vector<Box const&> boxes_of_column;
  491. for (auto& positioned_box : positioned_boxes) {
  492. if (positioned_box.column == index && positioned_box.column_span == 1)
  493. boxes_of_column.append(positioned_box.box);
  494. }
  495. // - For min-content minimums:
  496. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  497. // items’ min-content contributions, floored at zero.
  498. // FIXME: Not implemented yet min-content.
  499. // - For max-content minimums:
  500. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  501. // items’ max-content contributions, floored at zero.
  502. // FIXME: Not implemented yet max-content.
  503. // - For auto minimums:
  504. // If the track has an auto min track sizing function and the grid container is being sized under a
  505. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  506. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  507. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  508. // limited by the max track sizing function (which could be the argument to a fit-content() track
  509. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  510. // below).
  511. // FIXME: Not implemented yet min-/max-content.
  512. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  513. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  514. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  515. // containing block in the relevant axis, its minimum contribution is the outer size that would
  516. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  517. // contribution is its min-content contribution. Because the minimum contribution often depends on
  518. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  519. // For items with a specified minimum size of auto (the initial value), the minimum contribution is
  520. // usually equivalent to the min-content contribution—but can differ in some cases, see § 6.6
  521. // Automatic Minimum Size of Grid Items. Also, minimum contribution ≤ min-content contribution ≤
  522. // max-content contribution.
  523. float grid_column_width = 0;
  524. for (auto& box_of_column : boxes_of_column)
  525. grid_column_width = max(grid_column_width, calculate_min_content_width(box_of_column));
  526. grid_column.base_size = grid_column_width;
  527. // - For min-content maximums:
  528. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  529. // the items’ min-content contributions.
  530. // FIXME: Not implemented yet min-content maximums.
  531. // - For max-content maximums:
  532. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  533. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  534. // limit by the fit-content() argument.
  535. // FIXME: Not implemented yet max-content maximums.
  536. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  537. // to match the base size.
  538. if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
  539. grid_column.growth_limit = grid_column.base_size;
  540. ++index;
  541. }
  542. index = 0;
  543. for (auto& grid_row : grid_rows) {
  544. if (!grid_row.min_track_sizing_function.is_intrinsic_track_sizing()) {
  545. ++index;
  546. continue;
  547. }
  548. Vector<PositionedBox&> positioned_boxes_of_row;
  549. for (auto& positioned_box : positioned_boxes) {
  550. if (positioned_box.row == index && positioned_box.row_span == 1)
  551. positioned_boxes_of_row.append(positioned_box);
  552. }
  553. // - For min-content minimums:
  554. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  555. // items’ min-content contributions, floored at zero.
  556. // FIXME: Not implemented yet min-content.
  557. // - For max-content minimums:
  558. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  559. // items’ max-content contributions, floored at zero.
  560. // FIXME: Not implemented yet max-content.
  561. // - For auto minimums:
  562. // If the track has an auto min track sizing function and the grid container is being sized under a
  563. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  564. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  565. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  566. // limited by the max track sizing function (which could be the argument to a fit-content() track
  567. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  568. // below).
  569. // FIXME: Not implemented yet min-/max-content.
  570. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  571. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  572. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  573. // containing block in the relevant axis, its minimum contribution is the outer size that would
  574. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  575. // contribution is its min-content contribution. Because the minimum contribution often depends on
  576. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  577. // For items with a specified minimum size of auto (the initial value), the minimum contribution is
  578. // usually equivalent to the min-content contribution—but can differ in some cases, see § 6.6
  579. // Automatic Minimum Size of Grid Items. Also, minimum contribution ≤ min-content contribution ≤
  580. // max-content contribution.
  581. float grid_row_height = 0;
  582. for (auto& positioned_box : positioned_boxes_of_row)
  583. grid_row_height = max(grid_row_height, positioned_box.computed_height);
  584. grid_row.base_size = grid_row_height;
  585. // - For min-content maximums:
  586. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  587. // the items’ min-content contributions.
  588. // FIXME: Not implemented yet min-content maximums.
  589. // - For max-content maximums:
  590. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  591. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  592. // limit by the fit-content() argument.
  593. // FIXME: Not implemented yet max-content maximums.
  594. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  595. // to match the base size.
  596. if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
  597. grid_row.growth_limit = grid_row.base_size;
  598. ++index;
  599. }
  600. // 3. Increase sizes to accommodate spanning items crossing content-sized tracks: Next, consider the
  601. // items with a span of 2 that do not span a track with a flexible sizing function.
  602. // FIXME: Content-sized tracks not implemented (min-content, etc.)
  603. // 3.1. For intrinsic minimums: First distribute extra space to base sizes of tracks with an intrinsic
  604. // min track sizing function, to accommodate these items’ minimum contributions.
  605. // If the grid container is being sized under a min- or max-content constraint, use the items’
  606. // limited min-content contributions in place of their minimum contributions here. (For an item
  607. // spanning multiple tracks, the upper limit used to calculate its limited min-/max-content
  608. // contribution is the sum of the fixed max track sizing functions of any tracks it spans, and is
  609. // applied if it only spans such tracks.)
  610. // 3.2. For content-based minimums: Next continue to distribute extra space to the base sizes of tracks
  611. // with a min track sizing function of min-content or max-content, to accommodate these items'
  612. // min-content contributions.
  613. // 3.3. For max-content minimums: Next, if the grid container is being sized under a max-content
  614. // constraint, continue to distribute extra space to the base sizes of tracks with a min track
  615. // sizing function of auto or max-content, to accommodate these items' limited max-content
  616. // contributions.
  617. // In all cases, continue to distribute extra space to the base sizes of tracks with a min track
  618. // sizing function of max-content, to accommodate these items' max-content contributions.
  619. // 3.4. If at this point any track’s growth limit is now less than its base size, increase its growth
  620. // limit to match its base size.
  621. // 3.5. For intrinsic maximums: Next distribute extra space to the growth limits of tracks with intrinsic
  622. // max track sizing function, to accommodate these items' min-content contributions. Mark any tracks
  623. // whose growth limit changed from infinite to finite in this step as infinitely growable for the
  624. // next step.
  625. // 3.6. For max-content maximums: Lastly continue to distribute extra space to the growth limits of
  626. // tracks with a max track sizing function of max-content, to accommodate these items' max-content
  627. // contributions. However, limit the growth of any fit-content() tracks by their fit-content()
  628. // argument.
  629. // Repeat incrementally for items with greater spans until all items have been considered.
  630. // FIXME: 4. Increase sizes to accommodate spanning items crossing flexible tracks: Next, repeat the previous
  631. // step instead considering (together, rather than grouped by span size) all items that do span a
  632. // track with a flexible sizing function while distributing space only to flexible tracks (i.e.
  633. // treating all other tracks as having a fixed sizing function)
  634. // if the sum of the flexible sizing functions of all flexible tracks spanned by the item is greater
  635. // than or equal to one, distributing space to such tracks according to the ratios of their flexible
  636. // sizing functions rather than distributing space equally; and if the sum is less than one,
  637. // distributing that proportion of space according to the ratios of their flexible sizing functions
  638. // and the rest equally
  639. // FIXME: 5. If any track still has an infinite growth limit (because, for example, it had no items placed in
  640. // it or it is a flexible track), set its growth limit to its base size.
  641. // https://drafts.csswg.org/css-grid/#extra-space
  642. // 12.5.1. Distributing Extra Space Across Spanned Tracks
  643. // 1. Maintain separately for each affected track a planned increase, initially set to 0. (This
  644. // prevents the size increases from becoming order-dependent.)
  645. // 2. For each accommodated item, considering only tracks the item spans:
  646. // 2.1. Find the space to distribute: Subtract the affected size of every spanned track (not just the
  647. // affected tracks) from the item’s size contribution, flooring it at zero. (For infinite growth
  648. // limits, substitute the track’s base size.) This remaining size contribution is the space to
  649. // distribute.
  650. // space = max(0, size contribution - ∑track-sizes)
  651. // 2.2. Distribute space up to limits:
  652. // Find the item-incurred increase for each affected track by: distributing the space equally among
  653. // these tracks, freezing a track’s item-incurred increase as its affected size + item-incurred
  654. // increase reaches its limit (and continuing to grow the unfrozen tracks as needed).
  655. // For base sizes, the limit is its growth limit. For growth limits, the limit is infinity if it is
  656. // marked as infinitely growable, and equal to the growth limit otherwise.
  657. // If the affected size was a growth limit and the track is not marked infinitely growable, then each
  658. // item-incurred increase will be zero.
  659. // 2.3. Distribute space beyond limits:
  660. // If extra space remains at this point, unfreeze and continue to distribute space to the
  661. // item-incurred increase of…
  662. // - when accommodating minimum contributions or accommodating min-content contributions: any affected
  663. // track that happens to also have an intrinsic max track sizing function; if there are no such
  664. // tracks, then all affected tracks.
  665. // - when accommodating max-content contributions: any affected track that happens to also have a
  666. // max-content max track sizing function; if there are no such tracks, then all affected tracks.
  667. // - when handling any intrinsic growth limit: all affected tracks.
  668. // For this purpose, the max track sizing function of a fit-content() track is treated as
  669. // max-content until it reaches the limit specified as the fit-content() argument, after which it is
  670. // treated as having a fixed sizing function of that argument.
  671. // This step prioritizes the distribution of space for accommodating size contributions beyond the
  672. // tracks' current growth limits based on the types of their max track sizing functions.
  673. // 2.4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned
  674. // increase set the track’s planned increase to that value.
  675. // 3. Update the tracks' affected sizes by adding in the planned increase, so that the next round of
  676. // space distribution will account for the increase. (If the affected size is an infinite growth
  677. // limit, set it to the track’s base size plus the planned increase.)
  678. // https://drafts.csswg.org/css-grid/#algo-grow-tracks
  679. // 12.6. Maximize Tracks
  680. // If the free space is positive, distribute it equally to the base sizes of all tracks, freezing
  681. // tracks as they reach their growth limits (and continuing to grow the unfrozen tracks as needed).
  682. // For the purpose of this step: if sizing the grid container under a max-content constraint, the
  683. // free space is infinite; if sizing under a min-content constraint, the free space is zero.
  684. // If this would cause the grid to be larger than the grid container’s inner size as limited by its
  685. // max-width/height, then redo this step, treating the available grid space as equal to the grid
  686. // container’s inner size when it’s sized to its max-width/height.
  687. // FIXME: Do later as at the moment all growth limits are equal to base sizes.
  688. // https://drafts.csswg.org/css-grid/#algo-flex-tracks
  689. // 12.7. Expand Flexible Tracks
  690. // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding
  691. // the available space.
  692. // First, find the grid’s used flex fraction:
  693. auto column_flex_factor_sum = 0;
  694. for (auto& grid_column : grid_columns) {
  695. if (grid_column.min_track_sizing_function.is_flexible_length())
  696. column_flex_factor_sum++;
  697. }
  698. // See 12.7.1.
  699. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  700. // than 1, set it to 1 instead.
  701. if (column_flex_factor_sum < 1)
  702. column_flex_factor_sum = 1;
  703. // See 12.7.1.
  704. float sized_column_widths = 0;
  705. for (auto& grid_column : grid_columns) {
  706. if (!grid_column.min_track_sizing_function.is_flexible_length())
  707. sized_column_widths += grid_column.base_size;
  708. }
  709. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  710. double free_horizontal_space = box_state.content_width() - sized_column_widths;
  711. // If the free space is zero or if sizing the grid container under a min-content constraint:
  712. // The used flex fraction is zero.
  713. // FIXME: Add min-content constraint check.
  714. // Otherwise, if the free space is a definite length:
  715. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  716. // and a space to fill of the available grid space.
  717. if (free_horizontal_space > 0) {
  718. for (auto& grid_column : grid_columns) {
  719. if (grid_column.min_track_sizing_function.is_flexible_length()) {
  720. // See 12.7.1.
  721. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  722. auto hypothetical_fr_size = static_cast<double>(1.0 / column_flex_factor_sum) * free_horizontal_space;
  723. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  724. // greater than the track’s base size, set its base size to that product.
  725. grid_column.base_size = max(grid_column.base_size, hypothetical_fr_size);
  726. }
  727. }
  728. }
  729. // First, find the grid’s used flex fraction:
  730. auto row_flex_factor_sum = 0;
  731. for (auto& grid_row : grid_rows) {
  732. if (grid_row.min_track_sizing_function.is_flexible_length())
  733. row_flex_factor_sum++;
  734. }
  735. // See 12.7.1.
  736. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  737. // than 1, set it to 1 instead.
  738. if (row_flex_factor_sum < 1)
  739. row_flex_factor_sum = 1;
  740. // See 12.7.1.
  741. float sized_row_heights = 0;
  742. for (auto& grid_row : grid_rows) {
  743. if (!grid_row.min_track_sizing_function.is_flexible_length())
  744. sized_row_heights += grid_row.base_size;
  745. }
  746. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  747. double free_vertical_space = box_state.content_height() - sized_row_heights;
  748. // If the free space is zero or if sizing the grid container under a min-content constraint:
  749. // The used flex fraction is zero.
  750. // FIXME: Add min-content constraint check.
  751. // Otherwise, if the free space is a definite length:
  752. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  753. // and a space to fill of the available grid space.
  754. if (free_vertical_space > 0) {
  755. for (auto& grid_row : grid_rows) {
  756. if (grid_row.min_track_sizing_function.is_flexible_length()) {
  757. // See 12.7.1.
  758. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  759. auto hypothetical_fr_size = static_cast<double>(1.0 / row_flex_factor_sum) * free_vertical_space;
  760. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  761. // greater than the track’s base size, set its base size to that product.
  762. grid_row.base_size = max(grid_row.base_size, hypothetical_fr_size);
  763. }
  764. }
  765. }
  766. // Otherwise, if the free space is an indefinite length:
  767. // FIXME: No tracks will have indefinite length as per current implementation.
  768. // The used flex fraction is the maximum of:
  769. // For each flexible track, if the flexible track’s flex factor is greater than one, the result of
  770. // dividing the track’s base size by its flex factor; otherwise, the track’s base size.
  771. // For each grid item that crosses a flexible track, the result of finding the size of an fr using
  772. // all the grid tracks that the item crosses and a space to fill of the item’s max-content
  773. // contribution.
  774. // If using this flex fraction would cause the grid to be smaller than the grid container’s
  775. // min-width/height (or larger than the grid container’s max-width/height), then redo this step,
  776. // treating the free space as definite and the available grid space as equal to the grid container’s
  777. // inner size when it’s sized to its min-width/height (max-width/height).
  778. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  779. // greater than the track’s base size, set its base size to that product.
  780. // https://drafts.csswg.org/css-grid/#algo-find-fr-size
  781. // 12.7.1. Find the Size of an fr
  782. // This algorithm finds the largest size that an fr unit can be without exceeding the target size.
  783. // It must be called with a set of grid tracks and some quantity of space to fill.
  784. // 1. Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  785. // 2. Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  786. // than 1, set it to 1 instead.
  787. // 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  788. // FIXME: 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the
  789. // track’s base size, restart this algorithm treating all such tracks as inflexible.
  790. // 5. Return the hypothetical fr size.
  791. // https://drafts.csswg.org/css-grid/#algo-stretch
  792. // 12.8. Stretch auto Tracks
  793. // When the content-distribution property of the grid container is normal or stretch in this axis,
  794. // this step expands tracks that have an auto max track sizing function by dividing any remaining
  795. // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
  796. // container has a definite min-width/height, use that size to calculate the free space for this
  797. // step instead.
  798. float used_horizontal_space = 0;
  799. for (auto& grid_column : grid_columns) {
  800. if (!(grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto()))
  801. used_horizontal_space += grid_column.base_size;
  802. }
  803. float remaining_horizontal_space = box_state.content_width() - used_horizontal_space;
  804. auto count_of_auto_max_column_tracks = 0;
  805. for (auto& grid_column : grid_columns) {
  806. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  807. count_of_auto_max_column_tracks++;
  808. }
  809. for (auto& grid_column : grid_columns) {
  810. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  811. grid_column.base_size = max(grid_column.base_size, remaining_horizontal_space / count_of_auto_max_column_tracks);
  812. }
  813. float used_vertical_space = 0;
  814. for (auto& grid_row : grid_rows) {
  815. if (!(grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto()))
  816. used_vertical_space += grid_row.base_size;
  817. }
  818. float remaining_vertical_space = box_state.content_height() - used_vertical_space;
  819. auto count_of_auto_max_row_tracks = 0;
  820. for (auto& grid_row : grid_rows) {
  821. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  822. count_of_auto_max_row_tracks++;
  823. }
  824. for (auto& grid_row : grid_rows) {
  825. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  826. grid_row.base_size = max(grid_row.base_size, remaining_vertical_space / count_of_auto_max_row_tracks);
  827. }
  828. // Do layout
  829. auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void {
  830. auto& child_box_state = m_state.get_mutable(child_box);
  831. float x_start = 0;
  832. float x_end = 0;
  833. float y_start = 0;
  834. float y_end = 0;
  835. for (int i = 0; i < column_start; i++)
  836. x_start += grid_columns[i].base_size;
  837. for (int i = 0; i < column_end; i++)
  838. x_end += grid_columns[i].base_size;
  839. for (int i = 0; i < row_start; i++)
  840. y_start += grid_rows[i].base_size;
  841. for (int i = 0; i < row_end; i++)
  842. y_end += grid_rows[i].base_size;
  843. child_box_state.set_content_width(x_end - x_start);
  844. child_box_state.set_content_height(y_end - y_start);
  845. child_box_state.offset = { x_start, y_start };
  846. };
  847. for (auto& positioned_box : positioned_boxes)
  848. layout_box(positioned_box.row, positioned_box.row + positioned_box.row_span, positioned_box.column, positioned_box.column + positioned_box.column_span, positioned_box.box);
  849. float total_y = 0;
  850. for (auto& grid_row : grid_rows)
  851. total_y += grid_row.base_size;
  852. m_automatic_content_height = total_y;
  853. }
  854. float GridFormattingContext::automatic_content_height() const
  855. {
  856. return m_automatic_content_height;
  857. }
  858. }