Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / painting / svg_path_paintable.lucb

#==============================================================================================
#
#   svg_path_paintable - Painting::SVGPathPaintable: the paintable of a shape, a path or text
#
#   DESCRIPTION:
#       Port of LibWeb/Painting/SVGPathPaintable.cpp (lines 1-229) and SVGPathPaintable.h:
#       creation, the computed path layout gives it, the bounds it adds to a clip path, hit
#       testing, and painting: the geometry alone for a clip path
#       (https://drafts.fxtf.org/css-masking/#ClipPathElement), otherwise the fill and the
#       stroke in paint-order, with paint servers or colors.
#
#       Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2023, MacDue <macdue@dueutil.tech>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Creation and the computed path ==========================================================

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:17 Web::Painting::SVGPathPaintable::create.
pub func paint_svg_path_paintable_create(layout_box: LayoutSvgGraphicsBox*) -> PaintSvgPathPaintable*:
    let heap = gc.cell_heap(layout_box.cell())
    let this = (PaintSvgPathPaintable*)web_heap_allocate_cell(heap, &paint_svg_path_paintable_class)
    gc.heap_defer_gc(heap)
    paint_svg_path_paintable_construct(this, layout_box)
    gc.heap_undefer_gc(heap)
    return this

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:22 Web::Painting::SVGPathPaintable::SVGPathPaintable.
pub func paint_svg_path_paintable_construct(this: PaintSvgPathPaintable*, layout_box: LayoutSvgGraphicsBox*):
    paint_svg_graphics_paintable_construct(this.paint_svg_graphics_paintable(), layout_box)
    paint_svg_graphics_paintable_set_maskable_class(this.paint_svg_graphics_paintable(), &paint_svg_path_paintable_svg_maskable_vtable)
    # The member's default initializer (m_computed_path = {}): the storage is zeroed, so the path is absent.

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:27 Web::Painting::SVGPathPaintable::reset_for_relayout (override).
pub func paint_svg_path_paintable_reset_for_relayout_impl(base: PaintPaintableBox*):
    let this = (PaintSvgPathPaintable*)base
    paint_svg_graphics_paintable_reset_for_relayout_impl(base)
    this.m_computed_path = none

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.h:28 Web::Painting::SVGPathPaintable::dom_node.
pub func paint_svg_path_paintable_dom_node(this: PaintSvgPathPaintable*) -> SvgGraphicsElement*:
    return as_svg_graphics_element(paint_paintable_dom_node(this.paint_paintable()) else trap("VERIFY: an SVG path paintable has a DOM node"))

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.h:30 Web::Painting::SVGPathPaintable::set_computed_path.
pub func paint_svg_path_paintable_set_computed_path(this: PaintSvgPathPaintable*, path: gfx.Path):
    this.m_computed_path = path

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.h:35 Web::Painting::SVGPathPaintable::computed_path.
pub func paint_svg_path_paintable_computed_path(this: PaintSvgPathPaintable*) -> gfx.Path?:
    return this.m_computed_path

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.h:45 Web::Painting::SVGPathPaintable::is_svg_path_paintable (override).
pub func paint_svg_path_paintable_is_svg_path_paintable_impl(base: PaintPaintable*) -> bool:
    _ = base
    return true

## `layout_box().first_ancestor_of_type<Layout::SVGSVGBox>()`.
func paint_svg_path_paintable_svg_node(this: PaintSvgPathPaintable*) -> LayoutSvgsvgBox*?:
    let layout_box = paint_svg_paintable_layout_box(this.paint_svg_paintable())
    return (LayoutSvgsvgBox*?)tree_node_first_ancestor_of_type(layout_box.layout_node(), &layout_svgsvg_box_class)

# mark: Clip paths and hit testing ==============================================================

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:33 Web::Painting::SVGPathPaintable::clip_path_geometry_bounds (override).
pub func paint_svg_path_paintable_clip_path_geometry_bounds_impl(base: PaintSvgPaintable*, additional_transform: gfx.AffineTransform) -> gfx.CssPixelRect?:
    let this = (PaintSvgPathPaintable*)base
    if not paint_svg_paintable_contributes_to_clip_path(base):
        return none
    let computed_path = this.m_computed_path else return none

    let svg_node = paint_svg_path_paintable_svg_node(this) else return none
    let svg_node_paintable_box = layout_box_paintable_box(svg_node.layout_box()) else return none

    let computed_transforms = paint_svg_graphics_paintable_computed_transforms(this.paint_svg_graphics_paintable())
    var path = gfx.path_copy_transformed(&computed_path, paint_svg_graphics_paintable_computed_transforms_svg_to_css_pixels_transform(&computed_transforms, &additional_transform))
    let svg_node_rect = paint_paintable_box_absolute_rect(svg_node_paintable_box)
    let svg_node_location = gfx.css_pixel_rect_location(&svg_node_rect)
    gfx.path_offset(&path, gfx.css_pixel_point_to_type_f32(&svg_node_location))

    let bounding_box = gfx.path_bounding_box(&path)
    return gfx.float_rect_to_type_css_pixels(&bounding_box)

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:48 Web::Painting::SVGPathPaintable::hit_test (override).
pub func paint_svg_path_paintable_hit_test_impl(base: PaintPaintable*, position: gfx.CssPixelPoint, type_: PaintHitTestType, callback: ak.Function1[PaintHitTestResult, TraversalDecision]) -> TraversalDecision:
    let this = (PaintSvgPathPaintable*)base
    let computed_path = this.m_computed_path else return TraversalDecision.continue_

    let computed_transforms = paint_svg_graphics_paintable_computed_transforms(this.paint_svg_graphics_paintable())
    let transformed_path = gfx.path_copy_transformed(&computed_path, paint_svg_graphics_paintable_computed_transforms_svg_to_css_pixels_transform(&computed_transforms))
    let bounding_box = gfx.path_bounding_box(&transformed_path)
    if not gfx.float_rect_contains_float_point(&bounding_box, gfx.css_pixel_point_to_type_f32(&position)):
        return TraversalDecision.continue_

    return paint_paintable_box_hit_test_impl(base, position, type_, callback)

# mark: Painting ================================================================================

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:60 Web::Painting::to_gfx_winding_rule.
func paint_to_gfx_winding_rule(fill_rule: SvgFillRule) -> gfx.WindingRule:
    match fill_rule:
        .nonzero:
            return gfx.WindingRule.nonzero
        .evenodd:
            return gfx.WindingRule.even_odd
        _:
            trap("VERIFY_NOT_REACHED")

## The state the `paint_fill` and `paint_stroke` lambdas of SVGPathPaintable::paint capture by reference.
struct PaintSvgPathPaintablePaintCaptures:
    var this: PaintSvgPathPaintable*
    var context: DisplayListRecordingContext*
    var graphics_element: SvgGraphicsElement*
    var path: gfx.Path
    var paint_context: SvgPaintContext
    var paint_transform: gfx.AffineTransform

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:127 the `paint_fill` lambda of
## Web::Painting::SVGPathPaintable::paint.
func paint_svg_path_paintable_paint_fill(captures: const PaintSvgPathPaintablePaintCaptures*):
    let graphics_element = captures.graphics_element
    let recorder = display_list_recording_context_display_list_recorder(captures.context)
    let should_anti_alias = paint_svg_paintable_should_anti_alias(captures.this.paint_svg_paintable())
    let fill_opacity = svg_graphics_element_fill_opacity(graphics_element) else 1.0f32
    let winding_rule = paint_to_gfx_winding_rule(svg_graphics_element_fill_rule(graphics_element) else SvgFillRule.nonzero)
    if let paint_style = svg_graphics_element_fill_paint_style(graphics_element, captures.paint_context, captures.context):
        display_list.display_list_recorder_fill_path(recorder, display_list.DisplayListRecorderFillPathParams(
            path = captures.path,
            opacity = fill_opacity,
            paint_style_or_color = display_list.PaintStyleOrColor.svg_paint_server_paint_style(paint_style),
            winding_rule = winding_rule,
            should_anti_alias = should_anti_alias,
        ))
    elif let fill_color = svg_graphics_element_fill_color(graphics_element):
        display_list.display_list_recorder_fill_path(recorder, display_list.DisplayListRecorderFillPathParams(
            path = captures.path,
            paint_style_or_color = display_list.PaintStyleOrColor.color(gfx.color_with_opacity(&fill_color, fill_opacity)),
            winding_rule = winding_rule,
            should_anti_alias = should_anti_alias,
        ))

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:148 the `paint_stroke` lambda of
## Web::Painting::SVGPathPaintable::paint.
func paint_svg_path_paintable_paint_stroke(captures: const PaintSvgPathPaintablePaintCaptures*):
    let graphics_element = captures.graphics_element
    let recorder = display_list_recording_context_display_list_recorder(captures.context)
    let should_anti_alias = paint_svg_paintable_should_anti_alias(captures.this.paint_svg_paintable())

    var cap_style = gfx.PathCapStyle.butt
    match svg_graphics_element_stroke_linecap(graphics_element) else css_initial_values_stroke_linecap():
        .butt:
            cap_style = gfx.PathCapStyle.butt
        .round:
            cap_style = gfx.PathCapStyle.round
        .square:
            cap_style = gfx.PathCapStyle.square
        _:
            _ = 0

    var join_style = gfx.PathJoinStyle.miter
    match svg_graphics_element_stroke_linejoin(graphics_element) else css_initial_values_stroke_linejoin():
        .miter:
            join_style = gfx.PathJoinStyle.miter
        .round:
            join_style = gfx.PathJoinStyle.round
        .bevel:
            join_style = gfx.PathJoinStyle.bevel
        _:
            _ = 0

    let miter_limit = svg_graphics_element_stroke_miterlimit(graphics_element) else 0.0
    let stroke_opacity = svg_graphics_element_stroke_opacity(graphics_element) else 1.0f32

    # Note: This is assuming .x_scale() == .y_scale() (which it does currently).
    let viewbox_scale = gfx.affine_transform_x_scale(&captures.paint_transform)
    let stroke_thickness = (svg_graphics_element_stroke_width(graphics_element) else 1.0f32) * viewbox_scale
    var stroke_dasharray = svg_graphics_element_stroke_dasharray(graphics_element)
    for value in &ak.vector_span(&stroke_dasharray):
        *value *= viewbox_scale
    let stroke_dashoffset = (svg_graphics_element_stroke_dashoffset(graphics_element) else 0.0f32) * viewbox_scale

    if let paint_style = svg_graphics_element_stroke_paint_style(graphics_element, captures.paint_context, captures.context):
        display_list.display_list_recorder_stroke_path(recorder, display_list.DisplayListRecorderStrokePathParams(
            cap_style = cap_style,
            join_style = join_style,
            miter_limit = (f32)miter_limit,
            dash_array = stroke_dasharray,
            dash_offset = stroke_dashoffset,
            path = captures.path,
            opacity = stroke_opacity,
            paint_style_or_color = display_list.PaintStyleOrColor.svg_paint_server_paint_style(paint_style),
            thickness = stroke_thickness,
            should_anti_alias = should_anti_alias,
        ))
    elif let stroke_color = svg_graphics_element_stroke_color(graphics_element):
        display_list.display_list_recorder_stroke_path(recorder, display_list.DisplayListRecorderStrokePathParams(
            cap_style = cap_style,
            join_style = join_style,
            miter_limit = (f32)miter_limit,
            dash_array = stroke_dasharray,
            dash_offset = stroke_dashoffset,
            path = captures.path,
            paint_style_or_color = display_list.PaintStyleOrColor.color(gfx.color_with_opacity(&stroke_color, stroke_opacity)),
            thickness = stroke_thickness,
            should_anti_alias = should_anti_alias,
        ))

## Ported from Libraries/LibWeb/Painting/SVGPathPaintable.cpp:72 Web::Painting::SVGPathPaintable::paint (override).
pub func paint_svg_path_paintable_paint_impl(base: PaintPaintable*, context: DisplayListRecordingContext*, phase: PaintPhase):
    let this = (PaintSvgPathPaintable*)base
    let computed_path = this.m_computed_path else return

    if display_list_recording_context_draw_svg_geometry_for_clip_path(context):
        if not paint_svg_paintable_contributes_to_clip_path(this.paint_svg_paintable()):
            return
    elif not paint_paintable_is_visible(base):
        return

    paint_paintable_box_paint_impl(base, context, phase)

    if phase != PaintPhase.foreground:
        return

    let graphics_element = paint_svg_path_paintable_dom_node(this)

    let svg_node = paint_svg_path_paintable_svg_node(this) else trap("VERIFY: an SVG path paintable is inside an <svg>")
    let svg_element_rect = paint_paintable_box_absolute_rect(layout_box_paintable_box(svg_node.layout_box()) else trap("VERIFY: the <svg> box has a paintable box"))

    let device_point = display_list_recording_context_rounded_device_point(context, gfx.css_pixel_rect_location(&svg_element_rect))
    let offset = gfx.int_point_to_type_f32(&device_point)
    let maybe_view_box = svg_fit_to_view_box_view_box(&layout_svgsvg_box_dom_node(svg_node).svg_fit_to_view_box)

    let computed_transforms = paint_svg_graphics_paintable_computed_transforms(this.paint_svg_graphics_paintable())
    let paint_transform = paint_svg_graphics_paintable_computed_transforms_svg_to_device_pixels_transform(&computed_transforms, context)
    var path = gfx.path_copy_transformed(&computed_path, paint_transform)
    gfx.path_offset(&path, offset)

    var svg_viewport: gfx.FloatRect = ---
    if let view_box = maybe_view_box:
        svg_viewport = gfx.FloatRect(m_location = gfx.FloatPoint(m_x = (f32)view_box.min_x, m_y = (f32)view_box.min_y), m_size = gfx.FloatSize(m_width = (f32)view_box.width, m_height = (f32)view_box.height))
    else:
        let svg_element_size = gfx.css_pixel_rect_size(&svg_element_rect)
        svg_viewport = gfx.FloatRect(m_location = gfx.FloatPoint(m_x = 0.0f32, m_y = 0.0f32), m_size = gfx.css_pixel_size_to_type_f32(&svg_element_size))

    if display_list_recording_context_draw_svg_geometry_for_clip_path(context):
        # https://drafts.fxtf.org/css-masking/#ClipPathElement:
        # The raw geometry of each child element exclusive of rendering properties such as fill, stroke, stroke-width
        # within a clipPath conceptually defines a 1-bit mask (with the possible exception of anti-aliasing along
        # the edge of the geometry) which represents the silhouette of the graphics associated with that element.
        display_list.display_list_recorder_fill_path(display_list_recording_context_display_list_recorder(context), display_list.DisplayListRecorderFillPathParams(
            path = path,
            paint_style_or_color = display_list.PaintStyleOrColor.color(gfx.color_new_named_color(gfx.ColorNamedColor.black)),
            winding_rule = paint_to_gfx_winding_rule(svg_graphics_element_clip_rule(graphics_element) else SvgFillRule.nonzero),
            should_anti_alias = paint_svg_paintable_should_anti_alias(this.paint_svg_paintable()),
        ))
        return

    let paint_context = SvgPaintContext(
        viewport = svg_viewport,
        path_bounding_box = gfx.path_bounding_box(&computed_path),
        paint_transform = paint_transform,
    )

    let captures = PaintSvgPathPaintablePaintCaptures(this = this, context = context, graphics_element = graphics_element, path = path, paint_context = paint_context, paint_transform = paint_transform)

    for paint_order in svg_graphics_element_paint_order(graphics_element):
        match paint_order:
            .fill:
                paint_svg_path_paintable_paint_fill(&captures)
            .stroke:
                paint_svg_path_paintable_paint_stroke(&captures)
            .markers:
                # FIXME: Implement marker painting
                _ = 0
            _:
                _ = 0