1use crate::{interop, prelude::*, scalar, Matrix, Rect, Vector};
2use skia_bindings::{self as sb, SkRRect};
3use std::{fmt, mem, ptr};
4
5pub use skia_bindings::SkRRect_Type as Type;
6variant_name!(Type::Complex);
7
8pub use skia_bindings::SkRRect_Corner as Corner;
9variant_name!(Corner::LowerLeft);
10
11#[derive(Copy, Clone)]
12#[repr(transparent)]
13pub struct RRect(SkRRect);
14
15native_transmutable!(SkRRect, RRect);
16
17impl PartialEq for RRect {
18 fn eq(&self, rhs: &Self) -> bool {
19 unsafe { sb::C_SkRRect_Equals(self.native(), rhs.native()) }
20 }
21}
22
23impl Default for RRect {
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl fmt::Debug for RRect {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 f.debug_struct("RRect")
32 .field("rect", &self.rect())
33 .field(
34 "radii",
35 &[
36 self.radii(Corner::UpperLeft),
37 self.radii(Corner::UpperRight),
38 self.radii(Corner::LowerRight),
39 self.radii(Corner::LowerLeft),
40 ],
41 )
42 .field("type", &self.get_type())
43 .finish()
44 }
45}
46
47impl AsRef<RRect> for RRect {
48 fn as_ref(&self) -> &RRect {
49 self
50 }
51}
52
53impl RRect {
54 pub fn new() -> Self {
55 RRect::construct(|rr| unsafe { sb::C_SkRRect_Construct(rr) })
56 }
57
58 pub fn get_type(&self) -> Type {
59 unsafe { sb::C_SkRRect_getType(self.native()) }
60 }
61
62 pub fn is_empty(&self) -> bool {
63 self.get_type() == Type::Empty
64 }
65
66 pub fn is_rect(&self) -> bool {
67 self.get_type() == Type::Rect
68 }
69
70 pub fn is_oval(&self) -> bool {
71 self.get_type() == Type::Oval
72 }
73
74 pub fn is_simple(&self) -> bool {
75 self.get_type() == Type::Simple
76 }
77
78 pub fn is_nine_patch(&self) -> bool {
79 self.get_type() == Type::NinePatch
80 }
81
82 pub fn is_complex(&self) -> bool {
83 self.get_type() == Type::Complex
84 }
85
86 pub fn width(&self) -> scalar {
87 self.rect().width()
88 }
89
90 pub fn height(&self) -> scalar {
91 self.rect().height()
92 }
93
94 pub fn simple_radii(&self) -> Vector {
95 self.radii(Corner::UpperLeft)
96 }
97
98 pub fn set_empty(&mut self) {
99 *self = Self::new()
100 }
101
102 pub fn set_rect(&mut self, rect: impl AsRef<Rect>) {
103 unsafe { sb::C_SkRRect_setRect(self.native_mut(), rect.as_ref().native()) }
104 }
105
106 pub fn new_empty() -> Self {
107 Self::new()
108 }
109
110 pub fn new_rect(rect: impl AsRef<Rect>) -> Self {
114 let mut rr = Self::default();
115 rr.set_rect(rect);
116 rr
117 }
118
119 pub fn new_oval(oval: impl AsRef<Rect>) -> Self {
120 let mut rr = Self::default();
121 rr.set_oval(oval);
122 rr
123 }
124
125 pub fn new_rect_xy(rect: impl AsRef<Rect>, x_rad: scalar, y_rad: scalar) -> Self {
126 let mut rr = Self::default();
127 rr.set_rect_xy(rect.as_ref(), x_rad, y_rad);
128 rr
129 }
130
131 pub fn new_rect_radii(rect: impl AsRef<Rect>, radii: &[Vector; 4]) -> Self {
132 let mut rr = Self::default();
133 rr.set_rect_radii(rect, radii);
134 rr
135 }
136
137 pub fn new_nine_patch(
138 rect: impl AsRef<Rect>,
139 left_rad: scalar,
140 top_rad: scalar,
141 right_rad: scalar,
142 bottom_rad: scalar,
143 ) -> Self {
144 let mut r = Self::default();
145 r.set_nine_patch(rect, left_rad, top_rad, right_rad, bottom_rad);
146 r
147 }
148
149 pub fn set_oval(&mut self, oval: impl AsRef<Rect>) {
150 unsafe { self.native_mut().setOval(oval.as_ref().native()) }
151 }
152
153 pub fn set_rect_xy(&mut self, rect: impl AsRef<Rect>, x_rad: scalar, y_rad: scalar) {
154 unsafe {
155 self.native_mut()
156 .setRectXY(rect.as_ref().native(), x_rad, y_rad)
157 }
158 }
159
160 pub fn set_nine_patch(
161 &mut self,
162 rect: impl AsRef<Rect>,
163 left_rad: scalar,
164 top_rad: scalar,
165 right_rad: scalar,
166 bottom_rad: scalar,
167 ) {
168 unsafe {
169 self.native_mut().setNinePatch(
170 rect.as_ref().native(),
171 left_rad,
172 top_rad,
173 right_rad,
174 bottom_rad,
175 )
176 }
177 }
178
179 pub fn set_rect_radii(&mut self, rect: impl AsRef<Rect>, radii: &[Vector; 4]) {
180 unsafe {
181 self.native_mut()
182 .setRectRadii(rect.as_ref().native(), radii.native().as_ptr())
183 }
184 }
185
186 pub fn rect(&self) -> &Rect {
187 Rect::from_native_ref(&self.native().fRect)
188 }
189
190 pub fn radii(&self, corner: Corner) -> Vector {
191 Vector::from_native_c(self.native().fRadii[corner as usize])
192 }
193
194 pub fn radii_ref(&self) -> &[Vector; 4] {
195 Vector::from_native_array_ref(&self.native().fRadii)
196 }
197
198 pub fn bounds(&self) -> &Rect {
199 self.rect()
200 }
201
202 pub fn inset(&mut self, delta: impl Into<Vector>) {
203 *self = self.with_inset(delta)
204 }
205
206 #[must_use]
207 pub fn with_inset(&self, delta: impl Into<Vector>) -> Self {
208 let delta = delta.into();
209 let mut r = Self::default();
210 unsafe { self.native().inset(delta.x, delta.y, r.native_mut()) };
211 r
212 }
213
214 pub fn outset(&mut self, delta: impl Into<Vector>) {
215 *self = self.with_outset(delta)
216 }
217
218 #[must_use]
219 pub fn with_outset(&self, delta: impl Into<Vector>) -> Self {
220 self.with_inset(-delta.into())
221 }
222
223 pub fn offset(&mut self, delta: impl Into<Vector>) {
224 Rect::from_native_ref_mut(&mut self.native_mut().fRect).offset(delta)
225 }
226
227 #[must_use]
228 pub fn with_offset(&self, delta: impl Into<Vector>) -> Self {
229 let mut copied = *self;
230 copied.offset(delta);
231 copied
232 }
233
234 pub fn contains(&self, rect: impl AsRef<Rect>) -> bool {
235 unsafe { self.native().contains(rect.as_ref().native()) }
236 }
237
238 pub fn is_valid(&self) -> bool {
239 unsafe { self.native().isValid() }
240 }
241
242 pub const SIZE_IN_MEMORY: usize = mem::size_of::<Self>();
243
244 pub fn write_to_memory(&self, buffer: &mut Vec<u8>) {
245 unsafe {
246 let size = self.native().writeToMemory(ptr::null_mut());
247 buffer.resize(size, 0);
248 let written = self.native().writeToMemory(buffer.as_mut_ptr() as _);
249 debug_assert_eq!(written, size);
250 }
251 }
252
253 pub fn read_from_memory(&mut self, buffer: &[u8]) -> usize {
254 unsafe {
255 self.native_mut()
256 .readFromMemory(buffer.as_ptr() as _, buffer.len())
257 }
258 }
259
260 #[must_use]
261 pub fn transform(&self, matrix: &Matrix) -> Option<Self> {
262 let mut r = Self::default();
263 unsafe { self.native().transform1(matrix.native(), r.native_mut()) }.then_some(r)
264 }
265
266 pub fn dump(&self, as_hex: impl Into<Option<bool>>) {
267 unsafe { self.native().dump(as_hex.into().unwrap_or_default()) }
268 }
269
270 pub fn dump_to_string(&self, as_hex: bool) -> String {
271 let mut str = interop::String::default();
272 unsafe { sb::C_SkRRect_dumpToString(self.native(), as_hex, str.native_mut()) }
273 str.to_string()
274 }
275
276 pub fn dump_hex(&self) {
277 self.dump(true)
278 }
279}