spanda 0.9.3

A general-purpose animation library for Rust — tweening, keyframes, timelines, and physics.
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! SVG path string parser — converts SVG `d` attribute strings to [`PathCommand`]s.
//!
//! Supports the standard SVG path commands: M, L, Q, C, Z (and lowercase
//! relative variants). This enables loading paths from SVG files or design
//! tools without any DOM dependency.
//!
//! GSAP equivalent: `path: "#svgPath"` (but operates on the `d` string
//! directly instead of querying the DOM).
//!
//! # Example
//!
//! ```rust
//! use spanda::svg_path::SvgPathParser;
//! use spanda::motion_path::{CompoundPath, PathCommand};
//!
//! let commands = SvgPathParser::parse("M 0 0 C 50 100 100 100 150 0 L 200 0");
//! let path = CompoundPath::new(commands);
//! let pos = path.position(0.5);
//! ```

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use crate::motion_path::PathCommand;

/// A zero-allocation SVG path string parser.
///
/// Converts an SVG `d` attribute string into a `Vec<PathCommand>`.
///
/// ## Supported commands
///
/// | Command | Meaning | Coordinates |
/// |---------|---------|-------------|
/// | `M`/`m` | Move to | `x y` |
/// | `L`/`l` | Line to | `x y` |
/// | `H`/`h` | Horizontal line | `x` |
/// | `V`/`v` | Vertical line | `y` |
/// | `Q`/`q` | Quadratic Bezier | `cx cy x y` |
/// | `C`/`c` | Cubic Bezier | `c1x c1y c2x c2y x y` |
/// | `Z`/`z` | Close path | — |
///
/// Lowercase commands are relative to the current point;
/// uppercase are absolute.
#[derive(Debug)]
pub struct SvgPathParser;

impl SvgPathParser {
    /// Parse an SVG path `d` attribute string into a list of [`PathCommand`]s.
    ///
    /// ```rust
    /// use spanda::svg_path::SvgPathParser;
    ///
    /// let cmds = SvgPathParser::parse("M 10 20 L 30 40 Z");
    /// assert_eq!(cmds.len(), 3); // MoveTo, LineTo, Close
    /// ```
    pub fn parse(d: &str) -> Vec<PathCommand> {
        let mut commands = Vec::new();
        let mut cursor = [0.0_f32; 2];
        let mut subpath_start = [0.0_f32; 2];

        let tokens = Tokenizer::new(d);
        let mut nums: Vec<f32> = Vec::new();
        let mut current_cmd: Option<char> = None;

        for token in tokens {
            match token {
                Token::Command(c) => {
                    // Flush any pending implicit repeats
                    if let Some(cmd) = current_cmd {
                        Self::flush_nums(
                            cmd,
                            &mut nums,
                            &mut commands,
                            &mut cursor,
                            &mut subpath_start,
                        );
                    }
                    current_cmd = Some(c);
                    nums.clear();

                    if c == 'Z' || c == 'z' {
                        commands.push(PathCommand::Close);
                        cursor = subpath_start;
                        current_cmd = None;
                    }
                }
                Token::Number(n) => {
                    nums.push(n);
                    if let Some(cmd) = current_cmd {
                        let needed = Self::args_needed(cmd);
                        if needed > 0 && nums.len() >= needed {
                            Self::emit_command(
                                cmd,
                                &nums[nums.len() - needed..],
                                &mut commands,
                                &mut cursor,
                                &mut subpath_start,
                            );
                            // After first M, implicit repeats become L
                            if cmd == 'M' {
                                current_cmd = Some('L');
                            } else if cmd == 'm' {
                                current_cmd = Some('l');
                            }
                            nums.clear();
                        }
                    }
                }
            }
        }

        // Flush remaining
        if let Some(cmd) = current_cmd {
            Self::flush_nums(
                cmd,
                &mut nums,
                &mut commands,
                &mut cursor,
                &mut subpath_start,
            );
        }

        commands
    }

    /// Number of arguments needed for a given command.
    fn args_needed(cmd: char) -> usize {
        match cmd {
            'M' | 'm' | 'L' | 'l' => 2,
            'H' | 'h' | 'V' | 'v' => 1,
            'Q' | 'q' => 4,
            'C' | 'c' => 6,
            'Z' | 'z' => 0,
            _ => 0,
        }
    }

    /// Flush accumulated numbers for commands that may have implicit repeats.
    fn flush_nums(
        cmd: char,
        nums: &mut Vec<f32>,
        commands: &mut Vec<PathCommand>,
        cursor: &mut [f32; 2],
        subpath_start: &mut [f32; 2],
    ) {
        let needed = Self::args_needed(cmd);
        if needed == 0 {
            return;
        }
        while nums.len() >= needed {
            let args: Vec<f32> = nums.drain(..needed).collect();
            Self::emit_command(cmd, &args, commands, cursor, subpath_start);
        }
    }

    /// Emit a single path command from parsed arguments.
    fn emit_command(
        cmd: char,
        args: &[f32],
        commands: &mut Vec<PathCommand>,
        cursor: &mut [f32; 2],
        subpath_start: &mut [f32; 2],
    ) {
        let is_relative = cmd.is_ascii_lowercase();
        let ox = if is_relative { cursor[0] } else { 0.0 };
        let oy = if is_relative { cursor[1] } else { 0.0 };

        match cmd.to_ascii_uppercase() {
            'M' => {
                let p = [args[0] + ox, args[1] + oy];
                commands.push(PathCommand::MoveTo(p));
                *cursor = p;
                *subpath_start = p;
            }
            'L' => {
                let p = [args[0] + ox, args[1] + oy];
                commands.push(PathCommand::LineTo(p));
                *cursor = p;
            }
            'H' => {
                let x = args[0] + if is_relative { cursor[0] } else { 0.0 };
                let p = [x, cursor[1]];
                commands.push(PathCommand::LineTo(p));
                *cursor = p;
            }
            'V' => {
                let y = args[0] + if is_relative { cursor[1] } else { 0.0 };
                let p = [cursor[0], y];
                commands.push(PathCommand::LineTo(p));
                *cursor = p;
            }
            'Q' => {
                let control = [args[0] + ox, args[1] + oy];
                let end = [args[2] + ox, args[3] + oy];
                commands.push(PathCommand::QuadTo { control, end });
                *cursor = end;
            }
            'C' => {
                let control1 = [args[0] + ox, args[1] + oy];
                let control2 = [args[2] + ox, args[3] + oy];
                let end = [args[4] + ox, args[5] + oy];
                commands.push(PathCommand::CubicTo {
                    control1,
                    control2,
                    end,
                });
                *cursor = end;
            }
            _ => {}
        }
    }
}

// ── Tokenizer ────────────────────────────────────────────────────────────────

/// Tokens produced by the SVG path tokenizer.
#[derive(Debug, Clone)]
enum Token {
    Command(char),
    Number(f32),
}

/// Simple tokenizer for SVG path `d` strings.
struct Tokenizer<'a> {
    chars: &'a [u8],
    pos: usize,
}

impl<'a> Tokenizer<'a> {
    fn new(s: &'a str) -> Self {
        Self {
            chars: s.as_bytes(),
            pos: 0,
        }
    }

    fn skip_whitespace_and_commas(&mut self) {
        while self.pos < self.chars.len() {
            let b = self.chars[self.pos];
            if b == b' ' || b == b',' || b == b'\t' || b == b'\n' || b == b'\r' {
                self.pos += 1;
            } else {
                break;
            }
        }
    }

    fn is_command(b: u8) -> bool {
        matches!(
            b,
            b'M' | b'm'
                | b'L'
                | b'l'
                | b'H'
                | b'h'
                | b'V'
                | b'v'
                | b'Q'
                | b'q'
                | b'C'
                | b'c'
                | b'Z'
                | b'z'
        )
    }

    fn parse_number(&mut self) -> Option<f32> {
        let start = self.pos;
        // optional sign
        if self.pos < self.chars.len()
            && (self.chars[self.pos] == b'-' || self.chars[self.pos] == b'+')
        {
            self.pos += 1;
        }
        // integer part
        while self.pos < self.chars.len() && self.chars[self.pos].is_ascii_digit() {
            self.pos += 1;
        }
        // decimal part
        if self.pos < self.chars.len() && self.chars[self.pos] == b'.' {
            self.pos += 1;
            while self.pos < self.chars.len() && self.chars[self.pos].is_ascii_digit() {
                self.pos += 1;
            }
        }
        // exponent
        if self.pos < self.chars.len()
            && (self.chars[self.pos] == b'e' || self.chars[self.pos] == b'E')
        {
            self.pos += 1;
            if self.pos < self.chars.len()
                && (self.chars[self.pos] == b'-' || self.chars[self.pos] == b'+')
            {
                self.pos += 1;
            }
            while self.pos < self.chars.len() && self.chars[self.pos].is_ascii_digit() {
                self.pos += 1;
            }
        }
        if self.pos > start {
            let s = core::str::from_utf8(&self.chars[start..self.pos]).ok()?;
            s.parse::<f32>().ok()
        } else {
            None
        }
    }
}

impl<'a> Iterator for Tokenizer<'a> {
    type Item = Token;

    fn next(&mut self) -> Option<Token> {
        self.skip_whitespace_and_commas();
        if self.pos >= self.chars.len() {
            return None;
        }

        let b = self.chars[self.pos];

        if Self::is_command(b) {
            self.pos += 1;
            return Some(Token::Command(b as char));
        }

        // Try to parse a number (starts with digit, '.', '+', or '-')
        if b.is_ascii_digit() || b == b'-' || b == b'+' || b == b'.' {
            if let Some(n) = self.parse_number() {
                return Some(Token::Number(n));
            }
        }

        // Skip unknown character
        self.pos += 1;
        self.next()
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

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

    #[test]
    fn parse_move_line_close() {
        let cmds = SvgPathParser::parse("M 0 0 L 100 0 L 100 100 Z");
        assert_eq!(cmds.len(), 4);
        match &cmds[0] {
            PathCommand::MoveTo(p) => assert!((p[0]).abs() < 1e-4 && (p[1]).abs() < 1e-4),
            _ => panic!("Expected MoveTo"),
        }
        match &cmds[1] {
            PathCommand::LineTo(p) => assert!((p[0] - 100.0).abs() < 1e-4),
            _ => panic!("Expected LineTo"),
        }
        match &cmds[3] {
            PathCommand::Close => {}
            _ => panic!("Expected Close"),
        }
    }

    #[test]
    fn parse_cubic() {
        let cmds = SvgPathParser::parse("M 0 0 C 50 100 100 100 150 0");
        assert_eq!(cmds.len(), 2);
        match &cmds[1] {
            PathCommand::CubicTo {
                control1,
                control2,
                end,
            } => {
                assert!((control1[0] - 50.0).abs() < 1e-4);
                assert!((control2[0] - 100.0).abs() < 1e-4);
                assert!((end[0] - 150.0).abs() < 1e-4);
            }
            _ => panic!("Expected CubicTo"),
        }
    }

    #[test]
    fn parse_quad() {
        let cmds = SvgPathParser::parse("M 0 0 Q 50 100 100 0");
        assert_eq!(cmds.len(), 2);
        match &cmds[1] {
            PathCommand::QuadTo { control, end } => {
                assert!((control[0] - 50.0).abs() < 1e-4);
                assert!((end[0] - 100.0).abs() < 1e-4);
            }
            _ => panic!("Expected QuadTo"),
        }
    }

    #[test]
    fn parse_relative() {
        let cmds = SvgPathParser::parse("M 10 20 l 30 40");
        assert_eq!(cmds.len(), 2);
        match &cmds[1] {
            PathCommand::LineTo(p) => {
                assert!((p[0] - 40.0).abs() < 1e-4, "Expected x=40, got {}", p[0]);
                assert!((p[1] - 60.0).abs() < 1e-4, "Expected y=60, got {}", p[1]);
            }
            _ => panic!("Expected LineTo"),
        }
    }

    #[test]
    fn parse_horizontal_vertical() {
        let cmds = SvgPathParser::parse("M 0 0 H 100 V 50");
        assert_eq!(cmds.len(), 3);
        match &cmds[1] {
            PathCommand::LineTo(p) => {
                assert!((p[0] - 100.0).abs() < 1e-4);
                assert!((p[1]).abs() < 1e-4);
            }
            _ => panic!("Expected LineTo for H"),
        }
        match &cmds[2] {
            PathCommand::LineTo(p) => {
                assert!((p[0] - 100.0).abs() < 1e-4); // x stays at 100
                assert!((p[1] - 50.0).abs() < 1e-4);
            }
            _ => panic!("Expected LineTo for V"),
        }
    }

    #[test]
    fn parse_relative_h_v() {
        let cmds = SvgPathParser::parse("M 10 20 h 30 v 40");
        assert_eq!(cmds.len(), 3);
        match &cmds[1] {
            PathCommand::LineTo(p) => {
                assert!((p[0] - 40.0).abs() < 1e-4);
                assert!((p[1] - 20.0).abs() < 1e-4);
            }
            _ => panic!("Expected LineTo for h"),
        }
        match &cmds[2] {
            PathCommand::LineTo(p) => {
                assert!((p[0] - 40.0).abs() < 1e-4);
                assert!((p[1] - 60.0).abs() < 1e-4);
            }
            _ => panic!("Expected LineTo for v"),
        }
    }

    #[test]
    fn parse_compact_notation() {
        // No spaces between commands and numbers, negative numbers as separators
        let cmds = SvgPathParser::parse("M0,0L100,50L200,0Z");
        assert_eq!(cmds.len(), 4);
        match &cmds[1] {
            PathCommand::LineTo(p) => {
                assert!((p[0] - 100.0).abs() < 1e-4);
                assert!((p[1] - 50.0).abs() < 1e-4);
            }
            _ => panic!("Expected LineTo"),
        }
    }

    #[test]
    fn parse_empty() {
        let cmds = SvgPathParser::parse("");
        assert!(cmds.is_empty());
    }

    #[test]
    fn parse_implicit_lineto() {
        // After M, extra coordinate pairs become implicit L
        let cmds = SvgPathParser::parse("M 0 0 50 50 100 0");
        assert_eq!(cmds.len(), 3); // M + L + L
        match &cmds[1] {
            PathCommand::LineTo(p) => {
                assert!((p[0] - 50.0).abs() < 1e-4);
                assert!((p[1] - 50.0).abs() < 1e-4);
            }
            _ => panic!("Expected implicit LineTo"),
        }
    }

    #[test]
    fn parse_negative_coords() {
        let cmds = SvgPathParser::parse("M -10 -20 L -30 -40");
        assert_eq!(cmds.len(), 2);
        match &cmds[0] {
            PathCommand::MoveTo(p) => {
                assert!((p[0] + 10.0).abs() < 1e-4);
                assert!((p[1] + 20.0).abs() < 1e-4);
            }
            _ => panic!("Expected MoveTo"),
        }
    }

    #[test]
    fn parse_into_compound_path() {
        use crate::motion_path::CompoundPath;

        let cmds = SvgPathParser::parse("M 0 0 C 50 100 100 100 150 0 L 200 0");
        let path = CompoundPath::new(cmds);

        let start = path.position(0.0);
        let end = path.position(1.0);
        assert!((start[0]).abs() < 2.0);
        assert!((end[0] - 200.0).abs() < 2.0);
    }

    #[test]
    fn parse_relative_cubic() {
        let cmds = SvgPathParser::parse("M 10 10 c 10 20 30 20 40 0");
        assert_eq!(cmds.len(), 2);
        match &cmds[1] {
            PathCommand::CubicTo {
                control1,
                control2,
                end,
            } => {
                assert!((control1[0] - 20.0).abs() < 1e-4);
                assert!((control1[1] - 30.0).abs() < 1e-4);
                assert!((control2[0] - 40.0).abs() < 1e-4);
                assert!((control2[1] - 30.0).abs() < 1e-4);
                assert!((end[0] - 50.0).abs() < 1e-4);
                assert!((end[1] - 10.0).abs() < 1e-4);
            }
            _ => panic!("Expected CubicTo"),
        }
    }
}