FlexFormattingContext.cpp 29 KB

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