Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / style_values / transformation_style_value_2.lucb

#==============================================================================================
#
#   transformation_style_value_2 - TransformationStyleValue: serialization, reification and
#                                  absolutization
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/TransformationStyleValue.cpp (lines 383-712) and the inline
#       members of TransformationStyleValue.h: serialize (with the individual transform properties
#       rotate, scale and translate), "reify a transform function" (Typed OM, phase 3),
#       absolutized and Properties::operator==.
#
#       Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
#       Copyright (c) 2024, Steffen T. Larssen <dudedbz@gmail.com>
#       Copyright (c) 2024-2025, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
#       Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Serialization ===========================================================================

## serialize's `resolve_to_number` lambda (the rotate property).
func css_transformation_rotate_resolve_to_number(value: CssStyleValue*) -> f64?:
    if css_style_value_is_number(value):
        return css_number_style_value_number(css_style_value_as_number(value))
    if css_style_value_is_calculated(value) and css_calculated_style_value_resolves_to_number(css_style_value_as_calculated(value)):
        return css_calculated_style_value_resolve_number(css_style_value_as_calculated(value), CssCalculationResolutionContext())

    trap("VERIFY_NOT_REACHED")

## serialize's `resolve_to_string` lambda for the scale property.
func css_transformation_scale_resolve_to_string(value: CssStyleValue*, mode: css_syntax.SerializationMode) -> ak.String:
    var raw_value: f64? = none

    if css_style_value_is_number(value):
        raw_value = css_number_style_value_number(css_style_value_as_number(value))
    if css_style_value_is_percentage(value):
        let percentage = css_percentage_style_value_percentage(css_style_value_as_percentage(value))
        raw_value = css_percentage_as_fraction(&percentage)
    if css_style_value_is_calculated(value):
        let calculated = css_style_value_as_calculated(value)
        if css_calculated_style_value_resolves_to_number(calculated):
            if let resolved = css_calculated_style_value_resolve_number(calculated, CssCalculationResolutionContext()):
                raw_value = resolved
        if css_calculated_style_value_resolves_to_percentage(calculated):
            if let resolved = css_calculated_style_value_resolve_percentage(calculated, CssCalculationResolutionContext()):
                raw_value = css_percentage_as_fraction(&resolved)

    let number = raw_value else return css_style_value_to_string(value, mode)

    return css_syntax.serialize_a_number_f64(number)

## serialize's `resolve_to_string` lambda for the translate property: none for 0px.
func css_transformation_translate_resolve_to_string(value: CssStyleValue*, mode: css_syntax.SerializationMode) -> ak.String?:
    let string_value = css_style_value_to_string(value, mode)

    if ak.string_eq_string_view(&string_value, ak.sv("0px")):
        return none

    return string_value

## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp:367 Web::CSS::TransformationStyleValue::serialize (override).
pub func css_transformation_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let this = (CssTransformationStyleValue*)base
    let values = &this.m_properties.values

    # https://drafts.csswg.org/css-transforms-2/#individual-transform-serialization
    if this.m_properties.property == css_data.PropertyId.rotate:
        # NOTE: Serialize simple rotations directly.
        match this.m_properties.transform_function:
            # If the axis is parallel with the x or y axes, it must serialize as the appropriate keyword.
            .rotate_x:
                ak.string_builder_append(builder, "x ")
                css_style_value_serialize(*ak.vector_at_const(values, 0), builder, mode)
                return
            .rotate_y:
                ak.string_builder_append(builder, "y ")
                css_style_value_serialize(*ak.vector_at_const(values, 0), builder, mode)
                return

            # If a rotation about the z axis (that is, in 2D) is specified, the property must serialize as just an <angle>.
            .rotate, .rotate_z:
                css_style_value_serialize(*ak.vector_at_const(values, 0), builder, mode)
                return

            _:
                _ = 0

        let rotation_x = *ak.vector_at_const(values, 0)
        let rotation_y = *ak.vector_at_const(values, 1)
        let rotation_z = *ak.vector_at_const(values, 2)
        let angle = *ak.vector_at_const(values, 3)

        let x_value = css_transformation_rotate_resolve_to_number(rotation_x) else 0.0
        let y_value = css_transformation_rotate_resolve_to_number(rotation_y) else 0.0
        let z_value = css_transformation_rotate_resolve_to_number(rotation_z) else 0.0

        # If the axis is parallel with the x or y axes, it must serialize as the appropriate keyword.
        if x_value > 0.0 and y_value == 0.0 and z_value == 0.0:
            ak.string_builder_append(builder, "x ")
            css_style_value_serialize(angle, builder, mode)
            return

        if x_value == 0.0 and y_value > 0.0 and z_value == 0.0:
            ak.string_builder_append(builder, "y ")
            css_style_value_serialize(angle, builder, mode)
            return

        # If a rotation about the z axis (that is, in 2D) is specified, the property must serialize as just an <angle>.
        if x_value == 0.0 and y_value == 0.0 and z_value > 0.0:
            css_style_value_serialize(angle, builder, mode)
            return

        # It must serialize as the keyword none if and only if none was originally specified.
        # NOTE: This is handled by returning a keyword from the parser.

        # If any other rotation is specified, the property must serialize with an axis specified.
        css_style_value_serialize(rotation_x, builder, mode)
        ak.string_builder_append_u8(builder, ' ')
        css_style_value_serialize(rotation_y, builder, mode)
        ak.string_builder_append_u8(builder, ' ')
        css_style_value_serialize(rotation_z, builder, mode)
        ak.string_builder_append_u8(builder, ' ')
        css_style_value_serialize(angle, builder, mode)
        return

    if this.m_properties.property == css_data.PropertyId.scale:
        let x_value = css_transformation_scale_resolve_to_string(*ak.vector_at_const(values, 0), mode)
        let y_value = css_transformation_scale_resolve_to_string(*ak.vector_at_const(values, 1), mode)
        var z_value: ak.String? = none
        if ak.vector_size(values) == 3:
            let z = *ak.vector_at_const(values, 2)
            if not css_style_value_is_number(z) or css_number_style_value_number(css_style_value_as_number(z)) != 1.0:
                z_value = css_transformation_scale_resolve_to_string(z, mode)

        var z_is_not_one = false
        if let z = z_value:
            z_is_not_one = not ak.string_eq_string_view(&z, ak.sv("1"))

        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&x_value))
        if not ak.string_equals(&x_value, &y_value) or z_is_not_one:
            ak.string_builder_append(builder, " ")
            ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&y_value))
        if z_is_not_one:
            let z = z_value else trap("unreachable")
            ak.string_builder_append(builder, " ")
            ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&z))
        return

    if this.m_properties.property == css_data.PropertyId.translate:
        let x_value = css_transformation_translate_resolve_to_string(*ak.vector_at_const(values, 0), mode)
        let y_value = css_transformation_translate_resolve_to_string(*ak.vector_at_const(values, 1), mode)
        var z_value: ak.String? = none
        if ak.vector_size(values) == 3:
            let z = *ak.vector_at_const(values, 2)
            let z_length = css_length_style_value_length(css_style_value_as_length(z)) if css_style_value_is_length(z) else css_length_make_px_f64(0.0)
            if not css_style_value_is_length(z) or not css_length_eq(&z_length, css_length_make_px_f64(0.0)):
                z_value = css_transformation_translate_resolve_to_string(z, mode)

        let x_text = x_value else ak.string_from_str("0px")
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&x_text))
        if y_value != none or z_value != none:
            ak.string_builder_append(builder, " ")
            let y_text = y_value else ak.string_from_str("0px")
            ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&y_text))
        if let z = z_value:
            ak.string_builder_append(builder, " ")
            ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&z))
        return

    ak.string_builder_append_string_view(builder, css_data.to_string_transform_function(this.m_properties.transform_function))
    ak.string_builder_append_u8(builder, '(')
    for i in 0..<ak.vector_size(values):
        let value = *ak.vector_at_const(values, i)

        # https://www.w3.org/TR/css-transforms-2/#individual-transforms
        # A <percentage> is equivalent to a <number>, for example scale: 100% is equivalent to scale: 1.
        # Numbers are used during serialization of specified and computed values.
        let function = this.m_properties.transform_function
        if ((function == css_data.TransformFunction.scale or function == css_data.TransformFunction.scale3_d or function == css_data.TransformFunction.scale_x or function == css_data.TransformFunction.scale_y or function == css_data.TransformFunction.scale_z) and css_style_value_is_percentage(value)):
            let percentage = css_percentage_style_value_percentage(css_style_value_as_percentage(value))
            let number = ak.string_number_f64(css_percentage_as_fraction(&percentage))
            ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&number))
        else:
            css_style_value_serialize(value, builder, mode)

        if i != ak.vector_size(values) - 1:
            ak.string_builder_append(builder, ", ")
    ak.string_builder_append_u8(builder, ')')

# mark: Reification =============================================================================

## reify_a_transform_function's `reify_numeric_argument` lambda.
func css_transformation_reify_numeric_argument(this: CssTransformationStyleValue*, realm: Realm*, index: usize) -> CssNumericValue*:
    let reified = css_style_value_reify(*ak.vector_at_const(&this.m_properties.values, index), realm, ak.FlyString())
    return as_if_css_numeric_value(reified) else trap("as<CSSNumericValue> failed")

## reify_a_transform_function's `reify_0`, `reify_1`, `reify_0px` and `reify_0deg` lambdas.
func css_transformation_reify_unit_value(realm: Realm*, value: f64, unit_: str) -> CssNumericValue*:
    return (CssNumericValue*)css_unit_value_create(realm, value, ak.fly_string_from_str(unit_))

## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp:539 Web::CSS::TransformationStyleValue::reify_a_transform_function.
# https://drafts.css-houdini.org/css-typed-om-1/#reify-a-transform-function
pub func css_transformation_style_value_reify_a_transform_function(this: CssTransformationStyleValue*, realm: Realm*) -> CssTransformComponent*!:
    let values_size = ak.vector_size(&this.m_properties.values)

    # To reify a <transform-function> func, perform the appropriate set of steps below, based on func:
    match this.m_properties.transform_function:
        # -> matrix()
        # -> matrix3d()
        #    1. Return a new CSSMatrixComponent object, whose matrix internal slot is set to a 4x4 matrix representing the
        #       same information as func, and whose is2D internal slot is true if func is matrix(), and false otherwise.
        .matrix, .matrix3_d:
            let transform_as_matrix = try css_transformation_style_value_to_matrix(this, none)
            let matrix = geometry_dom_matrix_create(realm)
            geometry_dom_matrix_set_m11(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 0, 0))
            geometry_dom_matrix_set_m12(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 1, 0))
            geometry_dom_matrix_set_m13(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 2, 0))
            geometry_dom_matrix_set_m14(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 3, 0))
            geometry_dom_matrix_set_m21(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 0, 1))
            geometry_dom_matrix_set_m22(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 1, 1))
            geometry_dom_matrix_set_m23(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 2, 1))
            geometry_dom_matrix_set_m24(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 3, 1))
            geometry_dom_matrix_set_m31(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 0, 2))
            geometry_dom_matrix_set_m32(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 1, 2))
            geometry_dom_matrix_set_m33(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 2, 2))
            geometry_dom_matrix_set_m34(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 3, 2))
            geometry_dom_matrix_set_m41(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 0, 3))
            geometry_dom_matrix_set_m42(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 1, 3))
            geometry_dom_matrix_set_m43(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 2, 3))
            geometry_dom_matrix_set_m44(matrix, (f64)gfx.float_matrix4_x4_at_const(&transform_as_matrix, 3, 3))

            let is_2d = CssTransformComponentIs2d.yes if this.m_properties.transform_function == css_data.TransformFunction.matrix else CssTransformComponentIs2d.no
            return css_matrix_component_create(realm, is_2d, matrix)

        # -> translate()
        # -> translateX()
        # -> translateY()
        # -> translate3d()
        # -> translateZ()
        #    1. Return a new CSSTranslate object, whose x, y, and z internal slots are set to the reification of the
        #       specified x/y/z offsets, or the reification of 0px if not specified in func, and whose is2D internal slot
        #       is true if func is translate(), translateX(), or translateY(), and false otherwise.
        .translate:
            # NB: Default y to 0px if it's not specified.
            let y = css_transformation_reify_numeric_argument(this, realm, 1) if values_size > 1 else css_transformation_reify_unit_value(realm, 0.0, "px")
            return css_translate_create(realm, .yes, css_transformation_reify_numeric_argument(this, realm, 0), y, css_transformation_reify_unit_value(realm, 0.0, "px"))
        .translate_x:
            return css_translate_create(realm, .yes, css_transformation_reify_numeric_argument(this, realm, 0), css_transformation_reify_unit_value(realm, 0.0, "px"), css_transformation_reify_unit_value(realm, 0.0, "px"))
        .translate_y:
            return css_translate_create(realm, .yes, css_transformation_reify_unit_value(realm, 0.0, "px"), css_transformation_reify_numeric_argument(this, realm, 0), css_transformation_reify_unit_value(realm, 0.0, "px"))
        .translate3_d:
            return css_translate_create(realm, .no, css_transformation_reify_numeric_argument(this, realm, 0), css_transformation_reify_numeric_argument(this, realm, 1), css_transformation_reify_numeric_argument(this, realm, 2))
        .translate_z:
            return css_translate_create(realm, .no, css_transformation_reify_unit_value(realm, 0.0, "px"), css_transformation_reify_unit_value(realm, 0.0, "px"), css_transformation_reify_numeric_argument(this, realm, 0))

        # -> scale()
        # -> scaleX()
        # -> scaleY()
        # -> scale3d()
        # -> scaleZ()
        #    1. Return a new CSSScale object, whose x, y, and z internal slots are set to the specified x/y/z scales, or
        #       to 1 if not specified in func and whose is2D internal slot is true if func is scale(), scaleX(), or
        #       scaleY(), and false otherwise.
        .scale:
            # NB: Default y to a copy of x if it's not specified.
            let y = css_transformation_reify_numeric_argument(this, realm, 1) if values_size > 1 else css_transformation_reify_numeric_argument(this, realm, 0)
            return css_scale_create(realm, .yes, css_transformation_reify_numeric_argument(this, realm, 0), y, css_transformation_reify_unit_value(realm, 1.0, "number"))
        .scale_x:
            return css_scale_create(realm, .yes, css_transformation_reify_numeric_argument(this, realm, 0), css_transformation_reify_unit_value(realm, 1.0, "number"), css_transformation_reify_unit_value(realm, 1.0, "number"))
        .scale_y:
            return css_scale_create(realm, .yes, css_transformation_reify_unit_value(realm, 1.0, "number"), css_transformation_reify_numeric_argument(this, realm, 0), css_transformation_reify_unit_value(realm, 1.0, "number"))
        .scale3_d:
            return css_scale_create(realm, .no, css_transformation_reify_numeric_argument(this, realm, 0), css_transformation_reify_numeric_argument(this, realm, 1), css_transformation_reify_numeric_argument(this, realm, 2))
        .scale_z:
            return css_scale_create(realm, .no, css_transformation_reify_unit_value(realm, 1.0, "number"), css_transformation_reify_unit_value(realm, 1.0, "number"), css_transformation_reify_numeric_argument(this, realm, 0))

        # -> rotate()
        # -> rotate3d()
        # -> rotateX()
        # -> rotateY()
        # -> rotateZ()
        #    1. Return a new CSSRotate object, whose angle internal slot is set to the reification of the specified angle,
        #       and whose x, y, and z internal slots are set to the specified rotation axis coordinates, or the implicit
        #       axis coordinates if not specified in func and whose is2D internal slot is true if func is rotate(), and
        #       false otherwise.
        .rotate:
            return css_rotate_create(realm, .yes, css_transformation_reify_unit_value(realm, 0.0, "number"), css_transformation_reify_unit_value(realm, 0.0, "number"), css_transformation_reify_unit_value(realm, 1.0, "number"), css_transformation_reify_numeric_argument(this, realm, 0))
        .rotate3_d:
            return css_rotate_create(realm, .no, css_transformation_reify_numeric_argument(this, realm, 0), css_transformation_reify_numeric_argument(this, realm, 1), css_transformation_reify_numeric_argument(this, realm, 2), css_transformation_reify_numeric_argument(this, realm, 3))
        .rotate_x:
            return css_rotate_create(realm, .no, css_transformation_reify_unit_value(realm, 1.0, "number"), css_transformation_reify_unit_value(realm, 0.0, "number"), css_transformation_reify_unit_value(realm, 0.0, "number"), css_transformation_reify_numeric_argument(this, realm, 0))
        .rotate_y:
            return css_rotate_create(realm, .no, css_transformation_reify_unit_value(realm, 0.0, "number"), css_transformation_reify_unit_value(realm, 1.0, "number"), css_transformation_reify_unit_value(realm, 0.0, "number"), css_transformation_reify_numeric_argument(this, realm, 0))
        .rotate_z:
            return css_rotate_create(realm, .no, css_transformation_reify_unit_value(realm, 0.0, "number"), css_transformation_reify_unit_value(realm, 0.0, "number"), css_transformation_reify_unit_value(realm, 1.0, "number"), css_transformation_reify_numeric_argument(this, realm, 0))

        # -> skew()
        #    1. Return a new CSSSkew object, whose ax and ay internal slots are set to the reification of the specified x
        #       and y angles, or the reification of 0deg if not specified in func, and whose is2D internal slot is true.
        .skew:
            # NB: Default y to 0deg if it's not specified.
            let y = css_transformation_reify_numeric_argument(this, realm, 1) if values_size > 1 else css_transformation_reify_unit_value(realm, 0.0, "deg")
            return css_skew_create(realm, css_transformation_reify_numeric_argument(this, realm, 0), y)

        # -> skewX()
        #    1. Return a new CSSSkewX object, whose ax internal slot is set to the reification of the specified x angle,
        #       or the reification of 0deg if not specified in func, and whose is2D internal slot is true.
        .skew_x:
            return css_skew_x_create(realm, css_transformation_reify_numeric_argument(this, realm, 0))

        # -> skewY()
        #    1. Return a new CSSSkewY object, whose ay internal slot is set to the reification of the specified y angle,
        #       or the reification of 0deg if not specified in func, and whose is2D internal slot is true.
        .skew_y:
            return css_skew_y_create(realm, css_transformation_reify_numeric_argument(this, realm, 0))

        # -> perspective()
        #    1. Return a new CSSPerspective object, whose length internal slot is set to the reification of the specified
        #       length (see reify a numeric value if it is a length, and reify an identifier if it is the keyword none)
        #       and whose is2D internal slot is false.
        .perspective:
            let reified = css_style_value_reify(*ak.vector_at_const(&this.m_properties.values, 0), realm, ak.FlyString())
            if let keyword = as_if_css_keyword_value(reified):
                return css_perspective_create(realm, CssPerspectiveValueInternal.css_keyword_value(keyword))
            if let numeric = as_if_css_numeric_value(reified):
                return css_perspective_create(realm, CssPerspectiveValueInternal.css_numeric_value(numeric))
            trap("VERIFY_NOT_REACHED")
        _:
            trap("VERIFY_NOT_REACHED")

# mark: Absolutization and equality =============================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp:684 Web::CSS::TransformationStyleValue::absolutized (override).
pub func css_transformation_style_value_absolutized_impl(base: CssStyleValue*, computation_context: CssComputationContext) -> CssStyleValue*:
    let this = (CssTransformationStyleValue*)base
    var absolutized_values = ak.Vector[CssStyleValue*]()

    var absolutized_values_different = false

    for value in ak.vector_span_const(&this.m_properties.values):
        let absolutized_value = css_style_value_absolutized(value, computation_context)

        if not css_style_values_equal(absolutized_value, value):
            absolutized_values_different = true

        ak.vector_append_t(&absolutized_values, absolutized_value)

    if not absolutized_values_different:
        return base

    return css_transformation_style_value_create(this.m_properties.property, this.m_properties.transform_function, absolutized_values).css_style_value()

## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.cpp:705 Web::CSS::TransformationStyleValue::Properties::operator==.
pub func css_transformation_style_value_properties_eq(this: const CssTransformationStyleValueProperties*, other: CssTransformationStyleValueProperties) -> bool:
    return (this.property == other.property and this.transform_function == other.transform_function and css_style_value_vectors_equal(&this.values, &other.values))

## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h:38 Web::CSS::TransformationStyleValue::properties_equal.
pub func css_transformation_style_value_properties_equal(this: CssTransformationStyleValue*, other: CssTransformationStyleValue*) -> bool:
    return css_transformation_style_value_properties_eq(&this.m_properties, other.m_properties)

## Ported from Libraries/LibWeb/CSS/StyleValues/TransformationStyleValue.h:40 Web::CSS::TransformationStyleValue::is_computationally_independent (override).
pub func css_transformation_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
    let this = (CssTransformationStyleValue*)base
    for value in ak.vector_span_const(&this.m_properties.values):
        if not css_style_value_is_computationally_independent(value):
            return false
    return true

## Ported from Libraries/LibWeb/CSS/StyleValues/StyleValue.h:202 Web::CSS::StyleValueWithDefaultOperators::equals for Web::CSS::TransformationStyleValue (CRTP helper) (override).
pub func css_transformation_style_value_equals_impl(base: CssStyleValue*, other: CssStyleValue*) -> bool:
    if css_style_value_type(base) != css_style_value_type(other):
        return false
    return css_transformation_style_value_properties_equal((CssTransformationStyleValue*)base, (CssTransformationStyleValue*)other)