Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.1

0.6.1 / src / gfx / rect_float_2.lucb

#==============================================================================================
#
#   rect_float_2 - Gfx::Rect<float> (FloatRect): edges to conversions
#
#   DESCRIPTION:
#       Port of Libraries/LibGfx/Rect.h (lines 349-599) and Rect.cpp (lines 20-24) for T = float:
#       edges, intersection and union, corners and centering, conversions, and the free function
#       enclosing_int_rect. rect_float_1.lucb has the first half of the class.
#
#==============================================================================================

# mark: Edges ===================================================================================

## Ported from Libraries/LibGfx/Rect.h:349 Gfx::Rect::first_edge_for_orientation for FloatRect.
pub func float_rect_first_edge_for_orientation(this: const FloatRect*, orientation: Orientation) -> f32:
    if orientation == Orientation.vertical:
        return float_rect_top(this)
    return float_rect_left(this)

## Ported from Libraries/LibGfx/Rect.h:356 Gfx::Rect::last_edge_for_orientation for FloatRect.
pub func float_rect_last_edge_for_orientation(this: const FloatRect*, orientation: Orientation) -> f32:
    if orientation == Orientation.vertical:
        return float_rect_bottom(this)
    return float_rect_right(this)

## Ported from Libraries/LibGfx/Rect.h:363 Gfx::Rect::left for FloatRect.
pub func float_rect_left(this: const FloatRect*) -> f32:
    return float_rect_x(this)

## Ported from Libraries/LibGfx/Rect.h:364 Gfx::Rect::right for FloatRect.
pub func float_rect_right(this: const FloatRect*) -> f32:
    return float_rect_x(this) + float_rect_width(this)

## Ported from Libraries/LibGfx/Rect.h:365 Gfx::Rect::top for FloatRect.
pub func float_rect_top(this: const FloatRect*) -> f32:
    return float_rect_y(this)

## Ported from Libraries/LibGfx/Rect.h:366 Gfx::Rect::bottom for FloatRect.
pub func float_rect_bottom(this: const FloatRect*) -> f32:
    return float_rect_y(this) + float_rect_height(this)

## Ported from Libraries/LibGfx/Rect.h:368 Gfx::Rect::set_left for FloatRect.
pub func float_rect_set_left(this: FloatRect*, left: f32):
    float_rect_set_x(this, left)

## Ported from Libraries/LibGfx/Rect.h:369 Gfx::Rect::set_top for FloatRect.
pub func float_rect_set_top(this: FloatRect*, top: f32):
    float_rect_set_y(this, top)

## Ported from Libraries/LibGfx/Rect.h:370 Gfx::Rect::set_right for FloatRect.
pub func float_rect_set_right(this: FloatRect*, right: f32):
    float_rect_set_width(this, right - float_rect_x(this))

## Ported from Libraries/LibGfx/Rect.h:371 Gfx::Rect::set_bottom for FloatRect.
pub func float_rect_set_bottom(this: FloatRect*, bottom: f32):
    float_rect_set_height(this, bottom - float_rect_y(this))

## Ported from Libraries/LibGfx/Rect.h:373 Gfx::Rect::set_right_without_resize for FloatRect.
pub func float_rect_set_right_without_resize(this: FloatRect*, new_right: f32):
    let delta = new_right - float_rect_right(this)
    float_rect_translate_by_f32_f32(this, delta, 0.0)

## Ported from Libraries/LibGfx/Rect.h:379 Gfx::Rect::set_bottom_without_resize for FloatRect.
pub func float_rect_set_bottom_without_resize(this: FloatRect*, new_bottom: f32):
    let delta = new_bottom - float_rect_bottom(this)
    float_rect_translate_by_f32_f32(this, 0.0, delta)

# mark: Intersection ============================================================================

## Ported from Libraries/LibGfx/Rect.h:385 Gfx::Rect::intersects_vertically for FloatRect.
pub func float_rect_intersects_vertically(this: const FloatRect*, other: FloatRect) -> bool:
    return float_rect_top(this) < float_rect_bottom(&other) and float_rect_top(&other) < float_rect_bottom(this)

## Ported from Libraries/LibGfx/Rect.h:390 Gfx::Rect::intersects_horizontally for FloatRect.
pub func float_rect_intersects_horizontally(this: const FloatRect*, other: FloatRect) -> bool:
    return float_rect_left(this) < float_rect_right(&other) and float_rect_left(&other) < float_rect_right(this)

## Ported from Libraries/LibGfx/Rect.h:395 Gfx::Rect::intersects for FloatRect.
pub func float_rect_intersects_float_rect(this: const FloatRect*, other: FloatRect) -> bool:
    return (float_rect_left(this) < float_rect_right(&other) and float_rect_left(&other) < float_rect_right(this) and float_rect_top(this) < float_rect_bottom(&other) and float_rect_top(&other) < float_rect_bottom(this))

## Ported from Libraries/LibGfx/Rect.h:403 Gfx::Rect::edge_adjacent_intersects for FloatRect.
pub func float_rect_edge_adjacent_intersects(this: const FloatRect*, other: FloatRect) -> bool:
    return (ak.max_f32(float_rect_left(this), float_rect_left(&other)) <= ak.min_f32(float_rect_right(this), float_rect_right(&other)) and ak.max_f32(float_rect_top(this), float_rect_top(&other)) <= ak.min_f32(float_rect_bottom(this), float_rect_bottom(&other)))

## Ported from Libraries/LibGfx/Rect.h:410 Gfx::Rect::intersects<Container> for FloatRect.
pub func float_rect_intersects_t_container(this: const FloatRect*, others: const FloatRect[]) -> bool:
    for other in others:
        if float_rect_intersects_float_rect(this, other):
            return true
    return false

## Ported from Libraries/LibGfx/Rect.h:420 Gfx::Rect::operator== for FloatRect (U = int).
pub func float_rect_eq(this: const FloatRect*, other: FloatRect) -> bool:
    return float_point_eq(&this.m_location, other.m_location) and float_size_eq(&this.m_size, other.m_size)

## Ported from Libraries/LibGfx/Rect.h:425 Gfx::Rect::operator* for FloatRect.
pub func float_rect_mul(this: const FloatRect*, factor: f32) -> FloatRect:
    return FloatRect(float_point_mul(&this.m_location, factor), float_size_mul(&this.m_size, factor))

## Ported from Libraries/LibGfx/Rect.h:427 Gfx::Rect::operator*= for FloatRect.
pub func float_rect_mul_assign(this: FloatRect*, factor: f32) -> FloatRect*:
    _ = float_point_mul_assign(&this.m_location, factor)
    _ = float_size_mul_assign(&this.m_size, factor)
    return this

## Ported from Libraries/LibGfx/Rect.h:434 Gfx::Rect::intersect for FloatRect.
pub func float_rect_intersect(this: FloatRect*, other: FloatRect):
    let l = ak.max_f32(float_rect_left(this), float_rect_left(&other))
    let r = ak.min_f32(float_rect_right(this), float_rect_right(&other))
    let t = ak.max_f32(float_rect_top(this), float_rect_top(&other))
    let b = ak.min_f32(float_rect_bottom(this), float_rect_bottom(&other))

    if l > r or t > b:
        this.m_location = FloatPoint(0.0, 0.0)
        this.m_size = FloatSize(0.0, 0.0)
        return

    float_rect_set_x(this, l)
    float_rect_set_y(this, t)
    float_rect_set_right(this, r)
    float_rect_set_bottom(this, b)

## Ported from Libraries/LibGfx/Rect.h:453 Gfx::Rect::from_two_points for FloatRect.
pub func float_rect_from_two_points(a: FloatPoint, b: FloatPoint) -> FloatRect:
    return FloatRect(
        FloatPoint(ak.min_f32(float_point_x(&a), float_point_x(&b)), ak.min_f32(float_point_y(&a), float_point_y(&b))),
        FloatSize(ak.abs_f32(float_point_x(&a) - float_point_x(&b)), ak.abs_f32(float_point_y(&a) - float_point_y(&b))),
    )

## Ported from Libraries/LibGfx/Rect.h:458 Gfx::Rect::intersection for FloatRect.
pub func float_rect_intersection(a: FloatRect, b: FloatRect) -> FloatRect:
    var r = a
    float_rect_intersect(&r, b)
    return r

## Ported from Libraries/LibGfx/Rect.h:465 Gfx::Rect::intersected for FloatRect.
pub func float_rect_intersected(this: const FloatRect*, other: FloatRect) -> FloatRect:
    return float_rect_intersection(*this, other)

# mark: Union ===================================================================================

## Ported from Libraries/LibGfx/Rect.h:470 Gfx::Rect::unite for FloatRect.
pub func float_rect_unite(this: FloatRect*, other: FloatRect):
    if float_rect_is_empty(this):
        *this = other
        return
    if float_rect_is_empty(&other):
        return
    float_rect_unite_horizontally(this, other)
    float_rect_unite_vertically(this, other)

## Ported from Libraries/LibGfx/Rect.h:482 Gfx::Rect::unite_horizontally for FloatRect.
pub func float_rect_unite_horizontally(this: FloatRect*, other: FloatRect):
    let new_left = ak.min_f32(float_rect_left(this), float_rect_left(&other))
    let new_right = ak.max_f32(float_rect_right(this), float_rect_right(&other))
    float_rect_set_left(this, new_left)
    float_rect_set_right(this, new_right)

## Ported from Libraries/LibGfx/Rect.h:490 Gfx::Rect::unite_vertically for FloatRect.
pub func float_rect_unite_vertically(this: FloatRect*, other: FloatRect):
    let new_top = ak.min_f32(float_rect_top(this), float_rect_top(&other))
    let new_bottom = ak.max_f32(float_rect_bottom(this), float_rect_bottom(&other))
    float_rect_set_top(this, new_top)
    float_rect_set_bottom(this, new_bottom)

## Ported from Libraries/LibGfx/Rect.h:498 Gfx::Rect::united for FloatRect.
pub func float_rect_united(this: const FloatRect*, other: FloatRect) -> FloatRect:
    var rect = *this
    float_rect_unite(&rect, other)
    return rect

# mark: Corners and centering ===================================================================

## Ported from Libraries/LibGfx/Rect.h:505 Gfx::Rect::top_left for FloatRect.
pub func float_rect_top_left(this: const FloatRect*) -> FloatPoint:
    return FloatPoint(float_rect_left(this), float_rect_top(this))

## Ported from Libraries/LibGfx/Rect.h:506 Gfx::Rect::top_right for FloatRect.
pub func float_rect_top_right(this: const FloatRect*) -> FloatPoint:
    return FloatPoint(float_rect_right(this), float_rect_top(this))

## Ported from Libraries/LibGfx/Rect.h:507 Gfx::Rect::bottom_left for FloatRect.
pub func float_rect_bottom_left(this: const FloatRect*) -> FloatPoint:
    return FloatPoint(float_rect_left(this), float_rect_bottom(this))

## Ported from Libraries/LibGfx/Rect.h:508 Gfx::Rect::bottom_right for FloatRect.
pub func float_rect_bottom_right(this: const FloatRect*) -> FloatPoint:
    return FloatPoint(float_rect_right(this), float_rect_bottom(this))

## Ported from Libraries/LibGfx/Rect.h:510 Gfx::Rect::centered_within for FloatRect.
pub func float_rect_centered_within(this: const FloatRect*, other: FloatRect) -> FloatRect:
    var rect = *this
    float_rect_center_horizontally_within(&rect, other)
    float_rect_center_vertically_within(&rect, other)
    return rect

## Ported from Libraries/LibGfx/Rect.h:518 Gfx::Rect::center_horizontally_within for FloatRect.
pub func float_rect_center_horizontally_within(this: FloatRect*, other: FloatRect):
    let center = float_rect_center(&other)
    float_rect_set_x(this, float_point_x(&center) - float_rect_width(this) / 2.0)

## Ported from Libraries/LibGfx/Rect.h:523 Gfx::Rect::center_vertically_within for FloatRect.
pub func float_rect_center_vertically_within(this: FloatRect*, other: FloatRect):
    let center = float_rect_center(&other)
    float_rect_set_y(this, float_point_y(&center) - float_rect_height(this) / 2.0)

# mark: Conversions =============================================================================

## Ported from Libraries/LibGfx/Rect.h:530 Gfx::Rect::to_type<int> for FloatRect (and the
## converting constructors Rect<int>(FloatRect) and Rect<int>(FloatPoint, FloatSize), Rect.h:44-51).
pub func float_rect_to_type_i32(this: const FloatRect*) -> DevicePixelRect:
    return DevicePixelRect(float_point_to_type_i32(&this.m_location), float_size_to_type_i32(&this.m_size))

## Ported from Libraries/LibGfx/Rect.h:530 Gfx::Rect::to_type<CSSPixels> for FloatRect (and the
## converting constructors Rect<CSSPixels>(FloatRect) and Rect<CSSPixels>(FloatPoint, FloatSize), Rect.h:44-51).
pub func float_rect_to_type_css_pixels(this: const FloatRect*) -> CssPixelRect:
    return CssPixelRect(float_point_to_type_css_pixels(&this.m_location), float_size_to_type_css_pixels(&this.m_size))

## Ported from Libraries/LibGfx/Rect.h:571 Gfx::Rect::to_rounded<int> for FloatRect.
pub func float_rect_to_rounded_i32(this: const FloatRect*) -> DevicePixelRect:
    return DevicePixelRect(
        IntPoint(ak.rounding_round_to_f32_i32(float_rect_x(this)), ak.rounding_round_to_f32_i32(float_rect_y(this))),
        IntSize(ak.rounding_round_to_f32_i32(float_rect_width(this)), ak.rounding_round_to_f32_i32(float_rect_height(this))),
    )

## Ported from Libraries/LibGfx/Rect.cpp:21 Gfx::Rect::to_byte_string for FloatRect.
pub func float_rect_to_byte_string(this: const FloatRect*) -> ak.ByteString:
    let arguments = [
        ak.arg_f32(float_rect_x(this)),
        ak.arg_f32(float_rect_y(this)),
        ak.arg_f32(float_rect_width(this)),
        ak.arg_f32(float_rect_height(this)),
    ]
    var params = ak.type_erased_format_params_create(arguments)
    return ak.byte_string_vformatted(ak.sv("[{},{} {}x{}]"), &params)

# mark: enclosing_int_rect ======================================================================

## Ported from Libraries/LibGfx/Rect.h:592 Gfx::enclosing_int_rect.
pub func enclosing_int_rect(float_rect: FloatRect) -> DevicePixelRect:
    let x1 = (i32)math32.floor(float_rect_x(&float_rect))
    let y1 = (i32)math32.floor(float_rect_y(&float_rect))
    let x2 = (i32)math32.ceil(float_rect_right(&float_rect))
    let y2 = (i32)math32.ceil(float_rect_bottom(&float_rect))
    return device_pixel_rect_from_two_points(IntPoint(x1, y1), IntPoint(x2, y2))