Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / layout / flex_formatting_context_4.lucb

#==============================================================================================
#
#   flex_formatting_context_4 - Layout::FlexFormattingContext: cross sizes, main-axis
#                               alignment and cross-axis alignment of the items
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/FlexFormattingContext.cpp (lines 1194-1643): the hypothetical
#       cross size of an item, the cross size of each flex line, the used cross size of each
#       item (stretching), distributing the remaining free space along the main axis
#       (auto margins and justify-content), the debug dump of the items, an item's effective
#       align-self, aligning the items along the cross axis, and the baseline alignment that
#       runs after the items are laid out.
#
#       Copyright (c) 2021-2025, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Cross sizes =============================================================================

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:1195 Web::Layout::FlexFormattingContext::determine_hypothetical_cross_size_of_item.
# https://drafts.csswg.org/css-flexbox/#hypothetical-cross-size
func layout_flex_formatting_context_determine_hypothetical_cross_size_of_item(this: LayoutFlexFormattingContext*, item: LayoutFlexFormattingContextFlexItem*):
    # Determine the hypothetical cross size of each item by performing layout as if it were an in-flow block-level box
    # with the used main size and the given available space, treating auto as fit-content.

    let computed_min_size = layout_flex_formatting_context_computed_cross_min_size(this, item.box)

    let clamp_min = layout_zero_px() if css_size_is_auto(&computed_min_size) else layout_flex_formatting_context_specified_cross_min_size(this, item)
    let clamp_max = gfx.css_pixels_max() if layout_flex_formatting_context_should_treat_cross_max_size_as_none(this, item.box) else layout_flex_formatting_context_specified_cross_max_size(this, item)

    # If we have a definite cross size, this is easy! No need to perform layout, we can just use it as-is.
    if layout_flex_formatting_context_has_definite_cross_size_layout_flex_formatting_context(this, item):
        item.hypothetical_cross_size = layout_css_clamp(layout_flex_formatting_context_inner_cross_size_layout_flex_formatting_context(this, item), clamp_min, clamp_max)
        return

    if layout_box_has_preferred_aspect_ratio(item.box):
        let auto_size = layout_box_auto_content_box_size(item.box)
        if item.used_flex_basis_is_definite or (css_size_with_aspect_ratio_has_width(&auto_size) and css_size_with_aspect_ratio_has_height(&auto_size)):
            let main_size = item.main_size else trap("VERIFY: the item's main size is determined")
            let aspect_ratio = layout_box_preferred_aspect_ratio(item.box) else trap("VERIFY: the box has a preferred aspect ratio")
            item.hypothetical_cross_size = layout_css_clamp(layout_flex_formatting_context_calculate_cross_size_from_main_size_and_aspect_ratio(this, main_size, aspect_ratio), clamp_min, clamp_max)
            return
        item.hypothetical_cross_size = layout_css_clamp(layout_flex_formatting_context_inner_cross_size_layout_state_used_values(this, this.m_flex_container_state), clamp_min, clamp_max)
        return

    let computed_cross_size = layout_flex_formatting_context_computed_cross_size(this, item.box)

    if css_size_is_min_content(&computed_cross_size):
        item.hypothetical_cross_size = layout_css_clamp(layout_flex_formatting_context_calculate_min_content_cross_size(this, item), clamp_min, clamp_max)
        return

    if css_size_is_max_content(&computed_cross_size):
        item.hypothetical_cross_size = layout_css_clamp(layout_flex_formatting_context_calculate_max_content_cross_size(this, item), clamp_min, clamp_max)
        return

    # "... treating auto as fit-content"
    var fit_content_cross_size: gfx.CssPixels
    if not layout_flex_formatting_context_cross_axis_is_horizontal(this):
        var available_width = layout_available_size_make_indefinite()
        if let main_size = item.main_size:
            available_width = layout_available_size_make_definite(layout_clamp_to_max_dimension_value(main_size))
        let available_height = layout_available_size_make_indefinite()
        fit_content_cross_size = layout_formatting_context_calculate_fit_content_height(this.layout_formatting_context(), item.box, LayoutAvailableSpace(width = available_width, height = available_height))
    else:
        fit_content_cross_size = layout_formatting_context_calculate_fit_content_width(this.layout_formatting_context(), item.box, layout_flex_formatting_context_available_space_for_items(this).space)
    item.hypothetical_cross_size = layout_css_clamp(fit_content_cross_size, clamp_min, clamp_max)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:1246 Web::Layout::FlexFormattingContext::calculate_cross_size_of_each_flex_line.
# https://www.w3.org/TR/css-flexbox-1/#algo-cross-line
func layout_flex_formatting_context_calculate_cross_size_of_each_flex_line(this: LayoutFlexFormattingContext*):
    # 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.
    if layout_flex_formatting_context_is_single_line(this) and layout_flex_formatting_context_has_definite_cross_size_layout_state_used_values(this, this.m_flex_container_state):
        ak.vector_at(&this.m_flex_lines, 0).cross_size = layout_flex_formatting_context_inner_cross_size_layout_state_used_values(this, this.m_flex_container_state)
        return

    # Otherwise, for each flex line:
    let lines = ak.vector_span(&this.m_flex_lines)
    for flex_line in &lines:
        # FIXME: 1. Collect all the flex items whose inline-axis is parallel to the main-axis, whose align-self is baseline,
        #           and whose cross-axis margins are both non-auto. Find the largest of the distances between each item’s baseline
        #           and its hypothetical outer cross-start edge, and the largest of the distances between each item’s baseline
        #           and its hypothetical outer cross-end edge, and sum these two values.

        # 2. Among all the items not collected by the previous step, find the largest outer hypothetical cross size.
        var largest_hypothetical_cross_size = layout_zero_px()
        for item in ak.vector_span(&flex_line.items):
            if largest_hypothetical_cross_size.lt(layout_flex_formatting_context_flex_item_hypothetical_cross_size_with_margins(item)):
                largest_hypothetical_cross_size = layout_flex_formatting_context_flex_item_hypothetical_cross_size_with_margins(item)

        # 3. The used cross-size of the flex line is the largest of the numbers found in the previous two steps and zero.
        flex_line.cross_size = ak.max(layout_zero_px(), largest_hypothetical_cross_size)

    # 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.
    # Note that if CSS 2.1’s definition of min/max-width/height applied more generally, this behavior would fall out automatically.
    # AD-HOC: We don't do this when the flex container is being sized under a min-content or max-content constraint.
    let available_cross = layout_flex_formatting_context_available_space_for_items(this).cross
    if layout_flex_formatting_context_is_single_line(this) and not layout_available_size_is_intrinsic_sizing_constraint(&available_cross):
        let flex_container = layout_flex_formatting_context_flex_container(this)
        let computed_min_size = layout_flex_formatting_context_computed_cross_min_size(this, flex_container)

        let cross_min_size = layout_flex_formatting_context_calculate_inner_flex_container_cross_min_size(this) if not css_size_is_auto(&computed_min_size) else layout_zero_px()
        let cross_max_size = layout_flex_formatting_context_calculate_inner_flex_container_cross_max_size(this) if not layout_flex_formatting_context_should_treat_cross_max_size_as_none(this, flex_container) else gfx.css_pixels_max()
        let first_line = ak.vector_at(&this.m_flex_lines, 0)
        first_line.cross_size = layout_css_clamp(first_line.cross_size, cross_min_size, cross_max_size)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:1285 Web::Layout::FlexFormattingContext::determine_used_cross_size_of_each_flex_item.
# https://www.w3.org/TR/css-flexbox-1/#algo-stretch
func layout_flex_formatting_context_determine_used_cross_size_of_each_flex_item(this: LayoutFlexFormattingContext*):
    let lines = ak.vector_span(&this.m_flex_lines)
    for flex_line in &lines:
        for item in ak.vector_span(&flex_line.items):
            #  If a flex item has align-self: stretch, its computed cross size property is auto,
            #  and neither of its cross-axis margins are auto, the used outer cross size is the used cross size of its flex line,
            #  clamped according to the item’s used min and max cross sizes.
            let flex_item_alignment = layout_flex_formatting_context_alignment_for_item(this, item.box)
            if (flex_item_alignment == css_data.AlignItems.stretch or flex_item_alignment == css_data.AlignItems.normal) and layout_flex_formatting_context_is_cross_auto(this, item.box) and not item.margins.cross_before_is_auto and not item.margins.cross_after_is_auto:
                let unclamped_cross_size = flex_line.cross_size.sub(item.margins.cross_before).sub(item.margins.cross_after).sub(item.padding.cross_before).sub(item.padding.cross_after).sub(item.borders.cross_before).sub(item.borders.cross_after)

                let computed_min_size = layout_flex_formatting_context_computed_cross_min_size(this, item.box)
                let cross_min_size = layout_flex_formatting_context_specified_cross_min_size(this, item) if (not css_size_is_auto(&computed_min_size) and not css_size_contains_percentage(&computed_min_size)) else layout_zero_px()
                let cross_max_size = layout_flex_formatting_context_specified_cross_max_size(this, item) if not layout_flex_formatting_context_should_treat_cross_max_size_as_none(this, item.box) else gfx.css_pixels_max()

                item.cross_size = layout_css_clamp(unclamped_cross_size, cross_min_size, cross_max_size)
            else:
                # Otherwise, the used cross size is the item’s hypothetical cross size.
                item.cross_size = item.hypothetical_cross_size

# mark: Main-axis alignment =====================================================================

## Where FlexFormattingContext::distribute_any_remaining_free_space starts placing a line's items: its local
## `enum class FlexRegionRenderCursor`. For reverse, Right indicates that the cursor offset is the end and items
## are rendered backwards; otherwise the cursor offset is the 'start' of the region or the initial offset.
enum LayoutFlexFormattingContextFlexRegionRenderCursor:
    left
    right

## The `place_item` lambda of FlexFormattingContext::distribute_any_remaining_free_space (its captures are the
## parameters; `cursor_offset` is updated in place).
func layout_flex_formatting_context_place_item(this: LayoutFlexFormattingContext*, flex_line: const LayoutFlexFormattingContextFlexLine*, item: LayoutFlexFormattingContextFlexItem*, index: usize, space_between_items: gfx.CssPixels, flex_region_render_cursor: LayoutFlexFormattingContextFlexRegionRenderCursor, cursor_offset: gfx.CssPixels*):
    let main_size = item.main_size else trap("VERIFY: the item's main size is determined")
    # CSS-FLEXBOX-2: Account for gap between items.
    var amount_of_main_size_used = main_size.add(item.margins.main_before).add(item.borders.main_before).add(item.padding.main_before).add(item.margins.main_after).add(item.borders.main_after).add(item.padding.main_after).add(space_between_items)

    if not layout_flex_formatting_context_is_direction_reverse(this) and flex_region_render_cursor == LayoutFlexFormattingContextFlexRegionRenderCursor.right:
        if index < ak.vector_size(&flex_line.items) -% 1:
            amount_of_main_size_used = amount_of_main_size_used.add(layout_flex_formatting_context_main_gap(this))
    else:
        amount_of_main_size_used = amount_of_main_size_used.add(layout_flex_formatting_context_main_gap(this))

    if layout_flex_formatting_context_is_direction_reverse(this) and flex_region_render_cursor == LayoutFlexFormattingContextFlexRegionRenderCursor.right:
        item.main_offset = cursor_offset.sub(main_size).sub(item.margins.main_after).sub(item.borders.main_after).sub(item.padding.main_after)
        *cursor_offset = cursor_offset.sub(amount_of_main_size_used)
    elif flex_region_render_cursor == LayoutFlexFormattingContextFlexRegionRenderCursor.right:
        *cursor_offset = cursor_offset.sub(amount_of_main_size_used)
        item.main_offset = cursor_offset.add(item.margins.main_before).add(item.borders.main_before).add(item.padding.main_before)
    else:
        item.main_offset = cursor_offset.add(item.margins.main_before).add(item.borders.main_before).add(item.padding.main_before)
        *cursor_offset = cursor_offset.add(amount_of_main_size_used)

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:1316 Web::Layout::FlexFormattingContext::distribute_any_remaining_free_space.
# https://www.w3.org/TR/css-flexbox-1/#algo-main-align
func layout_flex_formatting_context_distribute_any_remaining_free_space(this: LayoutFlexFormattingContext*):
    let justify_content = css_computed_values_justify_content(layout_box_computed(layout_flex_formatting_context_flex_container(this)))
    let lines = ak.vector_span(&this.m_flex_lines)
    for flex_line in &lines:
        let items = ak.vector_span(&flex_line.items)
        # 12.1.
        var used_main_space = layout_zero_px()
        var auto_margins: usize = 0
        for used_item in items:
            used_main_space = used_main_space.add(used_item.main_size else trap("VERIFY: the item's main size is determined"))
            if used_item.margins.main_before_is_auto:
                auto_margins += 1

            if used_item.margins.main_after_is_auto:
                auto_margins += 1

            used_main_space = used_main_space.add(used_item.margins.main_before.add(used_item.margins.main_after).add(used_item.borders.main_before).add(used_item.borders.main_after).add(used_item.padding.main_before).add(used_item.padding.main_after))

        # CSS-FLEXBOX-2: Account for gap between flex items.
        used_main_space = used_main_space.add(layout_flex_formatting_context_mul_count(layout_flex_formatting_context_main_gap(this), items.length -% 1))

        var has_positive_remaining_free_space = false
        if let remaining_free_space = flex_line.remaining_free_space:
            has_positive_remaining_free_space = gfx.gt_css_pixels_int(remaining_free_space, 0)
        if has_positive_remaining_free_space and auto_margins > 0:
            let size_per_auto_margin = layout_div_int(flex_line.remaining_free_space else trap("unreachable: the remaining free space has a value"), (i64)auto_margins)
            for auto_margin_item in items:
                if auto_margin_item.margins.main_before_is_auto:
                    layout_flex_formatting_context_set_main_axis_first_margin(this, auto_margin_item, size_per_auto_margin)
                if auto_margin_item.margins.main_after_is_auto:
                    layout_flex_formatting_context_set_main_axis_second_margin(this, auto_margin_item, size_per_auto_margin)
        else:
            for zero_margin_item in items:
                if zero_margin_item.margins.main_before_is_auto:
                    layout_flex_formatting_context_set_main_axis_first_margin(this, zero_margin_item, layout_zero_px())
                if zero_margin_item.margins.main_after_is_auto:
                    layout_flex_formatting_context_set_main_axis_second_margin(this, zero_margin_item, layout_zero_px())

        # 12.2.
        var space_between_items = layout_zero_px()
        var initial_offset = layout_zero_px()
        let number_of_items = items.length

        if auto_margins == 0 and number_of_items > 0:
            let inner_main_size = layout_flex_formatting_context_inner_main_size_layout_state_used_values(this, this.m_flex_container_state)
            match justify_content:
                .start, .left:
                    initial_offset = layout_zero_px()
                .stretch, .normal, .flex_start:
                    if layout_flex_formatting_context_is_direction_reverse(this):
                        initial_offset = inner_main_size
                    else:
                        initial_offset = layout_zero_px()
                .end:
                    initial_offset = inner_main_size
                .right:
                    if layout_flex_formatting_context_is_row_layout(this):
                        initial_offset = inner_main_size
                    else:
                        initial_offset = layout_zero_px()
                .flex_end:
                    if layout_flex_formatting_context_is_direction_reverse(this):
                        initial_offset = layout_zero_px()
                    else:
                        initial_offset = inner_main_size
                .center:
                    initial_offset = layout_div_int(inner_main_size.sub(used_main_space), 2)
                    if layout_flex_formatting_context_is_direction_reverse(this):
                        initial_offset = inner_main_size.sub(initial_offset)
                .space_between:
                    if layout_flex_formatting_context_is_direction_reverse(this):
                        initial_offset = inner_main_size
                    else:
                        initial_offset = layout_zero_px()
                    if let remaining_free_space = flex_line.remaining_free_space:
                        if number_of_items > 1:
                            space_between_items = ak.max(layout_zero_px(), layout_div_int(remaining_free_space, (i64)(number_of_items - 1)))
                .space_around:
                    if let remaining_free_space = flex_line.remaining_free_space:
                        space_between_items = ak.max(layout_zero_px(), layout_div_int(remaining_free_space, (i64)number_of_items))
                    if layout_flex_formatting_context_is_direction_reverse(this):
                        initial_offset = inner_main_size.sub(layout_div_int(space_between_items, 2))
                    else:
                        initial_offset = layout_div_int(space_between_items, 2)
                .space_evenly:
                    if let remaining_free_space = flex_line.remaining_free_space:
                        space_between_items = ak.max(layout_zero_px(), layout_div_int(remaining_free_space, (i64)(number_of_items + 1)))
                    if layout_flex_formatting_context_is_direction_reverse(this):
                        initial_offset = inner_main_size.sub(space_between_items)
                    else:
                        initial_offset = space_between_items
                _:
                    # The C++ switch has no default: other values leave the offsets alone.
                    _ = 0

        # For reverse, we use FlexRegionRenderCursor::Right
        # to indicate the cursor offset is the end and render backwards
        # Otherwise the cursor offset is the 'start' of the region or initial offset
        var flex_region_render_cursor = LayoutFlexFormattingContextFlexRegionRenderCursor.left

        if auto_margins == 0:
            match justify_content:
                .start, .left:
                    flex_region_render_cursor = LayoutFlexFormattingContextFlexRegionRenderCursor.left
                .normal, .flex_start, .center, .space_around, .space_between, .space_evenly, .stretch:
                    if layout_flex_formatting_context_is_direction_reverse(this):
                        flex_region_render_cursor = LayoutFlexFormattingContextFlexRegionRenderCursor.right
                .end:
                    flex_region_render_cursor = LayoutFlexFormattingContextFlexRegionRenderCursor.right
                .right:
                    if layout_flex_formatting_context_is_row_layout(this):
                        flex_region_render_cursor = LayoutFlexFormattingContextFlexRegionRenderCursor.right
                    else:
                        flex_region_render_cursor = LayoutFlexFormattingContextFlexRegionRenderCursor.left
                .flex_end:
                    if not layout_flex_formatting_context_is_direction_reverse(this):
                        flex_region_render_cursor = LayoutFlexFormattingContextFlexRegionRenderCursor.right
                _:
                    _ = 0

        var cursor_offset = initial_offset

        # (place_item is layout_flex_formatting_context_place_item.)
        if flex_region_render_cursor == LayoutFlexFormattingContextFlexRegionRenderCursor.right:
            var index = items.length
            while index > 0:
                index -= 1
                layout_flex_formatting_context_place_item(this, flex_line, items[index], index, space_between_items, flex_region_render_cursor, &cursor_offset)
        else:
            for index in 0..<items.length:
                layout_flex_formatting_context_place_item(this, flex_line, items[index], index, space_between_items, flex_region_render_cursor, &cursor_offset)

# mark: The debug dump ==========================================================================

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:1521 Web::Layout::FlexFormattingContext::dump_items.
pub func layout_flex_formatting_context_dump_items(this: LayoutFlexFormattingContext*):
    let flex_container = layout_flex_formatting_context_flex_container(this)
    let flex_container_description = layout_node_debug_description(flex_container.layout_node())
    var builder = ak.string_builder_new()
    dump_appendff(&builder, "\u{1b}[34;1mflex-container\u{1b}[0m {}, direction: {}, current-size: {}x{}", [
        dump_arg_string(&flex_container_description),
        ak.arg_string_view(ak.sv("row" if layout_flex_formatting_context_is_row_layout(this) else "column")),
        dump_arg_css_pixels(layout_state_used_values_content_width(this.m_flex_container_state)),
        dump_arg_css_pixels(layout_state_used_values_content_height(this.m_flex_container_state)),
    ])
    ak.dbgln(ak.string_view_text(ak.string_builder_string_view(&builder)))
    for line_index in 0..<ak.vector_size(&this.m_flex_lines):
        var line_builder = ak.string_builder_new()
        dump_appendff(&line_builder, "{} flex-line #{}:", [dump_arg_string(&flex_container_description), ak.arg_u64((u64)line_index)])
        ak.dbgln(ak.string_view_text(ak.string_builder_string_view(&line_builder)))
        let line_items = &ak.vector_at(&this.m_flex_lines, line_index).items
        for item_index in 0..<ak.vector_size(line_items):
            let item = *ak.vector_at(line_items, item_index)
            let item_description = layout_node_debug_description(item.box.layout_node())
            var item_builder = ak.string_builder_new()
            dump_appendff(&item_builder, "{}   flex-item #{}: {} (main:{}, cross:{})", [
                dump_arg_string(&flex_container_description),
                ak.arg_u64((u64)item_index),
                dump_arg_string(&item_description),
                dump_arg_css_pixels(item.main_size else gfx.CssPixels.from_int(-1)),
                dump_arg_css_pixels(item.cross_size else gfx.CssPixels.from_int(-1)),
            ])
            ak.dbgln(ak.string_view_text(ak.string_builder_string_view(&item_builder)))

# mark: Cross-axis alignment ====================================================================

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:1533 Web::Layout::FlexFormattingContext::alignment_for_item.
func layout_flex_formatting_context_alignment_for_item(this: LayoutFlexFormattingContext*, box: LayoutBox*) -> css_data.AlignItems:
    match css_computed_values_align_self(layout_box_computed(box)):
        .auto:
            return css_computed_values_align_items(layout_box_computed(layout_flex_formatting_context_flex_container(this)))
        .end:
            return css_data.AlignItems.end
        .normal:
            return css_data.AlignItems.normal
        .self_start:
            return css_data.AlignItems.self_start
        .self_end:
            return css_data.AlignItems.self_end
        .flex_start:
            return css_data.AlignItems.flex_start
        .flex_end:
            return css_data.AlignItems.flex_end
        .center:
            return css_data.AlignItems.center
        .baseline:
            return css_data.AlignItems.baseline
        .start:
            return css_data.AlignItems.start
        .stretch:
            return css_data.AlignItems.stretch
        .safe:
            return css_data.AlignItems.safe
        .unsafe:
            return css_data.AlignItems.unsafe
        _:
            trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:1567 Web::Layout::FlexFormattingContext::align_all_flex_items_along_the_cross_axis.
func layout_flex_formatting_context_align_all_flex_items_along_the_cross_axis(this: LayoutFlexFormattingContext*):
    let flex_wrap = css_computed_values_flex_wrap(layout_box_computed(layout_flex_formatting_context_flex_container(this)))
    # FIXME: Take better care of margins
    let lines = ak.vector_span(&this.m_flex_lines)
    for flex_line in &lines:
        for item in ak.vector_span(&flex_line.items):
            let half_line_size = layout_div_int(flex_line.cross_size, 2)
            let alignment = layout_flex_formatting_context_alignment_for_item(this, item.box)
            match alignment:
                .normal:
                    # https://drafts.csswg.org/css-flexbox/#flex-wrap-property
                    # When flex-wrap is wrap-reverse, the cross-start and cross-end directions are swapped.
                    if flex_wrap == css_data.FlexWrap.wrap_reverse:
                        let cross_size = item.cross_size else trap("VERIFY: the item's cross size is determined")
                        item.cross_offset = half_line_size.sub(cross_size).sub(item.margins.cross_after).sub(item.borders.cross_after).sub(item.padding.cross_after)
                    else:
                        item.cross_offset = half_line_size.neg().add(item.margins.cross_before).add(item.borders.cross_before).add(item.padding.cross_before)
                .baseline, .start, .flex_start, .self_start, .stretch:
                    if alignment == css_data.AlignItems.baseline:
                        # https://drafts.csswg.org/css-flexbox-1/#valdef-align-items-baseline
                        # NB: Baseline-aligned items are initially placed at the cross-start edge (like flex-start). Their
                        #     positions are adjusted after layout in resolve_baseline_aligned_items().
                        flex_line.has_baseline_aligned_items = true
                        # [[fallthrough]]
                    # FIXME: 'start', 'flex-start' and 'self-start' have subtly different behavior.
                    #        The same goes for the end values.
                    item.cross_offset = half_line_size.neg().add(item.margins.cross_before).add(item.borders.cross_before).add(item.padding.cross_before)
                .end, .flex_end, .self_end:
                    let cross_size = item.cross_size else trap("VERIFY: the item's cross size is determined")
                    item.cross_offset = half_line_size.sub(cross_size).sub(item.margins.cross_after).sub(item.borders.cross_after).sub(item.padding.cross_after)
                .center:
                    # https://drafts.csswg.org/css-flexbox/#align-items-property
                    # The flex item’s margin box is centered in the cross axis within the line
                    let cross_size = item.cross_size else trap("VERIFY: the item's cross size is determined")
                    item.cross_offset = layout_div_int(cross_size.neg().add(item.margins.cross_before).add(item.borders.cross_before).add(item.padding.cross_before).sub(item.margins.cross_after).sub(item.borders.cross_after).sub(item.padding.cross_after), 2)
                _:
                    _ = 0

## Ported from Libraries/LibWeb/Layout/FlexFormattingContext.cpp:1615 Web::Layout::FlexFormattingContext::resolve_baseline_aligned_items.
# https://drafts.csswg.org/css-flexbox-1/#valdef-align-items-baseline
func layout_flex_formatting_context_resolve_baseline_aligned_items(this: LayoutFlexFormattingContext*):
    # all participating flex items on the line are aligned such that their baselines align, and the item with the
    # largest distance between its baseline and its cross-start margin edge is placed flush against the cross-start
    # edge of the line.

    # NB: This runs after layout_inside() so that line boxes are available for baseline computation.
    let lines = ak.vector_span(&this.m_flex_lines)
    for flex_line in &lines:
        if not flex_line.has_baseline_aligned_items:
            continue

        var max_baseline = layout_zero_px()
        for baseline_item in ak.vector_span(&flex_line.items):
            if layout_flex_formatting_context_alignment_for_item(this, baseline_item.box) == css_data.AlignItems.baseline:
                max_baseline = ak.max(max_baseline, layout_formatting_context_box_baseline(this.layout_formatting_context(), baseline_item.box))

        for item in ak.vector_span(&flex_line.items):
            if layout_flex_formatting_context_alignment_for_item(this, item.box) != css_data.AlignItems.baseline:
                continue

            let adjustment = max_baseline.sub(layout_formatting_context_box_baseline(this.layout_formatting_context(), item.box))
            if layout_flex_formatting_context_main_axis_is_horizontal(this):
                layout_state_used_values_set_content_y(item.used_values, item.used_values.offset.m_y.add(adjustment))
            else:
                layout_state_used_values_set_content_x(item.used_values, item.used_values.offset.m_x.add(adjustment))