Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / inline_formatting_context_1.lucb

#==============================================================================================
#
#   inline_formatting_context_1 - Layout::InlineFormattingContext: construction, floats beside
#                                 lines, running the context and dimensioning boxes on a line
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/InlineFormattingContext.cpp (lines 1-199) and
#       InlineFormattingContext.h: the context of a block container whose children are inline,
#       where the inline content of a line starts and how wide a line can be beside the floats
#       of the parent block formatting context, run (line boxes, then the automatic content
#       size), and the used size of an atomic inline-level box placed on a line.
#
#       The context lives where its creator puts it (BlockFormattingContext constructs it in
#       its frame, as the C++ does); its destructor is the default one.
#
#       Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Construction ============================================================================

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:19 Web::Layout::InlineFormattingContext::InlineFormattingContext.
pub func layout_inline_formatting_context_construct(this: LayoutInlineFormattingContext*, state: LayoutState*, layout_mode: LayoutMode, containing_block: LayoutBlockContainer*, containing_block_used_values: LayoutStateUsedValues*, parent: LayoutBlockFormattingContext*):
    layout_formatting_context_construct(this.layout_formatting_context(), LayoutFormattingContextType.inline, layout_mode, state, containing_block.layout_box(), parent.layout_formatting_context())
    layout_formatting_context_set_class(this.layout_formatting_context(), &layout_inline_formatting_context_class)
    this.m_containing_block_used_values = containing_block_used_values
    this.m_available_space = none
    this.m_automatic_content_width = layout_zero_px()
    this.m_automatic_content_height = layout_zero_px()
    this.m_vertical_float_clearance = layout_zero_px()

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:32 Web::Layout::InlineFormattingContext::parent.
pub func layout_inline_formatting_context_parent(this: LayoutInlineFormattingContext*) -> LayoutBlockFormattingContext*:
    let parent = layout_formatting_context_parent(this.layout_formatting_context()) else trap("an inline formatting context without its parent")
    return (LayoutBlockFormattingContext*)parent

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:37 Web::Layout::InlineFormattingContext::parent.
pub func layout_inline_formatting_context_parent_const(this: LayoutInlineFormattingContext*) -> LayoutBlockFormattingContext*:
    let parent = layout_formatting_context_parent_const(this.layout_formatting_context()) else trap("an inline formatting context without its parent")
    return (LayoutBlockFormattingContext*)parent

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.h:24 Web::Layout::InlineFormattingContext::containing_block.
pub func layout_inline_formatting_context_containing_block(this: LayoutInlineFormattingContext*) -> LayoutBlockContainer*:
    return (LayoutBlockContainer*)layout_formatting_context_context_box(this.layout_formatting_context())

## The available space the context runs in (`*m_available_space`, which run() sets).
func layout_inline_formatting_context_available_space(this: LayoutInlineFormattingContext*) -> LayoutAvailableSpace:
    return this.m_available_space else trap("an inline formatting context used before it runs")

# mark: Floats beside lines =====================================================================

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:42 Web::Layout::InlineFormattingContext::leftmost_inline_offset_at.
pub func layout_inline_formatting_context_leftmost_inline_offset_at(this: LayoutInlineFormattingContext*, y: gfx.CssPixels) -> gfx.CssPixels:
    let parent = layout_inline_formatting_context_parent_const(this)
    # NOTE: Floats are relative to the BFC root box, not necessarily the containing block of this IFC.
    let box_in_root_rect = layout_formatting_context_content_box_rect_in_ancestor_coordinate_space(this.layout_formatting_context(), this.m_containing_block_used_values, layout_block_formatting_context_root(parent).layout_box())
    let y_in_root = gfx.css_pixel_rect_y(&box_in_root_rect).add(y)
    let space_and_containing_margin = layout_block_formatting_context_space_used_and_containing_margin_for_floats(parent, y_in_root)

    # If there's no float box to take into account, we start at the left edge of the containing block.
    if space_and_containing_margin.matching_left_float_box == none:
        return layout_zero_px()

    # If the left edge of the containing block is to the right of the rightmost left-side float, start placing inline
    # content at the left edge of the containing block.
    let left_side_floats_limit_to_right = space_and_containing_margin.left_total_containing_margin.add(space_and_containing_margin.left_used_space)
    if gfx.css_pixel_rect_x(&box_in_root_rect).ge(left_side_floats_limit_to_right):
        return layout_zero_px()

    # The left edge of the containing block is to the left of the rightmost left-side float.
    # We adjust the inline content insertion point by the overlap between the containing block and the float.
    return left_side_floats_limit_to_right.sub(gfx.css_pixel_rect_x(&box_in_root_rect))

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:64 Web::Layout::InlineFormattingContext::available_space_for_line.
pub func layout_inline_formatting_context_available_space_for_line(this: LayoutInlineFormattingContext*, y: gfx.CssPixels) -> LayoutAvailableSize:
    let available_space = layout_inline_formatting_context_available_space(this)
    if not layout_available_size_is_definite(&available_space.width):
        return available_space.width

    let intrusions = layout_block_formatting_context_intrusion_by_floats_into_box_layout_state_used_values(layout_inline_formatting_context_parent_const(this), this.m_containing_block_used_values, y)
    return layout_available_size_make_definite(layout_available_size_to_px_or_zero(&available_space.width).sub(intrusions.left).sub(intrusions.right))

# mark: Running the context =====================================================================

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:73 Web::Layout::InlineFormattingContext::automatic_content_width (override).
pub func layout_inline_formatting_context_automatic_content_width_impl(base: LayoutFormattingContext*) -> gfx.CssPixels:
    let this = (LayoutInlineFormattingContext*)base
    return this.m_automatic_content_width

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:78 Web::Layout::InlineFormattingContext::automatic_content_height (override).
pub func layout_inline_formatting_context_automatic_content_height_impl(base: LayoutFormattingContext*) -> gfx.CssPixels:
    let this = (LayoutInlineFormattingContext*)base
    return this.m_automatic_content_height

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:83 Web::Layout::InlineFormattingContext::run (override).
pub func layout_inline_formatting_context_run_impl(base: LayoutFormattingContext*, available_space: LayoutAvailableSpace):
    let this = (LayoutInlineFormattingContext*)base
    let containing_block = layout_inline_formatting_context_containing_block(this)
    assert(layout_node_children_are_inline(containing_block.layout_node()))
    this.m_available_space = available_space
    layout_inline_formatting_context_generate_line_boxes(this)

    var content_height = layout_zero_px()

    let line_boxes = ak.vector_span(&this.m_containing_block_used_values.line_boxes)
    for line_box in &line_boxes:
        content_height = content_height.add(layout_line_box_height(line_box))

    # NOTE: We ask the parent BFC to calculate the automatic content width of this IFC.
    #       This ensures that any floated boxes are taken into account.
    this.m_automatic_content_width = layout_formatting_context_greatest_child_width(layout_inline_formatting_context_parent(this).layout_formatting_context(), containing_block.layout_box())
    this.m_automatic_content_height = content_height

# mark: Boxes on a line =========================================================================

## Ported from Libraries/LibWeb/Layout/InlineFormattingContext.cpp:101 Web::Layout::InlineFormattingContext::dimension_box_on_line.
pub func layout_inline_formatting_context_dimension_box_on_line(this: LayoutInlineFormattingContext*, box: LayoutBox*, layout_mode: LayoutMode):
    let base = this.layout_formatting_context()
    let available_space = layout_inline_formatting_context_available_space(this)
    let width_of_containing_block = layout_available_size_to_px_or_zero(&available_space.width)
    let box_state = layout_state_get_mutable(base.m_state, box.layout_node_with_style())
    let computed_values = layout_box_computed(box)
    let margin = css_computed_values_margin(computed_values)
    let padding = css_computed_values_padding(computed_values)
    let margin_left = css_length_box_left_const(&margin)
    let margin_right = css_length_box_right_const(&margin)
    let margin_top = css_length_box_top_const(&margin)
    let margin_bottom = css_length_box_bottom_const(&margin)
    let padding_left = css_length_box_left_const(&padding)
    let padding_right = css_length_box_right_const(&padding)
    let padding_top = css_length_box_top_const(&padding)
    let padding_bottom = css_length_box_bottom_const(&padding)

    box_state.margin_left = css_length_percentage_or_auto_to_px_or_zero(&margin_left, box.layout_node(), width_of_containing_block)
    box_state.border_left = css_computed_values_border_left(computed_values).width
    box_state.padding_left = css_length_percentage_or_auto_to_px_or_zero(&padding_left, box.layout_node(), width_of_containing_block)

    box_state.margin_right = css_length_percentage_or_auto_to_px_or_zero(&margin_right, box.layout_node(), width_of_containing_block)
    box_state.border_right = css_computed_values_border_right(computed_values).width
    box_state.padding_right = css_length_percentage_or_auto_to_px_or_zero(&padding_right, box.layout_node(), width_of_containing_block)

    box_state.margin_top = css_length_percentage_or_auto_to_px_or_zero(&margin_top, box.layout_node(), width_of_containing_block)
    box_state.border_top = css_computed_values_border_top(computed_values).width
    box_state.padding_top = css_length_percentage_or_auto_to_px_or_zero(&padding_top, box.layout_node(), width_of_containing_block)

    box_state.padding_bottom = css_length_percentage_or_auto_to_px_or_zero(&padding_bottom, box.layout_node(), width_of_containing_block)
    box_state.border_bottom = css_computed_values_border_bottom(computed_values).width
    box_state.margin_bottom = css_length_percentage_or_auto_to_px_or_zero(&margin_bottom, box.layout_node(), width_of_containing_block)

    if layout_formatting_context_box_is_sized_as_replaced_element(base, box, available_space):
        layout_state_used_values_set_content_width(box_state, layout_formatting_context_compute_width_for_replaced_element(base, box, available_space))
        layout_state_used_values_set_content_height(box_state, layout_formatting_context_compute_height_for_replaced_element(base, box, available_space))
        let replaced_formatting_context = layout_formatting_context_layout_inside(base, box, layout_mode, layout_state_used_values_available_inner_space_or_constraints_from(box_state, available_space))
        defer layout_formatting_context_destruct(replaced_formatting_context)
        if let context = replaced_formatting_context:
            layout_formatting_context_parent_context_did_dimension_child_root_box(context)
        return

    # Any box that has simple flow inside should have generated line box fragments already.
    let display = layout_node_display(box.layout_node())
    if css_display_is_flow_inside(&display):
        ak.dbgln("FIXME: InlineFormattingContext::dimension_box_on_line got unexpected box in inline context:")
        dump_tree_layout_node(box.layout_node())
        return

    let width_value = css_computed_values_width(computed_values)
    var unconstrained_width = layout_zero_px()
    if layout_formatting_context_should_treat_width_as_auto(base, box, available_space):
        let result = layout_formatting_context_calculate_shrink_to_fit_widths(base, box)

        if layout_available_size_is_definite(&available_space.width):
            let available_width = (layout_available_size_to_px_or_zero(&available_space.width).sub(box_state.margin_left).sub(box_state.border_left).sub(box_state.padding_left).sub(box_state.padding_right).sub(box_state.border_right).sub(box_state.margin_right))

            unconstrained_width = ak.min(ak.max(result.preferred_minimum_width, available_width), result.preferred_width)
        elif layout_available_size_is_min_content(&available_space.width):
            unconstrained_width = result.preferred_minimum_width
        else:
            unconstrained_width = result.preferred_width
    else:
        if css_size_contains_percentage(&width_value) and not layout_available_size_is_definite(&available_space.width):
            # NOTE: We can't resolve percentages yet. We'll have to wait until after inner layout.
            _ = 0
        else:
            let inner_width = layout_formatting_context_calculate_inner_width(base, box, available_space.width, width_value)
            unconstrained_width = inner_width

    var width = unconstrained_width
    if not layout_formatting_context_should_treat_max_width_as_none(base, box, available_space.width):
        let max_width = layout_formatting_context_calculate_inner_width(base, box, available_space.width, css_computed_values_max_width(computed_values))
        width = ak.min(width, max_width)

    let computed_min_width = css_computed_values_min_width(computed_values)
    if not css_size_is_auto(&computed_min_width):
        let min_width = layout_formatting_context_calculate_inner_width(base, box, available_space.width, computed_min_width)
        width = ak.max(width, min_width)

    layout_state_used_values_set_content_width(box_state, width)

    let parent = layout_inline_formatting_context_parent(this)
    layout_block_formatting_context_resolve_used_height_if_not_treated_as_auto(parent, box, LayoutAvailableSpace(width = layout_available_size_make_definite(width), height = layout_available_size_make_indefinite()))

    # NOTE: Flex containers with `auto` height are treated as `max-content`, so we can compute their height early.
    if css_display_is_flex_inside(&display):
        layout_block_formatting_context_resolve_used_height_if_treated_as_auto(parent, box, LayoutAvailableSpace(width = layout_available_size_make_definite(width), height = layout_available_size_make_indefinite()))

    let independent_formatting_context = layout_formatting_context_layout_inside(base, box, layout_mode, layout_state_used_values_available_inner_space_or_constraints_from(box_state, available_space))
    defer layout_formatting_context_destruct(independent_formatting_context)

    if layout_formatting_context_should_treat_height_as_auto(base, box, available_space):
        # FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
        layout_block_formatting_context_resolve_used_height_if_treated_as_auto(parent, box, available_space)
    else:
        layout_block_formatting_context_resolve_used_height_if_not_treated_as_auto(parent, box, available_space)

    if let context = independent_formatting_context:
        layout_formatting_context_parent_context_did_dimension_child_root_box(context)