svgdom 0.7.0

Library to represent an SVG as a DOM.
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::{
    fmt,
    str,
};

use super::writer;

use types::FuzzyEq;

use WriteOptions;

/// List of all path commands.
#[derive(Copy,Clone,Debug,PartialEq)]
#[allow(missing_docs)]
pub enum Command {
    MoveTo,
    LineTo,
    HorizontalLineTo,
    VerticalLineTo,
    CurveTo,
    SmoothCurveTo,
    Quadratic,
    SmoothQuadratic,
    EllipticalArc,
    ClosePath,
}

#[derive(Copy,Clone,Debug,PartialEq)]
#[allow(missing_docs)]
pub enum SegmentData {
    MoveTo {
        x: f64,
        y: f64,
    },
    LineTo {
        x: f64,
        y: f64,
    },
    HorizontalLineTo {
        x: f64,
    },
    VerticalLineTo {
        y: f64,
    },
    CurveTo {
        x1: f64,
        y1: f64,
        x2: f64,
        y2: f64,
        x: f64,
        y: f64,
    },
    SmoothCurveTo {
        x2: f64,
        y2: f64,
        x: f64,
        y: f64,
    },
    Quadratic {
        x1: f64,
        y1: f64,
        x: f64,
        y: f64,
    },
    SmoothQuadratic {
        x: f64,
        y: f64,
    },
    EllipticalArc {
        rx: f64,
        ry: f64,
        x_axis_rotation: f64,
        large_arc: bool,
        sweep: bool,
        x: f64,
        y: f64,
    },
    ClosePath,
}

/// Representation of the path segment.
///
/// If you want to change the segment type (for example MoveTo to LineTo)
/// - you should create a new segment.
/// But you still can change points or make segment relative or absolute.
#[derive(Copy,Clone,Debug,PartialEq)]
pub struct Segment {
    /// Indicate that segment is absolute.
    pub absolute: bool,
    /// Segment data.
    pub data: SegmentData,
}

impl Segment {
    // TODO: to_relative
    // TODO: to_absolute

    /// Constructs a new MoveTo `Segment`.
    pub fn new_move_to(x: f64, y: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::MoveTo { x: x, y: y },
        }
    }

    /// Constructs a new ClosePath `Segment`.
    pub fn new_close_path() -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::ClosePath,
        }
    }

    /// Constructs a new LineTo `Segment`.
    pub fn new_line_to(x: f64, y: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::LineTo { x: x, y: y },
        }
    }

    /// Constructs a new HorizontalLineTo `Segment`.
    pub fn new_hline_to(x: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::HorizontalLineTo { x: x },
        }
    }

    /// Constructs a new VerticalLineTo `Segment`.
    pub fn new_vline_to(y: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::VerticalLineTo { y: y },
        }
    }

    /// Constructs a new CurveTo `Segment`.
    pub fn new_curve_to(x1: f64, y1: f64, x2: f64, y2: f64, x: f64, y: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::CurveTo {
                x1: x1,
                y1: y1,
                x2: x2,
                y2: y2,
                x: x,
                y: y,
            },
        }
    }

    /// Constructs a new SmoothCurveTo `Segment`.
    pub fn new_smooth_curve_to(x2: f64, y2: f64, x: f64, y: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::SmoothCurveTo {
                x2: x2,
                y2: y2,
                x: x,
                y: y,
            },
        }
    }

    /// Constructs a new QuadTo `Segment`.
    pub fn new_quad_to(x1: f64, y1: f64, x: f64, y: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::Quadratic {
                x1: x1,
                y1: y1,
                x: x,
                y: y,
            },
        }
    }

    /// Constructs a new SmoothQuadTo `Segment`.
    pub fn new_smooth_quad_to(x: f64, y: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::SmoothQuadratic {
                x: x,
                y: y,
            },
        }
    }

    /// Constructs a new ArcTo `Segment`.
    pub fn new_arc_to(rx: f64, ry: f64, x_axis_rotation: f64, large_arc: bool, sweep: bool,
                  x: f64, y: f64) -> Segment {
        Segment {
            absolute: true,
            data: SegmentData::EllipticalArc {
                rx: rx,
                ry: ry,
                x_axis_rotation: x_axis_rotation,
                large_arc: large_arc,
                sweep: sweep,
                x: x,
                y: y,
            },
        }
    }

    /// Returns a segment type.
    pub fn cmd(&self) -> Command {
        match *self.data() {
            SegmentData::MoveTo { .. } => Command::MoveTo,
            SegmentData::LineTo { .. } => Command::LineTo,
            SegmentData::HorizontalLineTo { .. } => Command::HorizontalLineTo,
            SegmentData::VerticalLineTo { .. } => Command::VerticalLineTo,
            SegmentData::CurveTo { .. } => Command::CurveTo,
            SegmentData::SmoothCurveTo { .. } => Command::SmoothCurveTo,
            SegmentData::Quadratic { .. } => Command::Quadratic,
            SegmentData::SmoothQuadratic { .. } => Command::SmoothQuadratic,
            SegmentData::EllipticalArc { .. } => Command::EllipticalArc,
            SegmentData::ClosePath => Command::ClosePath,
        }
    }

    /// Returns segment's data.
    pub fn data(&self) -> &SegmentData {
        &self.data
    }

    /// Returns segment's mutable data.
    pub fn data_mut(&mut self) -> &mut SegmentData {
        &mut self.data
    }

    /// Returns `true` if the segment is absolute.
    #[inline]
    pub fn is_absolute(&self) -> bool {
        self.absolute
    }

    #[inline]
    /// Returns `true` if the segment is relative.
    pub fn is_relative(&self) -> bool {
        !self.absolute
    }

    /// Returns the `x` coordinate of the segment if it has one.
    pub fn x(&self) -> Option<f64> {
        match *self.data() {
              SegmentData::MoveTo { x, .. }
            | SegmentData::LineTo { x, .. }
            | SegmentData::HorizontalLineTo { x }
            | SegmentData::CurveTo { x, .. }
            | SegmentData::SmoothCurveTo { x, .. }
            | SegmentData::Quadratic { x, .. }
            | SegmentData::SmoothQuadratic { x, .. }
            | SegmentData::EllipticalArc { x, .. } => Some(x),

              SegmentData::VerticalLineTo { .. }
            | SegmentData::ClosePath => None,
        }
    }

    /// Returns the `y` coordinate of the segment if it has one.
    pub fn y(&self) -> Option<f64> {
        match *self.data() {
              SegmentData::MoveTo { y, .. }
            | SegmentData::LineTo { y, .. }
            | SegmentData::VerticalLineTo { y }
            | SegmentData::CurveTo { y, .. }
            | SegmentData::SmoothCurveTo { y, .. }
            | SegmentData::Quadratic { y, .. }
            | SegmentData::SmoothQuadratic { y, .. }
            | SegmentData::EllipticalArc { y, .. } => Some(y),

              SegmentData::HorizontalLineTo { .. }
            | SegmentData::ClosePath => None,
        }
    }

    /// Compares two segments using fuzzy float compare algorithm.
    ///
    /// Use it instead of `==`.
    ///
    /// It's not very fast.
    pub fn fuzzy_eq(&self, other: &Segment) -> bool {
        if self.absolute != other.absolute {
            return false;
        }

        use self::SegmentData as Seg;

        // TODO: find a way to wrap it in macro
        match (self.data, other.data) {
            (Seg::MoveTo { x, y }, Seg::MoveTo { x: ox, y: oy }) |
            (Seg::LineTo { x, y }, Seg::LineTo { x: ox, y: oy }) |
            (Seg::SmoothQuadratic { x, y }, Seg::SmoothQuadratic { x: ox, y: oy }) => {
                x.fuzzy_eq(&ox) && y.fuzzy_eq(&oy)
            }
            (Seg::HorizontalLineTo { x }, Seg::HorizontalLineTo { x: ox }) => {
                x.fuzzy_eq(&ox)
            }
            (Seg::VerticalLineTo { y }, Seg::VerticalLineTo { y: oy }) => {
                y.fuzzy_eq(&oy)
            }
            (Seg::CurveTo { x1, y1, x2, y2, x, y },
                Seg::CurveTo { x1: ox1, y1: oy1, x2: ox2, y2: oy2, x: ox, y: oy }) => {
                   x.fuzzy_eq(&ox)   && y.fuzzy_eq(&oy)
                && x1.fuzzy_eq(&ox1) && y1.fuzzy_eq(&oy1)
                && x2.fuzzy_eq(&ox2) && y2.fuzzy_eq(&oy2)
            }
            (Seg::SmoothCurveTo { x2, y2, x, y },
                Seg::SmoothCurveTo { x2: ox2, y2: oy2, x: ox, y: oy }) => {
                   x.fuzzy_eq(&ox)   && y.fuzzy_eq(&oy)
                && x2.fuzzy_eq(&ox2) && y2.fuzzy_eq(&oy2)
            }
            (Seg::Quadratic { x1, y1, x, y },
                Seg::Quadratic { x1: ox1, y1: oy1, x: ox, y: oy }) => {
                   x.fuzzy_eq(&ox)   && y.fuzzy_eq(&oy)
                && x1.fuzzy_eq(&ox1) && y1.fuzzy_eq(&oy1)
            }
            (Seg::EllipticalArc { rx, ry, x_axis_rotation, large_arc, sweep, x, y },
                Seg::EllipticalArc { rx: orx, ry: ory, x_axis_rotation: ox_axis_rotation,
                                     large_arc: olarge_arc, sweep: osweep, x: ox, y: oy }) => {
                   x.fuzzy_eq(&ox)   && y.fuzzy_eq(&oy)
                && rx.fuzzy_eq(&orx) && ry.fuzzy_eq(&ory)
                && x_axis_rotation == ox_axis_rotation
                && large_arc == olarge_arc
                && sweep == osweep
            }
            (Seg::ClosePath, Seg::ClosePath) => true,
            _ => false,
        }
    }
}

impl fmt::Display for Segment {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut buf: Vec<u8> = Vec::new();
        let mut opt = WriteOptions::default();
        opt.paths.use_compact_notation = true;

        writer::write_cmd_char(self, &mut buf);

        if self.cmd() != Command::ClosePath {
            buf.push(b' ');
        }

        writer::write_segment(self.data(), true, &mut false, &opt, &mut buf);

        write!(f, "{}", str::from_utf8(&buf).unwrap())
    }
}

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

    macro_rules! test_seg {
        ($name:ident,  $seg1:expr, $seg2:expr) => (
        #[test]
        fn $name() {
            assert!($seg1 != $seg2);
            assert!($seg1.fuzzy_eq(&$seg2));
        })
    }

    test_seg!(test_fuzzy_eq_m,
        Segment::new_move_to(10.0, 10.1 + 10.2),
        Segment::new_move_to(10.0, 20.3)
    );

    test_seg!(test_fuzzy_eq_l,
        Segment::new_line_to(10.0, 10.1 + 10.2),
        Segment::new_line_to(10.0, 20.3)
    );

    test_seg!(test_fuzzy_eq_h,
        Segment::new_hline_to(10.1 + 10.2),
        Segment::new_hline_to(20.3)
    );

    test_seg!(test_fuzzy_eq_v,
        Segment::new_vline_to(10.1 + 10.2),
        Segment::new_vline_to(20.3)
    );

    test_seg!(test_fuzzy_eq_c,
        Segment::new_curve_to(10.0, 10.1 + 10.2, 10.0, 10.0, 10.0, 10.0),
        Segment::new_curve_to(10.0, 20.3, 10.0, 10.0, 10.0, 10.0)
    );

    test_seg!(test_fuzzy_eq_s,
        Segment::new_smooth_curve_to(10.0, 10.1 + 10.2, 10.0, 10.0),
        Segment::new_smooth_curve_to(10.0, 20.3, 10.0, 10.0)
    );

    test_seg!(test_fuzzy_eq_q,
        Segment::new_smooth_curve_to(10.0, 10.1 + 10.2, 10.0, 10.0),
        Segment::new_smooth_curve_to(10.0, 20.3, 10.0, 10.0)
    );

    test_seg!(test_fuzzy_eq_t,
        Segment::new_smooth_quad_to(10.0, 10.1 + 10.2),
        Segment::new_smooth_quad_to(10.0, 20.3)
    );

    test_seg!(test_fuzzy_eq_a,
        Segment::new_arc_to(10.0, 10.0, 0.0, true, true, 10.0, 10.1 + 10.2),
        Segment::new_arc_to(10.0, 10.0, 0.0, true, true, 10.0, 20.3)
    );

    #[test]
    fn test_fuzzy_ne_1() {
        let seg1 = Segment::new_move_to(10.0, 10.0);
        let seg2 = Segment::new_move_to(10.0, 20.0);

        assert!(seg1 != seg2);
        assert!(!seg1.fuzzy_eq(&seg2));
    }

    #[test]
    fn test_seg_display_1() {
        let mut seg = Segment::new_move_to(10.0, 20.0);
        assert_eq_text!(format!("{}", seg), "M 10 20");

        seg.absolute = false;
        assert_eq_text!(format!("{}", seg), "m 10 20");

        assert_eq_text!(format!("{}", Segment::new_close_path()), "Z");
    }
}