dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / grapheme_edge_tracker.lucb
#==============================================================================================
#
# grapheme_edge_tracker - moving a text cursor to the visually closest position on the next
# or previous line
#
# DESCRIPTION:
# Port of Libraries/LibWeb/GraphemeEdgeTracker.cpp (lines 1-119) with the inline
# GraphemeEdgeTracker class of GraphemeEdgeTracker.h (lines 39-79).
#
# When we want to move the cursor from some position within a line to a visually-equivalent
# position in an adjacent line, we measure the text between the start of the line and the
# starting index; on the next line, we want to find the position whose corresponding width
# is as close to the starting width as possible, considering the text affinity (which side
# of a grapheme the cursor is visually closest to). The header's full explanation uses a
# <textarea> holding emoji sequences as its example.
#
# Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: GraphemeEdgeTracker =====================================================================
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.h:41 Web::GraphemeEdgeTracker::GraphemeEdgeTracker.
pub func grapheme_edge_tracker_construct(this: GraphemeEdgeTracker*, target_width: f32):
this.m_target_width = target_width
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.h:46 Web::GraphemeEdgeTracker::update.
pub func grapheme_edge_tracker_update(this: GraphemeEdgeTracker*, grapheme_length_in_code_units: usize, grapheme_width: f32) -> ak.IterationDecision:
if grapheme_width == 0.0:
return ak.IterationDecision.continue_
this.m_right_edge += grapheme_length_in_code_units
this.m_width_to_right_edge += grapheme_width
if this.m_width_to_right_edge >= this.m_target_width:
return ak.IterationDecision.break_
this.m_left_edge = this.m_right_edge
this.m_width_to_left_edge = this.m_width_to_right_edge
return ak.IterationDecision.continue_
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.h:63 Web::GraphemeEdgeTracker::resolve.
pub func grapheme_edge_tracker_resolve(this: const GraphemeEdgeTracker*) -> usize:
if (this.m_target_width - this.m_width_to_left_edge) < (this.m_width_to_right_edge - this.m_target_width):
return this.m_left_edge
return this.m_right_edge
# mark: Lines ===================================================================================
# FIXME: Using newline characters to determine line breaks is insufficient. If a line is wrapped due space constraints,
# we want to consider each segment of the wrapped line as its own line in the algorithms below.
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.cpp:18 Web::find_line_start.
pub func find_line_start(view: ak.Utf16View, offset: usize) -> usize:
var position = offset
while position != 0 and ak.utf16_view_code_unit_at(&view, position - 1) != (u16)'\n':
position -= 1
return position
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.cpp:25 Web::find_line_end.
pub func find_line_end(view: ak.Utf16View, offset: usize) -> usize:
let length = ak.utf16_view_length_in_code_units(&view)
var position = offset
while position < length and ak.utf16_view_code_unit_at(&view, position) != (u16)'\n':
position += 1
return position
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.cpp:33 Web::measure_text_width.
func measure_text_width(text_node: LayoutTextNode*, text: ak.Utf16View) -> f32:
if ak.utf16_view_is_empty(&text):
return 0.0
let grapheme_segmenter = web_unicode.segmenter_clone(layout_text_node_grapheme_segmenter(text_node))
web_unicode.segmenter_set_segmented_text_utf16_view(grapheme_segmenter, text)
let line_segmenter = web_unicode.segmenter_clone(layout_text_node_line_segmenter(text_node))
web_unicode.segmenter_set_segmented_text_utf16_view(line_segmenter, text)
var iterator: LayoutTextNodeChunkIterator = ---
layout_text_node_chunk_iterator_construct_utf16_view(&iterator, text_node, text, grapheme_segmenter, line_segmenter, css_data.WordBreak.normal, false, false)
let letter_spacing = css_computed_values_letter_spacing(&layout_node_computed_values(text_node.layout_node()).computed_values).to_float()
var width: f32 = 0.0
while let chunk = layout_text_node_chunk_iterator_next(&iterator):
width += web_fonts.measure_text_width(chunk.view, chunk.font, letter_spacing)
return width
## The lambda translate_position_across_lines passes to Segmenter::for_each_boundary.
struct TranslatePositionAcrossLinesBoundary:
var text_node: LayoutTextNode*
var target_line: ak.Utf16View
var tracker: GraphemeEdgeTracker*
var previous_index: usize
static func call(context: void*?, boundary: u64) -> ak.IterationDecision:
let captures = (TranslatePositionAcrossLinesBoundary*)(context else trap("for_each_boundary: no captures"))
let index = (usize)boundary
let current_width = measure_text_width(captures.text_node, ak.utf16_view_substring_view_usize(&captures.target_line, captures.previous_index, index - captures.previous_index))
if grapheme_edge_tracker_update(captures.tracker, index - captures.previous_index, current_width) == ak.IterationDecision.break_:
return ak.IterationDecision.break_
captures.previous_index = index
return ak.IterationDecision.continue_
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.cpp:54 Web::translate_position_across_lines.
func translate_position_across_lines(text_node: LayoutTextNode*, source_line: ak.Utf16View, target_line: ak.Utf16View) -> usize:
var tracker: GraphemeEdgeTracker
grapheme_edge_tracker_construct(&tracker, measure_text_width(text_node, source_line))
var captures = TranslatePositionAcrossLinesBoundary(text_node = text_node, target_line = target_line, tracker = &tracker, previous_index = 0)
let callback = ak.Function1[u64, ak.IterationDecision](call = TranslatePositionAcrossLinesBoundary.call, context = &captures)
web_unicode.segmenter_for_each_boundary_utf16_view(web_unicode.segmenter_clone(layout_text_node_grapheme_segmenter(text_node)), target_line, callback)
return grapheme_edge_tracker_resolve(&tracker)
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.cpp:72 Web::compute_cursor_position_on_next_line.
pub func compute_cursor_position_on_next_line(dom_node: DomText*, current_offset: usize) -> usize?:
# NB: Called during text editing, layout may not be current.
let unsafe_layout_node = dom_node_unsafe_layout_node(dom_node.dom_node()) else return none
let layout_node = as_if_layout_text_node(unsafe_layout_node) else return none
let data = dom_character_data_data(dom_node.dom_character_data())
let text = ak.utf16_string_base_utf16_view(&data.utf16_string_base)
var new_offset = ak.utf16_view_length_in_code_units(&text)
let current_line_end = find_line_end(text, current_offset)
if current_line_end < ak.utf16_view_length_in_code_units(&text):
let current_line_start = find_line_start(text, current_offset)
let current_line_text = ak.utf16_view_substring_view_usize(&text, current_line_start, current_offset - current_line_start)
let next_line_start = current_line_end + 1
let next_line_length = find_line_end(text, next_line_start) - next_line_start
let next_line_text = ak.utf16_view_substring_view_usize(&text, next_line_start, next_line_length)
new_offset = next_line_start + translate_position_across_lines(layout_node, current_line_text, next_line_text)
return new_offset
## Ported from Libraries/LibWeb/GraphemeEdgeTracker.cpp:96 Web::compute_cursor_position_on_previous_line.
pub func compute_cursor_position_on_previous_line(dom_node: DomText*, current_offset: usize) -> usize?:
# NB: Called during text editing, layout may not be current.
let unsafe_layout_node = dom_node_unsafe_layout_node(dom_node.dom_node()) else return none
let layout_node = as_if_layout_text_node(unsafe_layout_node) else return none
let data = dom_character_data_data(dom_node.dom_character_data())
let text = ak.utf16_string_base_utf16_view(&data.utf16_string_base)
var new_offset: usize = 0
let current_line_start = find_line_start(text, current_offset)
if current_line_start != 0:
let current_line_text = ak.utf16_view_substring_view_usize(&text, current_line_start, current_offset - current_line_start)
let previous_line_start = find_line_start(text, current_line_start - 1)
let previous_line_length = current_line_start - previous_line_start - 1
let previous_line_text = ak.utf16_view_substring_view_usize(&text, previous_line_start, previous_line_length)
new_offset = previous_line_start + translate_position_across_lines(layout_node, current_line_text, previous_line_text)
return new_offset