use std::collections::HashMap;
use std::sync::OnceLock;
const AGL_TEXT: &str = include_str!("../data/agl/glyphlist.txt");
const AGLFN_TEXT: &str = include_str!("../data/agl/aglfn.txt");
const ZAPF_DINGBATS: &str = include_str!("../data/agl/zapfdingbats.txt");
struct Dicts {
legacy_agl2uv: HashMap<&'static str, Vec<u32>>,
agl2uv: HashMap<&'static str, u32>,
uv2agl: HashMap<u32, &'static str>,
}
fn dicts() -> &'static Dicts {
static DICTS: OnceLock<Dicts> = OnceLock::new();
DICTS.get_or_init(|| {
let mut d = Dicts {
legacy_agl2uv: HashMap::new(),
agl2uv: HashMap::new(),
uv2agl: HashMap::new(),
};
for line in AGL_TEXT.lines() {
if line.is_empty() || line.starts_with('#') {
continue;
}
let Some((name, codes)) = line.split_once(';') else {
continue;
};
let unicodes = codes
.split(' ')
.filter_map(|c| u32::from_str_radix(c, 16).ok())
.collect();
d.legacy_agl2uv.insert(name, unicodes);
}
for line in AGLFN_TEXT.lines() {
if line.is_empty() || line.starts_with('#') {
continue;
}
let mut fields = line.splitn(3, ';');
let (Some(code), Some(name)) = (fields.next(), fields.next()) else {
continue;
};
let Ok(code) = u32::from_str_radix(code, 16) else {
continue;
};
d.agl2uv.insert(name, code);
d.uv2agl.insert(code, name);
}
d
})
}
pub fn uv2agl(codepoint: u32) -> Option<&'static str> {
dicts().uv2agl.get(&codepoint).copied()
}
pub fn agl2uv(name: &str) -> Option<u32> {
dicts().agl2uv.get(name).copied()
}
pub fn legacy_agl2uv(name: &str) -> Option<&'static [u32]> {
dicts().legacy_agl2uv.get(name).map(Vec::as_slice)
}
pub fn uv2agl_map() -> &'static HashMap<u32, &'static str> {
&dicts().uv2agl
}
pub fn agl2uv_map() -> &'static HashMap<&'static str, u32> {
&dicts().agl2uv
}
pub fn to_unicode(glyph: &str, is_zapf_dingbats: bool) -> String {
let glyph = glyph.split('.').next().unwrap_or_default();
glyph
.split('_')
.map(|c| component_to_unicode(c, is_zapf_dingbats))
.collect()
}
fn component_to_unicode(component: &str, is_zapf_dingbats: bool) -> String {
if is_zapf_dingbats && let Some(c) = zapf_dingbats_to_unicode(component) {
return c.to_string();
}
if let Some(chars) = legacy_agl2uv(component)
&& !chars.is_empty()
{
return chars.iter().filter_map(|&c| char::from_u32(c)).collect();
}
if let Some(s) = uni_to_unicode(component) {
return s;
}
if let Some(c) = u_to_unicode(component) {
return c.to_string();
}
String::new()
}
fn zapf_dingbats_to_unicode(glyph: &str) -> Option<char> {
let digits = glyph.strip_prefix('a').filter(|d| !d.is_empty())?;
let gid: i64 = digits.trim().parse().ok()?;
let gid = usize::try_from(gid).ok()?;
let c = ZAPF_DINGBATS.chars().nth(gid)?;
(c != ' ').then_some(c)
}
fn is_upper_hex(s: &str) -> bool {
!s.is_empty()
&& s.bytes()
.all(|b| b.is_ascii_digit() || (b'A'..=b'F').contains(&b))
}
fn uni_to_unicode(component: &str) -> Option<String> {
let digits = component.strip_prefix("uni")?;
if !is_upper_hex(digits) || digits.len() % 4 != 0 {
return None;
}
let chars: Vec<u32> = digits
.as_bytes()
.chunks(4)
.map(|c| u32::from_str_radix(std::str::from_utf8(c).unwrap_or("0"), 16).unwrap_or(0))
.collect();
if chars.iter().any(|c| (0xD800..=0xDFFF).contains(c)) {
return None;
}
Some(chars.into_iter().filter_map(char::from_u32).collect())
}
fn u_to_unicode(component: &str) -> Option<char> {
let digits = component.strip_prefix('u')?;
if !is_upper_hex(digits) || !(4..=6).contains(&digits.len()) {
return None;
}
let value = u32::from_str_radix(digits, 16).ok()?;
if value <= 0xD7FF || (0xE000..=0x10FFFF).contains(&value) {
char::from_u32(value)
} else {
None
}
}