FlexFormattingContext.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. // Skip any "out-of-flow" children
  231. if (child_box.is_out_of_flow(*this))
  232. return IterationDecision::Continue;
  233. child_box.set_flex_item(true);
  234. flex_items.append({ child_box });
  235. return IterationDecision::Continue;
  236. });
  237. // 2. Determine the available main and cross space for the flex items
  238. float main_available_size = 0;
  239. [[maybe_unused]] float cross_available_size = 0;
  240. [[maybe_unused]] float main_max_size = NumericLimits<float>::max();
  241. [[maybe_unused]] float main_min_size = 0;
  242. float cross_max_size = NumericLimits<float>::max();
  243. float cross_min_size = 0;
  244. bool main_is_constrained = false;
  245. bool cross_is_constrained = false;
  246. if (has_definite_main_size(box)) {
  247. main_is_constrained = true;
  248. main_available_size = specified_main_size(box);
  249. } else {
  250. if (has_main_max_size(box)) {
  251. main_max_size = specified_main_max_size(box);
  252. main_is_constrained = true;
  253. }
  254. if (has_main_min_size(box)) {
  255. main_min_size = specified_main_min_size(box);
  256. main_is_constrained = true;
  257. }
  258. if (!main_is_constrained) {
  259. auto available_main_size = containing_block_effective_main_size(box);
  260. main_available_size = available_main_size - sum_of_margin_padding_border_in_main_axis(box);
  261. }
  262. }
  263. if (has_definite_cross_size(box)) {
  264. cross_available_size = specified_cross_size(box);
  265. } else {
  266. if (has_cross_max_size(box)) {
  267. cross_max_size = specified_cross_max_size(box);
  268. cross_is_constrained = true;
  269. }
  270. if (has_cross_min_size(box)) {
  271. cross_min_size = specified_cross_min_size(box);
  272. cross_is_constrained = true;
  273. }
  274. // FIXME: Is this right? Probably not.
  275. if (!cross_is_constrained)
  276. cross_available_size = cross_max_size;
  277. }
  278. // 3. Determine the flex base size and hypothetical main size of each item
  279. for (auto& flex_item : flex_items) {
  280. auto& child_box = flex_item.box;
  281. auto flex_basis = child_box.computed_values().flex_basis();
  282. if (flex_basis.type == CSS::FlexBasis::Length) {
  283. // A
  284. flex_item.flex_base_size = get_pixel_size(child_box, flex_basis.length);
  285. } else if (flex_basis.type == CSS::FlexBasis::Content
  286. && has_definite_cross_size(child_box)
  287. // FIXME: && has intrinsic aspect ratio.
  288. && false) {
  289. // B
  290. TODO();
  291. // flex_base_size is calculated from definite cross size and intrinsic aspect ratio
  292. } else if (flex_basis.type == CSS::FlexBasis::Content
  293. // FIXME: && sized under min-content or max-content contstraints
  294. && false) {
  295. // C
  296. TODO();
  297. // Size child_box under the constraints, flex_base_size is then the resulting main_size.
  298. } else if (flex_basis.type == CSS::FlexBasis::Content
  299. // FIXME: && main_size is infinite && inline axis is parallel to the main axis
  300. && false && false) {
  301. // D
  302. TODO();
  303. // Use rules for a box in orthogonal flow
  304. } else {
  305. // E
  306. // FIXME: This is probably too naive.
  307. // FIXME: Care about FlexBasis::Auto
  308. if (has_definite_main_size(child_box)) {
  309. flex_item.flex_base_size = specified_main_size_of_child_box(box, child_box);
  310. } else {
  311. layout_for_maximum_main_size(child_box);
  312. flex_item.flex_base_size = calculated_main_size(child_box);
  313. }
  314. }
  315. auto clamp_min = has_main_min_size(child_box)
  316. ? specified_main_min_size(child_box)
  317. : 0;
  318. auto clamp_max = has_main_max_size(child_box)
  319. ? specified_main_max_size(child_box)
  320. : NumericLimits<float>::max();
  321. flex_item.hypothetical_main_size = clamp(flex_item.flex_base_size, clamp_min, clamp_max);
  322. }
  323. // 4. Determine the main size of the flex container
  324. if (!main_is_constrained || main_available_size == 0) {
  325. // Uses https://www.w3.org/TR/css-flexbox-1/#intrinsic-main-sizes
  326. // 9.9.1
  327. // 1.
  328. float largest_max_content_flex_fraction = 0;
  329. for (auto& flex_item : flex_items) {
  330. // FIXME: This needs some serious work.
  331. float max_content_contribution = calculated_main_size(flex_item.box);
  332. float max_content_flex_fraction = max_content_contribution - flex_item.flex_base_size;
  333. if (max_content_flex_fraction > 0) {
  334. max_content_flex_fraction /= max(flex_item.box.computed_values().flex_grow_factor().value_or(1), 1.0f);
  335. } else {
  336. max_content_flex_fraction /= max(flex_item.box.computed_values().flex_shrink_factor().value_or(1), 1.0f) * flex_item.flex_base_size;
  337. }
  338. flex_item.max_content_flex_fraction = max_content_flex_fraction;
  339. if (max_content_flex_fraction > largest_max_content_flex_fraction)
  340. largest_max_content_flex_fraction = max_content_flex_fraction;
  341. }
  342. // 2. Omitted
  343. // 3.
  344. float result = 0;
  345. for (auto& flex_item : flex_items) {
  346. auto product = 0;
  347. if (flex_item.max_content_flex_fraction > 0) {
  348. product = largest_max_content_flex_fraction * flex_item.box.computed_values().flex_grow_factor().value_or(1);
  349. } else {
  350. 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;
  351. }
  352. result += flex_item.flex_base_size + product;
  353. }
  354. main_available_size = clamp(result, main_min_size, main_max_size);
  355. }
  356. set_main_size(box, main_available_size);
  357. // 5. Collect flex items into flex lines:
  358. // After this step no additional items are to be added to flex_lines or any of its items!
  359. Vector<FlexLine> flex_lines;
  360. // FIXME: Also support wrap-reverse
  361. if (box.computed_values().flex_wrap() == CSS::FlexWrap::Nowrap) {
  362. FlexLine line;
  363. for (auto& flex_item : flex_items) {
  364. line.items.append(&flex_item);
  365. }
  366. flex_lines.append(line);
  367. } else {
  368. FlexLine line;
  369. float line_main_size = 0;
  370. for (auto& flex_item : flex_items) {
  371. if ((line_main_size + flex_item.hypothetical_main_size) > main_available_size) {
  372. flex_lines.append(line);
  373. line = {};
  374. line_main_size = 0;
  375. }
  376. line.items.append(&flex_item);
  377. line_main_size += flex_item.hypothetical_main_size;
  378. }
  379. flex_lines.append(line);
  380. }
  381. // 6. Resolve the flexible lengths https://www.w3.org/TR/css-flexbox-1/#resolve-flexible-lengths
  382. enum FlexFactor {
  383. FlexGrowFactor,
  384. FlexShrinkFactor
  385. };
  386. FlexFactor used_flex_factor;
  387. // 6.1. Determine used flex factor
  388. for (auto& flex_line : flex_lines) {
  389. size_t number_of_unfrozen_items_on_line = flex_line.items.size();
  390. float sum_of_hypothetical_main_sizes = 0;
  391. for (auto& flex_item : flex_line.items) {
  392. sum_of_hypothetical_main_sizes += flex_item->hypothetical_main_size;
  393. }
  394. if (sum_of_hypothetical_main_sizes < main_available_size)
  395. used_flex_factor = FlexFactor::FlexGrowFactor;
  396. else
  397. used_flex_factor = FlexFactor::FlexShrinkFactor;
  398. for (auto& flex_item : flex_line.items) {
  399. if (used_flex_factor == FlexFactor::FlexGrowFactor)
  400. flex_item->flex_factor = flex_item->box.computed_values().flex_grow_factor();
  401. else if (used_flex_factor == FlexFactor::FlexShrinkFactor)
  402. flex_item->flex_factor = flex_item->box.computed_values().flex_shrink_factor();
  403. }
  404. // 6.2. Size inflexible items
  405. auto freeze_item_setting_target_main_size_to_hypothetical_main_size = [&number_of_unfrozen_items_on_line](FlexItem& item) {
  406. item.target_main_size = item.hypothetical_main_size;
  407. number_of_unfrozen_items_on_line--;
  408. item.frozen = true;
  409. };
  410. for (auto& flex_item : flex_line.items) {
  411. if (flex_item->flex_factor.has_value() && flex_item->flex_factor.value() == 0) {
  412. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  413. } else if (flex_item->flex_factor.has_value()) {
  414. // FIXME: This isn't spec
  415. continue;
  416. } else if (used_flex_factor == FlexFactor::FlexGrowFactor) {
  417. // FIXME: Spec doesn't include the == case, but we take a too basic approach to calculating the values used so this is appropriate
  418. if (flex_item->flex_base_size >= flex_item->hypothetical_main_size) {
  419. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  420. }
  421. } else if (used_flex_factor == FlexFactor::FlexShrinkFactor) {
  422. if (flex_item->flex_base_size < flex_item->hypothetical_main_size) {
  423. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  424. }
  425. }
  426. }
  427. // 6.3. Calculate initial free space
  428. auto calculate_free_space = [&]() {
  429. float sum_of_items_on_line = 0;
  430. for (auto& flex_item : flex_line.items) {
  431. if (flex_item->frozen)
  432. sum_of_items_on_line += flex_item->target_main_size;
  433. else
  434. sum_of_items_on_line += flex_item->flex_base_size;
  435. }
  436. return main_available_size - sum_of_items_on_line;
  437. };
  438. float initial_free_space = calculate_free_space();
  439. // 6.4 Loop
  440. auto for_each_unfrozen_item = [&flex_line](auto callback) {
  441. for (auto& flex_item : flex_line.items) {
  442. if (!flex_item->frozen)
  443. callback(flex_item);
  444. }
  445. };
  446. while (number_of_unfrozen_items_on_line > 0) {
  447. // b Calculate the remaining free space
  448. auto remaining_free_space = calculate_free_space();
  449. float sum_of_unfrozen_flex_items_flex_factors = 0;
  450. for_each_unfrozen_item([&](FlexItem* item) {
  451. sum_of_unfrozen_flex_items_flex_factors += item->flex_factor.value_or(1);
  452. });
  453. if (sum_of_unfrozen_flex_items_flex_factors < 1) {
  454. auto intermediate_free_space = initial_free_space * sum_of_unfrozen_flex_items_flex_factors;
  455. if (AK::abs(intermediate_free_space) < AK::abs(remaining_free_space))
  456. remaining_free_space = intermediate_free_space;
  457. }
  458. // c Distribute free space proportional to the flex factors
  459. if (remaining_free_space != 0) {
  460. if (used_flex_factor == FlexFactor::FlexGrowFactor) {
  461. float sum_of_flex_grow_factor_of_unfrozen_items = sum_of_unfrozen_flex_items_flex_factors;
  462. for_each_unfrozen_item([&](FlexItem* flex_item) {
  463. float ratio = flex_item->flex_factor.value_or(1) / sum_of_flex_grow_factor_of_unfrozen_items;
  464. flex_item->target_main_size = flex_item->flex_base_size + (remaining_free_space * ratio);
  465. });
  466. } else if (used_flex_factor == FlexFactor::FlexShrinkFactor) {
  467. float sum_of_scaled_flex_shrink_factor_of_unfrozen_items = 0;
  468. for_each_unfrozen_item([&](FlexItem* flex_item) {
  469. flex_item->scaled_flex_shrink_factor = flex_item->flex_factor.value_or(1) * flex_item->flex_base_size;
  470. sum_of_scaled_flex_shrink_factor_of_unfrozen_items += flex_item->scaled_flex_shrink_factor;
  471. });
  472. for_each_unfrozen_item([&](FlexItem* flex_item) {
  473. float ratio = 1.0f;
  474. if (sum_of_scaled_flex_shrink_factor_of_unfrozen_items != 0.0f)
  475. ratio = flex_item->scaled_flex_shrink_factor / sum_of_scaled_flex_shrink_factor_of_unfrozen_items;
  476. flex_item->target_main_size = flex_item->flex_base_size - (AK::abs(remaining_free_space) * ratio);
  477. });
  478. }
  479. } else {
  480. // This isn't spec but makes sense.
  481. for_each_unfrozen_item([&](FlexItem* flex_item) {
  482. flex_item->target_main_size = flex_item->flex_base_size;
  483. });
  484. }
  485. // d Fix min/max violations.
  486. float adjustments = 0.0f;
  487. for_each_unfrozen_item([&](FlexItem* item) {
  488. auto min_main = has_main_min_size(item->box)
  489. ? specified_main_min_size(item->box)
  490. : 0;
  491. auto max_main = has_main_max_size(item->box)
  492. ? specified_main_max_size(item->box)
  493. : NumericLimits<float>::max();
  494. float original_target_size = item->target_main_size;
  495. if (item->target_main_size < min_main) {
  496. item->target_main_size = min_main;
  497. item->is_min_violation = true;
  498. }
  499. if (item->target_main_size > max_main) {
  500. item->target_main_size = max_main;
  501. item->is_max_violation = true;
  502. }
  503. float delta = item->target_main_size - original_target_size;
  504. adjustments += delta;
  505. });
  506. // e Freeze over-flexed items
  507. float total_violation = adjustments;
  508. if (total_violation == 0) {
  509. for_each_unfrozen_item([&](FlexItem* item) {
  510. --number_of_unfrozen_items_on_line;
  511. item->frozen = true;
  512. });
  513. } else if (total_violation > 0) {
  514. for_each_unfrozen_item([&](FlexItem* item) {
  515. if (item->is_min_violation) {
  516. --number_of_unfrozen_items_on_line;
  517. item->frozen = true;
  518. }
  519. });
  520. } else if (total_violation < 0) {
  521. for_each_unfrozen_item([&](FlexItem* item) {
  522. if (item->is_max_violation) {
  523. --number_of_unfrozen_items_on_line;
  524. item->frozen = true;
  525. }
  526. });
  527. }
  528. }
  529. // 6.5.
  530. for (auto& flex_item : flex_line.items) {
  531. flex_item->main_size = flex_item->target_main_size;
  532. };
  533. }
  534. // Cross Size Determination
  535. // 7. Determine the hypothetical cross size of each item
  536. for (auto& flex_item : flex_items) {
  537. flex_item.hypothetical_cross_size = calculate_hypothetical_cross_size(flex_item.box);
  538. }
  539. // 8. Calculate the cross size of each flex line.
  540. if (flex_lines.size() == 1 && has_definite_cross_size(box)) {
  541. flex_lines[0].cross_size = specified_cross_size(box);
  542. } else {
  543. for (auto& flex_line : flex_lines) {
  544. // FIXME: Implement 8.1
  545. // 8.2
  546. float largest_hypothetical_cross_size = 0;
  547. for (auto& flex_item : flex_line.items) {
  548. if (largest_hypothetical_cross_size < flex_item->hypothetical_cross_size)
  549. largest_hypothetical_cross_size = flex_item->hypothetical_cross_size;
  550. }
  551. // 8.3
  552. flex_line.cross_size = max(0.0f, largest_hypothetical_cross_size);
  553. }
  554. if (flex_lines.size() == 1) {
  555. clamp(flex_lines[0].cross_size, cross_min_size, cross_max_size);
  556. }
  557. }
  558. // 9. Handle 'align-content: stretch'.
  559. // FIXME: This
  560. // 10. Collapse visibility:collapse items.
  561. // FIXME: This
  562. // 11. Determine the used cross size of each flex item.
  563. // FIXME: align-stretch
  564. for (auto& flex_line : flex_lines) {
  565. for (auto& flex_item : flex_line.items) {
  566. if (is_cross_auto(flex_item->box)) {
  567. // FIXME: Take margins into account
  568. flex_item->cross_size = flex_line.cross_size;
  569. } else {
  570. flex_item->cross_size = flex_item->hypothetical_cross_size;
  571. }
  572. }
  573. }
  574. // 12. Distribute any remaining free space.
  575. for (auto& flex_line : flex_lines) {
  576. // 12.1.
  577. float used_main_space = 0;
  578. size_t auto_margins = 0;
  579. for (auto& flex_item : flex_line.items) {
  580. used_main_space += flex_item->main_size;
  581. if (is_main_axis_margin_first_auto(flex_item->box))
  582. ++auto_margins;
  583. if (is_main_axis_margin_second_auto(flex_item->box))
  584. ++auto_margins;
  585. }
  586. float remaining_free_space = main_available_size - used_main_space;
  587. if (remaining_free_space > 0) {
  588. float size_per_auto_margin = remaining_free_space / (float)auto_margins;
  589. for (auto& flex_item : flex_line.items) {
  590. if (is_main_axis_margin_first_auto(flex_item->box))
  591. set_main_axis_first_margin(flex_item->box, size_per_auto_margin);
  592. if (is_main_axis_margin_second_auto(flex_item->box))
  593. set_main_axis_second_margin(flex_item->box, size_per_auto_margin);
  594. }
  595. } else {
  596. for (auto& flex_item : flex_line.items) {
  597. if (is_main_axis_margin_first_auto(flex_item->box))
  598. set_main_axis_first_margin(flex_item->box, 0);
  599. if (is_main_axis_margin_second_auto(flex_item->box))
  600. set_main_axis_second_margin(flex_item->box, 0);
  601. }
  602. }
  603. // 12.2.
  604. float space_between_items = 0;
  605. float space_before_first_item = 0;
  606. auto number_of_items = flex_line.items.size();
  607. switch (box.computed_values().justify_content()) {
  608. case CSS::JustifyContent::FlexStart:
  609. break;
  610. case CSS::JustifyContent::FlexEnd:
  611. space_before_first_item = main_available_size - used_main_space;
  612. break;
  613. case CSS::JustifyContent::Center:
  614. space_before_first_item = (main_available_size - used_main_space) / 2.0f;
  615. break;
  616. case CSS::JustifyContent::SpaceBetween:
  617. space_between_items = remaining_free_space / (number_of_items - 1);
  618. break;
  619. case CSS::JustifyContent::SpaceAround:
  620. space_between_items = remaining_free_space / number_of_items;
  621. space_before_first_item = space_between_items / 2.0f;
  622. break;
  623. }
  624. // FIXME: Support reverse
  625. float main_offset = space_before_first_item;
  626. for (auto& flex_item : flex_line.items) {
  627. flex_item->main_offset = main_offset;
  628. main_offset += flex_item->main_size + space_between_items;
  629. }
  630. }
  631. // 13. Resolve cross-axis auto margins.
  632. // FIXME: This
  633. // 14. Align all flex items along the cross-axis
  634. // FIXME: Support align-self
  635. // 15. Determine the flex container’s used cross size:
  636. if (has_definite_cross_size(box)) {
  637. float clamped_cross_size = clamp(specified_cross_size(box), cross_min_size, cross_max_size);
  638. set_cross_size(box, clamped_cross_size);
  639. } else {
  640. float sum_of_flex_lines_cross_sizes = 0;
  641. for (auto& flex_line : flex_lines) {
  642. sum_of_flex_lines_cross_sizes += flex_line.cross_size;
  643. }
  644. float clamped_cross_size = clamp(sum_of_flex_lines_cross_sizes, cross_min_size, cross_max_size);
  645. set_cross_size(box, clamped_cross_size);
  646. }
  647. // 16. Align all flex lines
  648. // FIXME: Support align-content
  649. // FIXME: Support reverse
  650. float cross_offset = 0;
  651. for (auto& flex_line : flex_lines) {
  652. for (auto* flex_item : flex_line.items) {
  653. flex_item->cross_offset = cross_offset;
  654. }
  655. cross_offset += flex_line.cross_size;
  656. }
  657. for (auto& flex_line : flex_lines) {
  658. for (auto* flex_item : flex_line.items) {
  659. set_main_size(flex_item->box, flex_item->main_size);
  660. set_cross_size(flex_item->box, flex_item->cross_size);
  661. set_offset(flex_item->box, flex_item->main_offset, flex_item->cross_offset);
  662. }
  663. }
  664. }
  665. }