Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / svg / svg_rect_element.lucb

#==============================================================================================
#
#   svg_rect_element - SVG::SVGRectElement: <rect>
#
#   DESCRIPTION:
#       Port of LibWeb/SVG/SVGRectElement.cpp (lines 1-216): the x, y, width, height, rx and ry
#       attributes (number-percentages), the rectangle's path with rounded corners
#       (https://svgwg.org/svg2-draft/shapes.html#RectElement) and the SVGAnimatedLength
#       accessors.
#
#==============================================================================================

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:20 Web::SVG::SVGRectElement::SVGRectElement.
pub func svg_rect_element_construct(this: SvgRectElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    svg_geometry_element_construct(this.svg_geometry_element(), document, qualified_name)
    # The vptrs of the mixin bases move to SVGRectElement's tables.
    let element = this.svg_element()
    element.element.slottable_mixin.vtable = &svg_rect_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &svg_rect_element_aria_mixin_vtable
    element.element.animatable.vtable = &svg_rect_element_animatable_vtable
    element.global_event_handlers.vtable = &svg_rect_element_global_event_handlers_vtable

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:25 Web::SVG::SVGRectElement::initialize (override).
pub func svg_rect_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.svg_rect_element)
    svg_geometry_element_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:31 Web::SVG::SVGRectElement::attribute_changed (override).
pub func svg_rect_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    let this = (SvgRectElement*)base
    svg_graphics_element_attribute_changed_impl(base, name, old_value, value, namespace_)
    let value_text = value else ak.String()  # value.value_or(String {})

    if ak.fly_string_eq_fly_string(&name, &svg_attribute_names_x):
        this.m_x = svg_attribute_parser_parse_number_percentage(ak.string_bytes_as_string_view(&value_text))
    elif ak.fly_string_eq_fly_string(&name, &svg_attribute_names_y):
        this.m_y = svg_attribute_parser_parse_number_percentage(ak.string_bytes_as_string_view(&value_text))
    elif ak.fly_string_eq_fly_string(&name, &svg_attribute_names_width):
        this.m_width = svg_attribute_parser_parse_number_percentage(ak.string_bytes_as_string_view(&value_text))
    elif ak.fly_string_eq_fly_string(&name, &svg_attribute_names_height):
        this.m_height = svg_attribute_parser_parse_number_percentage(ak.string_bytes_as_string_view(&value_text))
    elif ak.fly_string_eq_fly_string(&name, &svg_attribute_names_rx):
        this.m_radius_x = svg_attribute_parser_parse_number_percentage(ak.string_bytes_as_string_view(&value_text))
    elif ak.fly_string_eq_fly_string(&name, &svg_attribute_names_ry):
        this.m_radius_y = svg_attribute_parser_parse_number_percentage(ak.string_bytes_as_string_view(&value_text))

## `m.value_or(NumberPercentage::create_number(0))` of an optional number-percentage.
func svg_number_percentage_or_zero(value: SvgNumberPercentage?) -> SvgNumberPercentage:
    return value else svg_number_percentage_create_number(0.0)

## `m.value_or(NumberPercentage::create_number(0)).resolve_relative_to(length)`.
func svg_number_percentage_or_zero_resolved(value: SvgNumberPercentage?, length: f32) -> f32:
    let number_percentage = svg_number_percentage_or_zero(value)
    return svg_number_percentage_resolve_relative_to(&number_percentage, length)

## `m.value_or(NumberPercentage::create_number(0)).value()`.
func svg_number_percentage_or_zero_value(value: SvgNumberPercentage?) -> f32:
    let number_percentage = svg_number_percentage_or_zero(value)
    return svg_number_percentage_value(&number_percentage)

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:50 Web::SVG::SVGRectElement::get_path (override).
pub func svg_rect_element_get_path_impl(base: SvgGeometryElement*, viewport_size: gfx.CssPixelSize) -> gfx.Path:
    let this = (SvgRectElement*)base
    let width = svg_number_percentage_or_zero_resolved(this.m_width, viewport_size.m_width.to_float())
    let height = svg_number_percentage_or_zero_resolved(this.m_height, viewport_size.m_height.to_float())
    let x = svg_number_percentage_or_zero_resolved(this.m_x, viewport_size.m_width.to_float())
    let y = svg_number_percentage_or_zero_resolved(this.m_y, viewport_size.m_height.to_float())

    var path: gfx.Path
    # If width or height is zero, rendering is disabled.
    if width == 0.0 or height == 0.0:
        return path

    let corner_radii = svg_rect_element_calculate_used_corner_radius_values(this, viewport_size)
    let rx = corner_radii.m_width
    let ry = corner_radii.m_height

    # 1. perform an absolute moveto operation to location (x+rx,y);
    gfx.path_move_to(&path, gfx.FloatPoint(m_x = x + rx, m_y = y))

    # 2, perform an absolute horizontal lineto with parameter x+width-rx;
    gfx.path_horizontal_line_to(&path, x + width - rx)

    # 3. if both rx and ry are greater than zero,
    #    perform an absolute elliptical arc operation to coordinate (x+width,y+ry),
    #    where rx and ry are used as the equivalent parameters to the elliptical arc command,
    #    the x-axis-rotation and large-arc-flag are set to zero,
    #    the sweep-flag is set to one;
    let x_axis_rotation: f64 = 0.0
    let large_arc_flag = false
    let sweep_flag = true
    if rx > 0.0 and ry > 0.0:
        gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = x + width, m_y = y + ry), corner_radii, (f32)x_axis_rotation, large_arc_flag, sweep_flag)

    # 4. perform an absolute vertical lineto parameter y+height-ry;
    gfx.path_vertical_line_to(&path, y + height - ry)

    # 5. if both rx and ry are greater than zero,
    #    perform an absolute elliptical arc operation to coordinate (x+width-rx,y+height),
    #    using the same parameters as previously;
    if rx > 0.0 and ry > 0.0:
        gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = x + width - rx, m_y = y + height), corner_radii, (f32)x_axis_rotation, large_arc_flag, sweep_flag)

    # 6. perform an absolute horizontal lineto parameter x+rx;
    gfx.path_horizontal_line_to(&path, x + rx)

    # 7. if both rx and ry are greater than zero,
    #    perform an absolute elliptical arc operation to coordinate (x,y+height-ry),
    #    using the same parameters as previously;
    if rx > 0.0 and ry > 0.0:
        gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = x, m_y = y + height - ry), corner_radii, (f32)x_axis_rotation, large_arc_flag, sweep_flag)

    # 8. perform an absolute vertical lineto parameter y+ry
    gfx.path_vertical_line_to(&path, y + ry)

    # 9. if both rx and ry are greater than zero,
    #    perform an absolute elliptical arc operation with a segment-completing close path operation,
    #    using the same parameters as previously.
    if rx > 0.0 and ry > 0.0:
        gfx.path_elliptical_arc_to(&path, gfx.FloatPoint(m_x = x + rx, m_y = y), corner_radii, (f32)x_axis_rotation, large_arc_flag, sweep_flag)

    gfx.path_close(&path)

    return path

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:115 Web::SVG::SVGRectElement::calculate_used_corner_radius_values.
func svg_rect_element_calculate_used_corner_radius_values(this: SvgRectElement*, viewport_size: gfx.CssPixelSize) -> gfx.FloatSize:
    # 1. Let rx and ry be length values.
    var rx: f32 = 0.0
    var ry: f32 = 0.0

    # 2. If neither ‘rx’ nor ‘ry’ are properly specified, then set both rx and ry to 0. (This will result in square corners.)
    if this.m_radius_x == none and this.m_radius_y == none:
        rx = 0.0
        ry = 0.0
    # 3. Otherwise, if a properly specified value is provided for ‘rx’, but not for ‘ry’, then set both rx and ry to the value of ‘rx’.
    elif this.m_radius_x != none and this.m_radius_y == none:
        let radius_x = this.m_radius_x else trap("VERIFY: m_radius_x")
        rx = svg_number_percentage_resolve_relative_to(&radius_x, viewport_size.m_width.to_float())
        ry = svg_number_percentage_resolve_relative_to(&radius_x, viewport_size.m_width.to_float())
    # 4. Otherwise, if a properly specified value is provided for ‘ry’, but not for ‘rx’, then set both rx and ry to the value of ‘ry’.
    elif this.m_radius_y != none and this.m_radius_x == none:
        let radius_y = this.m_radius_y else trap("VERIFY: m_radius_y")
        rx = svg_number_percentage_resolve_relative_to(&radius_y, viewport_size.m_height.to_float())
        ry = svg_number_percentage_resolve_relative_to(&radius_y, viewport_size.m_height.to_float())
    # 5. Otherwise, both ‘rx’ and ‘ry’ were specified properly. Set rx to the value of ‘rx’ and ry to the value of ‘ry’.
    else:
        let radius_x = this.m_radius_x else trap("VERIFY: m_radius_x")
        let radius_y = this.m_radius_y else trap("VERIFY: m_radius_y")
        rx = svg_number_percentage_resolve_relative_to(&radius_x, viewport_size.m_width.to_float())
        ry = svg_number_percentage_resolve_relative_to(&radius_y, viewport_size.m_height.to_float())

    # 6. If rx is greater than half of ‘width’, then set rx to half of ‘width’.
    let half_width = svg_number_percentage_or_zero_resolved(this.m_width, viewport_size.m_width.to_float()) / 2.0
    if rx > half_width:
        rx = half_width

    # 7. If ry is greater than half of ‘height’, then set ry to half of ‘height’.
    let half_height = svg_number_percentage_or_zero_resolved(this.m_height, viewport_size.m_height.to_float()) / 2.0
    if ry > half_height:
        ry = half_height

    # 8. The effective values of ‘rx’ and ‘ry’ are rx and ry, respectively.
    return gfx.FloatSize(m_width = rx, m_height = ry)

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:157 Web::SVG::SVGRectElement::x.
# https://www.w3.org/TR/SVG11/shapes.html#RectElementXAttribute
pub func svg_rect_element_x(this: SvgRectElement*) -> SvgAnimatedLength*:
    let realm = dom_element_realm(this.dom_element())
    # FIXME: Populate the unit type when it is parsed (0 here is "unknown").
    # FIXME: Create a proper animated value when animations are supported.
    let base_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_x), SvgLengthReadOnly.no)
    let anim_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_x), SvgLengthReadOnly.yes)
    return svg_animated_length_create(realm, base_length, anim_length)

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:167 Web::SVG::SVGRectElement::y.
# https://www.w3.org/TR/SVG11/shapes.html#RectElementYAttribute
pub func svg_rect_element_y(this: SvgRectElement*) -> SvgAnimatedLength*:
    let realm = dom_element_realm(this.dom_element())
    # FIXME: Populate the unit type when it is parsed (0 here is "unknown").
    # FIXME: Create a proper animated value when animations are supported.
    let base_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_y), SvgLengthReadOnly.no)
    let anim_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_y), SvgLengthReadOnly.yes)
    return svg_animated_length_create(realm, base_length, anim_length)

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:177 Web::SVG::SVGRectElement::width.
# https://www.w3.org/TR/SVG11/shapes.html#RectElementWidthAttribute
pub func svg_rect_element_width(this: SvgRectElement*) -> SvgAnimatedLength*:
    let realm = dom_element_realm(this.dom_element())
    # FIXME: Populate the unit type when it is parsed (0 here is "unknown").
    # FIXME: Create a proper animated value when animations are supported.
    let base_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_width), SvgLengthReadOnly.no)
    let anim_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_width), SvgLengthReadOnly.yes)
    return svg_animated_length_create(realm, base_length, anim_length)

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:187 Web::SVG::SVGRectElement::height.
# https://www.w3.org/TR/SVG11/shapes.html#RectElementHeightAttribute
pub func svg_rect_element_height(this: SvgRectElement*) -> SvgAnimatedLength*:
    let realm = dom_element_realm(this.dom_element())
    # FIXME: Populate the unit type when it is parsed (0 here is "unknown").
    # FIXME: Create a proper animated value when animations are supported.
    let base_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_height), SvgLengthReadOnly.no)
    let anim_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_height), SvgLengthReadOnly.yes)
    return svg_animated_length_create(realm, base_length, anim_length)

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:197 Web::SVG::SVGRectElement::rx.
# https://www.w3.org/TR/SVG11/shapes.html#RectElementRXAttribute
pub func svg_rect_element_rx(this: SvgRectElement*) -> SvgAnimatedLength*:
    let realm = dom_element_realm(this.dom_element())
    # FIXME: Populate the unit type when it is parsed (0 here is "unknown").
    # FIXME: Create a proper animated value when animations are supported.
    let base_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_radius_x), SvgLengthReadOnly.no)
    let anim_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_radius_x), SvgLengthReadOnly.yes)
    return svg_animated_length_create(realm, base_length, anim_length)

## Ported from Libraries/LibWeb/SVG/SVGRectElement.cpp:207 Web::SVG::SVGRectElement::ry.
# https://www.w3.org/TR/SVG11/shapes.html#RectElementRYAttribute
pub func svg_rect_element_ry(this: SvgRectElement*) -> SvgAnimatedLength*:
    let realm = dom_element_realm(this.dom_element())
    # FIXME: Populate the unit type when it is parsed (0 here is "unknown").
    # FIXME: Create a proper animated value when animations are supported.
    let base_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_radius_y), SvgLengthReadOnly.no)
    let anim_length = svg_length_create(realm, 0, svg_number_percentage_or_zero_value(this.m_radius_y), SvgLengthReadOnly.yes)
    return svg_animated_length_create(realm, base_length, anim_length)