1use crate::{prelude::*, scalar};
2use skia_bindings::{self as sb, SkISize, SkSize};
3use std::ops::{Div, DivAssign, Mul, MulAssign};
4
5#[repr(C)]
6#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
7pub struct ISize {
8 pub width: i32,
9 pub height: i32,
10}
11
12native_transmutable!(SkISize, ISize);
13
14impl ISize {
15 pub const fn new(w: i32, h: i32) -> ISize {
16 ISize {
17 width: w,
18 height: h,
19 }
20 }
21
22 pub const fn new_empty() -> ISize {
23 Self::new(0, 0)
24 }
25
26 pub fn set(&mut self, w: i32, h: i32) {
27 *self = Self::new(w, h);
28 }
29
30 pub fn is_zero(self) -> bool {
32 self.width == 0 && self.height == 0
33 }
34
35 pub fn is_empty(self) -> bool {
37 self.width <= 0 || self.height <= 0
38 }
39
40 pub fn set_empty(&mut self) {
42 *self = Self::new_empty();
43 }
44
45 pub const fn area(self) -> i64 {
46 self.width as i64 * self.height as i64
47 }
48
49 pub fn equals(self, w: i32, h: i32) -> bool {
52 self == Self::new(w, h)
53 }
54}
55
56#[repr(C)]
57#[derive(Copy, Clone, PartialEq, Default, Debug)]
58pub struct Size {
59 pub width: scalar,
60 pub height: scalar,
61}
62
63native_transmutable!(SkSize, Size);
64
65impl Size {
66 pub const fn new(w: scalar, h: scalar) -> Size {
67 Size {
68 width: w,
69 height: h,
70 }
71 }
72
73 pub const fn from_isize(src: ISize) -> Size {
74 Self::new(src.width as _, src.height as _)
75 }
76
77 pub const fn new_empty() -> Self {
78 Self::new(0.0, 0.0)
79 }
80
81 pub fn set(&mut self, w: scalar, h: scalar) {
82 *self = Self::new(w, h);
83 }
84
85 pub fn is_zero(self) -> bool {
87 self.width == 0.0 && self.height == 0.0
88 }
89
90 pub fn is_empty(self) -> bool {
92 self.width <= 0.0 || self.height <= 0.0
93 }
94
95 pub fn set_empty(&mut self) {
97 *self = Self::new_empty()
98 }
99
100 pub fn equals(self, w: scalar, h: scalar) -> bool {
103 self == Self::new(w, h)
104 }
105
106 pub fn to_round(self) -> ISize {
107 ISize::from_native_c(unsafe { sb::C_SkSize_toRound(self.native()) })
108 }
109
110 pub fn to_ceil(self) -> ISize {
111 ISize::from_native_c(unsafe { sb::C_SkSize_toCeil(self.native()) })
112 }
113
114 pub fn to_floor(self) -> ISize {
115 ISize::from_native_c(unsafe { sb::C_SkSize_toFloor(self.native()) })
116 }
117}
118
119impl From<(i32, i32)> for ISize {
124 fn from(source: (i32, i32)) -> Self {
125 Self::new(source.0, source.1)
126 }
127}
128
129impl From<(scalar, scalar)> for Size {
130 fn from(source: (scalar, scalar)) -> Self {
131 Self::new(source.0, source.1)
132 }
133}
134
135impl From<ISize> for Size {
136 fn from(size: ISize) -> Self {
137 Self::new(size.width as _, size.height as _)
138 }
139}
140
141impl From<(i32, i32)> for Size {
143 fn from(source: (i32, i32)) -> Self {
144 (source.0 as scalar, source.1 as scalar).into()
145 }
146}
147
148impl Div<scalar> for Size {
149 type Output = Self;
150 fn div(self, rhs: scalar) -> Self {
151 Self::new(self.width / rhs, self.height / rhs)
152 }
153}
154
155impl DivAssign<scalar> for Size {
156 fn div_assign(&mut self, rhs: scalar) {
157 *self = *self / rhs
158 }
159}
160
161impl Mul<scalar> for Size {
162 type Output = Self;
163 fn mul(self, rhs: scalar) -> Self {
164 Self::new(self.width * rhs, self.height * rhs)
165 }
166}
167
168impl MulAssign<scalar> for Size {
169 fn mul_assign(&mut self, rhs: scalar) {
170 *self = *self * rhs
171 }
172}