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
#[derive(Clone,Copy,Debug)]
pub enum FillRule {
NonZero,
EvenOdd,
}
pub enum PathOp {
Close(),
Move(f32, f32),
Line(f32, f32),
Quad(f32, f32, f32, f32),
Cubic(f32, f32, f32, f32, f32, f32),
PenWidth(f32),
}
pub struct Path2D {
ops : Vec<PathOp>,
}
pub struct IterPath2D<'a> {
path : &'a Path2D,
pos : usize,
}
pub struct PathBuilder {
ops : Vec<PathOp>,
absolute : bool,
pen_x : f32,
pen_y : f32,
}
impl Path2D {
pub fn iter(&self) -> IterPath2D {
IterPath2D {
path : self,
pos : 0,
}
}
}
impl<'a> IntoIterator for &'a Path2D {
type Item = &'a PathOp;
type IntoIter = IterPath2D<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a> Iterator for IterPath2D<'a> {
type Item = &'a PathOp;
fn next(&mut self) -> Option<Self::Item> {
let p = self.pos;
self.pos += 1;
self.path.ops.get(p)
}
}
impl PathBuilder {
pub fn new() -> PathBuilder {
let ops = Vec::with_capacity(32);
PathBuilder {
ops,
absolute : false,
pen_x : 0f32,
pen_y : 0f32,
}
}
pub fn absolute(mut self) -> Self {
self.absolute = true;
self
}
pub fn relative(mut self) -> Self {
self.absolute = false;
self
}
fn pt(&self, x: f32, y: f32) -> (f32, f32) {
if self.absolute {
(x, y)
} else {
(self.pen_x + x, self.pen_y + y)
}
}
pub fn close(mut self) -> Self {
self.ops.push(PathOp::Close());
self.pen_x = 0f32;
self.pen_y = 0f32;
self
}
pub fn move_to(mut self, bx: f32, by: f32) -> Self {
let (abx, aby) = self.pt(bx, by);
self.ops.push(PathOp::Move(abx, aby));
self.pen_x = abx;
self.pen_y = aby;
self
}
pub fn line_to(mut self, bx: f32, by: f32) -> Self {
let (abx, aby) = self.pt(bx, by);
self.ops.push(PathOp::Line(abx, aby));
self.pen_x = abx;
self.pen_y = aby;
self
}
pub fn quad_to(mut self, bx: f32, by: f32, cx: f32, cy: f32) -> Self {
let (abx, aby) = self.pt(bx, by);
let (acx, acy) = self.pt(cx, cy);
self.ops.push(PathOp::Quad(abx, aby, acx, acy));
self.pen_x = acx;
self.pen_y = acy;
self
}
pub fn cubic_to(mut self, bx: f32, by: f32, cx: f32, cy: f32, dx: f32,
dy: f32) -> Self
{
let (abx, aby) = self.pt(bx, by);
let (acx, acy) = self.pt(cx, cy);
let (adx, ady) = self.pt(dx, dy);
self.ops.push(PathOp::Cubic(abx, aby, acx, acy, adx, ady));
self.pen_x = adx;
self.pen_y = ady;
self
}
pub fn pen_width(mut self, width: f32) -> Self {
self.ops.push(PathOp::PenWidth(width));
self
}
pub fn build(self) -> Path2D {
Path2D {
ops : self.ops,
}
}
}