-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.rs
61 lines (48 loc) · 1.63 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
use std::{fs::File, path::Path};
use ico::{IconDir, IconDirEntry};
use resvg::{
tiny_skia::{Pixmap, Transform},
usvg::{Options, Tree, TreeParsing},
};
#[cfg(windows)]
use winres::WindowsResource;
fn write_icon(s: u32, tree: &Tree, icon_dir: &mut IconDir) {
let mut pixmap = Pixmap::new(s, s).unwrap();
resvg::render(
tree,
resvg::FitTo::Size(s, s),
Transform::default(),
pixmap.as_mut(),
)
.unwrap();
if s == 16 {
std::fs::write(
Path::new(std::env::var_os("OUT_DIR").as_ref().unwrap()).join("icon.bitmap"),
pixmap.data(),
)
.unwrap();
}
let image = ico::IconImage::from_rgba_data(s, s, pixmap.take());
icon_dir.add_entry(IconDirEntry::encode(&image).unwrap());
}
fn main() {
let svg = std::fs::read_to_string("assets/logo.svg").unwrap();
let tree = Tree::from_str(&svg, &Options::default()).unwrap();
let mut icon_dir = ico::IconDir::new(ico::ResourceType::Icon);
{
let small_svg = std::fs::read_to_string("assets/logo_16.svg").unwrap();
let small_tree = Tree::from_str(&small_svg, &Options::default()).unwrap();
write_icon(16, &small_tree, &mut icon_dir)
}
for s in [24, 32, 48, 96, 128, 256] {
write_icon(s, &tree, &mut icon_dir);
}
let icon_path = Path::new(std::env::var_os("OUT_DIR").as_ref().unwrap()).join("icon.ico");
#[cfg(windows)]
{
WindowsResource::new().set_icon(icon_path.to_str().unwrap());
}
icon_dir.write(File::create(icon_path).unwrap()).unwrap();
#[cfg(not(windows))]
println!("cargo:rerun-if-changed=assets/logo.svg")
}