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
//! SVG parsing helpers and free functions.
use crate::types::SvgAnimation;
pub fn parse_svg_animations(data: &[u8]) -> Vec<SvgAnimation> {
let mut parsed_animations = Vec::new();
if let Ok(xml_doc) = roxmltree::Document::parse(std::str::from_utf8(data).unwrap_or("")) {
for node in xml_doc.descendants() {
if node.tag_name().name() == "animateTransform" || node.tag_name().name() == "animate" {
let target_id = node
.attribute("href")
.or_else(|| node.attribute(("http://www.w3.org/1999/xlink", "href")))
.or_else(|| node.attribute("xlink:href"))
.or_else(|| node.parent_element().and_then(|p| p.attribute("id")))
.unwrap_or("")
.trim_start_matches('#')
.to_string();
if !target_id.is_empty() {
let dur_str = node.attribute("dur").unwrap_or("1s");
let duration = if dur_str == "indefinite" {
f32::INFINITY
} else if dur_str.ends_with("ms") {
dur_str
.trim_end_matches("ms")
.parse::<f32>()
.unwrap_or(1000.0)
/ 1000.0
} else {
dur_str.trim_end_matches('s').parse::<f32>().unwrap_or(1.0)
};
let attr = node
.attribute("attributeName")
.unwrap_or("transform")
.to_string();
let (keyframe_values, key_times) =
if let Some(values) = node.attribute("values") {
let parts: Vec<&str> = values.split(';').collect();
let vals: Vec<f32> = parts
.iter()
.map(|p| p.trim().parse::<f32>().unwrap_or(0.0))
.collect();
// Parse keyTimes if present
let kt: Vec<f32> = if let Some(kt_str) = node.attribute("keyTimes") {
kt_str
.split(';')
.map(|p| p.trim().parse::<f32>().unwrap_or(0.0))
.collect()
} else {
Vec::new()
};
(vals, kt)
} else {
let f = node
.attribute("from")
.unwrap_or(if attr == "stroke-dashoffset" {
"1"
} else {
"0"
})
.parse::<f32>()
.unwrap_or(0.0);
let t = node
.attribute("to")
.unwrap_or(if attr == "stroke-dashoffset" {
"0"
} else {
"360"
})
.parse::<f32>()
.unwrap_or(if attr == "stroke-dashoffset" {
0.0
} else {
360.0
});
(vec![f, t], Vec::new())
};
parsed_animations.push(SvgAnimation {
target_id,
attribute_name: attr,
keyframe_values,
key_times,
duration,
vertex_range: 0..0, // Will be filled during tessellation
});
}
}
}
}
parsed_animations
}
// --- SVG Helpers ---
pub(crate) fn usvg_to_lyon(path: &usvg::Path, transform: usvg::Transform) -> lyon::path::Path {
let mut builder = lyon::path::Path::builder();
let mut is_open = false;
// Helper to transform a point
let tx = |p: usvg::tiny_skia_path::Point| -> lyon::math::Point {
let nx = transform.sx * p.x + transform.kx * p.y + transform.tx;
let ny = transform.ky * p.x + transform.sy * p.y + transform.ty;
lyon::math::point(nx, ny)
};
for segment in path.data().segments() {
match segment {
usvg::tiny_skia_path::PathSegment::MoveTo(p) => {
if is_open {
builder.end(false);
}
builder.begin(tx(p));
is_open = true;
}
usvg::tiny_skia_path::PathSegment::LineTo(p) => {
builder.line_to(tx(p));
}
usvg::tiny_skia_path::PathSegment::QuadTo(p1, p) => {
builder.quadratic_bezier_to(tx(p1), tx(p));
}
usvg::tiny_skia_path::PathSegment::CubicTo(p1, p2, p) => {
builder.cubic_bezier_to(tx(p1), tx(p2), tx(p));
}
usvg::tiny_skia_path::PathSegment::Close => {
if is_open {
builder.end(true);
is_open = false;
}
}
}
}
if is_open {
builder.end(false);
}
builder.build()
}