Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / style_values / abstract_image_style_value.lucb

#==============================================================================================
#
#   abstract_image_style_value - AbstractImageStyleValue: images and gradients; color stop lists
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/AbstractImageStyleValue.cpp (lines 1-68) and the inline
#       members of AbstractImageStyleValue.h (lines 1-74): the base of ImageStyleValue,
#       ImageSetStyleValue and the gradients, and the color stop lists the gradients share.
#
#       Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
#       Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: AbstractImageStyleValue ================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:22 Web::CSS::AbstractImageStyleValue::natural_width (virtual).
pub func css_abstract_image_style_value_natural_width_impl(this: CssAbstractImageStyleValue*) -> gfx.CssPixels?:
    _ = this
    return none

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:23 Web::CSS::AbstractImageStyleValue::natural_height (virtual).
pub func css_abstract_image_style_value_natural_height_impl(this: CssAbstractImageStyleValue*) -> gfx.CssPixels?:
    _ = this
    return none

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:25 Web::CSS::AbstractImageStyleValue::natural_aspect_ratio (virtual).
pub func css_abstract_image_style_value_natural_aspect_ratio_impl(this: CssAbstractImageStyleValue*) -> gfx.CssPixelFraction?:
    let width = css_abstract_image_style_value_natural_width(this)
    let height = css_abstract_image_style_value_natural_height(this)
    if let w = width:
        if let h = height:
            if not gfx.eq_css_pixels_int(h, 0):
                return w.div(h)
    return none

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:34 Web::CSS::AbstractImageStyleValue::load_any_resources (virtual).
pub func css_abstract_image_style_value_load_any_resources_dom_document_impl(this: CssAbstractImageStyleValue*, arg0: DomDocument*):
    _ = this
    _ = arg0

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.cpp:20 Web::CSS::AbstractImageStyleValue::load_any_resources (virtual).
pub func css_abstract_image_style_value_load_any_resources_layout_node_with_style_impl(this: CssAbstractImageStyleValue*, layout_node: LayoutNodeWithStyle*):
    css_abstract_image_style_value_load_any_resources_dom_document(this, layout_node_document((LayoutNode*)layout_node))

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:36 Web::CSS::AbstractImageStyleValue::resolve_for_size (virtual).
pub func css_abstract_image_style_value_resolve_for_size_impl(this: CssAbstractImageStyleValue*, arg0: LayoutNodeWithStyle*, arg1: gfx.CssPixelSize):
    _ = this
    _ = arg0
    _ = arg1

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:38 Web::CSS::AbstractImageStyleValue::is_paintable (virtual).
pub func css_abstract_image_style_value_is_paintable_impl(this: CssAbstractImageStyleValue*) -> bool:
    _ = this
    trap("pure virtual: Web::CSS::AbstractImageStyleValue::is_paintable")

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:39 Web::CSS::AbstractImageStyleValue::paint (virtual).
pub func css_abstract_image_style_value_paint_impl(this: CssAbstractImageStyleValue*, context: DisplayListRecordingContext*, dest_rect: gfx.DevicePixelRect, arg2: css_data.ImageRendering):
    _ = this
    _ = context
    _ = dest_rect
    _ = arg2
    trap("pure virtual: Web::CSS::AbstractImageStyleValue::paint")

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:41 Web::CSS::AbstractImageStyleValue::color_if_single_pixel_bitmap (virtual).
pub func css_abstract_image_style_value_color_if_single_pixel_bitmap_impl(this: CssAbstractImageStyleValue*) -> gfx.Color?:
    _ = this
    return none

# https://drafts.css-houdini.org/css-typed-om-1/#reify-stylevalue
## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.cpp:14 Web::CSS::AbstractImageStyleValue::reify (override).
pub func css_abstract_image_style_value_reify_impl(base: CssStyleValue*, realm: Realm*, associated_property: ak.FlyString) -> CssCssStyleValue*:
    _ = associated_property
    # AD-HOC: There's no spec description of how to reify as a CSSImageValue.
    return (CssCssStyleValue*)css_image_value_create(realm, base)

# mark: ColorStopListElement ====================================================================

## `absolutize_if_nonnull`, the lambda of ColorStopListElement::absolutized.
func css_color_stop_list_element_absolutize_if_nonnull(input: CssStyleValue*?, context: CssComputationContext) -> CssStyleValue*?:
    let value = input else return none
    return css_style_value_absolutized(value, context)

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.cpp:25 Web::CSS::ColorStopListElement::absolutized.
pub func css_color_stop_list_element_absolutized(this: const CssColorStopListElement*, context: CssComputationContext) -> CssColorStopListElement:
    return CssColorStopListElement(
        transition_hint = css_color_stop_list_element_absolutize_if_nonnull(this.transition_hint, context),
        color_stop = CssColorStopListElementColorStop(
            color = css_color_stop_list_element_absolutize_if_nonnull(this.color_stop.color, context),
            position = css_color_stop_list_element_absolutize_if_nonnull(this.color_stop.position, context),
            second_position = css_color_stop_list_element_absolutize_if_nonnull(this.color_stop.second_position, context),
        ),
    )

## `operator==(ColorStopListElement const&) const = default` (AbstractImageStyleValue.h:61), with the
## ColorStop's defaulted `==` (line 59): each member compares as a ValueComparingRefPtr.
pub func css_color_stop_list_element_eq(this: const CssColorStopListElement*, other: const CssColorStopListElement*) -> bool:
    return (css_optional_style_values_equal(this.transition_hint, other.transition_hint) and css_optional_style_values_equal(this.color_stop.color, other.color_stop.color) and css_optional_style_values_equal(this.color_stop.position, other.color_stop.position) and css_optional_style_values_equal(this.color_stop.second_position, other.color_stop.second_position))

## `Vector<ColorStopListElement>`'s `==`: the same size and equal elements.
pub func css_color_stop_lists_equal(a: const ak.Vector[CssColorStopListElement]*, b: const ak.Vector[CssColorStopListElement]*) -> bool:
    if ak.vector_size(a) != ak.vector_size(b):
        return false
    for i in 0..<ak.vector_size(a):
        if not css_color_stop_list_element_eq(ak.vector_at_const(a, i), ak.vector_at_const(b, i)):
            return false
    return true

## `!value || value->is_computationally_independent()`, for the optional members below.
func css_is_null_or_computationally_independent(value: CssStyleValue*?) -> bool:
    let present = value else return true
    return css_style_value_is_computationally_independent(present)

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.h:64 Web::CSS::ColorStopListElement::is_computationally_independent.
pub func css_color_stop_list_element_is_computationally_independent(this: const CssColorStopListElement*) -> bool:
    return (css_is_null_or_computationally_independent(this.transition_hint) and css_is_null_or_computationally_independent(this.color_stop.color) and css_is_null_or_computationally_independent(this.color_stop.position) and css_is_null_or_computationally_independent(this.color_stop.second_position))

## Ported from Libraries/LibWeb/CSS/StyleValues/AbstractImageStyleValue.cpp:43 Web::CSS::serialize_color_stop_list.
pub func css_serialize_color_stop_list(builder: ak.StringBuilder*, color_stop_list: ak.Vector[CssColorStopListElement], mode: css_syntax.SerializationMode):
    var first = true
    for element in ak.vector_span_const(&color_stop_list):
        if not first:
            ak.string_builder_append(builder, ", ")

        if let transition_hint = element.transition_hint:
            css_style_value_serialize(transition_hint, builder, mode)
            ak.string_builder_append(builder, ", ")

        css_style_value_serialize(element.color_stop.color else trap("VERIFY: a color stop's color"), builder, mode)
        if let position = element.color_stop.position:
            ak.string_builder_append_u8(builder, ' ')
            css_style_value_serialize(position, builder, mode)
        if let second_position = element.color_stop.second_position:
            ak.string_builder_append_u8(builder, ' ')
            css_style_value_serialize(second_position, builder, mode)
        first = false

## `color_stop_list.find_first_index_if(...).has_value()` in the gradients' `create`: whether any color stop has a
## non-keyword color in the modern syntax.
pub func css_color_stop_list_has_non_legacy_color(color_stop_list: const ak.Vector[CssColorStopListElement]*) -> bool:
    for stop in ak.vector_span_const(color_stop_list):
        let color = stop.color_stop.color else trap("VERIFY: a color stop's color")
        if not css_style_value_is_keyword(color) and css_color_style_value_color_syntax(css_style_value_as_color(color)) == CssColorSyntax.modern:
            return true
    return false

## The absolutized color stops of a gradient's `absolutized`.
pub func css_color_stop_list_absolutized(color_stop_list: const ak.Vector[CssColorStopListElement]*, context: CssComputationContext) -> ak.Vector[CssColorStopListElement]:
    var absolutized_color_stops = ak.Vector[CssColorStopListElement]()
    ak.vector_ensure_capacity(&absolutized_color_stops, ak.vector_size(color_stop_list))
    for color_stop in ak.vector_span_const(color_stop_list):
        ak.vector_append_t(&absolutized_color_stops, css_color_stop_list_element_absolutized(&color_stop, context))
    return absolutized_color_stops