lyon_extra 1.1.0

Various optional utilities for the lyon crate.
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
use path::{
    geom::{ArcFlags, SvgArc},
    math::{point, vector, Angle, Point},
    traits::PathBuilder,
};
use core::{mem};

extern crate alloc;
use alloc::{string::String, vec::Vec};

use thiserror::Error;

#[non_exhaustive]
#[derive(Error, Clone, Debug, PartialEq)]
pub enum ParseError {
    #[error("Line {line} Column {column}: Expected number, got {src:?}.")]
    Number { src: String, line: i32, column: i32 },
    #[error("Line {line} Column {column}: Expected flag (0/1), got {src:?}.")]
    Flag { src: char, line: i32, column: i32 },
    #[error("Line {line} Column {column}: Invalid command {command:?}.")]
    Command {
        command: char,
        line: i32,
        column: i32,
    },
    #[error("Line {line} Column {column}: Expected move-to command, got {command:?}.")]
    MissingMoveTo {
        command: char,
        line: i32,
        column: i32,
    },
}

#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
pub struct ParserOptions {
    /// Number of custom attributes per endpoint.
    pub num_attributes: usize,
    /// Optionally stop parsing when encountering a provided special character.
    pub stop_at: Option<char>,
}

impl ParserOptions {
    pub const DEFAULT: ParserOptions = ParserOptions {
        num_attributes: 0,
        stop_at: None,
    };
}

// A buffered iterator of characters keeping track of line and column.
pub struct Source<Iter> {
    src: Iter,
    current: char,
    line: i32,
    col: i32,
    finished: bool,
}

impl<Iter: Iterator<Item = char>> Source<Iter> {
    pub fn new<IntoIter>(src: IntoIter) -> Self
    where
        IntoIter: IntoIterator<IntoIter = Iter>,
    {
        Self::with_position(0, 0, src)
    }

    pub fn with_position<IntoIter>(line: i32, column: i32, src: IntoIter) -> Self
    where
        IntoIter: IntoIterator<IntoIter = Iter>,
    {
        let mut src = src.into_iter();

        let (current, finished) = match src.next() {
            Some(c) => (c, false),
            None => (' ', true),
        };

        let line = line + if current == '\n' { 1 } else { 0 };

        Source {
            current,
            finished,
            src,
            line,
            col: column,
        }
    }

    /// Consume the source and returns the iterator, line and column.
    pub fn unwrap(self) -> (Iter, i32, i32) {
        (self.src, self.line, self.col)
    }

    fn skip_whitespace(&mut self) {
        while !self.finished && (self.current.is_whitespace() || self.current == ',') {
            self.advance_one();
        }
    }

    fn advance_one(&mut self) {
        if self.finished {
            return;
        }
        match self.src.next() {
            Some('\n') => {
                self.current = '\n';
                self.line += 1;
                self.col = -1;
            }
            Some(c) => {
                self.current = c;
                self.col += 1;
            }
            None => {
                self.current = '~';
                self.finished = true;
            }
        }
    }
}

/// A context object for parsing the extended path syntax.
///
/// # Syntax
///
/// The extended path syntax is a super-set of the SVG path syntax, with support for extra per-endpoint
/// data for custom attributes.
///
/// Wherever an endpoint is specified, the extended path syntax expects a sequence N extra numbers
/// where N is the number of custom attributes.
///
/// For example with 1 custom attribute `M 0 0 10 Q 1 1 2 2 20 L 3 3 30` reads as follows:
///
/// - Begin at endpoint position [0, 0] with custom attribute 10,
/// - quadratic bézier curve ending at endpoint [2, 2] custom attribute 20, with control point [1, 1],
/// - line to endpoint [3, 3] custom attribute 30.
///
/// Note that only endpoints have custom attributes, control points do not.
#[derive(Debug, Default)]
pub struct PathParser {
    attribute_buffer: Vec<f32>,
    float_buffer: String,
    num_attributes: usize,
    stop_at: Option<char>,
    current_position: Point,
    need_end: bool,
}

impl PathParser {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn parse<Iter, Builder>(
        &mut self,
        options: &ParserOptions,
        src: &mut Source<Iter>,
        output: &mut Builder,
    ) -> Result<(), ParseError>
    where
        Iter: Iterator<Item = char>,
        Builder: PathBuilder,
    {
        self.num_attributes = options.num_attributes;
        self.stop_at = options.stop_at;
        self.need_end = false;

        let res = self.parse_path(src, output);

        if self.need_end {
            output.end(false);
        }

        res
    }

    fn parse_path(
        &mut self,
        src: &mut Source<impl Iterator<Item = char>>,
        output: &mut impl PathBuilder,
    ) -> Result<(), ParseError> {
        // Per-spec: "If a relative moveto (m) appears as the first element of the path, then it is
        // treated as a pair of absolute coordinates."
        self.current_position = point(0.0, 0.0);
        let mut first_position = point(0.0, 0.0);

        let mut need_start = false;
        let mut prev_cubic_ctrl = None;
        let mut prev_quadratic_ctrl = None;
        let mut implicit_cmd = 'M';

        src.skip_whitespace();

        while !src.finished {
            let mut cmd = src.current;
            let cmd_line = src.line;
            let cmd_col = src.col;

            if self.stop_at == Some(cmd) {
                break;
            }

            if cmd.is_ascii_alphabetic() {
                src.advance_one();
            } else {
                cmd = implicit_cmd;
            }

            if need_start && cmd != 'm' && cmd != 'M' {
                return Err(ParseError::MissingMoveTo {
                    command: cmd,
                    line: cmd_line,
                    column: cmd_col,
                });
            }

            //println!("{:?} at line {:?} column {:?}", cmd, cmd_line, cmd_col);

            let is_relative = cmd.is_lowercase();

            match cmd {
                'l' | 'L' => {
                    let to = self.parse_endpoint(is_relative, src)?;
                    output.line_to(to, &self.attribute_buffer);
                }
                'h' | 'H' => {
                    let mut x = self.parse_number(src)?;
                    if is_relative {
                        x += self.current_position.x;
                    }
                    let to = point(x, self.current_position.y);
                    self.current_position = to;
                    self.parse_attributes(src)?;
                    output.line_to(to, &self.attribute_buffer);
                }
                'v' | 'V' => {
                    let mut y = self.parse_number(src)?;
                    if is_relative {
                        y += self.current_position.y;
                    }
                    let to = point(self.current_position.x, y);
                    self.current_position = to;
                    self.parse_attributes(src)?;
                    output.line_to(to, &self.attribute_buffer);
                }
                'q' | 'Q' => {
                    let ctrl = self.parse_point(is_relative, src)?;
                    let to = self.parse_endpoint(is_relative, src)?;
                    prev_quadratic_ctrl = Some(ctrl);
                    output.quadratic_bezier_to(ctrl, to, &self.attribute_buffer);
                }
                't' | 'T' => {
                    let ctrl = self.get_smooth_ctrl(prev_quadratic_ctrl);
                    let to = self.parse_endpoint(is_relative, src)?;
                    prev_quadratic_ctrl = Some(ctrl);
                    output.quadratic_bezier_to(ctrl, to, &self.attribute_buffer);
                }
                'c' | 'C' => {
                    let ctrl1 = self.parse_point(is_relative, src)?;
                    let ctrl2 = self.parse_point(is_relative, src)?;
                    let to = self.parse_endpoint(is_relative, src)?;
                    prev_cubic_ctrl = Some(ctrl2);
                    output.cubic_bezier_to(ctrl1, ctrl2, to, &self.attribute_buffer);
                }
                's' | 'S' => {
                    let ctrl1 = self.get_smooth_ctrl(prev_cubic_ctrl);
                    let ctrl2 = self.parse_point(is_relative, src)?;
                    let to = self.parse_endpoint(is_relative, src)?;
                    prev_cubic_ctrl = Some(ctrl2);
                    output.cubic_bezier_to(ctrl1, ctrl2, to, &self.attribute_buffer);
                }
                'a' | 'A' => {
                    let prev_attributes = self.attribute_buffer.clone();
                    let mut interpolated_attributes = self.attribute_buffer.clone();

                    let from = self.current_position;
                    let rx = self.parse_number(src)?;
                    let ry = self.parse_number(src)?;
                    let x_rotation = self.parse_number(src)?;
                    let large_arc = self.parse_flag(src)?;
                    let sweep = self.parse_flag(src)?;
                    let to = self.parse_endpoint(is_relative, src)?;
                    let svg_arc = SvgArc {
                        from,
                        to,
                        radii: vector(rx, ry),
                        x_rotation: Angle::degrees(x_rotation),
                        flags: ArcFlags { large_arc, sweep },
                    };

                    if svg_arc.is_straight_line() {
                        output.line_to(to, &self.attribute_buffer[..]);
                    } else {
                        let arc = svg_arc.to_arc();

                        arc.for_each_quadratic_bezier_with_t(&mut |curve, range| {
                            for i in 0..self.num_attributes {
                                interpolated_attributes[i] = prev_attributes[i] * (1.0 - range.end)
                                    + self.attribute_buffer[i] * range.end;
                            }
                            output.quadratic_bezier_to(
                                curve.ctrl,
                                curve.to,
                                &interpolated_attributes,
                            );
                        });
                    }
                }
                'm' | 'M' => {
                    if self.need_end {
                        output.end(false);
                        self.need_end = false;
                    }

                    let to = self.parse_endpoint(is_relative, src)?;
                    first_position = to;
                    output.begin(to, &self.attribute_buffer);
                    self.need_end = true;
                    need_start = false;
                }
                'z' | 'Z' => {
                    output.end(true);
                    self.current_position = first_position;
                    self.need_end = false;
                    need_start = true;
                }
                _ => {
                    return Err(ParseError::Command {
                        command: cmd,
                        line: cmd_line,
                        column: cmd_col,
                    });
                }
            }

            match cmd {
                'c' | 'C' | 's' | 'S' => {
                    prev_quadratic_ctrl = None;
                }
                'q' | 'Q' | 't' | 'T' => {
                    prev_cubic_ctrl = None;
                }
                _ => {
                    prev_cubic_ctrl = None;
                    prev_quadratic_ctrl = None;
                }
            }

            implicit_cmd = match cmd {
                'm' => 'l',
                'M' => 'L',
                'z' => 'm',
                'Z' => 'M',
                c => c,
            };

            src.skip_whitespace();
        }

        Ok(())
    }

    fn get_smooth_ctrl(&self, prev_ctrl: Option<Point>) -> Point {
        if let Some(prev_ctrl) = prev_ctrl {
            self.current_position + (self.current_position - prev_ctrl)
        } else {
            self.current_position
        }
    }

    fn parse_endpoint(
        &mut self,
        is_relative: bool,
        src: &mut Source<impl Iterator<Item = char>>,
    ) -> Result<Point, ParseError> {
        let position = self.parse_point(is_relative, src)?;
        self.current_position = position;

        self.parse_attributes(src)?;

        Ok(position)
    }

    fn parse_attributes(
        &mut self,
        src: &mut Source<impl Iterator<Item = char>>,
    ) -> Result<(), ParseError> {
        self.attribute_buffer.clear();
        for _ in 0..self.num_attributes {
            let value = self.parse_number(src)?;
            self.attribute_buffer.push(value);
        }

        Ok(())
    }

    fn parse_point(
        &mut self,
        is_relative: bool,
        src: &mut Source<impl Iterator<Item = char>>,
    ) -> Result<Point, ParseError> {
        let mut x = self.parse_number(src)?;
        let mut y = self.parse_number(src)?;

        if is_relative {
            x += self.current_position.x;
            y += self.current_position.y;
        }

        Ok(point(x, y))
    }

    fn parse_number(
        &mut self,
        src: &mut Source<impl Iterator<Item = char>>,
    ) -> Result<f32, ParseError> {
        self.float_buffer.clear();

        src.skip_whitespace();

        let line = src.line;
        let column = src.col;

        if src.current == '-' {
            self.float_buffer.push('-');
            src.advance_one();
        }

        while src.current.is_numeric() {
            self.float_buffer.push(src.current);
            src.advance_one();
        }

        if src.current == '.' {
            self.float_buffer.push('.');
            src.advance_one();

            while src.current.is_numeric() {
                self.float_buffer.push(src.current);
                src.advance_one();
            }
        }

        if src.current == 'e' || src.current == 'E' {
            self.float_buffer.push(src.current);
            src.advance_one();

            if src.current == '-' {
                self.float_buffer.push('-');
                src.advance_one();
            }

            while src.current.is_numeric() {
                self.float_buffer.push(src.current);
                src.advance_one();
            }
        }

        match self.float_buffer.parse::<f32>() {
            Ok(val) => Ok(val),
            Err(_) => Err(ParseError::Number {
                src: mem::take(&mut self.float_buffer),
                line,
                column,
            }),
        }
    }

    fn parse_flag(
        &mut self,
        src: &mut Source<impl Iterator<Item = char>>,
    ) -> Result<bool, ParseError> {
        src.skip_whitespace();
        match src.current {
            '1' => {
                src.advance_one();
                Ok(true)
            }
            '0' => {
                src.advance_one();
                Ok(false)
            }
            _ => Err(ParseError::Flag {
                src: src.current,
                line: src.line,
                column: src.col,
            }),
        }
    }
}

#[cfg(test)]
use crate::path::Path;

#[test]
fn empty() {
    let options = ParserOptions {
        num_attributes: 0,
        ..ParserOptions::DEFAULT
    };

    let mut parser = PathParser::new();

    let mut builder = Path::builder_with_attributes(options.num_attributes);
    parser
        .parse(&options, &mut Source::new("".chars()), &mut builder)
        .unwrap();

    let mut builder = Path::builder_with_attributes(options.num_attributes);
    parser
        .parse(&options, &mut Source::new(" ".chars()), &mut builder)
        .unwrap();
}

#[test]
fn simple_square() {
    let options = ParserOptions {
        num_attributes: 0,
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();
    let mut builder = Path::builder_with_attributes(options.num_attributes);
    let mut src = Source::new("M 0 0 L 1 0 L 1 1 L 0 1 Z".chars());

    parser.parse(&options, &mut src, &mut builder).unwrap();
}

#[test]
fn simple_attr() {
    let options = ParserOptions {
        num_attributes: 1,
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();
    let mut builder = Path::builder_with_attributes(options.num_attributes);
    let mut src = Source::new("M 0 0 1.0 L 1 0 2.0 L 1 1 3.0 L 0 1 4.0 Z".chars());

    parser.parse(&options, &mut src, &mut builder).unwrap();
}

#[test]
fn implicit_polyline() {
    let options = ParserOptions {
        num_attributes: 1,
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();
    let mut builder = Path::builder_with_attributes(options.num_attributes);
    let mut src = Source::new("0 0 0 1 1 1.0 2 2 2.0 3 3 3".chars());

    parser.parse(&options, &mut src, &mut builder).unwrap();
}

#[test]
fn invalid_cmd() {
    let options = ParserOptions {
        num_attributes: 1,
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();
    let mut src = Source::new("x 0 0 0".chars());

    let mut builder = Path::builder_with_attributes(options.num_attributes);
    let result = parser
        .parse(&options, &mut src, &mut builder)
        .err()
        .unwrap();
    assert_eq!(
        result,
        ParseError::Command {
            command: 'x',
            line: 0,
            column: 0
        }
    );

    let mut builder = Path::builder_with_attributes(options.num_attributes);
    let mut src = Source::new("\n M 0 \n0 1 x 1 1 1".chars());

    let result = parser
        .parse(&options, &mut src, &mut builder)
        .err()
        .unwrap();
    assert_eq!(
        result,
        ParseError::Command {
            command: 'x',
            line: 2,
            column: 4
        }
    );
}

#[test]
fn number_01() {
    let options = ParserOptions {
        num_attributes: 0,
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();

    // Per SVG spec, this is equivalent to "M 0.6 0.5".
    let mut src = Source::new("M 0.6.5".chars());
    let mut builder = Path::builder_with_attributes(options.num_attributes);

    parser.parse(&options, &mut src, &mut builder).unwrap();
    let path = builder.build();

    let mut iter = path.iter();
    use path::PathEvent;
    assert_eq!(
        iter.next(),
        Some(PathEvent::Begin {
            at: point(0.6, 0.5)
        })
    );
    assert_eq!(
        iter.next(),
        Some(PathEvent::End {
            last: point(0.6, 0.5),
            first: point(0.6, 0.5),
            close: false
        })
    );
    assert_eq!(iter.next(), None);
}

#[test]
fn number_scientific_notation() {
    let options = ParserOptions {
        num_attributes: 0,
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();
    let mut builder = Path::builder_with_attributes(options.num_attributes);
    let mut src = Source::new("M 1e-2 -1E3".chars());

    parser.parse(&options, &mut src, &mut builder).unwrap();
}

#[test]
fn bad_numbers() {
    let options = ParserOptions {
        num_attributes: 0,
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();

    let bad_number = &mut|src: &str| {
        let r = parser.parse(
            &options,
            &mut Source::new(src.chars()),
            &mut Path::builder_with_attributes(0)
        );
        match r {
            Err(ParseError::Number { .. }) => true,
            _ => {
                println!("{r:?}");
                false
            }
        }
    };

    assert!(bad_number("M 0 --1"));
    assert!(bad_number("M 0 1ee2"));
    assert!(bad_number("M 0 1e--1"));
    assert!(bad_number("M 0 *2"));
    assert!(bad_number("M 0 e"));
    assert!(bad_number("M 0 1e"));
    assert!(bad_number("M 0 +1"));
}

#[test]
fn stop() {
    let options = ParserOptions {
        num_attributes: 0,
        stop_at: Some('|'),
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();

    let parse = &mut |src: &str| {
        parser.parse(
            &options,
            &mut Source::new(src.chars()),
            &mut Path::builder_with_attributes(0),
        )
    };

    parse("M 0 0 | xxxxxx").unwrap();
    parse("M 0 0| xxxxxx").unwrap();
    parse("| xxxxxx").unwrap();
    parse("    | xxxxxx").unwrap();
}

#[test]
fn need_start() {
    let options = ParserOptions {
        num_attributes: 0,
        stop_at: Some('|'),
        ..ParserOptions::DEFAULT
    };
    let mut parser = PathParser::new();

    let mut builder = Path::builder_with_attributes(options.num_attributes);
    let res = parser.parse(
        &options,
        &mut Source::new("M 0 0 Z L 1 1 2 2 L 3 3 Z M 4 4".chars()),
        &mut builder,
    );
    let p1 = builder.build();
    match res {
        Err(ParseError::MissingMoveTo { .. }) => {}
        _ => {
            panic!("{:?}", res);
        }
    }

    let mut builder = Path::builder_with_attributes(options.num_attributes);
    parser
        .parse(&options, &mut Source::new("M 0 0 Z".chars()), &mut builder)
        .unwrap();
    let p2 = builder.build();

    let mut p1 = p1.iter();
    let mut p2 = p2.iter();
    loop {
        let e1 = p1.next();
        let e2 = p2.next();

        assert_eq!(e1, e2);

        if e1.is_none() {
            break;
        }
    }
}

#[test]
fn issue_895() {
    let options = ParserOptions::DEFAULT;
    let mut parser = PathParser::new();

    let parse = &mut |src: &str| {
        parser.parse(
            &options,
            &mut Source::new(src.chars()),
            &mut Path::builder_with_attributes(0),
        )
    };

    parse("M 1e-9 0").unwrap();
    parse("M -1e-9 0").unwrap();
    parse("M -1e11 0").unwrap();
    parse("M 1.e-9 1.4e-4z").unwrap();
    parse("M 1.6e-9 1.4e-4 z").unwrap();
    parse("M0 1.6e-9L0 1.4e-4").unwrap();
}

#[test]
fn issue_919() {
    let mut builder = lyon_path::Path::builder();
    let mut parser = PathParser::new();
    let _ = parser.parse(
        &ParserOptions::DEFAULT,
        &mut Source::new("0 0M".chars()),
        &mut builder,
    );
}