-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
182 lines (172 loc) · 5.91 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
172
173
174
175
176
177
178
179
180
181
182
use std::env;
use std::io::Result;
use std::path::PathBuf;
fn main() -> Result<()> {
println!("cargo:rerun-if-changed=build.rs");
build_resource()?;
build_helvetica()?;
build_locales()?;
Ok(())
}
fn build_resource() -> Result<()> {
let target_os = std::env::var("CARGO_CFG_TARGET_OS");
if target_os.as_deref() == Ok("windows") {
let name = env!("CARGO_PKG_NAME");
let version = env!("CARGO_PKG_VERSION");
let repo = env!("CARGO_PKG_REPOSITORY");
let output_dir = std::env::var("OUT_DIR").unwrap_or_else(|_| ".".to_string());
let header = std::path::PathBuf::from(&output_dir).join("papercraft.h");
std::fs::write(
header,
format!(
r#"
#define PC_PROJECT "{name}"
#define PC_VERSION "{version}"
#define PC_REPO "{repo}"
"#
),
)?;
let output = std::path::PathBuf::from(&output_dir).join("resource.o");
#[allow(clippy::option_env_unwrap)]
let status = if let Some(windres) = option_env!("WINDRES") {
std::process::Command::new(windres)
.arg("-I")
.arg(&output_dir)
.arg("res/resource.rc")
.arg(&output)
.status()?
} else if let Some(rc) = option_env!("RC") {
std::process::Command::new(rc)
.arg("/i")
.arg(&output_dir)
.arg("/fo")
.arg(&output)
.arg("res/resource.rc")
.status()?
} else {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"RC or WINDRES should be defined",
));
};
if !status.success() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"windres error",
));
}
println!("cargo:rustc-link-arg={}", output.display());
for entry in std::fs::read_dir("res")? {
let entry = entry?;
println!("cargo:rerun-if-changed={}", entry.path().display());
}
}
Ok(())
}
// Metrics for well-known PDF fonts are in AFM files
fn build_helvetica() -> Result<()> {
use std::{
collections::BTreeMap,
fs::File,
io::{BufRead, BufReader, BufWriter, Write},
};
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out = File::create(out_path.join("helvetica_afm.rs"))?;
let mut out = BufWriter::new(out);
let mut widths = BTreeMap::<u16, u32>::new(); // Unicode to width
let mut names = BTreeMap::<String, u16>::new(); // name to Unicode
let mut kerns = BTreeMap::<u16, Vec<(u16, i32)>>::new(); // Second-Unicode to list of (First-Unicode, kerning)
println!("cargo:rerun-if-changed=thirdparty/afm/names.txt");
let char_names = File::open("thirdparty/afm/names.txt").unwrap();
let char_names = BufReader::new(char_names);
for line in char_names.lines() {
let line = line.unwrap();
let pieces: Vec<&str> = line.split('\t').collect();
let name = pieces[0];
let code = u16::from_str_radix(pieces[1], 16).unwrap();
names.insert(name.to_owned(), code);
}
let afm_file = "thirdparty/afm/Helvetica.afm";
println!("cargo:rerun-if-changed={afm_file}");
let afm = File::open(afm_file).unwrap();
let afm = BufReader::new(afm);
for line in afm.lines() {
let line = line.unwrap();
let pieces: Vec<&str> = line.split(';').collect();
let words0: Vec<&str> = pieces[0].split_ascii_whitespace().collect();
if words0.is_empty() {
continue;
}
match words0[0] {
"C" => {
let mut width: Option<u32> = None;
let mut name: Option<&str> = None;
for piece in &pieces[1..] {
let words: Vec<&str> = piece.split_ascii_whitespace().collect();
if words.is_empty() {
continue;
}
match words[0] {
"WX" => {
width = Some(words[1].parse().unwrap());
}
"N" => {
name = Some(words[1]);
}
_ => {}
}
}
if let (Some(width), Some(name)) = (width, name) {
let Some(&char) = names.get(name) else {
continue;
};
widths.insert(char, width);
}
}
"KPX" => {
let Some(&c1) = names.get(words0[1]) else {
continue;
};
let Some(&c2) = names.get(words0[2]) else {
continue;
};
let kern: i32 = words0[3].parse().unwrap();
kerns.entry(c2).or_default().push((c1, kern));
}
_ => {}
}
}
// Each char maps to (width, [(previous char, kerning)]).
writeln!(
out,
r#"
pub struct CharInfo {{
pub width: u32,
pub kerns: &'static [(char, i32)],
}}
"#
)?;
writeln!(
out,
"pub static CHARS: [(char, CharInfo); {}] = [",
widths.len(),
)?;
for (c, w) in widths {
write!(out, "('\\u{{{c:x}}}', CharInfo {{ width: {w}, kerns: &[")?;
if let Some(mut ks) = kerns.remove(&c) {
ks.sort_by_key(|(c, _)| *c);
for (c2, k) in ks {
write!(out, "('\\u{{{c2:x}}}', {k}), ")?;
}
}
writeln!(out, "]}}),")?;
}
writeln!(out, "];")?;
Ok(())
}
fn build_locales() -> Result<()> {
let output_dir = std::env::var("OUT_DIR").unwrap();
let out = PathBuf::from(&output_dir).join("locale/translators.rs");
include_po::generate_locales_from_dir("locales", out).unwrap();
Ok(())
}