vsvg 0.5.0

Core library for pen-plotter graphics.
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
use crate::svg::inkscape_layer_preprocessor::{preprocess_inkscape_layer, GroupInfo};
use crate::{
    Color, Document, DocumentTrait, IntoBezPath, Layer, LayerID, LayerTrait, PageSize, Path,
    PathTrait,
};
use kurbo::{BezPath, PathEl};
use lazy_static::lazy_static;
use regex::Regex;
use std::{error::Error, fs, path};
use usvg::{tiny_skia_path::PathSegment, Tree};

impl IntoBezPath for usvg::tiny_skia_path::Path {
    fn into_bezpath(self) -> BezPath {
        fn p2p(p: usvg::tiny_skia_path::Point) -> kurbo::Point {
            kurbo::Point::new(f64::from(p.x), f64::from(p.y))
        }

        self.segments()
            .map(|seg| match seg {
                PathSegment::MoveTo(p) => PathEl::MoveTo(p2p(p)),
                PathSegment::LineTo(p) => PathEl::LineTo(p2p(p)),
                PathSegment::QuadTo(p0, p1) => PathEl::QuadTo(p2p(p0), p2p(p1)),
                PathSegment::CubicTo(p0, p1, p2) => PathEl::CurveTo(p2p(p0), p2p(p1), p2p(p2)),
                PathSegment::Close => PathEl::ClosePath,
            })
            .collect()
    }
}

impl Path {
    #[must_use]
    fn from_usvg(svg_path: &usvg::Path) -> Self {
        let mut skia_path = (*svg_path.data()).clone();
        skia_path = skia_path.transform(svg_path.abs_transform()).unwrap();

        let mut res = Self {
            data: skia_path.into_bezpath(),
            ..Default::default()
        };

        // extract metadata
        if let Some(stroke) = &svg_path.stroke() {
            if let usvg::Paint::Color(c) = stroke.paint() {
                res.metadata_mut().color = Color {
                    r: c.red,
                    g: c.green,
                    b: c.blue,
                    a: stroke.opacity().to_u8(),
                };
            }
            res.metadata_mut().stroke_width = f64::from(stroke.width().get());
        }

        res
    }
}

fn parse_group(group: &usvg::Group, layer: &mut Layer) {
    group.children().iter().for_each(|node| match node {
        usvg::Node::Path(path) => {
            layer.paths.push(Path::from_usvg(path));
        }
        usvg::Node::Group(group) => {
            parse_group(group, layer);
        }
        _ => {}
    });
}

lazy_static! {
    static ref DIGITS_RE: Regex = Regex::new(r"\d+").unwrap();
}

/// Interpret the attributes of a top-level group to determine its layer ID.
///
/// See <https://github.com/abey79/vsvg/issues/7> for the strategy used here.
fn layer_id_from_attribute(id: &str, label: Option<&str>) -> Option<LayerID> {
    fn extract_id(id: &str) -> Option<LayerID> {
        DIGITS_RE.find(id).map(|m| {
            let mut id = m
                .as_str()
                .parse::<usize>()
                .expect("regex guarantees only digits");
            if id == 0 {
                id = 1;
            }

            id
        })
    }

    if let Some(id) = label {
        let lid = extract_id(id);
        if lid.is_some() {
            return lid;
        }
    }

    extract_id(id)
}

impl Document {
    /// Create a `Document` based on a path to an SVG file.
    ///
    /// See [`Document::from_string`] for more details on layer handling.
    pub fn from_svg<P: AsRef<path::Path>>(
        path: P,
        single_layer: bool,
    ) -> Result<Self, Box<dyn Error>> {
        let svg = fs::read_to_string(path)?;
        Document::from_string(&svg, single_layer)
    }

    /// Create a `Document` based on a string containing SVG data.
    ///
    /// The `single_layer` parameter determines how layer are handled. If `true`, all content is
    /// added to layer 0. Other each top-level group is added to a layer based on the following
    /// rules:
    ///
    /// 1. If the group has an `inkscape:groupmode` attribute with the value `layer`, then the
    ///    layer ID is determined by the first group of digits in the `inkscape:label` attribute, if
    ///    any.
    /// 2. Otherwise, the layer ID is determined by teh first group of digits in the `id` attribute,
    ///    if any.
    /// 3. If neither of the above rules apply, then the layer ID is determined by the top-level
    ///    group's order of appearance in the SVG file.
    pub fn from_string(svg: &str, single_layer: bool) -> Result<Self, Box<dyn Error>> {
        let preprocessed_svg;

        let tree = Tree::from_str(
            if single_layer {
                svg
            } else {
                preprocessed_svg = preprocess_inkscape_layer(svg)?;
                preprocessed_svg.as_str()
            },
            &usvg::Options::default(),
        )?;

        // add frame for the page
        let (w, h) = (
            f64::from(tree.size().width()),
            f64::from(tree.size().height()),
        );
        let mut doc = Document::new_with_page_size(PageSize::new(w, h));

        if single_layer {
            doc.load_tree(&tree);
        } else {
            doc.load_tree_multilayer(&tree);
        }

        doc.crop(0., 0., w, h);
        Ok(doc)
    }

    /// Load a [Tree] into this document. All content is added to layer 0.
    fn load_tree(&mut self, tree: &Tree) {
        let layer = self.get_mut(0);
        for child in tree.root().children() {
            match child {
                usvg::Node::Group(group) => {
                    parse_group(group, layer);
                }
                usvg::Node::Path(path) => {
                    layer.paths.push(Path::from_usvg(path));
                }
                _ => {}
            }
        }
    }

    /// Load a [Tree] into this document, splitting the content into multiple layers.
    ///
    /// See [`Document::from_string`] for more details on layer handling.
    fn load_tree_multilayer(&mut self, tree: &Tree) {
        let mut top_level_index = 0;
        let root_group = tree.root();

        // Check if the top-level group is a virtual group created by usvg, for example to implement
        // the `viewBox` transform. Due to the preprocessing step, no `<g>` from the input ever has
        // an empty ID, so we can safely assume that an empty ID means this is a virtual group.
        let group_to_parse = if let [usvg::Node::Group(group)] = root_group.children() {
            if group.id().is_empty() {
                group
            } else {
                root_group
            }
        } else {
            root_group
        };

        for child in group_to_parse.children() {
            match child {
                usvg::Node::Group(group_data) => {
                    let group_info = GroupInfo::decode(group_data.id());
                    let layer_id;
                    let layer_name;
                    match group_info {
                        // top-level group with inkscape layer information
                        Some(group_info) if group_info.groupmode.as_deref() == Some("layer") => {
                            top_level_index += 1;
                            layer_id = layer_id_from_attribute(
                                group_info.id.as_deref().unwrap_or(""),
                                group_info.label.as_deref(),
                            );

                            layer_name = if group_info.label.is_some() {
                                group_info.label
                            } else {
                                group_info.id
                            };
                        }

                        // top-level group without layer information
                        Some(group_info) => {
                            top_level_index += 1;
                            layer_id = layer_id_from_attribute(
                                group_info.id.as_deref().unwrap_or(""),
                                None,
                            );
                            layer_name =
                                group_info
                                    .id
                                    .and_then(|id| if id.is_empty() { None } else { Some(id) });
                        }

                        // this is a top-level path that was embedded in a group by usvg
                        None => {
                            layer_id = Some(0);
                            layer_name = None;
                        }
                    }

                    let layer = self.get_mut(layer_id.unwrap_or(top_level_index));
                    parse_group(group_data, layer);

                    layer.metadata_mut().name = layer_name;
                }
                usvg::Node::Path(path) => {
                    self.get_mut(0).paths.push(Path::from_usvg(path));
                }
                _ => {}
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{test_file, Document, DocumentTrait, LayerTrait, PathDataTrait};
    use kurbo::BezPath;

    #[test]
    fn test_top_level_path_in_layer_0() {
        let doc = Document::from_svg(test_file!("multilayer.svg"), false).unwrap();
        assert_eq!(doc.layers.len(), 3);
        assert_eq!(doc.try_get(0).unwrap().paths.len(), 1);
        assert_eq!(doc.try_get(2).unwrap().paths.len(), 2);
        assert_eq!(doc.try_get(3).unwrap().paths.len(), 1);
    }

    #[test]
    fn test_one_layer() {
        let doc = Document::from_svg(test_file!("singlelayer.svg"), false).unwrap();
        assert_eq!(doc.layers.len(), 1);
        assert_eq!(doc.try_get(3).unwrap().paths.len(), 1);
    }

    #[test]
    fn test_virtual_group() {
        // this SVG triggers the creation of a virtual group by usvg, which should not be considered
        // a top-level group and should not be assigned a layer ID
        let doc = Document::from_svg(test_file!("spurious_group.svg"), false).unwrap();
        assert_eq!(doc.layers.len(), 1);
        assert_eq!(doc.try_get(0).unwrap().paths.len(), 2);
    }

    #[test]
    fn test_single_layer() {
        for file in &["singlelayer.svg", "multilayer.svg", "spurious_group.svg"] {
            let doc = Document::from_svg(test_file!(file), true).unwrap();
            assert_eq!(doc.layers.len(), 1);
            assert!(!doc.try_get(0).unwrap().paths.is_empty());
        }
    }

    #[test]
    fn test_layer_names() {
        let doc = Document::from_string(
            r#"<?xml version="1.0"?>
            <svg xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
               xmlns="http://www.w3.org/2000/svg"
               width="100" height="100" viewBox="50 50 10 10">
                <g id="layer10" inkscape:label="Layer 10" inkscape:groupmode="layer">
                  <line x1="50" y1="50" x2="60" y2="60" />
                </g>
                <g id="layer11" >
                  <line x1="50" y1="50" x2="60" y2="60" />
                </g>
                <g inkscape:label="Hello" inkscape:groupmode="layer">
                  <line x1="50" y1="50" x2="60" y2="60" />
                </g>
                <g id="world">
                  <line x1="50" y1="50" x2="60" y2="60" />
                </g>
                <g id="notaname" inkscape:label="layer_name" inkscape:groupmode="layer">
                  <line x1="50" y1="50" x2="60" y2="60" />
                </g>
            </svg>"#,
            false,
        )
        .unwrap();

        assert_eq!(doc.layers.len(), 5);
        assert_eq!(
            doc.try_get(10).unwrap().metadata().name,
            Some("Layer 10".to_owned())
        );
        assert_eq!(
            doc.try_get(11).unwrap().metadata().name,
            Some("layer11".to_owned())
        );
        assert_eq!(
            doc.try_get(3).unwrap().metadata().name,
            Some("Hello".to_owned())
        );
        assert_eq!(
            doc.try_get(4).unwrap().metadata().name,
            Some("world".to_owned())
        );
        assert_eq!(
            doc.try_get(5).unwrap().metadata().name,
            Some("layer_name".to_owned())
        );
    }

    #[test]
    fn test_layer_numbering() {
        let doc = Document::from_string(
            r#"<?xml version="1.0"?>
            <svg xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
               xmlns="http://www.w3.org/2000/svg"
               width="100" height="100" viewBox="50 50 10 10">
                <g id="layer_one" >
                  <line x1="50" y1="50" x2="60" y2="60" />
                </g>
                <!-- this should trigger a virtual layer -->
                <line x1="50" y1="50" x2="60" y2="60" opacity="0.5" />
                <g id="layer11" >
                  <line x1="50" y1="50" x2="60" y2="60" />
                </g>
                <g id="layer_three" >
                  <line x1="50" y1="50" x2="60" y2="60" />
                </g>
            </svg>"#,
            false,
        )
        .unwrap();

        assert_eq!(doc.layers.len(), 4);
        assert_eq!(doc.try_get(0).unwrap().paths.len(), 1);
        assert_eq!(
            doc.try_get(1).unwrap().metadata().name,
            Some("layer_one".to_owned())
        );
        assert_eq!(
            doc.try_get(11).unwrap().metadata().name,
            Some("layer11".to_owned())
        );
        assert_eq!(
            doc.try_get(3).unwrap().metadata().name,
            Some("layer_three".to_owned())
        );
    }

    #[test]
    fn test_empty_path() {
        let doc = Document::from_string(
            r#"<?xml version="1.0"?>
            <svg xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
               xmlns="http://www.w3.org/2000/svg"
               width="100" height="100" viewBox="50 50 10 10">
                <path d="" />
                <path d="M 0 0" />
            </svg>"#,
            false,
        )
        .unwrap();

        assert_eq!(doc.layers.len(), 0);
    }

    #[ignore] //TODO: this needs to be fixed in usvg
    #[test]
    fn test_point_path() {
        let doc = Document::from_string(
            r#"<?xml version="1.0"?>
            <svg xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
               xmlns="http://www.w3.org/2000/svg"
               width="100" height="100" viewBox="50 50 10 10">
                <path d="M 10,0 L 10,0" />
            </svg>"#,
            false,
        )
        .unwrap();

        assert_eq!(doc.layers.len(), 1);
        assert_eq!(doc.try_get(0).unwrap().paths.len(), 1);
        assert!(doc.try_get(0).unwrap().paths[0].data.is_point());
    }

    #[test]
    fn test_viewbox() {
        let doc = Document::from_string(
            r#"<?xml version="1.0"?>
            <svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg"
               width="100" height="100" viewBox="50 50 10 10">
               <line x1="50" y1="50" x2="60" y2="60" />
            </svg>"#,
            false,
        )
        .unwrap();

        let page_size = doc.metadata().page_size.unwrap();
        assert_eq!(page_size.w(), 100.);
        assert_eq!(page_size.h(), 100.);
        assert_eq!(doc.try_get(0).unwrap().paths.len(), 1);
        assert_eq!(
            doc.try_get(0).unwrap().paths[0].data,
            BezPath::from_svg("M 0 0 L 100 100").unwrap()
        );
    }

    #[test]
    fn test_transforms() {
        let doc = Document::from_string(
            r#"<?xml version="1.0"?>
            <svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg"
               width="100" height="100" >
                <g transform="translate(10, 20)">
                    <g transform="rotate(90)">
                        <line x1="0" y1="0" x2="10" y2="10" transform="scale(0.5, 0.5)" />
                    </g>
                </g>
            </svg>"#,
            false,
        )
        .unwrap();

        assert_eq!(doc.try_get(1).unwrap().paths.len(), 1);
        assert_eq!(
            doc.try_get(1).unwrap().paths[0].data,
            // ground truth obtained with vpype
            BezPath::from_svg("M 10 20 L 5 25").unwrap()
        );
    }

    #[test]
    fn test_transforms_viewbox() {
        let doc = Document::from_string(
            r#"<?xml version="1.0"?>
            <svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg"
               width="100" height="100" viewBox="0 0 100 50">
                <g transform="translate(10, 20)">
                    <g transform="rotate(90)">
                        <line x1="0" y1="0" x2="10" y2="10" transform="scale(0.5, 0.5)" />
                    </g>
                </g>
            </svg>"#,
            false,
        )
        .unwrap();

        assert_eq!(doc.try_get(1).unwrap().paths.len(), 1);
        assert_eq!(
            doc.try_get(1).unwrap().paths[0].data,
            // ground truth obtained with vpype
            BezPath::from_svg("M 10 45 L 5 50").unwrap()
        );
    }

    #[test]
    fn test_reader_quad_beziers() {
        let doc = Document::from_string(
            r##"<svg height="200.00000" viewBox="200.00000 200.00000 150.00000 200.00000" width="150.00000" xmlns="http://www.w3.org/2000/svg">
<g id="layer3">
<path d="M200,200 L200,400 Q500,300,200,200 z" fill="none" stroke="#006400" stroke-width="3"/>
</g>
</svg>"##,
            false,
        ).unwrap();

        //TODO: the crop removes the Close command, which is not ideal...
        assert_eq!(
            doc.try_get(3).unwrap().paths[0].data,
            BezPath::from_svg("M 0 0 L 0 200 Q 300 100 0 0").unwrap()
        );
    }
}