Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-render

Source of release 0.6.2

0.6.2 / src / raster / geom.lucb

#==============================================================================================
#
#   geom - integer and float sizes and rectangles
#
#   DESCRIPTION:
#       Port of tiny-skia-path size.rs (IntSize, Size), rect.rs (IntRect, Rect,
#       NonZeroRect) and tiny-skia geom.rs (ScreenIntRect, IntSizeExt, IntRectExt). The
#       LengthU32 / NonZeroPositiveF32 / FiniteF32 fields are plain scalars whose invariant
#       the constructors check.
#
#==============================================================================================

# mark: IntSize ===============================================================================

## An integer size: width and height are both > 0.
pub struct IntSize:
    let width_: u32
    let height_: u32

    ## Creates a new `IntSize` from width and height; none when either is zero.
    pub static func from_wh(width: u32, height: u32) -> IntSize?:
        if width == 0 or height == 0:
            return none
        return IntSize(width_ = width, height_ = height)

    ## Returns width.
    pub func width() -> u32:
        return self.width_

    ## Returns height.
    pub func height() -> u32:
        return self.height_

    ## Returns width and height as a tuple.
    pub func dimensions() -> (u32, u32):
        return (self.width_, self.height_)

    ## Scales current size by the specified factor.
    pub func scale_by(factor: f32) -> IntSize?:
        return IntSize.from_wh((u32)math32.round((f32)self.width_ * factor), (u32)math32.round((f32)self.height_ * factor))

    ## Scales current size to the specified size.
    pub func scale_to(to: IntSize) -> IntSize:
        return size_scale(self, to, false)

    ## Scales current size to the specified width.
    pub func scale_to_width(new_width: u32) -> IntSize?:
        let new_height = math32.ceil((f32)new_width * (f32)self.height_ / (f32)self.width_)
        return IntSize.from_wh(new_width, (u32)new_height)

    ## Scales current size to the specified height.
    pub func scale_to_height(new_height: u32) -> IntSize?:
        let new_width = math32.ceil((f32)new_height * (f32)self.width_ / (f32)self.height_)
        return IntSize.from_wh((u32)new_width, new_height)

    ## Converts into `Size`.
    pub func to_size() -> Size:
        return Size.from_wh((f32)self.width_, (f32)self.height_) else trap("unreachable")

    ## Converts into `IntRect` at the provided position.
    pub func to_int_rect(x: i32, y: i32) -> IntRect:
        return IntRect.from_xywh(x, y, self.width_, self.height_) else trap("unreachable")

    ## IntSizeExt::to_screen_int_rect.
    func to_screen_int_rect(x: u32, y: u32) -> ScreenIntRect:
        return ScreenIntRect.from_xywh(x, y, self.width_, self.height_) else trap("unreachable")

func size_scale(s1: IntSize, s2: IntSize, expand: bool) -> IntSize:
    let rw = (u32)math32.ceil((f32)s2.height() * (f32)s1.width() / (f32)s1.height())
    let with_h = rw <= s2.width() if expand else rw >= s2.width()
    if not with_h:
        return IntSize.from_wh(rw, s2.height()) else trap("unreachable")
    let h = (u32)math32.ceil((f32)s2.width() * (f32)s1.height() / (f32)s1.width())
    return IntSize.from_wh(s2.width(), h) else trap("unreachable")

# mark: Size ==================================================================================

## A size: width and height are positive, non-zero and finite.
pub struct Size:
    let width_: f32
    let height_: f32

    ## Creates a new `Size` from width and height.
    pub static func from_wh(width: f32, height: f32) -> Size?:
        if not (math32.is_finite(width) and width > 0.0) or not (math32.is_finite(height) and height > 0.0):
            return none
        return Size(width_ = width, height_ = height)

    ## Returns width.
    pub func width() -> f32:
        return self.width_

    ## Returns height.
    pub func height() -> f32:
        return self.height_

    ## Scales current size to specified size.
    pub func scale_to(to: Size) -> Size:
        return size_scale_f64(self, to, false)

    ## Expands current size to specified size.
    pub func expand_to(to: Size) -> Size:
        return size_scale_f64(self, to, true)

    ## Scales current size by the specified factor.
    pub func scale_by(factor: f32) -> Size?:
        return Size.from_wh(self.width_ * factor, self.height_ * factor)

    ## Scales current size to the specified width.
    pub func scale_to_width(new_width: f32) -> Size?:
        let new_height = new_width * self.height_ / self.width_
        return Size.from_wh(new_width, new_height)

    ## Scales current size to the specified height.
    pub func scale_to_height(new_height: f32) -> Size?:
        let new_width = new_height * self.width_ / self.height_
        return Size.from_wh(new_width, new_height)

    ## Converts into `IntSize`.
    pub func to_int_size() -> IntSize:
        let w = (u32)math32.round(self.width_)
        let h = (u32)math32.round(self.height_)
        return IntSize.from_wh(w if w > 1 else 1, h if h > 1 else 1) else trap("unreachable")

    ## Converts the current size to `Rect` at provided position.
    pub func to_rect(x: f32, y: f32) -> Rect?:
        return Rect.from_xywh(x, y, self.width_, self.height_)

    ## Converts the current size to `NonZeroRect` at provided position.
    pub func to_non_zero_rect(x: f32, y: f32) -> NonZeroRect:
        return NonZeroRect.from_xywh(x, y, self.width_, self.height_) else trap("unreachable")

func size_scale_f64(s1: Size, s2: Size, expand: bool) -> Size:
    let rw = s2.height() * s1.width() / s1.height()
    let with_h = rw <= s2.width() if expand else rw >= s2.width()
    if not with_h:
        return Size.from_wh(rw, s2.height()) else trap("unreachable")
    let h = s2.width() * s1.height() / s1.width()
    return Size.from_wh(s2.width(), h) else trap("unreachable")

# mark: IntRect ===============================================================================

## An integer rectangle: width and height are > 0 and right/bottom do not overflow i32.
pub struct IntRect:
    let x_: i32
    let y_: i32
    let width_: u32
    let height_: u32

    ## Creates a new `IntRect`.
    pub static func from_xywh(x: i32, y: i32, width: u32, height: u32) -> IntRect?:
        if width > (u32)i32_max or height > (u32)i32_max:
            return none
        if x +? (i32)width == none or y +? (i32)height == none:
            return none
        if width == 0 or height == 0:
            return none
        return IntRect(x_ = x, y_ = y, width_ = width, height_ = height)

    ## Creates a new `IntRect` from left, top, right and bottom.
    pub static func from_ltrb(left: i32, top: i32, right: i32, bottom: i32) -> IntRect?:
        let w = right -? left else return none
        let h = bottom -? top else return none
        if w < 0 or h < 0:
            return none
        return IntRect.from_xywh(left, top, (u32)w, (u32)h)

    ## Returns rect's X position.
    pub func x() -> i32:
        return self.x_

    ## Returns rect's Y position.
    pub func y() -> i32:
        return self.y_

    ## Returns rect's width.
    pub func width() -> u32:
        return self.width_

    ## Returns rect's height.
    pub func height() -> u32:
        return self.height_

    ## Returns rect's left edge.
    pub func left() -> i32:
        return self.x_

    ## Returns rect's top edge.
    pub func top() -> i32:
        return self.y_

    ## Returns rect's right edge. No overflow is guaranteed by constructors.
    pub func right() -> i32:
        return self.x_ + (i32)self.width_

    ## Returns rect's bottom edge. No overflow is guaranteed by constructors.
    pub func bottom() -> i32:
        return self.y_ + (i32)self.height_

    ## Returns rect's size.
    pub func size() -> IntSize:
        return IntSize(width_ = self.width_, height_ = self.height_)

    ## Checks that the rect is completely includes `other` Rect.
    pub func contains(other: IntRect) -> bool:
        return self.x_ <= other.x_ and self.y_ <= other.y_ and self.right() >= other.right() and self.bottom() >= other.bottom()

    ## Returns an intersection of two rectangles. Returns none otherwise.
    pub func intersect(other: IntRect) -> IntRect?:
        let left = self.x_ if self.x_ > other.x_ else other.x_
        let top = self.y_ if self.y_ > other.y_ else other.y_
        let right = self.right() if self.right() < other.right() else other.right()
        let bottom = self.bottom() if self.bottom() < other.bottom() else other.bottom()
        let w = right -? left else return none
        let h = bottom -? top else return none
        if w < 0 or h < 0:
            return none
        return IntRect.from_xywh(left, top, (u32)w, (u32)h)

    ## Insets the rectangle.
    pub func inset(dx: i32, dy: i32) -> IntRect?:
        return IntRect.from_ltrb(self.left() + dx, self.top() + dy, self.right() - dx, self.bottom() - dy)

    ## Outsets the rectangle.
    pub func make_outset(dx: i32, dy: i32) -> IntRect?:
        return IntRect.from_ltrb(self.left() -| dx, self.top() -| dy, self.right() +| dx, self.bottom() +| dy)

    ## Translates the rect by the specified offset.
    pub func translate(tx: i32, ty: i32) -> IntRect?:
        return IntRect.from_xywh(self.x_ + tx, self.y_ + ty, self.width_, self.height_)

    ## Translates the rect to the specified position.
    pub func translate_to(x: i32, y: i32) -> IntRect?:
        return IntRect.from_xywh(x, y, self.width_, self.height_)

    ## Converts into `Rect`.
    pub func to_rect() -> Rect:
        # Can't fail, because `IntRect` is always valid.
        return Rect.from_ltrb((f32)self.x_, (f32)self.y_, (f32)self.x_ + (f32)self.width_, (f32)self.y_ + (f32)self.height_) else trap("unreachable")

    ## IntRectExt::to_screen_int_rect.
    func to_screen_int_rect() -> ScreenIntRect?:
        if self.x_ < 0 or self.y_ < 0:
            return none
        return ScreenIntRect.from_xywh((u32)self.x_, (u32)self.y_, self.width_, self.height_)

# mark: Rect ==================================================================================

## A rectangle defined by left, top, right and bottom edges. Can have zero width and/or
## height. But not a negative one. All values are finite.
pub struct Rect:
    let left_: f32
    let top_: f32
    let right_: f32
    let bottom_: f32

    ## Creates new `Rect`.
    pub static func from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> Rect?:
        if not math32.is_finite(left) or not math32.is_finite(top) or not math32.is_finite(right) or not math32.is_finite(bottom):
            return none
        if left <= right and top <= bottom:
            # Width and height must not overflow.
            if checked_f32_sub(right, left) == none or checked_f32_sub(bottom, top) == none:
                return none
            return Rect(left_ = left, top_ = top, right_ = right, bottom_ = bottom)
        return none

    ## Creates new `Rect`.
    pub static func from_xywh(x: f32, y: f32, w: f32, h: f32) -> Rect?:
        return Rect.from_ltrb(x, y, w + x, h + y)

    ## Returns the left edge.
    pub func left() -> f32:
        return self.left_

    ## Returns the top edge.
    pub func top() -> f32:
        return self.top_

    ## Returns the right edge.
    pub func right() -> f32:
        return self.right_

    ## Returns the bottom edge.
    pub func bottom() -> f32:
        return self.bottom_

    ## Returns rect's X position.
    pub func x() -> f32:
        return self.left_

    ## Returns rect's Y position.
    pub func y() -> f32:
        return self.top_

    ## Returns rect's width.
    pub func width() -> f32:
        return self.right_ - self.left_

    ## Returns rect's height.
    pub func height() -> f32:
        return self.bottom_ - self.top_

    ## Checks that the rect has zero width or height.
    pub func is_empty() -> bool:
        return self.left_ == self.right_ or self.top_ == self.bottom_

    ## Converts into an `IntRect` by adding 0.5 and discarding the fractional portion.
    ## Width and height are guarantee to be >= 1.
    pub func round() -> IntRect?:
        let left = saturate_round(self.left_)
        let top = saturate_round(self.top_)
        let right = saturate_round(self.right_)
        let bottom = saturate_round(self.bottom_)
        let w = right -| left
        let h = bottom -| top
        return IntRect.from_xywh(left, top, (u32)(w if w > 1 else 1), (u32)(h if h > 1 else 1))

    ## Converts into an `IntRect` rounding outwards. Width and height are guarantee to be >= 1.
    pub func round_out() -> IntRect?:
        let left = saturate_floor(self.left_)
        let top = saturate_floor(self.top_)
        let right = saturate_ceil(self.right_)
        let bottom = saturate_ceil(self.bottom_)
        let w = right -| left
        let h = bottom -| top
        return IntRect.from_xywh(left, top, (u32)(w if w > 1 else 1), (u32)(h if h > 1 else 1))

    ## Returns an intersection of two rectangles. Returns none otherwise.
    pub func intersect(other: Rect) -> Rect?:
        let left = fmax(self.x(), other.x())
        let top = fmax(self.y(), other.y())
        let right = fmin(self.right(), other.right())
        let bottom = fmin(self.bottom(), other.bottom())
        return Rect.from_ltrb(left, top, right, bottom)

    ## Returns the union of two rectangles; an empty one is ignored.
    pub func join(other: Rect) -> Rect?:
        if other.is_empty():
            return self
        if self.is_empty():
            return other
        let left = fmin(self.x(), other.x())
        let top = fmin(self.y(), other.y())
        let right = fmax(self.right(), other.right())
        let bottom = fmax(self.bottom(), other.bottom())
        return Rect.from_ltrb(left, top, right, bottom)

    ## Creates a Rect from Point array. Returns none if count is zero or if Point array
    ## contains an infinity or NaN.
    pub static func from_points(points: const Point[]) -> Rect?:
        var offset: usize = 0
        var min: f32[4] = ---
        var max: f32[4] = ---
        # Handle trivial-size lists without creating vectors, since they won't actually use
        # the vector min/max code
        if points.length == 0:
            return none
        if points.length == 1:
            return Rect.from_xywh(points[0].x, points[0].y, 0.0, 0.0)
        if points.length == 2:
            let x0 = points[0].x
            let y0 = points[0].y
            let x1 = points[1].x
            let y1 = points[1].y
            # Avoid min/max to preserve a NaN for rejection by from_ltrb
            let l = x0 if x0 < x1 else x1
            let r = x1 if x0 < x1 else x0
            let t = y0 if y0 < y1 else y1
            let b = y1 if y0 < y1 else y0
            return Rect.from_ltrb(l, t, r, b)
        if points.length & 1 != 0:
            let pt = points[0]
            min = [pt.x, pt.y, pt.x, pt.y]
            max = min
            offset = 1
        else:
            let pt0 = points[0]
            let pt1 = points[1]
            min = [pt0.x, pt0.y, pt1.x, pt1.y]
            max = min
            offset = 2

        var accum = f32[4](0.0)
        while offset != points.length:
            let pt0 = points[offset + 0]
            let pt1 = points[offset + 1]
            let xy: f32[4] = [pt0.x, pt0.y, pt1.x, pt1.y]
            accum = accum * xy
            for i: usize in 0..<4:
                min[i] = fmin(min[i], xy[i])
                max[i] = fmax(max[i], xy[i])
            offset += 2

        let zero = accum * f32[4](0.0)
        let all_finite = zero[0] == 0.0 and zero[1] == 0.0 and zero[2] == 0.0 and zero[3] == 0.0
        if all_finite:
            return Rect.from_ltrb(fmin(min[0], min[2]), fmin(min[1], min[3]), fmax(max[0], max[2]), fmax(max[1], max[3]))
        return none

    ## Insets the rectangle by the specified offset.
    pub func inset(dx: f32, dy: f32) -> Rect?:
        return Rect.from_ltrb(self.left() + dx, self.top() + dy, self.right() - dx, self.bottom() - dy)

    ## Outsets the rectangle by the specified offset.
    pub func outset(dx: f32, dy: f32) -> Rect?:
        return self.inset(-dx, -dy)

    ## Transforms the rect using the provided `Transform`. This method is expensive.
    pub func transform(ts: Transform) -> Rect?:
        if ts.is_identity():
            return self
        if ts.has_perspective():
            # SkMatrix::mapRect: the rect as a path, transformed (and cut at w = 0), bounded;
            # the empty rect when nothing is left.
            var path = PathBuilder.from_rect(self)
            if not path.transform(ts):
                return Rect.from_ltrb(0.0, 0.0, 0.0, 0.0)
            let bounds = path.bounds()
            path.release()
            return bounds
        if ts.has_skew():
            # we need to transform all 4 corners
            var pts: Point[4] = [
                Point.from_xy(self.left(), self.top()),
                Point.from_xy(self.right(), self.top()),
                Point.from_xy(self.left(), self.bottom()),
                Point.from_xy(self.right(), self.bottom()),
            ]
            ts.map_points(pts)
            return Rect.from_points(pts)
        # Faster (more common) case
        var pts: Point[2] = [Point.from_xy(self.left(), self.top()), Point.from_xy(self.right(), self.bottom())]
        ts.map_points(pts)
        return Rect.from_points(pts)

    ## Applies a bounding box transform.
    pub func bbox_transform(bbox: NonZeroRect) -> Rect:
        let x = self.x() * bbox.width() + bbox.x()
        let y = self.y() * bbox.height() + bbox.y()
        let w = self.width() * bbox.width()
        let h = self.height() * bbox.height()
        return Rect.from_xywh(x, y, w, h) else trap("unreachable")

    ## Converts into `NonZeroRect`.
    pub func to_non_zero_rect() -> NonZeroRect?:
        return NonZeroRect.from_xywh(self.x(), self.y(), self.width(), self.height())

func checked_f32_sub(a: f32, b: f32) -> f32?:
    let n = (f64)a - (f64)b
    # Not sure if this is perfectly correct.
    if n > -3.4028234663852886e38 and n < 3.4028234663852886e38:
        return (f32)n
    return none

# mark: NonZeroRect ===========================================================================

## A rectangle defined by left, top, right and bottom edges. Width and height are > 0 and
## all values are finite.
pub struct NonZeroRect:
    let left_: f32
    let top_: f32
    let right_: f32
    let bottom_: f32

    ## Creates new `NonZeroRect`.
    pub static func from_ltrb(left: f32, top: f32, right: f32, bottom: f32) -> NonZeroRect?:
        if not math32.is_finite(left) or not math32.is_finite(top) or not math32.is_finite(right) or not math32.is_finite(bottom):
            return none
        if left < right and top < bottom:
            # Width and height must not overflow.
            if checked_f32_sub(right, left) == none or checked_f32_sub(bottom, top) == none:
                return none
            return NonZeroRect(left_ = left, top_ = top, right_ = right, bottom_ = bottom)
        return none

    ## Creates new `NonZeroRect`.
    pub static func from_xywh(x: f32, y: f32, w: f32, h: f32) -> NonZeroRect?:
        return NonZeroRect.from_ltrb(x, y, w + x, h + y)

    ## Returns the left edge.
    pub func left() -> f32:
        return self.left_

    ## Returns the top edge.
    pub func top() -> f32:
        return self.top_

    ## Returns the right edge.
    pub func right() -> f32:
        return self.right_

    ## Returns the bottom edge.
    pub func bottom() -> f32:
        return self.bottom_

    ## Returns rect's X position.
    pub func x() -> f32:
        return self.left_

    ## Returns rect's Y position.
    pub func y() -> f32:
        return self.top_

    ## Returns rect's width.
    pub func width() -> f32:
        return self.right_ - self.left_

    ## Returns rect's height.
    pub func height() -> f32:
        return self.bottom_ - self.top_

    ## Returns rect's size.
    pub func size() -> Size:
        return Size.from_wh(self.width(), self.height()) else trap("unreachable")

    ## Translates the rect to the specified position.
    pub func translate_to(x: f32, y: f32) -> NonZeroRect?:
        return NonZeroRect.from_xywh(x, y, self.width(), self.height())

    ## Transforms the rect using the provided `Transform`. This method is expensive.
    pub func transform(ts: Transform) -> NonZeroRect?:
        if ts.is_identity():
            return self
        let r = self.to_rect().transform(ts) else return none
        return r.to_non_zero_rect()

    ## Applies a bounding box transform.
    pub func bbox_transform(bbox: NonZeroRect) -> NonZeroRect:
        let x = self.x() * bbox.width() + bbox.x()
        let y = self.y() * bbox.height() + bbox.y()
        let w = self.width() * bbox.width()
        let h = self.height() * bbox.height()
        return NonZeroRect.from_xywh(x, y, w, h) else trap("unreachable")

    ## Converts into `Rect`.
    pub func to_rect() -> Rect:
        return Rect.from_xywh(self.x(), self.y(), self.width(), self.height()) else trap("unreachable")

    ## Converts into `IntRect`.
    pub func to_int_rect() -> IntRect:
        let w = (u32)math32.ceil(self.width())
        let h = (u32)math32.ceil(self.height())
        return IntRect.from_xywh((i32)math32.floor(self.x()), (i32)math32.floor(self.y()), w if w > 1 else 1, h if h > 1 else 1) else trap("unreachable")

# mark: ScreenIntRect =========================================================================

## A screen `IntRect`: x and y are >= 0, width and height are > 0, and x/y/width/height and
## right/bottom fit in i32.
pub struct ScreenIntRect:
    let x_: u32
    let y_: u32
    let width_: u32
    let height_: u32

    ## Creates a new `ScreenIntRect`.
    pub static func from_xywh(x: u32, y: u32, width: u32, height: u32) -> ScreenIntRect?:
        let limit = (u32)i32_max
        if x > limit or y > limit or width > limit or height > limit:
            return none
        if x +? width == none or y +? height == none:
            return none
        if width == 0 or height == 0:
            return none
        return ScreenIntRect(x_ = x, y_ = y, width_ = width, height_ = height)

    ## Creates a new `ScreenIntRect` from checked parts.
    static func from_xywh_safe(x: u32, y: u32, width: u32, height: u32) -> ScreenIntRect:
        return ScreenIntRect(x_ = x, y_ = y, width_ = width, height_ = height)

    ## Returns rect's X position.
    pub func x() -> u32:
        return self.x_

    ## Returns rect's Y position.
    pub func y() -> u32:
        return self.y_

    ## Returns rect's width.
    pub func width() -> u32:
        return self.width_

    ## Returns rect's height.
    pub func height() -> u32:
        return self.height_

    ## Returns rect's left edge.
    pub func left() -> u32:
        return self.x_

    ## Returns rect's top edge.
    pub func top() -> u32:
        return self.y_

    ## Returns rect's right edge. No overflow is guaranteed by constructors.
    pub func right() -> u32:
        return self.x_ + self.width_

    ## Returns rect's bottom edge. No overflow is guaranteed by constructors.
    pub func bottom() -> u32:
        return self.y_ + self.height_

    ## Returns rect's size.
    pub func size() -> IntSize:
        return IntSize(width_ = self.width_, height_ = self.height_)

    ## Checks that the rect is completely includes `other` Rect.
    pub func contains(other: ScreenIntRect) -> bool:
        return self.x_ <= other.x_ and self.y_ <= other.y_ and self.right() >= other.right() and self.bottom() >= other.bottom()

    ## Converts into a `IntRect`.
    pub func to_int_rect() -> IntRect:
        # Everything is already checked by constructors.
        return IntRect(x_ = (i32)self.x_, y_ = (i32)self.y_, width_ = self.width_, height_ = self.height_)

    ## Converts into a `Rect`.
    pub func to_rect() -> Rect:
        # Can't fail, because `ScreenIntRect` is always valid. And u32 always fits into f32.
        return Rect.from_ltrb((f32)self.x_, (f32)self.y_, (f32)self.x_ + (f32)self.width_, (f32)self.y_ + (f32)self.height_) else trap("unreachable")