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
use crate::prelude::*;
use crate::{scalar, Matrix, NativeFlattenable, Path, Point, Rect, StrokeRec, Vector};
use skia_bindings as sb;
use skia_bindings::{
SkFlattenable, SkPathEffect, SkPathEffect_DashType, SkPathEffect_PointData, SkRefCntBase,
};
use std::os::raw;
use std::slice;
#[repr(C)]
pub struct PointData {
pub flags: point_data::PointFlags,
points: *const Point,
num_points: raw::c_int,
pub size: Vector,
pub clip_rect: Rect,
pub path: Path,
pub first: Path,
pub last: Path,
}
impl NativeTransmutable<SkPathEffect_PointData> for PointData {}
#[test]
fn test_point_data_layout() {
Point::test_layout();
Vector::test_layout();
Rect::test_layout();
PointData::test_layout();
}
impl Drop for PointData {
fn drop(&mut self) {
unsafe {
sb::C_SkPathEffect_PointData_deletePoints(self.native_mut())
}
}
}
impl Default for PointData {
fn default() -> Self {
PointData::construct(|point_data| unsafe {
sb::C_SkPathEffect_PointData_Construct(point_data)
})
}
}
impl PointData {
pub fn points(&self) -> &[Point] {
unsafe { slice::from_raw_parts(self.points, self.num_points.try_into().unwrap()) }
}
}
pub mod point_data {
use skia_bindings as sb;
bitflags! {
pub struct PointFlags: u32 {
const CIRCLES = sb::SkPathEffect_PointData_PointFlags_kCircles_PointFlag as _;
const USE_PATH = sb::SkPathEffect_PointData_PointFlags_kUsePath_PointFlag as _;
const USE_CLIP = sb::SkPathEffect_PointData_PointFlags_kUseClip_PointFlag as _;
}
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct DashInfo {
pub intervals: Vec<scalar>,
pub phase: scalar,
}
pub type PathEffect = RCHandle<SkPathEffect>;
impl NativeRefCountedBase for SkPathEffect {
type Base = SkRefCntBase;
fn ref_counted_base(&self) -> &Self::Base {
&self._base._base._base
}
}
impl NativeFlattenable for SkPathEffect {
fn native_flattenable(&self) -> &SkFlattenable {
&self._base
}
fn native_deserialize(data: &[u8]) -> *mut Self {
unsafe { sb::C_SkPathEffect_Deserialize(data.as_ptr() as _, data.len()) }
}
}
impl RCHandle<SkPathEffect> {
pub fn sum(first: PathEffect, second: PathEffect) -> PathEffect {
PathEffect::from_ptr(unsafe {
sb::C_SkPathEffect_MakeSum(first.into_ptr(), second.into_ptr())
})
.unwrap()
}
pub fn compose(first: PathEffect, second: PathEffect) -> PathEffect {
PathEffect::from_ptr(unsafe {
sb::C_SkPathEffect_MakeCompose(first.into_ptr(), second.into_ptr())
})
.unwrap()
}
pub fn filter_path(
&self,
src: &Path,
stroke_rec: &StrokeRec,
cull_rect: impl AsRef<Rect>,
) -> Option<(Path, StrokeRec)> {
let mut dst = Path::default();
let mut stroke_rec_r = stroke_rec.clone();
self.filter_path_inplace(&mut dst, src, &mut stroke_rec_r, cull_rect)
.if_true_some((dst, stroke_rec_r))
}
pub fn filter_path_inplace(
&self,
dst: &mut Path,
src: &Path,
stroke_rec: &mut StrokeRec,
cull_rect: impl AsRef<Rect>,
) -> bool {
unsafe {
self.native().filterPath(
dst.native_mut(),
src.native(),
stroke_rec.native_mut(),
cull_rect.as_ref().native(),
)
}
}
pub fn compute_fast_bounds(&self, src: impl AsRef<Rect>) -> Rect {
let mut r: Rect = Rect::default();
unsafe {
self.native()
.computeFastBounds(r.native_mut(), src.as_ref().native())
};
r
}
pub fn as_points(
&self,
src: &Path,
stroke_rect: &StrokeRec,
matrix: &Matrix,
cull_rect: impl AsRef<Rect>,
) -> Option<PointData> {
let mut point_data = PointData::default();
unsafe {
self.native().asPoints(
point_data.native_mut(),
src.native(),
stroke_rect.native(),
matrix.native(),
cull_rect.as_ref().native(),
)
}
.if_true_some(point_data)
}
#[deprecated(since = "0.12.0", note = "use as_a_dash()")]
pub fn as_dash(&self) -> Option<DashInfo> {
self.as_a_dash()
}
pub fn as_a_dash(&self) -> Option<DashInfo> {
let mut dash_info = construct(|di| unsafe { sb::C_SkPathEffect_DashInfo_Construct(di) });
let dash_type = unsafe { self.native().asADash(&mut dash_info) };
match dash_type {
SkPathEffect_DashType::kDash_DashType => {
let mut v: Vec<scalar> = vec![0.0; dash_info.fCount.try_into().unwrap()];
dash_info.fIntervals = v.as_mut_ptr();
unsafe {
assert_eq!(dash_type, self.native().asADash(&mut dash_info));
}
Some(DashInfo {
intervals: v,
phase: dash_info.fPhase,
})
}
SkPathEffect_DashType::kNone_DashType => None,
}
}
}
#[test]
fn create_and_drop_point_data() {
let data = PointData::default();
drop(data)
}