Skip to main content

skia_safe/core/
rect.rs

1use std::{
2    cmp::{max, min},
3    mem,
4};
5
6use crate::{
7    Contains, IPoint, ISize, IVector, PathDirection, Point, Size, Vector, interop,
8    prelude::*,
9    private::{
10        is_finite,
11        safe32::{sk32, sk64},
12    },
13};
14use skia_bindings::{self as sb, SkIRect, SkRect};
15
16#[repr(C)]
17#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
18pub struct IRect {
19    /// The x coordinate of the rectangle's left edge.
20    pub left: i32,
21    /// The y coordinate of the rectangle's top edge.
22    pub top: i32,
23    /// The x coordinate of the rectangle's right edge.
24    pub right: i32,
25    /// The y coordinate of the rectangle's bottom edge.
26    pub bottom: i32,
27}
28
29native_transmutable!(SkIRect, IRect);
30
31impl AsRef<IRect> for IRect {
32    fn as_ref(&self) -> &IRect {
33        self
34    }
35}
36
37impl IRect {
38    pub const fn new(left: i32, top: i32, right: i32, bottom: i32) -> Self {
39        Self {
40            left,
41            top,
42            right,
43            bottom,
44        }
45    }
46
47    #[must_use]
48    pub const fn new_empty() -> Self {
49        Self::new(0, 0, 0, 0)
50    }
51
52    #[must_use]
53    pub fn from_wh(w: i32, h: i32) -> Self {
54        Self::from_size((w, h))
55    }
56
57    #[must_use]
58    pub fn from_size(size: impl Into<ISize>) -> Self {
59        let size = size.into();
60        Self::new(0, 0, size.width, size.height)
61    }
62
63    #[must_use]
64    pub fn from_pt_size(pt: impl Into<IPoint>, size: impl Into<ISize>) -> Self {
65        let pt = pt.into();
66        let size = size.into();
67        Self::from_xywh(pt.x, pt.y, size.width, size.height)
68    }
69
70    #[must_use]
71    pub const fn from_ltrb(l: i32, t: i32, r: i32, b: i32) -> Self {
72        Self::new(l, t, r, b)
73    }
74
75    #[must_use]
76    pub fn from_xywh(x: i32, y: i32, w: i32, h: i32) -> Self {
77        IRect {
78            left: x,
79            top: y,
80            right: sk32::sat_add(x, w),
81            bottom: sk32::sat_add(y, h),
82        }
83    }
84
85    pub const fn left(&self) -> i32 {
86        self.left
87    }
88
89    pub const fn top(&self) -> i32 {
90        self.top
91    }
92
93    pub const fn right(&self) -> i32 {
94        self.right
95    }
96
97    pub const fn bottom(&self) -> i32 {
98        self.bottom
99    }
100
101    pub const fn x(&self) -> i32 {
102        self.left
103    }
104
105    pub const fn y(&self) -> i32 {
106        self.top
107    }
108
109    pub const fn width(&self) -> i32 {
110        sk32::can_overflow_sub(self.right, self.left)
111    }
112
113    pub const fn height(&self) -> i32 {
114        sk32::can_overflow_sub(self.bottom, self.top)
115    }
116
117    pub const fn size(&self) -> ISize {
118        ISize::new(self.width(), self.height())
119    }
120
121    pub const fn width_64(&self) -> i64 {
122        self.right as i64 - self.left as i64
123    }
124
125    pub const fn height_64(&self) -> i64 {
126        self.bottom as i64 - self.top as i64
127    }
128
129    pub fn is_empty_64(&self) -> bool {
130        self.right <= self.left || self.bottom <= self.top
131    }
132
133    pub fn is_empty(&self) -> bool {
134        unsafe { sb::C_SkIRect_isEmpty(self.native()) }
135    }
136
137    pub fn set_empty(&mut self) {
138        *self = Self::new_empty()
139    }
140
141    pub fn set_ltrb(&mut self, left: i32, top: i32, right: i32, bottom: i32) {
142        *self = Self::new(left, top, right, bottom);
143    }
144
145    pub fn set_xywh(&mut self, x: i32, y: i32, w: i32, h: i32) {
146        *self = Self::from_xywh(x, y, w, h);
147    }
148
149    pub fn set_wh(&mut self, width: i32, height: i32) {
150        self.left = 0;
151        self.top = 0;
152        self.right = width;
153        self.bottom = height;
154    }
155
156    pub fn set_size(&mut self, size: impl Into<ISize>) {
157        let size = size.into();
158        self.left = 0;
159        self.top = 0;
160        self.right = size.width;
161        self.bottom = size.height;
162    }
163
164    #[must_use]
165    pub fn with_offset(&self, delta: impl Into<IVector>) -> Self {
166        let mut copied = *self;
167        copied.offset(delta);
168        copied
169    }
170
171    #[must_use]
172    pub fn with_inset(&self, delta: impl Into<IVector>) -> Self {
173        self.with_outset(-delta.into())
174    }
175
176    #[must_use]
177    pub fn with_outset(&self, delta: impl Into<IVector>) -> Self {
178        let delta = delta.into();
179        let (dx, dy) = (delta.x, delta.y);
180        IRect::new(
181            sk32::sat_sub(self.left, dx),
182            sk32::sat_sub(self.top, dy),
183            sk32::sat_add(self.right, dx),
184            sk32::sat_add(self.bottom, dy),
185        )
186    }
187
188    pub fn offset(&mut self, delta: impl Into<IPoint>) {
189        let delta = delta.into();
190        let (dx, dy) = (delta.x, delta.y);
191
192        self.left = sk32::sat_add(self.left, dx);
193        self.top = sk32::sat_add(self.top, dy);
194        self.right = sk32::sat_add(self.right, dx);
195        self.bottom = sk32::sat_add(self.bottom, dy);
196    }
197
198    pub fn offset_to(&mut self, new_p: impl Into<IPoint>) {
199        *self = self.with_offset_to(new_p)
200    }
201
202    #[must_use]
203    pub fn with_offset_to(&self, new_p: impl Into<IPoint>) -> Self {
204        let new_p = new_p.into();
205        let (new_x, new_y) = (new_p.x, new_p.y);
206
207        IRect::new(
208            sk64::pin_to_s32(i64::from(self.right) + i64::from(new_x) - i64::from(self.left)),
209            sk64::pin_to_s32(i64::from(self.bottom) + i64::from(new_y) - i64::from(self.top)),
210            new_x,
211            new_y,
212        )
213    }
214
215    pub fn inset(&mut self, delta: impl Into<IVector>) {
216        *self = self.with_inset(delta)
217    }
218
219    pub fn outset(&mut self, delta: impl Into<IVector>) {
220        *self = self.with_outset(delta)
221    }
222
223    #[must_use]
224    pub fn with_adjustment(&self, d_l: i32, d_t: i32, d_r: i32, d_b: i32) -> Self {
225        IRect::new(
226            sk32::sat_add(self.left, d_l),
227            sk32::sat_add(self.top, d_t),
228            sk32::sat_add(self.right, d_r),
229            sk32::sat_add(self.bottom, d_b),
230        )
231    }
232
233    pub fn adjust(&mut self, d_l: i32, d_t: i32, d_r: i32, d_b: i32) {
234        *self = self.with_adjustment(d_l, d_t, d_r, d_b)
235    }
236
237    // contains() is implemented through a trait below.
238
239    pub fn contains_no_empty_check(&self, r: &Self) -> bool {
240        debug_assert!(self.left < self.right && self.top < self.bottom);
241        debug_assert!(r.left < r.right && r.top < r.bottom);
242
243        self.left <= r.left && self.top <= r.top && self.right >= r.right && self.bottom >= r.bottom
244    }
245
246    #[must_use]
247    pub fn intersect(a: &Self, b: &Self) -> Option<Self> {
248        let mut r = Self::default();
249        unsafe { r.native_mut().intersect(a.native(), b.native()) }.then_some(r)
250    }
251
252    pub fn intersects(a: &Self, b: &Self) -> bool {
253        Self::intersect(a, b).is_some()
254    }
255
256    pub fn intersect_no_empty_check_(a: &Self, b: &Self) -> Option<Self> {
257        debug_assert!(!a.is_empty_64() && !b.is_empty_64());
258        let r = IRect::new(
259            max(a.left, b.left),
260            max(a.top, b.top),
261            min(a.right, b.right),
262            min(a.bottom, b.bottom),
263        );
264        (!r.is_empty()).then_some(r)
265    }
266
267    /// Returns the union of `a` and `b`.
268    ///
269    /// Has no effect if `b` is empty. Otherwise, if `a` is empty, returns `b`.
270    ///
271    /// The C++ mutating variant `join(const SkIRect&)` is not wrapped; this static version
272    /// returns a new rectangle instead.
273    ///
274    /// - `a` rectangle to join
275    /// - `b` expansion rectangle
276    ///
277    /// Example (C++): <https://fiddle.skia.org/c/@IRect_join_2>
278    pub fn join(a: &Self, b: &Self) -> Self {
279        let mut copied = *a;
280        unsafe { copied.native_mut().join(b.native()) }
281        copied
282    }
283
284    pub fn sort(&mut self) {
285        *self = self.sorted()
286    }
287
288    #[must_use]
289    pub fn sorted(&self) -> Self {
290        Self::new(
291            min(self.left, self.right),
292            min(self.top, self.bottom),
293            max(self.left, self.right),
294            max(self.top, self.bottom),
295        )
296    }
297
298    pub fn as_i32s(&self) -> &[i32] {
299        unsafe { safer::from_raw_parts(&self.left, 4) }
300    }
301
302    #[deprecated(since = "0.27.0", note = "removed without replacement")]
303    #[must_use]
304    pub fn empty() -> &'static Self {
305        &EMPTY_IRECT
306    }
307}
308
309pub const EMPTY_IRECT: IRect = IRect {
310    left: 0,
311    top: 0,
312    right: 0,
313    bottom: 0,
314};
315
316impl Contains<IPoint> for IRect {
317    fn contains(&self, other: IPoint) -> bool {
318        let (x, y) = (other.x, other.y);
319        x >= self.left && x < self.right && y >= self.top && y < self.bottom
320    }
321}
322
323impl Contains<&IRect> for IRect {
324    fn contains(&self, r: &IRect) -> bool {
325        !r.is_empty()
326            && !self.is_empty()
327            && self.left <= r.left
328            && self.top <= r.top
329            && self.right >= r.right
330            && self.bottom >= r.bottom
331    }
332}
333
334impl Contains<&Rect> for IRect {
335    fn contains(&self, other: &Rect) -> bool {
336        unsafe { sb::C_SkIRect_contains(self.native(), other.native()) }
337    }
338}
339
340impl Contains<IRect> for IRect {
341    fn contains(&self, other: IRect) -> bool {
342        self.contains(&other)
343    }
344}
345
346impl Contains<Rect> for IRect {
347    fn contains(&self, other: Rect) -> bool {
348        self.contains(&other)
349    }
350}
351
352#[repr(C)]
353#[derive(Copy, Clone, PartialEq, Default, Debug)]
354pub struct Rect {
355    /// The x coordinate of the rectangle's left edge.
356    pub left: f32,
357    /// The y coordinate of the rectangle's top edge.
358    pub top: f32,
359    /// The x coordinate of the rectangle's right edge.
360    pub right: f32,
361    /// The y coordinate of the rectangle's bottom edge.
362    pub bottom: f32,
363}
364
365native_transmutable!(SkRect, Rect);
366
367impl AsRef<Rect> for Rect {
368    fn as_ref(&self) -> &Rect {
369        self
370    }
371}
372
373impl Rect {
374    #[must_use]
375    pub fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
376        Self {
377            left,
378            top,
379            right,
380            bottom,
381        }
382    }
383
384    pub fn new_empty() -> Self {
385        Self::new(0.0, 0.0, 0.0, 0.0)
386    }
387
388    #[must_use]
389    pub fn from_wh(w: f32, h: f32) -> Self {
390        Self::new(0.0, 0.0, w, h)
391    }
392
393    #[must_use]
394    pub fn from_iwh(w: i32, h: i32) -> Self {
395        Self::from_wh(w as f32, h as f32)
396    }
397
398    #[must_use]
399    pub fn from_size(size: impl Into<Size>) -> Self {
400        (Point::default(), size.into()).into()
401    }
402
403    #[must_use]
404    pub fn from_ltrb(l: f32, t: f32, r: f32, b: f32) -> Self {
405        Self::new(l, t, r, b)
406    }
407
408    #[must_use]
409    pub fn from_xywh(x: f32, y: f32, w: f32, h: f32) -> Self {
410        Self::new(x, y, x + w, y + h)
411    }
412
413    #[must_use]
414    pub fn from_point_and_size(p: impl Into<Point>, sz: impl Into<Size>) -> Self {
415        (p.into(), sz.into()).into()
416    }
417
418    #[must_use]
419    pub fn from_isize(isize: impl Into<ISize>) -> Self {
420        let isize = isize.into();
421        Self::from_iwh(isize.width, isize.height)
422    }
423
424    #[must_use]
425    pub fn from_irect(irect: impl AsRef<IRect>) -> Self {
426        let irect = irect.as_ref();
427        Self::new(
428            irect.left as f32,
429            irect.top as f32,
430            irect.right as f32,
431            irect.bottom as f32,
432        )
433    }
434
435    pub fn is_empty(&self) -> bool {
436        // We write it as the NOT of a non-empty rect, so we will return true if any values
437        // are NaN.
438        !(self.left < self.right && self.top < self.bottom)
439    }
440
441    pub fn is_sorted(&self) -> bool {
442        self.left <= self.right && self.top <= self.bottom
443    }
444
445    pub fn is_finite(&self) -> bool {
446        is_finite(&[self.left, self.top, self.right, self.bottom])
447    }
448
449    pub const fn x(&self) -> f32 {
450        self.left
451    }
452
453    pub const fn y(&self) -> f32 {
454        self.top
455    }
456
457    pub const fn left(&self) -> f32 {
458        self.left
459    }
460
461    pub const fn top(&self) -> f32 {
462        self.top
463    }
464
465    pub const fn right(&self) -> f32 {
466        self.right
467    }
468
469    pub const fn bottom(&self) -> f32 {
470        self.bottom
471    }
472
473    pub fn size(&self) -> Size {
474        (self.width(), self.height()).into()
475    }
476
477    pub fn width(&self) -> f32 {
478        self.native().fRight - self.native().fLeft
479    }
480
481    pub fn height(&self) -> f32 {
482        self.native().fBottom - self.native().fTop
483    }
484
485    pub fn center_x(&self) -> f32 {
486        // don't use (fLeft + fBottom) * 0.5 as that might overflow before the 0.5
487        self.left * 0.5 + self.right * 0.5
488    }
489
490    pub fn center_y(&self) -> f32 {
491        // don't use (fTop + fBottom) * 0.5 as that might overflow before the 0.5
492        self.top * 0.5 + self.bottom * 0.5
493    }
494
495    pub fn center(&self) -> Point {
496        Point::from((self.center_x(), self.center_y()))
497    }
498
499    pub fn tl(&self) -> Point {
500        Point::from((self.left, self.top))
501    }
502
503    pub fn tr(&self) -> Point {
504        Point::from((self.right, self.top))
505    }
506
507    pub fn bl(&self) -> Point {
508        Point::from((self.left, self.bottom))
509    }
510
511    pub fn br(&self) -> Point {
512        Point::from((self.right, self.bottom))
513    }
514
515    pub fn to_quad(&self, dir: impl Into<Option<PathDirection>>) -> [Point; 4] {
516        let mut pts = [Point::default(); 4];
517        self.copy_to_quad(&mut pts, dir);
518        pts
519    }
520
521    pub fn copy_to_quad(&self, pts: &mut [Point], dir: impl Into<Option<PathDirection>>) {
522        debug_assert!(pts.len() >= 4);
523        pts[0] = self.tl();
524        pts[2] = self.br();
525        match dir.into().unwrap_or(PathDirection::CW) {
526            PathDirection::CW => {
527                pts[1] = self.tr();
528                pts[3] = self.bl();
529            }
530            PathDirection::CCW => {
531                pts[1] = self.bl();
532                pts[3] = self.tr();
533            }
534        }
535    }
536
537    pub fn set_empty(&mut self) {
538        *self = Self::new_empty()
539    }
540
541    // TODO: deprecate and rename to set() as soon the other set() variant is removed.
542    pub fn set_irect(&mut self, irect: impl AsRef<IRect>) {
543        *self = Self::from_irect(irect)
544    }
545
546    pub fn set_ltrb(&mut self, left: f32, top: f32, right: f32, bottom: f32) {
547        *self = Self::new(left, top, right, bottom)
548    }
549
550    pub fn bounds(pts: &[Point]) -> Option<Rect> {
551        let mut bounds = Rect::default();
552        unsafe { sb::C_SkRect_Bounds(pts.native().as_ptr(), pts.len(), bounds.native_mut()) }
553            .then_some(bounds)
554    }
555
556    pub fn bounds_or_empty(pts: &[Point]) -> Rect {
557        Self::bounds(pts).unwrap_or_else(Self::new_empty)
558    }
559
560    pub fn set_bounds(&mut self, points: &[Point]) {
561        self.set_bounds_check(points);
562    }
563
564    /// Sets to the bounds of the span of points, and returns true (if all point values were
565    /// finite).
566    ///
567    /// If the span is empty, sets the rect to empty and returns true. If any point contains an
568    /// infinity or NaN, sets the rect to empty and returns false.
569    ///
570    /// - `points` point span
571    ///
572    /// Example (C++): <https://fiddle.skia.org/c/@Rect_setBoundsCheck>
573    pub fn set_bounds_check(&mut self, points: &[Point]) -> bool {
574        unsafe {
575            sb::C_SkRect_setBoundsCheck(self.native_mut(), points.native().as_ptr(), points.len())
576        }
577    }
578
579    /// Sets to the bounds of the span of points.
580    ///
581    /// If the span is empty, sets the rect to empty. If any point contains an infinity or NaN,
582    /// sets the rect to NaN.
583    ///
584    /// - `points` point span
585    ///
586    /// Example (C++): <https://fiddle.skia.org/c/@Rect_setBoundsNoCheck>
587    pub fn set_bounds_no_check(&mut self, points: &[Point]) {
588        unsafe {
589            sb::C_SkRect_setBoundsNoCheck(self.native_mut(), points.native().as_ptr(), points.len())
590        }
591    }
592
593    pub fn set_bounds2(&mut self, p0: impl Into<Point>, p1: impl Into<Point>) {
594        let (p0, p1) = (p0.into(), p1.into());
595        self.left = p0.x.min(p1.x);
596        self.right = p0.x.max(p1.x);
597        self.top = p0.y.min(p1.y);
598        self.bottom = p0.y.max(p1.y);
599    }
600
601    pub fn from_bounds(points: &[Point]) -> Option<Self> {
602        let mut r = Self::default();
603        r.set_bounds_check(points).then_some(r)
604    }
605
606    pub fn set_xywh(&mut self, x: f32, y: f32, width: f32, height: f32) {
607        *self = Self::from_xywh(x, y, width, height)
608    }
609
610    pub fn set_wh(&mut self, w: f32, h: f32) {
611        *self = Self::from_wh(w, h)
612    }
613
614    pub fn set_iwh(&mut self, width: i32, height: i32) {
615        *self = Self::from_iwh(width, height)
616    }
617
618    #[must_use]
619    pub fn with_offset(&self, d: impl Into<Vector>) -> Self {
620        let d = d.into();
621        Self::new(
622            self.left + d.x,
623            self.top + d.y,
624            self.right + d.x,
625            self.bottom + d.y,
626        )
627    }
628
629    #[must_use]
630    pub fn with_inset(&self, d: impl Into<Vector>) -> Self {
631        let d = d.into();
632        Self::new(
633            self.left + d.x,
634            self.top + d.y,
635            self.right - d.x,
636            self.bottom - d.y,
637        )
638    }
639
640    #[must_use]
641    pub fn with_outset(&self, d: impl Into<Vector>) -> Self {
642        let d = d.into();
643        Self::new(
644            self.left - d.x,
645            self.top - d.y,
646            self.right + d.x,
647            self.bottom + d.y,
648        )
649    }
650
651    pub fn offset(&mut self, d: impl Into<Vector>) {
652        *self = self.with_offset(d)
653    }
654
655    pub fn offset_to(&mut self, new_p: impl Into<Point>) {
656        *self = self.with_offset_to(new_p)
657    }
658
659    #[must_use]
660    pub fn with_offset_to(&self, new_p: impl Into<Point>) -> Self {
661        let new_p = new_p.into();
662        Self::new(new_p.x, new_p.y, new_p.x - self.left, new_p.y - self.top)
663    }
664
665    pub fn inset(&mut self, d: impl Into<Vector>) {
666        *self = self.with_inset(d)
667    }
668
669    pub fn outset(&mut self, d: impl Into<Vector>) {
670        *self = self.with_outset(d)
671    }
672
673    /// Returns true if this rectangle intersects `r`, and sets this rectangle to the
674    /// intersection. Returns false if this rectangle does not intersect `r`, and leaves this
675    /// rectangle unchanged.
676    ///
677    /// Returns false if either `r` or this rectangle is empty, leaving this rectangle unchanged.
678    ///
679    /// - `r` limit of result
680    ///
681    /// Example (C++): <https://fiddle.skia.org/c/@Rect_intersect>
682    pub fn intersect(&mut self, r: impl AsRef<Rect>) -> bool {
683        unsafe { self.native_mut().intersect(r.as_ref().native()) }
684    }
685
686    #[must_use]
687    pub fn intersect2(&mut self, a: impl AsRef<Rect>, b: impl AsRef<Rect>) -> bool {
688        unsafe {
689            self.native_mut()
690                .intersect1(a.as_ref().native(), b.as_ref().native())
691        }
692    }
693
694    pub fn intersects(&self, r: impl AsRef<Rect>) -> bool {
695        let r = r.as_ref();
696        Self::intersects_(
697            self.left,
698            self.top,
699            self.right,
700            self.bottom,
701            r.left,
702            r.top,
703            r.right,
704            r.bottom,
705        )
706    }
707
708    pub fn intersects2(a: impl AsRef<Rect>, b: impl AsRef<Rect>) -> bool {
709        a.as_ref().intersects(b)
710    }
711
712    #[allow(clippy::too_many_arguments)]
713    fn intersects_(al: f32, at: f32, ar: f32, ab: f32, bl: f32, bt: f32, br: f32, bb: f32) -> bool {
714        let l = al.max(bl);
715        let r = ar.min(br);
716        let t = at.max(bt);
717        let b = ab.min(bb);
718        l < r && t < b
719    }
720
721    /// Sets this rectangle to the union of itself and `r`.
722    ///
723    /// Has no effect if `r` is empty. Otherwise, if this rectangle is empty, sets this rectangle
724    /// to `r`.
725    ///
726    /// - `r` expansion rectangle
727    ///
728    /// Example (C++): <https://fiddle.skia.org/c/@Rect_join_2>
729    pub fn join(&mut self, r: impl AsRef<Rect>) {
730        let r = r.as_ref();
731        unsafe { self.native_mut().join(r.native()) }
732    }
733
734    pub fn join2(a: impl AsRef<Rect>, b: impl AsRef<Rect>) -> Rect {
735        let mut result = *a.as_ref();
736        result.join(b);
737        result
738    }
739
740    pub fn join_non_empty_arg(&mut self, r: impl AsRef<Rect>) {
741        let r = r.as_ref();
742        debug_assert!(!r.is_empty());
743        if self.left >= self.right || self.top >= self.bottom {
744            *self = *r;
745        } else {
746            self.join_possibly_empty_rect(r);
747        }
748    }
749
750    pub fn join_possibly_empty_rect(&mut self, r: impl AsRef<Rect>) {
751        let r = r.as_ref();
752        self.left = self.left.min(r.left);
753        self.top = self.top.min(r.top);
754        self.right = self.right.max(r.right);
755        self.bottom = self.bottom.max(r.bottom);
756    }
757
758    // The set of contains() functions are defined as a trait below.
759
760    #[must_use]
761    pub fn round(&self) -> IRect {
762        let mut r = IRect::default();
763        unsafe { sb::C_SkRect_round(self.native(), r.native_mut()) };
764        r
765    }
766
767    // The functions round_out() are defined as a trait below.
768
769    #[must_use]
770    pub fn round_in(&self) -> IRect {
771        let mut r = IRect::default();
772        unsafe { sb::C_SkRect_roundIn(self.native(), r.native_mut()) };
773        r
774    }
775
776    pub fn sort(&mut self) {
777        if self.left > self.right {
778            mem::swap(&mut self.left, &mut self.right);
779        }
780
781        if self.top > self.bottom {
782            mem::swap(&mut self.top, &mut self.bottom);
783        }
784    }
785
786    #[must_use]
787    pub fn sorted(&self) -> Rect {
788        Rect::new(
789            self.left.min(self.right),
790            self.top.min(self.bottom),
791            self.left.max(self.right),
792            self.top.max(self.bottom),
793        )
794    }
795
796    pub fn as_scalars(&self) -> &[f32; 4] {
797        unsafe { transmute_ref(&self.left) }
798    }
799
800    /// Writes a text representation of this rectangle to standard output. Set `as_hex` to true to
801    /// generate exact binary representations of floating point numbers.
802    ///
803    /// - `as_hex` true if scalar values are written as hexadecimal
804    ///
805    /// Example (C++): <https://fiddle.skia.org/c/@Rect_dump>
806    pub fn dump(&self, as_hex: impl Into<Option<bool>>) {
807        unsafe { self.native().dump(as_hex.into().unwrap_or_default()) }
808    }
809
810    pub fn dump_to_string(&self, as_hex: bool) -> String {
811        let mut str = interop::String::default();
812        unsafe { sb::C_SkRect_dumpToString(self.native(), as_hex, str.native_mut()) }
813        str.to_string()
814    }
815
816    pub fn dump_hex(&self) {
817        self.dump(true)
818    }
819}
820
821impl Contains<Point> for Rect {
822    fn contains(&self, p: Point) -> bool {
823        self.contains(&p)
824    }
825}
826
827impl Contains<&Point> for Rect {
828    fn contains(&self, p: &Point) -> bool {
829        p.x >= self.left && p.x < self.right && p.y >= self.top && p.y < self.bottom
830    }
831}
832
833impl Contains<Rect> for Rect {
834    fn contains(&self, r: Rect) -> bool {
835        self.contains(&r)
836    }
837}
838
839impl Contains<&Rect> for Rect {
840    fn contains(&self, r: &Rect) -> bool {
841        // TODO: can we eliminate the this->is_empty check?
842        !r.is_empty()
843            && !self.is_empty()
844            && self.left <= r.left
845            && self.top <= r.top
846            && self.right >= r.right
847            && self.bottom >= r.bottom
848    }
849}
850
851impl Contains<IRect> for Rect {
852    fn contains(&self, r: IRect) -> bool {
853        self.contains(&r)
854    }
855}
856
857impl Contains<&IRect> for Rect {
858    fn contains(&self, r: &IRect) -> bool {
859        // TODO: can we eliminate the this->isEmpty check?
860        !r.is_empty()
861            && !self.is_empty()
862            && self.left <= r.left as f32
863            && self.top <= r.top as f32
864            && self.right >= r.right as f32
865            && self.bottom >= r.bottom as f32
866    }
867}
868
869#[test]
870fn contains_overloads_compile() {
871    let r = Rect::default();
872    r.contains(Point::default());
873    r.contains(Rect::default());
874    r.contains(IRect::default());
875}
876
877pub trait RoundOut<R> {
878    fn round_out(&self) -> R;
879}
880
881impl RoundOut<IRect> for Rect {
882    fn round_out(&self) -> IRect {
883        let mut r = IRect::default();
884        unsafe { sb::C_SkRect_roundOut(self.native(), r.native_mut()) };
885        r
886    }
887}
888
889impl RoundOut<Rect> for Rect {
890    fn round_out(&self) -> Rect {
891        Rect::new(
892            self.left.floor(),
893            self.top.floor(),
894            self.right.ceil(),
895            self.bottom.ceil(),
896        )
897    }
898}
899
900//
901// From
902//
903
904impl From<(IPoint, ISize)> for IRect {
905    fn from((point, size): (IPoint, ISize)) -> Self {
906        IRect::new(
907            point.x,
908            point.y,
909            point.x + size.width,
910            point.y + size.height,
911        )
912    }
913}
914
915impl From<(Point, Size)> for Rect {
916    fn from((point, size): (Point, Size)) -> Self {
917        Rect::new(
918            point.x,
919            point.y,
920            point.x + size.width,
921            point.y + size.height,
922        )
923    }
924}
925
926impl From<ISize> for Rect {
927    fn from(isize: ISize) -> Self {
928        Self::from_isize(isize)
929    }
930}
931impl From<IRect> for Rect {
932    fn from(irect: IRect) -> Self {
933        Self::from_irect(irect)
934    }
935}