pltrs-core 0.1.0

Geometry, paths, affine transforms, colours and numpy's float semantics shared by every pltrs 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
//! Reading SVG path data (the `d` attribute of `<path>`) into a [`Path`].
//!
//! The grammar is SVG 1.1's: absolute (upper case) and relative (lower
//! case) moves, lines (`L`, `H`, `V`), cubic (`C`, `S`) and quadratic
//! (`Q`, `T`) Béziers and closures (`Z`), numbers separated by white
//! space or commas or by nothing where a sign or a second decimal point
//! ends one (`M0-1.5.5`), and repeated coordinates continuing the last
//! command (a move's further pairs are lines). Elliptical arcs (`A`) are
//! refused.

use std::fmt;

use super::{Path, PathBuilder};
use crate::Point;

/// Why SVG path data could not be read.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SvgPathError {
    /// A character that is neither a command, a number nor a separator,
    /// at a byte offset.
    UnexpectedCharacter {
        /// Byte offset in the data.
        offset: usize,
        /// The character found.
        found: char,
    },
    /// A command this reader does not draw (elliptical arcs).
    UnsupportedCommand(char),
    /// Data that does not start with a move.
    MissingInitialMove,
    /// A command whose coordinates stop short, at a byte offset.
    MissingNumber {
        /// The command.
        command: char,
        /// Byte offset where a number was expected.
        offset: usize,
    },
}

impl fmt::Display for SvgPathError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnexpectedCharacter { offset, found } => {
                write!(f, "unexpected {found:?} at byte {offset} of SVG path data")
            }
            Self::UnsupportedCommand(command) => {
                write!(f, "SVG path command {command:?} is not supported")
            }
            Self::MissingInitialMove => f.write_str("SVG path data must start with a move"),
            Self::MissingNumber { command, offset } => write!(
                f,
                "SVG path command {command:?} lacks a number at byte {offset}"
            ),
        }
    }
}

impl std::error::Error for SvgPathError {}

impl Path {
    /// Reads SVG path data, as in `<path d="M0 0H10V10Z"/>`: SVG 1.1's
    /// grammar, absolute and relative moves, lines (`L`, `H`, `V`), cubic
    /// (`C`, `S`) and quadratic (`Q`, `T`) Béziers and closures (`Z`),
    /// with numbers run together where a sign or a second decimal point
    /// separates them (`M0-1.5.5`). Elliptical arcs (`A`) are refused.
    ///
    /// ```
    /// use pltrs_core::{Path, Point};
    /// let square = Path::from_svg("M0 0h10v10H0z")?;
    /// assert_eq!(square.vertices()[2], Point::new(10.0, 10.0));
    /// # Ok::<(), pltrs_core::path::SvgPathError>(())
    /// ```
    ///
    /// # Errors
    ///
    /// When the data does not follow the grammar, does not start with a
    /// move, or has an arc.
    pub fn from_svg(data: &str) -> Result<Self, SvgPathError> {
        let mut reader = Reader {
            scanner: Scanner { data, offset: 0 },
            builder: Path::builder(),
            current: Point::ORIGIN,
            start: Point::ORIGIN,
            reflected: None,
        };
        let mut command = None;
        while let Some(next) = reader.scanner.next_token()? {
            let letter = match next {
                Token::Command(letter) => {
                    reader.scanner.offset += 1;
                    letter
                }
                // Coordinates repeat the last command; after a move,
                // they are lines.
                Token::Number => match command {
                    Some('M') => 'L',
                    Some('m') => 'l',
                    Some(letter) if !letter.eq_ignore_ascii_case(&'z') => letter,
                    None => return Err(SvgPathError::MissingInitialMove),
                    Some(_) => {
                        return Err(SvgPathError::UnexpectedCharacter {
                            offset: reader.scanner.offset,
                            found: reader.scanner.peek().unwrap_or(' '),
                        });
                    }
                },
            };
            if command.is_none() && !letter.eq_ignore_ascii_case(&'m') {
                return Err(SvgPathError::MissingInitialMove);
            }
            reader.command(letter)?;
            command = Some(letter);
        }
        Ok(reader.builder.build())
    }
}

/// What comes next in the data.
enum Token {
    /// A command letter (not yet consumed).
    Command(char),
    /// The start of a number.
    Number,
}

/// A position in path data.
struct Scanner<'a> {
    data: &'a str,
    offset: usize,
}

impl Scanner<'_> {
    fn peek(&self) -> Option<char> {
        self.data[self.offset..].chars().next()
    }

    fn skip_separators(&mut self) {
        while let Some(c) = self.peek()
            && (c.is_ascii_whitespace() || c == ',')
        {
            self.offset += 1;
        }
    }

    /// Skips separators and classifies what follows; `None` at the end.
    fn next_token(&mut self) -> Result<Option<Token>, SvgPathError> {
        self.skip_separators();
        let Some(c) = self.peek() else {
            return Ok(None);
        };
        match c {
            'M' | 'm' | 'L' | 'l' | 'H' | 'h' | 'V' | 'v' | 'C' | 'c' | 'S' | 's' | 'Q' | 'q'
            | 'T' | 't' | 'Z' | 'z' => Ok(Some(Token::Command(c))),
            'A' | 'a' => Err(SvgPathError::UnsupportedCommand(c)),
            '0'..='9' | '+' | '-' | '.' => Ok(Some(Token::Number)),
            found => Err(SvgPathError::UnexpectedCharacter {
                offset: self.offset,
                found,
            }),
        }
    }

    /// Reads a number after optional separators: sign, digits, fraction
    /// and exponent.
    fn number(&mut self, command: char) -> Result<f64, SvgPathError> {
        self.skip_separators();
        let bytes = self.data.as_bytes();
        let start = self.offset;
        let mut end = start;
        let digits = |mut at: usize| {
            while bytes.get(at).is_some_and(u8::is_ascii_digit) {
                at += 1;
            }
            at
        };
        if matches!(bytes.get(end), Some(b'+' | b'-')) {
            end += 1;
        }
        let integer = digits(end);
        let mut mantissa = integer > end;
        end = integer;
        if bytes.get(end) == Some(&b'.') {
            let fraction = digits(end + 1);
            mantissa |= fraction > end + 1;
            end = fraction;
        }
        if mantissa && matches!(bytes.get(end), Some(b'e' | b'E')) {
            let mut exponent = end + 1;
            if matches!(bytes.get(exponent), Some(b'+' | b'-')) {
                exponent += 1;
            }
            let exponent_end = digits(exponent);
            if exponent_end > exponent {
                end = exponent_end;
            }
        }
        let missing = SvgPathError::MissingNumber {
            command,
            offset: start,
        };
        if !mantissa {
            return Err(missing);
        }
        let value = self.data[start..end].parse().map_err(|_| missing)?;
        self.offset = end;
        Ok(value)
    }
}

/// The state of a path being read.
struct Reader<'a> {
    scanner: Scanner<'a>,
    builder: PathBuilder,
    /// The current point.
    current: Point,
    /// The start of the current sub-path, where closing returns.
    start: Point,
    /// The control point the last curve command leaves for a smooth one
    /// to reflect: cubic (`true`) or quadratic.
    reflected: Option<(Point, bool)>,
}

impl Reader<'_> {
    /// A point from two numbers, relative to the current point for lower
    /// case commands.
    fn point(&mut self, command: char) -> Result<Point, SvgPathError> {
        let x = self.scanner.number(command)?;
        let y = self.scanner.number(command)?;
        Ok(self.place(command, Point::new(x, y)))
    }

    fn place(&self, command: char, p: Point) -> Point {
        if command.is_ascii_lowercase() {
            Point::new(self.current.x + p.x, self.current.y + p.y)
        } else {
            p
        }
    }

    /// The reflection of the last curve's control point of the same
    /// kind, or the current point.
    fn reflection(&self, cubic: bool) -> Point {
        match self.reflected {
            Some((control, kind)) if kind == cubic => Point::new(
                2.0 * self.current.x - control.x,
                2.0 * self.current.y - control.y,
            ),
            _ => self.current,
        }
    }

    /// Reads one command's coordinates and adds its segment.
    fn command(&mut self, command: char) -> Result<(), SvgPathError> {
        let mut builder = std::mem::take(&mut self.builder);
        let mut reflected = None;
        let end = match command.to_ascii_uppercase() {
            'M' => {
                let p = self.point(command)?;
                self.start = p;
                builder = builder.move_to(p);
                p
            }
            'L' => {
                let p = self.point(command)?;
                builder = builder.line_to(p);
                p
            }
            'H' => {
                let x = self.scanner.number(command)?;
                let x = if command == 'h' {
                    self.current.x + x
                } else {
                    x
                };
                let p = Point::new(x, self.current.y);
                builder = builder.line_to(p);
                p
            }
            'V' => {
                let y = self.scanner.number(command)?;
                let y = if command == 'v' {
                    self.current.y + y
                } else {
                    y
                };
                let p = Point::new(self.current.x, y);
                builder = builder.line_to(p);
                p
            }
            'C' | 'S' => {
                let first = if command.eq_ignore_ascii_case(&'C') {
                    self.point(command)?
                } else {
                    self.reflection(true)
                };
                let second = self.point(command)?;
                let p = self.point(command)?;
                builder = builder.cubic_to(first, second, p);
                reflected = Some((second, true));
                p
            }
            'Q' | 'T' => {
                let control = if command.eq_ignore_ascii_case(&'Q') {
                    self.point(command)?
                } else {
                    self.reflection(false)
                };
                let p = self.point(command)?;
                builder = builder.quad_to(control, p);
                reflected = Some((control, false));
                p
            }
            _ => {
                builder = builder.close();
                self.start
            }
        };
        self.builder = builder;
        self.current = end;
        self.reflected = reflected;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::PathCode::{ClosePoly, Curve3, Curve4, LineTo, MoveTo};

    fn p(x: f64, y: f64) -> Point {
        Point::new(x, y)
    }

    #[test]
    fn absolute_and_relative_lines_close() {
        let path = Path::from_svg("M10 20 H30 V40 h-5 v-5 L0,0 l1 1 Z").unwrap();
        assert_eq!(
            path.vertices(),
            [
                p(10.0, 20.0),
                p(30.0, 20.0),
                p(30.0, 40.0),
                p(25.0, 40.0),
                p(25.0, 35.0),
                p(0.0, 0.0),
                p(1.0, 1.0),
                p(10.0, 20.0),
            ]
        );
        assert_eq!(
            path.codes().unwrap(),
            [
                MoveTo, LineTo, LineTo, LineTo, LineTo, LineTo, LineTo, ClosePoly
            ]
        );
    }

    #[test]
    fn repeated_pairs_after_a_move_are_lines() {
        let path = Path::from_svg("m1 1 2 0 0 2z m1 1 1 0").unwrap();
        assert_eq!(
            path.vertices(),
            [
                p(1.0, 1.0),
                p(3.0, 1.0),
                p(3.0, 3.0),
                p(1.0, 1.0),
                // A relative move after a closure starts from the
                // sub-path's start.
                p(2.0, 2.0),
                p(3.0, 2.0),
            ]
        );
        assert_eq!(
            path.codes().unwrap(),
            [MoveTo, LineTo, LineTo, ClosePoly, MoveTo, LineTo]
        );
    }

    #[test]
    fn numbers_need_no_separators() {
        // A sign or a second point starts a number; exponents take signs.
        let path = Path::from_svg("M0-1.5.5 1e1,2E-1+3").unwrap();
        assert_eq!(path.vertices(), [p(0.0, -1.5), p(0.5, 10.0), p(0.2, 3.0)]);
        // An exponent marker without digits ends the number before it.
        assert_eq!(
            Path::from_svg("M1e 2"),
            Err(SvgPathError::MissingNumber {
                command: 'M',
                offset: 2
            })
        );
    }

    #[test]
    fn cubic_and_smooth_cubic_curves() {
        let path = Path::from_svg("M0 0C1 0 2 1 2 2S3 4 4 4c1 0 1 1 1 1").unwrap();
        assert_eq!(
            path.vertices(),
            [
                p(0.0, 0.0),
                p(1.0, 0.0),
                p(2.0, 1.0),
                p(2.0, 2.0),
                // Reflects (2, 1) through (2, 2).
                p(2.0, 3.0),
                p(3.0, 4.0),
                p(4.0, 4.0),
                p(5.0, 4.0),
                p(5.0, 5.0),
                p(5.0, 5.0),
            ]
        );
        assert_eq!(
            path.codes().unwrap(),
            [
                MoveTo, Curve4, Curve4, Curve4, Curve4, Curve4, Curve4, Curve4, Curve4, Curve4
            ]
        );
    }

    #[test]
    fn quadratic_and_smooth_quadratic_curves() {
        let path = Path::from_svg("M0 0Q1 1 2 0T4 0t2 0").unwrap();
        assert_eq!(
            path.vertices(),
            [
                p(0.0, 0.0),
                p(1.0, 1.0),
                p(2.0, 0.0),
                p(3.0, -1.0),
                p(4.0, 0.0),
                p(5.0, 1.0),
                p(6.0, 0.0),
            ]
        );
        assert_eq!(
            path.codes().unwrap(),
            [MoveTo, Curve3, Curve3, Curve3, Curve3, Curve3, Curve3]
        );
        // A smooth curve without a curve of its kind before it takes the
        // current point as its control.
        let path = Path::from_svg("M0 0C1 1 2 1 3 0T5 0").unwrap();
        assert_eq!(path.vertices()[4], p(3.0, 0.0));
    }

    #[test]
    fn empty_data_is_an_empty_path() {
        assert!(Path::from_svg("").unwrap().is_empty());
        assert!(Path::from_svg(" \n, ").unwrap().is_empty());
    }

    #[test]
    fn malformed_data_is_refused() {
        assert_eq!(
            Path::from_svg("L1 1"),
            Err(SvgPathError::MissingInitialMove)
        );
        assert_eq!(Path::from_svg("1 1"), Err(SvgPathError::MissingInitialMove));
        assert_eq!(
            Path::from_svg("M0 0A1 1 0 0 1 2 2"),
            Err(SvgPathError::UnsupportedCommand('A'))
        );
        assert_eq!(
            Path::from_svg("M0 0 L1"),
            Err(SvgPathError::MissingNumber {
                command: 'L',
                offset: 7
            })
        );
        assert_eq!(
            Path::from_svg("M0 0 L1 1 x"),
            Err(SvgPathError::UnexpectedCharacter {
                offset: 10,
                found: 'x'
            })
        );
        assert_eq!(
            Path::from_svg("M0 0z 1 1"),
            Err(SvgPathError::UnexpectedCharacter {
                offset: 6,
                found: '1'
            })
        );
        assert!(Path::from_svg("M. 0").is_err());
        assert!(Path::from_svg("M- 0").is_err());
        assert!(Path::from_svg("M0 0 L1 é").is_err());
        assert!(
            SvgPathError::UnsupportedCommand('a')
                .to_string()
                .contains("'a'")
        );
    }
}