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