script/svgpath/
path.rs

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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2021 the SVG Types Authors
// Copyright 2025 the Servo Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use canvas_traits::canvas::PathSegment;

use crate::svgpath::{Error, Stream};

pub struct PathParser<'a> {
    stream: Stream<'a>,
    state: State,
    last_cmd: u8,
}

impl<'a> PathParser<'a> {
    pub fn new(string: &'a str) -> Self {
        Self {
            stream: Stream::from(string),
            state: State::default(),
            last_cmd: b' ',
        }
    }
}

impl Iterator for PathParser<'_> {
    type Item = Result<PathSegment, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        self.stream.skip_spaces();

        let Ok(curr_byte) = self.stream.curr_byte() else {
            return None;
        };

        let cmd = if self.last_cmd == b' ' {
            if let move_to @ (b'm' | b'M') = curr_byte {
                self.stream.advance(1);
                move_to
            } else {
                return Some(Err(Error));
            }
        } else if curr_byte.is_ascii_alphabetic() {
            self.stream.advance(1);
            curr_byte
        } else {
            match self.last_cmd {
                b'm' => b'l',
                b'M' => b'L',
                b'z' | b'Z' => return Some(Err(Error)),
                cmd => cmd,
            }
        };

        self.last_cmd = cmd;
        Some(to_point(&mut self.stream, cmd, &mut self.state))
    }
}

#[derive(Default)]
pub struct State {
    start: (f32, f32),
    pos: (f32, f32),
    quad: (f32, f32),
    cubic: (f32, f32),
}

pub fn to_point(s: &mut Stream<'_>, cmd: u8, state: &mut State) -> Result<PathSegment, Error> {
    let abs = cmd.is_ascii_uppercase();
    let cmd = cmd.to_ascii_lowercase();
    let (dx, dy) = if abs { (0., 0.) } else { state.pos };
    let seg = match cmd {
        b'm' => PathSegment::MoveTo {
            x: s.parse_list_number()? + dx,
            y: s.parse_list_number()? + dy,
        },
        b'l' => PathSegment::LineTo {
            x: s.parse_list_number()? + dx,
            y: s.parse_list_number()? + dy,
        },
        b'h' => PathSegment::LineTo {
            x: s.parse_list_number()? + dx,
            y: state.pos.1,
        },
        b'v' => PathSegment::LineTo {
            x: state.pos.0,
            y: s.parse_list_number()? + dy,
        },
        b'c' => PathSegment::Bezier {
            cp1x: s.parse_list_number()? + dx,
            cp1y: s.parse_list_number()? + dy,
            cp2x: s.parse_list_number()? + dx,
            cp2y: s.parse_list_number()? + dy,
            x: s.parse_list_number()? + dx,
            y: s.parse_list_number()? + dy,
        },
        b's' => PathSegment::Bezier {
            cp1x: state.cubic.0,
            cp1y: state.cubic.1,
            cp2x: s.parse_list_number()? + dx,
            cp2y: s.parse_list_number()? + dy,
            x: s.parse_list_number()? + dx,
            y: s.parse_list_number()? + dy,
        },
        b'q' => PathSegment::Quadratic {
            cpx: s.parse_list_number()? + dx,
            cpy: s.parse_list_number()? + dy,
            x: s.parse_list_number()? + dx,
            y: s.parse_list_number()? + dy,
        },
        b't' => PathSegment::Quadratic {
            cpx: state.quad.0,
            cpy: state.quad.1,
            x: s.parse_list_number()? + dx,
            y: s.parse_list_number()? + dy,
        },
        b'a' => PathSegment::SvgArc {
            radius_x: s.parse_list_number()?,
            radius_y: s.parse_list_number()?,
            rotation: s.parse_list_number()?,
            large_arc: s.parse_flag()?,
            sweep: s.parse_flag()?,
            x: s.parse_list_number()? + dx,
            y: s.parse_list_number()? + dy,
        },
        b'z' => PathSegment::ClosePath,
        _ => return Err(crate::svgpath::Error),
    };

    match seg {
        PathSegment::MoveTo { x, y } => {
            state.start = (x, y);
            state.pos = (x, y);
            state.quad = (x, y);
            state.cubic = (x, y);
        },
        PathSegment::LineTo { x, y } | PathSegment::SvgArc { x, y, .. } => {
            state.pos = (x, y);
            state.quad = (x, y);
            state.cubic = (x, y);
        },
        PathSegment::Bezier {
            cp2x, cp2y, x, y, ..
        } => {
            state.pos = (x, y);
            state.quad = (x, y);
            state.cubic = (x * 2.0 - cp2x, y * 2.0 - cp2y);
        },
        PathSegment::Quadratic { cpx, cpy, x, y, .. } => {
            state.pos = (x, y);
            state.quad = (x * 2.0 - cpx, y * 2.0 - cpy);
            state.cubic = (x, y);
        },
        PathSegment::ClosePath => {
            state.pos = state.start;
            state.quad = state.start;
            state.cubic = state.start;
        },
        _ => {},
    }

    Ok(seg)
}

#[rustfmt::skip]
#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! test {
        ($name:ident, $text:expr, $( $seg:expr ),*) => (
            #[test]
            fn $name() {
                let mut s = PathParser::new($text);
                $(
                    assert_eq!(s.next().unwrap().unwrap(), $seg);
                )*

                if let Some(res) = s.next() {
                    assert!(res.is_err());
                }
            }
        )
    }

    test!(null, "", );
    test!(not_a_path, "q", );
    test!(not_a_move_to, "L 20 30", );
    test!(stop_on_err_1, "M 10 20 L 30 40 L 50",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 30.0, y: 40.0 }
    );

    test!(move_to_1, "M 10 20", PathSegment::MoveTo { x: 10.0, y: 20.0 });
    test!(move_to_2, "m 10 20", PathSegment::MoveTo { x: 10.0, y: 20.0 });
    test!(move_to_3, "M 10 20 30 40 50 60",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 30.0, y: 40.0 },
        PathSegment::LineTo { x: 50.0, y: 60.0 }
    );
    test!(move_to_4, "M 10 20 30 40 50 60 M 70 80 90 100 110 120",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 30.0, y: 40.0 },
        PathSegment::LineTo { x: 50.0, y: 60.0 },
        PathSegment::MoveTo { x: 70.0, y: 80.0 },
        PathSegment::LineTo { x: 90.0, y: 100.0 },
        PathSegment::LineTo { x: 110.0, y: 120.0 }
    );

    test!(arc_to_1, "M 10 20 A 5 5 30 1 1 20 20",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::SvgArc {
            radius_x: 5.0, radius_y: 5.0,
            rotation: 30.0,
            large_arc: true, sweep: true,
            x: 20.0, y: 20.0
        }
    );

    test!(arc_to_2, "M 10 20 a 5 5 30 0 0 20 20",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::SvgArc {
            radius_x: 5.0, radius_y: 5.0,
            rotation: 30.0,
            large_arc: false, sweep: false,
            x: 30.0, y: 40.0
        }
    );

    test!(arc_to_10, "M10-20A5.5.3-4 010-.1",
        PathSegment::MoveTo { x: 10.0, y: -20.0 },
        PathSegment::SvgArc {
            radius_x: 5.5, radius_y: 0.3,
            rotation: -4.0,
            large_arc: false, sweep: true,
            x: 0.0, y: -0.1
        }
    );

    test!(separator_1, "M 10 20 L 5 15 C 10 20 30 40 50 60",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 5.0, y: 15.0 },
        PathSegment::Bezier {
            cp1x: 10.0, cp1y: 20.0,
            cp2x: 30.0, cp2y: 40.0,
            x:    50.0, y:    60.0,
        }
    );

    test!(separator_2, "M 10, 20 L 5, 15 C 10, 20 30, 40 50, 60",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 5.0, y: 15.0 },
        PathSegment::Bezier {
            cp1x: 10.0, cp1y: 20.0,
            cp2x: 30.0, cp2y: 40.0,
            x:    50.0, y:    60.0,
        }
    );

    test!(separator_3, "M 10,20 L 5,15 C 10,20 30,40 50,60",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 5.0, y: 15.0 },
        PathSegment::Bezier {
            cp1x: 10.0, cp1y: 20.0,
            cp2x: 30.0, cp2y: 40.0,
            x:    50.0, y:    60.0,
        }
    );

    test!(separator_4, "M10, 20 L5, 15 C10, 20 30 40 50 60",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 5.0, y: 15.0 },
        PathSegment::Bezier {
            cp1x: 10.0, cp1y: 20.0,
            cp2x: 30.0, cp2y: 40.0,
            x:    50.0, y:    60.0,
        }
    );

    test!(separator_5, "M10 20V30H40V50H60Z",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 10.0, y: 30.0 },
        PathSegment::LineTo { x: 40.0, y: 30.0 },
        PathSegment::LineTo { x: 40.0, y: 50.0 },
        PathSegment::LineTo { x: 60.0, y: 50.0 },
        PathSegment::ClosePath
    );

    test!(all_segments_1, "M 10 20 L 30 40 H 50 V 60 C 70 80 90 100 110 120 S 130 140 150 160
        Q 170 180 190 200 T 210 220 A 50 50 30 1 1 230 240 Z",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 30.0, y: 40.0 },
        PathSegment::LineTo { x: 50.0, y: 40.0 },
        PathSegment::LineTo { x: 50.0, y: 60.0 },
        PathSegment::Bezier {
            cp1x:  70.0, cp1y:  80.0,
            cp2x:  90.0, cp2y: 100.0,
            x:    110.0, y:    120.0,
        },
        PathSegment::Bezier {
            cp1x: 130.0, cp1y: 140.0,
            cp2x: 130.0, cp2y: 140.0,
            x:    150.0, y:    160.0,
        },
        PathSegment::Quadratic {
            cpx: 170.0, cpy: 180.0,
            x:   190.0, y:   200.0,
        },
        PathSegment::Quadratic {
            cpx: 210.0, cpy: 220.0,
            x:   210.0, y:   220.0,
        },
        PathSegment::SvgArc {
            radius_x: 50.0, radius_y: 50.0,
            rotation: 30.0,
            large_arc: true, sweep: true,
            x: 230.0, y: 240.0
        },
        PathSegment::ClosePath
    );

    test!(all_segments_2, "m 10 20 l 30 40 h 50 v 60 c 70 80 90 100 110 120 s 130 140 150 160
        q 170 180 190 200 t 210 220 a 50 50 30 1 1 230 240 z",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 40.0, y: 60.0 },
        PathSegment::LineTo { x: 90.0, y: 60.0 },
        PathSegment::LineTo { x: 90.0, y: 120.0 },
        PathSegment::Bezier {
            cp1x: 160.0, cp1y: 200.0,
            cp2x: 180.0, cp2y: 220.0,
            x:    200.0, y:    240.0,
        },
        PathSegment::Bezier {
            cp1x: 220.0, cp1y: 260.0, //?
            cp2x: 330.0, cp2y: 380.0,
            x:    350.0, y:    400.0,
        },
        PathSegment::Quadratic {
            cpx: 520.0, cpy: 580.0,
            x:   540.0, y:   600.0,
        },
        PathSegment::Quadratic {
            cpx: 560.0, cpy: 620.0, //?
            x:   750.0, y:   820.0
        },
        PathSegment::SvgArc {
            radius_x: 50.0, radius_y: 50.0,
            rotation: 30.0,
            large_arc: true, sweep: true,
            x: 980.0, y: 1060.0
        },
        PathSegment::ClosePath
    );

    test!(close_path_1, "M10 20 L 30 40 ZM 100 200 L 300 400",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 30.0, y: 40.0 },
        PathSegment::ClosePath,
        PathSegment::MoveTo { x: 100.0, y: 200.0 },
        PathSegment::LineTo { x: 300.0, y: 400.0 }
    );

    test!(close_path_2, "M10 20 L 30 40 zM 100 200 L 300 400",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 30.0, y: 40.0 },
        PathSegment::ClosePath,
        PathSegment::MoveTo { x: 100.0, y: 200.0 },
        PathSegment::LineTo { x: 300.0, y: 400.0 }
    );

    test!(close_path_3, "M10 20 L 30 40 Z Z Z",
        PathSegment::MoveTo { x: 10.0, y: 20.0 },
        PathSegment::LineTo { x: 30.0, y: 40.0 },
        PathSegment::ClosePath,
        PathSegment::ClosePath,
        PathSegment::ClosePath
    );

    // first token should be EndOfStream
    test!(invalid_1, "M\t.", );

    // ClosePath can't be followed by a number
    test!(invalid_2, "M 0 0 Z 2",
        PathSegment::MoveTo { x: 0.0, y: 0.0 },
        PathSegment::ClosePath
    );

    // ClosePath can be followed by any command
    test!(invalid_3, "M 0 0 Z H 10",
        PathSegment::MoveTo { x: 0.0, y: 0.0 },
        PathSegment::ClosePath,
        PathSegment::LineTo { x: 10.0, y: 0.0 }
    );
}