use std::cell::RefCell;
use std::collections::BTreeMap;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct LuaFont {
pub filename: String,
pub subfont: i64,
pub psname: Option<String>,
pub glyphs: BTreeMap<u32, (u32, Option<String>)>,
}
thread_local! {
static FONTS: RefCell<BTreeMap<u16, LuaFont>> = const { RefCell::new(BTreeMap::new()) };
}
pub fn register_lua_font(
id: u16,
filename: &str,
subfont: i64,
psname: Option<&str>,
glyphs: &[(u32, u32, Option<String>)],
) {
let font = LuaFont {
filename: filename.to_string(),
subfont,
psname: psname.map(str::to_string),
glyphs: glyphs
.iter()
.map(|(c, g, t)| (*c, (*g, t.clone())))
.collect(),
};
FONTS.with(|m| m.borrow_mut().insert(id, font));
}
pub fn lua_font(id: u16) -> Option<LuaFont> {
FONTS.with(|m| m.borrow().get(&id).cloned())
}
pub fn is_lua_font(id: u16) -> bool {
FONTS.with(|m| m.borrow().contains_key(&id))
}
pub fn clear_lua_fonts() {
FONTS.with(|m| m.borrow_mut().clear());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_registered_font_is_found_by_id_and_cleared() {
register_lua_font(
4711,
"/x/y.otf",
1,
Some("Y-Regular"),
&[(65, 36, Some("0041".into())), (66, 37, None)],
);
assert!(is_lua_font(4711));
let f = lua_font(4711).unwrap();
assert_eq!(f.glyphs[&65], (36, Some("0041".into())));
assert_eq!(
(f.filename.as_str(), f.subfont, f.psname.as_deref()),
("/x/y.otf", 1, Some("Y-Regular"))
);
assert!(!is_lua_font(4712));
}
}