-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
69 lines (54 loc) · 2.1 KB
/
build.rs
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
use std::{env, error, fs, io::Read, path::Path};
use tiny_skia::{Pixmap, Transform};
use usvg::{Options, Tree, TreeParsing};
fn main() {
generate_image().unwrap();
}
// generate raw bytes of svg as const var.
// this would remove the runtime cost of parsing them.
fn generate_image() -> Result<(), Box<dyn error::Error + Send + Sync>> {
let out_dir = env::var_os("OUT_DIR").unwrap();
let path = Path::new(&out_dir).join("const_image.rs");
let mut buf = Vec::new();
let map = render_svg("./resource/broken-image.svg")?;
let string = format!(
"pub const BROKEN_IMAGE: &[u8] = &{:?};pub const BROKEN_IMAGE_SIZE: [usize; 2] = [{}, {}];",
map.data(),
map.width() as usize,
map.height() as usize
);
buf.extend_from_slice(string.as_bytes());
let map = render_svg("./resource/shin-hentai.svg")?;
let string = format!(
"#[allow(dead_code)]pub const ICON_IMAGE: &[u8] = &{:?};#[allow(dead_code)]pub const ICON_IMAGE_SIZE: [u32; 2] = [{}, {}];",
map.data(),
map.width(),
map.height()
);
buf.extend_from_slice(string.as_bytes());
let map = render_svg("./resource/drag-drop.svg")?;
let string = format!(
"pub const DRAG_DROP_IMAGE: &[u8] = &{:?};pub const DRAG_DROP_IMAGE_SIZE: [usize; 2] = [{}, {}];",
map.data(),
map.width() as usize,
map.height() as usize
);
buf.extend_from_slice(string.as_bytes());
fs::write(path, &buf)?;
Ok(())
}
fn render_svg(path: impl AsRef<Path>) -> Result<Pixmap, Box<dyn error::Error + Send + Sync>> {
let mut buf = Vec::new();
let path = path.as_ref();
let mut file = fs::File::open(path)?;
file.read_to_end(&mut buf)?;
let tree = Tree::from_data(&buf, &Options::default())?;
let pixmap_size = tree.size.to_int_size();
let [w, h] = [pixmap_size.width(), pixmap_size.height()];
Pixmap::new(w, h)
.map(|mut map| {
resvg::Tree::from_usvg(&tree).render(Transform::default(), &mut map.as_mut());
map
})
.ok_or_else(|| format!("can not render svg from path: {path:?}",).into())
}