GridFormattingContext.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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. for (int x = 0; x < column_repeat_count; ++x) {
  442. for (auto& meta_grid_track_size : box.computed_values().grid_template_columns().meta_grid_track_sizes())
  443. m_grid_columns.append({ meta_grid_track_size.min_grid_track_size(), meta_grid_track_size.max_grid_track_size() });
  444. }
  445. for (int x = 0; x < row_repeat_count; ++x) {
  446. for (auto& meta_grid_track_size : box.computed_values().grid_template_rows().meta_grid_track_sizes())
  447. m_grid_rows.append({ meta_grid_track_size.min_grid_track_size(), meta_grid_track_size.max_grid_track_size() });
  448. }
  449. for (int column_index = m_grid_columns.size(); column_index < occupation_grid.column_count(); column_index++)
  450. m_grid_columns.append({ CSS::GridTrackSize::make_auto(), CSS::GridTrackSize::make_auto() });
  451. for (int row_index = m_grid_rows.size(); row_index < occupation_grid.row_count(); row_index++)
  452. m_grid_rows.append({ CSS::GridTrackSize::make_auto(), CSS::GridTrackSize::make_auto() });
  453. // https://www.w3.org/TR/css-grid-2/#algo-overview
  454. // 12.1. Grid Sizing Algorithm
  455. // 1. First, the track sizing algorithm is used to resolve the sizes of the grid columns.
  456. // In this process, any grid item which is subgridded in the grid container’s inline axis is treated
  457. // as empty and its grid items (the grandchildren) are treated as direct children of the grid
  458. // container (their grandparent). This introspection is recursive.
  459. // Items which are subgridded only in the block axis, and whose grid container size in the inline
  460. // axis depends on the size of its contents are also introspected: since the size of the item in
  461. // this dimension can be dependent on the sizing of its subgridded tracks in the other, the size
  462. // contribution of any such item to this grid’s column sizing (see Resolve Intrinsic Track Sizes) is
  463. // taken under the provision of having determined its track sizing only up to the same point in the
  464. // Grid Sizing Algorithm as this itself. E.g. for the first pass through this step, the item will
  465. // have its tracks sized only through this first step; if a second pass of this step is triggered
  466. // then the item will have completed a first pass through steps 1-3 as well as the second pass of
  467. // this step prior to returning its size for consideration in this grid’s column sizing. Again, this
  468. // introspection is recursive.
  469. // If calculating the layout of a grid item in this step depends on the available space in the block
  470. // axis, assume the available space that it would have if any row with a definite max track sizing
  471. // function had that size and all other rows were infinite. If both the grid container and all
  472. // tracks have definite sizes, also apply align-content to find the final effective size of any gaps
  473. // spanned by such items; otherwise ignore the effects of track alignment in this estimation.
  474. // 2. Next, the track sizing algorithm resolves the sizes of the grid rows.
  475. // In this process, any grid item which is subgridded in the grid container’s block axis is treated
  476. // as empty and its grid items (the grandchildren) are treated as direct children of the grid
  477. // container (their grandparent). This introspection is recursive.
  478. // As with sizing columns, items which are subgridded only in the inline axis, and whose grid
  479. // container size in the block axis depends on the size of its contents are also introspected. (As
  480. // with sizing columns, the size contribution to this grid’s row sizing is taken under the provision
  481. // of having determined its track sizing only up to this corresponding point in the algorithm; and
  482. // again, this introspection is recursive.)
  483. // To find the inline-axis available space for any items whose block-axis size contributions require
  484. // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
  485. // size is definite, also apply justify-content to account for the effective column gap sizes.
  486. // 3. Then, if the min-content contribution of any grid item has changed based on the row sizes and
  487. // alignment calculated in step 2, re-resolve the sizes of the grid columns with the new min-content
  488. // and max-content contributions (once only).
  489. // To find the block-axis available space for any items whose inline-axis size contributions require
  490. // it, use the grid row sizes calculated in the previous step. If the grid container’s block size is
  491. // definite, also apply align-content to account for the effective row gap sizes
  492. // 4. Next, if the min-content contribution of any grid item has changed based on the column sizes and
  493. // alignment calculated in step 3, re-resolve the sizes of the grid rows with the new min-content
  494. // and max-content contributions (once only).
  495. // To find the inline-axis available space for any items whose block-axis size contributions require
  496. // it, use the grid column sizes calculated in the previous step. If the grid container’s inline
  497. // size is definite, also apply justify-content to account for the effective column gap sizes.
  498. // 5. Finally, the grid container is sized using the resulting size of the grid as its content size,
  499. // and the tracks are aligned within the grid container according to the align-content and
  500. // justify-content properties.
  501. // Once the size of each grid area is thus established, the grid items are laid out into their
  502. // respective containing blocks. The grid area’s width and height are considered definite for this
  503. // purpose.
  504. // https://www.w3.org/TR/css-grid-2/#algo-track-sizing
  505. // 12.3. Track Sizing Algorithm
  506. // The remainder of this section is the track sizing algorithm, which calculates from the min and
  507. // max track sizing functions the used track size. Each track has a base size, a <length> which
  508. // grows throughout the algorithm and which will eventually be the track’s final size, and a growth
  509. // limit, a <length> which provides a desired maximum size for the base size. There are 5 steps:
  510. // 1. Initialize Track Sizes
  511. // 2. Resolve Intrinsic Track Sizes
  512. // 3. Maximize Tracks
  513. // 4. Expand Flexible Tracks
  514. // 5. Expand Stretched auto Tracks
  515. // https://www.w3.org/TR/css-grid-2/#algo-init
  516. // 12.4. Initialize Track Sizes
  517. // Initialize each track’s base size and growth limit.
  518. for (auto& grid_column : m_grid_columns) {
  519. // For each track, if the track’s min track sizing function is:
  520. switch (grid_column.min_track_sizing_function.type()) {
  521. // - A fixed sizing function
  522. // Resolve to an absolute length and use that size as the track’s initial base size.
  523. case CSS::GridTrackSize::Type::Length:
  524. if (!grid_column.min_track_sizing_function.length().is_auto())
  525. grid_column.base_size = grid_column.min_track_sizing_function.length().to_px(box);
  526. break;
  527. case CSS::GridTrackSize::Type::Percentage:
  528. grid_column.base_size = grid_column.min_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  529. break;
  530. // - An intrinsic sizing function
  531. // Use an initial base size of zero.
  532. case CSS::GridTrackSize::Type::FlexibleLength:
  533. break;
  534. default:
  535. VERIFY_NOT_REACHED();
  536. }
  537. // For each track, if the track’s max track sizing function is:
  538. switch (grid_column.max_track_sizing_function.type()) {
  539. // - A fixed sizing function
  540. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  541. case CSS::GridTrackSize::Type::Length:
  542. if (!grid_column.max_track_sizing_function.length().is_auto())
  543. grid_column.growth_limit = grid_column.max_track_sizing_function.length().to_px(box);
  544. else
  545. // - An intrinsic sizing function
  546. // Use an initial growth limit of infinity.
  547. grid_column.growth_limit = -1;
  548. break;
  549. case CSS::GridTrackSize::Type::Percentage:
  550. grid_column.growth_limit = grid_column.max_track_sizing_function.percentage().as_fraction() * box_state.content_width();
  551. break;
  552. // - A flexible sizing function
  553. // Use an initial growth limit of infinity.
  554. case CSS::GridTrackSize::Type::FlexibleLength:
  555. grid_column.growth_limit = -1;
  556. break;
  557. default:
  558. VERIFY_NOT_REACHED();
  559. }
  560. // In all cases, if the growth limit is less than the base size, increase the growth limit to match
  561. // the base size.
  562. if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
  563. grid_column.growth_limit = grid_column.base_size;
  564. }
  565. // Initialize each track’s base size and growth limit.
  566. for (auto& grid_row : m_grid_rows) {
  567. // For each track, if the track’s min track sizing function is:
  568. switch (grid_row.min_track_sizing_function.type()) {
  569. // - A fixed sizing function
  570. // Resolve to an absolute length and use that size as the track’s initial base size.
  571. case CSS::GridTrackSize::Type::Length:
  572. if (!grid_row.min_track_sizing_function.length().is_auto())
  573. grid_row.base_size = grid_row.min_track_sizing_function.length().to_px(box);
  574. break;
  575. case CSS::GridTrackSize::Type::Percentage:
  576. grid_row.base_size = grid_row.min_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  577. break;
  578. // - An intrinsic sizing function
  579. // Use an initial base size of zero.
  580. case CSS::GridTrackSize::Type::FlexibleLength:
  581. break;
  582. default:
  583. VERIFY_NOT_REACHED();
  584. }
  585. // For each track, if the track’s max track sizing function is:
  586. switch (grid_row.max_track_sizing_function.type()) {
  587. // - A fixed sizing function
  588. // Resolve to an absolute length and use that size as the track’s initial growth limit.
  589. case CSS::GridTrackSize::Type::Length:
  590. if (!grid_row.max_track_sizing_function.length().is_auto())
  591. grid_row.growth_limit = grid_row.max_track_sizing_function.length().to_px(box);
  592. else
  593. // - An intrinsic sizing function
  594. // Use an initial growth limit of infinity.
  595. grid_row.growth_limit = -1;
  596. break;
  597. case CSS::GridTrackSize::Type::Percentage:
  598. grid_row.growth_limit = grid_row.max_track_sizing_function.percentage().as_fraction() * box_state.content_height();
  599. break;
  600. // - A flexible sizing function
  601. // Use an initial growth limit of infinity.
  602. case CSS::GridTrackSize::Type::FlexibleLength:
  603. grid_row.growth_limit = -1;
  604. break;
  605. default:
  606. VERIFY_NOT_REACHED();
  607. }
  608. // In all cases, if the growth limit is less than the base size, increase the growth limit to match
  609. // the base size.
  610. if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
  611. grid_row.growth_limit = grid_row.base_size;
  612. }
  613. // https://www.w3.org/TR/css-grid-2/#algo-content
  614. // 12.5. Resolve Intrinsic Track Sizes
  615. // This step resolves intrinsic track sizing functions to absolute lengths. First it resolves those
  616. // sizes based on items that are contained wholly within a single track. Then it gradually adds in
  617. // the space requirements of items that span multiple tracks, evenly distributing the extra space
  618. // across those tracks insofar as possible.
  619. // FIXME: 1. Shim baseline-aligned items so their intrinsic size contributions reflect their baseline
  620. // alignment. For the items in each baseline-sharing group, add a “shim” (effectively, additional
  621. // margin) on the start/end side (for first/last-baseline alignment) of each item so that, when
  622. // start/end-aligned together their baselines align as specified.
  623. // Consider these “shims” as part of the items’ intrinsic size contribution for the purpose of track
  624. // sizing, below. If an item uses multiple intrinsic size contributions, it can have different shims
  625. // for each one.
  626. // 2. Size tracks to fit non-spanning items: For each track with an intrinsic track sizing function and
  627. // not a flexible sizing function, consider the items in it with a span of 1:
  628. int index = 0;
  629. for (auto& grid_column : m_grid_columns) {
  630. if (!grid_column.min_track_sizing_function.is_intrinsic_track_sizing()) {
  631. ++index;
  632. continue;
  633. }
  634. Vector<Box const&> boxes_of_column;
  635. for (auto& positioned_box : positioned_boxes) {
  636. if (positioned_box.column == index && positioned_box.column_span == 1)
  637. boxes_of_column.append(positioned_box.box);
  638. }
  639. // - For min-content minimums:
  640. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  641. // items’ min-content contributions, floored at zero.
  642. // FIXME: Not implemented yet min-content.
  643. // - For max-content minimums:
  644. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  645. // items’ max-content contributions, floored at zero.
  646. // FIXME: Not implemented yet max-content.
  647. // - For auto minimums:
  648. // If the track has an auto min track sizing function and the grid container is being sized under a
  649. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  650. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  651. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  652. // limited by the max track sizing function (which could be the argument to a fit-content() track
  653. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  654. // below).
  655. // FIXME: Not implemented yet min-/max-content.
  656. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  657. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  658. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  659. // containing block in the relevant axis, its minimum contribution is the outer size that would
  660. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  661. // contribution is its min-content contribution. Because the minimum contribution often depends on
  662. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  663. float grid_column_width = 0;
  664. for (auto& box_of_column : boxes_of_column)
  665. grid_column_width = max(grid_column_width, calculate_min_content_width(box_of_column));
  666. grid_column.base_size = grid_column_width;
  667. // - For min-content maximums:
  668. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  669. // the items’ min-content contributions.
  670. // FIXME: Not implemented yet min-content maximums.
  671. // - For max-content maximums:
  672. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  673. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  674. // limit by the fit-content() argument.
  675. // FIXME: Not implemented yet max-content maximums.
  676. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  677. // to match the base size.
  678. if (grid_column.growth_limit != -1 && grid_column.growth_limit < grid_column.base_size)
  679. grid_column.growth_limit = grid_column.base_size;
  680. ++index;
  681. }
  682. index = 0;
  683. for (auto& grid_row : m_grid_rows) {
  684. if (!grid_row.min_track_sizing_function.is_intrinsic_track_sizing()) {
  685. ++index;
  686. continue;
  687. }
  688. Vector<PositionedBox&> positioned_boxes_of_row;
  689. for (auto& positioned_box : positioned_boxes) {
  690. if (positioned_box.row == index && positioned_box.row_span == 1)
  691. positioned_boxes_of_row.append(positioned_box);
  692. }
  693. // - For min-content minimums:
  694. // If the track has a min-content min track sizing function, set its base size to the maximum of the
  695. // items’ min-content contributions, floored at zero.
  696. // FIXME: Not implemented yet min-content.
  697. // - For max-content minimums:
  698. // If the track has a max-content min track sizing function, set its base size to the maximum of the
  699. // items’ max-content contributions, floored at zero.
  700. // FIXME: Not implemented yet max-content.
  701. // - For auto minimums:
  702. // If the track has an auto min track sizing function and the grid container is being sized under a
  703. // min-/max-content constraint, set the track’s base size to the maximum of its items’ limited
  704. // min-/max-content contributions (respectively), floored at zero. The limited min-/max-content
  705. // contribution of an item is (for this purpose) its min-/max-content contribution (accordingly),
  706. // limited by the max track sizing function (which could be the argument to a fit-content() track
  707. // sizing function) if that is fixed and ultimately floored by its minimum contribution (defined
  708. // below).
  709. // FIXME: Not implemented yet min-/max-content.
  710. // Otherwise, set the track’s base size to the maximum of its items’ minimum contributions, floored
  711. // at zero. The minimum contribution of an item is the smallest outer size it can have.
  712. // Specifically, if the item’s computed preferred size behaves as auto or depends on the size of its
  713. // containing block in the relevant axis, its minimum contribution is the outer size that would
  714. // result from assuming the item’s used minimum size as its preferred size; else the item’s minimum
  715. // contribution is its min-content contribution. Because the minimum contribution often depends on
  716. // the size of the item’s content, it is considered a type of intrinsic size contribution.
  717. float grid_row_height = 0;
  718. for (auto& positioned_box : positioned_boxes_of_row)
  719. grid_row_height = max(grid_row_height, positioned_box.computed_height);
  720. grid_row.base_size = grid_row_height;
  721. // - For min-content maximums:
  722. // If the track has a min-content max track sizing function, set its growth limit to the maximum of
  723. // the items’ min-content contributions.
  724. // FIXME: Not implemented yet min-content maximums.
  725. // - For max-content maximums:
  726. // If the track has a max-content max track sizing function, set its growth limit to the maximum of
  727. // the items’ max-content contributions. For fit-content() maximums, furthermore clamp this growth
  728. // limit by the fit-content() argument.
  729. // FIXME: Not implemented yet max-content maximums.
  730. // In all cases, if a track’s growth limit is now less than its base size, increase the growth limit
  731. // to match the base size.
  732. if (grid_row.growth_limit != -1 && grid_row.growth_limit < grid_row.base_size)
  733. grid_row.growth_limit = grid_row.base_size;
  734. ++index;
  735. }
  736. // 3. Increase sizes to accommodate spanning items crossing content-sized tracks: Next, consider the
  737. // items with a span of 2 that do not span a track with a flexible sizing function.
  738. // FIXME: Content-sized tracks not implemented (min-content, etc.)
  739. // 3.1. For intrinsic minimums: First increase the base size of tracks with an intrinsic min track sizing
  740. // function by distributing extra space as needed to accommodate these items’ minimum contributions.
  741. // If the grid container is being sized under a min- or max-content constraint, use the items’
  742. // limited min-content contributions in place of their minimum contributions here. (For an item
  743. // spanning multiple tracks, the upper limit used to calculate its limited min-/max-content
  744. // contribution is the sum of the fixed max track sizing functions of any tracks it spans, and is
  745. // applied if it only spans such tracks.)
  746. // 3.2. For content-based minimums: Next continue to increase the base size of tracks with a min track
  747. // sizing function of min-content or max-content by distributing extra space as needed to account
  748. // for these items' min-content contributions.
  749. // 3.3. For max-content minimums: Next, if the grid container is being sized under a max-content
  750. // constraint, continue to increase the base size of tracks with a min track sizing function of auto
  751. // or max-content by distributing extra space as needed to account for these items' limited
  752. // max-content contributions.
  753. // In all cases, continue to increase the base size of tracks with a min track sizing function of
  754. // max-content by distributing extra space as needed to account for these items' max-content
  755. // contributions.
  756. // 3.4. If at this point any track’s growth limit is now less than its base size, increase its growth
  757. // limit to match its base size.
  758. // 3.5. For intrinsic maximums: Next increase the growth limit of tracks with an intrinsic max track
  759. // sizing function by distributing extra space as needed to account for these items' min-content
  760. // contributions. Mark any tracks whose growth limit changed from infinite to finite in this step as
  761. // infinitely growable for the next step.
  762. // 3.6. For max-content maximums: Lastly continue to increase the growth limit of tracks with a max track
  763. // sizing function of max-content by distributing extra space as needed to account for these items'
  764. // max-content contributions. However, limit the growth of any fit-content() tracks by their
  765. // fit-content() argument.
  766. // Repeat incrementally for items with greater spans until all items have been considered.
  767. // FIXME: 4. Increase sizes to accommodate spanning items crossing flexible tracks: Next, repeat the previous
  768. // step instead considering (together, rather than grouped by span size) all items that do span a
  769. // track with a flexible sizing function while
  770. // - distributing space only to flexible tracks (i.e. treating all other tracks as having a fixed
  771. // sizing function)
  772. // - if the sum of the flexible sizing functions of all flexible tracks spanned by the item is greater
  773. // than zero, distributing space to such tracks according to the ratios of their flexible sizing
  774. // functions rather than distributing space equally
  775. // FIXME: 5. If any track still has an infinite growth limit (because, for example, it had no items placed in
  776. // it or it is a flexible track), set its growth limit to its base size.
  777. // https://www.w3.org/TR/css-grid-2/#extra-space
  778. // 12.5.1. Distributing Extra Space Across Spanned Tracks
  779. // To distribute extra space by increasing the affected sizes of a set of tracks as required by a
  780. // set of intrinsic size contributions,
  781. // 1. Maintain separately for each affected base size or growth limit a planned increase, initially
  782. // set to 0. (This prevents the size increases from becoming order-dependent.)
  783. // 2. For each considered item,
  784. // 2.1. Find the space to distribute: Subtract the corresponding size (base size or growth limit) of
  785. // every spanned track from the item’s size contribution to find the item’s remaining size
  786. // contribution. (For infinite growth limits, substitute the track’s base size.) This is the space
  787. // to distribute. Floor it at zero.
  788. // extra-space = max(0, size-contribution - ∑track-sizes)
  789. // 2.2. Distribute space up to limits: Find the item-incurred increase for each spanned track with an
  790. // affected size by: distributing the space equally among such tracks, freezing a track’s
  791. // item-incurred increase as its affected size + item-incurred increase reaches its limit (and
  792. // continuing to grow the unfrozen tracks as needed).
  793. // For base sizes, the limit is its growth limit. For growth limits, the limit is infinity if it is
  794. // marked as infinitely growable, and equal to the growth limit otherwise.
  795. // If the affected size was a growth limit and the track is not marked infinitely growable, then each
  796. // item-incurred increase will be zero.
  797. // 2.3. Distribute space beyond limits: If space remains after all tracks are frozen, unfreeze and
  798. // continue to distribute space to the item-incurred increase of…
  799. // - when accommodating minimum contributions or accommodating min-content contributions: any affected
  800. // track that happens to also have an intrinsic max track sizing function; if there are no such
  801. // tracks, then all affected tracks.
  802. // - when accommodating max-content contributions: any affected track that happens to also have a
  803. // max-content max track sizing function; if there are no such tracks, then all affected tracks.
  804. // - when handling any intrinsic growth limit: all affected tracks.
  805. // For this purpose, the max track sizing function of a fit-content() track is treated as
  806. // max-content until it reaches the limit specified as the fit-content() argument, after which it is
  807. // treated as having a fixed sizing function of that argument.
  808. // This step prioritizes the distribution of space for accommodating space required by the
  809. // tracks’ min track sizing functions beyond their current growth limits based on the types of their
  810. // max track sizing functions.
  811. // 2.4. For each affected track, if the track’s item-incurred increase is larger than the track’s planned
  812. // increase set the track’s planned increase to that value.
  813. // 3. Update the tracks' affected sizes by adding in the planned increase so that the next round of
  814. // space distribution will account for the increase. (If the affected size is an infinite growth
  815. // limit, set it to the track’s base size plus the planned increase.)
  816. // https://www.w3.org/TR/css-grid-2/#algo-grow-tracks
  817. // 12.6. Maximize Tracks
  818. // If the free space is positive, distribute it equally to the base sizes of all tracks, freezing
  819. // tracks as they reach their growth limits (and continuing to grow the unfrozen tracks as needed).
  820. // For the purpose of this step: if sizing the grid container under a max-content constraint, the
  821. // free space is infinite; if sizing under a min-content constraint, the free space is zero.
  822. // If this would cause the grid to be larger than the grid container’s inner size as limited by its
  823. // max-width/height, then redo this step, treating the available grid space as equal to the grid
  824. // container’s inner size when it’s sized to its max-width/height.
  825. // FIXME: Do later as at the moment all growth limits are equal to base sizes.
  826. // https://drafts.csswg.org/css-grid/#algo-flex-tracks
  827. // 12.7. Expand Flexible Tracks
  828. // This step sizes flexible tracks using the largest value it can assign to an fr without exceeding
  829. // the available space.
  830. // First, find the grid’s used flex fraction:
  831. auto column_flex_factor_sum = 0;
  832. for (auto& grid_column : m_grid_columns) {
  833. if (grid_column.min_track_sizing_function.is_flexible_length())
  834. column_flex_factor_sum++;
  835. }
  836. // See 12.7.1.
  837. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  838. // than 1, set it to 1 instead.
  839. if (column_flex_factor_sum < 1)
  840. column_flex_factor_sum = 1;
  841. // See 12.7.1.
  842. float sized_column_widths = 0;
  843. for (auto& grid_column : m_grid_columns) {
  844. if (!grid_column.min_track_sizing_function.is_flexible_length())
  845. sized_column_widths += grid_column.base_size;
  846. }
  847. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  848. double free_horizontal_space = box_state.content_width() - sized_column_widths;
  849. // If the free space is zero or if sizing the grid container under a min-content constraint:
  850. // The used flex fraction is zero.
  851. // FIXME: Add min-content constraint check.
  852. // Otherwise, if the free space is a definite length:
  853. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  854. // and a space to fill of the available grid space.
  855. if (free_horizontal_space > 0) {
  856. for (auto& grid_column : m_grid_columns) {
  857. if (grid_column.min_track_sizing_function.is_flexible_length()) {
  858. // See 12.7.1.
  859. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  860. auto hypothetical_fr_size = static_cast<double>(1.0 / column_flex_factor_sum) * free_horizontal_space;
  861. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  862. // greater than the track’s base size, set its base size to that product.
  863. grid_column.base_size = max(grid_column.base_size, hypothetical_fr_size);
  864. }
  865. }
  866. }
  867. // First, find the grid’s used flex fraction:
  868. auto row_flex_factor_sum = 0;
  869. for (auto& grid_row : m_grid_rows) {
  870. if (grid_row.min_track_sizing_function.is_flexible_length())
  871. row_flex_factor_sum++;
  872. }
  873. // See 12.7.1.
  874. // Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  875. // than 1, set it to 1 instead.
  876. if (row_flex_factor_sum < 1)
  877. row_flex_factor_sum = 1;
  878. // See 12.7.1.
  879. float sized_row_heights = 0;
  880. for (auto& grid_row : m_grid_rows) {
  881. if (!grid_row.min_track_sizing_function.is_flexible_length())
  882. sized_row_heights += grid_row.base_size;
  883. }
  884. // Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  885. double free_vertical_space = box_state.content_height() - sized_row_heights;
  886. // If the free space is zero or if sizing the grid container under a min-content constraint:
  887. // The used flex fraction is zero.
  888. // FIXME: Add min-content constraint check.
  889. // Otherwise, if the free space is a definite length:
  890. // The used flex fraction is the result of finding the size of an fr using all of the grid tracks
  891. // and a space to fill of the available grid space.
  892. if (free_vertical_space > 0) {
  893. for (auto& grid_row : m_grid_rows) {
  894. if (grid_row.min_track_sizing_function.is_flexible_length()) {
  895. // See 12.7.1.
  896. // Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  897. auto hypothetical_fr_size = static_cast<double>(1.0 / row_flex_factor_sum) * free_vertical_space;
  898. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  899. // greater than the track’s base size, set its base size to that product.
  900. grid_row.base_size = max(grid_row.base_size, hypothetical_fr_size);
  901. }
  902. }
  903. }
  904. // Otherwise, if the free space is an indefinite length:
  905. // FIXME: No tracks will have indefinite length as per current implementation.
  906. // The used flex fraction is the maximum of:
  907. // For each flexible track, if the flexible track’s flex factor is greater than one, the result of
  908. // dividing the track’s base size by its flex factor; otherwise, the track’s base size.
  909. // For each grid item that crosses a flexible track, the result of finding the size of an fr using
  910. // all the grid tracks that the item crosses and a space to fill of the item’s max-content
  911. // contribution.
  912. // If using this flex fraction would cause the grid to be smaller than the grid container’s
  913. // min-width/height (or larger than the grid container’s max-width/height), then redo this step,
  914. // treating the free space as definite and the available grid space as equal to the grid container’s
  915. // inner size when it’s sized to its min-width/height (max-width/height).
  916. // For each flexible track, if the product of the used flex fraction and the track’s flex factor is
  917. // greater than the track’s base size, set its base size to that product.
  918. // https://drafts.csswg.org/css-grid/#algo-find-fr-size
  919. // 12.7.1. Find the Size of an fr
  920. // This algorithm finds the largest size that an fr unit can be without exceeding the target size.
  921. // It must be called with a set of grid tracks and some quantity of space to fill.
  922. // 1. Let leftover space be the space to fill minus the base sizes of the non-flexible grid tracks.
  923. // 2. Let flex factor sum be the sum of the flex factors of the flexible tracks. If this value is less
  924. // than 1, set it to 1 instead.
  925. // 3. Let the hypothetical fr size be the leftover space divided by the flex factor sum.
  926. // FIXME: 4. If the product of the hypothetical fr size and a flexible track’s flex factor is less than the
  927. // track’s base size, restart this algorithm treating all such tracks as inflexible.
  928. // 5. Return the hypothetical fr size.
  929. // https://drafts.csswg.org/css-grid/#algo-stretch
  930. // 12.8. Stretch auto Tracks
  931. // When the content-distribution property of the grid container is normal or stretch in this axis,
  932. // this step expands tracks that have an auto max track sizing function by dividing any remaining
  933. // positive, definite free space equally amongst them. If the free space is indefinite, but the grid
  934. // container has a definite min-width/height, use that size to calculate the free space for this
  935. // step instead.
  936. float used_horizontal_space = 0;
  937. for (auto& grid_column : m_grid_columns) {
  938. if (!(grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto()))
  939. used_horizontal_space += grid_column.base_size;
  940. }
  941. float remaining_horizontal_space = box_state.content_width() - used_horizontal_space;
  942. auto count_of_auto_max_column_tracks = 0;
  943. for (auto& grid_column : m_grid_columns) {
  944. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  945. count_of_auto_max_column_tracks++;
  946. }
  947. for (auto& grid_column : m_grid_columns) {
  948. if (grid_column.max_track_sizing_function.is_length() && grid_column.max_track_sizing_function.length().is_auto())
  949. grid_column.base_size = max(grid_column.base_size, remaining_horizontal_space / count_of_auto_max_column_tracks);
  950. }
  951. float used_vertical_space = 0;
  952. for (auto& grid_row : m_grid_rows) {
  953. if (!(grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto()))
  954. used_vertical_space += grid_row.base_size;
  955. }
  956. float remaining_vertical_space = box_state.content_height() - used_vertical_space;
  957. auto count_of_auto_max_row_tracks = 0;
  958. for (auto& grid_row : m_grid_rows) {
  959. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  960. count_of_auto_max_row_tracks++;
  961. }
  962. for (auto& grid_row : m_grid_rows) {
  963. if (grid_row.max_track_sizing_function.is_length() && grid_row.max_track_sizing_function.length().is_auto())
  964. grid_row.base_size = max(grid_row.base_size, remaining_vertical_space / count_of_auto_max_row_tracks);
  965. }
  966. auto layout_box = [&](int row_start, int row_end, int column_start, int column_end, Box const& child_box) -> void {
  967. auto& child_box_state = m_state.get_mutable(child_box);
  968. float x_start = 0;
  969. float x_end = 0;
  970. float y_start = 0;
  971. float y_end = 0;
  972. for (int i = 0; i < column_start; i++)
  973. x_start += m_grid_columns[i].base_size;
  974. for (int i = 0; i < column_end; i++)
  975. x_end += m_grid_columns[i].base_size;
  976. for (int i = 0; i < row_start; i++)
  977. y_start += m_grid_rows[i].base_size;
  978. for (int i = 0; i < row_end; i++)
  979. y_end += m_grid_rows[i].base_size;
  980. child_box_state.set_content_width(x_end - x_start);
  981. child_box_state.set_content_height(y_end - y_start);
  982. child_box_state.offset = { x_start, y_start };
  983. };
  984. for (auto& positioned_box : positioned_boxes) {
  985. auto resolved_span = positioned_box.row + positioned_box.row_span > static_cast<int>(m_grid_rows.size()) ? static_cast<int>(m_grid_rows.size()) - positioned_box.row : positioned_box.row_span;
  986. layout_box(positioned_box.row, positioned_box.row + resolved_span, positioned_box.column, positioned_box.column + positioned_box.column_span, positioned_box.box);
  987. }
  988. float total_y = 0;
  989. for (auto& grid_row : m_grid_rows)
  990. total_y += grid_row.base_size;
  991. m_automatic_content_height = total_y;
  992. }
  993. float GridFormattingContext::automatic_content_height() const
  994. {
  995. return m_automatic_content_height;
  996. }
  997. bool GridFormattingContext::is_auto_positioned_row(CSS::GridTrackPlacement const& grid_row_start, CSS::GridTrackPlacement const& grid_row_end) const
  998. {
  999. return is_auto_positioned_track(grid_row_start, grid_row_end);
  1000. }
  1001. bool GridFormattingContext::is_auto_positioned_column(CSS::GridTrackPlacement const& grid_column_start, CSS::GridTrackPlacement const& grid_column_end) const
  1002. {
  1003. return is_auto_positioned_track(grid_column_start, grid_column_end);
  1004. }
  1005. bool GridFormattingContext::is_auto_positioned_track(CSS::GridTrackPlacement const& grid_track_start, CSS::GridTrackPlacement const& grid_track_end) const
  1006. {
  1007. return grid_track_start.is_auto_positioned() && grid_track_end.is_auto_positioned();
  1008. }
  1009. OccupationGrid::OccupationGrid(int column_count, int row_count)
  1010. {
  1011. Vector<bool> occupation_grid_row;
  1012. for (int column_index = 0; column_index < max(column_count, 1); column_index++)
  1013. occupation_grid_row.append(false);
  1014. for (int row_index = 0; row_index < max(row_count, 1); row_index++)
  1015. m_occupation_grid.append(occupation_grid_row);
  1016. }
  1017. void OccupationGrid::maybe_add_column(int needed_number_of_columns)
  1018. {
  1019. if (needed_number_of_columns <= column_count())
  1020. return;
  1021. auto column_count_before_modification = column_count();
  1022. for (auto& occupation_grid_row : m_occupation_grid)
  1023. for (int idx = 0; idx < needed_number_of_columns - column_count_before_modification; idx++)
  1024. occupation_grid_row.append(false);
  1025. }
  1026. void OccupationGrid::maybe_add_row(int needed_number_of_rows)
  1027. {
  1028. if (needed_number_of_rows <= row_count())
  1029. return;
  1030. Vector<bool> new_occupation_grid_row;
  1031. for (int idx = 0; idx < column_count(); idx++)
  1032. new_occupation_grid_row.append(false);
  1033. for (int idx = 0; idx < needed_number_of_rows - row_count(); idx++)
  1034. m_occupation_grid.append(new_occupation_grid_row);
  1035. }
  1036. void OccupationGrid::set_occupied(int column_start, int column_end, int row_start, int row_end)
  1037. {
  1038. for (int row_index = 0; row_index < row_count(); row_index++) {
  1039. if (row_index >= row_start && row_index < row_end) {
  1040. for (int column_index = 0; column_index < column_count(); column_index++) {
  1041. if (column_index >= column_start && column_index < column_end)
  1042. set_occupied(column_index, row_index);
  1043. }
  1044. }
  1045. }
  1046. }
  1047. void OccupationGrid::set_occupied(int column_index, int row_index)
  1048. {
  1049. m_occupation_grid[row_index][column_index] = true;
  1050. }
  1051. bool OccupationGrid::is_occupied(int column_index, int row_index)
  1052. {
  1053. return m_occupation_grid[row_index][column_index];
  1054. }
  1055. }