1use crate::path::Path;
4use crate::point::Point;
5use crate::pathkit;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum PathVerb {
10 Move,
12 Line,
14 Quad,
16 Conic,
18 Cubic,
20 Close,
22 Done,
24}
25
26#[derive(Debug, Clone)]
28pub enum PathVerbItem {
29 Move(Point),
31 Line(Point, Point),
33 Quad(Point, Point),
35 Conic(Point, Point, f32),
37 Cubic(Point, Point, Point),
39 Close,
41}
42
43impl PathVerbItem {
44 pub fn verb(&self) -> PathVerb {
46 match self {
47 PathVerbItem::Move(_) => PathVerb::Move,
48 PathVerbItem::Line(_, _) => PathVerb::Line,
49 PathVerbItem::Quad(_, _) => PathVerb::Quad,
50 PathVerbItem::Conic(_, _, _) => PathVerb::Conic,
51 PathVerbItem::Cubic(_, _, _) => PathVerb::Cubic,
52 PathVerbItem::Close => PathVerb::Close,
53 }
54 }
55}
56
57pub struct PathIter<'a> {
59 _path: &'a Path,
60 inner: pathkit::SkPath_Iter,
61 pts: [pathkit::SkPoint; 4],
62}
63
64impl<'a> PathIter<'a> {
65 pub(crate) fn new(path: &'a Path, force_close: bool) -> Self {
66 let inner = unsafe {
67 pathkit::SkPath_Iter::new1(path.as_raw() as *const _, force_close)
68 };
69 let pts = [
70 pathkit::SkPoint { fX: 0.0, fY: 0.0 },
71 pathkit::SkPoint { fX: 0.0, fY: 0.0 },
72 pathkit::SkPoint { fX: 0.0, fY: 0.0 },
73 pathkit::SkPoint { fX: 0.0, fY: 0.0 },
74 ];
75 Self {
76 _path: path,
77 inner,
78 pts,
79 }
80 }
81}
82
83impl<'a> Iterator for PathIter<'a> {
84 type Item = PathVerbItem;
85
86 fn next(&mut self) -> Option<Self::Item> {
87 let verb = unsafe {
88 self.inner.next(self.pts.as_mut_ptr())
89 };
90 let v = verb as u32;
91 let item = match v {
92 pathkit::SkPath_Verb::kMove_Verb => PathVerbItem::Move(self.pts[0].into()),
93 pathkit::SkPath_Verb::kLine_Verb => {
94 PathVerbItem::Line(self.pts[0].into(), self.pts[1].into())
95 }
96 pathkit::SkPath_Verb::kQuad_Verb => {
97 PathVerbItem::Quad(self.pts[1].into(), self.pts[2].into())
98 }
99 pathkit::SkPath_Verb::kConic_Verb => {
100 PathVerbItem::Conic(self.pts[1].into(), self.pts[2].into(), 1.0)
103 }
104 pathkit::SkPath_Verb::kCubic_Verb => PathVerbItem::Cubic(
105 self.pts[1].into(),
106 self.pts[2].into(),
107 self.pts[3].into(),
108 ),
109 pathkit::SkPath_Verb::kClose_Verb => PathVerbItem::Close,
110 pathkit::SkPath_Verb::kDone_Verb => return None,
111 _ => return None,
112 };
113 Some(item)
114 }
115}