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
use std::path;
use svgdom;
use libflate;
use {
Error,
};
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)
}
}
}
pub 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)
}
pub fn parse_dom(text: &str) -> svgdom::Document {
let opt = svgdom::ParseOptions {
parse_comments: false,
parse_declarations: false,
parse_unknown_elements: false,
parse_unknown_attributes: false,
parse_px_unit: false,
skip_invalid_attributes: true,
skip_invalid_css: true,
skip_paint_fallback: true,
skip_elements_crosslink: true,
.. svgdom::ParseOptions::default()
};
svgdom::Document::from_str_with_opt(text, &opt).unwrap_or_else(|e| {
warn!("Failed to parse an SVG data cause {}.", e);
svgdom::Document::new()
})
}