Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / painting / image_paintable.lucb

#==============================================================================================
#
#   image_paintable - Painting::ImagePaintable: the paintable of an image (<img>, SVG <image>)
#
#   DESCRIPTION:
#       Port of LibWeb/Painting/ImagePaintable.cpp (lines 1-99) and ImagePaintable.h: creation
#       for an ImageBox or an SVGImageBox, the image provider's edges, the alt text state
#       refreshed for a relayout, and painting the alt text or the current frame of the
#       decoded image (object-fit, image-rendering, rounded corners) and the selection
#       highlight. Images are decoded through ImageCodecPlugin in phase 2; the paintable draws
#       whatever decoded image data its provider has.
#
#       Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2026, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## `layout_box.heap().allocate<ImagePaintable>(...)`.
func paint_image_paintable_allocate(layout_box: LayoutBox*, image_provider: LayoutImageProvider*, renders_as_alt_text: bool, alt_text: ak.String, is_svg_image: bool) -> PaintImagePaintable*:
    let heap = gc.cell_heap(layout_box.cell())
    let this = (PaintImagePaintable*)web_heap_allocate_cell(heap, &paint_image_paintable_class)
    gc.heap_defer_gc(heap)
    paint_image_paintable_construct(this, layout_box, image_provider, renders_as_alt_text, alt_text, is_svg_image)
    gc.heap_undefer_gc(heap)
    return this

## Ported from Libraries/LibWeb/Painting/ImagePaintable.cpp:22 Web::Painting::ImagePaintable::create.
pub func paint_image_paintable_create_layout_svg_image_box(layout_box: LayoutSvgImageBox*) -> PaintImagePaintable*:
    let image_element = layout_svg_image_box_dom_node(layout_box)
    return paint_image_paintable_allocate(layout_box.layout_box(), &image_element.image_provider, false, ak.String(), true)

## Ported from Libraries/LibWeb/Painting/ImagePaintable.cpp:27 Web::Painting::ImagePaintable::create.
pub func paint_image_paintable_create_layout_image_box(layout_box: LayoutImageBox*) -> PaintImagePaintable*:
    var alt = ak.String()
    if let element = layout_replaced_box_dom_node(layout_box.layout_replaced_box()):
        alt = dom_element_get_attribute_value(element, html_attribute_names_alt)
    return paint_image_paintable_allocate(layout_box.layout_box(), layout_image_box_image_provider(layout_box), layout_image_box_renders_as_alt_text(layout_box), alt, false)

## Ported from Libraries/LibWeb/Painting/ImagePaintable.cpp:35 Web::Painting::ImagePaintable::ImagePaintable.
pub func paint_image_paintable_construct(this: PaintImagePaintable*, layout_box: LayoutBox*, image_provider: LayoutImageProvider*, renders_as_alt_text: bool, alt_text: ak.String, is_svg_image: bool):
    paint_paintable_box_construct_layout_box(this.paint_paintable_box(), layout_box)
    this.m_renders_as_alt_text = renders_as_alt_text
    this.m_alt_text = alt_text
    this.m_image_provider = image_provider
    this.m_is_svg_image = is_svg_image

## Ported from Libraries/LibWeb/Painting/ImagePaintable.cpp:44 Web::Painting::ImagePaintable::visit_edges (override).
pub func paint_image_paintable_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (PaintImagePaintable*)base
    paint_paintable_box_visit_edges_impl(base, visitor)
    layout_image_provider_image_provider_visit_edges(this.m_image_provider, visitor)

## Ported from Libraries/LibWeb/Painting/ImagePaintable.cpp:50 Web::Painting::ImagePaintable::reset_for_relayout (override).
pub func paint_image_paintable_reset_for_relayout_impl(base: PaintPaintableBox*):
    let this = (PaintImagePaintable*)base
    paint_paintable_box_reset_for_relayout_impl(base)

    if not this.m_is_svg_image:
        this.m_renders_as_alt_text = not layout_image_provider_is_image_available(this.m_image_provider)
        if let image_box = as_if_layout_image_box(paint_paintable_layout_node(base.paint_paintable())):
            if let element = layout_replaced_box_dom_node(image_box.layout_replaced_box()):
                this.m_alt_text = dom_element_get_attribute_value(element, html_attribute_names_alt)

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

    paint_paintable_box_paint_impl(base, context, phase)

    if phase == PaintPhase.foreground:
        let recorder = display_list_recording_context_display_list_recorder(context)
        let computed_values = &paint_paintable_computed_values(base).computed_values
        let image_rect = paint_paintable_box_absolute_rect(this.paint_paintable_box())
        let image_rect_device_pixels = display_list_recording_context_rounded_device_rect(context, image_rect)
        if this.m_renders_as_alt_text:
            if not ak.string_is_empty(&this.m_alt_text):
                let enclosing_rect = display_list_recording_context_enclosing_device_rect(context, image_rect)
                let alt_text = ak.utf16_string_from_utf8_string(&this.m_alt_text)
                display_list.display_list_recorder_draw_text(recorder, enclosing_rect, &alt_text, layout_node_font_display_list_recording_context(paint_paintable_layout_node(base), context), gfx.TextAlignment.center, css_computed_values_color(computed_values))
        elif let decoded_image_data = layout_image_provider_decoded_image_data(this.m_image_provider):
            var corner_clip = paint_scoped_corner_radius_clip_new(context, image_rect_device_pixels, paint_paintable_box_normalized_border_radii_data(this.paint_paintable_box(), PaintPaintableBoxShrinkRadiiForBorders.yes))
            defer paint_scoped_corner_radius_clip_destruct(&corner_clip)
            let image_int_rect_device_pixels = image_rect_device_pixels
            let frame_index = layout_image_provider_current_frame_index(this.m_image_provider)
            let bitmap_rect = html_decoded_image_data_frame_rect(decoded_image_data, frame_index) else image_int_rect_device_pixels
            let scaling_mode = css_to_gfx_scaling_mode(css_computed_values_image_rendering(computed_values), gfx.device_pixel_rect_size(&bitmap_rect), gfx.device_pixel_rect_size(&image_int_rect_device_pixels))

            # https://drafts.csswg.org/css-images/#the-object-fit
            let object_fit = css_data.ObjectFit.contain if this.m_is_svg_image else css_computed_values_object_fit(computed_values)
            let draw_rect = paint_get_replaced_box_painting_area(this.paint_paintable_box(), context, object_fit, gfx.device_pixel_rect_size(&bitmap_rect))
            if not gfx.device_pixel_rect_is_empty(&draw_rect):
                html_decoded_image_data_paint(decoded_image_data, context, frame_index, draw_rect, image_int_rect_device_pixels, scaling_mode)

        if paint_paintable_selection_state(base) != PaintPaintableSelectionState.none_:
            let selection_background_color = paint_paintable_selection_style(base).background_color
            if gfx.color_alpha(&selection_background_color) > 0:
                display_list.display_list_recorder_fill_rect(recorder, image_rect_device_pixels, selection_background_color)