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
//! Path commands.

use super::geometry::{Point, Transform};
use super::path_builder::PathBuilder;

use core::borrow::Borrow;

/// Path command.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Command {
    /// Begins a new subpath at the specified point.
    MoveTo(Point),
    /// A straight line from the previous point to the specified point.
    LineTo(Point),
    /// A cubic bezier curve from the previous point to the final point with
    /// two intermediate control points.
    CurveTo(Point, Point, Point),
    /// A quadratic curve from the previous point to the final point with one
    /// intermediate control point.
    QuadTo(Point, Point),
    /// Closes a subpath, connecting the final point to the initial point.
    Close,
}

impl Command {
    /// Returns the associated verb for the command.
    pub fn verb(&self) -> Verb {
        use Command::*;
        match self {
            MoveTo(..) => Verb::MoveTo,
            LineTo(..) => Verb::LineTo,
            QuadTo(..) => Verb::QuadTo,
            CurveTo(..) => Verb::CurveTo,
            Close => Verb::CurveTo,
        }
    }

    /// Returns the result of a transformation matrix applied to the command.
    #[inline]
    pub fn transform(&self, transform: &Transform) -> Self {
        use Command::*;
        let t = transform;
        match self {
            MoveTo(p) => MoveTo(t.transform_point(*p)),
            LineTo(p) => LineTo(t.transform_point(*p)),
            QuadTo(c, p) => QuadTo(t.transform_point(*c), t.transform_point(*p)),
            CurveTo(c1, c2, p) => CurveTo(
                t.transform_point(*c1),
                t.transform_point(*c2),
                t.transform_point(*p),
            ),
            Close => Close,
        }
    }
}

/// Action of a path command.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Verb {
    MoveTo,
    LineTo,
    CurveTo,
    QuadTo,
    Close,
}

#[derive(Clone)]
pub struct PointsCommands<'a> {
    points: &'a [Point],
    verbs: &'a [Verb],
    point: usize,
    verb: usize,
}

impl<'a> PointsCommands<'a> {
    pub(super) fn new(points: &'a [Point], verbs: &'a [Verb]) -> Self {
        Self {
            points,
            verbs,
            point: 0,
            verb: 0,
        }
    }

    #[inline(always)]
    pub(super) fn copy_to(&self, sink: &mut impl PathBuilder) {
        self.copy_to_inner(sink);
    }

    #[inline(always)]
    fn copy_to_inner(&self, sink: &mut impl PathBuilder) -> Option<()> {
        let mut i = 0;
        for verb in self.verbs {
            match verb {
                Verb::MoveTo => {
                    let p = self.points.get(i)?;
                    i += 1;
                    sink.move_to(*p);
                }
                Verb::LineTo => {
                    let p = self.points.get(i)?;
                    i += 1;
                    sink.line_to(*p);
                }
                Verb::QuadTo => {
                    let p = self.points.get(i + 1)?;
                    let c = self.points.get(i)?;
                    i += 2;
                    sink.quad_to(*c, *p);
                }
                Verb::CurveTo => {
                    let p = self.points.get(i + 2)?;
                    let c2 = self.points.get(i + 1)?;
                    let c1 = self.points.get(i)?;
                    i += 3;
                    sink.curve_to(*c1, *c2, *p);
                }
                Verb::Close => {
                    sink.close();
                }
            }
        }
        Some(())
    }
}

impl<'a> Iterator for PointsCommands<'a> {
    type Item = Command;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        use Command::*;
        let verb = self.verbs.get(self.verb)?;
        self.verb += 1;
        Some(match verb {
            Verb::MoveTo => {
                let p = self.points.get(self.point)?;
                self.point += 1;
                MoveTo(*p)
            }
            Verb::LineTo => {
                let p = self.points.get(self.point)?;
                self.point += 1;
                LineTo(*p)
            }
            Verb::QuadTo => {
                let p = self.points.get(self.point..self.point + 2)?;
                self.point += 2;
                QuadTo(p[0], p[1])
            }
            Verb::CurveTo => {
                let p = self.points.get(self.point..self.point + 3)?;
                self.point += 3;
                CurveTo(p[0], p[1], p[2])
            }
            Verb::Close => Close,
        })
    }
}

#[derive(Clone)]
pub struct TransformCommands<D> {
    pub data: D,
    pub transform: Transform,
}

impl<D> Iterator for TransformCommands<D>
where
    D: Iterator + Clone,
    D::Item: Borrow<Command>,
{
    type Item = Command;

    fn next(&mut self) -> Option<Self::Item> {
        Some(self.data.next()?.borrow().transform(&self.transform))
    }
}