Skip to content

Commit 44cafed

Browse files
committed
add glyphlist
1 parent 1395545 commit 44cafed

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "agl-aglfn"]
2+
path = agl-aglfn
3+
url = https://github.com/adobe-type-tools/agl-aglfn

agl-aglfn

Submodule agl-aglfn added at 5de337b

build.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::fs::{File};
2+
use std::io::{Write, BufReader, BufRead};
3+
use std::env;
4+
use std::path::{Path, PathBuf};
5+
use std::iter::once;
6+
7+
fn main() {
8+
let path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("agl-aglfn/glyphlist.txt");
9+
println!("cargo:rerun-if-changed=build.rs");
10+
println!("cargo:rerun-if-changed={}", path.to_str().expect("no-utf8 path"));
11+
12+
let mut glyph_list = File::create(PathBuf::from(env::var("OUT_DIR").unwrap()).join("glyphlist.rs")).unwrap();
13+
14+
writeln!(glyph_list, "[");
15+
for line in BufReader::new(File::open(path).unwrap()).lines() {
16+
let line = line.unwrap();
17+
if line.starts_with("#") {
18+
continue;
19+
}
20+
let mut parts = line.split(";");
21+
let name = parts.next().unwrap();
22+
let unicode: String = parts.next().unwrap()
23+
.split(" ")
24+
.map(|s| u32::from_str_radix(s, 16).unwrap())
25+
.map(|cp| std::char::from_u32(cp).unwrap())
26+
.collect();
27+
28+
writeln!(glyph_list, " ({:?}, {:?}),", name, unicode);
29+
}
30+
writeln!(glyph_list, "]");
31+
}

src/lib.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[macro_use] extern crate lazy_static;
22
use std::num::NonZeroU32;
33
use std::convert::TryInto;
4+
use std::collections::HashMap;
45

56
#[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
67
pub enum Encoding {
@@ -135,3 +136,29 @@ fn test_forward() {
135136
fn test_reverse() {
136137
assert_eq!(UNICODE_TO_STANDARD.get(0x2014), Some(0xD0));
137138
}
139+
140+
pub static GLYPH_LIST: &[(&'static str, &'static str)] = &include!(concat!(env!("OUT_DIR"), "/glyphlist.rs"));
141+
142+
lazy_static! {
143+
// glyph name -> unicode string
144+
static ref UNICODE_MAP: HashMap<&'static str, &'static str> = {
145+
GLYPH_LIST.iter().cloned().collect()
146+
};
147+
}
148+
149+
pub fn glyphname_to_unicode(name: &str) -> Option<&'static str> {
150+
UNICODE_MAP.get(&name).cloned()
151+
}
152+
153+
#[test]
154+
fn test_glyphname() {
155+
let cases = [
156+
("a", "a"),
157+
("Alpha", "Α"),
158+
("gamma", "γ"),
159+
("qofhatafpatah", "קֲ")
160+
];
161+
for &(glyph, unicode) in cases.iter() {
162+
assert_eq!(glyphname_to_unicode(glyph), Some(unicode));
163+
}
164+
}

0 commit comments

Comments
 (0)