Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / inline_level_iterator.lucb

#==============================================================================================
#
#   inline_level_iterator - Layout::InlineLevelIterator: the inline-level items of an inline
#                           formatting context
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/InlineLevelIterator.cpp (lines 1-463) and InlineLevelIterator.h:
#       walks the inline-level descendants of a block container in pre-order (not into
#       inline-blocks), resolves the box model metrics of inline nodes on the way in, and
#       pre-generates every item an inline formatting context places on lines: text chunks
#       (shaped into glyph runs, with tab stops and the bidi direction resolved from their
#       neighbours), atomic inline boxes (dimensioned on the line), forced breaks, and
#       absolutely positioned and floating boxes. The margins, borders and padding of the
#       inline nodes an item starts or ends are added to the item.
#
#       Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Item ====================================================================================

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.h:47 Web::Layout::InlineLevelIterator::Item::border_box_width.
pub func layout_inline_level_iterator_item_border_box_width(this: const LayoutInlineLevelIteratorItem*) -> gfx.CssPixels:
    return this.border_start.add(this.padding_start).add(this.width).add(this.padding_end).add(this.border_end)

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

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:18 Web::Layout::InlineLevelIterator::InlineLevelIterator.
pub func layout_inline_level_iterator_construct(this: LayoutInlineLevelIterator*, inline_formatting_context: LayoutInlineFormattingContext*, layout_state: LayoutState*, containing_block: LayoutBlockContainer*, containing_block_used_values: const LayoutStateUsedValues*, layout_mode: LayoutMode):
    this.m_inline_formatting_context = inline_formatting_context
    this.m_layout_state = layout_state
    this.m_containing_block = containing_block
    this.m_containing_block_used_values = containing_block_used_values
    this.m_current_node = none
    this.m_next_node = tree_node_first_child(containing_block.layout_node())
    this.m_layout_mode = layout_mode
    this.m_text_node_context = LayoutInlineLevelIteratorTextNodeContext()
    this.m_has_text_node_context = false
    this.m_extra_leading_metrics = none
    this.m_extra_trailing_metrics = none
    this.m_box_model_node_stack = ak.Vector[LayoutNodeWithStyleAndBoxModelMetrics*]()
    this.m_items = ak.Vector[LayoutInlineLevelIteratorItem]()
    this.m_next_item_index = 0
    this.m_accumulated_width_for_tabs = layout_zero_px()
    this.m_previous_chunk_can_break_after = false

    layout_inline_level_iterator_skip_to_next(this)
    layout_inline_level_iterator_generate_all_items(this)

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:30 Web::Layout::InlineLevelIterator::generate_all_items.
func layout_inline_level_iterator_generate_all_items(this: LayoutInlineLevelIterator*):
    while true:
        let item = layout_inline_level_iterator_generate_next_item(this) else break

        # Track accumulated width for tab calculations.
        # Reset on forced breaks since tabs measure from line start.
        if item.type_ == LayoutInlineLevelIteratorItemType.forced_break:
            this.m_accumulated_width_for_tabs = layout_zero_px()
        else:
            this.m_accumulated_width_for_tabs = this.m_accumulated_width_for_tabs.add(layout_inline_level_iterator_item_border_box_width(&item))

        ak.vector_append_t(&this.m_items, item)

# mark: Box model metrics of inline nodes =======================================================

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:49 Web::Layout::InlineLevelIterator::enter_node_with_box_model_metrics.
func layout_inline_level_iterator_enter_node_with_box_model_metrics(this: LayoutInlineLevelIterator*, node: LayoutNodeWithStyleAndBoxModelMetrics*):
    var extra_leading_metrics = this.m_extra_leading_metrics else LayoutInlineLevelIteratorExtraBoxMetrics()

    # FIXME: It's really weird that *this* is where we assign box model metrics for these layout nodes..

    let used_values = layout_state_get_mutable(this.m_layout_state, node.layout_node_with_style())
    let computed_values = &layout_node_computed_values(node.layout_node()).computed_values
    let containing_block_content_width = layout_state_used_values_content_width(this.m_containing_block_used_values)
    let margin = css_computed_values_margin(computed_values)
    let padding = css_computed_values_padding(computed_values)
    let margin_top = css_length_box_top_const(&margin)
    let margin_bottom = css_length_box_bottom_const(&margin)
    let margin_left = css_length_box_left_const(&margin)
    let margin_right = css_length_box_right_const(&margin)
    let padding_left = css_length_box_left_const(&padding)
    let padding_right = css_length_box_right_const(&padding)
    let padding_bottom = css_length_box_bottom_const(&padding)
    let padding_top = css_length_box_top_const(&padding)

    used_values.margin_top = css_length_percentage_or_auto_to_px_or_zero(&margin_top, node.layout_node(), containing_block_content_width)
    used_values.margin_bottom = css_length_percentage_or_auto_to_px_or_zero(&margin_bottom, node.layout_node(), containing_block_content_width)

    used_values.margin_left = css_length_percentage_or_auto_to_px_or_zero(&margin_left, node.layout_node(), containing_block_content_width)
    used_values.border_left = css_computed_values_border_left(computed_values).width
    used_values.padding_left = css_length_percentage_or_auto_to_px_or_zero(&padding_left, node.layout_node(), containing_block_content_width)

    used_values.margin_right = css_length_percentage_or_auto_to_px_or_zero(&margin_right, node.layout_node(), containing_block_content_width)
    used_values.border_right = css_computed_values_border_right(computed_values).width
    used_values.padding_right = css_length_percentage_or_auto_to_px_or_zero(&padding_right, node.layout_node(), containing_block_content_width)

    used_values.border_top = css_computed_values_border_top(computed_values).width
    used_values.border_bottom = css_computed_values_border_bottom(computed_values).width
    used_values.padding_bottom = css_length_percentage_or_auto_to_px_or_zero(&padding_bottom, node.layout_node(), containing_block_content_width)
    used_values.padding_top = css_length_percentage_or_auto_to_px_or_zero(&padding_top, node.layout_node(), containing_block_content_width)

    extra_leading_metrics.margin = extra_leading_metrics.margin.add(used_values.margin_left)
    extra_leading_metrics.border = extra_leading_metrics.border.add(used_values.border_left)
    extra_leading_metrics.padding = extra_leading_metrics.padding.add(used_values.padding_left)
    this.m_extra_leading_metrics = extra_leading_metrics

    # Now's our chance to resolve the inset properties for this node.
    let context = this.m_inline_formatting_context.layout_formatting_context()
    let content_box_rect = layout_formatting_context_content_box_rect_layout_state_used_values(context, this.m_containing_block_used_values)
    layout_formatting_context_compute_inset(context, node, gfx.css_pixel_rect_size(&content_box_rect))

    ak.vector_append_t(&this.m_box_model_node_stack, node)

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:85 Web::Layout::InlineLevelIterator::exit_node_with_box_model_metrics.
func layout_inline_level_iterator_exit_node_with_box_model_metrics(this: LayoutInlineLevelIterator*):
    var extra_trailing_metrics = this.m_extra_trailing_metrics else LayoutInlineLevelIteratorExtraBoxMetrics()

    let node = *ak.vector_last(&this.m_box_model_node_stack)
    let used_values = layout_state_get_mutable(this.m_layout_state, node.layout_node_with_style())

    extra_trailing_metrics.margin = extra_trailing_metrics.margin.add(used_values.margin_right)
    extra_trailing_metrics.border = extra_trailing_metrics.border.add(used_values.border_right)
    extra_trailing_metrics.padding = extra_trailing_metrics.padding.add(used_values.padding_right)
    this.m_extra_trailing_metrics = extra_trailing_metrics

    _ = ak.vector_take_last(&this.m_box_model_node_stack)

## Whether `node` is the last node on the box model node stack (`m_box_model_node_stack.last() == node`).
func layout_inline_level_iterator_is_last_on_box_model_node_stack(this: LayoutInlineLevelIterator*, node: LayoutNode*?) -> bool:
    if ak.vector_is_empty(&this.m_box_model_node_stack):
        return false
    let last = *ak.vector_last(&this.m_box_model_node_stack)
    return (LayoutNode*?)last.layout_node() == node

# mark: Walking the inline-level nodes ==========================================================

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:101 Web::Layout::InlineLevelIterator::next_inline_node_in_pre_order.
# This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
func layout_inline_level_iterator_next_inline_node_in_pre_order(this: LayoutInlineLevelIterator*, current: LayoutNode*, stay_within: LayoutNode*?) -> LayoutNode*?:
    if let first_child = tree_node_first_child(current):
        let first_child_display = layout_node_display(first_child)
        let current_display = layout_node_display(current)
        if css_display_is_inline_outside(&first_child_display) and css_display_is_flow_inside(&current_display) and not layout_node_is_replaced_box(current):
            if not layout_node_is_box(current) or not layout_node_is_out_of_flow_layout_formatting_context(current, this.m_inline_formatting_context.layout_formatting_context()):
                return first_child

    var node = current
    var next: LayoutNode*? = none
    while true:
        next = tree_node_next_sibling(node)
        if next != none:
            break
        var parent: LayoutNode*? = none
        if let parent_with_style = layout_node_parent(node):
            parent = parent_with_style.layout_node()

        # If node is the last node on the "box model node stack", pop it off.
        if layout_inline_level_iterator_is_last_on_box_model_node_stack(this, parent):
            layout_inline_level_iterator_exit_node_with_box_model_metrics(this)
        let parent_node = parent else return none
        if parent == stay_within:
            return none
        node = parent_node

    # If node is the last node on the "box model node stack", pop it off.
    if layout_inline_level_iterator_is_last_on_box_model_node_stack(this, node):
        layout_inline_level_iterator_exit_node_with_box_model_metrics(this)

    return next

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:134 Web::Layout::InlineLevelIterator::compute_next.
func layout_inline_level_iterator_compute_next(this: LayoutInlineLevelIterator*):
    if this.m_next_node == none:
        return
    let context = this.m_inline_formatting_context.layout_formatting_context()
    while true:
        let node = this.m_next_node else trap("unreachable: the loop runs while there is a next node")
        this.m_next_node = layout_inline_level_iterator_next_inline_node_in_pre_order(this, node, this.m_containing_block.layout_node())
        if let next_node = this.m_next_node:
            if layout_node_is_svg_mask_box(next_node):
                # NOTE: It is possible to encounter SVGMaskBox nodes while doing layout of formatting context established by <foreignObject> with a mask.
                #       We should skip and let SVGFormattingContext take care of them.
                this.m_next_node = tree_node_next_sibling(next_node)
        let next_node = this.m_next_node else break
        if layout_node_is_inline(next_node) or layout_node_is_out_of_flow_layout_formatting_context(next_node, context):
            break

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:148 Web::Layout::InlineLevelIterator::skip_to_next.
func layout_inline_level_iterator_skip_to_next(this: LayoutInlineLevelIterator*):
    if let next_node = this.m_next_node:
        let display = layout_node_display(next_node)
        if is_layout_node_with_style_and_box_model_metrics(next_node) and css_display_is_flow_inside(&display) and not layout_node_is_out_of_flow_layout_formatting_context(next_node, this.m_inline_formatting_context.layout_formatting_context()) and not layout_node_is_replaced_box(next_node):
            layout_inline_level_iterator_enter_node_with_box_model_metrics(this, (LayoutNodeWithStyleAndBoxModelMetrics*)next_node)

    this.m_current_node = this.m_next_node
    layout_inline_level_iterator_compute_next(this)

# mark: Items ===================================================================================

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:161 Web::Layout::InlineLevelIterator::next.
pub func layout_inline_level_iterator_next(this: LayoutInlineLevelIterator*) -> LayoutInlineLevelIteratorItem?:
    if this.m_next_item_index >= ak.vector_size(&this.m_items):
        return none
    let item = *ak.vector_at(&this.m_items, this.m_next_item_index)
    this.m_next_item_index += 1
    return item

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:168 Web::Layout::InlineLevelIterator::next_non_whitespace_sequence_width.
pub func layout_inline_level_iterator_next_non_whitespace_sequence_width(this: LayoutInlineLevelIterator*) -> gfx.CssPixels:
    var next_width = layout_zero_px()
    for i in this.m_next_item_index..<ak.vector_size(&this.m_items):
        let next_item = ak.vector_at_const(&this.m_items, i)
        if next_item.type_ == LayoutInlineLevelIteratorItemType.forced_break:
            break
        let next_node = next_item.node else trap("an item without a node")
        if css_computed_values_text_wrap_mode(&layout_node_computed_values(next_node).computed_values) == css_data.TextWrapMode.wrap:
            if next_item.type_ != LayoutInlineLevelIteratorItemType.text:
                break
            if next_item.is_collapsible_whitespace:
                break
            let next_text_node = as_layout_text_node(next_node)
            let text_for_rendering = layout_text_node_text_for_rendering(next_text_node)
            let text = ak.utf16_string_base_utf16_view(&text_for_rendering.utf16_string_base)
            let next_view = ak.utf16_view_substring_view_usize(&text, next_item.offset_in_node, next_item.length_in_node)
            if ak.utf16_view_is_ascii_whitespace(&next_view):
                break
        next_width = next_width.add(layout_inline_level_iterator_item_border_box_width(next_item))
    return next_width

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:190 Web::Layout::InlineLevelIterator::resolve_text_direction_from_context.
func layout_inline_level_iterator_resolve_text_direction_from_context(this: LayoutInlineLevelIterator*) -> web_fonts.GlyphRunTextType:
    assert(this.m_has_text_node_context)
    let context = &this.m_text_node_context

    # Search forward in the pre-generated chunks array to find the next chunk with known direction.
    # Since chunks are pre-generated, this is just O(1) array access per iteration.
    var next_known_direction: web_fonts.GlyphRunTextType? = none
    for i in context.next_chunk_index..<ak.vector_size(&context.chunks):
        let chunk = ak.vector_at_const(&context.chunks, i)
        if chunk.text_type == web_fonts.GlyphRunTextType.ltr or chunk.text_type == web_fonts.GlyphRunTextType.rtl:
            next_known_direction = chunk.text_type
            break

    let last_known_direction = context.last_known_direction

    if let last = last_known_direction:
        if let next = next_known_direction:
            if last != next:
                match css_computed_values_direction(&layout_node_computed_values(this.m_containing_block.layout_node()).computed_values):
                    .ltr:
                        return web_fonts.GlyphRunTextType.ltr
                    .rtl:
                        return web_fonts.GlyphRunTextType.rtl
                    _:
                        _ = 0

    if let last = last_known_direction:
        return last
    if let next = next_known_direction:
        return next

    return web_fonts.GlyphRunTextType.context_dependent

## Whether an empty text node is an editable empty text field (the `is_empty_editable` test of
## InlineLevelIterator::generate_next_item).
func layout_inline_level_iterator_is_empty_editable(text_node: LayoutTextNode*) -> bool:
    let dom_node = layout_text_node_dom_node(text_node).dom_node()
    var is_empty_editable = false
    if let shadow_root = as_if_dom_shadow_root(tree_node_root(dom_node)):
        if let host = dom_document_fragment_host(shadow_root.dom_document_fragment()):
            if let form_associated_element = as_if_form_associated_text_control_element(host):
                let html_element = html_form_associated_text_control_element_text_control_to_html_element(form_associated_element)
                is_empty_editable = html_form_associated_element_is_mutable(&html_element.form_associated_element)
    if let parent = tree_node_parent(dom_node):
        is_empty_editable = is_empty_editable or dom_node_is_editing_host(parent)
    return is_empty_editable

## The width of the tab stops a chunk's leading tabs advance to (the `has_breaking_tab` branch of
## InlineLevelIterator::generate_next_item); drops the tabs from the chunk's view.
func layout_inline_level_iterator_tab_stop_width(this: LayoutInlineLevelIterator*, text_node: LayoutTextNode*, chunk: LayoutTextNodeChunk*, letter_spacing: gfx.CssPixels, word_spacing: gfx.CssPixels) -> f32:
    # Use the accumulated width we've been tracking during pre-generation.
    # This accounts for items that would appear before this tab on the same line.
    let accumulated_width = this.m_accumulated_width_for_tabs

    # https://drafts.csswg.org/css-text/#tab-size-property
    let tab_width = match css_computed_values_tab_size(&layout_node_computed_values(text_node.layout_node()).computed_values):
        .css_length(length) => css_length_absolute_length_to_px(&length)
        .f64_(tab_number) => gfx.css_pixels_nearest_value_for_f64(tab_number * (f64)(web_fonts.font_glyph_width(chunk.font, 0x20) + word_spacing.to_float() + letter_spacing.to_float()))

    # https://drafts.csswg.org/css-text/#white-space-phase-2
    # if fragments have added to the width, calculate the net distance to the next tab stop, otherwise the shift will just be the tab width
    var tab_stop_dist = tab_width
    if accumulated_width.gt(layout_zero_px()):
        let fraction = accumulated_width.div(tab_width)
        tab_stop_dist = gfx.ceil(gfx.css_pixel_fraction_to_css_pixels(&fraction)).mul(tab_width).sub(accumulated_width)
    let ch_width = web_fonts.font_glyph_width(chunk.font, 0x30)

    # If this distance is less than 0.5ch, then the subsequent tab stop is used instead
    if tab_stop_dist.to_double() < (f64)ch_width * 0.5:
        tab_stop_dist = tab_stop_dist.add(tab_width)

    # account for consecutive tabs
    var num_of_tabs: i64 = 0
    var it = ak.utf16_view_begin(&chunk.view)
    while not ak.utf16_code_point_iterator_done(&it):
        if ak.utf16_code_point_iterator_deref(&it) != 0x09:
            break
        num_of_tabs += 1
        _ = ak.utf16_code_point_iterator_inc(&it)
    tab_stop_dist = tab_stop_dist.mul(gfx.CssPixels.from_int(num_of_tabs))

    # remove tabs, we don't want to render them when we shape the text
    chunk.view = ak.utf16_view_substring_view(&chunk.view, (usize)num_of_tabs)
    return tab_stop_dist.to_float()

## The item of the text node the iterator is in (the text node branch of InlineLevelIterator::generate_next_item).
func layout_inline_level_iterator_generate_next_text_item(this: LayoutInlineLevelIterator*, text_node: LayoutTextNode*) -> LayoutInlineLevelIteratorItem?:
    if not this.m_has_text_node_context:
        layout_inline_level_iterator_enter_text_node(this, text_node)
    let context = &this.m_text_node_context

    # Track chunk position locally
    let is_first_chunk = context.next_chunk_index == 0

    # Get the next chunk from the pre-generated array
    var chunk_or_none: LayoutTextNodeChunk? = none
    var has_chunk = false
    if context.next_chunk_index < ak.vector_size(&context.chunks):
        chunk_or_none = *ak.vector_at(&context.chunks, context.next_chunk_index)
        has_chunk = true
        context.next_chunk_index += 1

    var is_last_chunk = context.next_chunk_index >= ak.vector_size(&context.chunks)

    var is_empty_editable = false
    if not has_chunk:
        let is_only_chunk = is_first_chunk and is_last_chunk
        let text_for_rendering = layout_text_node_text_for_rendering(text_node)
        if is_only_chunk and ak.utf16_string_base_is_empty(&text_for_rendering.utf16_string_base):
            is_empty_editable = layout_inline_level_iterator_is_empty_editable(text_node)

        if is_empty_editable:
            # Create an empty chunk for editable empty text fields
            chunk_or_none = LayoutTextNodeChunk(
                view = ak.utf16_view_new(),
                font = web_fonts.font_cascade_list_first(css_computed_values_font_list(&layout_node_computed_values(text_node.layout_node()).computed_values)),
                is_all_whitespace = true,
                text_type = web_fonts.GlyphRunTextType.common)
            # Advance the index so the next call will move to the next node
            context.next_chunk_index = 1
        else:
            this.m_text_node_context = LayoutInlineLevelIteratorTextNodeContext()
            this.m_has_text_node_context = false
            this.m_previous_chunk_can_break_after = false
            layout_inline_level_iterator_skip_to_next(this)
            return layout_inline_level_iterator_generate_next_item(this)

    var chunk = chunk_or_none else trap("unreachable: the chunk was just set")
    var text_type = chunk.text_type
    if text_type == web_fonts.GlyphRunTextType.ltr or text_type == web_fonts.GlyphRunTextType.rtl:
        context.last_known_direction = text_type

    let do_respect_linebreak = context.should_respect_linebreaks
    if do_respect_linebreak and chunk.has_breaking_newline:
        is_last_chunk = true
        if chunk.is_all_whitespace:
            text_type = web_fonts.GlyphRunTextType.end_padding

    if text_type == web_fonts.GlyphRunTextType.context_dependent:
        text_type = layout_inline_level_iterator_resolve_text_direction_from_context(this)

    if do_respect_linebreak and chunk.has_breaking_newline:
        return LayoutInlineLevelIteratorItem(type_ = LayoutInlineLevelIteratorItemType.forced_break)

    let computed_values = &layout_node_computed_values(text_node.layout_node()).computed_values
    let letter_spacing = css_computed_values_letter_spacing(computed_values)
    # FIXME: We should apply word spacing to all word-separator characters not just breaking tabs
    let word_spacing = css_computed_values_word_spacing(computed_values)

    var x: f32 = 0.0
    if chunk.has_breaking_tab:
        x = layout_inline_level_iterator_tab_stop_width(this, text_node, &chunk, letter_spacing, word_spacing)

    let glyph_run = web_fonts.shape_text_f32(gfx.FloatPoint(m_x = x, m_y = 0.0), letter_spacing.to_float(), chunk.view, chunk.font, text_type)

    let chunk_width = gfx.css_pixels_nearest_value_for_f32(web_fonts.glyph_run_width(glyph_run) + x)

    # NOTE: We never consider `content: ""` to be collapsible whitespace.
    let is_generated_empty_string = is_empty_editable or (layout_node_is_generated_for_pseudo_element(text_node.layout_node()) and chunk.length == 0)
    let collapse_whitespace = this.m_text_node_context.should_collapse_whitespace

    var item = LayoutInlineLevelIteratorItem(
        type_ = LayoutInlineLevelIteratorItemType.text,
        node = text_node.layout_node(),
        glyph_run = glyph_run,
        offset_in_node = chunk.start,
        length_in_node = chunk.length,
        width = chunk_width,
        is_collapsible_whitespace = collapse_whitespace and chunk.is_all_whitespace and not is_generated_empty_string,
        can_break_before = this.m_previous_chunk_can_break_after)

    this.m_previous_chunk_can_break_after = chunk.can_break_after

    layout_inline_level_iterator_add_extra_box_model_metrics_to_item(this, &item, is_first_chunk, is_last_chunk)
    return item

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:224 Web::Layout::InlineLevelIterator::generate_next_item.
func layout_inline_level_iterator_generate_next_item(this: LayoutInlineLevelIterator*) -> LayoutInlineLevelIteratorItem?:
    let current_node = this.m_current_node else return none

    if let text_node = as_if_layout_text_node(current_node):
        return layout_inline_level_iterator_generate_next_text_item(this, text_node)

    if layout_node_is_absolutely_positioned(current_node):
        layout_inline_level_iterator_skip_to_next(this)
        return LayoutInlineLevelIteratorItem(type_ = LayoutInlineLevelIteratorItemType.absolutely_positioned_element, node = current_node)

    if layout_node_is_floating(current_node):
        layout_inline_level_iterator_skip_to_next(this)
        return LayoutInlineLevelIteratorItem(type_ = LayoutInlineLevelIteratorItemType.floating_element, node = current_node)

    if is_layout_break_node(current_node):
        layout_inline_level_iterator_skip_to_next(this)
        return LayoutInlineLevelIteratorItem(type_ = LayoutInlineLevelIteratorItemType.forced_break, node = current_node)

    if is_layout_list_item_marker_box(current_node):
        layout_inline_level_iterator_skip_to_next(this)
        return layout_inline_level_iterator_generate_next_item(this)

    if not is_layout_box(current_node):
        layout_inline_level_iterator_skip_to_next(this)
        return layout_inline_level_iterator_generate_next_item(this)

    let box = as_layout_box(current_node)
    let box_state = layout_state_get(this.m_layout_state, box.layout_node_with_style())
    layout_inline_formatting_context_dimension_box_on_line(this.m_inline_formatting_context, box, this.m_layout_mode)

    var item = LayoutInlineLevelIteratorItem(
        type_ = LayoutInlineLevelIteratorItemType.element,
        node = box.layout_node(),
        offset_in_node = 0,
        length_in_node = 0,
        width = layout_state_used_values_content_width(box_state),
        padding_start = box_state.padding_left,
        padding_end = box_state.padding_right,
        border_start = box_state.border_left,
        border_end = box_state.border_right,
        margin_start = box_state.margin_left,
        margin_end = box_state.margin_right)
    layout_inline_level_iterator_add_extra_box_model_metrics_to_item(this, &item, true, true)
    layout_inline_level_iterator_skip_to_next(this)
    return item

# mark: Text nodes ==============================================================================

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:417 Web::Layout::InlineLevelIterator::enter_text_node.
func layout_inline_level_iterator_enter_text_node(this: LayoutInlineLevelIterator*, text_node: LayoutTextNode*):
    let computed_values = &layout_node_computed_values(text_node.layout_node()).computed_values
    let white_space_collapse = css_computed_values_white_space_collapse(computed_values)
    let text_wrap_mode = css_computed_values_text_wrap_mode(computed_values)

    # https://drafts.csswg.org/css-text-4/#collapse
    let do_wrap_lines = text_wrap_mode == css_data.TextWrapMode.wrap
    let do_respect_linebreaks = white_space_collapse == css_data.WhiteSpaceCollapse.preserve or white_space_collapse == css_data.WhiteSpaceCollapse.preserve_breaks or white_space_collapse == css_data.WhiteSpaceCollapse.break_spaces

    # Pre-generate all chunks for this text node upfront.
    # This allows O(1) peek() and next() operations instead of lazy generation with O(n) queue operations.
    var chunk_iterator: LayoutTextNodeChunkIterator = ---
    layout_text_node_chunk_iterator_construct(&chunk_iterator, text_node, do_wrap_lines, do_respect_linebreaks)
    var chunks = ak.Vector[LayoutTextNodeChunk]()
    while let chunk = layout_text_node_chunk_iterator_next(&chunk_iterator):
        ak.vector_append_t(&chunks, chunk)

    this.m_text_node_context = LayoutInlineLevelIteratorTextNodeContext(
        chunks = chunks,
        next_chunk_index = 0,
        should_collapse_whitespace = layout_text_node_chunk_iterator_should_collapse_whitespace(&chunk_iterator),
        should_wrap_lines = do_wrap_lines,
        should_respect_linebreaks = do_respect_linebreaks)
    this.m_has_text_node_context = true

## Ported from Libraries/LibWeb/Layout/InlineLevelIterator.cpp:446 Web::Layout::InlineLevelIterator::add_extra_box_model_metrics_to_item.
func layout_inline_level_iterator_add_extra_box_model_metrics_to_item(this: LayoutInlineLevelIterator*, item: LayoutInlineLevelIteratorItem*, add_leading_metrics: bool, add_trailing_metrics: bool):
    if add_leading_metrics:
        if let extra_leading_metrics = this.m_extra_leading_metrics:
            item.margin_start = item.margin_start.add(extra_leading_metrics.margin)
            item.border_start = item.border_start.add(extra_leading_metrics.border)
            item.padding_start = item.padding_start.add(extra_leading_metrics.padding)
            this.m_extra_leading_metrics = none

    if add_trailing_metrics:
        if let extra_trailing_metrics = this.m_extra_trailing_metrics:
            item.margin_end = item.margin_end.add(extra_trailing_metrics.margin)
            item.border_end = item.border_end.add(extra_trailing_metrics.border)
            item.padding_end = item.padding_end.add(extra_trailing_metrics.padding)
            this.m_extra_trailing_metrics = none