GridFormattingContext.cpp 68 KB

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