-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
87 lines (82 loc) · 2.89 KB
/
Copy pathbuild.rs
File metadata and controls
87 lines (82 loc) · 2.89 KB
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
use std::collections::BTreeMap;
use std::env;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::iter::FromIterator;
use std::path::Path;
use std::str::FromStr;
fn main() {
let input_dir = env::current_dir().unwrap();
let input_path = Path::new(&input_dir).join("data/unifont.hex");
let input_file = File::open(input_path).unwrap();
let input_reader = BufReader::new(&input_file);
let mut glyph_map: BTreeMap<u16, String> = BTreeMap::new();
for line_result in input_reader.lines() {
let line = line_result.unwrap();
let code_point = u16::from_str_radix(&line[0..4], 16).unwrap();
let data = String::from_str(&line[5..]).unwrap();
glyph_map.insert(code_point, data);
}
// enumerate contiguous ranges
let mut ranges: Vec<(i32, i32)> = vec![];
let mut prev_code_point = -2;
for code_point_u16 in glyph_map.keys() {
let code_point = *code_point_u16 as i32;
if prev_code_point + 1 < code_point {
ranges.push((code_point, code_point + 1));
} else {
let i = ranges.len() - 1;
ranges[i].1 = code_point + 1;
}
prev_code_point = code_point;
}
let output_dir = env::var("OUT_DIR").unwrap();
let output_path = Path::new(&output_dir).join("glyph_table.rs");
let mut output_file = File::create(output_path).unwrap();
writeln!(
output_file,
"static CODE_POINT_RANGES: [(usize, usize); {}] = [",
ranges.len()
)
.unwrap();
for (start, end) in ranges {
writeln!(output_file, " ({}, {}),", start, end).unwrap();
}
writeln!(output_file, "];").unwrap();
writeln!(
output_file,
"static GLYPH_TABLE: [Glyph; {}] = [",
glyph_map.len()
)
.unwrap();
for data in glyph_map.values() {
match data.len() {
32 => {
let u8s: Vec<String> = Vec::from_iter(data.chars())
.chunks(2)
.map(|chunk| {
let hex: String = chunk.iter().cloned().collect();
format!("0x{}", hex)
})
.collect();
writeln!(output_file, " Glyph::Halfwidth([{}]),", u8s.join(", ")).unwrap();
}
64 => {
let u16s: Vec<String> = Vec::from_iter(data.chars())
.chunks(4)
.map(|chunk| {
let hex: String = chunk.iter().cloned().collect();
format!("0x{}", hex)
})
.collect();
writeln!(output_file, " Glyph::Fullwidth([{}]),", u16s.join(", ")).unwrap();
}
_ => {
writeln!(output_file, "ERROR: invalid glyph data: {}", data).unwrap();
}
}
}
writeln!(output_file, "];").unwrap();
}