FlexFormattingContext.cpp 33 KB

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