Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / painting / field_set_paintable.lucb

#==============================================================================================
#
#   field_set_paintable - Painting::FieldSetPaintable: the paintable of a <fieldset>
#
#   DESCRIPTION:
#       Port of LibWeb/Painting/FieldSetPaintable.cpp (lines 1-144) and FieldSetPaintable.h:
#       creation, the block-start border the rendered legend widens
#       (https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements),
#       the background clipped to the visual border box, and the borders with the top border
#       interrupted behind the legend.
#
#       Copyright (c) 2024, Kostya Farber <kostya.farber@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Painting/FieldSetPaintable.cpp:16 Web::Painting::FieldSetPaintable::create.
pub func paint_field_set_paintable_create(layout_box: LayoutFieldSetBox*) -> PaintFieldSetPaintable*:
    let heap = gc.cell_heap(layout_box.cell())
    let this = (PaintFieldSetPaintable*)web_heap_allocate_cell(heap, &paint_field_set_paintable_class)
    gc.heap_defer_gc(heap)
    paint_field_set_paintable_construct(this, layout_box)
    gc.heap_undefer_gc(heap)
    return this

## Ported from Libraries/LibWeb/Painting/FieldSetPaintable.cpp:21 Web::Painting::FieldSetPaintable::FieldSetPaintable.
pub func paint_field_set_paintable_construct(this: PaintFieldSetPaintable*, layout_box: LayoutFieldSetBox*):
    paint_paintable_box_construct_layout_box(this.paint_paintable_box(), layout_box.layout_box())

## Ported from Libraries/LibWeb/Painting/FieldSetPaintable.cpp:26 Web::Painting::FieldSetPaintable::layout_box.
func paint_field_set_paintable_layout_box(this: PaintFieldSetPaintable*) -> LayoutFieldSetBox*:
    return (LayoutFieldSetBox*)paint_paintable_layout_node(this.paint_paintable())

## Ported from Libraries/LibWeb/Painting/FieldSetPaintable.cpp:31 Web::Painting::FieldSetPaintable::layout_box.
func paint_field_set_paintable_layout_box_const(this: PaintFieldSetPaintable*) -> LayoutFieldSetBox*:
    return paint_field_set_paintable_layout_box(this)

## The paintable box of the rendered legend (`legend->paintable_box()`, which C++ dereferences).
func paint_field_set_paintable_legend_paintable(legend: LayoutLegendBox*) -> PaintPaintableBox*:
    return layout_box_paintable_box(legend.layout_box()) else trap("VERIFY: the rendered legend has a paintable box")

## Ported from Libraries/LibWeb/Painting/FieldSetPaintable.cpp:37 Web::Painting::FieldSetPaintable::effective_border_top.
# https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements
func paint_field_set_paintable_effective_border_top(this: PaintFieldSetPaintable*) -> gfx.CssPixels:
    # The space allocated for the element's border on the block-start side is expected to be the element's
    # 'border-block-start-width' or the rendered legend's margin box size in the fieldset's block-flow direction,
    # whichever is greater.
    let css_border_top = css_computed_values_border_top(&paint_paintable_computed_values(this.paint_paintable()).computed_values).width
    if let legend = layout_field_set_box_rendered_legend(paint_field_set_paintable_layout_box_const(this)):
        let legend_paintable = paint_field_set_paintable_legend_paintable(legend)
        let box_model = paint_paintable_box_box_model(legend_paintable)
        let legend_border_box_rect = paint_paintable_box_absolute_border_box_rect(legend_paintable)
        let legend_margin_box_height = box_model.margin.top.add(gfx.css_pixel_rect_height(&legend_border_box_rect)).add(box_model.margin.bottom)
        return ak.max(css_border_top, legend_margin_box_height)
    return css_border_top

## Ported from Libraries/LibWeb/Painting/FieldSetPaintable.cpp:53 Web::Painting::FieldSetPaintable::visual_border_box_rect.
func paint_field_set_paintable_visual_border_box_rect(this: PaintFieldSetPaintable*) -> gfx.CssPixelRect:
    let css_border_top = css_computed_values_border_top(&paint_paintable_computed_values(this.paint_paintable()).computed_values).width
    let allocated_border_top = paint_field_set_paintable_effective_border_top(this)

    # The CSS border is painted centered within the effective border area (which may be larger than the CSS border when
    # the legend is taller).
    var visual_border_box_rect = paint_paintable_box_absolute_border_box_rect(this.paint_paintable_box())
    if allocated_border_top.le(css_border_top):
        return visual_border_box_rect

    _ = gfx.css_pixel_rect_take_from_top(&visual_border_box_rect, layout_div_int(allocated_border_top.sub(css_border_top), 2))
    return visual_border_box_rect

## Ported from Libraries/LibWeb/Painting/FieldSetPaintable.cpp:68 Web::Painting::FieldSetPaintable::paint_background (override).
pub func paint_field_set_paintable_paint_background_impl(base: PaintPaintableBox*, context: DisplayListRecordingContext*):
    let this = (PaintFieldSetPaintable*)base
    let recorder = display_list_recording_context_display_list_recorder(context)
    display_list.display_list_recorder_save(recorder)
    display_list.display_list_recorder_add_clip_rect(recorder, display_list_recording_context_rounded_device_rect(context, paint_field_set_paintable_visual_border_box_rect(this)))
    paint_paintable_box_paint_background_impl(base, context)
    display_list.display_list_recorder_restore(recorder)

## The state the `paint_borders_with_optional_clip` lambda of FieldSetPaintable::paint captures by reference.
struct PaintFieldSetPaintablePaintCaptures:
    var this: PaintFieldSetPaintable*
    var context: DisplayListRecordingContext*
    var display_list_recorder: display_list.DisplayListRecorder*
    var device_border_rect: gfx.DevicePixelRect

## Ported from Libraries/LibWeb/Painting/FieldSetPaintable.cpp:103 the `paint_borders_with_optional_clip` lambda of
## Web::Painting::FieldSetPaintable::paint.
func paint_field_set_paintable_paint_borders_with_optional_clip(captures: const PaintFieldSetPaintablePaintCaptures*, borders: display_list.BordersDataDevicePixels, clip: gfx.DevicePixelRect?):
    if let clip_rect = clip:
        display_list.display_list_recorder_save(captures.display_list_recorder)
        display_list.display_list_recorder_add_clip_rect(captures.display_list_recorder, clip_rect)

    let radii = paint_paintable_box_normalized_border_radii_data(captures.this.paint_paintable_box())
    let corners = border_radii_data_as_corners(&radii, display_list_recording_context_device_pixel_converter(captures.context))
    paint_paint_all_borders(captures.display_list_recorder, captures.device_border_rect, corners, borders)

    if clip != none:
        display_list.display_list_recorder_restore(captures.display_list_recorder)

## `CSS::BorderData {}`: a transparent border without a style or a width.
func paint_field_set_paintable_empty_border_data() -> display_list.BorderData:
    return display_list.BorderData(color = gfx.color_new_named_color(gfx.ColorNamedColor.transparent), line_style = css_data.LineStyle.none_, width = layout_zero_px())

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

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

    let rendered_legend = layout_field_set_box_rendered_legend(paint_field_set_paintable_layout_box_const(this))
    if rendered_legend == none:
        paint_paintable_box_paint_impl(base, context, phase)
        return
    let legend = rendered_legend else trap("unreachable: the legend was just checked")

    let legend_paintable = paint_field_set_paintable_legend_paintable(legend)

    let legend_border_rect = display_list_recording_context_rounded_device_rect(context, paint_paintable_box_absolute_border_box_rect(legend_paintable))

    let computed_values = &paint_paintable_computed_values(base).computed_values
    let top_border_data = css_computed_values_border_top(computed_values)
    let top_border = display_list_recording_context_enclosing_device_pixels(context, top_border_data.width)

    let device_border_rect = display_list_recording_context_rounded_device_rect(context, paint_field_set_paintable_visual_border_box_rect(this))

    let captures = PaintFieldSetPaintablePaintCaptures(this = this, context = context, display_list_recorder = display_list_recording_context_display_list_recorder(context), device_border_rect = device_border_rect)

    let borders_data = display_list.BordersData(
        top = paint_field_set_paintable_empty_border_data(),
        right = css_computed_values_border_right(computed_values),
        bottom = css_computed_values_border_bottom(computed_values),
        left = css_computed_values_border_left(computed_values),
    )
    paint_field_set_paintable_paint_borders_with_optional_clip(&captures, borders_data_to_device_pixels(&borders_data, *context), none)

    # The top border is not expected to be painted behind the border box of the legend.
    let top_border_only_data = display_list.BordersData(top = top_border_data, right = paint_field_set_paintable_empty_border_data(), bottom = paint_field_set_paintable_empty_border_data(), left = paint_field_set_paintable_empty_border_data())
    let top_border_only = borders_data_to_device_pixels(&top_border_only_data, *context)

    var left_segment: gfx.DevicePixelRect = ---
    gfx.device_pixel_rect_construct_i32(&left_segment,
                                        gfx.device_pixel_rect_x(&device_border_rect),
                                        gfx.device_pixel_rect_y(&device_border_rect),
                                        gfx.device_pixel_rect_x(&legend_border_rect) - gfx.device_pixel_rect_x(&device_border_rect),
                                        top_border)
    paint_field_set_paintable_paint_borders_with_optional_clip(&captures, top_border_only, left_segment)

    var right_segment: gfx.DevicePixelRect = ---
    gfx.device_pixel_rect_construct_i32(&right_segment,
                                        gfx.device_pixel_rect_right(&legend_border_rect),
                                        gfx.device_pixel_rect_y(&device_border_rect),
                                        gfx.device_pixel_rect_right(&device_border_rect) - gfx.device_pixel_rect_right(&legend_border_rect),
                                        top_border)
    paint_field_set_paintable_paint_borders_with_optional_clip(&captures, top_border_only, right_segment)