use std::path::Path;
use std::sync::Arc;
use text_typeset::SharedFontData;
#[cfg(target_os = "linux")]
const CANDIDATES: &[&str] = &[
"/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf",
"/usr/share/fonts/noto/NotoColorEmoji.ttf",
"/usr/share/fonts/NotoColorEmoji.ttf",
"/usr/local/share/fonts/NotoColorEmoji.ttf",
"/usr/share/fonts/truetype/NotoColorEmoji.ttf",
"/usr/share/fonts/google-noto-emoji/NotoColorEmoji.ttf",
"/usr/share/fonts/google-noto-color-emoji-fonts/NotoColorEmoji.ttf",
"/usr/share/fonts/google-noto-color-emoji-fonts/Noto-COLRv1.ttf",
];
#[cfg(target_os = "macos")]
const CANDIDATES: &[&str] = &["/System/Library/Fonts/Apple Color Emoji.ttc"];
#[cfg(target_os = "windows")]
const CANDIDATES: &[&str] = &["C:/Windows/Fonts/seguiemj.ttf"];
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
const CANDIDATES: &[&str] = &[];
pub(crate) fn load_system_emoji_data() -> Option<SharedFontData> {
let path = first_existing(CANDIDATES, |path| Path::new(path).is_file())?;
mmap_font(path)
}
fn first_existing<'a>(candidates: &[&'a str], exists: impl Fn(&str) -> bool) -> Option<&'a str> {
candidates.iter().copied().find(|path| exists(path))
}
fn mmap_font(path: &str) -> Option<SharedFontData> {
let file = std::fs::File::open(path).ok()?;
let mmap = unsafe { memmap2::Mmap::map(&file).ok()? };
Some(Arc::new(mmap))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_first_candidate_that_exists_wins() {
let list = ["/a/one.ttf", "/b/two.ttf", "/c/three.ttf"];
assert_eq!(
first_existing(&list, |p| p != "/a/one.ttf"),
Some("/b/two.ttf")
);
assert_eq!(first_existing(&list, |_| true), Some("/a/one.ttf"));
assert_eq!(first_existing(&list, |_| false), None);
}
#[cfg(target_os = "linux")]
#[test]
fn fedora_44_is_found_where_only_its_font_exists() {
const FEDORA: &str = "/usr/share/fonts/google-noto-color-emoji-fonts/Noto-COLRv1.ttf";
assert_eq!(first_existing(CANDIDATES, |p| p == FEDORA), Some(FEDORA));
}
#[cfg(target_os = "linux")]
#[test]
fn a_bitmap_build_is_preferred_to_the_colrv1_one() {
let colr = "Noto-COLRv1.ttf";
let found = first_existing(CANDIDATES, |_| true).unwrap();
assert!(!found.ends_with(colr));
let last = CANDIDATES.last().unwrap();
assert!(last.ends_with(colr));
}
}