dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / layout / line_box_fragment.lucb
#==============================================================================================
#
# line_box_fragment - Layout::LineBoxFragment: a piece of one layout node on a line box
#
# DESCRIPTION:
# Port of LibWeb/Layout/LineBoxFragment.cpp (lines 1-180) and LineBoxFragment.h: a
# fragment's offset and size in the inline and block axes (mapped to physical x/y by the
# writing mode), its baseline, its text, and the glyph runs of the text chunks a line
# box merges into it, placed in visual order for left-to-right and right-to-left
# fragments.
#
# Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Construction ============================================================================
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:15 Web::Layout::LineBoxFragment::LineBoxFragment.
pub func layout_line_box_fragment_construct(this: LayoutLineBoxFragment*, layout_node: LayoutNode*, start: usize, length_in_code_units: usize, inline_offset: gfx.CssPixels, block_offset: gfx.CssPixels, inline_length: gfx.CssPixels, block_length: gfx.CssPixels, border_box_top: gfx.CssPixels, direction: css_data.Direction, writing_mode: css_data.WritingMode, glyph_run: web_fonts.GlyphRun*?):
this.m_layout_node = layout_node
this.m_start = start
this.m_length_in_code_units = length_in_code_units
this.m_inline_offset = inline_offset
this.m_block_offset = block_offset
this.m_inline_length = inline_length
this.m_block_length = block_length
this.m_border_box_top = border_box_top
this.m_baseline = layout_zero_px()
this.m_direction = direction
this.m_writing_mode = writing_mode
this.m_glyph_run = glyph_run
this.m_insert_position = 0.0
this.m_current_insert_direction = css_data.Direction.ltr
this.m_has_trailing_whitespace = false
this.m_is_fully_truncated = false
if let run = this.m_glyph_run:
this.m_current_insert_direction = layout_line_box_fragment_resolve_glyph_run_direction(this, web_fonts.glyph_run_text_type(run))
if this.m_direction == css_data.Direction.rtl:
this.m_insert_position = this.m_inline_length.to_float()
## A LineBoxFragment by value (`LineBoxFragment { ... }`).
pub func layout_line_box_fragment_new(layout_node: LayoutNode*, start: usize, length_in_code_units: usize, inline_offset: gfx.CssPixels, block_offset: gfx.CssPixels, inline_length: gfx.CssPixels, block_length: gfx.CssPixels, border_box_top: gfx.CssPixels, direction: css_data.Direction, writing_mode: css_data.WritingMode, glyph_run: web_fonts.GlyphRun*?) -> LayoutLineBoxFragment:
var fragment: LayoutLineBoxFragment = ---
layout_line_box_fragment_construct(&fragment, layout_node, start, length_in_code_units, inline_offset, block_offset, inline_length, block_length, border_box_top, direction, writing_mode, glyph_run)
return fragment
# mark: Accessors (LineBoxFragment.h) ===========================================================
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:23 Web::Layout::LineBoxFragment::layout_node.
pub func layout_line_box_fragment_layout_node(this: const LayoutLineBoxFragment*) -> LayoutNode*:
return this.m_layout_node
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:24 Web::Layout::LineBoxFragment::start.
pub func layout_line_box_fragment_start(this: const LayoutLineBoxFragment*) -> usize:
return this.m_start
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:25 Web::Layout::LineBoxFragment::length_in_code_units.
pub func layout_line_box_fragment_length_in_code_units(this: const LayoutLineBoxFragment*) -> usize:
return this.m_length_in_code_units
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:28 Web::Layout::LineBoxFragment::inline_offset.
pub func layout_line_box_fragment_inline_offset(this: const LayoutLineBoxFragment*) -> gfx.CssPixels:
return this.m_inline_offset
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:29 Web::Layout::LineBoxFragment::block_offset.
pub func layout_line_box_fragment_block_offset(this: const LayoutLineBoxFragment*) -> gfx.CssPixels:
return this.m_block_offset
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:30 Web::Layout::LineBoxFragment::set_inline_offset.
pub func layout_line_box_fragment_set_inline_offset(this: LayoutLineBoxFragment*, inline_offset: gfx.CssPixels):
this.m_inline_offset = inline_offset
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:31 Web::Layout::LineBoxFragment::set_block_offset.
pub func layout_line_box_fragment_set_block_offset(this: LayoutLineBoxFragment*, block_offset: gfx.CssPixels):
this.m_block_offset = block_offset
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:34 Web::Layout::LineBoxFragment::set_baseline.
# The baseline of a fragment is the number of pixels from the top to the text baseline.
pub func layout_line_box_fragment_set_baseline(this: LayoutLineBoxFragment*, y: gfx.CssPixels):
this.m_baseline = y
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:35 Web::Layout::LineBoxFragment::baseline.
pub func layout_line_box_fragment_baseline(this: const LayoutLineBoxFragment*) -> gfx.CssPixels:
return this.m_baseline
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:38 Web::Layout::LineBoxFragment::width.
pub func layout_line_box_fragment_width(this: const LayoutLineBoxFragment*) -> gfx.CssPixels:
return layout_line_box_fragment_size(this).m_width
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:39 Web::Layout::LineBoxFragment::height.
pub func layout_line_box_fragment_height(this: const LayoutLineBoxFragment*) -> gfx.CssPixels:
return layout_line_box_fragment_size(this).m_height
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:40 Web::Layout::LineBoxFragment::inline_length.
pub func layout_line_box_fragment_inline_length(this: const LayoutLineBoxFragment*) -> gfx.CssPixels:
return this.m_inline_length
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:41 Web::Layout::LineBoxFragment::block_length.
pub func layout_line_box_fragment_block_length(this: const LayoutLineBoxFragment*) -> gfx.CssPixels:
return this.m_block_length
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:42 Web::Layout::LineBoxFragment::set_inline_length.
pub func layout_line_box_fragment_set_inline_length(this: LayoutLineBoxFragment*, inline_length: gfx.CssPixels):
this.m_inline_length = inline_length
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:43 Web::Layout::LineBoxFragment::set_block_length.
pub func layout_line_box_fragment_set_block_length(this: LayoutLineBoxFragment*, block_length: gfx.CssPixels):
this.m_block_length = block_length
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:45 Web::Layout::LineBoxFragment::border_box_top.
pub func layout_line_box_fragment_border_box_top(this: const LayoutLineBoxFragment*) -> gfx.CssPixels:
return this.m_border_box_top
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:53 Web::Layout::LineBoxFragment::glyph_run.
pub func layout_line_box_fragment_glyph_run(this: const LayoutLineBoxFragment*) -> web_fonts.GlyphRun*?:
return this.m_glyph_run
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:54 Web::Layout::LineBoxFragment::writing_mode.
pub func layout_line_box_fragment_writing_mode(this: const LayoutLineBoxFragment*) -> css_data.WritingMode:
return this.m_writing_mode
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:57 Web::Layout::LineBoxFragment::has_trailing_whitespace.
pub func layout_line_box_fragment_has_trailing_whitespace(this: const LayoutLineBoxFragment*) -> bool:
return this.m_has_trailing_whitespace
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:58 Web::Layout::LineBoxFragment::set_has_trailing_whitespace.
pub func layout_line_box_fragment_set_has_trailing_whitespace(this: LayoutLineBoxFragment*, value: bool):
this.m_has_trailing_whitespace = value
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:60 Web::Layout::LineBoxFragment::is_fully_truncated.
pub func layout_line_box_fragment_is_fully_truncated(this: const LayoutLineBoxFragment*) -> bool:
return this.m_is_fully_truncated
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.h:61 Web::Layout::LineBoxFragment::set_fully_truncated.
pub func layout_line_box_fragment_set_fully_truncated(this: LayoutLineBoxFragment*, value: bool):
this.m_is_fully_truncated = value
# mark: Geometry and text =======================================================================
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:37 Web::Layout::LineBoxFragment::offset.
pub func layout_line_box_fragment_offset(this: const LayoutLineBoxFragment*) -> gfx.CssPixelPoint:
if this.m_writing_mode != css_data.WritingMode.horizontal_tb:
return gfx.CssPixelPoint(m_x = this.m_block_offset, m_y = this.m_inline_offset)
return gfx.CssPixelPoint(m_x = this.m_inline_offset, m_y = this.m_block_offset)
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:44 Web::Layout::LineBoxFragment::size.
pub func layout_line_box_fragment_size(this: const LayoutLineBoxFragment*) -> gfx.CssPixelSize:
if this.m_writing_mode != css_data.WritingMode.horizontal_tb:
return gfx.CssPixelSize(m_width = this.m_block_length, m_height = this.m_inline_length)
return gfx.CssPixelSize(m_width = this.m_inline_length, m_height = this.m_block_length)
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:51 Web::Layout::LineBoxFragment::ends_in_whitespace.
pub func layout_line_box_fragment_ends_in_whitespace(this: const LayoutLineBoxFragment*) -> bool:
if this.m_length_in_code_units == 0:
return false
let text = layout_line_box_fragment_text(this)
return ak.is_ascii_space((u32)ak.utf16_view_code_unit_at(&text, this.m_length_in_code_units - 1))
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:58 Web::Layout::LineBoxFragment::is_justifiable_whitespace.
pub func layout_line_box_fragment_is_justifiable_whitespace(this: const LayoutLineBoxFragment*) -> bool:
let text = layout_line_box_fragment_text(this)
return ak.utf16_view_eq_string_view(&text, ak.sv(" "))
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:63 Web::Layout::LineBoxFragment::text.
pub func layout_line_box_fragment_text(this: const LayoutLineBoxFragment*) -> ak.Utf16View:
if let text_node = as_if_layout_text_node(layout_line_box_fragment_layout_node(this)):
let text_for_rendering = layout_text_node_text_for_rendering(text_node)
let view = ak.utf16_string_base_utf16_view(&text_for_rendering.utf16_string_base)
return ak.utf16_view_substring_view_usize(&view, this.m_start, this.m_length_in_code_units)
return ak.utf16_view_new()
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:70 Web::Layout::LineBoxFragment::is_atomic_inline.
pub func layout_line_box_fragment_is_atomic_inline(this: const LayoutLineBoxFragment*) -> bool:
return layout_node_is_atomic_inline(layout_line_box_fragment_layout_node(this))
# mark: Glyph runs ==============================================================================
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:75 Web::Layout::LineBoxFragment::resolve_glyph_run_direction.
pub func layout_line_box_fragment_resolve_glyph_run_direction(this: const LayoutLineBoxFragment*, text_type: web_fonts.GlyphRunTextType) -> css_data.Direction:
match text_type:
.common, .context_dependent, .end_padding:
return this.m_direction
.ltr:
return css_data.Direction.ltr
.rtl:
return css_data.Direction.rtl
_:
trap("VERIFY_NOT_REACHED")
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:91 Web::Layout::LineBoxFragment::append_glyph_run.
pub func layout_line_box_fragment_append_glyph_run(this: LayoutLineBoxFragment*, glyph_run: web_fonts.GlyphRun*?, run_width: gfx.CssPixels):
match this.m_direction:
.ltr:
layout_line_box_fragment_append_glyph_run_ltr(this, glyph_run, run_width)
.rtl:
layout_line_box_fragment_append_glyph_run_rtl(this, glyph_run, run_width)
_:
_ = 0
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:103 Web::Layout::LineBoxFragment::append_glyph_run_ltr.
pub func layout_line_box_fragment_append_glyph_run_ltr(this: LayoutLineBoxFragment*, glyph_run: web_fonts.GlyphRun*?, run_width: gfx.CssPixels):
let run = glyph_run else trap("null glyph run")
let own_run = this.m_glyph_run else trap("null glyph run")
let run_direction = layout_line_box_fragment_resolve_glyph_run_direction(this, web_fonts.glyph_run_text_type(run))
let inline_offset = this.m_inline_length.to_float()
if this.m_current_insert_direction != run_direction:
if run_direction == css_data.Direction.rtl:
this.m_insert_position = inline_offset
this.m_current_insert_direction = run_direction
let glyphs = web_fonts.glyph_run_glyphs(own_run)
let appended = web_fonts.glyph_run_glyphs(run)
ak.vector_ensure_capacity(glyphs, ak.vector_size(glyphs) + ak.vector_size(appended))
let existing_glyphs = ak.vector_span(glyphs)
let appended_glyphs = ak.vector_span(appended)
match run_direction:
.ltr:
for glyph in &appended_glyphs:
gfx.float_point_translate_by_f32_f32(&glyph.position, inline_offset, 0.0)
ak.vector_unchecked_append_u(glyphs, *glyph)
.rtl:
for glyph in &existing_glyphs:
if glyph.position.m_x >= this.m_insert_position:
gfx.float_point_translate_by_f32_f32(&glyph.position, run_width.to_float(), 0.0)
for glyph in &appended_glyphs:
gfx.float_point_translate_by_f32_f32(&glyph.position, this.m_insert_position, 0.0)
ak.vector_unchecked_append_u(glyphs, *glyph)
_:
_ = 0
this.m_inline_length = this.m_inline_length.add(run_width)
## Ported from Libraries/LibWeb/Layout/LineBoxFragment.cpp:139 Web::Layout::LineBoxFragment::append_glyph_run_rtl.
pub func layout_line_box_fragment_append_glyph_run_rtl(this: LayoutLineBoxFragment*, glyph_run: web_fonts.GlyphRun*?, run_width: gfx.CssPixels):
let run = glyph_run else trap("null glyph run")
let own_run = this.m_glyph_run else trap("null glyph run")
let run_direction = layout_line_box_fragment_resolve_glyph_run_direction(this, web_fonts.glyph_run_text_type(run))
let run_offset = run_width.to_float()
if this.m_current_insert_direction != run_direction:
if run_direction == css_data.Direction.ltr:
this.m_insert_position = 0.0
this.m_current_insert_direction = run_direction
let glyphs = web_fonts.glyph_run_glyphs(own_run)
let appended = web_fonts.glyph_run_glyphs(run)
ak.vector_ensure_capacity(glyphs, ak.vector_size(glyphs) + ak.vector_size(appended))
let existing_glyphs = ak.vector_span(glyphs)
let appended_glyphs = ak.vector_span(appended)
match run_direction:
.ltr:
for glyph in &existing_glyphs:
if glyph.position.m_x >= this.m_insert_position:
gfx.float_point_translate_by_f32_f32(&glyph.position, run_offset, 0.0)
for glyph in &appended_glyphs:
gfx.float_point_translate_by_f32_f32(&glyph.position, this.m_insert_position, 0.0)
ak.vector_unchecked_append_u(glyphs, *glyph)
.rtl:
if web_fonts.glyph_run_text_type(run) != web_fonts.GlyphRunTextType.end_padding:
for glyph in &existing_glyphs:
gfx.float_point_translate_by_f32_f32(&glyph.position, run_offset, 0.0)
for glyph in appended_glyphs:
ak.vector_unchecked_append_u(glyphs, glyph)
_:
_ = 0
this.m_inline_length = this.m_inline_length.add(run_width)
this.m_insert_position += run_width.to_float()