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
// Copyright 2023 the Vello Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Append a [`usvg::Tree`] to a Vello [`Scene`]
//!
//! This currently lacks support for a [number of important](crate#unsupported-features) SVG features.
//! This is because this integration was developed for examples, which only need to support enough SVG
//! to demonstrate Vello.
//!
//! However, this is also intended to be the preferred integration between Vello and [usvg], so [consider
//! contributing](https://github.com/linebender/vello) if you need a feature which is missing.
//!
//! [`render_tree_with`] is the primary entry point function, which supports choosing the behaviour
//! when [unsupported features](crate#unsupported-features) are detected. In a future release where there are
//! no unsupported features, this may be phased out
//!
//! [`render_tree`] is a convenience wrapper around [`render_tree_with`] which renders an indicator around not
//! yet supported features
//!
//! This crate also re-exports [`usvg`], to make handling dependency versions easier
//!
//! # Unsupported features
//!
//! Missing features include:
//! - text
//! - group opacity
//! - mix-blend-modes
//! - clipping
//! - masking
//! - filter effects
//! - group background
//! - path shape-rendering
//! - patterns

mod geom;

use std::convert::Infallible;
use std::sync::Arc;
use vello::kurbo::{Affine, BezPath, Point, Rect, Stroke};
use vello::peniko::{BlendMode, Blob, Brush, Color, Fill, Image};
use vello::Scene;

/// Re-export vello.
pub use vello;

/// Re-export usvg.
pub use usvg;

/// Append a [`usvg::Tree`] into a Vello [`Scene`], with default error handling
/// This will draw a red box over (some) unsupported elements
///
/// Calls [`render_tree_with`] with an error handler implementing the above.
///
/// See the [module level documentation](crate#unsupported-features) for a list of some unsupported svg features
pub fn render_tree(scene: &mut Scene, svg: &usvg::Tree) {
    render_tree_with::<_, Infallible>(
        scene,
        svg,
        &usvg::Transform::identity(),
        &mut default_error_handler,
    )
    .unwrap_or_else(|e| match e {});
}

/// Append a [`usvg::Tree`] into a Vello [`Scene`].
///
/// Calls [`render_tree_with`] with [`default_error_handler`].
/// This will draw a red box over unsupported element types.
///
/// See the [module level documentation](crate#unsupported-features) for a list of some unsupported svg features
pub fn render_tree_with<F: FnMut(&mut Scene, &usvg::Node) -> Result<(), E>, E>(
    scene: &mut Scene,
    svg: &usvg::Tree,
    ts: &usvg::Transform,
    error_handler: &mut F,
) -> Result<(), E> {
    render_tree_impl(scene, svg, &svg.view_box(), ts, error_handler)
}

fn render_tree_impl<F: FnMut(&mut Scene, &usvg::Node) -> Result<(), E>, E>(
    scene: &mut Scene,
    svg: &usvg::Tree,
    view_box: &usvg::ViewBox,
    ts: &usvg::Transform,
    error_handler: &mut F,
) -> Result<(), E> {
    let transform = to_affine(ts);
    scene.push_layer(
        BlendMode {
            mix: vello::peniko::Mix::Clip,
            compose: vello::peniko::Compose::SrcOver,
        },
        1.0,
        transform,
        &vello::kurbo::Rect::new(
            view_box.rect.left().into(),
            view_box.rect.top().into(),
            view_box.rect.right().into(),
            view_box.rect.bottom().into(),
        ),
    );
    let (view_box_transform, clip) =
        geom::view_box_to_transform_with_clip(view_box, svg.size().to_int_size());
    if let Some(clip) = clip {
        scene.push_layer(
            BlendMode {
                mix: vello::peniko::Mix::Clip,
                compose: vello::peniko::Compose::SrcOver,
            },
            1.0,
            transform,
            &vello::kurbo::Rect::new(
                clip.left().into(),
                clip.top().into(),
                clip.right().into(),
                clip.bottom().into(),
            ),
        );
    }
    render_group(
        scene,
        svg.root(),
        &ts.pre_concat(view_box_transform)
            .pre_concat(svg.root().transform()),
        error_handler,
    )?;
    if clip.is_some() {
        scene.pop_layer();
    }
    scene.pop_layer();

    Ok(())
}

fn render_group<F: FnMut(&mut Scene, &usvg::Node) -> Result<(), E>, E>(
    scene: &mut Scene,
    group: &usvg::Group,
    ts: &usvg::Transform,
    error_handler: &mut F,
) -> Result<(), E> {
    for node in group.children() {
        let transform = to_affine(ts);
        match node {
            usvg::Node::Group(g) => {
                let mut pushed_clip = false;
                if let Some(clip_path) = g.clip_path() {
                    if let Some(usvg::Node::Path(clip_path)) = clip_path.root().children().first() {
                        // support clip-path with a single path
                        let local_path = to_bez_path(clip_path);
                        scene.push_layer(
                            BlendMode {
                                mix: vello::peniko::Mix::Clip,
                                compose: vello::peniko::Compose::SrcOver,
                            },
                            1.0,
                            transform,
                            &local_path,
                        );
                        pushed_clip = true;
                    }
                }

                render_group(scene, g, &ts.pre_concat(g.transform()), error_handler)?;

                if pushed_clip {
                    scene.pop_layer();
                }
            }
            usvg::Node::Path(path) => {
                if path.visibility() != usvg::Visibility::Visible {
                    continue;
                }
                let local_path = to_bez_path(path);

                let do_fill = |scene: &mut Scene, error_handler: &mut F| {
                    if let Some(fill) = &path.fill() {
                        if let Some((brush, brush_transform)) =
                            paint_to_brush(fill.paint(), fill.opacity())
                        {
                            scene.fill(
                                match fill.rule() {
                                    usvg::FillRule::NonZero => Fill::NonZero,
                                    usvg::FillRule::EvenOdd => Fill::EvenOdd,
                                },
                                transform,
                                &brush,
                                Some(brush_transform),
                                &local_path,
                            );
                        } else {
                            return error_handler(scene, node);
                        }
                    }
                    Ok(())
                };
                let do_stroke = |scene: &mut Scene, error_handler: &mut F| {
                    if let Some(stroke) = &path.stroke() {
                        if let Some((brush, brush_transform)) =
                            paint_to_brush(stroke.paint(), stroke.opacity())
                        {
                            let mut conv_stroke = Stroke::new(stroke.width().get() as f64)
                                .with_caps(match stroke.linecap() {
                                    usvg::LineCap::Butt => vello::kurbo::Cap::Butt,
                                    usvg::LineCap::Round => vello::kurbo::Cap::Round,
                                    usvg::LineCap::Square => vello::kurbo::Cap::Square,
                                })
                                .with_join(match stroke.linejoin() {
                                    usvg::LineJoin::Miter | usvg::LineJoin::MiterClip => {
                                        vello::kurbo::Join::Miter
                                    }
                                    usvg::LineJoin::Round => vello::kurbo::Join::Round,
                                    usvg::LineJoin::Bevel => vello::kurbo::Join::Bevel,
                                })
                                .with_miter_limit(stroke.miterlimit().get() as f64);
                            if let Some(dash_array) = stroke.dasharray().as_ref() {
                                conv_stroke = conv_stroke.with_dashes(
                                    stroke.dashoffset() as f64,
                                    dash_array.iter().map(|x| *x as f64),
                                );
                            }
                            scene.stroke(
                                &conv_stroke,
                                transform,
                                &brush,
                                Some(brush_transform),
                                &local_path,
                            );
                        } else {
                            return error_handler(scene, node);
                        }
                    }
                    Ok(())
                };
                match path.paint_order() {
                    usvg::PaintOrder::FillAndStroke => {
                        do_fill(scene, error_handler)?;
                        do_stroke(scene, error_handler)?;
                    }
                    usvg::PaintOrder::StrokeAndFill => {
                        do_stroke(scene, error_handler)?;
                        do_fill(scene, error_handler)?;
                    }
                }
            }
            usvg::Node::Image(img) => {
                if img.visibility() != usvg::Visibility::Visible {
                    continue;
                }
                match img.kind() {
                    usvg::ImageKind::JPEG(_)
                    | usvg::ImageKind::PNG(_)
                    | usvg::ImageKind::GIF(_) => {
                        let Ok(decoded_image) = decode_raw_raster_image(img.kind()) else {
                            error_handler(scene, node)?;
                            continue;
                        };
                        let Some(size) = usvg::Size::from_wh(
                            decoded_image.width() as f32,
                            decoded_image.height() as f32,
                        ) else {
                            error_handler(scene, node)?;
                            continue;
                        };
                        let view_box = img.view_box();
                        let new_size = geom::fit_view_box(size, &view_box);
                        let (tx, ty) = usvg::utils::aligned_pos(
                            view_box.aspect.align,
                            view_box.rect.x(),
                            view_box.rect.y(),
                            view_box.rect.width() - new_size.width(),
                            view_box.rect.height() - new_size.height(),
                        );
                        let (sx, sy) = (
                            new_size.width() / size.width(),
                            new_size.height() / size.height(),
                        );
                        let view_box_transform =
                            usvg::Transform::from_row(sx, 0.0, 0.0, sy, tx, ty);
                        let (width, height) = (decoded_image.width(), decoded_image.height());
                        scene.push_layer(
                            BlendMode {
                                mix: vello::peniko::Mix::Clip,
                                compose: vello::peniko::Compose::SrcOver,
                            },
                            1.0,
                            transform,
                            &vello::kurbo::Rect::new(
                                view_box.rect.left().into(),
                                view_box.rect.top().into(),
                                view_box.rect.right().into(),
                                view_box.rect.bottom().into(),
                            ),
                        );

                        let image_ts = to_affine(&ts.pre_concat(view_box_transform));
                        let image_data: Arc<Vec<u8>> = decoded_image.into_vec().into();
                        scene.draw_image(
                            &Image::new(
                                Blob::new(image_data),
                                vello::peniko::Format::Rgba8,
                                width,
                                height,
                            ),
                            image_ts,
                        );

                        scene.pop_layer();
                    }
                    usvg::ImageKind::SVG(svg) => {
                        render_tree_impl(scene, svg, &img.view_box(), ts, error_handler)?;
                    }
                }
            }
            usvg::Node::Text(_) => {
                error_handler(scene, node)?;
            }
        }
    }

    Ok(())
}

fn decode_raw_raster_image(img: &usvg::ImageKind) -> Result<image::RgbaImage, image::ImageError> {
    let res = match img {
        usvg::ImageKind::JPEG(data) => {
            image::load_from_memory_with_format(data, image::ImageFormat::Jpeg)
        }
        usvg::ImageKind::PNG(data) => {
            image::load_from_memory_with_format(data, image::ImageFormat::Png)
        }
        usvg::ImageKind::GIF(data) => {
            image::load_from_memory_with_format(data, image::ImageFormat::Gif)
        }
        usvg::ImageKind::SVG(_) => unreachable!(),
    }?
    .into_rgba8();
    Ok(res)
}

fn to_affine(ts: &usvg::Transform) -> Affine {
    let usvg::Transform {
        sx,
        kx,
        ky,
        sy,
        tx,
        ty,
    } = ts;
    Affine::new([sx, kx, ky, sy, tx, ty].map(|&x| f64::from(x)))
}

fn to_bez_path(path: &usvg::Path) -> BezPath {
    let mut local_path = BezPath::new();
    // The semantics of SVG paths don't line up with `BezPath`; we
    // must manually track initial points
    let mut just_closed = false;
    let mut most_recent_initial = (0., 0.);
    for elt in path.data().segments() {
        match elt {
            usvg::tiny_skia_path::PathSegment::MoveTo(p) => {
                if std::mem::take(&mut just_closed) {
                    local_path.move_to(most_recent_initial);
                }
                most_recent_initial = (p.x.into(), p.y.into());
                local_path.move_to(most_recent_initial)
            }
            usvg::tiny_skia_path::PathSegment::LineTo(p) => {
                if std::mem::take(&mut just_closed) {
                    local_path.move_to(most_recent_initial);
                }
                local_path.line_to(Point::new(p.x as f64, p.y as f64))
            }
            usvg::tiny_skia_path::PathSegment::QuadTo(p1, p2) => {
                if std::mem::take(&mut just_closed) {
                    local_path.move_to(most_recent_initial);
                }
                local_path.quad_to(
                    Point::new(p1.x as f64, p1.y as f64),
                    Point::new(p2.x as f64, p2.y as f64),
                )
            }
            usvg::tiny_skia_path::PathSegment::CubicTo(p1, p2, p3) => {
                if std::mem::take(&mut just_closed) {
                    local_path.move_to(most_recent_initial);
                }
                local_path.curve_to(
                    Point::new(p1.x as f64, p1.y as f64),
                    Point::new(p2.x as f64, p2.y as f64),
                    Point::new(p3.x as f64, p3.y as f64),
                )
            }
            usvg::tiny_skia_path::PathSegment::Close => {
                just_closed = true;
                local_path.close_path()
            }
        }
    }

    local_path
}

/// Error handler function for [`render_tree_with`] which draws a transparent red box
/// instead of unsupported SVG features
pub fn default_error_handler(scene: &mut Scene, node: &usvg::Node) -> Result<(), Infallible> {
    let bb = node.bounding_box();
    let rect = Rect {
        x0: bb.left() as f64,
        y0: bb.top() as f64,
        x1: bb.right() as f64,
        y1: bb.bottom() as f64,
    };
    scene.fill(
        Fill::NonZero,
        Affine::IDENTITY,
        Color::RED.with_alpha_factor(0.5),
        None,
        &rect,
    );

    Ok(())
}

fn paint_to_brush(paint: &usvg::Paint, opacity: usvg::Opacity) -> Option<(Brush, Affine)> {
    match paint {
        usvg::Paint::Color(color) => Some((
            Brush::Solid(Color::rgba8(
                color.red,
                color.green,
                color.blue,
                opacity.to_u8(),
            )),
            Affine::IDENTITY,
        )),
        usvg::Paint::LinearGradient(gr) => {
            let stops: Vec<vello::peniko::ColorStop> = gr
                .stops()
                .iter()
                .map(|stop| {
                    let mut cstop = vello::peniko::ColorStop::default();
                    cstop.color.r = stop.color().red;
                    cstop.color.g = stop.color().green;
                    cstop.color.b = stop.color().blue;
                    cstop.color.a = (stop.opacity() * opacity).to_u8();
                    cstop.offset = stop.offset().get();
                    cstop
                })
                .collect();
            let start = Point::new(gr.x1() as f64, gr.y1() as f64);
            let end = Point::new(gr.x2() as f64, gr.y2() as f64);
            let arr = [
                gr.transform().sx,
                gr.transform().ky,
                gr.transform().kx,
                gr.transform().sy,
                gr.transform().tx,
                gr.transform().ty,
            ]
            .map(f64::from);
            let transform = Affine::new(arr);
            let gradient =
                vello::peniko::Gradient::new_linear(start, end).with_stops(stops.as_slice());
            Some((Brush::Gradient(gradient), transform))
        }
        usvg::Paint::RadialGradient(gr) => {
            let stops: Vec<vello::peniko::ColorStop> = gr
                .stops()
                .iter()
                .map(|stop| {
                    let mut cstop = vello::peniko::ColorStop::default();
                    cstop.color.r = stop.color().red;
                    cstop.color.g = stop.color().green;
                    cstop.color.b = stop.color().blue;
                    cstop.color.a = (stop.opacity() * opacity).to_u8();
                    cstop.offset = stop.offset().get();
                    cstop
                })
                .collect();

            let start_center = Point::new(gr.cx() as f64, gr.cy() as f64);
            let end_center = Point::new(gr.fx() as f64, gr.fy() as f64);
            let start_radius = 0_f32;
            let end_radius = gr.r().get();
            let arr = [
                gr.transform().sx,
                gr.transform().ky,
                gr.transform().kx,
                gr.transform().sy,
                gr.transform().tx,
                gr.transform().ty,
            ]
            .map(f64::from);
            let transform = Affine::new(arr);
            let gradient = vello::peniko::Gradient::new_two_point_radial(
                start_center,
                start_radius,
                end_center,
                end_radius,
            )
            .with_stops(stops.as_slice());
            Some((Brush::Gradient(gradient), transform))
        }
        usvg::Paint::Pattern(_) => None,
    }
}