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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
mod parser;
mod segment;
mod writer;
mod builder;
pub use self::parser::*;
pub use self::segment::*;
pub use self::builder::*;
#[derive(Clone, PartialEq)]
pub struct Path(pub Vec<PathSegment>);
impl Path {
pub fn new() -> Self {
Path(Vec::new())
}
pub fn with_capacity(capacity: usize) -> Self {
Path(Vec::with_capacity(capacity))
}
pub fn conv_to_absolute(&mut self) {
let mut prev_x = 0.0;
let mut prev_y = 0.0;
let mut prev_mx = 0.0;
let mut prev_my = 0.0;
let mut prev_cmd = PathCommand::MoveTo;
for seg in self.iter_mut() {
if seg.cmd() == PathCommand::ClosePath {
prev_x = prev_mx;
prev_y = prev_my;
seg.set_absolute(true);
continue;
}
let offset_x;
let offset_y;
if seg.is_relative() {
if seg.cmd() == PathCommand::MoveTo && prev_cmd == PathCommand::ClosePath {
offset_x = prev_mx;
offset_y = prev_my;
} else {
offset_x = prev_x;
offset_y = prev_y;
}
} else {
offset_x = 0.0;
offset_y = 0.0;
}
if seg.is_relative() {
shift_segment_data(seg, offset_x, offset_y);
}
if seg.cmd() == PathCommand::MoveTo {
prev_mx = seg.x().unwrap();
prev_my = seg.y().unwrap();
}
seg.set_absolute(true);
if seg.cmd() == PathCommand::HorizontalLineTo {
prev_x = seg.x().unwrap();
} else if seg.cmd() == PathCommand::VerticalLineTo {
prev_y = seg.y().unwrap();
} else {
prev_x = seg.x().unwrap();
prev_y = seg.y().unwrap();
}
prev_cmd = seg.cmd();
}
}
pub fn conv_to_relative(&mut self) {
let mut prev_x = 0.0;
let mut prev_y = 0.0;
let mut prev_mx = 0.0;
let mut prev_my = 0.0;
let mut prev_cmd = PathCommand::MoveTo;
for seg in self.iter_mut() {
if seg.cmd() == PathCommand::ClosePath {
prev_x = prev_mx;
prev_y = prev_my;
seg.set_absolute(false);
continue;
}
let offset_x;
let offset_y;
if seg.is_absolute() {
if seg.cmd() == PathCommand::MoveTo && prev_cmd == PathCommand::ClosePath {
offset_x = prev_mx;
offset_y = prev_my;
} else {
offset_x = prev_x;
offset_y = prev_y;
}
} else {
offset_x = 0.0;
offset_y = 0.0;
}
if seg.is_absolute() {
if seg.cmd() == PathCommand::HorizontalLineTo {
prev_x = seg.x().unwrap();
} else if seg.cmd() == PathCommand::VerticalLineTo {
prev_y = seg.y().unwrap();
} else {
prev_x = seg.x().unwrap();
prev_y = seg.y().unwrap();
}
} else {
if seg.cmd() == PathCommand::HorizontalLineTo {
prev_x += seg.x().unwrap();
} else if seg.cmd() == PathCommand::VerticalLineTo {
prev_y += seg.y().unwrap();
} else {
prev_x += seg.x().unwrap();
prev_y += seg.y().unwrap();
}
}
if seg.cmd() == PathCommand::MoveTo {
if seg.is_absolute() {
prev_mx = seg.x().unwrap();
prev_my = seg.y().unwrap();
} else {
prev_mx += seg.x().unwrap();
prev_my += seg.y().unwrap();
}
}
if seg.is_absolute() {
shift_segment_data(seg, -offset_x, -offset_y);
}
seg.set_absolute(false);
prev_cmd = seg.cmd();
}
}
}
fn shift_segment_data(d: &mut PathSegment, offset_x: f64, offset_y: f64) {
match *d {
PathSegment::MoveTo { ref mut x, ref mut y, .. } => {
*x += offset_x;
*y += offset_y;
}
PathSegment::LineTo { ref mut x, ref mut y, .. } => {
*x += offset_x;
*y += offset_y;
}
PathSegment::HorizontalLineTo { ref mut x, .. } => {
*x += offset_x;
}
PathSegment::VerticalLineTo { ref mut y, .. } => {
*y += offset_y;
}
PathSegment::CurveTo { ref mut x1, ref mut y1, ref mut x2, ref mut y2,
ref mut x, ref mut y, .. } => {
*x1 += offset_x;
*y1 += offset_y;
*x2 += offset_x;
*y2 += offset_y;
*x += offset_x;
*y += offset_y;
}
PathSegment::SmoothCurveTo { ref mut x2, ref mut y2, ref mut x, ref mut y, .. } => {
*x2 += offset_x;
*y2 += offset_y;
*x += offset_x;
*y += offset_y;
}
PathSegment::Quadratic { ref mut x1, ref mut y1, ref mut x, ref mut y, .. } => {
*x1 += offset_x;
*y1 += offset_y;
*x += offset_x;
*y += offset_y;
}
PathSegment::SmoothQuadratic { ref mut x, ref mut y, .. } => {
*x += offset_x;
*y += offset_y;
}
PathSegment::EllipticalArc { ref mut x, ref mut y, .. } => {
*x += offset_x;
*y += offset_y;
}
PathSegment::ClosePath { .. } => {}
}
}
impl_from_vec!(Path, Path, PathSegment);
impl_vec_defer!(Path, PathSegment);
#[cfg(test)]
mod to_absolute {
use std::str::FromStr;
use super::*;
macro_rules! test {
($name:ident, $in_text:expr, $out_text:expr) => (
#[test]
fn $name() {
let mut path = Path::from_str($in_text).unwrap();
path.conv_to_absolute();
assert_eq!(path.to_string(), $out_text);
}
)
}
test!(line_to,
"m 10 20 l 20 20",
"M 10 20 L 30 40");
test!(close_path,
"m 10 20 l 20 20 z",
"M 10 20 L 30 40 Z");
test!(implicit_line_to,
"m 10 20 20 20",
"M 10 20 L 30 40");
test!(hline_vline,
"m 10 20 v 10 h 10 l 10 10",
"M 10 20 V 30 H 20 L 30 40");
test!(curve,
"m 10 20 c 10 10 10 10 10 10",
"M 10 20 C 20 30 20 30 20 30");
test!(move_to_1,
"m 10 20 l 10 10 m 10 10 l 10 10",
"M 10 20 L 20 30 M 30 40 L 40 50");
test!(move_to_2,
"m 10 20 l 10 10 z m 10 10 l 10 10",
"M 10 20 L 20 30 Z M 20 30 L 30 40");
test!(move_to_3,
"m 10 20 l 10 10 Z m 10 10 l 10 10",
"M 10 20 L 20 30 Z M 20 30 L 30 40");
test!(move_to_4,
"m 10 20 l 10 10 Z l 10 10",
"M 10 20 L 20 30 Z L 20 30");
test!(smooth_curve,
"m 10 20 s 10 10 10 10",
"M 10 20 S 20 30 20 30");
test!(quad,
"m 10 20 q 10 10 10 10",
"M 10 20 Q 20 30 20 30");
test!(arc_mixed,
"M 30 150 a 40 40 0 0 1 65 50 Z m 30 30 A 20 20 0 0 0 125 230 Z \
m 40 24 a 20 20 0 0 1 65 50 z",
"M 30 150 A 40 40 0 0 1 95 200 Z M 60 180 A 20 20 0 0 0 125 230 Z \
M 100 204 A 20 20 0 0 1 165 254 Z");
}
#[cfg(test)]
mod to_relative {
use std::str::FromStr;
use super::*;
macro_rules! test {
($name:ident, $in_text:expr, $out_text:expr) => (
#[test]
fn $name() {
let mut path = Path::from_str($in_text).unwrap();
path.conv_to_relative();
assert_eq!(path.to_string(), $out_text);
}
)
}
test!(line_to,
"M 10 20 L 30 40",
"m 10 20 l 20 20");
test!(close_path,
"M 10 20 L 30 40 Z",
"m 10 20 l 20 20 z");
test!(implicit_line_to,
"M 10 20 30 40",
"m 10 20 l 20 20");
test!(hline_vline,
"M 10 20 V 30 H 20 L 30 40",
"m 10 20 v 10 h 10 l 10 10");
test!(curve,
"M 10 20 C 20 30 20 30 20 30",
"m 10 20 c 10 10 10 10 10 10");
test!(move_to_1,
"M 10 20 L 20 30 M 30 40 L 40 50",
"m 10 20 l 10 10 m 10 10 l 10 10");
test!(move_to_2,
"M 10 20 L 20 30 Z M 20 30 L 30 40",
"m 10 20 l 10 10 z m 10 10 l 10 10");
test!(move_to_3,
"M 10 20 L 20 30 z M 20 30 L 30 40",
"m 10 20 l 10 10 z m 10 10 l 10 10");
test!(move_to_4,
"M 10 20 L 20 30 Z L 20 30",
"m 10 20 l 10 10 z l 10 10");
test!(smooth_curve,
"M 10 20 S 20 30 20 30",
"m 10 20 s 10 10 10 10");
test!(quad,
"M 10 20 Q 20 30 20 30",
"m 10 20 q 10 10 10 10");
test!(arc_mixed,
"M 30 150 a 40 40 0 0 1 65 50 Z m 30 30 A 20 20 0 0 0 125 230 Z \
m 40 24 a 20 20 0 0 1 65 50 z",
"m 30 150 a 40 40 0 0 1 65 50 z m 30 30 a 20 20 0 0 0 65 50 z \
m 40 24 a 20 20 0 0 1 65 50 z");
}