Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / painting / marker_paintable.lucb

#==============================================================================================
#
#   marker_paintable - Painting::MarkerPaintable: the paintable of a list item's marker
#
#   DESCRIPTION:
#       Port of LibWeb/Painting/MarkerPaintable.cpp (lines 1-126) and MarkerPaintable.h:
#       creation, and painting a marker: its list-style-image, its text, or the image of a
#       square, circle, disc or disclosure triangle
#       (https://drafts.csswg.org/css-counter-styles-3/#simple-symbolic).
#
#       Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Painting/MarkerPaintable.cpp:17 Web::Painting::MarkerPaintable::create.
pub func paint_marker_paintable_create(layout_box: LayoutListItemMarkerBox*) -> PaintMarkerPaintable*:
    let heap = gc.cell_heap(layout_box.cell())
    let this = (PaintMarkerPaintable*)web_heap_allocate_cell(heap, &paint_marker_paintable_class)
    gc.heap_defer_gc(heap)
    paint_marker_paintable_construct(this, layout_box)
    gc.heap_undefer_gc(heap)
    return this

## Ported from Libraries/LibWeb/Painting/MarkerPaintable.cpp:22 Web::Painting::MarkerPaintable::MarkerPaintable.
pub func paint_marker_paintable_construct(this: PaintMarkerPaintable*, layout_box: LayoutListItemMarkerBox*):
    paint_paintable_box_construct_layout_box(this.paint_paintable_box(), layout_box.layout_box())

## Ported from Libraries/LibWeb/Painting/MarkerPaintable.cpp:27 Web::Painting::MarkerPaintable::layout_box.
pub func paint_marker_paintable_layout_box(this: PaintMarkerPaintable*) -> LayoutListItemMarkerBox*:
    return (LayoutListItemMarkerBox*)paint_paintable_layout_node(this.paint_paintable())

## Ported from Libraries/LibWeb/Painting/MarkerPaintable.cpp:34 Web::Painting::MarkerPaintable::paint (override).
pub func paint_marker_paintable_paint_impl(base: PaintPaintable*, context: DisplayListRecordingContext*, phase: PaintPhase):
    let this = (PaintMarkerPaintable*)base
    if not paint_paintable_is_visible(base):
        return

    if phase == PaintPhase.overlay:
        paint_paintable_box_paint_impl(base, context, phase)
    if phase != PaintPhase.foreground:
        return

    let absolute_rect = paint_paintable_box_absolute_rect(this.paint_paintable_box())
    let marker_rect = gfx.css_pixel_rect_to_rounded(&absolute_rect)
    let device_rect = display_list_recording_context_enclosing_device_rect(context, marker_rect)
    let layout_box = paint_marker_paintable_layout_box(this)
    let computed_values = &paint_paintable_computed_values(base).computed_values
    let recorder = display_list_recording_context_display_list_recorder(context)

    if let list_style_image = layout_node_with_style_list_style_image(layout_box.layout_node_with_style()):
        css_abstract_image_style_value_resolve_for_size(list_style_image, layout_box.layout_node_with_style(), gfx.css_pixel_rect_size(&marker_rect))
        css_abstract_image_style_value_paint(list_style_image, context, device_rect, css_computed_values_image_rendering(computed_values))
        return

    let list_style_type = layout_list_item_marker_box_list_style_type(layout_box)

    match list_style_type:
        .empty:
            trap("VERIFY: a painted marker has a list-style-type")
        _:
            _ = 0

    let color = css_computed_values_color(computed_values)

    if let text = layout_list_item_marker_box_text(layout_box):
        # FIXME: This should use proper text layout logic!
        # This does not line up with the text in the <li> element which looks very sad :(
        let text_utf16 = ak.utf16_string_from_utf8_string(&text)
        display_list.display_list_recorder_draw_text(recorder, device_rect, &text_utf16, layout_node_font_display_list_recording_context(layout_box.layout_node(), context), gfx.TextAlignment.center, color)
        return

    var counter_style: CssCounterStyle*? = none
    match list_style_type:
        .css_counter_style(style):
            counter_style = style
        _:
            trap("VERIFY: the list-style-type of a marker without text is a counter style")
    assert(layout_list_item_marker_box_counter_style_is_rendered_with_custom_image(counter_style))
    let counter_style_name = css_counter_style_name(counter_style else trap("unreachable: a custom image is a counter style's"))

    if ak.fly_string_eq_string_view(&counter_style_name, ak.sv("square")):
        display_list.display_list_recorder_fill_rect(recorder, device_rect, color)
        return

    if ak.fly_string_eq_string_view(&counter_style_name, ak.sv("circle")):
        display_list.display_list_recorder_draw_ellipse(recorder, device_rect, color, 1)
        return

    if ak.fly_string_eq_string_view(&counter_style_name, ak.sv("disc")):
        display_list.display_list_recorder_fill_ellipse(recorder, device_rect, color)
        return

    let left = (f32)gfx.device_pixel_rect_x(&device_rect)
    let right = left + (f32)gfx.device_pixel_rect_width(&device_rect)
    let top = (f32)gfx.device_pixel_rect_y(&device_rect)
    let bottom = top + (f32)gfx.device_pixel_rect_height(&device_rect)

    if ak.fly_string_eq_string_view(&counter_style_name, ak.sv("disclosure-closed")):
        # https://drafts.csswg.org/css-counter-styles-3/#disclosure-closed
        # For the disclosure-open and disclosure-closed counter styles, the marker must be an image or character
        # suitable for indicating the open and closed states of a disclosure widget, such as HTML’s details element.
        # FIXME: If the image is directional, it must respond to the writing mode of the element, similar to the
        #        bidi-sensitive images feature of the Images 4 module.

        # Draw an equilateral triangle pointing right.
        var path: gfx.Path
        gfx.path_move_to(&path, gfx.FloatPoint(m_x = left, m_y = top))
        gfx.path_line_to(&path, gfx.FloatPoint(m_x = left + paint_sin_60_deg * (right - left), m_y = (top + bottom) / 2.0f32))
        gfx.path_line_to(&path, gfx.FloatPoint(m_x = left, m_y = bottom))
        gfx.path_close(&path)
        display_list.display_list_recorder_fill_path(recorder, display_list.DisplayListRecorderFillPathParams(path = path, paint_style_or_color = display_list.PaintStyleOrColor.color(color), winding_rule = gfx.WindingRule.even_odd))
        return

    if ak.fly_string_eq_string_view(&counter_style_name, ak.sv("disclosure-open")):
        # https://drafts.csswg.org/css-counter-styles-3/#disclosure-open
        # For the disclosure-open and disclosure-closed counter styles, the marker must be an image or character
        # suitable for indicating the open and closed states of a disclosure widget, such as HTML’s details element.
        # FIXME: If the image is directional, it must respond to the writing mode of the element, similar to the
        #        bidi-sensitive images feature of the Images 4 module.

        # Draw an equilateral triangle pointing down.
        var path: gfx.Path
        gfx.path_move_to(&path, gfx.FloatPoint(m_x = left, m_y = top))
        gfx.path_line_to(&path, gfx.FloatPoint(m_x = right, m_y = top))
        gfx.path_line_to(&path, gfx.FloatPoint(m_x = (left + right) / 2.0f32, m_y = top + paint_sin_60_deg * (bottom - top)))
        gfx.path_close(&path)
        display_list.display_list_recorder_fill_path(recorder, display_list.DisplayListRecorderFillPathParams(path = path, paint_style_or_color = display_list.PaintStyleOrColor.color(color), winding_rule = gfx.WindingRule.even_odd))
        return

    trap("VERIFY_NOT_REACHED")