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
extern crate rctree;
use std::cell::Ref;
use std::path;
use svgdom;
use libflate;
pub use self::nodes::*;
pub use self::attributes::*;
use {
Error,
Options,
};
mod attributes;
mod export;
mod nodes;
mod numbers;
pub mod prelude {
pub use IsDefault;
pub use tree::FuzzyEq;
pub use tree::FuzzyZero;
pub use super::NodeExt;
}
pub type Node = rctree::Node<NodeKind>;
#[allow(missing_debug_implementations)]
#[derive(Clone)]
pub struct Tree {
root: Node,
}
impl Tree {
pub fn from_data(data: &[u8], opt: &Options) -> Result<Self, Error> {
if data.starts_with(&[0x1f, 0x8b]) {
let text = deflate(data, data.len())?;
Self::from_str(&text, opt)
} else {
let text = ::std::str::from_utf8(data).map_err(|_| Error::NotAnUtf8Str)?;
Self::from_str(text, opt)
}
}
pub fn from_str(text: &str, opt: &Options) -> Result<Self, Error> {
let dom_opt = svgdom::ParseOptions {
skip_invalid_attributes: true,
skip_invalid_css: true,
skip_unresolved_classes: true,
};
let doc = svgdom::Document::from_str_with_opt(text, &dom_opt)
.map_err(|e| Error::ParsingFailed(e))?;
Self::from_dom(doc, &opt)
}
pub fn from_dom(mut doc: svgdom::Document, opt: &Options) -> Result<Self, Error> {
super::convert::prepare_doc(&mut doc);
super::convert::convert_doc(&doc, opt)
}
pub fn from_file<P: AsRef<path::Path>>(
path: P,
opt: &Options,
) -> Result<Self, Error> {
let text = load_svg_file(path.as_ref())?;
Self::from_str(&text, opt)
}
pub fn create(svg: Svg) -> Self {
let mut root_node = Node::new(NodeKind::Svg(svg));
let defs_node = Node::new(NodeKind::Defs);
root_node.append(defs_node);
Tree {
root: root_node,
}
}
pub fn root(&self) -> Node {
self.root.clone()
}
pub fn svg_node(&self) -> Ref<Svg> {
Ref::map(self.root.borrow(), |v| {
match *v {
NodeKind::Svg(ref svg) => svg,
_ => unreachable!(),
}
})
}
pub fn defs(&self) -> Node {
self.root.first_child().unwrap()
}
pub fn is_in_defs(&self, node: &Node) -> bool {
let defs = self.defs();
node.ancestors().any(|n| n == defs)
}
pub fn append_to_defs(&mut self, kind: NodeKind) -> Node {
debug_assert!(self.defs_by_id(kind.id()).is_none(),
"Element #{} already exists in 'defs'.", kind.id());
let new_node = Node::new(kind);
self.defs().append(new_node.clone());
new_node
}
pub fn defs_by_id(&self, id: &str) -> Option<Node> {
for n in self.defs().children() {
if &*n.id() == id {
return Some(n);
}
}
None
}
pub fn node_by_id(&self, id: &str) -> Option<Node> {
if id.is_empty() {
return None;
}
for node in self.root().descendants() {
if !self.is_in_defs(&node) {
if &*node.id() == id {
return Some(node);
}
}
}
None
}
pub fn to_svgdom(&self) -> svgdom::Document {
export::convert(self)
}
}
pub trait NodeExt {
fn id(&self) -> Ref<str>;
fn transform(&self) -> Transform;
fn append_kind(&mut self, kind: NodeKind) -> Node;
fn tree(&self) -> Tree;
}
impl NodeExt for Node {
fn id(&self) -> Ref<str> {
Ref::map(self.borrow(), |v| v.id())
}
fn transform(&self) -> Transform {
self.borrow().transform()
}
fn append_kind(&mut self, kind: NodeKind) -> Node {
let new_node = Node::new(kind);
self.append(new_node.clone());
new_node
}
fn tree(&self) -> Tree {
Tree { root: self.root() }
}
}
pub fn load_svg_file(path: &path::Path) -> Result<String, Error> {
use std::fs;
use std::io::Read;
use std::path::Path;
let mut file = fs::File::open(path).map_err(|_| Error::FileOpenFailed)?;
let length = file.metadata().map_err(|_| Error::FileOpenFailed)?.len() as usize + 1;
let ext = if let Some(ext) = Path::new(path).extension() {
ext.to_str().map(|s| s.to_lowercase()).unwrap_or_default()
} else {
String::new()
};
match ext.as_str() {
"svgz" => {
deflate(&file, length)
}
"svg" => {
let mut s = String::with_capacity(length);
file.read_to_string(&mut s).map_err(|_| Error::NotAnUtf8Str)?;
Ok(s)
}
_ => {
Err(Error::InvalidFileSuffix)
}
}
}
fn deflate<R: ::std::io::Read>(inner: R, len: usize) -> Result<String, Error> {
use std::io::Read;
let mut decoder = libflate::gzip::Decoder::new(inner).map_err(|_| Error::MalformedGZip)?;
let mut decoded = String::with_capacity(len * 2);
decoder.read_to_string(&mut decoded).map_err(|_| Error::NotAnUtf8Str)?;
Ok(decoded)
}