FlexFormattingContext.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*
  2. * Copyright (c) 2021-2022, 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/Function.h>
  9. #include <AK/StdLibExtras.h>
  10. #include <LibWeb/Layout/BlockContainer.h>
  11. #include <LibWeb/Layout/BlockFormattingContext.h>
  12. #include <LibWeb/Layout/Box.h>
  13. #include <LibWeb/Layout/FlexFormattingContext.h>
  14. #include <LibWeb/Layout/InitialContainingBlock.h>
  15. #include <LibWeb/Layout/TextNode.h>
  16. namespace Web::Layout {
  17. static float get_pixel_size(FormattingState const& state, Box const& box, Optional<CSS::LengthPercentage> const& length_percentage)
  18. {
  19. if (!length_percentage.has_value())
  20. return 0;
  21. auto inner_main_size = CSS::Length::make_px(state.get(*box.containing_block()).content_width);
  22. return length_percentage->resolved(box, inner_main_size).to_px(box);
  23. }
  24. static bool is_undefined_or_auto(Optional<CSS::LengthPercentage> const& length_percentage)
  25. {
  26. if (!length_percentage.has_value())
  27. return true;
  28. return length_percentage->is_length() && length_percentage->length().is_auto();
  29. }
  30. FlexFormattingContext::FlexFormattingContext(FormattingState& state, Box const& flex_container, FormattingContext* parent)
  31. : FormattingContext(Type::Flex, state, flex_container, parent)
  32. , m_flex_container_state(m_state.get_mutable(flex_container))
  33. , m_flex_direction(flex_container.computed_values().flex_direction())
  34. {
  35. }
  36. FlexFormattingContext::~FlexFormattingContext()
  37. {
  38. }
  39. void FlexFormattingContext::run(Box const& run_box, LayoutMode)
  40. {
  41. VERIFY(&run_box == &flex_container());
  42. // This implements https://www.w3.org/TR/css-flexbox-1/#layout-algorithm
  43. // FIXME: Implement reverse and ordering.
  44. // 1. Generate anonymous flex items
  45. generate_anonymous_flex_items();
  46. // 2. Determine the available main and cross space for the flex items
  47. float main_max_size = NumericLimits<float>::max();
  48. float main_min_size = 0;
  49. float cross_max_size = NumericLimits<float>::max();
  50. float cross_min_size = 0;
  51. bool main_is_constrained = false;
  52. bool cross_is_constrained = false;
  53. determine_available_main_and_cross_space(main_is_constrained, cross_is_constrained, main_min_size, main_max_size, cross_min_size, cross_max_size);
  54. // 3. Determine the flex base size and hypothetical main size of each item
  55. for (auto& flex_item : m_flex_items) {
  56. determine_flex_base_size_and_hypothetical_main_size(flex_item);
  57. }
  58. // 4. Determine the main size of the flex container
  59. determine_main_size_of_flex_container(main_is_constrained, main_min_size, main_max_size);
  60. // 5. Collect flex items into flex lines:
  61. // After this step no additional items are to be added to flex_lines or any of its items!
  62. collect_flex_items_into_flex_lines();
  63. // 6. Resolve the flexible lengths
  64. resolve_flexible_lengths();
  65. // Cross Size Determination
  66. // 7. Determine the hypothetical cross size of each item
  67. for (auto& flex_item : m_flex_items) {
  68. determine_hypothetical_cross_size_of_item(flex_item);
  69. }
  70. // 8. Calculate the cross size of each flex line.
  71. calculate_cross_size_of_each_flex_line(cross_min_size, cross_max_size);
  72. // 9. Handle 'align-content: stretch'.
  73. // FIXME: This
  74. // 10. Collapse visibility:collapse items.
  75. // FIXME: This
  76. // 11. Determine the used cross size of each flex item.
  77. determine_used_cross_size_of_each_flex_item();
  78. // 12. Distribute any remaining free space.
  79. distribute_any_remaining_free_space();
  80. // 13. Resolve cross-axis auto margins.
  81. // FIXME: This
  82. // 14. Align all flex items along the cross-axis
  83. align_all_flex_items_along_the_cross_axis();
  84. // 15. Determine the flex container’s used cross size:
  85. determine_flex_container_used_cross_size(cross_min_size, cross_max_size);
  86. // 16. Align all flex lines (per align-content)
  87. align_all_flex_lines();
  88. // AD-HOC: Layout the inside of all flex items.
  89. copy_dimensions_from_flex_items_to_boxes();
  90. for (auto& flex_item : m_flex_items) {
  91. auto independent_formatting_context = layout_inside(flex_item.box, LayoutMode::Default);
  92. independent_formatting_context->parent_context_did_dimension_child_root_box();
  93. }
  94. // FIXME: We run the "copy dimensions" step *again* here, in order to override any sizes
  95. // assigned to the flex item by the "layout inside" step above. This is definitely not
  96. // part of the spec, and simply covering up the fact that our inside layout currently
  97. // mutates the height of BFC roots.
  98. copy_dimensions_from_flex_items_to_boxes();
  99. }
  100. void FlexFormattingContext::populate_specified_margins(FlexItem& item, CSS::FlexDirection flex_direction) const
  101. {
  102. auto width_of_containing_block = m_state.get(*item.box.containing_block()).content_width;
  103. auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
  104. // FIXME: This should also take reverse-ness into account
  105. if (flex_direction == CSS::FlexDirection::Row || flex_direction == CSS::FlexDirection::RowReverse) {
  106. item.margins.main_before = item.box.computed_values().margin().left.resolved(item.box, width_of_containing_block_as_length).to_px(item.box);
  107. item.margins.main_after = item.box.computed_values().margin().right.resolved(item.box, width_of_containing_block_as_length).to_px(item.box);
  108. item.margins.cross_before = item.box.computed_values().margin().top.resolved(item.box, width_of_containing_block_as_length).to_px(item.box);
  109. item.margins.cross_after = item.box.computed_values().margin().bottom.resolved(item.box, width_of_containing_block_as_length).to_px(item.box);
  110. } else {
  111. item.margins.main_before = item.box.computed_values().margin().top.resolved(item.box, width_of_containing_block_as_length).to_px(item.box);
  112. item.margins.main_after = item.box.computed_values().margin().bottom.resolved(item.box, width_of_containing_block_as_length).to_px(item.box);
  113. item.margins.cross_before = item.box.computed_values().margin().left.resolved(item.box, width_of_containing_block_as_length).to_px(item.box);
  114. item.margins.cross_after = item.box.computed_values().margin().right.resolved(item.box, width_of_containing_block_as_length).to_px(item.box);
  115. }
  116. };
  117. // https://www.w3.org/TR/css-flexbox-1/#flex-items
  118. void FlexFormattingContext::generate_anonymous_flex_items()
  119. {
  120. // More like, sift through the already generated items.
  121. // After this step no items are to be added or removed from flex_items!
  122. // It holds every item we need to consider and there should be nothing in the following
  123. // calculations that could change that.
  124. // This is particularly important since we take references to the items stored in flex_items
  125. // later, whose addresses won't be stable if we added or removed any items.
  126. flex_container().for_each_child_of_type<Box>([&](Box& child_box) {
  127. // Skip anonymous text runs that are only whitespace.
  128. if (child_box.is_anonymous() && !child_box.first_child_of_type<BlockContainer>()) {
  129. bool contains_only_white_space = true;
  130. child_box.for_each_in_inclusive_subtree_of_type<TextNode>([&contains_only_white_space](auto& text_node) {
  131. if (!text_node.dom_node().data().is_whitespace()) {
  132. contains_only_white_space = false;
  133. return IterationDecision::Break;
  134. }
  135. return IterationDecision::Continue;
  136. });
  137. if (contains_only_white_space)
  138. return IterationDecision::Continue;
  139. }
  140. // Skip any "out-of-flow" children
  141. if (child_box.is_out_of_flow(*this))
  142. return IterationDecision::Continue;
  143. child_box.set_flex_item(true);
  144. FlexItem flex_item = { child_box };
  145. populate_specified_margins(flex_item, m_flex_direction);
  146. m_flex_items.append(move(flex_item));
  147. return IterationDecision::Continue;
  148. });
  149. }
  150. bool FlexFormattingContext::has_definite_main_size(Box const& box) const
  151. {
  152. return is_row_layout() ? box.has_definite_width() : box.has_definite_height();
  153. }
  154. float FlexFormattingContext::specified_main_size(Box const& box) const
  155. {
  156. auto const& box_state = m_state.get(box);
  157. return is_row_layout() ? box_state.content_width : box_state.content_height;
  158. }
  159. float FlexFormattingContext::specified_cross_size(Box const& box) const
  160. {
  161. auto const& box_state = m_state.get(box);
  162. return is_row_layout() ? box_state.content_height : box_state.content_width;
  163. }
  164. float FlexFormattingContext::resolved_definite_cross_size(Box const& box) const
  165. {
  166. if (is_row_layout())
  167. VERIFY(box.has_definite_height());
  168. else
  169. VERIFY(box.has_definite_width());
  170. auto const& cross_value = is_row_layout() ? box.computed_values().height() : box.computed_values().width();
  171. if (cross_value->is_length())
  172. return cross_value->length().to_px(box);
  173. return cross_value->resolved(box, CSS::Length::make_px(specified_cross_size(flex_container()))).to_px(box);
  174. }
  175. float FlexFormattingContext::resolved_definite_main_size(Box const& box) const
  176. {
  177. if (is_row_layout())
  178. VERIFY(box.has_definite_width());
  179. else
  180. VERIFY(box.has_definite_height());
  181. auto const& cross_value = is_row_layout() ? box.computed_values().width() : box.computed_values().height();
  182. if (cross_value->is_length())
  183. return cross_value->length().to_px(box);
  184. return cross_value->resolved(box, CSS::Length::make_px(specified_main_size(flex_container()))).to_px(box);
  185. }
  186. bool FlexFormattingContext::has_main_min_size(Box const& box) const
  187. {
  188. auto value = is_row_layout() ? box.computed_values().min_width() : box.computed_values().min_height();
  189. return !is_undefined_or_auto(value);
  190. }
  191. bool FlexFormattingContext::has_cross_min_size(Box const& box) const
  192. {
  193. auto value = is_row_layout() ? box.computed_values().min_height() : box.computed_values().min_width();
  194. return !is_undefined_or_auto(value);
  195. }
  196. bool FlexFormattingContext::has_definite_cross_size(Box const& box) const
  197. {
  198. return is_row_layout() ? box.has_definite_height() : box.has_definite_width();
  199. }
  200. float FlexFormattingContext::specified_main_size_of_child_box(Box const& child_box) const
  201. {
  202. auto main_size_of_parent = specified_main_size(flex_container());
  203. auto& value = is_row_layout() ? child_box.computed_values().width() : child_box.computed_values().height();
  204. if (!value.has_value())
  205. return 0;
  206. return value->resolved(child_box, CSS::Length::make_px(main_size_of_parent)).to_px(child_box);
  207. }
  208. float FlexFormattingContext::specified_main_min_size(Box const& box) const
  209. {
  210. return is_row_layout()
  211. ? get_pixel_size(m_state, box, box.computed_values().min_width())
  212. : get_pixel_size(m_state, box, box.computed_values().min_height());
  213. }
  214. float FlexFormattingContext::specified_cross_min_size(Box const& box) const
  215. {
  216. return is_row_layout()
  217. ? get_pixel_size(m_state, box, box.computed_values().min_height())
  218. : get_pixel_size(m_state, box, box.computed_values().min_width());
  219. }
  220. bool FlexFormattingContext::has_main_max_size(Box const& box) const
  221. {
  222. return is_row_layout()
  223. ? !is_undefined_or_auto(box.computed_values().max_width())
  224. : !is_undefined_or_auto(box.computed_values().max_height());
  225. }
  226. bool FlexFormattingContext::has_cross_max_size(Box const& box) const
  227. {
  228. return is_row_layout()
  229. ? !is_undefined_or_auto(box.computed_values().max_height())
  230. : !is_undefined_or_auto(box.computed_values().max_width());
  231. }
  232. float FlexFormattingContext::specified_main_max_size(Box const& box) const
  233. {
  234. return is_row_layout()
  235. ? get_pixel_size(m_state, box, box.computed_values().max_width())
  236. : get_pixel_size(m_state, box, box.computed_values().max_height());
  237. }
  238. float FlexFormattingContext::specified_cross_max_size(Box const& box) const
  239. {
  240. return is_row_layout()
  241. ? get_pixel_size(m_state, box, box.computed_values().max_height())
  242. : get_pixel_size(m_state, box, box.computed_values().max_width());
  243. }
  244. float FlexFormattingContext::calculated_main_size(Box const& box) const
  245. {
  246. auto const& box_state = m_state.get(box);
  247. return is_row_layout() ? box_state.content_width : box_state.content_height;
  248. }
  249. bool FlexFormattingContext::is_cross_auto(Box const& box) const
  250. {
  251. auto& cross_length = is_row_layout() ? box.computed_values().height() : box.computed_values().width();
  252. return cross_length.has_value() && cross_length->is_length() && cross_length->length().is_auto();
  253. }
  254. bool FlexFormattingContext::is_main_axis_margin_first_auto(Box const& box) const
  255. {
  256. if (is_row_layout())
  257. return box.computed_values().margin().left.is_length() && box.computed_values().margin().left.length().is_auto();
  258. return box.computed_values().margin().top.is_length() && box.computed_values().margin().top.length().is_auto();
  259. }
  260. bool FlexFormattingContext::is_main_axis_margin_second_auto(Box const& box) const
  261. {
  262. if (is_row_layout())
  263. return box.computed_values().margin().right.is_length() && box.computed_values().margin().right.length().is_auto();
  264. return box.computed_values().margin().bottom.is_length() && box.computed_values().margin().bottom.length().is_auto();
  265. }
  266. void FlexFormattingContext::set_main_size(Box const& box, float size)
  267. {
  268. if (is_row_layout())
  269. m_state.get_mutable(box).content_width = size;
  270. else
  271. m_state.get_mutable(box).content_height = size;
  272. }
  273. void FlexFormattingContext::set_cross_size(Box const& box, float size)
  274. {
  275. if (is_row_layout())
  276. m_state.get_mutable(box).content_height = size;
  277. else
  278. m_state.get_mutable(box).content_width = size;
  279. }
  280. void FlexFormattingContext::set_offset(Box const& box, float main_offset, float cross_offset)
  281. {
  282. if (is_row_layout())
  283. m_state.get_mutable(box).offset = Gfx::FloatPoint { main_offset, cross_offset };
  284. else
  285. m_state.get_mutable(box).offset = Gfx::FloatPoint { cross_offset, main_offset };
  286. }
  287. void FlexFormattingContext::set_main_axis_first_margin(Box const& box, float margin)
  288. {
  289. if (is_row_layout())
  290. m_state.get_mutable(box).margin_left = margin;
  291. else
  292. m_state.get_mutable(box).margin_top = margin;
  293. }
  294. void FlexFormattingContext::set_main_axis_second_margin(Box const& box, float margin)
  295. {
  296. if (is_row_layout())
  297. m_state.get_mutable(box).margin_right = margin;
  298. else
  299. m_state.get_mutable(box).margin_bottom = margin;
  300. }
  301. float FlexFormattingContext::sum_of_margin_padding_border_in_main_axis(Box const& box) const
  302. {
  303. auto const& box_state = m_state.get(box);
  304. if (is_row_layout()) {
  305. return box_state.margin_left + box_state.margin_right
  306. + box_state.padding_left + box_state.padding_right
  307. + box_state.border_left + box_state.border_right;
  308. } else {
  309. return box_state.margin_top + box_state.margin_bottom
  310. + box_state.padding_top + box_state.padding_bottom
  311. + box_state.border_top + box_state.border_bottom;
  312. }
  313. }
  314. // https://www.w3.org/TR/css-flexbox-1/#algo-available
  315. void FlexFormattingContext::determine_available_main_and_cross_space(bool& main_is_constrained, bool& cross_is_constrained, float& main_min_size, float& main_max_size, float& cross_min_size, float& cross_max_size)
  316. {
  317. auto containing_block_effective_main_size = [&](Box const& box) -> Optional<float> {
  318. auto& containing_block = *box.containing_block();
  319. if (has_definite_main_size(containing_block))
  320. return resolved_definite_main_size(containing_block);
  321. return {};
  322. };
  323. Optional<float> main_available_space;
  324. main_is_constrained = false;
  325. // For each dimension,
  326. // if that dimension of the flex container’s content box is a definite size, use that;
  327. // if that dimension of the flex container is being sized under a min or max-content constraint, the available space in that dimension is that constraint;
  328. // otherwise, subtract the flex container’s margin, border, and padding from the space available to the flex container in that dimension and use that value. (This might result in an infinite value.)
  329. if (has_definite_main_size(flex_container())) {
  330. main_is_constrained = true;
  331. main_available_space = specified_main_size(flex_container());
  332. } else {
  333. if (has_main_max_size(flex_container())) {
  334. main_max_size = specified_main_max_size(flex_container());
  335. main_available_space = main_max_size;
  336. main_is_constrained = true;
  337. }
  338. if (has_main_min_size(flex_container())) {
  339. main_min_size = specified_main_min_size(flex_container());
  340. main_is_constrained = true;
  341. }
  342. if (!main_is_constrained) {
  343. auto available_main_size = containing_block_effective_main_size(flex_container());
  344. main_available_space = available_main_size.value_or(NumericLimits<float>::max()) - sum_of_margin_padding_border_in_main_axis(flex_container());
  345. if (flex_container().computed_values().flex_wrap() == CSS::FlexWrap::Wrap || flex_container().computed_values().flex_wrap() == CSS::FlexWrap::WrapReverse) {
  346. main_available_space = specified_main_size(*flex_container().containing_block());
  347. main_is_constrained = true;
  348. }
  349. }
  350. }
  351. Optional<float> cross_available_space;
  352. cross_is_constrained = false;
  353. if (has_definite_cross_size(flex_container())) {
  354. cross_available_space = specified_cross_size(flex_container());
  355. } else {
  356. if (has_cross_max_size(flex_container())) {
  357. cross_max_size = specified_cross_max_size(flex_container());
  358. cross_is_constrained = true;
  359. }
  360. if (has_cross_min_size(flex_container())) {
  361. cross_min_size = specified_cross_min_size(flex_container());
  362. cross_is_constrained = true;
  363. }
  364. // FIXME: Is this right? Probably not.
  365. if (!cross_is_constrained)
  366. cross_available_space = cross_max_size;
  367. }
  368. m_available_space = AvailableSpace { .main = main_available_space, .cross = cross_available_space };
  369. }
  370. float FlexFormattingContext::calculate_indefinite_main_size(FlexItem const& item)
  371. {
  372. VERIFY(!has_definite_main_size(item.box));
  373. if (has_definite_cross_size(item.box)) {
  374. // For indefinite main sizes, we perform a throwaway layout and then measure it.
  375. FormattingState throwaway_state(&m_state);
  376. auto& box_state = throwaway_state.get_mutable(item.box);
  377. // Item has definite cross size, layout with that as the used cross size.
  378. auto independent_formatting_context = create_independent_formatting_context_if_needed(throwaway_state, item.box);
  379. // NOTE: Flex items should always create an independent formatting context!
  380. VERIFY(independent_formatting_context);
  381. if (is_row_layout()) {
  382. box_state.content_height = resolved_definite_cross_size(item.box);
  383. } else {
  384. box_state.content_width = resolved_definite_cross_size(item.box);
  385. }
  386. independent_formatting_context->run(item.box, LayoutMode::Default);
  387. if (is_row_layout())
  388. return box_state.content_width;
  389. return BlockFormattingContext::compute_theoretical_height(throwaway_state, item.box);
  390. }
  391. // Item has indefinite cross size, layout with "fit-content"
  392. // If we're in a row layout and looking for the width, just use the fit-content width.
  393. if (is_row_layout())
  394. return calculate_fit_content_width(item.box, m_available_space->main);
  395. // We're in a column layout, looking for the height. Figure out the fit-content width,
  396. // then layout with that and see what height comes out of it.
  397. float fit_content_cross_size = calculate_fit_content_width(item.box, m_available_space->cross);
  398. FormattingState throwaway_state(&m_state);
  399. auto& box_state = throwaway_state.get_mutable(item.box);
  400. // Item has definite cross size, layout with that as the used cross size.
  401. auto independent_formatting_context = create_independent_formatting_context_if_needed(throwaway_state, item.box);
  402. // NOTE: Flex items should always create an independent formatting context!
  403. VERIFY(independent_formatting_context);
  404. box_state.content_width = fit_content_cross_size;
  405. independent_formatting_context->run(item.box, LayoutMode::Default);
  406. return BlockFormattingContext::compute_theoretical_height(throwaway_state, item.box);
  407. }
  408. // https://www.w3.org/TR/css-flexbox-1/#algo-main-item
  409. void FlexFormattingContext::determine_flex_base_size_and_hypothetical_main_size(FlexItem& flex_item)
  410. {
  411. auto& child_box = flex_item.box;
  412. flex_item.flex_base_size = [&] {
  413. auto const& used_flex_basis = child_box.computed_values().flex_basis();
  414. // A. If the item has a definite used flex basis, that’s the flex base size.
  415. if (used_flex_basis.is_definite()) {
  416. auto specified_base_size = get_pixel_size(m_state, child_box, used_flex_basis.length_percentage.value());
  417. if (specified_base_size == 0)
  418. return calculated_main_size(flex_item.box);
  419. return specified_base_size;
  420. }
  421. // B. If the flex item has ...
  422. // - an intrinsic aspect ratio,
  423. // - a used flex basis of content, and
  424. // - a definite cross size,
  425. if (flex_item.box.has_intrinsic_aspect_ratio()
  426. && used_flex_basis.type == CSS::FlexBasis::Content
  427. && has_definite_cross_size(child_box)) {
  428. TODO();
  429. // flex_base_size is calculated from definite cross size and intrinsic aspect ratio
  430. }
  431. // C. If the used flex basis is content or depends on its available space,
  432. // and the flex container is being sized under a min-content or max-content constraint
  433. // (e.g. when performing automatic table layout [CSS21]), size the item under that constraint.
  434. // The flex base size is the item’s resulting main size.
  435. if (used_flex_basis.type == CSS::FlexBasis::Content
  436. // FIXME: && sized under min-content or max-content constraints
  437. && false) {
  438. TODO();
  439. // Size child_box under the constraints, flex_base_size is then the resulting main_size.
  440. }
  441. // D. Otherwise, if the used flex basis is content or depends on its available space,
  442. // the available main size is infinite, and the flex item’s inline axis is parallel to the main axis,
  443. // lay the item out using the rules for a box in an orthogonal flow [CSS3-WRITING-MODES].
  444. // The flex base size is the item’s max-content main size.
  445. if (used_flex_basis.type == CSS::FlexBasis::Content
  446. // FIXME: && main_size is infinite && inline axis is parallel to the main axis
  447. && false && false) {
  448. TODO();
  449. // Use rules for a flex_container in orthogonal flow
  450. }
  451. // E. Otherwise, size the item into the available space using its used flex basis in place of its main size,
  452. // treating a value of content as max-content. If a cross size is needed to determine the main size
  453. // (e.g. when the flex item’s main size is in its block axis) and the flex item’s cross size is auto and not definite,
  454. // in this calculation use fit-content as the flex item’s cross size.
  455. // The flex base size is the item’s resulting main size.
  456. // FIXME: This is probably too naive.
  457. // FIXME: Care about FlexBasis::Auto
  458. if (has_definite_main_size(child_box))
  459. return specified_main_size_of_child_box(child_box);
  460. return calculate_indefinite_main_size(flex_item);
  461. }();
  462. // The hypothetical main size is the item’s flex base size clamped according to its used min and max main sizes (and flooring the content box size at zero).
  463. auto clamp_min = has_main_min_size(child_box) ? specified_main_min_size(child_box) : 0;
  464. auto clamp_max = has_main_max_size(child_box) ? specified_main_max_size(child_box) : NumericLimits<float>::max();
  465. flex_item.hypothetical_main_size = clamp(flex_item.flex_base_size, clamp_min, clamp_max);
  466. }
  467. // https://www.w3.org/TR/css-flexbox-1/#algo-main-container
  468. void FlexFormattingContext::determine_main_size_of_flex_container(bool const main_is_constrained, float const main_min_size, float const main_max_size)
  469. {
  470. if (!main_is_constrained || !m_available_space->main.has_value()) {
  471. // Uses https://www.w3.org/TR/css-flexbox-1/#intrinsic-main-sizes
  472. // 9.9.1
  473. // 1.
  474. float largest_max_content_flex_fraction = 0;
  475. for (auto& flex_item : m_flex_items) {
  476. // FIXME: This needs some serious work.
  477. float max_content_contribution = calculated_main_size(flex_item.box);
  478. float max_content_flex_fraction = max_content_contribution - flex_item.flex_base_size;
  479. if (max_content_flex_fraction > 0) {
  480. max_content_flex_fraction /= max(flex_item.box.computed_values().flex_grow(), 1.0f);
  481. } else {
  482. max_content_flex_fraction /= max(flex_item.box.computed_values().flex_shrink(), 1.0f) * flex_item.flex_base_size;
  483. }
  484. flex_item.max_content_flex_fraction = max_content_flex_fraction;
  485. if (max_content_flex_fraction > largest_max_content_flex_fraction)
  486. largest_max_content_flex_fraction = max_content_flex_fraction;
  487. }
  488. // 2. Omitted
  489. // 3.
  490. float result = 0;
  491. for (auto& flex_item : m_flex_items) {
  492. auto product = 0;
  493. if (flex_item.max_content_flex_fraction > 0) {
  494. product = largest_max_content_flex_fraction * flex_item.box.computed_values().flex_grow();
  495. } else {
  496. product = largest_max_content_flex_fraction * max(flex_item.box.computed_values().flex_shrink(), 1.0f) * flex_item.flex_base_size;
  497. }
  498. result += flex_item.flex_base_size + product;
  499. }
  500. m_available_space->main = clamp(result, main_min_size, main_max_size);
  501. }
  502. set_main_size(flex_container(), m_available_space->main.value_or(NumericLimits<float>::max()));
  503. }
  504. // https://www.w3.org/TR/css-flexbox-1/#algo-line-break
  505. void FlexFormattingContext::collect_flex_items_into_flex_lines()
  506. {
  507. // FIXME: Also support wrap-reverse
  508. // If the flex container is single-line, collect all the flex items into a single flex line.
  509. if (is_single_line()) {
  510. FlexLine line;
  511. for (auto& flex_item : m_flex_items) {
  512. line.items.append(&flex_item);
  513. }
  514. m_flex_lines.append(move(line));
  515. return;
  516. }
  517. // Otherwise, starting from the first uncollected item, collect consecutive items one by one
  518. // until the first time that the next collected item would not fit into the flex container’s inner main size
  519. // (or until a forced break is encountered, see §10 Fragmenting Flex Layout).
  520. // If the very first uncollected item wouldn't fit, collect just it into the line.
  521. // For this step, the size of a flex item is its outer hypothetical main size. (Note: This can be negative.)
  522. // Repeat until all flex items have been collected into flex lines.
  523. FlexLine line;
  524. float line_main_size = 0;
  525. for (auto& flex_item : m_flex_items) {
  526. if ((line_main_size + flex_item.hypothetical_main_size) > m_available_space->main.value_or(NumericLimits<float>::max())) {
  527. m_flex_lines.append(move(line));
  528. line = {};
  529. line_main_size = 0;
  530. }
  531. line.items.append(&flex_item);
  532. line_main_size += flex_item.hypothetical_main_size;
  533. }
  534. m_flex_lines.append(move(line));
  535. }
  536. // https://www.w3.org/TR/css-flexbox-1/#resolve-flexible-lengths
  537. void FlexFormattingContext::resolve_flexible_lengths()
  538. {
  539. enum FlexFactor {
  540. FlexGrowFactor,
  541. FlexShrinkFactor
  542. };
  543. FlexFactor used_flex_factor;
  544. // 6.1. Determine used flex factor
  545. for (auto& flex_line : m_flex_lines) {
  546. size_t number_of_unfrozen_items_on_line = flex_line.items.size();
  547. float sum_of_hypothetical_main_sizes = 0;
  548. for (auto& flex_item : flex_line.items) {
  549. sum_of_hypothetical_main_sizes += flex_item->hypothetical_main_size;
  550. }
  551. if (sum_of_hypothetical_main_sizes < m_available_space->main.value_or(NumericLimits<float>::max()))
  552. used_flex_factor = FlexFactor::FlexGrowFactor;
  553. else
  554. used_flex_factor = FlexFactor::FlexShrinkFactor;
  555. for (auto& flex_item : flex_line.items) {
  556. if (used_flex_factor == FlexFactor::FlexGrowFactor)
  557. flex_item->flex_factor = flex_item->box.computed_values().flex_grow();
  558. else if (used_flex_factor == FlexFactor::FlexShrinkFactor)
  559. flex_item->flex_factor = flex_item->box.computed_values().flex_shrink();
  560. }
  561. // 6.2. Size inflexible items
  562. auto freeze_item_setting_target_main_size_to_hypothetical_main_size = [&number_of_unfrozen_items_on_line](FlexItem& item) {
  563. item.target_main_size = item.hypothetical_main_size;
  564. number_of_unfrozen_items_on_line--;
  565. item.frozen = true;
  566. };
  567. for (auto& flex_item : flex_line.items) {
  568. if (flex_item->flex_factor.has_value() && flex_item->flex_factor.value() == 0) {
  569. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  570. } else if (used_flex_factor == FlexFactor::FlexGrowFactor) {
  571. // FIXME: Spec doesn't include the == case, but we take a too basic approach to calculating the values used so this is appropriate
  572. if (flex_item->flex_base_size > flex_item->hypothetical_main_size) {
  573. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  574. }
  575. } else if (used_flex_factor == FlexFactor::FlexShrinkFactor) {
  576. if (flex_item->flex_base_size < flex_item->hypothetical_main_size) {
  577. freeze_item_setting_target_main_size_to_hypothetical_main_size(*flex_item);
  578. }
  579. }
  580. }
  581. // 6.3. Calculate initial free space
  582. auto calculate_free_space = [&]() {
  583. float sum_of_items_on_line = 0;
  584. for (auto& flex_item : flex_line.items) {
  585. if (flex_item->frozen)
  586. sum_of_items_on_line += flex_item->target_main_size;
  587. else
  588. sum_of_items_on_line += flex_item->flex_base_size;
  589. }
  590. return specified_main_size(flex_container()) - sum_of_items_on_line;
  591. };
  592. float initial_free_space = calculate_free_space();
  593. // 6.4 Loop
  594. auto for_each_unfrozen_item = [&flex_line](auto callback) {
  595. for (auto& flex_item : flex_line.items) {
  596. if (!flex_item->frozen)
  597. callback(flex_item);
  598. }
  599. };
  600. while (number_of_unfrozen_items_on_line > 0) {
  601. // b Calculate the remaining free space
  602. auto remaining_free_space = calculate_free_space();
  603. float sum_of_unfrozen_flex_items_flex_factors = 0;
  604. for_each_unfrozen_item([&](FlexItem* item) {
  605. sum_of_unfrozen_flex_items_flex_factors += item->flex_factor.value_or(1);
  606. });
  607. if (sum_of_unfrozen_flex_items_flex_factors < 1) {
  608. auto intermediate_free_space = initial_free_space * sum_of_unfrozen_flex_items_flex_factors;
  609. if (AK::abs(intermediate_free_space) < AK::abs(remaining_free_space))
  610. remaining_free_space = intermediate_free_space;
  611. }
  612. // c Distribute free space proportional to the flex factors
  613. if (remaining_free_space != 0) {
  614. if (used_flex_factor == FlexFactor::FlexGrowFactor) {
  615. float sum_of_flex_grow_factor_of_unfrozen_items = sum_of_unfrozen_flex_items_flex_factors;
  616. for_each_unfrozen_item([&](FlexItem* flex_item) {
  617. float ratio = flex_item->flex_factor.value_or(1) / sum_of_flex_grow_factor_of_unfrozen_items;
  618. flex_item->target_main_size = flex_item->flex_base_size + (remaining_free_space * ratio);
  619. });
  620. } else if (used_flex_factor == FlexFactor::FlexShrinkFactor) {
  621. float sum_of_scaled_flex_shrink_factor_of_unfrozen_items = 0;
  622. for_each_unfrozen_item([&](FlexItem* flex_item) {
  623. flex_item->scaled_flex_shrink_factor = flex_item->flex_factor.value_or(1) * flex_item->flex_base_size;
  624. sum_of_scaled_flex_shrink_factor_of_unfrozen_items += flex_item->scaled_flex_shrink_factor;
  625. });
  626. for_each_unfrozen_item([&](FlexItem* flex_item) {
  627. float ratio = 1.0f;
  628. if (sum_of_scaled_flex_shrink_factor_of_unfrozen_items != 0.0f)
  629. ratio = flex_item->scaled_flex_shrink_factor / sum_of_scaled_flex_shrink_factor_of_unfrozen_items;
  630. flex_item->target_main_size = flex_item->flex_base_size - (AK::abs(remaining_free_space) * ratio);
  631. });
  632. }
  633. } else {
  634. // This isn't spec but makes sense.
  635. for_each_unfrozen_item([&](FlexItem* flex_item) {
  636. flex_item->target_main_size = flex_item->flex_base_size;
  637. });
  638. }
  639. // d Fix min/max violations.
  640. float adjustments = 0.0f;
  641. for_each_unfrozen_item([&](FlexItem* item) {
  642. auto min_main = has_main_min_size(item->box)
  643. ? specified_main_min_size(item->box)
  644. : 0;
  645. auto max_main = has_main_max_size(item->box)
  646. ? specified_main_max_size(item->box)
  647. : NumericLimits<float>::max();
  648. float original_target_size = item->target_main_size;
  649. if (item->target_main_size < min_main) {
  650. item->target_main_size = min_main;
  651. item->is_min_violation = true;
  652. }
  653. if (item->target_main_size > max_main) {
  654. item->target_main_size = max_main;
  655. item->is_max_violation = true;
  656. }
  657. float delta = item->target_main_size - original_target_size;
  658. adjustments += delta;
  659. });
  660. // e Freeze over-flexed items
  661. float total_violation = adjustments;
  662. if (total_violation == 0) {
  663. for_each_unfrozen_item([&](FlexItem* item) {
  664. --number_of_unfrozen_items_on_line;
  665. item->frozen = true;
  666. });
  667. } else if (total_violation > 0) {
  668. for_each_unfrozen_item([&](FlexItem* item) {
  669. if (item->is_min_violation) {
  670. --number_of_unfrozen_items_on_line;
  671. item->frozen = true;
  672. }
  673. });
  674. } else if (total_violation < 0) {
  675. for_each_unfrozen_item([&](FlexItem* item) {
  676. if (item->is_max_violation) {
  677. --number_of_unfrozen_items_on_line;
  678. item->frozen = true;
  679. }
  680. });
  681. }
  682. }
  683. // 6.5.
  684. for (auto& flex_item : flex_line.items) {
  685. flex_item->main_size = flex_item->target_main_size;
  686. }
  687. }
  688. }
  689. // https://drafts.csswg.org/css-flexbox-1/#algo-cross-item
  690. void FlexFormattingContext::determine_hypothetical_cross_size_of_item(FlexItem& item)
  691. {
  692. // Determine the hypothetical cross size of each item by performing layout
  693. // as if it were an in-flow block-level box with the used main size
  694. // and the given available space, treating auto as fit-content.
  695. // If we have a definite cross size, this is easy! No need to perform layout, we can just use it as-is.
  696. if (has_definite_cross_size(item.box)) {
  697. item.hypothetical_cross_size = resolved_definite_cross_size(item.box);
  698. return;
  699. }
  700. if (has_definite_main_size(item.box)) {
  701. // For indefinite cross sizes, we perform a throwaway layout and then measure it.
  702. FormattingState throwaway_state(&m_state);
  703. auto& box_state = throwaway_state.get_mutable(item.box);
  704. // Item has definite main size, layout with that as the used main size.
  705. auto independent_formatting_context = create_independent_formatting_context_if_needed(throwaway_state, item.box);
  706. // NOTE: Flex items should always create an independent formatting context!
  707. VERIFY(independent_formatting_context);
  708. if (is_row_layout()) {
  709. box_state.content_width = resolved_definite_main_size(item.box);
  710. } else {
  711. box_state.content_height = resolved_definite_main_size(item.box);
  712. }
  713. independent_formatting_context->run(item.box, LayoutMode::Default);
  714. if (is_row_layout())
  715. item.hypothetical_cross_size = BlockFormattingContext::compute_theoretical_height(throwaway_state, item.box);
  716. else
  717. item.hypothetical_cross_size = box_state.content_width;
  718. } else {
  719. // Item has indefinite main size, layout with "fit-content"
  720. float fit_content_main_size;
  721. if (is_row_layout())
  722. fit_content_main_size = calculate_fit_content_width(item.box, m_available_space->main);
  723. else
  724. fit_content_main_size = calculate_fit_content_height(item.box, m_available_space->main);
  725. FormattingState throwaway_state(&m_state);
  726. auto& box_state = throwaway_state.get_mutable(item.box);
  727. // Item has definite main size, layout with that as the used main size.
  728. auto independent_formatting_context = create_independent_formatting_context_if_needed(throwaway_state, item.box);
  729. // NOTE: Flex items should always create an independent formatting context!
  730. VERIFY(independent_formatting_context);
  731. if (is_row_layout()) {
  732. box_state.content_width = fit_content_main_size;
  733. } else {
  734. box_state.content_height = fit_content_main_size;
  735. }
  736. independent_formatting_context->run(item.box, LayoutMode::Default);
  737. if (is_row_layout()) {
  738. item.hypothetical_cross_size = BlockFormattingContext::compute_theoretical_height(throwaway_state, item.box);
  739. } else {
  740. item.hypothetical_cross_size = box_state.content_width;
  741. }
  742. }
  743. }
  744. // https://www.w3.org/TR/css-flexbox-1/#algo-cross-line
  745. void FlexFormattingContext::calculate_cross_size_of_each_flex_line(float const cross_min_size, float const cross_max_size)
  746. {
  747. // If the flex container is single-line and has a definite cross size, the cross size of the flex line is the flex container’s inner cross size.
  748. if (is_single_line() && has_definite_cross_size(flex_container())) {
  749. m_flex_lines[0].cross_size = specified_cross_size(flex_container());
  750. return;
  751. }
  752. // Otherwise, for each flex line:
  753. for (auto& flex_line : m_flex_lines) {
  754. // FIXME: 1. Collect all the flex items whose inline-axis is parallel to the main-axis, whose align-self is baseline,
  755. // and whose cross-axis margins are both non-auto. Find the largest of the distances between each item’s baseline
  756. // and its hypothetical outer cross-start edge, and the largest of the distances between each item’s baseline
  757. // and its hypothetical outer cross-end edge, and sum these two values.
  758. // FIXME: This isn't spec but makes sense here
  759. if (has_definite_cross_size(flex_container()) && flex_container().computed_values().align_items() == CSS::AlignItems::Stretch) {
  760. flex_line.cross_size = specified_cross_size(flex_container()) / m_flex_lines.size();
  761. continue;
  762. }
  763. // 2. Among all the items not collected by the previous step, find the largest outer hypothetical cross size.
  764. float largest_hypothetical_cross_size = 0;
  765. for (auto& flex_item : flex_line.items) {
  766. if (largest_hypothetical_cross_size < flex_item->hypothetical_cross_size_with_margins())
  767. largest_hypothetical_cross_size = flex_item->hypothetical_cross_size_with_margins();
  768. }
  769. // 3. The used cross-size of the flex line is the largest of the numbers found in the previous two steps and zero.
  770. flex_line.cross_size = max(0.0f, largest_hypothetical_cross_size);
  771. }
  772. // If the flex container is single-line, then clamp the line’s cross-size to be within the container’s computed min and max cross sizes.
  773. // Note that if CSS 2.1’s definition of min/max-width/height applied more generally, this behavior would fall out automatically.
  774. if (is_single_line())
  775. clamp(m_flex_lines[0].cross_size, cross_min_size, cross_max_size);
  776. }
  777. // https://www.w3.org/TR/css-flexbox-1/#algo-stretch
  778. void FlexFormattingContext::determine_used_cross_size_of_each_flex_item()
  779. {
  780. // FIXME: Get the alignment via "align-self" of the item (which accesses "align-items" of the parent if unset)
  781. for (auto& flex_line : m_flex_lines) {
  782. for (auto& flex_item : flex_line.items) {
  783. if (is_cross_auto(flex_item->box) && flex_container().computed_values().align_items() == CSS::AlignItems::Stretch) {
  784. flex_item->cross_size = flex_line.cross_size;
  785. } else {
  786. flex_item->cross_size = flex_item->hypothetical_cross_size;
  787. }
  788. }
  789. }
  790. }
  791. // https://www.w3.org/TR/css-flexbox-1/#algo-main-align
  792. void FlexFormattingContext::distribute_any_remaining_free_space()
  793. {
  794. for (auto& flex_line : m_flex_lines) {
  795. // 12.1.
  796. float used_main_space = 0;
  797. size_t auto_margins = 0;
  798. for (auto& flex_item : flex_line.items) {
  799. used_main_space += flex_item->main_size;
  800. if (is_main_axis_margin_first_auto(flex_item->box))
  801. ++auto_margins;
  802. if (is_main_axis_margin_second_auto(flex_item->box))
  803. ++auto_margins;
  804. }
  805. float remaining_free_space = m_available_space->main.value_or(NumericLimits<float>::max()) - used_main_space;
  806. if (remaining_free_space > 0) {
  807. float size_per_auto_margin = remaining_free_space / (float)auto_margins;
  808. for (auto& flex_item : flex_line.items) {
  809. if (is_main_axis_margin_first_auto(flex_item->box))
  810. set_main_axis_first_margin(flex_item->box, size_per_auto_margin);
  811. if (is_main_axis_margin_second_auto(flex_item->box))
  812. set_main_axis_second_margin(flex_item->box, size_per_auto_margin);
  813. }
  814. } else {
  815. for (auto& flex_item : flex_line.items) {
  816. if (is_main_axis_margin_first_auto(flex_item->box))
  817. set_main_axis_first_margin(flex_item->box, 0);
  818. if (is_main_axis_margin_second_auto(flex_item->box))
  819. set_main_axis_second_margin(flex_item->box, 0);
  820. }
  821. }
  822. // 12.2.
  823. float space_between_items = 0;
  824. float space_before_first_item = 0;
  825. auto number_of_items = flex_line.items.size();
  826. switch (flex_container().computed_values().justify_content()) {
  827. case CSS::JustifyContent::FlexStart:
  828. break;
  829. case CSS::JustifyContent::FlexEnd:
  830. space_before_first_item = m_available_space->main.value_or(NumericLimits<float>::max()) - used_main_space;
  831. break;
  832. case CSS::JustifyContent::Center:
  833. space_before_first_item = (m_available_space->main.value_or(NumericLimits<float>::max()) - used_main_space) / 2.0f;
  834. break;
  835. case CSS::JustifyContent::SpaceBetween:
  836. space_between_items = remaining_free_space / (number_of_items - 1);
  837. break;
  838. case CSS::JustifyContent::SpaceAround:
  839. space_between_items = remaining_free_space / number_of_items;
  840. space_before_first_item = space_between_items / 2.0f;
  841. break;
  842. }
  843. // FIXME: Support reverse
  844. float main_offset = space_before_first_item;
  845. for (auto& flex_item : flex_line.items) {
  846. flex_item->main_offset = main_offset;
  847. main_offset += flex_item->main_size + space_between_items;
  848. }
  849. }
  850. }
  851. void FlexFormattingContext::align_all_flex_items_along_the_cross_axis()
  852. {
  853. // FIXME: Get the alignment via "align-self" of the item (which accesses "align-items" of the parent if unset)
  854. // FIXME: Take better care of margins
  855. float line_cross_offset = 0;
  856. for (auto& flex_line : m_flex_lines) {
  857. for (auto* flex_item : flex_line.items) {
  858. switch (flex_container().computed_values().align_items()) {
  859. case CSS::AlignItems::Baseline:
  860. // FIXME: Implement this
  861. // Fallthrough
  862. case CSS::AlignItems::FlexStart:
  863. case CSS::AlignItems::Stretch:
  864. flex_item->cross_offset = line_cross_offset + flex_item->margins.cross_before;
  865. break;
  866. case CSS::AlignItems::FlexEnd:
  867. flex_item->cross_offset = line_cross_offset + flex_line.cross_size - flex_item->cross_size;
  868. break;
  869. case CSS::AlignItems::Center:
  870. flex_item->cross_offset = line_cross_offset + (flex_line.cross_size / 2.0f) - (flex_item->cross_size / 2.0f);
  871. break;
  872. default:
  873. break;
  874. }
  875. }
  876. line_cross_offset += flex_line.cross_size;
  877. }
  878. }
  879. // https://www.w3.org/TR/css-flexbox-1/#algo-cross-container
  880. void FlexFormattingContext::determine_flex_container_used_cross_size(float const cross_min_size, float const cross_max_size)
  881. {
  882. float cross_size = 0;
  883. if (has_definite_cross_size(flex_container())) {
  884. // Flex container has definite cross size: easy-peasy.
  885. cross_size = specified_cross_size(flex_container());
  886. } else {
  887. // Flex container has indefinite cross size.
  888. auto cross_size_value = is_row_layout() ? flex_container().computed_values().height() : flex_container().computed_values().width();
  889. if (!cross_size_value.has_value() || (cross_size_value->is_length() && cross_size_value->length().is_auto())) {
  890. // If a content-based cross size is needed, use the sum of the flex lines' cross sizes.
  891. float sum_of_flex_lines_cross_sizes = 0;
  892. for (auto& flex_line : m_flex_lines) {
  893. sum_of_flex_lines_cross_sizes += flex_line.cross_size;
  894. }
  895. cross_size = sum_of_flex_lines_cross_sizes;
  896. } else {
  897. // Otherwise, resolve the indefinite size at this point.
  898. cross_size = cross_size_value->resolved(flex_container(), CSS::Length::make_px(specified_cross_size(*flex_container().containing_block()))).to_px(flex_container());
  899. }
  900. }
  901. set_cross_size(flex_container(), clamp(cross_size, cross_min_size, cross_max_size));
  902. }
  903. // https://www.w3.org/TR/css-flexbox-1/#algo-line-align
  904. void FlexFormattingContext::align_all_flex_lines()
  905. {
  906. // FIXME: Support reverse
  907. if (is_single_line()) {
  908. // For single-line flex containers, we only need to center the line along the cross axis.
  909. auto& flex_line = m_flex_lines[0];
  910. float cross_size_of_flex_container = specified_cross_size(flex_container());
  911. for (auto* flex_item : flex_line.items)
  912. flex_item->cross_offset += (cross_size_of_flex_container / 2.0f) - (flex_line.cross_size / 2.0f);
  913. } else {
  914. // FIXME: Support align-content
  915. }
  916. }
  917. void FlexFormattingContext::copy_dimensions_from_flex_items_to_boxes()
  918. {
  919. for (auto& flex_item : m_flex_items) {
  920. set_main_size(flex_item.box, flex_item.main_size);
  921. set_cross_size(flex_item.box, flex_item.cross_size);
  922. set_offset(flex_item.box, flex_item.main_offset, flex_item.cross_offset);
  923. }
  924. }
  925. }