the aliasing svg renderer
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
use anyhow::Result;
use std::rc::Rc;
use tiny_skia_path::{NonZeroPositiveF32, NormalizedF32, Path, Point, Size, Transform};
use usvg::{Color, Fill, Opacity, Options, Paint, TreeParsing};
use vecto::Vector2;

#[derive(Debug)]
pub struct Tree {
    pub width: f32,
    pub height: f32,
    pub children: Box<[Node]>,
}

impl std::fmt::Debug for Node {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        fn pack(Color { red, green, blue }: Color, a: Opacity) -> u64 {
            ((red as u64) << 24)
                | ((green as u64) << 16)
                | ((blue as u64) << 8)
                | (a.to_u8() as u64)
        }

        macro_rules! hex {
            ($c:expr, $a:expr) => {
                &format_args!("#{:x}", pack($c, $a))
            };
        }
        // can be transmuted etc but takes much effort
        fn vec(points: &[Point]) -> Vec<Vector2<u64>> {
            points
                .iter()
                .map(|&Point { x, y }| Vector2::new(x.round() as u64, y.round() as u64))
                .collect()
        }
        match self {
            Self::Fill {
                color,
                opacity,
                path,
            } => f
                .debug_struct("Fill")
                .field("color", hex!(*color, *opacity))
                .field("path", &format_args!("{:?}", vec(path)))
                .finish(),
            Self::Stroke {
                color,
                opacity,
                stroke_color,
                stroke_opacity,
                stroke,
                path,
            } => f
                .debug_struct("Stroke")
                .field("color", hex!(*color, *opacity))
                .field("stroke_color", hex!(*stroke_color, *stroke_opacity))
                .field("stroke", &stroke.get())
                .field("path", &format_args!("{:?}", vec(path)))
                .finish(),
        }
    }
}

pub enum Node {
    Fill {
        color: Color,
        opacity: Opacity,
        path: Box<[Point]>,
    },
    Stroke {
        color: Color,
        opacity: Opacity,
        stroke_color: Color,
        stroke_opacity: Opacity,
        stroke: NonZeroPositiveF32,
        path: Box<[Point]>,
    },
}

fn pointify(p: &Rc<Path>, t: Transform) -> Box<[Point]> {
    let mut p: Box<[Point]> = p.points().into();
    t.map_points(&mut p);
    p
}

impl Node {
    fn transform(&mut self, t: Transform) {
        let (Self::Fill { path, .. } | Self::Stroke { path, .. }) = self;
        if t.is_identity() {
            return;
        }
        t.map_points(path);
    }
}

impl From<usvg::Tree> for Tree {
    fn from(tree: usvg::Tree) -> Self {
        let mut children = vec![];
        collect(tree.root, &mut children);
        Self {
            width: tree.view_box.rect.width(),
            height: tree.view_box.rect.height(),
            children: children.into(),
        }
    }
}

trait PColor {
    fn col(&self) -> Color;
}

impl PColor for Paint {
    fn col(&self) -> Color {
        match self {
            Self::Color(c) => *c,
            _ => unimplemented!(),
        }
    }
}
fn convert(node: usvg::Node, to: &mut Vec<Node>) {
    match &*node.clone().borrow() {
        usvg::NodeKind::Path(usvg::Path {
            stroke:
                Some(usvg::Stroke {
                    paint: stroke_paint,
                    opacity: stroke_opacity,
                    width: stroke,
                    ..
                }),
            transform,
            fill: Some(Fill { opacity, paint, .. }),
            data: path,
            ..
        }) => {
            to.push(Node::Stroke {
                color: paint.col(),
                opacity: *opacity,
                stroke: *stroke,
                stroke_opacity: *stroke_opacity,
                stroke_color: stroke_paint.col(),
                path: pointify(path, *transform),
            });
        }
        usvg::NodeKind::Path(usvg::Path {
            stroke:
                Some(usvg::Stroke {
                    paint: stroke_paint,
                    opacity: stroke_opacity,
                    width: stroke,
                    ..
                }),
            transform,
            fill: None,
            data: path,
            ..
        }) => {
            to.push(Node::Stroke {
                color: Color::black(),
                opacity: NormalizedF32::new(0.0).unwrap(),
                stroke: *stroke,
                stroke_opacity: *stroke_opacity,
                stroke_color: stroke_paint.col(),
                path: pointify(path, *transform),
            });
        }
        usvg::NodeKind::Path(usvg::Path {
            transform,
            stroke: None,
            fill: Some(Fill { opacity, paint, .. }),
            data: path,
            ..
        }) => {
            to.push(Node::Fill {
                color: paint.col(),
                opacity: *opacity,
                path: pointify(path, *transform),
            });
        }
        usvg::NodeKind::Path(usvg::Path {
            transform,
            stroke: None,
            fill: None,
            data: path,
            ..
        }) => {
            to.push(Node::Fill {
                color: Color::black(),
                opacity: NormalizedF32::new(0.0).unwrap(),
                path: pointify(path, *transform),
            });
        }
        usvg::NodeKind::Group(_) => {}
        usvg::NodeKind::Image(_) => todo!(),
        usvg::NodeKind::Text(_) => unimplemented!(),
    }
}

fn collect(node: usvg::Node, to: &mut Vec<Node>) {
    for child in node.children() {
        collect(child.clone(), to);
        convert(child, to);
    }
}

impl Tree {
    pub fn new(svg: &str) -> Result<(Self, Size)> {
        let t = usvg::Tree::from_str(svg, &Options::default())?;
        let ts = t.size;
        Ok((Tree::from(t), ts))
    }

    pub fn resize(&mut self, w: f32, h: f32) {
        let t = Transform::from_scale(w / self.width, h / self.height);
        for child in &mut *self.children {
            child.transform(t);
        }
        self.width = w;
        self.height = h;
        log::debug!("changed size to {w}x{h}");
    }
}