Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / line_box.lucb

#==============================================================================================
#
#   line_box - Layout::LineBox: one line of an inline formatting context
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/LineBox.cpp (lines 1-152) and LineBox.h: a line box's physical
#       width, height and bottom, adding fragments (a text chunk that continues the last
#       fragment's text in the same font extends it instead), and measuring or trimming the
#       collapsible whitespace at the end of the line.
#
#       Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Construction and accessors ==============================================================

## Ported from Libraries/LibWeb/Layout/LineBox.h:16 Web::Layout::LineBox::LineBox.
pub func layout_line_box_construct(this: LayoutLineBox*, direction: css_data.Direction, writing_mode: css_data.WritingMode):
    this.m_fragments = ak.Vector[LayoutLineBoxFragment]()
    this.m_inline_length = layout_zero_px()
    this.m_block_length = layout_zero_px()
    this.m_bottom = layout_zero_px()
    this.m_baseline = layout_zero_px()
    layout_line_box_init_fields(this)
    this.m_has_break = false
    this.m_has_forced_break = false
    this.m_direction = direction
    this.m_writing_mode = writing_mode

## A LineBox by value (`LineBox(direction, writing_mode)`).
pub func layout_line_box_new(direction: css_data.Direction, writing_mode: css_data.WritingMode) -> LayoutLineBox:
    var line_box: LayoutLineBox
    layout_line_box_construct(&line_box, direction, writing_mode)
    return line_box

## Ported from Libraries/LibWeb/Layout/LineBox.h:26 Web::Layout::LineBox::inline_length.
pub func layout_line_box_inline_length(this: const LayoutLineBox*) -> gfx.CssPixels:
    return this.m_inline_length

## Ported from Libraries/LibWeb/Layout/LineBox.h:27 Web::Layout::LineBox::block_length.
pub func layout_line_box_block_length(this: const LayoutLineBox*) -> gfx.CssPixels:
    return this.m_block_length

## Ported from Libraries/LibWeb/Layout/LineBox.h:28 Web::Layout::LineBox::baseline.
pub func layout_line_box_baseline(this: const LayoutLineBox*) -> gfx.CssPixels:
    return this.m_baseline

## Ported from Libraries/LibWeb/Layout/LineBox.h:32 Web::Layout::LineBox::fragments.
pub func layout_line_box_fragments_const(this: const LayoutLineBox*) -> const ak.Vector[LayoutLineBoxFragment]*:
    return &this.m_fragments

## Ported from Libraries/LibWeb/Layout/LineBox.h:33 Web::Layout::LineBox::fragments.
pub func layout_line_box_fragments(this: LayoutLineBox*) -> ak.Vector[LayoutLineBoxFragment]*:
    return &this.m_fragments

## Ported from Libraries/LibWeb/Layout/LineBox.h:39 Web::Layout::LineBox::is_empty.
pub func layout_line_box_is_empty(this: const LayoutLineBox*) -> bool:
    return ak.vector_is_empty(&this.m_fragments) and not this.m_has_break

## Ported from Libraries/LibWeb/Layout/LineBox.h:41 Web::Layout::LineBox::original_available_width.
pub func layout_line_box_original_available_width(this: const LayoutLineBox*) -> LayoutAvailableSize:
    return this.m_original_available_width

# mark: Geometry ================================================================================

## Ported from Libraries/LibWeb/Layout/LineBox.cpp:19 Web::Layout::LineBox::width.
pub func layout_line_box_width(this: const LayoutLineBox*) -> gfx.CssPixels:
    if this.m_writing_mode != css_data.WritingMode.horizontal_tb:
        return this.m_block_length
    return this.m_inline_length

## Ported from Libraries/LibWeb/Layout/LineBox.cpp:26 Web::Layout::LineBox::height.
pub func layout_line_box_height(this: const LayoutLineBox*) -> gfx.CssPixels:
    if this.m_writing_mode != css_data.WritingMode.horizontal_tb:
        return this.m_inline_length
    return this.m_block_length

## Ported from Libraries/LibWeb/Layout/LineBox.cpp:33 Web::Layout::LineBox::bottom.
pub func layout_line_box_bottom(this: const LayoutLineBox*) -> gfx.CssPixels:
    if this.m_writing_mode != css_data.WritingMode.horizontal_tb:
        return this.m_inline_length
    return this.m_bottom

# mark: Fragments ===============================================================================

## Ported from Libraries/LibWeb/Layout/LineBox.cpp:40 Web::Layout::LineBox::add_fragment.
pub func layout_line_box_add_fragment(this: LayoutLineBox*, layout_node: LayoutNode*, start: usize, length: usize, leading_size: gfx.CssPixels, trailing_size: gfx.CssPixels, leading_margin: gfx.CssPixels, trailing_margin: gfx.CssPixels, content_width: gfx.CssPixels, content_height: gfx.CssPixels, border_box_top: gfx.CssPixels, border_box_bottom: gfx.CssPixels, glyph_run: web_fonts.GlyphRun*? = none):
    let text_align_is_justify = css_computed_values_text_align(&layout_node_computed_values(layout_node).computed_values) == css_data.TextAlign.justify
    var extends_last_fragment = false
    if let run = glyph_run:
        if not text_align_is_justify and not ak.vector_is_empty(&this.m_fragments):
            let last = ak.vector_last(&this.m_fragments)
            if layout_line_box_fragment_layout_node(last) == layout_node:
                let last_run = last.m_glyph_run else trap("a text fragment without a glyph run")
                if web_fonts.glyph_run_font(last_run) == web_fonts.glyph_run_font(run) and layout_line_box_fragment_start(last) + layout_line_box_fragment_length_in_code_units(last) == start:
                    extends_last_fragment = true

    if extends_last_fragment:
        # The fragment we're adding is from the last Layout::Node on the line.
        # Expand the last fragment instead of adding a new one with the same Layout::Node.
        let last = ak.vector_last(&this.m_fragments)
        last.m_length_in_code_units += length
        layout_line_box_fragment_append_glyph_run(last, glyph_run, content_width)
    else:
        let inline_offset = leading_margin.add(leading_size).add(this.m_inline_length)
        let block_offset = layout_zero_px()
        ak.vector_append_t(&this.m_fragments, layout_line_box_fragment_new(layout_node, start, length, inline_offset, block_offset, content_width,
                                                                           content_height, border_box_top, this.m_direction, this.m_writing_mode, glyph_run))
    this.m_inline_length = this.m_inline_length.add(leading_margin.add(leading_size).add(content_width).add(trailing_size).add(trailing_margin))
    this.m_block_length = ak.max(this.m_block_length, content_height.add(border_box_top).add(border_box_bottom))

## The `should_trim` lambda of LineBox::calculate_or_trim_trailing_whitespace.
func layout_line_box_should_trim(fragment: const LayoutLineBoxFragment*) -> bool:
    let white_space_collapse = css_computed_values_white_space_collapse(&layout_node_computed_values(layout_line_box_fragment_layout_node(fragment)).computed_values)

    return white_space_collapse == css_data.WhiteSpaceCollapse.collapse or white_space_collapse == css_data.WhiteSpaceCollapse.preserve_breaks

## Ported from Libraries/LibWeb/Layout/LineBox.cpp:63 Web::Layout::LineBox::calculate_or_trim_trailing_whitespace.
pub func layout_line_box_calculate_or_trim_trailing_whitespace(this: LayoutLineBox*, should_remove: LayoutLineBoxRemoveTrailingWhitespace) -> gfx.CssPixels:
    var whitespace_width = layout_zero_px()
    var trailing_whitespace_width = layout_zero_px()
    var last_fragment_or_none: LayoutLineBoxFragment*? = none
    var fragment_index = ak.vector_size(&this.m_fragments)
    while true:
        if fragment_index == 0:
            return whitespace_width

        fragment_index -= 1
        let fragment = ak.vector_at(&this.m_fragments, fragment_index)
        last_fragment_or_none = fragment
        if let dom_node = layout_node_dom_node(layout_line_box_fragment_layout_node(fragment)):
            if let cursor_position = dom_document_cursor_position(dom_node_document(dom_node)):
                if dom_position_node(cursor_position) == dom_node:
                    return whitespace_width
        if not layout_line_box_should_trim(fragment):
            return whitespace_width
        if not layout_line_box_fragment_is_justifiable_whitespace(fragment):
            break

        whitespace_width = whitespace_width.add(layout_line_box_fragment_inline_length(fragment))
        trailing_whitespace_width = trailing_whitespace_width.add(layout_line_box_fragment_inline_length(fragment))
        if should_remove == LayoutLineBoxRemoveTrailingWhitespace.yes:
            this.m_inline_length = this.m_inline_length.sub(layout_line_box_fragment_inline_length(fragment))
            ak.vector_remove(&this.m_fragments, fragment_index)

    let last_fragment = last_fragment_or_none else trap("unreachable: the loop sets the last fragment")
    let last_text = layout_line_box_fragment_text(last_fragment)
    if ak.utf16_view_is_empty(&last_text):
        # No text to trim, but we may have removed whitespace-only fragments.
        if should_remove == LayoutLineBoxRemoveTrailingWhitespace.yes and trailing_whitespace_width.gt(layout_zero_px()):
            layout_line_box_fragment_set_has_trailing_whitespace(last_fragment, true)
        return whitespace_width

    # Trim trailing whitespace characters from the last fragment.
    var last_text_length = ak.utf16_view_length_in_code_units(&last_text)
    while last_text_length != 0:
        last_text_length -= 1
        let last_character = ak.utf16_view_code_unit_at(&last_text, last_text_length)
        if not ak.is_ascii_space((u32)last_character):
            break

        var font = layout_node_first_available_font(layout_line_box_fragment_layout_node(last_fragment))
        if let run = layout_line_box_fragment_glyph_run(last_fragment):
            font = web_fonts.glyph_run_font(run)
        let letter_spacing = css_computed_values_letter_spacing(&layout_node_computed_values(layout_line_box_fragment_layout_node(last_fragment)).computed_values)
        let last_character_width = gfx.CssPixels.from_f32(web_fonts.font_glyph_width(font, (u32)last_character)).add(letter_spacing)
        whitespace_width = whitespace_width.add(last_character_width)
        trailing_whitespace_width = trailing_whitespace_width.add(last_character_width)
        if should_remove == LayoutLineBoxRemoveTrailingWhitespace.yes:
            last_fragment.m_length_in_code_units -= 1
            layout_line_box_fragment_set_inline_length(last_fragment, layout_line_box_fragment_inline_length(last_fragment).sub(last_character_width))
            this.m_inline_length = this.m_inline_length.sub(last_character_width)

    # Track trimmed whitespace for selection purposes, but don't overwrite a value
    # that was already set during line breaking (for wrapped lines).
    if should_remove == LayoutLineBoxRemoveTrailingWhitespace.yes and (trailing_whitespace_width.gt(layout_zero_px()) or not layout_line_box_fragment_has_trailing_whitespace(last_fragment)):
        layout_line_box_fragment_set_has_trailing_whitespace(last_fragment, trailing_whitespace_width.gt(layout_zero_px()))

    return whitespace_width

## Ported from Libraries/LibWeb/Layout/LineBox.cpp:134 Web::Layout::LineBox::get_trailing_whitespace_width.
pub func layout_line_box_get_trailing_whitespace_width(this: const LayoutLineBox*) -> gfx.CssPixels:
    return layout_line_box_calculate_or_trim_trailing_whitespace((LayoutLineBox*)this, LayoutLineBoxRemoveTrailingWhitespace.no)

## Ported from Libraries/LibWeb/Layout/LineBox.cpp:139 Web::Layout::LineBox::trim_trailing_whitespace.
pub func layout_line_box_trim_trailing_whitespace(this: LayoutLineBox*):
    _ = layout_line_box_calculate_or_trim_trailing_whitespace(this, LayoutLineBoxRemoveTrailingWhitespace.yes)

## Ported from Libraries/LibWeb/Layout/LineBox.cpp:144 Web::Layout::LineBox::is_empty_or_ends_in_whitespace.
pub func layout_line_box_is_empty_or_ends_in_whitespace(this: const LayoutLineBox*) -> bool:
    if ak.vector_is_empty(&this.m_fragments):
        return true

    return layout_line_box_fragment_ends_in_whitespace(ak.vector_last_const(&this.m_fragments))