GridFormattingContext.cpp 65 KB

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