dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / painting / paintable_with_lines_1.lucb
#==============================================================================================
#
# paintable_with_lines_1 - Painting::PaintableWithLines: a box with line box fragments
#
# DESCRIPTION:
# Port of LibWeb/Painting/PaintableWithLines.cpp (lines 1-480) and PaintableWithLines.h:
# the paintable of a block container or of one line of an inline node, its fragments,
# hit testing of the fragments (with the text cursor mode's nearest fragment edges), the
# text decoration thickness and shadows of its text fragments, painting the foreground
# (selection backgrounds, text shadows, text and the cursor) and the render spans of a
# fragment split at the selection.
#
# Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
# Copyright (c) 2024-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
# Copyright (c) 2025-2026, Jelle Raaijmakers <jelle@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Creation and the fragments ===============================================================
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:34 Web::Painting::PaintableWithLines::create.
pub func paint_paintable_with_lines_create_layout_block_container(block_container: LayoutBlockContainer*) -> PaintPaintableWithLines*:
let heap = gc.cell_heap(block_container.cell())
let this = (PaintPaintableWithLines*)web_heap_allocate_cell(heap, &paint_paintable_with_lines_class)
gc.heap_defer_gc(heap)
paint_paintable_with_lines_construct_layout_block_container(this, block_container)
gc.heap_undefer_gc(heap)
return this
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:39 Web::Painting::PaintableWithLines::create.
pub func paint_paintable_with_lines_create_layout_inline_node(inline_node: LayoutInlineNode*, line_index: usize) -> PaintPaintableWithLines*:
let heap = gc.cell_heap(inline_node.cell())
let this = (PaintPaintableWithLines*)web_heap_allocate_cell(heap, &paint_paintable_with_lines_class)
gc.heap_defer_gc(heap)
paint_paintable_with_lines_construct_layout_inline_node(this, inline_node, line_index)
gc.heap_undefer_gc(heap)
return this
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:44 Web::Painting::PaintableWithLines::PaintableWithLines.
pub func paint_paintable_with_lines_construct_layout_block_container(this: PaintPaintableWithLines*, layout_box: LayoutBlockContainer*):
paint_paintable_box_construct_layout_box(this.paint_paintable_box(), layout_box.layout_box())
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:49 Web::Painting::PaintableWithLines::PaintableWithLines.
pub func paint_paintable_with_lines_construct_layout_inline_node(this: PaintPaintableWithLines*, inline_node: LayoutInlineNode*, line_index: usize):
paint_paintable_box_construct_layout_inline_node(this.paint_paintable_box(), inline_node)
this.m_line_index = line_index
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:59 Web::Painting::PaintableWithLines::reset_for_relayout (override).
pub func paint_paintable_with_lines_reset_for_relayout_impl(base: PaintPaintableBox*):
let this = (PaintPaintableWithLines*)base
paint_paintable_box_reset_for_relayout_impl(base)
ak.vector_clear(&this.m_fragments)
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.h:27 Web::Painting::PaintableWithLines::fragments.
pub func paint_paintable_with_lines_fragments_const(this: PaintPaintableWithLines*) -> ak.Vector[PaintPaintableFragment]:
return this.m_fragments
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.h:28 Web::Painting::PaintableWithLines::fragments.
pub func paint_paintable_with_lines_fragments(this: PaintPaintableWithLines*) -> ak.Vector[PaintPaintableFragment]*:
return &this.m_fragments
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.h:30 Web::Painting::PaintableWithLines::add_fragment.
pub func paint_paintable_with_lines_add_fragment(this: PaintPaintableWithLines*, fragment: LayoutLineBoxFragment):
ak.vector_append_t(&this.m_fragments, paint_paintable_fragment_new(fragment))
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.h:41 Web::Painting::PaintableWithLines::visit_edges (override).
pub func paint_paintable_with_lines_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (PaintPaintableWithLines*)base
paint_paintable_box_visit_edges_impl(base, visitor)
for fragment in &ak.vector_span(&this.m_fragments):
gc.cell_visitor_visit_t_2(visitor, paint_paintable_fragment_layout_node(fragment))
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.h:48 Web::Painting::PaintableWithLines::line_index.
pub func paint_paintable_with_lines_line_index(this: PaintPaintableWithLines*) -> usize:
return this.m_line_index
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.h:55 Web::Painting::PaintableWithLines::is_paintable_with_lines (override).
pub func paint_paintable_with_lines_is_paintable_with_lines_impl(base: PaintPaintable*) -> bool:
_ = base
return true
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:65 Web::Painting::PaintableWithLines::paint_text_fragment_debug_highlight.
pub func paint_paintable_with_lines_paint_text_fragment_debug_highlight(context: DisplayListRecordingContext*, fragment: const PaintPaintableFragment*):
let fragment_absolute_rect = paint_paintable_fragment_absolute_rect(fragment)
let fragment_absolute_device_rect = display_list_recording_context_enclosing_device_rect(context, fragment_absolute_rect)
let recorder = display_list_recording_context_display_list_recorder(context)
display_list.display_list_recorder_draw_rect(recorder, fragment_absolute_device_rect, gfx.color_new_named_color(gfx.ColorNamedColor.green))
let top_left = gfx.css_pixel_rect_top_left(&fragment_absolute_rect)
let top_right = gfx.css_pixel_rect_top_right(&fragment_absolute_rect)
let baseline_start = display_list_recording_context_rounded_device_point(context, gfx.css_pixel_point_translated_css_pixels_css_pixels(&top_left, gfx.CssPixels.from_int(0), paint_paintable_fragment_baseline(fragment)))
let baseline_end = display_list_recording_context_rounded_device_point(context, gfx.css_pixel_point_translated_css_pixels_css_pixels(&top_right, gfx.CssPixels.from_int(-1), paint_paintable_fragment_baseline(fragment)))
display_list.display_list_recorder_draw_line(recorder, baseline_start, baseline_end, gfx.color_new_named_color(gfx.ColorNamedColor.red))
# mark: Hit testing ================================================================================
## The `ensure_local_position` lambda of PaintableWithLines::hit_test.
struct PaintPaintableWithLinesLocalPosition:
var local_position: gfx.CssPixelPoint?
var acquired_local_position: bool
## The `ensure_local_position` lambda of PaintableWithLines::hit_test.
func paint_paintable_with_lines_ensure_local_position(this: PaintPaintableWithLines*, state: PaintPaintableWithLinesLocalPosition*, position: gfx.CssPixelPoint):
if ak.exchange(&state.acquired_local_position, true):
return
state.local_position = paint_paintable_box_transform_point_to_local(this.paint_paintable_box(), position)
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:76 Web::Painting::PaintableWithLines::hit_test (override).
pub func paint_paintable_with_lines_hit_test_impl(base: PaintPaintable*, position: gfx.CssPixelPoint, type_: PaintHitTestType, callback: ak.Function1[PaintHitTestResult, TraversalDecision]) -> TraversalDecision:
let this = (PaintPaintableWithLines*)base
let is_visible = css_computed_values_visibility(&paint_paintable_computed_values(base).computed_values) == css_data.Visibility.visible
var local_state = PaintPaintableWithLinesLocalPosition(local_position = none, acquired_local_position = false)
# TextCursor hit testing mode should be able to place cursor in contenteditable elements even if they are empty.
let layout_node = paint_paintable_layout_node(base)
var empty_editable = false
if ak.vector_is_empty(&this.m_fragments) and not tree_node_has_children(base) and type_ == PaintHitTestType.text_cursor:
if let dom_node = layout_node_dom_node(layout_node):
empty_editable = dom_node_is_editable_or_editing_host(dom_node) and is_visible and paint_paintable_visible_for_hit_testing(base)
if empty_editable:
paint_paintable_with_lines_ensure_local_position(this, &local_state, position)
if let local_position = local_state.local_position:
let border_box_rect = paint_paintable_box_absolute_border_box_rect(this.paint_paintable_box())
if gfx.css_pixel_rect_contains_css_pixel_point(&border_box_rect, local_position):
let hit_test_result = PaintHitTestResult(
paintable = base,
index_in_node = 0,
vertical_distance = gfx.CssPixels.from_int(0),
horizontal_distance = gfx.CssPixels.from_int(0),
)
if ak.function1_call(&callback, hit_test_result) == TraversalDecision.break_:
return TraversalDecision.break_
if not layout_node_children_are_inline(layout_node):
return paint_paintable_box_hit_test_impl(base, position, type_, callback)
# Only hit test chrome for visible elements.
if is_visible:
if paint_paintable_box_hit_test_chrome(this.paint_paintable_box(), position, callback) == TraversalDecision.break_:
return TraversalDecision.break_
if paint_paintable_box_hit_test_children(this.paint_paintable_box(), position, type_, callback) == TraversalDecision.break_:
return TraversalDecision.break_
# Hidden elements and elements with pointer-events: none shouldn't be hit.
if not is_visible or not paint_paintable_visible_for_hit_testing(base):
return TraversalDecision.continue_
paint_paintable_with_lines_ensure_local_position(this, &local_state, position)
let local_position = local_state.local_position else return TraversalDecision.continue_
# Fragments are descendants of this element, so use the descendants' visual context to account for this element's
# own scroll offset during fragment hit testing.
if let local_position_for_fragments = paint_paintable_box_transform_point_to_local_for_descendants(this.paint_paintable_box(), position):
if paint_paintable_with_lines_hit_test_fragments(this, position, local_position_for_fragments, type_, callback) == TraversalDecision.break_:
return TraversalDecision.break_
let border_box_rect = paint_paintable_box_absolute_border_box_rect(this.paint_paintable_box())
if paint_paintable_box_stacking_context(this.paint_paintable_box()) == none and (not layout_node_is_anonymous(layout_node) or paint_paintable_is_positioned(base)) and gfx.css_pixel_rect_contains_css_pixel_point(&border_box_rect, local_position):
if ak.function1_call(&callback, PaintHitTestResult(paintable = base)) == TraversalDecision.break_:
return TraversalDecision.break_
return TraversalDecision.continue_
## The `common_ancestor_parent` lambda of PaintableWithLines::hit_test_fragments.
func paint_paintable_with_lines_common_ancestor_parent(this: PaintPaintableWithLines*) -> DomNode*?:
let selection = dom_document_get_selection(paint_paintable_document(this.paint_paintable())) else return none
let range = selection_selection_range(selection) else return none
let common_ancestor = dom_range_common_ancestor_container(range)
if let parent = tree_node_parent(common_ancestor):
return parent
return common_ancestor
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:144 Web::Painting::PaintableWithLines::hit_test_fragments.
pub func paint_paintable_with_lines_hit_test_fragments(this: PaintPaintableWithLines*, position: gfx.CssPixelPoint, local_position: gfx.CssPixelPoint, type_: PaintHitTestType, callback: ak.Function1[PaintHitTestResult, TraversalDecision]) -> TraversalDecision:
for fragment in &ak.vector_span(&this.m_fragments):
let fragment_paintable = paint_paintable_fragment_paintable(fragment)
if paint_paintable_has_stacking_context(fragment_paintable) or not paint_paintable_is_visible(fragment_paintable) or not paint_paintable_visible_for_hit_testing(fragment_paintable):
continue
let fragment_absolute_rect = paint_paintable_fragment_absolute_rect(fragment)
if gfx.css_pixel_rect_contains_css_pixel_point(&fragment_absolute_rect, local_position):
if paint_paintable_hit_test(fragment_paintable, position, type_, callback) == TraversalDecision.break_:
return TraversalDecision.break_
let hit_test_result = PaintHitTestResult(paintable = fragment_paintable, index_in_node = paint_paintable_fragment_index_in_node_for_point(fragment, local_position), vertical_distance = gfx.CssPixels.from_int(0), horizontal_distance = gfx.CssPixels.from_int(0))
if ak.function1_call(&callback, hit_test_result) == TraversalDecision.break_:
return TraversalDecision.break_
elif type_ == PaintHitTestType.text_cursor:
let common_ancestor_parent = paint_paintable_with_lines_common_ancestor_parent(this)
# If we reached this point, the position is not within the fragment. However, the fragment start or end might be
# the place to place the cursor, so long as it does not have user-select: none.
if layout_node_user_select_used_value(paint_paintable_fragment_layout_node(fragment)) == css_data.UserSelect.none_:
continue
let fragment_dom_node = layout_node_dom_node(paint_paintable_fragment_layout_node(fragment))
if let ancestor = common_ancestor_parent:
if let fragment_node = fragment_dom_node:
if tree_node_is_ancestor_of(ancestor, fragment_node):
# To determine the best place, we first find the closest fragment horizontally to the cursor. If we could not
# find one, then find for the closest vertically above the cursor. If we knew the direction of selection, we
# would look above if selecting upward.
var hit_test_result = PaintHitTestResult(
paintable = fragment_paintable,
index_in_node = paint_paintable_fragment_start_offset(fragment),
)
if gfx.css_pixel_rect_bottom(&fragment_absolute_rect).sub(gfx.CssPixels.from_int(1)).le(local_position.m_y):
# fully below the fragment
hit_test_result.index_in_node += paint_paintable_fragment_length_in_code_units(fragment)
hit_test_result.vertical_distance = local_position.m_y.sub(gfx.css_pixel_rect_bottom(&fragment_absolute_rect))
elif local_position.m_y.lt(gfx.css_pixel_rect_top(&fragment_absolute_rect)):
# fully above the fragment
hit_test_result.vertical_distance = gfx.css_pixel_rect_top(&fragment_absolute_rect).sub(local_position.m_y)
else:
# vertically within the fragment
hit_test_result.vertical_distance = gfx.CssPixels.from_int(0)
if local_position.m_x.lt(gfx.css_pixel_rect_left(&fragment_absolute_rect)):
hit_test_result.horizontal_distance = gfx.css_pixel_rect_left(&fragment_absolute_rect).sub(local_position.m_x)
else:
hit_test_result.index_in_node += paint_paintable_fragment_length_in_code_units(fragment)
hit_test_result.horizontal_distance = local_position.m_x.sub(gfx.css_pixel_rect_right(&fragment_absolute_rect))
if ak.function1_call(&callback, hit_test_result) == TraversalDecision.break_:
return TraversalDecision.break_
return TraversalDecision.continue_
# mark: Painting ===================================================================================
## The `line_thickness` lambda of resolve_text_fragment_properties.
func paint_resolve_text_fragment_line_thickness(text_node: LayoutTextNode*, glyph_height: gfx.CssPixels) -> gfx.CssPixels:
let one = gfx.CssPixels.from_int(1)
let thickness = css_computed_values_text_decoration_thickness(&layout_node_computed_values(text_node.layout_node()).computed_values)
match thickness.value:
.css_text_decoration_thickness_auto(_):
# https://drafts.csswg.org/css-text-decor-4/#valdef-text-decoration-thickness-auto
# The UA chooses an appropriate thickness for text decoration lines; see below.
return ak.max(gfx.css_pixels_scaled_f64(&glyph_height, 0.1), one)
.css_text_decoration_thickness_from_font(_):
# https://drafts.csswg.org/css-text-decor-4/#valdef-text-decoration-thickness-from-font
# If the first available font has metrics indicating a preferred underline width, use that width,
# otherwise behaves as auto.
# FIXME: Implement this properly.
return ak.max(gfx.css_pixels_scaled_f64(&glyph_height, 0.1), one)
.css_length_percentage(length_percentage):
# https://drafts.csswg.org/css-text-decor-4/#valdef-text-decoration-thickness-length-percentage
let em = css_length_new(1.0, css_data.LengthUnit.em)
let resolved = css_percentage_or_resolved_css_pixels(&length_percentage.percentage_or, text_node.layout_node(), css_length_to_px_layout_node(&em, text_node.layout_node()))
let resolved_length = css_length_to_px_layout_node(&resolved, text_node.layout_node())
return ak.max(resolved_length, one)
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:202 Web::Painting::resolve_text_fragment_properties.
func paint_resolve_text_fragment_properties(paintable_with_lines: PaintPaintableWithLines*):
let parent_layout_node = paint_paintable_layout_node(paintable_with_lines.paint_paintable())
for fragment in &ak.vector_span(paint_paintable_with_lines_fragments(paintable_with_lines)):
let text_node = as_if_layout_text_node(paint_paintable_fragment_layout_node(fragment)) else continue
let font = layout_node_first_available_font(text_node.layout_node())
let glyph_height = gfx.css_pixels_nearest_value_for_f32(web_fonts.font_pixel_size(font))
let line_thickness = paint_resolve_text_fragment_line_thickness(text_node, glyph_height)
paint_paintable_fragment_set_text_decoration_thickness(fragment, line_thickness)
var text_shadow = css_computed_values_text_shadow(&layout_node_computed_values(text_node.layout_node()).computed_values)
var resolved_shadow_data: ak.Vector[PaintShadowData]
if not ak.vector_is_empty(&text_shadow):
ak.vector_ensure_capacity(&resolved_shadow_data, ak.vector_size(&text_shadow))
for layer in ak.vector_span(&text_shadow):
ak.vector_append_t(&resolved_shadow_data, paint_shadow_data_from_css(layer, parent_layout_node))
paint_paintable_fragment_set_shadows(fragment, resolved_shadow_data)
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:246 Web::Painting::PaintableWithLines::paint (override).
pub func paint_paintable_with_lines_paint_impl(base: PaintPaintable*, context: DisplayListRecordingContext*, phase: PaintPhase):
let this = (PaintPaintableWithLines*)base
if not paint_paintable_is_visible(base):
return
paint_paintable_box_paint_impl(base, context, phase)
if phase == PaintPhase.foreground:
paint_resolve_text_fragment_properties(this)
var spans: ak.Vector[PaintPaintableFragmentFragmentSpan]
for fragment in &ak.vector_span(&this.m_fragments):
paint_compute_render_spans(fragment, &spans)
for span in &ak.vector_span(&spans):
if gfx.color_alpha(&span.background_color) > 0:
let selection_rect = display_list_recording_context_rounded_device_rect(context, paint_paintable_fragment_selection_rect(span.fragment))
display_list.display_list_recorder_fill_rect(display_list_recording_context_display_list_recorder(context), selection_rect, span.background_color)
for span in ak.vector_span(&spans):
paint_paint_text_shadow(context, span)
for span in ak.vector_span(&spans):
paint_paint_text_fragment(context, span)
if dom_document_cursor_position(paint_paintable_document(base)) != none:
paint_paintable_with_lines_paint_cursor(this, context)
## A FragmentSpan with no selection styling.
func paint_fragment_span(fragment: const PaintPaintableFragment*, start_code_unit: usize, end_code_unit: usize, text_color: gfx.Color) -> PaintPaintableFragmentFragmentSpan:
return PaintPaintableFragmentFragmentSpan(
fragment = fragment,
start_code_unit = start_code_unit,
end_code_unit = end_code_unit,
text_color = text_color,
background_color = gfx.color_new_named_color(gfx.ColorNamedColor.transparent),
shadow_layers = none,
text_decoration = none,
)
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:280 Web::Painting::compute_render_spans.
## The fragment is passed by pointer (C++: by const&; each span refers to it).
func paint_compute_render_spans(fragment: const PaintPaintableFragment*, spans: ak.Vector[PaintPaintableFragmentFragmentSpan]*):
let transparent = gfx.color_new_named_color(gfx.ColorNamedColor.transparent)
let text_paintable_or_none = as_if_paint_text_paintable(paint_paintable_fragment_paintable(fragment))
if text_paintable_or_none == none:
# Non-text fragments still need shadow painting.
ak.vector_append_t(spans, paint_fragment_span(fragment, 0, 0, transparent))
return
let text_paintable = text_paintable_or_none else trap("unreachable")
if not paint_paintable_is_visible(text_paintable.paint_paintable()):
return
let text_color = css_computed_values_webkit_text_fill_color(&paint_paintable_computed_values(text_paintable.paint_paintable()).computed_values)
# No selection: single span with base styling.
let selection_offsets_or_none = paint_paintable_fragment_selection_offsets(fragment)
if selection_offsets_or_none == none:
ak.vector_append_t(spans, paint_fragment_span(fragment, 0, paint_paintable_fragment_length_in_code_units(fragment), text_color))
return
let selection_offsets = selection_offsets_or_none else trap("unreachable")
let selection_start = selection_offsets.start
let selection_end = selection_offsets.end
let selection_style = paint_paintable_selection_style(text_paintable.paint_paintable())
let selection_text_color = selection_style.text_color else text_color
# Convert selection text decoration to fragment text decoration data.
var selection_text_decoration: PaintPaintableFragmentTextDecorationData? = none
if let text_decoration = selection_style.text_decoration:
selection_text_decoration = PaintPaintableFragmentTextDecorationData(
line = text_decoration.line,
style = text_decoration.style,
color = text_decoration.color,
)
# Before selection.
if selection_start > 0:
ak.vector_append_t(spans, paint_fragment_span(fragment, 0, selection_start, text_color))
# Selected portion.
if selection_start < selection_end:
ak.vector_append_t(spans, PaintPaintableFragmentFragmentSpan(
fragment = fragment,
start_code_unit = selection_start,
end_code_unit = selection_end,
text_color = selection_text_color,
background_color = selection_style.background_color,
shadow_layers = selection_style.text_shadow,
text_decoration = selection_text_decoration,
))
# After selection.
if selection_end < paint_paintable_fragment_length_in_code_units(fragment):
ak.vector_append_t(spans, paint_fragment_span(fragment, selection_end, paint_paintable_fragment_length_in_code_units(fragment), text_color))
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:381 Web::Painting::paint_text_fragment.
func paint_paint_text_fragment(context: DisplayListRecordingContext*, span: PaintPaintableFragmentFragmentSpan):
let fragment = span.fragment
# Skip non-text spans (they're only for shadow painting).
if span.start_code_unit == span.end_code_unit:
return
let text_paintable = as_paint_text_paintable(paint_paintable_fragment_paintable(fragment))
if display_list_recording_context_should_show_line_box_borders(context):
paint_paintable_with_lines_paint_text_fragment_debug_highlight(context, fragment)
let glyph_run = paint_paintable_fragment_glyph_run(fragment) else return
let painter = display_list_recording_context_display_list_recorder(context)
let fragment_absolute_rect = paint_paintable_fragment_absolute_rect(fragment)
let fragment_device_rect = display_list_recording_context_enclosing_device_rect(context, fragment_absolute_rect)
let scale = display_list_recording_context_device_pixels_per_css_pixel(context)
let baseline_start = gfx.FloatPoint(
m_x = gfx.css_pixel_rect_x(&fragment_absolute_rect).to_float() * (f32)scale,
m_y = (gfx.css_pixel_rect_y(&fragment_absolute_rect).to_float() + paint_paintable_fragment_baseline(fragment).to_float()) * (f32)scale,
)
# Paint text, clipped to span range if not full fragment.
let is_full_fragment = span.start_code_unit == 0 and span.end_code_unit == paint_paintable_fragment_length_in_code_units(fragment)
if is_full_fragment:
display_list.display_list_recorder_draw_glyph_run(painter, baseline_start, glyph_run, span.text_color, fragment_device_rect, scale, paint_paintable_fragment_orientation(fragment))
else:
let range_rect = paint_paintable_fragment_range_rect(fragment, PaintPaintableSelectionState.start_and_end,
paint_paintable_fragment_start_offset(fragment) + span.start_code_unit,
paint_paintable_fragment_start_offset(fragment) + span.end_code_unit)
let span_rect = display_list_recording_context_rounded_device_rect(context, range_rect)
display_list.display_list_recorder_save(painter)
display_list.display_list_recorder_add_clip_rect(painter, span_rect)
display_list.display_list_recorder_draw_glyph_run(painter, baseline_start, glyph_run, span.text_color, fragment_device_rect, scale, paint_paintable_fragment_orientation(fragment))
display_list.display_list_recorder_restore(painter)
paint_paint_text_decoration(context, text_paintable, span)
# mark: The cursor =================================================================================
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:423 Web::Painting::PaintableWithLines::fragment_at_position.
func paint_paintable_with_lines_fragment_at_position(this: PaintPaintableWithLines*, position: DomPosition*) -> const PaintPaintableFragment*?:
# m_fragments.first_matching([&](auto const& fragment) { ... })
for fragment in &ak.vector_span(&this.m_fragments):
let text_paintable = as_if_paint_text_paintable(paint_paintable_fragment_paintable(fragment)) else continue
if (usize)dom_position_offset(position) < paint_paintable_fragment_start_offset(fragment):
continue
if (usize)dom_position_offset(position) > paint_paintable_fragment_start_offset(fragment) + paint_paintable_fragment_length_in_code_units(fragment):
continue
if (DomNode*?)dom_position_node(position) == paint_paintable_dom_node(text_paintable.paint_paintable()):
return fragment
return none
## Ported from Libraries/LibWeb/Painting/PaintableWithLines.cpp:437 Web::Painting::PaintableWithLines::paint_cursor.
func paint_paintable_with_lines_paint_cursor(this: PaintPaintableWithLines*, context: DisplayListRecordingContext*):
let document = paint_paintable_document(this.paint_paintable())
let navigable = dom_document_navigable(document) else trap("a document with a cursor has no navigable")
if not dom_document_cursor_blink_state(document) or not html_navigable_is_focused(navigable):
return
let cursor_position = dom_document_cursor_position(document) else trap("VERIFY: no cursor position")
let dom_node = layout_node_dom_node(paint_paintable_layout_node(this.paint_paintable())) else return
var active_element_is_editable = false
if let active_element = dom_document_active_element(document):
if let text_control = as_if_form_associated_text_control_element(active_element):
let html_element = html_form_associated_text_control_element_text_control_to_html_element(text_control)
active_element_is_editable = html_form_associated_element_is_mutable(&html_element.form_associated_element)
if not active_element_is_editable and not dom_node_is_editable_or_editing_host(dom_node):
return
var cursor_rect = gfx.CssPixelRect()
var caret_color = gfx.Color()
if let fragment = paint_paintable_with_lines_fragment_at_position(this, cursor_position):
caret_color = css_computed_values_caret_color(&paint_paintable_computed_values(as_paint_text_paintable(paint_paintable_fragment_paintable(fragment)).paint_paintable()).computed_values)
cursor_rect = paint_paintable_fragment_range_rect(fragment, PaintPaintableSelectionState.start_and_end, (usize)dom_position_offset(cursor_position), (usize)dom_position_offset(cursor_position))
else:
# Empty editable elements have no fragments, but should still draw a cursor.
if dom_position_node(cursor_position) != dom_node:
return
let computed_values = &paint_paintable_computed_values(this.paint_paintable()).computed_values
caret_color = css_computed_values_caret_color(computed_values)
let content_box = paint_paintable_box_absolute_padding_box_rect(this.paint_paintable_box())
cursor_rect = gfx.CssPixelRect(gfx.css_pixel_rect_location(&content_box), gfx.CssPixelSize(gfx.CssPixels.from_int(1), css_computed_values_line_height(computed_values)))
if gfx.color_alpha(&caret_color) == 0:
return
let cursor_device_rect = display_list_recording_context_rounded_device_rect(context, cursor_rect)
display_list.display_list_recorder_fill_rect(display_list_recording_context_display_list_recorder(context), cursor_device_rect, caret_color)