#[cfg(feature = "glyph-list")]
mod list {
use std::collections::HashMap;
use std::sync::OnceLock;
static DATA: &str = include_str!("../data/adobe-glyph-list/glyphlist.txt");
fn legacy_piece(cp: u32) -> Option<u32> {
Some(match cp {
0xF8EB => 0x239B,
0xF8EC => 0x239C,
0xF8ED => 0x239D,
0xF8F6 => 0x239E,
0xF8F7 => 0x239F,
0xF8F8 => 0x23A0,
0xF8EE => 0x23A1,
0xF8EF => 0x23A2,
0xF8F0 => 0x23A3,
0xF8F9 => 0x23A4,
0xF8FA => 0x23A5,
0xF8FB => 0x23A6,
0xF8F1 => 0x23A7,
0xF8F2 => 0x23A8,
0xF8F3 => 0x23A9,
0xF8F4 => 0x23AA,
0xF8FC => 0x23AB,
0xF8FD => 0x23AC,
0xF8FE => 0x23AD,
0xF8F5 => 0x23AE,
0xF8E7 => 0x23AF,
0xF8E6 => 0x23D0,
_ => return None,
})
}
fn value(hex: &str) -> Option<char> {
let cp = u32::from_str_radix(hex, 16).ok()?;
let cp = legacy_piece(cp).unwrap_or(cp);
let cp = match cp {
0x2329 => 0x27E8,
0x232A => 0x27E9,
c => c,
};
let ch = char::from_u32(cp)?;
(!ch.is_control() && !(0xE000..=0xF8FF).contains(&cp)).then_some(ch)
}
pub(super) fn table() -> &'static HashMap<&'static str, char> {
static TABLE: OnceLock<HashMap<&'static str, char>> = OnceLock::new();
TABLE.get_or_init(|| {
DATA.lines()
.filter(|l| !l.starts_with('#'))
.filter_map(|l| {
let (name, hex) = l.split_once(';')?;
Some((name, value(hex)?))
})
.collect()
})
}
}
pub(crate) fn lookup(name: &str) -> Option<char> {
#[cfg(feature = "glyph-list")]
{
list::table().get(name).copied()
}
#[cfg(not(feature = "glyph-list"))]
{
let _ = name;
None
}
}
#[cfg(all(test, feature = "glyph-list"))]
mod tests {
use super::*;
#[test]
fn the_list_is_whole() {
let lines = include_str!("../data/adobe-glyph-list/glyphlist.txt")
.lines()
.filter(|l| !l.starts_with('#'))
.count();
assert_eq!(lines, 4282);
let t = list::table();
assert!(t.len() > 3500 && t.len() <= 4282 - 82, "{} names", t.len());
assert!(t
.values()
.all(|&c| { !c.is_control() && !('\u{E000}'..='\u{F8FF}').contains(&c) }));
}
#[test]
fn names_resolve_as_the_list_defines_them() {
for (name, want) in [
("afii10017", 'А'), ("afii57664", 'א'), ("afii57415", 'ا'), ("Abreveacute", 'Ắ'), ("arrowdblup", '⇑'),
("SF100000", '─'), ("bracelefttp", '⎧'), ("angleleft", '⟨'), ("angleright", '⟩'),
] {
assert_eq!(lookup(name), Some(want), "{name}");
}
assert_eq!(lookup("Asmall"), None, "a private-use variant");
assert_eq!(lookup("dalethatafpatah"), None, "a sequence");
assert_eq!(lookup("controlSTX"), None, "a control");
assert_eq!(lookup("nosuchglyphname"), None);
}
#[test]
fn the_wheel_notice_is_this_notice() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let Ok(copy) =
std::fs::read_to_string(root.join("../docling-py/LICENSE-adobe-glyph-list.txt"))
else {
return;
};
assert_eq!(
copy,
include_str!("../data/adobe-glyph-list/LICENSE.txt"),
"copy data/adobe-glyph-list/LICENSE.txt over crates/docling-py/LICENSE-adobe-glyph-list.txt"
);
}
}