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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use std::fmt;
use std::marker::PhantomData;
use skia_bindings::{
self as sb, SkContourMeasure, SkContourMeasureIter, SkContourMeasure_ForwardVerbIterator,
SkContourMeasure_VerbMeasure, SkRefCntBase,
};
use crate::{prelude::*, scalar, Matrix, Path, PathBuilder, PathVerb, Point, Vector};
pub type ContourMeasure = RCHandle<SkContourMeasure>;
unsafe_send_sync!(ContourMeasure);
impl NativeRefCountedBase for SkContourMeasure {
type Base = SkRefCntBase;
}
bitflags! {
/// Flags that control what [`ContourMeasure::get_matrix()`] computes.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MatrixFlags : u32 {
/// Compute the position component.
const GET_POSITION = sb::SkContourMeasure_MatrixFlags_kGetPosition_MatrixFlag as _;
/// Compute the tangent component.
const GET_TANGENT = sb::SkContourMeasure_MatrixFlags_kGetTangent_MatrixFlag as _;
/// Compute both position and tangent components.
const GET_POS_AND_TAN = Self::GET_POSITION.bits() | Self::GET_TANGENT.bits();
}
}
impl Default for MatrixFlags {
fn default() -> Self {
Self::GET_POS_AND_TAN
}
}
impl fmt::Debug for ContourMeasure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ContourMeasure")
.field("length", &self.length())
.field("is_closed", &self.is_closed())
.finish()
}
}
impl ContourMeasure {
/// Returns the length of the contour.
pub fn length(&self) -> scalar {
unsafe { sb::C_SkContourMeasure_length(self.native()) }
}
/// Pins `distance` to `0 <= distance <= length()`, then computes the corresponding
/// position and tangent.
///
/// - `distance`: distance along the contour.
#[must_use]
pub fn pos_tan(&self, distance: scalar) -> Option<(Point, Vector)> {
let mut p = Point::default();
let mut v = Vector::default();
unsafe {
self.native()
.getPosTan(distance, p.native_mut(), v.native_mut())
}
.then_some((p, v))
}
#[must_use]
/// Pins `distance` to `0 <= distance <= length()`, then computes the corresponding
/// matrix (by calling [`Self::pos_tan()`]).
///
/// Returns `None` if there is no path, or a zero-length path was specified.
///
/// - `distance`: distance along the contour.
/// - `flags`: controls whether position, tangent, or both are computed.
pub fn get_matrix(
&self,
distance: scalar,
flags: impl Into<Option<MatrixFlags>>,
) -> Option<Matrix> {
let mut m = Matrix::default();
unsafe {
self.native().getMatrix(
distance,
m.native_mut(),
// note: depending on the OS, different representation types are generated for
// MatrixFlags, so the try_into() is required, even though clippy complains about
// it.
#[allow(clippy::useless_conversion)]
flags.into().unwrap_or_default().bits().try_into().unwrap(),
)
}
.then_some(m)
}
#[deprecated(since = "0.94.0", note = "Use get_segment()")]
#[must_use]
/// Given a start and stop distance, appends the intervening segment(s) to `path_builder`.
///
/// If the segment is zero-length, returns `false`; otherwise returns `true`.
/// `start_d` and `stop_d` are pinned to legal values (`0..length()`). If
/// `start_d > stop_d`, returns `false` and leaves `path_builder` untouched.
///
/// Begins the segment with a `move_to` if `start_with_move_to` is `true`.
///
/// - `start_d`: start distance along the contour.
/// - `stop_d`: stop distance along the contour.
/// - `path_builder`: destination that receives the segment.
/// - `start_with_move_to`: whether to begin with `move_to`.
pub fn segment(
&self,
start_d: scalar,
stop_d: scalar,
path_builder: &mut PathBuilder,
start_with_move_to: bool,
) -> bool {
self.get_segment(start_d, stop_d, path_builder, start_with_move_to)
}
#[must_use]
/// Given a start and stop distance, appends the intervening segment(s) to `path_builder`.
///
/// If the segment is zero-length, returns `false`; otherwise returns `true`.
/// `start_d` and `stop_d` are pinned to legal values (`0..length()`). If
/// `start_d > stop_d`, returns `false` and leaves `path_builder` untouched.
///
/// Begins the segment with a `move_to` if `start_with_move_to` is `true`.
///
/// - `start_d`: start distance along the contour.
/// - `stop_d`: stop distance along the contour.
/// - `path_builder`: destination that receives the segment.
/// - `start_with_move_to`: whether to begin with `move_to`.
pub fn get_segment(
&self,
start_d: scalar,
stop_d: scalar,
path_builder: &mut PathBuilder,
start_with_move_to: bool,
) -> bool {
unsafe {
self.native().getSegment(
start_d,
stop_d,
path_builder.native_mut(),
start_with_move_to,
)
}
}
/// Returns `true` if the contour is closed.
pub fn is_closed(&self) -> bool {
unsafe { sb::C_SkContourMeasure_isClosed(self.native()) }
}
/// Returns an iterator over measurement data for the contour's verbs.
pub fn verbs(&self) -> ForwardVerbIterator {
let iterator =
construct(|iterator| unsafe { sb::C_SkContourMeasure_begin(self.native(), iterator) });
ForwardVerbIterator {
iterator,
contour_measure: self,
}
}
}
/// Utility for iterating over a contour's verbs.
pub struct ForwardVerbIterator<'a> {
iterator: SkContourMeasure_ForwardVerbIterator,
contour_measure: &'a ContourMeasure,
}
unsafe_send_sync!(ForwardVerbIterator<'_>);
impl fmt::Debug for ForwardVerbIterator<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ForwardVerbIterator").finish()
}
}
impl PartialEq for ForwardVerbIterator<'_> {
fn eq(&self, other: &Self) -> bool {
unsafe {
sb::C_SkContourMeasure_ForwardVerbIterator_Equals(&self.iterator, &other.iterator)
}
}
}
impl<'a> Iterator for ForwardVerbIterator<'a> {
type Item = VerbMeasure<'a>;
fn next(&mut self) -> Option<Self::Item> {
let end = construct(|end| unsafe {
sb::C_SkContourMeasure_end(self.contour_measure.native(), end)
});
if unsafe { sb::C_SkContourMeasure_ForwardVerbIterator_Equals(&self.iterator, &end) } {
return None;
}
let item = construct(|item| unsafe {
sb::C_SkContourMeasure_ForwardVerbIterator_item(&self.iterator, item)
});
unsafe { sb::C_SkContourMeasure_ForwardVerbIterator_next(&mut self.iterator) };
Some(VerbMeasure {
verb_measure: item,
_pd: PhantomData,
})
}
}
/// Measurement data for an individual verb.
pub struct VerbMeasure<'a> {
verb_measure: SkContourMeasure_VerbMeasure,
_pd: PhantomData<ForwardVerbIterator<'a>>,
}
unsafe_send_sync!(VerbMeasure<'_>);
impl fmt::Debug for VerbMeasure<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VerbMeasure")
.field("verb", &self.verb())
.field("distance", &self.distance())
.field("points", &self.points())
.finish()
}
}
impl VerbMeasure<'_> {
/// Returns the verb type.
pub fn verb(&self) -> PathVerb {
self.verb_measure.fVerb
}
/// Returns the cumulative distance along the current contour.
pub fn distance(&self) -> scalar {
self.verb_measure.fDistance
}
/// Returns the verb points.
pub fn points(&self) -> &[Point] {
unsafe {
safer::from_raw_parts(
Point::from_native_ptr(self.verb_measure.fPts.fPtr),
self.verb_measure.fPts.fSize,
)
}
}
}
pub type ContourMeasureIter = Handle<SkContourMeasureIter>;
unsafe_send_sync!(ContourMeasureIter);
impl NativeDrop for SkContourMeasureIter {
fn drop(&mut self) {
unsafe {
sb::C_SkContourMeasureIter_destruct(self);
}
}
}
impl Iterator for ContourMeasureIter {
type Item = ContourMeasure;
/// Iterates through contours in the path, returning a [`ContourMeasure`] for each contour.
/// Returns `None` when iteration is complete.
///
/// Only non-zero-length contours are returned, where a contour is the segments
/// between a move verb and either:
/// - the next move verb,
/// - one or more close verbs,
/// - or the end of the path.
///
/// Zero-length contours are skipped.
fn next(&mut self) -> Option<Self::Item> {
ContourMeasure::from_ptr(unsafe { sb::C_SkContourMeasureIter_next(self.native_mut()) })
}
}
impl fmt::Debug for ContourMeasureIter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ContourMeasureIter").finish()
}
}
impl ContourMeasureIter {
/// Initializes the iterator with a path.
///
/// The parts of the path that are needed are copied, so the caller is free
/// to modify or delete the path after this call.
///
/// `res_scale` controls the precision of the measure. Values greater than
/// `1` increase precision (and may slow down the computation).
///
/// - `path`: source path to iterate.
/// - `force_closed`: whether open contours are treated as closed.
/// - `res_scale`: optional precision scale (defaults to `1.0`).
pub fn new(path: &Path, force_closed: bool, res_scale: impl Into<Option<scalar>>) -> Self {
Self::from_path(path, force_closed, res_scale)
}
/// Initializes the iterator with a path.
///
/// The parts of the path that are needed are copied, so the caller is free
/// to modify or delete the path after this call.
///
/// `res_scale` controls the precision of the measure. Values greater than
/// `1` increase precision (and may slow down the computation).
///
/// - `path`: source path to iterate.
/// - `force_closed`: whether open contours are treated as closed.
/// - `res_scale`: optional precision scale (defaults to `1.0`).
pub fn from_path(
path: &Path,
force_closed: bool,
res_scale: impl Into<Option<scalar>>,
) -> Self {
Self::from_native_c(unsafe {
SkContourMeasureIter::new1(path.native(), force_closed, res_scale.into().unwrap_or(1.0))
})
}
/// Resets the iterator with a path.
///
/// The parts of the path that are needed are copied, so the caller is free
/// to modify or delete the path after this call.
///
/// - `path`: source path to iterate.
/// - `force_closed`: whether open contours are treated as closed.
/// - `res_scale`: optional precision scale (defaults to `1.0`).
pub fn reset(
&mut self,
path: &Path,
force_closed: bool,
res_scale: impl Into<Option<scalar>>,
) -> &mut Self {
unsafe {
self.native_mut()
.reset(path.native(), force_closed, res_scale.into().unwrap_or(1.0))
}
self
}
}
#[cfg(test)]
mod tests {
use super::ContourMeasureIter;
use crate::{Path, Rect};
#[test]
fn contour_and_verb_measure() {
let p = Path::rect(Rect::new(0.0, 0.0, 10.0, 10.0), None);
let measure = ContourMeasureIter::new(&p, true, None);
for contour in measure {
for verb in contour.verbs() {
println!("verb: {verb:?}")
}
}
}
}