GridFormattingContext.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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, AvailableSpace const& available_space)
  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. // https://drafts.csswg.org/css-grid/#overview-placement
  33. // 2.2. Placing Items
  34. // The contents of the grid container are organized into individual grid items (analogous to
  35. // flex items), which are then assigned to predefined areas in the grid. They can be explicitly
  36. // placed using coordinates through the grid-placement properties or implicitly placed into
  37. // empty areas using auto-placement.
  38. struct PositionedBox {
  39. Box const& box;
  40. int row { 0 };
  41. int row_span { 1 };
  42. int column { 0 };
  43. int column_span { 1 };
  44. float computed_height { 0 };
  45. };
  46. Vector<PositionedBox> positioned_boxes;
  47. Vector<Box const&> boxes_to_place;
  48. box.for_each_child_of_type<Box>([&](Box& child_box) {
  49. if (should_skip_is_anonymous_text_run(child_box))
  50. return IterationDecision::Continue;
  51. boxes_to_place.append(child_box);
  52. return IterationDecision::Continue;
  53. });
  54. auto column_repeat_count = box.computed_values().grid_template_columns().is_repeat() ? box.computed_values().grid_template_columns().repeat_count() : 1;
  55. auto row_repeat_count = box.computed_values().grid_template_rows().is_repeat() ? box.computed_values().grid_template_rows().repeat_count() : 1;
  56. auto occupation_grid = OccupationGrid(column_repeat_count * box.computed_values().grid_template_columns().meta_grid_track_sizes().size(), row_repeat_count * box.computed_values().grid_template_rows().meta_grid_track_sizes().size());
  57. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  58. // 8.5. Grid Item Placement Algorithm
  59. // FIXME: 0. Generate anonymous grid items
  60. // 1. Position anything that's not auto-positioned.
  61. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  62. auto const& child_box = boxes_to_place[i];
  63. if (is_auto_positioned_row(child_box.computed_values().grid_row_start(), child_box.computed_values().grid_row_end())
  64. || is_auto_positioned_column(child_box.computed_values().grid_column_start(), child_box.computed_values().grid_column_end()))
  65. continue;
  66. int row_start = child_box.computed_values().grid_row_start().raw_value();
  67. int row_end = child_box.computed_values().grid_row_end().raw_value();
  68. int column_start = child_box.computed_values().grid_column_start().raw_value();
  69. int column_end = child_box.computed_values().grid_column_end().raw_value();
  70. // https://drafts.csswg.org/css-grid/#line-placement
  71. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  72. // https://drafts.csswg.org/css-grid/#grid-placement-slot
  73. // FIXME: <custom-ident>
  74. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  75. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  76. // contributes the first such line to the grid item’s placement.
  77. // Note: Named grid areas automatically generate implicitly-assigned line names of this form, so
  78. // specifying grid-row-start: foo will choose the start edge of that named grid area (unless another
  79. // line named foo-start was explicitly specified before it).
  80. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  81. // https://drafts.csswg.org/css-grid/#grid-placement-int
  82. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  83. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  84. // instead counts in reverse, starting from the end edge of the explicit grid.
  85. if (row_end < 0)
  86. row_end = occupation_grid.row_count() + row_end + 2;
  87. if (column_end < 0)
  88. column_end = occupation_grid.column_count() + column_end + 2;
  89. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  90. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  91. // of finding this position.
  92. // An <integer> value of zero makes the declaration invalid.
  93. // https://drafts.csswg.org/css-grid/#grid-placement-span-int
  94. // span && [ <integer [1,∞]> || <custom-ident> ]
  95. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  96. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  97. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  98. // grid-column-start line.
  99. int row_span = 1;
  100. int column_span = 1;
  101. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_span())
  102. row_span = child_box.computed_values().grid_row_end().raw_value();
  103. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_span())
  104. column_span = child_box.computed_values().grid_column_end().raw_value();
  105. if (child_box.computed_values().grid_row_end().is_position() && child_box.computed_values().grid_row_start().is_span()) {
  106. row_span = child_box.computed_values().grid_row_start().raw_value();
  107. row_start = row_end - row_span;
  108. }
  109. if (child_box.computed_values().grid_column_end().is_position() && child_box.computed_values().grid_column_start().is_span()) {
  110. column_span = child_box.computed_values().grid_column_start().raw_value();
  111. column_start = column_end - column_span;
  112. }
  113. // 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 on the side of the explicit grid
  115. // corresponding to the search direction are assumed to have that name for the purpose of counting
  116. // this span.
  117. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  118. // auto
  119. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  120. // default span of one. (See § 8 Placing Grid Items, above.)
  121. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  122. // 8.3.1. Grid Placement Conflict Handling
  123. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  124. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  125. // line.
  126. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position()) {
  127. if (row_start > row_end)
  128. swap(row_start, row_end);
  129. if (row_start != row_end)
  130. row_span = row_end - row_start;
  131. }
  132. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position()) {
  133. if (column_start > column_end)
  134. swap(column_start, column_end);
  135. if (column_start != column_end)
  136. column_span = column_end - column_start;
  137. }
  138. // If the placement contains two spans, remove the one contributed by the end grid-placement
  139. // property.
  140. if (child_box.computed_values().grid_row_start().is_span() && child_box.computed_values().grid_row_end().is_span())
  141. row_span = child_box.computed_values().grid_row_start().raw_value();
  142. if (child_box.computed_values().grid_column_start().is_span() && child_box.computed_values().grid_column_end().is_span())
  143. column_span = child_box.computed_values().grid_column_start().raw_value();
  144. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  145. row_start -= 1;
  146. column_start -= 1;
  147. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  148. occupation_grid.maybe_add_row(row_start + row_span);
  149. occupation_grid.maybe_add_column(column_start + column_span);
  150. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  151. boxes_to_place.remove(i);
  152. i--;
  153. }
  154. // 2. Process the items locked to a given row.
  155. // FIXME: Do "dense" packing
  156. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  157. auto const& child_box = boxes_to_place[i];
  158. if (is_auto_positioned_row(child_box.computed_values().grid_row_start(), child_box.computed_values().grid_row_end()))
  159. continue;
  160. int row_start = child_box.computed_values().grid_row_start().raw_value();
  161. int row_end = child_box.computed_values().grid_row_end().raw_value();
  162. // https://drafts.csswg.org/css-grid/#line-placement
  163. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  164. // https://drafts.csswg.org/css-grid/#grid-placement-slot
  165. // FIXME: <custom-ident>
  166. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  167. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  168. // contributes the first such line to the grid item’s placement.
  169. // Note: Named grid areas automatically generate implicitly-assigned line names of this form, so
  170. // specifying grid-row-start: foo will choose the start edge of that named grid area (unless another
  171. // line named foo-start was explicitly specified before it).
  172. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  173. // https://drafts.csswg.org/css-grid/#grid-placement-int
  174. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  175. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  176. // instead counts in reverse, starting from the end edge of the explicit grid.
  177. if (row_end < 0)
  178. row_end = occupation_grid.row_count() + row_end + 2;
  179. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  180. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  181. // of finding this position.
  182. // An <integer> value of zero makes the declaration invalid.
  183. // https://drafts.csswg.org/css-grid/#grid-placement-span-int
  184. // span && [ <integer [1,∞]> || <custom-ident> ]
  185. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  186. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  187. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  188. // grid-column-start line.
  189. int row_span = 1;
  190. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_span())
  191. row_span = child_box.computed_values().grid_row_end().raw_value();
  192. if (child_box.computed_values().grid_row_end().is_position() && child_box.computed_values().grid_row_start().is_span()) {
  193. row_span = child_box.computed_values().grid_row_start().raw_value();
  194. row_start = row_end - row_span;
  195. // FIXME: Remove me once have implemented spans overflowing into negative indexes, e.g., grid-row: span 2 / 1
  196. if (row_start < 0)
  197. row_start = 1;
  198. }
  199. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  200. // lines with that name exist, all implicit grid lines on the side of the explicit grid
  201. // corresponding to the search direction are assumed to have that name for the purpose of counting
  202. // this span.
  203. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  204. // auto
  205. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  206. // default span of one. (See § 8 Placing Grid Items, above.)
  207. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  208. // 8.3.1. Grid Placement Conflict Handling
  209. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  210. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  211. // line.
  212. if (child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position()) {
  213. if (row_start > row_end)
  214. swap(row_start, row_end);
  215. if (row_start != row_end)
  216. row_span = row_end - row_start;
  217. }
  218. // FIXME: Have yet to find the spec for this.
  219. if (!child_box.computed_values().grid_row_start().is_position() && child_box.computed_values().grid_row_end().is_position() && row_end == 1)
  220. row_start = 1;
  221. // If the placement contains two spans, remove the one contributed by the end grid-placement
  222. // property.
  223. if (child_box.computed_values().grid_row_start().is_span() && child_box.computed_values().grid_row_end().is_span())
  224. row_span = child_box.computed_values().grid_row_start().raw_value();
  225. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  226. row_start -= 1;
  227. occupation_grid.maybe_add_row(row_start + row_span);
  228. int column_start = 0;
  229. auto column_span = child_box.computed_values().grid_column_start().is_span() ? child_box.computed_values().grid_column_start().raw_value() : 1;
  230. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  231. // 8.5. Grid Item Placement Algorithm
  232. // 3.3. If the largest column span among all the items without a definite column position is larger
  233. // than the width of the implicit grid, add columns to the end of the implicit grid to accommodate
  234. // that column span.
  235. occupation_grid.maybe_add_column(column_span);
  236. bool found_available_column = false;
  237. for (int column_index = column_start; column_index < occupation_grid.column_count(); column_index++) {
  238. if (!occupation_grid.is_occupied(column_index, row_start)) {
  239. found_available_column = true;
  240. column_start = column_index;
  241. break;
  242. }
  243. }
  244. if (!found_available_column) {
  245. column_start = occupation_grid.column_count();
  246. occupation_grid.maybe_add_column(column_start + column_span);
  247. }
  248. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  249. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  250. boxes_to_place.remove(i);
  251. i--;
  252. }
  253. // 3. Determine the columns in the implicit grid.
  254. // NOTE: "implicit grid" here is the same as the occupation_grid
  255. // 3.1. Start with the columns from the explicit grid.
  256. // NOTE: Done in step 1.
  257. // 3.2. Among all the items with a definite column position (explicitly positioned items, items
  258. // positioned in the previous step, and items not yet positioned but with a definite column) add
  259. // columns to the beginning and end of the implicit grid as necessary to accommodate those items.
  260. // NOTE: "Explicitly positioned items" and "items positioned in the previous step" done in step 1
  261. // and 2, respectively. Adding columns for "items not yet positioned but with a definite column"
  262. // will be done in step 4.
  263. // 4. Position the remaining grid items.
  264. // For each grid item that hasn't been positioned by the previous steps, in order-modified document
  265. // order:
  266. auto auto_placement_cursor_x = 0;
  267. auto auto_placement_cursor_y = 0;
  268. for (size_t i = 0; i < boxes_to_place.size(); i++) {
  269. auto const& child_box = boxes_to_place[i];
  270. // 4.1. For sparse packing:
  271. // FIXME: no distinction made. See #4.2
  272. // 4.1.1. If the item has a definite column position:
  273. if (!is_auto_positioned_column(child_box.computed_values().grid_column_start(), child_box.computed_values().grid_column_end())) {
  274. int column_start = child_box.computed_values().grid_column_start().raw_value();
  275. int column_end = child_box.computed_values().grid_column_end().raw_value();
  276. // https://drafts.csswg.org/css-grid/#line-placement
  277. // 8.3. Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
  278. // https://drafts.csswg.org/css-grid/#grid-placement-slot
  279. // FIXME: <custom-ident>
  280. // First attempt to match the grid area’s edge to a named grid area: if there is a grid line whose
  281. // line name is <custom-ident>-start (for grid-*-start) / <custom-ident>-end (for grid-*-end),
  282. // contributes the first such line to the grid item’s placement.
  283. // Note: Named grid areas automatically generate implicitly-assigned line names of this form, so
  284. // specifying grid-row-start: foo will choose the start edge of that named grid area (unless another
  285. // line named foo-start was explicitly specified before it).
  286. // Otherwise, treat this as if the integer 1 had been specified along with the <custom-ident>.
  287. // https://drafts.csswg.org/css-grid/#grid-placement-int
  288. // [ <integer [−∞,−1]> | <integer [1,∞]> ] && <custom-ident>?
  289. // Contributes the Nth grid line to the grid item’s placement. If a negative integer is given, it
  290. // instead counts in reverse, starting from the end edge of the explicit grid.
  291. if (column_end < 0)
  292. column_end = occupation_grid.column_count() + column_end + 2;
  293. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  294. // lines with that name exist, all implicit grid lines are assumed to have that name for the purpose
  295. // of finding this position.
  296. // An <integer> value of zero makes the declaration invalid.
  297. // https://drafts.csswg.org/css-grid/#grid-placement-span-int
  298. // span && [ <integer [1,∞]> || <custom-ident> ]
  299. // Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid
  300. // item’s grid area is N lines from its opposite edge in the corresponding direction. For example,
  301. // grid-column-end: span 2 indicates the second grid line in the endward direction from the
  302. // grid-column-start line.
  303. int column_span = 1;
  304. auto row_span = child_box.computed_values().grid_row_start().is_span() ? child_box.computed_values().grid_row_start().raw_value() : 1;
  305. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_span())
  306. column_span = child_box.computed_values().grid_column_end().raw_value();
  307. if (child_box.computed_values().grid_column_end().is_position() && child_box.computed_values().grid_column_start().is_span()) {
  308. column_span = child_box.computed_values().grid_column_start().raw_value();
  309. column_start = column_end - column_span;
  310. // FIXME: Remove me once have implemented spans overflowing into negative indexes, e.g., grid-column: span 2 / 1
  311. if (column_start < 0)
  312. column_start = 1;
  313. }
  314. // FIXME: Have yet to find the spec for this.
  315. if (!child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position() && column_end == 1)
  316. column_start = 1;
  317. // If a name is given as a <custom-ident>, only lines with that name are counted. If not enough
  318. // lines with that name exist, all implicit grid lines on the side of the explicit grid
  319. // corresponding to the search direction are assumed to have that name for the purpose of counting
  320. // this span.
  321. // https://drafts.csswg.org/css-grid/#grid-placement-auto
  322. // auto
  323. // The property contributes nothing to the grid item’s placement, indicating auto-placement or a
  324. // default span of one. (See § 8 Placing Grid Items, above.)
  325. // https://drafts.csswg.org/css-grid/#grid-placement-errors
  326. // 8.3.1. Grid Placement Conflict Handling
  327. // If the placement for a grid item contains two lines, and the start line is further end-ward than
  328. // the end line, swap the two lines. If the start line is equal to the end line, remove the end
  329. // line.
  330. if (child_box.computed_values().grid_column_start().is_position() && child_box.computed_values().grid_column_end().is_position()) {
  331. if (column_start > column_end)
  332. swap(column_start, column_end);
  333. if (column_start != column_end)
  334. column_span = column_end - column_start;
  335. }
  336. // If the placement contains two spans, remove the one contributed by the end grid-placement
  337. // property.
  338. if (child_box.computed_values().grid_column_start().is_span() && child_box.computed_values().grid_column_end().is_span())
  339. column_span = child_box.computed_values().grid_column_start().raw_value();
  340. // FIXME: If the placement contains only a span for a named line, replace it with a span of 1.
  341. column_start -= 1;
  342. // 4.1.1.1. Set the column position of the cursor to the grid item's column-start line. If this is
  343. // less than the previous column position of the cursor, increment the row position by 1.
  344. if (column_start < auto_placement_cursor_x)
  345. auto_placement_cursor_y++;
  346. auto_placement_cursor_x = column_start;
  347. occupation_grid.maybe_add_column(auto_placement_cursor_x + column_span);
  348. occupation_grid.maybe_add_row(auto_placement_cursor_y + row_span);
  349. // 4.1.1.2. Increment the cursor's row position until a value is found where the grid item does not
  350. // overlap any occupied grid cells (creating new rows in the implicit grid as necessary).
  351. while (true) {
  352. if (!occupation_grid.is_occupied(column_start, auto_placement_cursor_y)) {
  353. break;
  354. }
  355. auto_placement_cursor_y++;
  356. occupation_grid.maybe_add_row(auto_placement_cursor_y + row_span);
  357. }
  358. // 4.1.1.3. Set the item's row-start line to the cursor's row position, and set the item's row-end
  359. // line according to its span from that position.
  360. occupation_grid.set_occupied(column_start, column_start + column_span, auto_placement_cursor_y, auto_placement_cursor_y + row_span);
  361. positioned_boxes.append({ child_box, auto_placement_cursor_y, row_span, column_start, column_span });
  362. }
  363. // 4.1.2. If the item has an automatic grid position in both axes:
  364. else {
  365. // 4.1.2.1. Increment the column position of the auto-placement cursor until either this item's grid
  366. // area does not overlap any occupied grid cells, or the cursor's column position, plus the item's
  367. // column span, overflow the number of columns in the implicit grid, as determined earlier in this
  368. // algorithm.
  369. auto column_start = 0;
  370. auto column_span = child_box.computed_values().grid_column_start().is_span() ? child_box.computed_values().grid_column_start().raw_value() : 1;
  371. // https://drafts.csswg.org/css-grid/#auto-placement-algo
  372. // 8.5. Grid Item Placement Algorithm
  373. // 3.3. If the largest column span among all the items without a definite column position is larger
  374. // than the width of the implicit grid, add columns to the end of the implicit grid to accommodate
  375. // that column span.
  376. occupation_grid.maybe_add_column(column_span);
  377. auto row_start = 0;
  378. auto row_span = child_box.computed_values().grid_row_start().is_span() ? child_box.computed_values().grid_row_start().raw_value() : 1;
  379. auto found_unoccupied_area = false;
  380. for (int row_index = auto_placement_cursor_y; row_index < occupation_grid.row_count(); row_index++) {
  381. for (int column_index = auto_placement_cursor_x; column_index < occupation_grid.column_count(); column_index++) {
  382. if (column_span + column_index <= occupation_grid.column_count()) {
  383. auto found_all_available = true;
  384. for (int span_index = 0; span_index < column_span; span_index++) {
  385. if (occupation_grid.is_occupied(column_index + span_index, row_index))
  386. found_all_available = false;
  387. }
  388. if (found_all_available) {
  389. found_unoccupied_area = true;
  390. column_start = column_index;
  391. row_start = row_index;
  392. goto finish;
  393. }
  394. }
  395. auto_placement_cursor_x = 0;
  396. }
  397. auto_placement_cursor_x = 0;
  398. auto_placement_cursor_y++;
  399. }
  400. finish:
  401. // 4.1.2.2. If a non-overlapping position was found in the previous step, set the item's row-start
  402. // and column-start lines to the cursor's position. Otherwise, increment the auto-placement cursor's
  403. // row position (creating new rows in the implicit grid as necessary), set its column position to the
  404. // start-most column line in the implicit grid, and return to the previous step.
  405. if (!found_unoccupied_area) {
  406. row_start = occupation_grid.row_count();
  407. occupation_grid.maybe_add_row(occupation_grid.row_count() + 1);
  408. }
  409. occupation_grid.set_occupied(column_start, column_start + column_span, row_start, row_start + row_span);
  410. positioned_boxes.append({ child_box, row_start, row_span, column_start, column_span });
  411. }
  412. boxes_to_place.remove(i);
  413. i--;
  414. // FIXME: 4.2. For dense packing:
  415. }
  416. auto& box_state = m_state.get_mutable(box);
  417. for (auto& positioned_box : positioned_boxes) {
  418. auto& child_box_state = m_state.get_mutable(positioned_box.box);
  419. if (child_box_state.content_height() > positioned_box.computed_height)
  420. positioned_box.computed_height = child_box_state.content_height();
  421. if (auto independent_formatting_context = layout_inside(positioned_box.box, LayoutMode::Normal, available_space))
  422. independent_formatting_context->parent_context_did_dimension_child_root_box();
  423. if (child_box_state.content_height() > positioned_box.computed_height)
  424. positioned_box.computed_height = child_box_state.content_height();
  425. }
  426. // https://drafts.csswg.org/css-grid/#overview-sizing
  427. // 2.3. Sizing the Grid
  428. // Once the grid items have been placed, the sizes of the grid tracks (rows and columns) are
  429. // calculated, accounting for the sizes of their contents and/or available space as specified in
  430. // the grid definition.
  431. // https://www.w3.org/TR/css-grid-2/#layout-algorithm
  432. // 12. Grid Sizing
  433. // This section defines the grid sizing algorithm, which determines the size of all grid tracks and,
  434. // by extension, the entire grid.
  435. // Each track has specified minimum and maximum sizing functions (which may be the same). Each
  436. // sizing function is either:
  437. // - A fixed sizing function (<length> or resolvable <percentage>).
  438. // - An intrinsic sizing function (min-content, max-content, auto, fit-content()).
  439. // - A flexible sizing function (<flex>).
  440. // The grid sizing algorithm defines how to resolve these sizing constraints into used track sizes.
  441. struct GridTrack {
  442. CSS::GridTrackSize min_track_sizing_function;
  443. CSS::GridTrackSize max_track_sizing_function;
  444. float base_size { 0 };
  445. float growth_limit { 0 };
  446. };
  447. Vector<GridTrack> grid_rows;
  448. Vector<GridTrack> grid_columns;
  449. for (int x = 0; x < column_repeat_count; ++x) {
  450. for (auto& meta_grid_track_size : box.computed_values().grid_template_columns().meta_grid_track_sizes())
  451. grid_columns.append({ meta_grid_track_size.min_grid_track_size(), meta_grid_track_size.max_grid_track_size() });
  452. }
  453. for (int x = 0; x < row_repeat_count; ++x) {
  454. for (auto& meta_grid_track_size : box.computed_values().grid_template_rows().meta_grid_track_sizes())
  455. grid_rows.append({ meta_grid_track_size.min_grid_track_size(), meta_grid_track_size.max_grid_track_size() });
  456. }
  457. for (int column_index = grid_columns.size(); column_index < occupation_grid.column_count(); column_index++)
  458. grid_columns.append({ CSS::GridTrackSize::make_auto(), CSS::GridTrackSize::make_auto() });
  459. for (int row_index = grid_rows.size(); row_index < occupation_grid.row_count(); row_index++)
  460. grid_rows.append({ CSS::GridTrackSize::make_auto(), CSS::GridTrackSize::make_auto() });
  461. // https://www.w3.org/TR/css-grid-2/#algo-overview
  462. // 12.1. Grid Sizing Algorithm
  463. // 1. First, the track sizing algorithm is used to resolve the sizes of the grid columns.
  464. // In this process, any grid item which is subgridded in the grid container’s inline axis is treated
  465. // as empty and its grid items (the grandchildren) are treated as direct children of the grid
  466. // container (their grandparent). This introspection is recursive.
  467. // Items which are subgridded only in the block axis, and whose grid container size in the inline
  468. // axis depends on the size of its contents are also introspected: since the size of the item in
  469. // this dimension can be dependent on the sizing of its subgridded tracks in the other, the size
  470. // contribution of any such item to this grid’s column sizing (see Resolve Intrinsic Track Sizes) is
  471. // taken under the provision of having determined its track sizing only up to the same point in the
  472. // Grid Sizing Algorithm as this itself. E.g. for the first pass through this step, the item will
  473. // have its tracks sized only through this first step; if a second pass of this step is triggered
  474. // then the item will have completed a first pass through steps 1-3 as well as the second pass of
  475. // this step prior to returning its size for consideration in this grid’s column sizing. Again, this
  476. // introspection is recursive.
  477. // If calculating the layout of a grid item in this step depends on the available space in the block
  478. // axis, assume the available space that it would have if any row with a definite max track sizing
  479. // function had that size and all other rows were infinite. If both the grid container and all
  480. // tracks have definite sizes, also apply align-content to find the final effective size of any gaps
  481. // spanned by such items; otherwise ignore the effects of track alignment in this estimation.
  482. // 2. Next, the track sizing algorithm resolves the sizes of the grid rows.
  483. // In this process, any grid item which is subgridded in the grid container’s block axis is treated
  484. // as empty and its grid items (the grandchildren) are treated as direct children of the grid
  485. // container (their grandparent). This introspection is recursive.
  486. // As with sizing columns, items which are subgridded only in the inline axis, and whose grid
  487. // container size in the block axis depends on the size of its contents are also introspected. (As
  488. // with sizing columns, the size contribution to this grid’s row sizing is taken under the provision
  489. // of having determined its track sizing only up to this corresponding point in the algorithm; and
  490. // again, this introspection is recursive.)
  491. // To find the inline-axis available space for any items whose block-axis size contributions require
  492. // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
  493. // size is definite, also apply justify-content to account for the effective column gap sizes.
  494. // 3. Then, if the min-content contribution of any grid item has changed based on the row sizes and
  495. // alignment calculated in step 2, re-resolve the sizes of the grid columns with the new min-content
  496. // and max-content contributions (once only).
  497. // To find the block-axis available space for any items whose inline-axis size contributions require
  498. // it, use the grid row sizes calculated in the previous step. If the grid container’s block size is
  499. // definite, also apply align-content to account for the effective row gap sizes
  500. // 4. Next, if the min-content contribution of any grid item has changed based on the column sizes and
  501. // alignment calculated in step 3, re-resolve the sizes of the grid rows with the new min-content
  502. // and max-content contributions (once only).
  503. // To find the inline-axis available space for any items whose block-axis size contributions require
  504. // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
  505. // size is definite, also apply justify-content to account for the effective column gap sizes.
  506. // 5. Finally, the grid container is sized using the resulting size of the grid as its content size,
  507. // and the tracks are aligned within the grid container according to the align-content and
  508. // justify-content properties.
  509. // Once the size of each grid area is thus established, the grid items are laid out into their
  510. // respective containing blocks. The grid area’s width and height are considered definite for this
  511. // purpose.
  512. // https://www.w3.org/TR/css-grid-2/#algo-track-sizing
  513. // 12.3. Track Sizing Algorithm
  514. // The remainder of this section is the track sizing algorithm, which calculates from the min and
  515. // max track sizing functions the used track size. Each track has a base size, a <length> which
  516. // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
  517. // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:
  518. // 1. Initialize Track Sizes
  519. // 2. Resolve Intrinsic Track Sizes
  520. // 3. Maximize Tracks
  521. // 4. Expand Flexible Tracks
  522. // 5. Expand Stretched auto Tracks
  523. // https://www.w3.org/TR/css-grid-2/#algo-init
  524. // 12.4. Initialize Track Sizes
  525. // Initialize each track’s base size and growth limit.
  526. for (auto& grid_column : grid_columns) {
  527. // For each track, if the track’s min track sizing function is:
  528. switch (grid_column.min_track_sizing_function.type()) {
  529. // - A fixed sizing function
  530. // Resolve to an absolute length and use that size as the track’s initial base size.
  531. case CSS::GridTrackSize::Type::Length:
  532. if (!grid_column.min_track_sizing_function.length().is_auto())
  533. grid_column.base_size = grid_column.min_track_sizing_function.length().to_px(box);
  534. break;
  535. case CSS::GridTrackSize::Type::Percentage:
  536. grid_column.base_size = grid_column.min_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  537. break;
  538. // - An intrinsic sizing function
  539. // Use an initial base size of zero.
  540. case CSS::GridTrackSize::Type::FlexibleLength:
  541. break;
  542. default:
  543. VERIFY_NOT_REACHED();
  544. }
  545. // For each track, if the track’s max track sizing function is:
  546. switch (grid_column.max_track_sizing_function.type()) {
  547. // - A fixed sizing function
  548. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  549. case CSS::GridTrackSize::Type::Length:
  550. if (!grid_column.max_track_sizing_function.length().is_auto())
  551. grid_column.growth_limit = grid_column.max_track_sizing_function.length().to_px(box);
  552. else
  553. // - An intrinsic sizing function
  554. // Use an initial growth limit of infinity.
  555. grid_column.growth_limit = -1;
  556. break;
  557. case CSS::GridTrackSize::Type::Percentage:
  558. grid_column.growth_limit = grid_column.max_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  559. break;
  560. // - A flexible sizing function
  561. // Use an initial growth limit of infinity.
  562. case CSS::GridTrackSize::Type::FlexibleLength:
  563. grid_column.growth_limit = -1;
  564. break;
  565. default:
  566. VERIFY_NOT_REACHED();
  567. }
  568. }
  569. // Initialize each track’s base size and growth limit.
  570. for (auto& grid_row : grid_rows) {
  571. // For each track, if the track’s min track sizing function is:
  572. switch (grid_row.min_track_sizing_function.type()) {
  573. // - A fixed sizing function
  574. // Resolve to an absolute length and use that size as the track’s initial base size.
  575. case CSS::GridTrackSize::Type::Length:
  576. if (!grid_row.min_track_sizing_function.length().is_auto())
  577. grid_row.base_size = grid_row.min_track_sizing_function.length().to_px(box);
  578. break;
  579. case CSS::GridTrackSize::Type::Percentage:
  580. grid_row.base_size = grid_row.min_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  581. break;
  582. // - An intrinsic sizing function
  583. // Use an initial base size of zero.
  584. case CSS::GridTrackSize::Type::FlexibleLength:
  585. break;
  586. default:
  587. VERIFY_NOT_REACHED();
  588. }
  589. // For each track, if the track’s max track sizing function is:
  590. switch (grid_row.max_track_sizing_function.type()) {
  591. // - A fixed sizing function
  592. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  593. case CSS::GridTrackSize::Type::Length:
  594. if (!grid_row.max_track_sizing_function.length().is_auto())
  595. grid_row.growth_limit = grid_row.max_track_sizing_function.length().to_px(box);
  596. else
  597. // - An intrinsic sizing function
  598. // Use an initial growth limit of infinity.
  599. grid_row.growth_limit = -1;
  600. break;
  601. case CSS::GridTrackSize::Type::Percentage:
  602. grid_row.growth_limit = grid_row.max_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  603. break;
  604. // - A flexible sizing function
  605. // Use an initial growth limit of infinity.
  606. case CSS::GridTrackSize::Type::FlexibleLength:
  607. grid_row.growth_limit = -1;
  608. break;
  609. default:
  610. VERIFY_NOT_REACHED();
  611. }
  612. }
  613. // FIXME: In all cases, if the growth limit is less than the base size, increase the growth limit to match
  614. // the base size.
  615. // https://www.w3.org/TR/css-grid-2/#algo-content
  616. // 12.5. Resolve Intrinsic Track Sizes
  617. // This step resolves intrinsic track sizing functions to absolute lengths. First it resolves those
  618. // sizes based on items that are contained wholly within a single track. Then it gradually adds in
  619. // the space requirements of items that span multiple tracks, evenly distributing the extra space
  620. // across those tracks insofar as possible.
  621. // FIXME: 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline
  622. // alignment. For the items in each baseline-sharing group, add a “shim” (effectively, additional
  623. // margin) on the start/end side (for first/last-baseline alignment) of each item so that, when
  624. // start/end-aligned together their baselines align as specified.
  625. // Consider these “shims” as part of the items’ intrinsic size contribution for the purpose of track
  626. // sizing, below. If an item uses multiple intrinsic size contributions, it can have different shims
  627. // for each one.
  628. // 2. Size tracks to fit non-spanning items: For each track with an intrinsic track sizing function and
  629. // not a flexible sizing function, consider the items in it with a span of 1:
  630. int index = 0;
  631. for (auto& grid_column : grid_columns) {
  632. if (!grid_column.min_track_sizing_function.is_intrinsic_track_sizing()) {
  633. ++index;
  634. continue;
  635. }
  636. Vector<Box const&> boxes_of_column;
  637. for (auto& positioned_box : positioned_boxes) {
  638. if (positioned_box.column == index && positioned_box.column_span == 1)
  639. boxes_of_column.append(positioned_box.box);
  640. }
  641. // - For min-content minimums:
  642. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  643. // items’ min-content contributions, floored at zero.
  644. // FIXME: Not implemented yet min-content.
  645. // - For max-content minimums:
  646. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  647. // items’ max-content contributions, floored at zero.
  648. // FIXME: Not implemented yet max-content.
  649. // - For auto minimums:
  650. // If the track has an auto min track sizing function and the grid container is being sized under a
  651. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  652. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  653. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  654. // limited by the max track sizing function (which could be the argument to a fit-content() track
  655. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  656. // below).
  657. // FIXME: Not implemented yet min-/max-content.
  658. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  659. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  660. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  661. // containing block in the relevant axis, its minimum contribution is the outer size that would
  662. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  663. // contribution is its min-content contribution. Because the minimum contribution often depends on
  664. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  665. float grid_column_width = 0;
  666. for (auto& box_of_column : boxes_of_column)
  667. grid_column_width = max(grid_column_width, calculate_min_content_width(box_of_column));
  668. grid_column.base_size = grid_column_width;
  669. // - For min-content maximums:
  670. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  671. // the items’ min-content contributions.
  672. // FIXME: Not implemented yet min-content maximums.
  673. // - For max-content maximums:
  674. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  675. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  676. // limit by the fit-content() argument.
  677. // FIXME: Not implemented yet max-content maximums.
  678. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  679. // to match the base size.
  680. if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
  681. grid_column.growth_limit = grid_column.base_size;
  682. ++index;
  683. }
  684. index = 0;
  685. for (auto& grid_row : grid_rows) {
  686. if (!grid_row.min_track_sizing_function.is_intrinsic_track_sizing()) {
  687. ++index;
  688. continue;
  689. }
  690. Vector<PositionedBox&> positioned_boxes_of_row;
  691. for (auto& positioned_box : positioned_boxes) {
  692. if (positioned_box.row == index && positioned_box.row_span == 1)
  693. positioned_boxes_of_row.append(positioned_box);
  694. }
  695. // - For min-content minimums:
  696. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  697. // items’ min-content contributions, floored at zero.
  698. // FIXME: Not implemented yet min-content.
  699. // - For max-content minimums:
  700. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  701. // items’ max-content contributions, floored at zero.
  702. // FIXME: Not implemented yet max-content.
  703. // - For auto minimums:
  704. // If the track has an auto min track sizing function and the grid container is being sized under a
  705. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  706. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  707. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  708. // limited by the max track sizing function (which could be the argument to a fit-content() track
  709. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  710. // below).
  711. // FIXME: Not implemented yet min-/max-content.
  712. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  713. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  714. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  715. // containing block in the relevant axis, its minimum contribution is the outer size that would
  716. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  717. // contribution is its min-content contribution. Because the minimum contribution often depends on
  718. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  719. float grid_row_height = 0;
  720. for (auto& positioned_box : positioned_boxes_of_row)
  721. grid_row_height = max(grid_row_height, positioned_box.computed_height);
  722. grid_row.base_size = grid_row_height;
  723. // - For min-content maximums:
  724. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  725. // the items’ min-content contributions.
  726. // FIXME: Not implemented yet min-content maximums.
  727. // - For max-content maximums:
  728. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  729. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  730. // limit by the fit-content() argument.
  731. // FIXME: Not implemented yet max-content maximums.
  732. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  733. // to match the base size.
  734. if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
  735. grid_row.growth_limit = grid_row.base_size;
  736. ++index;
  737. }
  738. // 3. Increase sizes to accommodate spanning items crossing content-sized tracks: Next, consider the
  739. // items with a span of 2 that do not span a track with a flexible sizing function.
  740. // FIXME: Content-sized tracks not implemented (min-content, etc.)
  741. // 3.1. For intrinsic minimums: First increase the base size of tracks with an intrinsic min track sizing
  742. // function by distributing extra space as needed to accommodate these items’ minimum contributions.
  743. // If the grid container is being sized under a min- or max-content constraint, use the items’
  744. // limited min-content contributions in place of their minimum contributions here. (For an item
  745. // spanning multiple tracks, the upper limit used to calculate its limited min-/max-content
  746. // contribution is the sum of the fixed max track sizing functions of any tracks it spans, and is
  747. // applied if it only spans such tracks.)
  748. // 3.2. For content-based minimums: Next continue to increase the base size of tracks with a min track
  749. // sizing function of min-content or max-content by distributing extra space as needed to account
  750. // for these items' min-content contributions.
  751. // 3.3. For max-content minimums: Next, if the grid container is being sized under a max-content
  752. // constraint, continue to increase the base size of tracks with a min track sizing function of auto
  753. // or max-content by distributing extra space as needed to account for these items' limited
  754. // max-content contributions.
  755. // In all cases, continue to increase the base size of tracks with a min track sizing function of
  756. // max-content by distributing extra space as needed to account for these items' max-content
  757. // contributions.
  758. // 3.4. If at this point any track’s growth limit is now less than its base size, increase its growth
  759. // limit to match its base size.
  760. // 3.5. For intrinsic maximums: Next increase the growth limit of tracks with an intrinsic max track
  761. // sizing function by distributing extra space as needed to account for these items' min-content
  762. // contributions. Mark any tracks whose growth limit changed from infinite to finite in this step as
  763. // infinitely growable for the next step.
  764. // 3.6. For max-content maximums: Lastly continue to increase the growth limit of tracks with a max track
  765. // sizing function of max-content by distributing extra space as needed to account for these items'
  766. // max-content contributions. However, limit the growth of any fit-content() tracks by their
  767. // fit-content() argument.
  768. // Repeat incrementally for items with greater spans until all items have been considered.
  769. // FIXME: 4. Increase sizes to accommodate spanning items crossing flexible tracks: Next, repeat the previous
  770. // step instead considering (together, rather than grouped by span size) all items that do span a
  771. // track with a flexible sizing function while
  772. // - distributing space only to flexible tracks (i.e. treating all other tracks as having a fixed
  773. // sizing function)
  774. // - if the sum of the flexible sizing functions of all flexible tracks spanned by the item is greater
  775. // than zero, distributing space to such tracks according to the ratios of their flexible sizing
  776. // functions rather than distributing space equally
  777. // FIXME: 5. If any track still has an infinite growth limit (because, for example, it had no items placed in
  778. // it or it is a flexible track), set its growth limit to its base size.
  779. // https://www.w3.org/TR/css-grid-2/#extra-space
  780. // 12.5.1. Distributing Extra Space Across Spanned Tracks
  781. // To distribute extra space by increasing the affected sizes of a set of tracks as required by a
  782. // set of intrinsic size contributions,
  783. // 1. Maintain separately for each affected base size or growth limit a planned increase, initially
  784. // set to 0. (This prevents the size increases from becoming order-dependent.)
  785. // 2. For each considered item,
  786. // 2.1. Find the space to distribute: Subtract the corresponding size (base size or growth limit) of
  787. // every spanned track from the item’s size contribution to find the item’s remaining size
  788. // contribution. (For infinite growth limits, substitute the track’s base size.) This is the space
  789. // to distribute. Floor it at zero.
  790. // extra-space = max(0, size-contribution - ∑track-sizes)
  791. // 2.2. Distribute space up to limits: Find the item-incurred increase for each spanned track with an
  792. // affected size by: distributing the space equally among such tracks, freezing a track’s
  793. // item-incurred increase as its affected size + item-incurred increase reaches its limit (and
  794. // continuing to grow the unfrozen tracks as needed).
  795. // For base sizes, the limit is its growth limit. For growth limits, the limit is infinity if it is
  796. // marked as infinitely growable, and equal to the growth limit otherwise.
  797. // If the affected size was a growth limit and the track is not marked infinitely growable, then each
  798. // item-incurred increase will be zero.
  799. // 2.3. Distribute space beyond limits: If space remains after all tracks are frozen, unfreeze and
  800. // continue to distribute space to the item-incurred increase of…
  801. // - when accommodating minimum contributions or accommodating min-content contributions: any affected
  802. // track that happens to also have an intrinsic max track sizing function; if there are no such
  803. // tracks, then all affected tracks.
  804. // - when accommodating max-content contributions: any affected track that happens to also have a
  805. // max-content max track sizing function; if there are no such tracks, then all affected tracks.
  806. // - when handling any intrinsic growth limit: all affected tracks.
  807. // For this purpose, the max track sizing function of a fit-content() track is treated as
  808. // max-content until it reaches the limit specified as the fit-content() argument, after which it is
  809. // treated as having a fixed sizing function of that argument.
  810. // This step prioritizes the distribution of space for accommodating space required by the
  811. // tracks’ min track sizing functions beyond their current growth limits based on the types of their
  812. // max track sizing functions.
  813. // 2.4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned
  814. // increase set the track’s planned increase to that value.
  815. // 3. Update the tracks' affected sizes by adding in the planned increase so that the next round of
  816. // space distribution will account for the increase. (If the affected size is an infinite growth
  817. // limit, set it to the track’s base size plus the planned increase.)
  818. // https://www.w3.org/TR/css-grid-2/#algo-grow-tracks
  819. // 12.6. Maximize Tracks
  820. // If the free space is positive, distribute it equally to the base sizes of all tracks, freezing
  821. // tracks as they reach their growth limits (and continuing to grow the unfrozen tracks as needed).
  822. // For the purpose of this step: if sizing the grid container under a max-content constraint, the
  823. // free space is infinite; if sizing under a min-content constraint, the free space is zero.
  824. // If this would cause the grid to be larger than the grid container’s inner size as limited by its
  825. // max-width/height, then redo this step, treating the available grid space as equal to the grid
  826. // container’s inner size when it’s sized to its max-width/height.
  827. // FIXME: Do later as at the moment all growth limits are equal to base sizes.
  828. // https://drafts.csswg.org/css-grid/#algo-flex-tracks
  829. // 12.7. Expand Flexible Tracks
  830. // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding
  831. // the available space.
  832. // First, find the grid’s used flex fraction:
  833. auto column_flex_factor_sum = 0;
  834. for (auto& grid_column : grid_columns) {
  835. if (grid_column.min_track_sizing_function.is_flexible_length())
  836. column_flex_factor_sum++;
  837. }
  838. // See 12.7.1.
  839. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  840. // than 1, set it to 1 instead.
  841. if (column_flex_factor_sum < 1)
  842. column_flex_factor_sum = 1;
  843. // See 12.7.1.
  844. float sized_column_widths = 0;
  845. for (auto& grid_column : grid_columns) {
  846. if (!grid_column.min_track_sizing_function.is_flexible_length())
  847. sized_column_widths += grid_column.base_size;
  848. }
  849. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  850. double free_horizontal_space = box_state.content_width() - sized_column_widths;
  851. // If the free space is zero or if sizing the grid container under a min-content constraint:
  852. // The used flex fraction is zero.
  853. // FIXME: Add min-content constraint check.
  854. // Otherwise, if the free space is a definite length:
  855. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  856. // and a space to fill of the available grid space.
  857. if (free_horizontal_space > 0) {
  858. for (auto& grid_column : grid_columns) {
  859. if (grid_column.min_track_sizing_function.is_flexible_length()) {
  860. // See 12.7.1.
  861. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  862. auto hypothetical_fr_size = static_cast<double>(1.0 / column_flex_factor_sum) * free_horizontal_space;
  863. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  864. // greater than the track’s base size, set its base size to that product.
  865. grid_column.base_size = max(grid_column.base_size, hypothetical_fr_size);
  866. }
  867. }
  868. }
  869. // First, find the grid’s used flex fraction:
  870. auto row_flex_factor_sum = 0;
  871. for (auto& grid_row : grid_rows) {
  872. if (grid_row.min_track_sizing_function.is_flexible_length())
  873. row_flex_factor_sum++;
  874. }
  875. // See 12.7.1.
  876. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  877. // than 1, set it to 1 instead.
  878. if (row_flex_factor_sum < 1)
  879. row_flex_factor_sum = 1;
  880. // See 12.7.1.
  881. float sized_row_heights = 0;
  882. for (auto& grid_row : grid_rows) {
  883. if (!grid_row.min_track_sizing_function.is_flexible_length())
  884. sized_row_heights += grid_row.base_size;
  885. }
  886. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  887. double free_vertical_space = box_state.content_height() - sized_row_heights;
  888. // If the free space is zero or if sizing the grid container under a min-content constraint:
  889. // The used flex fraction is zero.
  890. // FIXME: Add min-content constraint check.
  891. // Otherwise, if the free space is a definite length:
  892. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  893. // and a space to fill of the available grid space.
  894. if (free_vertical_space > 0) {
  895. for (auto& grid_row : grid_rows) {
  896. if (grid_row.min_track_sizing_function.is_flexible_length()) {
  897. // See 12.7.1.
  898. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  899. auto hypothetical_fr_size = static_cast<double>(1.0 / row_flex_factor_sum) * free_vertical_space;
  900. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  901. // greater than the track’s base size, set its base size to that product.
  902. grid_row.base_size = max(grid_row.base_size, hypothetical_fr_size);
  903. }
  904. }
  905. }
  906. // Otherwise, if the free space is an indefinite length:
  907. // FIXME: No tracks will have indefinite length as per current implementation.
  908. // The used flex fraction is the maximum of:
  909. // For each flexible track, if the flexible track’s flex factor is greater than one, the result of
  910. // dividing the track’s base size by its flex factor; otherwise, the track’s base size.
  911. // For each grid item that crosses a flexible track, the result of finding the size of an fr using
  912. // all the grid tracks that the item crosses and a space to fill of the item’s max-content
  913. // contribution.
  914. // If using this flex fraction would cause the grid to be smaller than the grid container’s
  915. // min-width/height (or larger than the grid container’s max-width/height), then redo this step,
  916. // treating the free space as definite and the available grid space as equal to the grid container’s
  917. // inner size when it’s sized to its min-width/height (max-width/height).
  918. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  919. // greater than the track’s base size, set its base size to that product.
  920. // https://drafts.csswg.org/css-grid/#algo-find-fr-size
  921. // 12.7.1. Find the Size of an fr
  922. // This algorithm finds the largest size that an fr unit can be without exceeding the target size.
  923. // It must be called with a set of grid tracks and some quantity of space to fill.
  924. // 1. Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  925. // 2. Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  926. // than 1, set it to 1 instead.
  927. // 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  928. // FIXME: 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the
  929. // track’s base size, restart this algorithm treating all such tracks as inflexible.
  930. // 5. Return the hypothetical fr size.
  931. // https://drafts.csswg.org/css-grid/#algo-stretch
  932. // 12.8. Stretch auto Tracks
  933. // When the content-distribution property of the grid container is normal or stretch in this axis,
  934. // this step expands tracks that have an auto max track sizing function by dividing any remaining
  935. // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
  936. // container has a definite min-width/height, use that size to calculate the free space for this
  937. // step instead.
  938. float used_horizontal_space = 0;
  939. for (auto& grid_column : grid_columns) {
  940. if (!(grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto()))
  941. used_horizontal_space += grid_column.base_size;
  942. }
  943. float remaining_horizontal_space = box_state.content_width() - used_horizontal_space;
  944. auto count_of_auto_max_column_tracks = 0;
  945. for (auto& grid_column : grid_columns) {
  946. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  947. count_of_auto_max_column_tracks++;
  948. }
  949. for (auto& grid_column : grid_columns) {
  950. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  951. grid_column.base_size = max(grid_column.base_size, remaining_horizontal_space / count_of_auto_max_column_tracks);
  952. }
  953. float used_vertical_space = 0;
  954. for (auto& grid_row : grid_rows) {
  955. if (!(grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto()))
  956. used_vertical_space += grid_row.base_size;
  957. }
  958. float remaining_vertical_space = box_state.content_height() - used_vertical_space;
  959. auto count_of_auto_max_row_tracks = 0;
  960. for (auto& grid_row : grid_rows) {
  961. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  962. count_of_auto_max_row_tracks++;
  963. }
  964. for (auto& grid_row : grid_rows) {
  965. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  966. grid_row.base_size = max(grid_row.base_size, remaining_vertical_space / count_of_auto_max_row_tracks);
  967. }
  968. auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void {
  969. auto& child_box_state = m_state.get_mutable(child_box);
  970. float x_start = 0;
  971. float x_end = 0;
  972. float y_start = 0;
  973. float y_end = 0;
  974. for (int i = 0; i < column_start; i++)
  975. x_start += grid_columns[i].base_size;
  976. for (int i = 0; i < column_end; i++)
  977. x_end += grid_columns[i].base_size;
  978. for (int i = 0; i < row_start; i++)
  979. y_start += grid_rows[i].base_size;
  980. for (int i = 0; i < row_end; i++)
  981. y_end += grid_rows[i].base_size;
  982. child_box_state.set_content_width(x_end - x_start);
  983. child_box_state.set_content_height(y_end - y_start);
  984. child_box_state.offset = { x_start, y_start };
  985. };
  986. for (auto& positioned_box : positioned_boxes) {
  987. auto resolved_span = positioned_box.row + positioned_box.row_span > static_cast<int>(grid_rows.size()) ? static_cast<int>(grid_rows.size()) - positioned_box.row : positioned_box.row_span;
  988. layout_box(positioned_box.row, positioned_box.row + resolved_span, positioned_box.column, positioned_box.column + positioned_box.column_span, positioned_box.box);
  989. }
  990. float total_y = 0;
  991. for (auto& grid_row : grid_rows)
  992. total_y += grid_row.base_size;
  993. m_automatic_content_height = total_y;
  994. }
  995. float GridFormattingContext::automatic_content_height() const
  996. {
  997. return m_automatic_content_height;
  998. }
  999. bool GridFormattingContext::is_auto_positioned_row(CSS::GridTrackPlacement const& grid_row_start, CSS::GridTrackPlacement const& grid_row_end) const
  1000. {
  1001. return is_auto_positioned_track(grid_row_start, grid_row_end);
  1002. }
  1003. bool GridFormattingContext::is_auto_positioned_column(CSS::GridTrackPlacement const& grid_column_start, CSS::GridTrackPlacement const& grid_column_end) const
  1004. {
  1005. return is_auto_positioned_track(grid_column_start, grid_column_end);
  1006. }
  1007. bool GridFormattingContext::is_auto_positioned_track(CSS::GridTrackPlacement const& grid_track_start, CSS::GridTrackPlacement const& grid_track_end) const
  1008. {
  1009. return grid_track_start.is_auto_positioned() && grid_track_end.is_auto_positioned();
  1010. }
  1011. OccupationGrid::OccupationGrid(int column_count, int row_count)
  1012. {
  1013. Vector<bool> occupation_grid_row;
  1014. for (int column_index = 0; column_index < max(column_count, 1); column_index++)
  1015. occupation_grid_row.append(false);
  1016. for (int row_index = 0; row_index < max(row_count, 1); row_index++)
  1017. m_occupation_grid.append(occupation_grid_row);
  1018. }
  1019. void OccupationGrid::maybe_add_column(int needed_number_of_columns)
  1020. {
  1021. if (needed_number_of_columns <= column_count())
  1022. return;
  1023. auto column_count_before_modification = column_count();
  1024. for (auto& occupation_grid_row : m_occupation_grid)
  1025. for (int idx = 0; idx < needed_number_of_columns - column_count_before_modification; idx++)
  1026. occupation_grid_row.append(false);
  1027. }
  1028. void OccupationGrid::maybe_add_row(int needed_number_of_rows)
  1029. {
  1030. if (needed_number_of_rows <= row_count())
  1031. return;
  1032. Vector<bool> new_occupation_grid_row;
  1033. for (int idx = 0; idx < column_count(); idx++)
  1034. new_occupation_grid_row.append(false);
  1035. for (int idx = 0; idx < needed_number_of_rows - row_count(); idx++)
  1036. m_occupation_grid.append(new_occupation_grid_row);
  1037. }
  1038. void OccupationGrid::set_occupied(int column_start, int column_end, int row_start, int row_end)
  1039. {
  1040. for (int row_index = 0; row_index < row_count(); row_index++) {
  1041. if (row_index >= row_start && row_index < row_end) {
  1042. for (int column_index = 0; column_index < column_count(); column_index++) {
  1043. if (column_index >= column_start && column_index < column_end)
  1044. set_occupied(column_index, row_index);
  1045. }
  1046. }
  1047. }
  1048. }
  1049. void OccupationGrid::set_occupied(int column_index, int row_index)
  1050. {
  1051. m_occupation_grid[row_index][column_index] = true;
  1052. }
  1053. bool OccupationGrid::is_occupied(int column_index, int row_index)
  1054. {
  1055. return m_occupation_grid[row_index][column_index];
  1056. }
  1057. }