FlexFormattingContext.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobi@tobyase.de>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StdLibExtras.h>
  8. #include <LibWeb/Layout/BlockBox.h>
  9. #include <LibWeb/Layout/BlockFormattingContext.h>
  10. #include <LibWeb/Layout/Box.h>
  11. #include <LibWeb/Layout/FlexFormattingContext.h>
  12. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  13. #include <LibWeb/Layout/TextNode.h>
  14. namespace Web::Layout {
  15. FlexFormattingContext::FlexFormattingContext(Box& context_box, FormattingContext* parent)
  16. : FormattingContext(context_box, parent)
  17. {
  18. }
  19. FlexFormattingContext::~FlexFormattingContext()
  20. {
  21. }
  22. struct FlexItem {
  23. Box& box;
  24. float flex_base_size { 0 };
  25. float hypothetical_main_size { 0 };
  26. float hypothetical_cross_size { 0 };
  27. float target_main_size { 0 };
  28. bool frozen { false };
  29. Optional<float> flex_factor {};
  30. float scaled_flex_shrink_factor { 0 };
  31. float max_content_flex_fraction { 0 };
  32. float main_size { 0 };
  33. float cross_size { 0 };
  34. float main_offset { 0 };
  35. float cross_offset { 0 };
  36. bool is_min_violation { false };
  37. bool is_max_violation { false };
  38. };
  39. struct FlexLine {
  40. Vector<FlexItem*> items;
  41. float cross_size { 0 };
  42. };
  43. void FlexFormattingContext::run(Box& box, LayoutMode)
  44. {
  45. // This implements https://www.w3.org/TR/css-flexbox-1/#layout-algorithm
  46. // FIXME: Implement reverse and ordering.
  47. // Determine main/cross direction
  48. auto flex_direction = box.computed_values().flex_direction();
  49. auto is_row = (flex_direction == CSS::FlexDirection::Row || flex_direction == CSS::FlexDirection::RowReverse);
  50. auto main_size_is_infinite = false;
  51. auto get_pixel_size = [](Box& box, const CSS::Length& length) {
  52. return length.resolved(CSS::Length::make_px(0), box, box.containing_block()->width()).to_px(box);
  53. };
  54. auto layout_for_maximum_main_size = [&](Box& box) {
  55. if (is_row)
  56. layout_inside(box, LayoutMode::OnlyRequiredLineBreaks);
  57. else
  58. layout_inside(box, LayoutMode::AllPossibleLineBreaks);
  59. };
  60. auto containing_block_effective_main_size = [&is_row, &main_size_is_infinite](Box& box) {
  61. if (is_row) {
  62. if (box.containing_block()->has_definite_width())
  63. return box.containing_block()->width();
  64. main_size_is_infinite = true;
  65. return NumericLimits<float>::max();
  66. } else {
  67. if (box.containing_block()->has_definite_height())
  68. return box.containing_block()->height();
  69. main_size_is_infinite = true;
  70. return NumericLimits<float>::max();
  71. }
  72. };
  73. auto has_definite_main_size = [&is_row](Box& box) {
  74. return is_row ? box.has_definite_width() : box.has_definite_height();
  75. };
  76. auto has_definite_cross_size = [&is_row](Box& box) {
  77. return is_row ? box.has_definite_height() : box.has_definite_width();
  78. };
  79. auto specified_main_size = [&is_row, &get_pixel_size](Box& box) {
  80. return is_row
  81. ? get_pixel_size(box, box.computed_values().width())
  82. : get_pixel_size(box, box.computed_values().height());
  83. };
  84. auto specified_cross_size = [&is_row, &get_pixel_size](Box& box) {
  85. return is_row
  86. ? get_pixel_size(box, box.computed_values().height())
  87. : get_pixel_size(box, box.computed_values().width());
  88. };
  89. auto has_main_min_size = [&is_row](Box& box) {
  90. return is_row
  91. ? !box.computed_values().min_width().is_undefined_or_auto()
  92. : !box.computed_values().min_height().is_undefined_or_auto();
  93. };
  94. auto has_cross_min_size = [&is_row](Box& box) {
  95. return is_row
  96. ? !box.computed_values().min_height().is_undefined_or_auto()
  97. : !box.computed_values().min_width().is_undefined_or_auto();
  98. };
  99. auto specified_main_min_size = [&is_row, &get_pixel_size](Box& box) {
  100. return is_row
  101. ? get_pixel_size(box, box.computed_values().min_width())
  102. : get_pixel_size(box, box.computed_values().min_height());
  103. };
  104. auto specified_cross_min_size = [&is_row, &get_pixel_size](Box& box) {
  105. return is_row
  106. ? get_pixel_size(box, box.computed_values().min_height())
  107. : get_pixel_size(box, box.computed_values().min_width());
  108. };
  109. auto has_main_max_size = [&is_row](Box& box) {
  110. return is_row
  111. ? !box.computed_values().max_width().is_undefined_or_auto()
  112. : !box.computed_values().max_height().is_undefined_or_auto();
  113. };
  114. auto has_cross_max_size = [&is_row](Box& box) {
  115. return is_row
  116. ? !box.computed_values().max_height().is_undefined_or_auto()
  117. : !box.computed_values().max_width().is_undefined_or_auto();
  118. };
  119. auto specified_main_max_size = [&is_row, &get_pixel_size](Box& box) {
  120. return is_row
  121. ? get_pixel_size(box, box.computed_values().max_width())
  122. : get_pixel_size(box, box.computed_values().max_height());
  123. };
  124. auto specified_cross_max_size = [&is_row, &get_pixel_size](Box& box) {
  125. return is_row
  126. ? get_pixel_size(box, box.computed_values().max_height())
  127. : get_pixel_size(box, box.computed_values().max_width());
  128. };
  129. auto calculated_main_size = [&is_row](Box& box) {
  130. return is_row ? box.width() : box.height();
  131. };
  132. auto is_cross_auto = [&is_row](Box& box) {
  133. return is_row ? box.computed_values().height().is_auto() : box.computed_values().width().is_auto();
  134. };
  135. auto is_main_axis_margin_first_auto = [&is_row](Box& box) {
  136. return is_row ? box.computed_values().margin().left.is_auto() : box.computed_values().margin().top.is_auto();
  137. };
  138. auto is_main_axis_margin_second_auto = [&is_row](Box& box) {
  139. return is_row ? box.computed_values().margin().right.is_auto() : box.computed_values().margin().bottom.is_auto();
  140. };
  141. auto sum_of_margin_padding_border_in_main_axis = [&is_row](Box& box) {
  142. if (is_row) {
  143. return box.box_model().margin.left
  144. + box.box_model().margin.right
  145. + box.box_model().padding.left
  146. + box.box_model().padding.right
  147. + box.box_model().border.left
  148. + box.box_model().border.right;
  149. } else {
  150. return box.box_model().margin.top
  151. + box.box_model().margin.bottom
  152. + box.box_model().padding.top
  153. + box.box_model().padding.bottom
  154. + box.box_model().border.top
  155. + box.box_model().border.bottom;
  156. }
  157. };
  158. auto calculate_hypothetical_cross_size = [&is_row, this](Box& box) {
  159. // FIXME: Don't use BFC exclusively, there are more FormattingContexts.
  160. if (is_row) {
  161. return BlockFormattingContext::compute_theoretical_height(box);
  162. } else {
  163. // FIXME: This is very bad.
  164. BlockFormattingContext context(box, this);
  165. context.compute_width(box);
  166. return box.width();
  167. }
  168. };
  169. auto set_main_size = [&is_row](Box& box, float size) {
  170. if (is_row)
  171. box.set_width(size);
  172. else
  173. box.set_height(size);
  174. };
  175. auto set_cross_size = [&is_row](Box& box, float size) {
  176. if (is_row)
  177. box.set_height(size);
  178. else
  179. box.set_width(size);
  180. };
  181. auto set_offset = [&is_row](Box& box, float main_offset, float cross_offset) {
  182. if (is_row)
  183. box.set_offset(main_offset, cross_offset);
  184. else
  185. box.set_offset(cross_offset, main_offset);
  186. };
  187. auto set_main_axis_first_margin = [&is_row](Box& box, float margin) {
  188. if (is_row)
  189. box.box_model().margin.left = margin;
  190. else
  191. box.box_model().margin.top = margin;
  192. };
  193. auto set_main_axis_second_margin = [&is_row](Box& box, float margin) {
  194. if (is_row)
  195. box.box_model().margin.right = margin;
  196. else
  197. box.box_model().margin.bottom = margin;
  198. };
  199. // 1. Generate anonymous flex items
  200. // More like, sift through the already generated items.
  201. // After this step no items are to be added or removed from flex_items!
  202. // It holds every item we need to consider and there should be nothing in the following
  203. // calculations that could change that.
  204. // This is particularly important since we take references to the items stored in flex_items
  205. // later, whose addresses won't be stable if we added or removed any items.
  206. Vector<FlexItem> flex_items;
  207. box.for_each_child_of_type<Box>([&](Box& child_box) {
  208. layout_inside(child_box, LayoutMode::Default);
  209. // Skip anonymous text runs that are only whitespace.
  210. if (child_box.is_anonymous()) {
  211. bool contains_only_white_space = true;
  212. child_box.for_each_in_inclusive_subtree_of_type<TextNode>([&contains_only_white_space](auto& text_node) {
  213. if (!text_node.text_for_rendering().is_whitespace()) {
  214. contains_only_white_space = false;
  215. return IterationDecision::Break;
  216. }
  217. return IterationDecision::Continue;
  218. });
  219. if (contains_only_white_space)
  220. return IterationDecision::Continue;
  221. }
  222. child_box.set_flex_item(true);
  223. flex_items.append({ child_box });
  224. return IterationDecision::Continue;
  225. });
  226. // 2. Determine the available main and cross space for the flex items
  227. float main_available_size = 0;
  228. [[maybe_unused]] float cross_available_size = 0;
  229. [[maybe_unused]] float main_max_size = NumericLimits<float>::max();
  230. [[maybe_unused]] float main_min_size = 0;
  231. float cross_max_size = NumericLimits<float>::max();
  232. float cross_min_size = 0;
  233. bool main_is_constrained = false;
  234. bool cross_is_constrained = false;
  235. if (has_definite_main_size(box)) {
  236. main_is_constrained = true;
  237. main_available_size = specified_main_size(box);
  238. } else {
  239. if (has_main_max_size(box)) {
  240. main_max_size = specified_main_max_size(box);
  241. main_is_constrained = true;
  242. }
  243. if (has_main_min_size(box)) {
  244. main_min_size = specified_main_min_size(box);
  245. main_is_constrained = true;
  246. }
  247. if (!main_is_constrained) {
  248. auto available_main_size = containing_block_effective_main_size(box);
  249. main_available_size = available_main_size - sum_of_margin_padding_border_in_main_axis(box);
  250. }
  251. }
  252. if (has_definite_cross_size(box)) {
  253. cross_available_size = specified_cross_size(box);
  254. } else {
  255. if (has_cross_max_size(box)) {
  256. cross_max_size = specified_cross_max_size(box);
  257. cross_is_constrained = true;
  258. }
  259. if (has_cross_min_size(box)) {
  260. cross_min_size = specified_cross_min_size(box);
  261. cross_is_constrained = true;
  262. }
  263. // FIXME: Is this right? Probably not.
  264. if (!cross_is_constrained)
  265. cross_available_size = cross_max_size;
  266. }
  267. // 3. Determine the flex base size and hypothetical main size of each item
  268. for (auto& flex_item : flex_items) {
  269. auto& child_box = flex_item.box;
  270. auto flex_basis = child_box.computed_values().flex_basis();
  271. if (flex_basis.type == CSS::FlexBasis::Length) {
  272. // A
  273. flex_item.flex_base_size = get_pixel_size(child_box, flex_basis.length);
  274. } else if (flex_basis.type == CSS::FlexBasis::Content
  275. && has_definite_cross_size(child_box)
  276. // FIXME: && has intrinsic aspect ratio.
  277. && false) {
  278. // B
  279. TODO();
  280. // flex_base_size is calculated from definite cross size and intrinsic aspect ratio
  281. } else if (flex_basis.type == CSS::FlexBasis::Content
  282. // FIXME: && sized under min-content or max-content contstraints
  283. && false) {
  284. // C
  285. TODO();
  286. // Size child_box under the constraints, flex_base_size is then the resulting main_size.
  287. } else if (flex_basis.type == CSS::FlexBasis::Content
  288. // FIXME: && main_size is infinite && inline axis is parallel to the main axis
  289. && false && false) {
  290. // D
  291. TODO();
  292. // Use rules for a box in orthogonal flow
  293. } else {
  294. // E
  295. // FIXME: This is probably too naive.
  296. // FIXME: Care about FlexBasis::Auto
  297. if (has_definite_main_size(child_box)) {
  298. flex_item.flex_base_size = specified_main_size(child_box);
  299. } else {
  300. layout_for_maximum_main_size(child_box);
  301. flex_item.flex_base_size = calculated_main_size(child_box);
  302. }
  303. }
  304. auto clamp_min = has_main_min_size(child_box)
  305. ? specified_main_min_size(child_box)
  306. : 0;
  307. auto clamp_max = has_main_max_size(child_box)
  308. ? specified_main_max_size(child_box)
  309. : NumericLimits<float>::max();
  310. flex_item.hypothetical_main_size = clamp(flex_item.flex_base_size, clamp_min, clamp_max);
  311. }
  312. // 4. Determine the main size of the flex container
  313. if (!main_is_constrained || main_available_size == 0) {
  314. // Uses https://www.w3.org/TR/css-flexbox-1/#intrinsic-main-sizes
  315. // 9.9.1
  316. // 1.
  317. float largest_max_content_flex_fraction = 0;
  318. for (auto& flex_item : flex_items) {
  319. // FIXME: This needs some serious work.
  320. float max_content_contribution = calculated_main_size(flex_item.box);
  321. float max_content_flex_fraction = max_content_contribution - flex_item.flex_base_size;
  322. if (max_content_flex_fraction > 0) {
  323. max_content_flex_fraction /= max(flex_item.box.computed_values().flex_grow_factor().value_or(1), 1.0f);
  324. } else {
  325. max_content_flex_fraction /= max(flex_item.box.computed_values().flex_shrink_factor().value_or(1), 1.0f) * flex_item.flex_base_size;
  326. }
  327. flex_item.max_content_flex_fraction = max_content_flex_fraction;
  328. if (max_content_flex_fraction > largest_max_content_flex_fraction)
  329. largest_max_content_flex_fraction = max_content_flex_fraction;
  330. }
  331. // 2. Omitted
  332. // 3.
  333. float result = 0;
  334. for (auto& flex_item : flex_items) {
  335. auto product = 0;
  336. if (flex_item.max_content_flex_fraction > 0) {
  337. product = largest_max_content_flex_fraction * flex_item.box.computed_values().flex_grow_factor().value_or(1);
  338. } else {
  339. product = largest_max_content_flex_fraction * max(flex_item.box.computed_values().flex_shrink_factor().value_or(1), 1.0f) * flex_item.flex_base_size;
  340. }
  341. result += flex_item.flex_base_size + product;
  342. }
  343. main_available_size = clamp(result, main_min_size, main_max_size);
  344. }
  345. set_main_size(box, main_available_size);
  346. // 5. Collect flex items into flex lines:
  347. // After this step no additional items are to be added to flex_lines or any of its items!
  348. Vector<FlexLine> flex_lines;
  349. // FIXME: Also support wrap-reverse
  350. if (box.computed_values().flex_wrap() == CSS::FlexWrap::Nowrap) {
  351. FlexLine line;
  352. for (auto& flex_item : flex_items) {
  353. line.items.append(&flex_item);
  354. }
  355. flex_lines.append(line);
  356. } else {
  357. FlexLine line;
  358. float line_main_size = 0;
  359. for (auto& flex_item : flex_items) {
  360. if ((line_main_size + flex_item.hypothetical_main_size) > main_available_size) {
  361. flex_lines.append(line);
  362. line = {};
  363. line_main_size = 0;
  364. }
  365. line.items.append(&flex_item);
  366. line_main_size += flex_item.hypothetical_main_size;
  367. }
  368. flex_lines.append(line);
  369. }
  370. // 6. Resolve the flexible lengths
  371. enum FlexFactor {
  372. FlexGrowFactor,
  373. FlexShrinkFactor
  374. };
  375. FlexFactor used_flex_factor;
  376. // 6.1. Determine used flex factor
  377. for (auto& flex_line : flex_lines) {
  378. size_t number_of_unfrozen_items_on_line = flex_line.items.size();
  379. float sum_of_hypothetical_main_sizes = 0;
  380. for (auto& flex_item : flex_line.items) {
  381. sum_of_hypothetical_main_sizes += flex_item->hypothetical_main_size;
  382. }
  383. if (sum_of_hypothetical_main_sizes < main_available_size)
  384. used_flex_factor = FlexFactor::FlexGrowFactor;
  385. else
  386. used_flex_factor = FlexFactor::FlexShrinkFactor;
  387. for (auto& flex_item : flex_line.items) {
  388. if (used_flex_factor == FlexFactor::FlexGrowFactor)
  389. flex_item->flex_factor = flex_item->box.computed_values().flex_grow_factor();
  390. else if (used_flex_factor == FlexFactor::FlexShrinkFactor)
  391. flex_item->flex_factor = flex_item->box.computed_values().flex_shrink_factor();
  392. }
  393. // 6.2. Size inflexible items
  394. auto freeze_item_setting_target_main_size_to_hypothetical_main_size = [&number_of_unfrozen_items_on_line](FlexItem& item) {
  395. item.target_main_size = item.hypothetical_main_size;
  396. number_of_unfrozen_items_on_line--;
  397. item.frozen = true;
  398. };
  399. for (auto& flex_item : flex_line.items) {
  400. if (flex_item->flex_factor.has_value() && flex_item->flex_factor.value() == 0) {
  401. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  402. } else if (flex_item->flex_factor.has_value()) {
  403. // FIXME: This isn't spec
  404. continue;
  405. } else if (used_flex_factor == FlexFactor::FlexGrowFactor) {
  406. // FIXME: Spec doesn't include the == case, but we take a too basic approach to calculating the values used so this is appropriate
  407. if (flex_item->flex_base_size >= flex_item->hypothetical_main_size) {
  408. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  409. }
  410. } else if (used_flex_factor == FlexFactor::FlexShrinkFactor) {
  411. if (flex_item->flex_base_size < flex_item->hypothetical_main_size) {
  412. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  413. }
  414. }
  415. }
  416. // 6.3. Calculate initial free space
  417. auto calculate_free_space = [&]() {
  418. float sum_of_items_on_line = 0;
  419. for (auto& flex_item : flex_line.items) {
  420. if (flex_item->frozen)
  421. sum_of_items_on_line += flex_item->target_main_size;
  422. else
  423. sum_of_items_on_line += flex_item->flex_base_size;
  424. }
  425. return main_available_size - sum_of_items_on_line;
  426. };
  427. float initial_free_space = calculate_free_space();
  428. // 6.4 Loop
  429. auto for_each_unfrozen_item = [&flex_line](auto callback) {
  430. for (auto& flex_item : flex_line.items) {
  431. if (!flex_item->frozen)
  432. callback(flex_item);
  433. }
  434. };
  435. while (number_of_unfrozen_items_on_line > 0) {
  436. // b Calculate the remaining free space
  437. auto remaining_free_space = calculate_free_space();
  438. float sum_of_unfrozen_flex_items_flex_factors = 0;
  439. for_each_unfrozen_item([&](FlexItem* item) {
  440. sum_of_unfrozen_flex_items_flex_factors += item->flex_factor.value_or(1);
  441. });
  442. if (sum_of_unfrozen_flex_items_flex_factors < 1) {
  443. auto intermediate_free_space = initial_free_space * sum_of_unfrozen_flex_items_flex_factors;
  444. if (AK::abs(intermediate_free_space) < AK::abs(remaining_free_space))
  445. remaining_free_space = intermediate_free_space;
  446. }
  447. // c Distribute free space proportional to the flex factors
  448. if (remaining_free_space != 0) {
  449. if (used_flex_factor == FlexFactor::FlexGrowFactor) {
  450. float sum_of_flex_grow_factor_of_unfrozen_items = sum_of_unfrozen_flex_items_flex_factors;
  451. for_each_unfrozen_item([&](FlexItem* flex_item) {
  452. float ratio = flex_item->flex_factor.value_or(1) / sum_of_flex_grow_factor_of_unfrozen_items;
  453. flex_item->target_main_size = flex_item->flex_base_size + (remaining_free_space * ratio);
  454. });
  455. } else if (used_flex_factor == FlexFactor::FlexShrinkFactor) {
  456. float sum_of_scaled_flex_shrink_factor_of_unfrozen_items = 0;
  457. for_each_unfrozen_item([&](FlexItem* flex_item) {
  458. flex_item->scaled_flex_shrink_factor = flex_item->flex_factor.value_or(1) * flex_item->flex_base_size;
  459. sum_of_scaled_flex_shrink_factor_of_unfrozen_items += flex_item->scaled_flex_shrink_factor;
  460. });
  461. for_each_unfrozen_item([&](FlexItem* flex_item) {
  462. float ratio = flex_item->scaled_flex_shrink_factor / sum_of_scaled_flex_shrink_factor_of_unfrozen_items;
  463. flex_item->target_main_size = flex_item->flex_base_size - (AK::abs(remaining_free_space) * ratio);
  464. });
  465. }
  466. } else {
  467. // This isn't spec but makes sense.
  468. for_each_unfrozen_item([&](FlexItem* flex_item) {
  469. flex_item->target_main_size = flex_item->flex_base_size;
  470. });
  471. }
  472. // d Fix min/max violations.
  473. float adjustments = 0;
  474. for_each_unfrozen_item([&](FlexItem* item) {
  475. auto min_main = has_main_min_size(item->box)
  476. ? specified_main_min_size(item->box)
  477. : 0;
  478. auto max_main = has_main_max_size(item->box)
  479. ? specified_main_max_size(item->box)
  480. : NumericLimits<float>::max();
  481. float original_target_size = item->target_main_size;
  482. if (item->target_main_size < min_main) {
  483. item->target_main_size = min_main;
  484. item->is_min_violation = true;
  485. }
  486. if (item->target_main_size > max_main) {
  487. item->target_main_size = max_main;
  488. item->is_max_violation = true;
  489. }
  490. float delta = item->target_main_size - original_target_size;
  491. adjustments += delta;
  492. });
  493. // e Freeze over-flexed items
  494. if (adjustments == 0) {
  495. for_each_unfrozen_item([&](FlexItem* item) {
  496. --number_of_unfrozen_items_on_line;
  497. item->frozen = true;
  498. });
  499. } else if (adjustments > 0) {
  500. for_each_unfrozen_item([&](FlexItem* item) {
  501. if (item->is_min_violation) {
  502. --number_of_unfrozen_items_on_line;
  503. item->frozen = true;
  504. }
  505. });
  506. } else if (adjustments < 0) {
  507. for_each_unfrozen_item([&](FlexItem* item) {
  508. if (item->is_max_violation) {
  509. --number_of_unfrozen_items_on_line;
  510. item->frozen = true;
  511. }
  512. });
  513. }
  514. }
  515. // 6.5.
  516. for (auto& flex_item : flex_line.items) {
  517. flex_item->main_size = flex_item->target_main_size;
  518. };
  519. }
  520. // Cross Size Determination
  521. // 7. Determine the hypothetical cross size of each item
  522. for (auto& flex_item : flex_items) {
  523. flex_item.hypothetical_cross_size = calculate_hypothetical_cross_size(flex_item.box);
  524. }
  525. // 8. Calculate the cross size of each flex line.
  526. if (flex_lines.size() == 1 && has_definite_cross_size(box)) {
  527. flex_lines[0].cross_size = specified_cross_size(box);
  528. } else {
  529. for (auto& flex_line : flex_lines) {
  530. // FIXME: Implement 8.1
  531. // 8.2
  532. float largest_hypothetical_cross_size = 0;
  533. for (auto& flex_item : flex_line.items) {
  534. if (largest_hypothetical_cross_size < flex_item->hypothetical_cross_size)
  535. largest_hypothetical_cross_size = flex_item->hypothetical_cross_size;
  536. }
  537. // 8.3
  538. flex_line.cross_size = max(0.0f, largest_hypothetical_cross_size);
  539. }
  540. if (flex_lines.size() == 1) {
  541. clamp(flex_lines[0].cross_size, cross_min_size, cross_max_size);
  542. }
  543. }
  544. // 9. Handle 'align-content: stretch'.
  545. // FIXME: This
  546. // 10. Collapse visibility:collapse items.
  547. // FIXME: This
  548. // 11. Determine the used cross size of each flex item.
  549. // FIXME: align-stretch
  550. for (auto& flex_line : flex_lines) {
  551. for (auto& flex_item : flex_line.items) {
  552. if (is_cross_auto(flex_item->box)) {
  553. // FIXME: Take margins into account
  554. flex_item->cross_size = flex_line.cross_size;
  555. } else {
  556. flex_item->cross_size = flex_item->hypothetical_cross_size;
  557. }
  558. }
  559. }
  560. // 12. Distribute any remaining free space.
  561. for (auto& flex_line : flex_lines) {
  562. // 12.1.
  563. float used_main_space = 0;
  564. size_t auto_margins = 0;
  565. for (auto& flex_item : flex_line.items) {
  566. used_main_space += flex_item->cross_size;
  567. if (is_main_axis_margin_first_auto(flex_item->box))
  568. ++auto_margins;
  569. if (is_main_axis_margin_second_auto(flex_item->box))
  570. ++auto_margins;
  571. }
  572. float remaining_free_space = main_available_size - used_main_space;
  573. if (remaining_free_space > 0) {
  574. float size_per_auto_margin = remaining_free_space / (float)auto_margins;
  575. for (auto& flex_item : flex_line.items) {
  576. if (is_main_axis_margin_first_auto(flex_item->box))
  577. set_main_axis_first_margin(flex_item->box, size_per_auto_margin);
  578. if (is_main_axis_margin_second_auto(flex_item->box))
  579. set_main_axis_second_margin(flex_item->box, size_per_auto_margin);
  580. }
  581. } else {
  582. for (auto& flex_item : flex_line.items) {
  583. if (is_main_axis_margin_first_auto(flex_item->box))
  584. set_main_axis_first_margin(flex_item->box, 0);
  585. if (is_main_axis_margin_second_auto(flex_item->box))
  586. set_main_axis_second_margin(flex_item->box, 0);
  587. }
  588. }
  589. // 12.2.
  590. float space_between_items = 0;
  591. float space_before_first_item = 0;
  592. auto number_of_items = flex_line.items.size();
  593. switch (box.computed_values().justify_content()) {
  594. case CSS::JustifyContent::FlexStart:
  595. break;
  596. case CSS::JustifyContent::FlexEnd:
  597. space_before_first_item = main_available_size - used_main_space;
  598. break;
  599. case CSS::JustifyContent::Center:
  600. space_before_first_item = (main_available_size - used_main_space) / 2.0f;
  601. break;
  602. case CSS::JustifyContent::SpaceBetween:
  603. space_between_items = remaining_free_space / (number_of_items - 1);
  604. break;
  605. case CSS::JustifyContent::SpaceAround:
  606. space_between_items = remaining_free_space / number_of_items;
  607. space_before_first_item = space_between_items / 2.0f;
  608. break;
  609. }
  610. // FIXME: Support reverse
  611. float main_offset = space_before_first_item;
  612. for (auto& flex_item : flex_line.items) {
  613. flex_item->main_offset = main_offset;
  614. main_offset += flex_item->main_size + space_between_items;
  615. }
  616. }
  617. // 13. Resolve cross-axis auto margins.
  618. // FIXME: This
  619. // 14. Align all flex items along the cross-axis
  620. // FIXME: Support align-self
  621. // 15. Determine the flex container’s used cross size:
  622. if (has_definite_cross_size(box)) {
  623. float clamped_cross_size = clamp(specified_cross_size(box), cross_min_size, cross_max_size);
  624. set_cross_size(box, clamped_cross_size);
  625. } else {
  626. float sum_of_flex_lines_cross_sizes = 0;
  627. for (auto& flex_line : flex_lines) {
  628. sum_of_flex_lines_cross_sizes += flex_line.cross_size;
  629. }
  630. float clamped_cross_size = clamp(sum_of_flex_lines_cross_sizes, cross_min_size, cross_max_size);
  631. set_cross_size(box, clamped_cross_size);
  632. }
  633. // 16. Align all flex lines
  634. // FIXME: Support align-content
  635. // FIXME: Support reverse
  636. float cross_offset = 0;
  637. for (auto& flex_line : flex_lines) {
  638. for (auto* flex_item : flex_line.items) {
  639. flex_item->cross_offset = cross_offset;
  640. }
  641. cross_offset += flex_line.cross_size;
  642. }
  643. for (auto& flex_line : flex_lines) {
  644. for (auto* flex_item : flex_line.items) {
  645. set_main_size(flex_item->box, flex_item->main_size);
  646. set_cross_size(flex_item->box, flex_item->cross_size);
  647. set_offset(flex_item->box, flex_item->main_offset, flex_item->cross_offset);
  648. }
  649. }
  650. }
  651. }