GridFormattingContext.cpp 80 KB

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