use std::sync::{Arc, Mutex, OnceLock};
use cosmic_text::{fontdb, Fallback, FontSystem, PlatformFallback};
use super::{family::FontFamily, weight::FontWeight};
use crate::Id;
struct EmojiFallback(Vec<&'static str>);
impl Fallback for EmojiFallback {
fn common_fallback(&self) -> &[&'static str] {
&self.0
}
fn forbidden_fallback(&self) -> &[&'static str] {
PlatformFallback.forbidden_fallback()
}
fn script_fallback(&self, script: unicode_script::Script, locale: &str) -> &[&'static str] {
PlatformFallback.script_fallback(script, locale)
}
}
#[cfg(feature = "bundled-emoji")]
const ALIAS_WEIGHTS: [FontWeight; 9] = [
FontWeight::THIN,
FontWeight::EXTRA_LIGHT,
FontWeight::LIGHT,
FontWeight::REGULAR,
FontWeight::MEDIUM,
FontWeight::SEMIBOLD,
FontWeight::BOLD,
FontWeight::EXTRA_BOLD,
FontWeight::BLACK,
];
pub fn font_system() -> &'static Mutex<FontSystem> {
static FONTS: OnceLock<Mutex<FontSystem>> = OnceLock::new();
FONTS.get_or_init(|| {
let system = FontSystem::new();
#[cfg_attr(not(feature = "bundled-emoji"), allow(unused_mut))]
let (locale, mut db) = system.into_locale_and_db();
#[cfg(feature = "bundled-emoji")]
add_faces(
&mut db,
"Noto Color Emoji",
Arc::new(z_emoji::FONT_DATA),
&ALIAS_WEIGHTS,
);
let common = std::iter::once("Noto Color Emoji")
.chain(PlatformFallback.common_fallback().iter().copied())
.collect();
Mutex::new(FontSystem::new_with_locale_and_db_and_fallback(
locale,
db,
EmojiFallback(common),
))
})
}
fn add_faces(
db: &mut fontdb::Database,
name: &str,
data: Arc<dyn AsRef<[u8]> + Send + Sync>,
weights: &[FontWeight],
) {
let mut parsed = fontdb::Database::new();
parsed.load_font_source(fontdb::Source::Binary(data));
for face in parsed.faces() {
for weight in weights {
let mut face = face.clone();
face.families = vec![(name.to_owned(), face.families[0].1)];
face.weight = weight.to_fontdb();
db.push_face_info(face);
}
}
}
#[derive(Clone, Debug)]
pub(super) struct Registered {
pub name: String,
pub id: Id,
pub family: FontFamily,
}
pub(super) fn register(family: FontFamily) -> Registered {
let id = family.identity();
let name = format!("zaxis-{id:?}");
let mut fonts = font_system().lock().unwrap();
let known = fonts
.db()
.faces()
.any(|face| face.families.iter().any(|(n, _)| n == &name));
if !known {
let db = fonts.db_mut();
for (weight, data) in family.faces() {
add_faces(db, &name, Arc::clone(data), &[weight]);
}
}
Registered { name, id, family }
}