Skip to main content

path_kit/
bridge.rs

1#[cxx::bridge]
2pub(crate) mod ffi {
3    /// 路径布尔运算,与 `pk::SkPathOp` 取值一致。
4    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
5    #[repr(u32)]
6    pub enum PathOp {
7        Difference = 0,
8        Intersect = 1,
9        Union = 2,
10        Xor = 3,
11        ReverseDifference = 4,
12    }
13
14    /// 与 `pk::SkPathDirection` 一致。
15    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
16    #[repr(u32)]
17    pub enum Direction {
18        Cw = 0,
19        Ccw = 1,
20    }
21
22    /// 与 `pk::SkPathFillType` 低位取值一致。
23    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
24    #[repr(u8)]
25    pub enum PathFillType {
26        Winding = 0,
27        EvenOdd = 1,
28        InverseWinding = 2,
29        InverseEvenOdd = 3,
30    }
31
32    /// 矩形起始角,与 `SkPath::Corner` / `addRect` 一致。
33    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
34    #[repr(u32)]
35    pub enum RectCorner {
36        UpperLeft = 0,
37        UpperRight = 1,
38        LowerRight = 2,
39        LowerLeft = 3,
40    }
41
42    /// 与 `pk::SkRRect::Type` 一致。
43    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
44    #[repr(i32)]
45    pub enum RRectType {
46        Empty = 0,
47        Rect = 1,
48        Oval = 2,
49        Simple = 3,
50        NinePatch = 4,
51        Complex = 5,
52    }
53
54    /// 与 `pk::SkPath::Verb` 一致。
55    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
56    #[repr(u32)]
57    pub enum PathVerb {
58        Move = 0,
59        Line = 1,
60        Quad = 2,
61        Conic = 3,
62        Cubic = 4,
63        Close = 5,
64        Done = 6,
65    }
66
67    /// 与 `pk::SkPaint::Style` 一致。
68    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
69    #[repr(u8)]
70    pub enum PaintStyle {
71        Fill = 0,
72        Stroke = 1,
73        StrokeAndFill = 2,
74    }
75
76    /// 与 `pk::SkPaint::Cap` / `SkStrokeRec` cap 一致。
77    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
78    #[repr(u32)]
79    pub enum StrokeCap {
80        Butt = 0,
81        Round = 1,
82        Square = 2,
83    }
84
85    /// 与 `pk::SkPaint::Join` / `SkStrokeRec` join 一致。
86    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
87    #[repr(u8)]
88    pub enum StrokeJoin {
89        Miter = 0,
90        Round = 1,
91        Bevel = 2,
92    }
93
94    /// `pk::SkStrokeRec::InitStyle`(C++ 侧仍为 `pk_stroke_rec_new`)。
95    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
96    #[repr(u32)]
97    pub enum StrokeRecInit {
98        Hairline = 0,
99        Fill = 1,
100    }
101
102    /// `pk::SkStrokeRec::Style`(`SkStrokeRec::getStyle` 返回值)。
103    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
104    #[repr(u32)]
105    pub enum StrokeRecStyleTag {
106        Hairline = 0,
107        Fill = 1,
108        Stroke = 2,
109        StrokeAndFill = 3,
110    }
111
112    #[derive(Clone, Copy, Debug)]
113    #[cxx_name = "SkPoint"]
114    struct Point {
115        fX: f32,
116        fY: f32,
117    }
118
119    #[derive(Clone, Copy, Debug)]
120    #[cxx_name = "SkRect"]
121    struct Rect {
122        fLeft: f32,
123        fTop: f32,
124        fRight: f32,
125        fBottom: f32,
126    }
127
128    #[derive(Clone, Copy, Debug)]
129    #[cxx_name = "SkRRect"]
130    struct RRect {
131        fRect: Rect,
132        fRadii: [Point; 4],
133        fType: RRectType,
134    }
135
136    unsafe extern "C++" {
137        include!("include/pathkit_cxx_decl.h");
138
139        #[cxx_name = "SkPath"]
140        #[namespace = "pk"]
141        type Path;
142
143        type PathIterInner;
144        type PathMeasureHolder;
145        type PaintHolder;
146        type StrokeRecHolder;
147        type PathEffectHolder;
148        type OpBuilderHolder;
149        type PathBuilderHolder;
150
151        #[cxx_name = "pk_path_new"]
152        fn path_new() -> UniquePtr<Path>;
153        #[cxx_name = "pk_path_clone"]
154        fn path_clone(p: &Path) -> UniquePtr<Path>;
155        #[cxx_name = "pk_path_reset"]
156        fn path_reset(p: Pin<&mut Path>);
157
158        #[cxx_name = "pk_path_count_points"]
159        fn path_count_points(p: &Path) -> i32;
160        #[cxx_name = "pk_path_count_verbs"]
161        fn path_count_verbs(p: &Path) -> i32;
162        #[cxx_name = "pk_path_get_point"]
163        fn path_get_point(p: &Path, index: i32, out: &mut Point);
164        #[cxx_name = "pk_path_compute_tight_bounds"]
165        fn path_compute_tight_bounds(p: &Path, out: &mut Rect);
166        #[cxx_name = "pk_path_is_last_contour_closed"]
167        fn path_is_last_contour_closed(p: &Path) -> bool;
168        #[cxx_name = "pk_path_conservatively_contains_rect"]
169        fn path_conservatively_contains_rect(p: &Path, r: &Rect) -> bool;
170        #[cxx_name = "pk_path_is_rect"]
171        fn path_is_rect(
172            p: &Path,
173            rect: &mut Rect,
174            is_closed: &mut bool,
175            direction: &mut Direction,
176        ) -> bool;
177        #[cxx_name = "pk_path_contains"]
178        fn path_contains(p: &Path, x: f32, y: f32) -> bool;
179        #[cxx_name = "pk_path_fill_type_bits"]
180        fn path_fill_type_bits(p: &Path) -> PathFillType;
181        #[cxx_name = "pk_path_set_fill_type_bits"]
182        fn path_set_fill_type_bits(p: Pin<&mut Path>, v: PathFillType);
183        #[cxx_name = "pk_path_toggle_inverse_fill_type"]
184        fn path_toggle_inverse_fill_type(p: Pin<&mut Path>);
185
186        #[cxx_name = "pk_path_move_to"]
187        fn path_move_to(p: Pin<&mut Path>, x: f32, y: f32);
188        #[cxx_name = "pk_path_line_to"]
189        fn path_line_to(p: Pin<&mut Path>, x: f32, y: f32);
190        #[cxx_name = "pk_path_quad_to"]
191        fn path_quad_to(p: Pin<&mut Path>, x1: f32, y1: f32, x2: f32, y2: f32);
192        #[cxx_name = "pk_path_cubic_to"]
193        fn path_cubic_to(
194            p: Pin<&mut Path>,
195            x1: f32,
196            y1: f32,
197            x2: f32,
198            y2: f32,
199            x3: f32,
200            y3: f32,
201        );
202        #[cxx_name = "pk_path_close"]
203        fn path_close(p: Pin<&mut Path>);
204
205        #[cxx_name = "pk_path_add_rect"]
206        fn path_add_rect(p: Pin<&mut Path>, rect: &Rect, dir: Direction, start: RectCorner);
207        #[cxx_name = "pk_path_add_oval"]
208        fn path_add_oval(p: Pin<&mut Path>, rect: &Rect, dir: Direction);
209        #[cxx_name = "pk_path_add_circle"]
210        fn path_add_circle(p: Pin<&mut Path>, cx: f32, cy: f32, radius: f32, dir: Direction);
211        #[cxx_name = "pk_path_add_round_rect"]
212        fn path_add_round_rect(
213            p: Pin<&mut Path>,
214            rect: &Rect,
215            rx: f32,
216            ry: f32,
217            dir: Direction,
218        );
219        #[cxx_name = "pk_path_add_rrect"]
220        fn path_add_rrect(p: Pin<&mut Path>, rr: &RRect, dir: Direction);
221        #[cxx_name = "pk_path_add_rrect_start"]
222        fn path_add_rrect_start(
223            p: Pin<&mut Path>,
224            rr: &RRect,
225            dir: Direction,
226            start: RectCorner,
227        );
228        #[cxx_name = "pk_path_is_rrect"]
229        fn path_is_rrect(p: &Path, out: &mut RRect) -> bool;
230
231        #[cxx_name = "pk_path_builder_new"]
232        fn path_builder_new() -> UniquePtr<PathBuilderHolder>;
233        #[cxx_name = "pk_path_builder_reset"]
234        fn path_builder_reset(b: Pin<&mut PathBuilderHolder>);
235        #[cxx_name = "pk_path_builder_fill_type"]
236        fn path_builder_fill_type(b: &PathBuilderHolder) -> PathFillType;
237        #[cxx_name = "pk_path_builder_set_fill_type"]
238        fn path_builder_set_fill_type(b: Pin<&mut PathBuilderHolder>, ft: PathFillType);
239        #[cxx_name = "pk_path_builder_toggle_inverse_fill_type"]
240        fn path_builder_toggle_inverse_fill_type(b: Pin<&mut PathBuilderHolder>);
241        #[cxx_name = "pk_path_builder_snapshot"]
242        fn path_builder_snapshot(b: &PathBuilderHolder) -> UniquePtr<Path>;
243        #[cxx_name = "pk_path_builder_detach"]
244        fn path_builder_detach(b: Pin<&mut PathBuilderHolder>) -> UniquePtr<Path>;
245        #[cxx_name = "pk_path_builder_move_to"]
246        fn path_builder_move_to(b: Pin<&mut PathBuilderHolder>, x: f32, y: f32);
247        #[cxx_name = "pk_path_builder_line_to"]
248        fn path_builder_line_to(b: Pin<&mut PathBuilderHolder>, x: f32, y: f32);
249        #[cxx_name = "pk_path_builder_quad_to"]
250        fn path_builder_quad_to(b: Pin<&mut PathBuilderHolder>, x1: f32, y1: f32, x2: f32, y2: f32);
251        #[cxx_name = "pk_path_builder_cubic_to"]
252        fn path_builder_cubic_to(
253            b: Pin<&mut PathBuilderHolder>,
254            x1: f32,
255            y1: f32,
256            x2: f32,
257            y2: f32,
258            x3: f32,
259            y3: f32,
260        );
261        #[cxx_name = "pk_path_builder_close"]
262        fn path_builder_close(b: Pin<&mut PathBuilderHolder>);
263        #[cxx_name = "pk_path_builder_add_rect"]
264        fn path_builder_add_rect(
265            b: Pin<&mut PathBuilderHolder>,
266            rect: &Rect,
267            dir: Direction,
268            start: RectCorner,
269        );
270        #[cxx_name = "pk_path_builder_add_oval"]
271        fn path_builder_add_oval(b: Pin<&mut PathBuilderHolder>, rect: &Rect, dir: Direction);
272        #[cxx_name = "pk_path_builder_add_circle"]
273        fn path_builder_add_circle(
274            b: Pin<&mut PathBuilderHolder>,
275            cx: f32,
276            cy: f32,
277            radius: f32,
278            dir: Direction,
279        );
280        #[cxx_name = "pk_path_builder_add_round_rect"]
281        fn path_builder_add_round_rect(
282            b: Pin<&mut PathBuilderHolder>,
283            rect: &Rect,
284            rx: f32,
285            ry: f32,
286            dir: Direction,
287        );
288        #[cxx_name = "pk_path_builder_add_rrect"]
289        fn path_builder_add_rrect(b: Pin<&mut PathBuilderHolder>, rr: &RRect, dir: Direction);
290        #[cxx_name = "pk_path_builder_add_rrect_start"]
291        fn path_builder_add_rrect_start(
292            b: Pin<&mut PathBuilderHolder>,
293            rr: &RRect,
294            dir: Direction,
295            start: RectCorner,
296        );
297        #[cxx_name = "pk_path_builder_add_path"]
298        fn path_builder_add_path(b: Pin<&mut PathBuilderHolder>, src: &Path);
299
300        #[cxx_name = "pk_path_iter_new"]
301        fn path_iter_new(path: &Path, force_close: bool) -> UniquePtr<PathIterInner>;
302        #[cxx_name = "pk_path_iter_next"]
303        fn path_iter_next(
304            it: Pin<&mut PathIterInner>,
305            p0: &mut Point,
306            p1: &mut Point,
307            p2: &mut Point,
308            p3: &mut Point,
309        ) -> PathVerb;
310
311        #[cxx_name = "pk_measure_new"]
312        fn measure_new() -> UniquePtr<PathMeasureHolder>;
313        #[cxx_name = "pk_measure_from_path"]
314        fn measure_from_path(path: &Path, force_closed: bool, res_scale: f32)
315            -> UniquePtr<PathMeasureHolder>;
316        #[cxx_name = "pk_measure_set_path"]
317        fn measure_set_path(m: Pin<&mut PathMeasureHolder>, path: &Path, force_closed: bool);
318        #[cxx_name = "pk_measure_length"]
319        fn measure_length(m: Pin<&mut PathMeasureHolder>) -> f32;
320        #[cxx_name = "pk_measure_get_pos_tan"]
321        fn measure_get_pos_tan(
322            m: Pin<&mut PathMeasureHolder>,
323            distance: f32,
324            position: &mut Point,
325            tangent: &mut Point,
326        ) -> bool;
327        #[cxx_name = "pk_measure_get_segment"]
328        fn measure_get_segment(
329            m: Pin<&mut PathMeasureHolder>,
330            start_d: f32,
331            stop_d: f32,
332            dst: Pin<&mut Path>,
333            start_with_move_to: bool,
334        ) -> bool;
335        #[cxx_name = "pk_measure_is_closed"]
336        fn measure_is_closed(m: Pin<&mut PathMeasureHolder>) -> bool;
337        #[cxx_name = "pk_measure_next_contour"]
338        fn measure_next_contour(m: Pin<&mut PathMeasureHolder>) -> bool;
339
340        #[cxx_name = "pk_paint_new"]
341        fn paint_new() -> UniquePtr<PaintHolder>;
342        #[cxx_name = "pk_paint_clone"]
343        fn paint_clone(p: &PaintHolder) -> UniquePtr<PaintHolder>;
344        #[cxx_name = "pk_paint_set_fill"]
345        fn paint_set_fill(p: Pin<&mut PaintHolder>);
346        #[cxx_name = "pk_paint_set_stroke"]
347        fn paint_set_stroke(p: Pin<&mut PaintHolder>, enable: bool);
348        #[cxx_name = "pk_paint_set_style"]
349        fn paint_set_style(p: Pin<&mut PaintHolder>, style: PaintStyle);
350        #[cxx_name = "pk_paint_set_stroke_width"]
351        fn paint_set_stroke_width(p: Pin<&mut PaintHolder>, width: f32);
352        #[cxx_name = "pk_paint_set_stroke_miter"]
353        fn paint_set_stroke_miter(p: Pin<&mut PaintHolder>, miter: f32);
354        #[cxx_name = "pk_paint_set_stroke_cap"]
355        fn paint_set_stroke_cap(p: Pin<&mut PaintHolder>, cap: StrokeCap);
356        #[cxx_name = "pk_paint_set_stroke_join"]
357        fn paint_set_stroke_join(p: Pin<&mut PaintHolder>, join: StrokeJoin);
358        #[cxx_name = "pk_paint_get_fill_path"]
359        fn paint_get_fill_path(
360            p: &PaintHolder,
361            src: &Path,
362            dst: Pin<&mut Path>,
363        ) -> bool;
364
365        #[cxx_name = "pk_stroke_rec_new"]
366        fn stroke_rec_new(init: StrokeRecInit) -> UniquePtr<StrokeRecHolder>;
367        #[cxx_name = "pk_stroke_rec_set_fill"]
368        fn stroke_rec_set_fill(p: Pin<&mut StrokeRecHolder>);
369        #[cxx_name = "pk_stroke_rec_set_hairline"]
370        fn stroke_rec_set_hairline(p: Pin<&mut StrokeRecHolder>);
371        #[cxx_name = "pk_stroke_rec_set_stroke_style"]
372        fn stroke_rec_set_stroke_style(p: Pin<&mut StrokeRecHolder>, width: f32, saf: bool);
373        #[cxx_name = "pk_stroke_rec_get_style"]
374        fn stroke_rec_get_style(p: &StrokeRecHolder) -> StrokeRecStyleTag;
375        #[cxx_name = "pk_stroke_rec_width"]
376        fn stroke_rec_width(p: &StrokeRecHolder) -> f32;
377        #[cxx_name = "pk_stroke_rec_cap"]
378        fn stroke_rec_cap(p: &StrokeRecHolder) -> StrokeCap;
379        #[cxx_name = "pk_stroke_rec_set_cap"]
380        fn stroke_rec_set_cap(p: Pin<&mut StrokeRecHolder>, cap: StrokeCap);
381        #[cxx_name = "pk_stroke_rec_join"]
382        fn stroke_rec_join(p: &StrokeRecHolder) -> StrokeJoin;
383        #[cxx_name = "pk_stroke_rec_set_join"]
384        fn stroke_rec_set_join(p: Pin<&mut StrokeRecHolder>, join: StrokeJoin);
385        #[cxx_name = "pk_stroke_rec_miter_limit"]
386        fn stroke_rec_miter_limit(p: &StrokeRecHolder) -> f32;
387        #[cxx_name = "pk_stroke_rec_set_stroke_params"]
388        fn stroke_rec_set_stroke_params(
389            p: Pin<&mut StrokeRecHolder>,
390            cap: StrokeCap,
391            join: StrokeJoin,
392            miter_limit: f32,
393        );
394        #[cxx_name = "pk_stroke_rec_inflation_radius"]
395        fn stroke_rec_inflation_radius(p: &StrokeRecHolder) -> f32;
396        #[cxx_name = "pk_stroke_rec_apply_to_path"]
397        fn stroke_rec_apply_to_path(
398            p: &StrokeRecHolder,
399            dst: Pin<&mut Path>,
400            src: &Path,
401        ) -> bool;
402
403        #[cxx_name = "pk_op"]
404        fn boolean_path_op(one: &Path, two: &Path, op: PathOp, result: Pin<&mut Path>) -> bool;
405
406        #[cxx_name = "pk_op_builder_new"]
407        fn op_builder_new() -> UniquePtr<OpBuilderHolder>;
408        #[cxx_name = "pk_op_builder_add"]
409        fn op_builder_add(h: Pin<&mut OpBuilderHolder>, path: &Path, op: PathOp);
410        #[cxx_name = "pk_op_builder_resolve"]
411        fn op_builder_resolve(h: Pin<&mut OpBuilderHolder>, result: Pin<&mut Path>) -> bool;
412
413        #[cxx_name = "pk_simplify"]
414        fn simplify_path(path: &Path, result: Pin<&mut Path>) -> bool;
415        #[cxx_name = "pk_tight_bounds"]
416        fn pathops_tight_bounds(path: &Path, out: &mut Rect) -> bool;
417
418        #[cxx_name = "pk_rrect_new_empty"]
419        fn rrect_new_empty(out: &mut RRect);
420        #[cxx_name = "pk_rrect_set_rect_xy"]
421        fn rrect_set_rect_xy(rr: &mut RRect, rect: &Rect, rx: f32, ry: f32);
422        #[cxx_name = "pk_rrect_set_oval"]
423        fn rrect_set_oval(rr: &mut RRect, rect: &Rect);
424        #[cxx_name = "pk_rrect_set_rect_radii"]
425        fn rrect_set_rect_radii(rr: &mut RRect, rect: &Rect, radii: &[Point]);
426        #[cxx_name = "pk_rrect_is_valid"]
427        fn rrect_is_valid(rr: &RRect) -> bool;
428
429        #[cxx_name = "pk_corner_effect_make"]
430        fn corner_effect_make(radius: f32) -> UniquePtr<PathEffectHolder>;
431        #[cxx_name = "pk_dash_effect_make"]
432        fn dash_effect_make(intervals: &[f32], phase: f32) -> UniquePtr<PathEffectHolder>;
433        #[cxx_name = "pk_path_effect_filter"]
434        fn path_effect_filter(
435            e: &PathEffectHolder,
436            dst: Pin<&mut Path>,
437            src: &Path,
438            rec: Pin<&mut StrokeRecHolder>,
439            cull: &Rect,
440        ) -> bool;
441    }
442}