Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   basic_shape_style_value - BasicShapeStyleValue: inset(), xywh(), rect(), circle(), ellipse(),
#                             polygon() and path()
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp (lines 1-476) and the inline
#       members of BasicShapeStyleValue.h: each shape's path, serialization and computational
#       independence, and the style value's serialization and absolutization (where xywh() and
#       rect() compute to inset()). The structs' defaulted operator== (ValueComparing pointers
#       compare values) are the css_*_equal helpers.
#
#       Copyright (c) 2024, MacDue <macdue@dueutil.tech>
#       Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Equality of the shapes ==================================================================

## SVG::PathInstruction's and SVG::Path's defaulted operator== (SVG/Path.h).
func css_svg_paths_equal(a: const SvgPath*, b: const SvgPath*) -> bool:
    if ak.vector_size(&a.m_instructions) != ak.vector_size(&b.m_instructions):
        return false
    for i in 0..<ak.vector_size(&a.m_instructions):
        let left = ak.vector_at_const(&a.m_instructions, i)
        let right = ak.vector_at_const(&b.m_instructions, i)
        if left.type_ != right.type_ or left.absolute != right.absolute:
            return false
        if ak.vector_size(&left.data) != ak.vector_size(&right.data):
            return false
        for j in 0..<ak.vector_size(&left.data):
            if *ak.vector_at_const(&left.data, j) != *ak.vector_at_const(&right.data, j):
                return false
    return true

## Polygon's defaulted operator== (its points' ValueComparing x and y, then the fill rule first as declared).
func css_polygons_equal(a: const CssPolygon*, b: const CssPolygon*) -> bool:
    if a.fill_rule != b.fill_rule:
        return false
    if ak.vector_size(&a.points) != ak.vector_size(&b.points):
        return false
    for i in 0..<ak.vector_size(&a.points):
        let left = ak.vector_at_const(&a.points, i)
        let right = ak.vector_at_const(&b.points, i)
        if not (css_style_values_equal(left.x, right.x) and css_style_values_equal(left.y, right.y)):
            return false
    return true

## BasicShape's operator== (Variant: the same alternative, and that struct's defaulted operator==).
pub func css_basic_shapes_equal(a: const CssBasicShape*, b: const CssBasicShape*) -> bool:
    match *a:
        .css_inset(left):
            match *b:
                .css_inset(right):
                    return (css_style_values_equal(left.top, right.top) and css_style_values_equal(left.right, right.right) and css_style_values_equal(left.bottom, right.bottom) and css_style_values_equal(left.left, right.left) and css_style_values_equal(left.border_radius, right.border_radius))
                _: return false
        .css_xywh(left):
            match *b:
                .css_xywh(right):
                    return (css_style_values_equal(left.x, right.x) and css_style_values_equal(left.y, right.y) and css_style_values_equal(left.width, right.width) and css_style_values_equal(left.height, right.height) and css_style_values_equal(left.border_radius, right.border_radius))
                _: return false
        .css_rect(left):
            match *b:
                .css_rect(right):
                    return (css_style_values_equal(left.top, right.top) and css_style_values_equal(left.right, right.right) and css_style_values_equal(left.bottom, right.bottom) and css_style_values_equal(left.left, right.left) and css_style_values_equal(left.border_radius, right.border_radius))
                _: return false
        .css_circle(left):
            match *b:
                .css_circle(right): return css_style_values_equal(left.radius, right.radius) and css_optional_style_values_equal(left.position, right.position)
                _: return false
        .css_ellipse(left):
            match *b:
                .css_ellipse(right): return css_style_values_equal(left.radius, right.radius) and css_optional_style_values_equal(left.position, right.position)
                _: return false
        .css_polygon(left):
            match *b:
                .css_polygon(right): return css_polygons_equal(&left, &right)
                _: return false
        .css_path(left):
            match *b:
                .css_path(right): return left.fill_rule == right.fill_rule and css_svg_paths_equal(&left.path_instructions, &right.path_instructions)
                _: return false

# mark: Paths ===================================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:21 Web::CSS::path_from_resolved_rect.
func css_path_from_resolved_rect(top: f32, right: f32, bottom: f32, left: f32) -> gfx.Path:
    var path = gfx.Path()
    gfx.path_move_to(&path, gfx.FloatPoint(m_x = left, m_y = top))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = right, m_y = top))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = right, m_y = bottom))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = left, m_y = bottom))
    gfx.path_close(&path)
    return path

## Inset::to_path's `to_border_radius_data` lambda.
func css_inset_to_border_radius_data(corner: CssStyleValue*) -> CssBorderRadiusData:
    let br = css_style_value_as_border_radius(corner)
    return CssBorderRadiusData(
        horizontal_radius = css_length_percentage_from_style_value(css_border_radius_style_value_horizontal_radius(br)),
        vertical_radius = css_length_percentage_from_style_value(css_border_radius_style_value_vertical_radius(br)),
    )

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:33 Web::CSS::Inset::to_path.
# https://drafts.csswg.org/css-shapes/#funcdef-basic-shape-inset
pub func css_inset_to_path(this: const CssInset*, reference_box: gfx.CssPixelRect, node: LayoutNode*) -> gfx.Path:
    let top_value = css_length_percentage_or_auto_from_style_value(this.top)
    let right_value = css_length_percentage_or_auto_from_style_value(this.right)
    let bottom_value = css_length_percentage_or_auto_from_style_value(this.bottom)
    let left_value = css_length_percentage_or_auto_from_style_value(this.left)
    var resolved_top = css_length_percentage_or_auto_to_px_or_zero(&top_value, node, gfx.css_pixel_rect_height(&reference_box)).to_float()
    var resolved_right = css_length_percentage_or_auto_to_px_or_zero(&right_value, node, gfx.css_pixel_rect_width(&reference_box)).to_float()
    var resolved_bottom = css_length_percentage_or_auto_to_px_or_zero(&bottom_value, node, gfx.css_pixel_rect_height(&reference_box)).to_float()
    var resolved_left = css_length_percentage_or_auto_to_px_or_zero(&left_value, node, gfx.css_pixel_rect_width(&reference_box)).to_float()

    # A pair of insets in either dimension that add up to more than the used dimension
    # (such as left and right insets of 75% apiece) use the CSS Backgrounds 3 § 4.5 Overlapping Curves rules
    # to proportionally reduce the inset effect to 100%.
    if resolved_top + resolved_bottom > gfx.css_pixel_rect_height(&reference_box).to_float() or resolved_left + resolved_right > gfx.css_pixel_rect_width(&reference_box).to_float():
        # https://drafts.csswg.org/css-backgrounds-3/#corner-overlap
        # Let f = min(Li/Si), where i ∈ {top, right, bottom, left}, Si is the sum of the two corresponding radii of the
        # corners on side i, and Ltop = Lbottom = the width of the box, and Lleft = Lright = the height of the box. If
        # f < 1, then all corner radii are reduced by multiplying them by f.
        # NB: We only care about vertical and horizontal here as top = bottom and left = right
        let s_vertical = resolved_top + resolved_bottom
        let s_horizontal = resolved_left + resolved_right
        let f = ak.min_f32(gfx.css_pixel_rect_height(&reference_box).to_float() / s_vertical, gfx.css_pixel_rect_width(&reference_box).to_float() / s_horizontal)
        resolved_top *= f
        resolved_right *= f
        resolved_bottom *= f
        resolved_left *= f

    let left_edge: f32 = resolved_left
    let top_edge: f32 = resolved_top
    let right_edge: f32 = gfx.css_pixel_rect_width(&reference_box).to_float() - resolved_right
    let bottom_edge: f32 = gfx.css_pixel_rect_height(&reference_box).to_float() - resolved_bottom

    var inset_rect: gfx.CssPixelRect = ---
    gfx.css_pixel_rect_construct_css_pixels(&inset_rect, gfx.css_pixels_from_f32(left_edge), gfx.css_pixels_from_f32(top_edge), gfx.css_pixels_from_f32(right_edge - left_edge), gfx.css_pixels_from_f32(bottom_edge - top_edge))

    let border_radius_rect = css_style_value_as_border_radius_rect(this.border_radius)

    let radii = paint_normalize_border_radii_data(
        node,
        inset_rect,
        reference_box,
        css_inset_to_border_radius_data(css_border_radius_rect_style_value_top_left(border_radius_rect)),
        css_inset_to_border_radius_data(css_border_radius_rect_style_value_top_right(border_radius_rect)),
        css_inset_to_border_radius_data(css_border_radius_rect_style_value_bottom_right(border_radius_rect)),
        css_inset_to_border_radius_data(css_border_radius_rect_style_value_bottom_left(border_radius_rect)),
    )

    if not display_list.border_radii_data_has_any_radius(&radii):
        return css_path_from_resolved_rect(top_edge, right_edge, bottom_edge, left_edge)

    let top_left_horizontal_radius = radii.top_left.horizontal_radius.to_float()
    let top_left_vertical_radius = radii.top_left.vertical_radius.to_float()
    let top_right_horizontal_radius = radii.top_right.horizontal_radius.to_float()
    let top_right_vertical_radius = radii.top_right.vertical_radius.to_float()
    let bottom_right_horizontal_radius = radii.bottom_right.horizontal_radius.to_float()
    let bottom_right_vertical_radius = radii.bottom_right.vertical_radius.to_float()
    let bottom_left_horizontal_radius = radii.bottom_left.horizontal_radius.to_float()
    let bottom_left_vertical_radius = radii.bottom_left.vertical_radius.to_float()

    var path = gfx.Path()
    gfx.path_move_to(&path, gfx.FloatPoint(m_x = left_edge + top_left_horizontal_radius, m_y = top_edge))
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = right_edge - top_right_horizontal_radius, m_y = top_edge))
    if top_right_horizontal_radius > 0.0 and top_right_vertical_radius > 0.0:
        gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = right_edge, m_y = top_edge + top_right_vertical_radius), gfx.FloatSize(m_width = top_right_horizontal_radius, m_height = top_right_vertical_radius), 0.0, false, true)
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = right_edge, m_y = bottom_edge - bottom_right_vertical_radius))
    if bottom_right_horizontal_radius > 0.0 and bottom_right_vertical_radius > 0.0:
        gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = right_edge - bottom_right_horizontal_radius, m_y = bottom_edge), gfx.FloatSize(m_width = bottom_right_horizontal_radius, m_height = bottom_right_vertical_radius), 0.0, false, true)
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = left_edge + bottom_left_horizontal_radius, m_y = bottom_edge))
    if bottom_left_horizontal_radius > 0.0 and bottom_left_vertical_radius > 0.0:
        gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = left_edge, m_y = bottom_edge - bottom_left_vertical_radius), gfx.FloatSize(m_width = bottom_left_horizontal_radius, m_height = bottom_left_vertical_radius), 0.0, false, true)
    gfx.path_line_to(&path, gfx.FloatPoint(m_x = left_edge, m_y = top_edge + top_left_vertical_radius))
    if top_left_horizontal_radius > 0.0 and top_left_vertical_radius > 0.0:
        gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = left_edge + top_left_horizontal_radius, m_y = top_edge), gfx.FloatSize(m_width = top_left_horizontal_radius, m_height = top_left_vertical_radius), 0.0, false, true)
    gfx.path_close(&path)
    return path

## Appends " round <radius>" unless the serialized border radius is 0px (inset(), xywh() and rect()).
func css_serialize_round_border_radius(builder: ak.StringBuilder*, border_radius: CssStyleValue*, mode: css_syntax.SerializationMode):
    let serialized_border_radius = css_style_value_to_string(border_radius, mode)
    if not ak.string_eq_string_view(&serialized_border_radius, ak.sv("0px")):
        css_appendff(builder, " round {}", [css_arg_string(&serialized_border_radius)])

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:128 Web::CSS::Inset::serialize.
pub func css_inset_serialize(this: const CssInset*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    ak.string_builder_append(builder, "inset(")
    var values = ak.Vector[CssStyleValue*]()
    ak.vector_append_t(&values, this.top)
    ak.vector_append_t(&values, this.right)
    ak.vector_append_t(&values, this.bottom)
    ak.vector_append_t(&values, this.left)
    let positional = css_serialize_a_positional_value_list(&values, mode)
    ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&positional))
    css_serialize_round_border_radius(builder, this.border_radius, mode)
    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:24 Web::CSS::Inset::is_computationally_independent.
pub func css_inset_is_computationally_independent(this: const CssInset*) -> bool:
    return (css_style_value_is_computationally_independent(this.top) and css_style_value_is_computationally_independent(this.right) and css_style_value_is_computationally_independent(this.bottom) and css_style_value_is_computationally_independent(this.left) and css_style_value_is_computationally_independent(this.border_radius))

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:141 Web::CSS::Xywh::serialize.
pub func css_xywh_serialize(this: const CssXywh*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    ak.string_builder_append(builder, "xywh(")
    css_style_value_serialize(this.x, builder, mode)
    ak.string_builder_append_u8(builder, ' ')
    css_style_value_serialize(this.y, builder, mode)
    ak.string_builder_append_u8(builder, ' ')
    css_style_value_serialize(this.width, builder, mode)
    ak.string_builder_append_u8(builder, ' ')
    css_style_value_serialize(this.height, builder, mode)
    css_serialize_round_border_radius(builder, this.border_radius, mode)
    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:46 Web::CSS::Xywh::is_computationally_independent.
pub func css_xywh_is_computationally_independent(this: const CssXywh*) -> bool:
    return (css_style_value_is_computationally_independent(this.x) and css_style_value_is_computationally_independent(this.y) and css_style_value_is_computationally_independent(this.width) and css_style_value_is_computationally_independent(this.height) and css_style_value_is_computationally_independent(this.border_radius))

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:159 Web::CSS::Rect::serialize.
pub func css_rect_serialize(this: const CssRect*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    ak.string_builder_append(builder, "rect(")
    css_style_value_serialize(this.top, builder, mode)
    ak.string_builder_append_u8(builder, ' ')
    css_style_value_serialize(this.right, builder, mode)
    ak.string_builder_append_u8(builder, ' ')
    css_style_value_serialize(this.bottom, builder, mode)
    ak.string_builder_append_u8(builder, ' ')
    css_style_value_serialize(this.left, builder, mode)
    css_serialize_round_border_radius(builder, this.border_radius, mode)
    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:68 Web::CSS::Rect::is_computationally_independent.
pub func css_rect_is_computationally_independent(this: const CssRect*) -> bool:
    return (css_style_value_is_computationally_independent(this.top) and css_style_value_is_computationally_independent(this.right) and css_style_value_is_computationally_independent(this.bottom) and css_style_value_is_computationally_independent(this.left) and css_style_value_is_computationally_independent(this.border_radius))

## The center of a circle() or ellipse() in a reference box translated to the origin (their to_path's first steps).
func css_basic_shape_center(position: CssStyleValue*?, translated_reference_box: gfx.CssPixelRect, node: LayoutNode*) -> gfx.CssPixelPoint:
    # The <position> argument defines the center of the circle. Unless otherwise specified, this defaults to center if omitted.
    var resolved_position = css_position_style_value_create_computed_center()
    if let given = position:
        resolved_position = css_style_value_as_position(given)
    return css_position_style_value_resolved(resolved_position, node, translated_reference_box)

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:177 Web::CSS::Circle::to_path.
pub func css_circle_to_path(this: const CssCircle*, reference_box: gfx.CssPixelRect, node: LayoutNode*) -> gfx.Path:
    # Translating the reference box because PositionStyleValues are resolved to an absolute position.
    let translated_reference_box = gfx.css_pixel_rect_translated_css_pixels_css_pixels(&reference_box, gfx.css_pixel_rect_x(&reference_box).neg(), gfx.css_pixel_rect_y(&reference_box).neg())

    # https://www.w3.org/TR/css-shapes/#funcdef-basic-shape-circle
    let center = css_basic_shape_center(this.position, translated_reference_box, node)

    let radius_px = css_radial_size_style_value_resolve_circle_size(css_style_value_as_radial_size(this.radius), center, translated_reference_box, node).to_float()

    let center_x = gfx.css_pixel_point_x(&center).to_float()
    let center_y = gfx.css_pixel_point_y(&center).to_float()
    var path = gfx.Path()
    gfx.path_move_to(&path, gfx.FloatPoint(m_x = center_x, m_y = center_y + radius_px))
    gfx.path_arc_to(&path, gfx.FloatPoint(m_x = center_x, m_y = center_y - radius_px), radius_px, true, true)
    gfx.path_arc_to(&path, gfx.FloatPoint(m_x = center_x, m_y = center_y + radius_px), radius_px, true, true)
    return path

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:199 Web::CSS::Circle::serialize.
pub func css_circle_serialize(this: const CssCircle*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    ak.string_builder_append(builder, "circle(")

    let serialized_radius = css_style_value_to_string(this.radius, mode)
    let has_radius = not ak.string_eq_string_view(&serialized_radius, ak.sv("closest-side"))
    if has_radius:
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&serialized_radius))

    if let position = this.position:
        if has_radius:
            ak.string_builder_append_u8(builder, ' ')
        ak.string_builder_append(builder, "at ")
        css_style_value_serialize(position, builder, mode)

    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:91 Web::CSS::Circle::is_computationally_independent.
pub func css_circle_is_computationally_independent(this: const CssCircle*) -> bool:
    if not css_style_value_is_computationally_independent(this.radius):
        return false
    if let position = this.position:
        return css_style_value_is_computationally_independent(position)
    return true

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:218 Web::CSS::Ellipse::to_path.
pub func css_ellipse_to_path(this: const CssEllipse*, reference_box: gfx.CssPixelRect, node: LayoutNode*) -> gfx.Path:
    # Translating the reference box because PositionStyleValues are resolved to an absolute position.
    let translated_reference_box = gfx.css_pixel_rect_translated_css_pixels_css_pixels(&reference_box, gfx.css_pixel_rect_x(&reference_box).neg(), gfx.css_pixel_rect_y(&reference_box).neg())

    # https://www.w3.org/TR/css-shapes/#funcdef-basic-shape-circle
    let center = css_basic_shape_center(this.position, translated_reference_box, node)

    let size = css_radial_size_style_value_resolve_ellipse_size(css_style_value_as_radial_size(this.radius), center, translated_reference_box, node)

    let center_x = gfx.css_pixel_point_x(&center).to_float()
    let center_y = gfx.css_pixel_point_y(&center).to_float()
    let width = gfx.css_pixel_size_width(&size).to_float()
    let height = gfx.css_pixel_size_height(&size).to_float()
    var path = gfx.Path()
    gfx.path_move_to(&path, gfx.FloatPoint(m_x = center_x, m_y = center_y + height))
    gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = center_x, m_y = center_y - height), gfx.FloatSize(m_width = width, m_height = height), 0.0, true, true)
    gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = center_x, m_y = center_y + height), gfx.FloatSize(m_width = width, m_height = height), 0.0, true, true)
    return path

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:239 Web::CSS::Ellipse::serialize.
pub func css_ellipse_serialize(this: const CssEllipse*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    ak.string_builder_append(builder, "ellipse(")

    let serialized_radius = css_style_value_to_string(this.radius, mode)
    let has_radius = not ak.string_eq_string_view(&serialized_radius, ak.sv("closest-side closest-side"))
    if has_radius:
        ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&serialized_radius))

    if let position = this.position:
        if has_radius:
            ak.string_builder_append_u8(builder, ' ')
        ak.string_builder_append(builder, "at ")
        css_style_value_serialize(position, builder, mode)

    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:107 Web::CSS::Ellipse::is_computationally_independent.
pub func css_ellipse_is_computationally_independent(this: const CssEllipse*) -> bool:
    if not css_style_value_is_computationally_independent(this.radius):
        return false
    if let position = this.position:
        return css_style_value_is_computationally_independent(position)
    return true

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:258 Web::CSS::Polygon::to_path.
pub func css_polygon_to_path(this: const CssPolygon*, reference_box: gfx.CssPixelRect, node: LayoutNode*) -> gfx.Path:
    var path = gfx.Path()
    gfx.path_set_fill_type(&path, this.fill_rule)
    var first = true
    for point in ak.vector_span_const(&this.points):
        let x = css_length_percentage_from_style_value(point.x)
        let y = css_length_percentage_from_style_value(point.y)
        let resolved_point = gfx.FloatPoint(
            m_x = css_percentage_or_to_px(&x.percentage_or, node, gfx.css_pixel_rect_width(&reference_box)).to_float(),
            m_y = css_percentage_or_to_px(&y.percentage_or, node, gfx.css_pixel_rect_height(&reference_box)).to_float(),
        )
        if first:
            gfx.path_move_to(&path, resolved_point)
        else:
            gfx.path_line_to(&path, resolved_point)
        first = false
    gfx.path_close(&path)
    return path

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:278 Web::CSS::Polygon::serialize.
pub func css_polygon_serialize(this: const CssPolygon*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    ak.string_builder_append(builder, "polygon(")
    var first = true
    match this.fill_rule:
        .nonzero: _ = 0
        .even_odd:
            first = false
            ak.string_builder_append(builder, "evenodd")
        _: _ = 0
    for point in ak.vector_span_const(&this.points):
        if not first:
            ak.string_builder_append(builder, ", ")
        first = false
        css_style_value_serialize(point.x, builder, mode)
        ak.string_builder_append_u8(builder, ' ')
        css_style_value_serialize(point.y, builder, mode)
    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:129 Web::CSS::Polygon::is_computationally_independent.
pub func css_polygon_is_computationally_independent(this: const CssPolygon*) -> bool:
    for point in ak.vector_span_const(&this.points):
        if not (css_style_value_is_computationally_independent(point.x) and css_style_value_is_computationally_independent(point.y)):
            return false
    return true

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:300 Web::CSS::Path::to_path.
pub func css_path_to_path(this: const CssPath*, reference_box: gfx.CssPixelRect, arg1: LayoutNode*) -> gfx.Path:
    _ = reference_box
    _ = arg1
    var result = svg_path_to_gfx_path(&this.path_instructions)
    gfx.path_set_fill_type(&result, this.fill_rule)
    return result

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:308 Web::CSS::Path::serialize.
# https://drafts.csswg.org/css-shapes/#basic-shape-serialization
pub func css_path_serialize(this: const CssPath*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    ak.string_builder_append(builder, "path(")

    # For serializing computed values, component values are computed, and omitted when possible without changing the meaning.
    # NB: So, we don't include `nonzero` in that case.
    if not (mode == css_syntax.SerializationMode.resolved_value and this.fill_rule == gfx.WindingRule.nonzero):
        match this.fill_rule:
            .nonzero: ak.string_builder_append(builder, "nonzero, ")
            .even_odd: ak.string_builder_append(builder, "evenodd, ")
            _: _ = 0

    let serialized = svg_path_serialize(&this.path_instructions)
    css_syntax.serialize_a_string_string_builder(builder, ak.string_bytes_as_string_view(&serialized))
    ak.string_builder_append_u8(builder, ')')

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:145 Web::CSS::Path::is_computationally_independent.
pub func css_path_is_computationally_independent(this: const CssPath*) -> bool:
    _ = this
    return true

# mark: BasicShapeStyleValue ====================================================================

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:156 Web::CSS::BasicShapeStyleValue::create.
pub func css_basic_shape_style_value_create(basic_shape: CssBasicShape) -> CssBasicShapeStyleValue*:
    let this = &(new CssBasicShapeStyleValue[1] --- else trap("out of memory"))[0]
    css_basic_shape_style_value_construct(this, basic_shape)
    return this

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:177 Web::CSS::BasicShapeStyleValue::BasicShapeStyleValue.
pub func css_basic_shape_style_value_construct(this: CssBasicShapeStyleValue*, basic_shape: CssBasicShape):
    css_style_value_construct(this.css_style_value(), CssStyleValueType.basic_shape)
    css_style_value_set_class(this.css_style_value(), (const CssStyleValueVTable*)&css_basic_shape_style_value_vtable, &css_basic_shape_style_value_class)
    this.m_basic_shape = basic_shape

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:162 Web::CSS::BasicShapeStyleValue::basic_shape.
pub func css_basic_shape_style_value_basic_shape(this: CssBasicShapeStyleValue*) -> CssBasicShape:
    return this.m_basic_shape

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:331 Web::CSS::BasicShapeStyleValue::to_path.
pub func css_basic_shape_style_value_to_path(this: CssBasicShapeStyleValue*, reference_box: gfx.CssPixelRect, node: LayoutNode*) -> gfx.Path:
    match this.m_basic_shape:
        .css_inset(shape): return css_inset_to_path(&shape, reference_box, node)
        .css_circle(shape): return css_circle_to_path(&shape, reference_box, node)
        .css_ellipse(shape): return css_ellipse_to_path(&shape, reference_box, node)
        .css_polygon(shape): return css_polygon_to_path(&shape, reference_box, node)
        .css_path(shape): return css_path_to_path(&shape, reference_box, node)
        # NB: Xywh and Rect don't require to_path functions as we should have already converted them to their
        #     respective Inset equivalents during absolutization
        _: trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:344 Web::CSS::BasicShapeStyleValue::serialize (override).
pub func css_basic_shape_style_value_serialize_impl(base: CssStyleValue*, builder: ak.StringBuilder*, mode: css_syntax.SerializationMode):
    let this = (CssBasicShapeStyleValue*)base
    match this.m_basic_shape:
        .css_inset(shape): css_inset_serialize(&shape, builder, mode)
        .css_xywh(shape): css_xywh_serialize(&shape, builder, mode)
        .css_rect(shape): css_rect_serialize(&shape, builder, mode)
        .css_circle(shape): css_circle_serialize(&shape, builder, mode)
        .css_ellipse(shape): css_ellipse_serialize(&shape, builder, mode)
        .css_polygon(shape): css_polygon_serialize(&shape, builder, mode)
        .css_path(shape): css_path_serialize(&shape, builder, mode)

## absolutized's `one_hundred_percent_minus` lambda: calc(100% - values...).
func css_one_hundred_percent_minus(values: const (CssStyleValue*)[], calculation_context: CssCalculationContext) -> CssCalculatedStyleValue*:
    var sum_components = ak.Vector[CssCalculationNode*]()
    ak.vector_append_t(&sum_components, css_numeric_calculation_node_create(CssCalculationNodeNumericValue.css_percentage(css_percentage_new(100.0)), calculation_context).css_calculation_node())
    for value in values:
        ak.vector_append_t(&sum_components, css_negate_calculation_node_create(css_calculation_node_from_style_value((CssStyleValue*)value, calculation_context)).css_calculation_node())
    return css_calculated_style_value_create(css_sum_calculation_node_create(sum_components).css_calculation_node(), css_numeric_type_new(.length, 1), calculation_context)

## absolutized's `absolutize_if_nonnull` lambda.
func css_absolutize_if_nonnull(value: CssStyleValue*?, computation_context: CssComputationContext) -> CssStyleValue*?:
    let present = value else return none
    return css_style_value_absolutized(present, computation_context)

## The Rect alternative's `resolve_auto` lambda.
func css_rect_resolve_auto(style_value: CssStyleValue*, value_of_auto: CssPercentage) -> CssStyleValue*:
    # An auto value makes the edge of the box coincide with the corresponding edge of the reference box:
    # it’s equivalent to 0% as the first (top) or fourth (left) value, and equivalent to 100% as the second
    # (right) or third (bottom) value.
    if css_style_value_is_keyword(style_value):
        assert(css_style_value_to_keyword(style_value) == css_data.Keyword.auto)
        return css_percentage_style_value_create(value_of_auto).css_style_value()
    return style_value

## The `visit` of absolutized: the absolutized shape.
func css_basic_shape_absolutized(shape_value: const CssBasicShape*, computation_context: CssComputationContext) -> CssBasicShape:
    let calculation_context = CssCalculationContext(percentages_resolve_as = CssValueType.length)
    match *shape_value:
        .css_inset(shape):
            let absolutized_top = css_style_value_absolutized(shape.top, computation_context)
            let absolutized_right = css_style_value_absolutized(shape.right, computation_context)
            let absolutized_bottom = css_style_value_absolutized(shape.bottom, computation_context)
            let absolutized_left = css_style_value_absolutized(shape.left, computation_context)
            let absolutized_border_radius = css_style_value_absolutized(shape.border_radius, computation_context)
            if (css_style_values_equal(absolutized_top, shape.top) and css_style_values_equal(absolutized_right, shape.right) and css_style_values_equal(absolutized_bottom, shape.bottom) and css_style_values_equal(absolutized_left, shape.left) and css_style_values_equal(absolutized_border_radius, shape.border_radius)):
                return *shape_value
            return CssBasicShape.css_inset(CssInset(top = absolutized_top, right = absolutized_right, bottom = absolutized_bottom, left = absolutized_left, border_radius = absolutized_border_radius))
        .css_xywh(shape):
            # Note: Given xywh(x y w h), the equivalent function is inset(y calc(100% - x - w) calc(100% - y - h) x).
            let absolutized_top = css_style_value_absolutized(shape.y, computation_context)
            let right_terms: CssStyleValue*[2] = [shape.x, shape.width]
            let absolutized_right = css_style_value_absolutized(css_one_hundred_percent_minus(right_terms, calculation_context).css_style_value(), computation_context)
            let bottom_terms: CssStyleValue*[2] = [shape.y, shape.height]
            let absolutized_bottom = css_style_value_absolutized(css_one_hundred_percent_minus(bottom_terms, calculation_context).css_style_value(), computation_context)
            let absolutized_left = css_style_value_absolutized(shape.x, computation_context)
            let absolutized_border_radius = css_style_value_absolutized(shape.border_radius, computation_context)
            return CssBasicShape.css_inset(CssInset(top = absolutized_top, right = absolutized_right, bottom = absolutized_bottom, left = absolutized_left, border_radius = absolutized_border_radius))
        .css_rect(shape):
            # Note: Given rect(t r b l), the equivalent function is inset(t calc(100% - r) calc(100% - b) l).
            let absolutized_top = css_style_value_absolutized(css_rect_resolve_auto(shape.top, css_percentage_new(0.0)), computation_context)
            let right_terms: CssStyleValue*[1] = [css_rect_resolve_auto(shape.right, css_percentage_new(100.0))]
            let absolutized_right = css_style_value_absolutized(css_one_hundred_percent_minus(right_terms, calculation_context).css_style_value(), computation_context)
            let bottom_terms: CssStyleValue*[1] = [css_rect_resolve_auto(shape.bottom, css_percentage_new(100.0))]
            let absolutized_bottom = css_style_value_absolutized(css_one_hundred_percent_minus(bottom_terms, calculation_context).css_style_value(), computation_context)
            let absolutized_left = css_style_value_absolutized(css_rect_resolve_auto(shape.left, css_percentage_new(0.0)), computation_context)
            let absolutized_border_radius = css_style_value_absolutized(shape.border_radius, computation_context)
            return CssBasicShape.css_inset(CssInset(top = absolutized_top, right = absolutized_right, bottom = absolutized_bottom, left = absolutized_left, border_radius = absolutized_border_radius))
        .css_circle(shape):
            let absolutized_radius = css_style_value_absolutized(shape.radius, computation_context)
            let absolutized_position = css_absolutize_if_nonnull(shape.position, computation_context)
            if css_style_values_equal(absolutized_radius, shape.radius) and css_optional_style_values_equal(absolutized_position, shape.position):
                return *shape_value
            return CssBasicShape.css_circle(CssCircle(radius = absolutized_radius, position = absolutized_position))
        .css_ellipse(shape):
            let absolutized_radius = css_style_value_absolutized(shape.radius, computation_context)
            let absolutized_position = css_absolutize_if_nonnull(shape.position, computation_context)
            if css_style_values_equal(absolutized_radius, shape.radius) and css_optional_style_values_equal(absolutized_position, shape.position):
                return *shape_value
            return CssBasicShape.css_ellipse(CssEllipse(radius = absolutized_radius, position = absolutized_position))
        .css_polygon(shape):
            var absolutized_points = ak.Vector[CssPolygonPoint]()
            ak.vector_ensure_capacity(&absolutized_points, ak.vector_size(&shape.points))
            var any_point_required_absolutization = false
            for point in ak.vector_span_const(&shape.points):
                let absolutized_x = css_style_value_absolutized(point.x, computation_context)
                let absolutized_y = css_style_value_absolutized(point.y, computation_context)
                if css_style_values_equal(absolutized_x, point.x) and css_style_values_equal(absolutized_y, point.y):
                    ak.vector_append_t(&absolutized_points, point)
                    continue
                any_point_required_absolutization = true
                ak.vector_append_t(&absolutized_points, CssPolygonPoint(x = absolutized_x, y = absolutized_y))
            if not any_point_required_absolutization:
                return *shape_value
            return CssBasicShape.css_polygon(CssPolygon(fill_rule = shape.fill_rule, points = absolutized_points))
        .css_path(_):
            return *shape_value

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp:352 Web::CSS::BasicShapeStyleValue::absolutized (override).
# https://www.w3.org/TR/css-shapes-1/#basic-shape-computed-values
pub func css_basic_shape_style_value_absolutized_impl(base: CssStyleValue*, computation_context: CssComputationContext) -> CssStyleValue*:
    let this = (CssBasicShapeStyleValue*)base
    # The values in a <basic-shape> function are computed as specified, with these exceptions:
    # - Omitted values are included and compute to their defaults.
    # FIXME: - A <position> value in circle() or ellipse() is computed as a pair of offsets (horizontal then vertical) from the top left origin, each given as a <length-percentage>.
    # FIXME: - A <'border-radius'> value in a <basic-shape-rect> function is computed as an expanded list of all eight <length-percentage> values.
    # - All <basic-shape-rect> functions compute to the equivalent inset() function.
    let absolutized_shape = css_basic_shape_absolutized(&this.m_basic_shape, computation_context)

    if css_basic_shapes_equal(&absolutized_shape, &this.m_basic_shape):
        return base

    return css_basic_shape_style_value_create(absolutized_shape).css_style_value()

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:167 Web::CSS::BasicShapeStyleValue::properties_equal.
pub func css_basic_shape_style_value_properties_equal(this: CssBasicShapeStyleValue*, other: CssBasicShapeStyleValue*) -> bool:
    return css_basic_shapes_equal(&this.m_basic_shape, &other.m_basic_shape)

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

## Ported from Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h:169 Web::CSS::BasicShapeStyleValue::is_computationally_independent (override).
pub func css_basic_shape_style_value_is_computationally_independent_impl(base: CssStyleValue*) -> bool:
    let this = (CssBasicShapeStyleValue*)base
    match this.m_basic_shape:
        .css_inset(shape): return css_inset_is_computationally_independent(&shape)
        .css_xywh(shape): return css_xywh_is_computationally_independent(&shape)
        .css_rect(shape): return css_rect_is_computationally_independent(&shape)
        .css_circle(shape): return css_circle_is_computationally_independent(&shape)
        .css_ellipse(shape): return css_ellipse_is_computationally_independent(&shape)
        .css_polygon(shape): return css_polygon_is_computationally_independent(&shape)
        .css_path(shape): return css_path_is_computationally_independent(&shape)