-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
171 lines (151 loc) · 4.09 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
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
use std::{
collections::BTreeMap,
env,
fs::File,
io::{BufWriter, Write},
path::Path,
};
use epaint::{
text::{FontData, FontDefinitions, FontTweak},
FontFamily,
};
use run_script::ScriptOptions;
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/unicode_helper.rs"
));
fn font_stripper(from: &str, out: &str, unicodes: Vec<char>) -> Result<Vec<u8>, String> {
let unicodes: Vec<String> = unicodes.iter().map(|c| to_unicode_hash(*c)).collect();
let new_path = [&env::var("OUT_DIR").unwrap(), out].concat();
let unicodes_formatted = unicodes
.iter()
.map(|u| format!("U+{}", u))
.collect::<Vec<String>>()
.join(",");
// Test to see if pyftsubset is found
let pyftsubset_detect = run_script::run("whereis pyftsubset", &(vec![]), &ScriptOptions::new());
match pyftsubset_detect {
Ok((_i, s1, _s2)) => {
if s1 == "pyftsubset: " {
return Err(String::from("pyftsubset not found"));
}
}
// It was not, return an error and abort
Err(x) => return Err(x.to_string()),
}
let script_result = run_script::run(
&format!(
"pyftsubset {}/assets/{} --unicodes={}
mv {}/assets/{} {}",
env!("CARGO_MANIFEST_DIR"),
from,
unicodes_formatted,
env!("CARGO_MANIFEST_DIR"),
from.replace(".ttf", ".subset.ttf"),
new_path
),
&(vec![]),
&ScriptOptions::new(),
);
if let Ok((_, _, error)) = script_result {
if error.is_empty() {
return Ok(std::fs::read(new_path).unwrap());
} else {
return Err(error);
}
} else if let Err(error) = script_result {
return Err(error.to_string());
}
unreachable!()
}
fn main() {
// rebuild if new commit or contents of `assets` folder changed
println!("cargo:rerun-if-changed=.git/logs/HEAD");
println!("cargo:rerun-if-changed=assets/*");
shadow_rs::new().expect("Could not initialize shadow_rs");
let mut main_chars: Vec<char> =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzsu0123456789?.,!(){}[]-_=+-/<>'\\ :^*`@#$%&|~;"
.iter()
.map(|c| *c as char)
.collect();
main_chars.append(&mut vec!['π', '"']);
{
let filtered_chars: Vec<char> = main_chars
.iter()
.filter(|c| !c.is_alphanumeric())
.cloned()
.collect();
let chars_array = format!(
"const VALID_EXTRA_CHARS: [char; {}] = {};",
filtered_chars.len(),
to_chars_array(filtered_chars),
);
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("valid_chars.rs");
let mut file = BufWriter::new(File::create(path).expect("Could not save compressed_data"));
write!(&mut file, "{}", chars_array).expect("unable to write chars_array");
}
let fonts = FontDefinitions {
font_data: BTreeMap::from([
(
"Ubuntu-Light".to_owned(),
FontData::from_owned(
font_stripper(
"Ubuntu-Light.ttf",
"ubuntu-light.ttf",
[main_chars, vec!['∫']].concat(),
)
.unwrap(),
),
),
(
"NotoEmoji-Regular".to_owned(),
FontData::from_owned(
font_stripper(
"NotoEmoji-Regular.ttf",
"noto-emoji.ttf",
vec!['🌞', '🌙', '✖'],
)
.unwrap(),
),
),
(
"emoji-icon-font".to_owned(),
FontData::from_owned(
font_stripper("emoji-icon-font.ttf", "emoji-icon.ttf", vec!['⚙']).unwrap(),
)
.tweak(FontTweak {
scale: 0.8,
y_offset_factor: 0.07,
y_offset: 0.0,
baseline_offset_factor: -0.0333,
}),
),
]),
families: BTreeMap::from([
(
FontFamily::Monospace,
vec![
"Ubuntu-Light".to_owned(),
"NotoEmoji-Regular".to_owned(),
"emoji-icon-font".to_owned(),
],
),
(
FontFamily::Proportional,
vec![
"Ubuntu-Light".to_owned(),
"NotoEmoji-Regular".to_owned(),
"emoji-icon-font".to_owned(),
],
),
]),
};
let data = bincode::serialize(&fonts).unwrap();
let zstd_levels = zstd::compression_level_range();
let data_compressed =
zstd::encode_all(data.as_slice(), *zstd_levels.end()).expect("Could not compress data");
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("compressed_data");
let mut file = BufWriter::new(File::create(path).expect("Could not save compressed_data"));
file.write_all(data_compressed.as_slice())
.expect("Failed to save compressed data");
}