Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / layout / text_node_1.lucb

#==============================================================================================
#
#   text_node_1 - Layout::TextNode: the node of a DOM text node and its text for rendering
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/TextNode.cpp (lines 1-424) and TextNode.h: the constructor, the
#       MathML italic mapping and the text-transform values, the text for rendering (with
#       segment breaks and tabs made spaces where white-space-collapse collapses them) and the
#       grapheme and line segmenters of that text.
#
#       Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
#       Copyright (c) 2026, Tim Ledbetter <tim.ledbetter@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

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

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:24 Web::Layout::TextNode::TextNode.
pub func layout_text_node_construct(this: LayoutTextNode*, document: DomDocument*, text: DomText*):
    layout_node_construct(this.layout_node(), document, text.dom_node())

## Ported from Libraries/LibWeb/Layout/TextNode.h:28 Web::Layout::TextNode::dom_node.
pub func layout_text_node_dom_node(this: LayoutTextNode*) -> DomText*:
    let dom_node = layout_node_dom_node(this.layout_node()) else trap("VERIFY: a text node has a DOM node")
    return (DomText*)dom_node

# mark: text-transform ==========================================================================

## `map_code_point_to_italic`: the lambda of apply_math_auto_text_transform.
# https://w3c.github.io/mathml-core/#italic-mappings
func layout_map_code_point_to_italic(code_point: u32) -> u32:
    return match code_point:
        0x0041 => 0x1D434
        0x0042 => 0x1D435
        0x0043 => 0x1D436
        0x0044 => 0x1D437
        0x0045 => 0x1D438
        0x0046 => 0x1D439
        0x0047 => 0x1D43A
        0x0048 => 0x1D43B
        0x0049 => 0x1D43C
        0x004a => 0x1D43D
        0x004b => 0x1D43E
        0x004c => 0x1D43F
        0x004d => 0x1D440
        0x004e => 0x1D441
        0x004f => 0x1D442
        0x0050 => 0x1D443
        0x0051 => 0x1D444
        0x0052 => 0x1D445
        0x0053 => 0x1D446
        0x0054 => 0x1D447
        0x0055 => 0x1D448
        0x0056 => 0x1D449
        0x0057 => 0x1D44A
        0x0058 => 0x1D44B
        0x0059 => 0x1D44C
        0x005a => 0x1D44D
        0x0061 => 0x1D44E
        0x0062 => 0x1D44F
        0x0063 => 0x1D450
        0x0064 => 0x1D451
        0x0065 => 0x1D452
        0x0066 => 0x1D453
        0x0067 => 0x1D454
        0x0068 => 0x0210E
        0x0069 => 0x1D456
        0x006a => 0x1D457
        0x006b => 0x1D458
        0x006c => 0x1D459
        0x006d => 0x1D45A
        0x006e => 0x1D45B
        0x006f => 0x1D45C
        0x0070 => 0x1D45D
        0x0071 => 0x1D45E
        0x0072 => 0x1D45F
        0x0073 => 0x1D460
        0x0074 => 0x1D461
        0x0075 => 0x1D462
        0x0076 => 0x1D463
        0x0077 => 0x1D464
        0x0078 => 0x1D465
        0x0079 => 0x1D466
        0x007a => 0x1D467
        0x0131 => 0x1D6A4
        0x0237 => 0x1D6A5
        0x0391 => 0x1D6E2
        0x0392 => 0x1D6E3
        0x0393 => 0x1D6E4
        0x0394 => 0x1D6E5
        0x0395 => 0x1D6E6
        0x0396 => 0x1D6E7
        0x0397 => 0x1D6E8
        0x0398 => 0x1D6E9
        0x0399 => 0x1D6EA
        0x039a => 0x1D6EB
        0x039b => 0x1D6EC
        0x039c => 0x1D6ED
        0x039d => 0x1D6EE
        0x039e => 0x1D6EF
        0x039f => 0x1D6F0
        0x03a0 => 0x1D6F1
        0x03a1 => 0x1D6F2
        0x03f4 => 0x1D6F3
        0x03a3 => 0x1D6F4
        0x03a4 => 0x1D6F5
        0x03a5 => 0x1D6F6
        0x03a6 => 0x1D6F7
        0x03a7 => 0x1D6F8
        0x03a8 => 0x1D6F9
        0x03a9 => 0x1D6FA
        0x2207 => 0x1D6FB
        0x03b1 => 0x1D6FC
        0x03b2 => 0x1D6FD
        0x03b3 => 0x1D6FE
        0x03b4 => 0x1D6FF
        0x03b5 => 0x1D700
        0x03b6 => 0x1D701
        0x03b7 => 0x1D702
        0x03b8 => 0x1D703
        0x03b9 => 0x1D704
        0x03ba => 0x1D705
        0x03bb => 0x1D706
        0x03bc => 0x1D707
        0x03bd => 0x1D708
        0x03be => 0x1D709
        0x03bf => 0x1D70A
        0x03c0 => 0x1D70B
        0x03c1 => 0x1D70C
        0x03c2 => 0x1D70D
        0x03c3 => 0x1D70E
        0x03c4 => 0x1D70F
        0x03c5 => 0x1D710
        0x03c6 => 0x1D711
        0x03c7 => 0x1D712
        0x03c8 => 0x1D713
        0x03c9 => 0x1D714
        0x2202 => 0x1D715
        0x03f5 => 0x1D716
        0x03d1 => 0x1D717
        0x03f0 => 0x1D718
        0x03d5 => 0x1D719
        0x03f1 => 0x1D71A
        0x03d6 => 0x1D71B
        _ => code_point

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:32 Web::Layout::apply_math_auto_text_transform.
# https://w3c.github.io/mathml-core/#new-text-transform-values
func layout_apply_math_auto_text_transform(string: ak.Utf16String) -> ak.Utf16String:
    var builder = ak.string_builder_new_mode_usize(ak.StringBuilderMode.utf16, ak.utf16_string_base_length_in_code_units(&string.utf16_string_base))

    var it = ak.utf16_string_base_begin(&string.utf16_string_base)
    while not ak.utf16_code_point_iterator_done(&it):
        ak.string_builder_append_code_point(&builder, layout_map_code_point_to_italic(ak.utf16_code_point_iterator_deref(&it)))
        _ = ak.utf16_code_point_iterator_inc(&it)

    return ak.string_builder_to_utf16_string(&builder)

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:274 Web::Layout::apply_text_transform.
func layout_apply_text_transform(string: ak.Utf16String, text_transform: css_data.TextTransform, locale: ak.StringView?) -> ak.Utf16String:
    match text_transform:
        .uppercase:
            return web_unicode.utf16_string_to_uppercase(&string, locale)
        .lowercase:
            return web_unicode.utf16_string_to_lowercase(&string, locale)
        .none_:
            return string
        .math_auto:
            return layout_apply_math_auto_text_transform(string)
        .capitalize:
            return web_unicode.utf16_string_to_titlecase(&string, locale, ak.TrailingCodePointTransformation.preserve_existing)
        .full_size_kana:
            ak.dbgln("FIXME: Implement text-transform full-size-kana")
            return string
        .full_width:
            return web_unicode.utf16_string_to_fullwidth(&string)
        _:
            trap("VERIFY_NOT_REACHED")

# mark: Text for rendering ======================================================================

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:297 Web::Layout::TextNode::invalidate_text_for_rendering.
pub func layout_text_node_invalidate_text_for_rendering(this: LayoutTextNode*):
    this.m_text_for_rendering = ak.utf16_string_new()
    this.m_has_text_for_rendering = false
    this.m_grapheme_segmenter = none
    this.m_line_segmenter = none

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:304 Web::Layout::TextNode::text_for_rendering.
pub func layout_text_node_text_for_rendering(this: LayoutTextNode*) -> const ak.Utf16String*:
    if not this.m_has_text_for_rendering:
        layout_text_node_compute_text_for_rendering(this)
    return &this.m_text_for_rendering

## `any_of(text, is_ascii_space)`: whether any code point of `text` is ASCII whitespace.
func layout_utf16_string_has_ascii_space(text: const ak.Utf16String*) -> bool:
    var it = ak.utf16_string_base_begin(&text.utf16_string_base)
    while not ak.utf16_code_point_iterator_done(&it):
        if ak.is_ascii_space(ak.utf16_code_point_iterator_deref(&it)):
            return true
        _ = ak.utf16_code_point_iterator_inc(&it)
    return false

## `m_text_for_rendering = text`: the optional gets a value.
func layout_text_node_set_text_for_rendering(this: LayoutTextNode*, text: ak.Utf16String):
    this.m_text_for_rendering = text
    this.m_has_text_for_rendering = true

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:311 Web::Layout::TextNode::compute_text_for_rendering.
func layout_text_node_compute_text_for_rendering(this: LayoutTextNode*):
    let dom_node = layout_text_node_dom_node(this)
    if dom_text_is_password_input(dom_node):
        let data = dom_character_data_data(dom_node.dom_character_data())
        layout_text_node_set_text_for_rendering(this, ak.utf16_string_repeated(0x25CF, ak.utf16_string_base_length_in_code_points(&data.utf16_string_base)))
        return

    # Apply text-transform
    # FIXME: This can generate more code points than there were before; we need to find a better way to map the
    #        resulting paintable fragments' offsets into the original text node data.
    #        See: https://github.com/LadybirdBrowser/ladybird/issues/6177
    var maybe_lang: ak.String? = none
    if let parent_element = dom_node_parent_element(dom_node.dom_node()):
        maybe_lang = dom_element_lang(parent_element)
    var lang: ak.StringView? = none
    if let value = maybe_lang:
        lang = ak.string_bytes_as_string_view(&value)
    let computed_values = &layout_node_computed_values(this.layout_node()).computed_values
    var text = layout_apply_text_transform(dom_character_data_data(dom_node.dom_character_data()), css_computed_values_text_transform(computed_values), lang)

    # The logic below deals with converting whitespace characters. If we don't have them, return early.
    if ak.utf16_string_base_is_empty(&text.utf16_string_base) or not layout_utf16_string_has_ascii_space(&text):
        layout_text_node_set_text_for_rendering(this, text)
        return

    # https://drafts.csswg.org/css-text-4/#white-space-phase-1
    var convert_newlines = false
    var convert_tabs = false

    # If white-space-collapse is set to collapse or preserve-breaks, white space characters are considered collapsible
    # and are processed by performing the following steps:
    let white_space_collapse = css_computed_values_white_space_collapse(computed_values)
    if white_space_collapse == css_data.WhiteSpaceCollapse.collapse or white_space_collapse == css_data.WhiteSpaceCollapse.preserve_breaks:
        # 1. FIXME: Any sequence of collapsible spaces and tabs immediately preceding or following a segment break is removed.

        # 2. Collapsible segment breaks are transformed for rendering according to the segment break transformation
        #    rules.
        # https://drafts.csswg.org/css-text-4/#line-break-transform
        # FIXME: When white-space-collapse is not collapse, segment breaks are not collapsible. For values other than
        # collapse or preserve-spaces (which transforms them into spaces), segment breaks are instead transformed
        # into a preserved line feed (U+000A).

        # When white-space-collapse is collapse, segment breaks are collapsible, and are collapsed as follows:
        if white_space_collapse == css_data.WhiteSpaceCollapse.collapse:
            # 1. FIXME: First, any collapsible segment break immediately following another collapsible segment break is
            #    removed.

            # 2. FIXME: Then any remaining segment break is either transformed into a space (U+0020) or removed depending
            #    on the context before and after the break. The rules for this operation are UA-defined in this
            #    level.
            convert_newlines = true

        # 3. Every collapsible tab is converted to a collapsible space (U+0020).
        convert_tabs = true

        # 4. Any collapsible space immediately following another collapsible space—even one outside the boundary of the
        #    inline containing that space, provided both spaces are within the same inline formatting context—is
        #    collapsed to have zero advance width. (It is invisible, but retains its soft wrap opportunity, if any.)
        # AD-HOC: This is handled by TextNode::ChunkIterator by removing the space.

    # If white-space-collapse is set to preserve-spaces, each tab and segment break is converted to a space.
    if white_space_collapse == css_data.WhiteSpaceCollapse.preserve_spaces:
        convert_tabs = true
        convert_newlines = true

    # AD-HOC: Prevent allocating a StringBuilder for a single space/newline/tab.
    if ak.utf16_string_base_eq_string_view(&text.utf16_string_base, ak.sv(" ")) or (convert_tabs and ak.utf16_string_base_eq_string_view(&text.utf16_string_base, ak.sv("\t"))) or (convert_newlines and ak.utf16_string_base_eq_string_view(&text.utf16_string_base, ak.sv("\n"))):
        layout_text_node_set_text_for_rendering(this, ak.utf16_string_from_str(" "))
        return

    # AD-HOC: It's important to not change the amount of code units in the resulting transformed text, so ChunkIterator
    #         can pass views to this string with associated code unit offsets that still match the original text.
    if convert_newlines or convert_tabs:
        var text_builder = ak.string_builder_new_mode_usize(ak.StringBuilderMode.utf16, ak.utf16_string_base_length_in_code_units(&text.utf16_string_base))
        var it = ak.utf16_string_base_begin(&text.utf16_string_base)
        while not ak.utf16_code_point_iterator_done(&it):
            var code_point = ak.utf16_code_point_iterator_deref(&it)
            if (convert_newlines and code_point == (u32)'\n') or (convert_tabs and code_point == (u32)'\t'):
                code_point = (u32)' '
            ak.string_builder_append_code_point(&text_builder, code_point)
            _ = ak.utf16_code_point_iterator_inc(&it)
        text = ak.string_builder_to_utf16_string(&text_builder)

    layout_text_node_set_text_for_rendering(this, text)

# mark: Segmenters ==============================================================================

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:399 Web::Layout::TextNode::grapheme_segmenter.
pub func layout_text_node_grapheme_segmenter(this: LayoutTextNode*) -> web_unicode.Segmenter*:
    if this.m_grapheme_segmenter == none:
        let text = layout_text_node_text_for_rendering(this)
        # Fast path: For ASCII text, every character is its own grapheme.
        # We can use a trivial segmenter that avoids all ICU overhead.
        if ak.utf16_string_base_is_ascii(&text.utf16_string_base):
            this.m_grapheme_segmenter = web_unicode.segmenter_create_for_ascii_grapheme(ak.utf16_string_base_length_in_code_units(&text.utf16_string_base))
        else:
            let segmenter = web_unicode.segmenter_clone(dom_document_grapheme_segmenter(layout_node_document(this.layout_node())))
            web_unicode.segmenter_set_segmented_text_utf16_view(segmenter, ak.utf16_string_base_utf16_view(&text.utf16_string_base))
            this.m_grapheme_segmenter = segmenter

    return this.m_grapheme_segmenter else trap("unreachable: the grapheme segmenter was just made")

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:416 Web::Layout::TextNode::line_segmenter.
pub func layout_text_node_line_segmenter(this: LayoutTextNode*) -> web_unicode.Segmenter*:
    if this.m_line_segmenter == none:
        let segmenter = web_unicode.segmenter_clone(dom_document_line_segmenter(layout_node_document(this.layout_node())))
        let text = layout_text_node_text_for_rendering(this)
        web_unicode.segmenter_set_segmented_text_utf16_view(segmenter, ak.utf16_string_base_utf16_view(&text.utf16_string_base))
        this.m_line_segmenter = segmenter

    return this.m_line_segmenter else trap("unreachable: the line segmenter was just made")

# mark: Paintables ==============================================================================

## Ported from Libraries/LibWeb/Layout/TextNode.cpp:802 Web::Layout::TextNode::create_paintable (override).
pub func layout_text_node_create_paintable_impl(base: LayoutNode*) -> PaintPaintable*?:
    return paint_text_paintable_create((LayoutTextNode*)base).paint_paintable()

## Ported from Libraries/LibWeb/Layout/TextNode.h:89 Web::Layout::TextNode::is_text_node (override).
pub func layout_text_node_is_text_node_impl(base: LayoutNode*) -> bool:
    _ = base
    return true