use std::{borrow::Cow, path::PathBuf, sync::Arc};
use fontique::{Collection, CollectionOptions, SourceCache};
use parlance::FontFamilyName;
use peniko::Blob;
use crate::FontContext;
pub(crate) const FONT_FAMILY_LIST: &[FontFamilyName<'_>] = &[
FontFamilyName::Named(Cow::Borrowed("Roboto")),
FontFamilyName::Named(Cow::Borrowed("Noto Kufi Arabic")),
];
pub(crate) fn load_fonts(
collection: &mut Collection,
font_dirs: impl Iterator<Item = PathBuf>,
) -> std::io::Result<()> {
for dir in font_dirs {
let paths = std::fs::read_dir(dir)?;
for entry in paths {
let entry = entry?;
if !entry.metadata()?.is_file() {
continue;
}
let path = entry.path();
if path
.extension()
.and_then(|ext| ext.to_str())
.is_none_or(|ext| !["ttf", "otf", "ttc", "otc"].contains(&ext))
{
continue;
}
let font_data = std::fs::read(&path)?;
collection.register_fonts(Blob::new(Arc::new(font_data)), None);
}
}
Ok(())
}
pub(crate) fn create_font_context() -> FontContext {
let mut collection = Collection::new(CollectionOptions {
shared: false,
system_fonts: false,
});
load_fonts(&mut collection, parley_dev::font_dirs()).unwrap();
for font in FONT_FAMILY_LIST {
if let FontFamilyName::Named(font_name) = font {
collection
.family_id(font_name)
.unwrap_or_else(|| panic!("{font_name} font not found"));
}
}
FontContext {
collection,
source_cache: SourceCache::default(),
}
}