mod face_info;
mod cmap;
mod error;
use std::path::Path;
use fontdb::Database;
use once_cell::sync::OnceCell;
pub use self::{error::Error, face_info::FaceInfo};
pub type Result<T> = std::result::Result<T, Error>;
static DATABASE: OnceCell<Database> = OnceCell::new();
pub fn init<I, P>(system: bool, paths: I)
where
I: IntoIterator<Item = P>,
P: AsRef<Path>,
{
let mut db = Database::default();
if system {
db.load_system_fonts();
}
for path in paths.into_iter() {
db.load_fonts_dir(path)
}
if DATABASE.set(db).is_err() {
panic!("call init more then once")
}
}
pub fn database() -> &'static Database {
DATABASE.get().expect("initialized")
}
pub fn query(c: char) -> Vec<FaceInfo> {
database()
.faces()
.filter_map(|info| {
let face = FaceInfo::parse_if_contains(info, c);
if let Err(ref err) = face {
log::warn!("Fail to get font face name of {:?}: {}", info.source, err)
}
face.transpose()
})
.filter_map(|f| f.ok())
.collect()
}