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
use lyon::math::Point;
use lyon::path::Path;
use lyon::tessellation;
use lyon::tessellation::geometry_builder::{simple_builder, VertexBuffers};
use usvg::TreeParsing;

use abstutil::VecMap;
use geom::{Bounds, Pt2D, Tessellation};

use crate::{Color, Fill, GeomBatch, LinearGradient, Prerender};

pub const HIGH_QUALITY: f32 = 0.01;
pub const LOW_QUALITY: f32 = 1.0;

// Code here adapted from
// https://github.com/nical/lyon/blob/0d0ee771180fb317b986d9cf30266722e0773e01/examples/wgpu_svg/src/main.rs

pub fn load_svg(prerender: &Prerender, filename: &str) -> (GeomBatch, Bounds) {
    let cache_key = format!("file://{}", filename);
    if let Some(pair) = prerender.assets.get_cached_svg(&cache_key) {
        return pair;
    }

    let bytes = (prerender.assets.read_svg)(filename);
    load_svg_from_bytes_uncached(&bytes)
        .map(|(batch, bounds)| {
            prerender.assets.cache_svg(cache_key, batch.clone(), bounds);
            (batch, bounds)
        })
        .unwrap_or_else(|_| panic!("error loading svg: {}", filename))
}

pub fn load_svg_bytes(
    prerender: &Prerender,
    cache_key: &str,
    bytes: &[u8],
) -> anyhow::Result<(GeomBatch, Bounds)> {
    let cache_key = format!("bytes://{}", cache_key);
    if let Some(pair) = prerender.assets.get_cached_svg(&cache_key) {
        return Ok(pair);
    }

    load_svg_from_bytes_uncached(bytes).map(|(batch, bounds)| {
        prerender.assets.cache_svg(cache_key, batch.clone(), bounds);
        (batch, bounds)
    })
}

pub fn load_svg_from_bytes_uncached(bytes: &[u8]) -> anyhow::Result<(GeomBatch, Bounds)> {
    let svg_tree = usvg::Tree::from_data(bytes, &usvg::Options::default())?;
    let mut batch = GeomBatch::new();
    match add_svg_inner(&mut batch, svg_tree, HIGH_QUALITY) {
        Ok(bounds) => Ok((batch, bounds)),
        Err(err) => Err(anyhow!(err)),
    }
}

// No offset. I'm not exactly sure how the simplification in usvg works, but this doesn't support
// transforms or strokes or text, just fills. Luckily, all of the files exported from Figma so far
// work just fine.
pub(crate) fn add_svg_inner(
    batch: &mut GeomBatch,
    svg_tree: usvg::Tree,
    tolerance: f32,
) -> Result<Bounds, String> {
    let mut fill_tess = tessellation::FillTessellator::new();
    let mut stroke_tess = tessellation::StrokeTessellator::new();
    // TODO This breaks on start.svg; the order there matters. color1, color2, then color1 again.
    let mut mesh_per_color: VecMap<Fill, VertexBuffers<_, u16>> = VecMap::new();

    for node in svg_tree.root.descendants() {
        if let usvg::NodeKind::Path(ref p) = *node.borrow() {
            // TODO Handle transforms

            if let Some(ref fill) = p.fill {
                let color = convert_color(&fill.paint, fill.opacity.get());
                let geom = mesh_per_color.mut_or_insert(color, VertexBuffers::new);
                if let Err(err) = fill_tess.tessellate(
                    &convert_path(p),
                    &tessellation::FillOptions::tolerance(tolerance),
                    &mut simple_builder(geom),
                ) {
                    return Err(format!("Couldn't tessellate something: {err}"));
                }
            }

            if let Some(ref stroke) = p.stroke {
                let (color, stroke_opts) = convert_stroke(stroke, tolerance);
                let geom = mesh_per_color.mut_or_insert(color, VertexBuffers::new);
                stroke_tess
                    .tessellate(&convert_path(p), &stroke_opts, &mut simple_builder(geom))
                    .unwrap();
            }
        }
    }

    for (color, mesh) in mesh_per_color.consume() {
        batch.push(
            color,
            Tessellation::new(
                mesh.vertices
                    .into_iter()
                    .map(|v| Pt2D::new(f64::from(v.x), f64::from(v.y)))
                    .collect(),
                mesh.indices.into_iter().map(|idx| idx as usize).collect(),
            ),
        );
    }
    Ok(Bounds::from(&[
        Pt2D::new(0.0, 0.0),
        Pt2D::new(svg_tree.size.width(), svg_tree.size.height()),
    ]))
}

fn convert_path(p: &usvg::Path) -> Path {
    let mut builder = Path::builder().with_svg();
    for segment in p.data.segments() {
        match segment {
            usvg::PathSegment::MoveTo { x, y } => {
                builder.move_to(Point::new(x as f32, y as f32));
            }
            usvg::PathSegment::LineTo { x, y } => {
                builder.line_to(Point::new(x as f32, y as f32));
            }
            usvg::PathSegment::CurveTo {
                x1,
                y1,
                x2,
                y2,
                x,
                y,
            } => {
                builder.cubic_bezier_to(
                    Point::new(x1 as f32, y1 as f32),
                    Point::new(x2 as f32, y2 as f32),
                    Point::new(x as f32, y as f32),
                );
            }
            usvg::PathSegment::ClosePath => {
                builder.close();
            }
        }
    }
    builder.build()
}

fn convert_stroke(s: &usvg::Stroke, tolerance: f32) -> (Fill, tessellation::StrokeOptions) {
    let color = convert_color(&s.paint, s.opacity.get());
    let linecap = match s.linecap {
        usvg::LineCap::Butt => tessellation::LineCap::Butt,
        usvg::LineCap::Square => tessellation::LineCap::Square,
        usvg::LineCap::Round => tessellation::LineCap::Round,
    };
    let linejoin = match s.linejoin {
        usvg::LineJoin::Miter => tessellation::LineJoin::Miter,
        usvg::LineJoin::Bevel => tessellation::LineJoin::Bevel,
        usvg::LineJoin::Round => tessellation::LineJoin::Round,
    };

    let opt = tessellation::StrokeOptions::tolerance(tolerance)
        .with_line_width(s.width.get() as f32)
        .with_line_cap(linecap)
        .with_line_join(linejoin);

    (color, opt)
}

fn convert_color(paint: &usvg::Paint, opacity: f64) -> Fill {
    match paint {
        usvg::Paint::Color(c) => Fill::Color(Color::rgba(
            c.red as usize,
            c.green as usize,
            c.blue as usize,
            opacity as f32,
        )),
        usvg::Paint::LinearGradient(lg) => LinearGradient::new_fill(lg),
        // No patterns or radial gradiants
        _ => panic!("Unsupported color style {:?}", paint),
    }
}