GridFormattingContext.cpp 76 KB

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