use cosmic_text::{FontSystem, fontdb};
use std::path::PathBuf;
#[cfg(target_os = "macos")]
const FONT_FILES: &[&str] = &[
"/System/Library/Fonts/Menlo.ttc",
"/System/Library/Fonts/HelveticaNeue.ttc",
"/System/Library/Fonts/Apple Color Emoji.ttc",
"/System/Library/Fonts/Apple Symbols.ttf",
"/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
];
#[cfg(target_os = "windows")]
const FONT_FILES: &[&str] = &[
"consola.ttf",
"consolab.ttf",
"consolai.ttf",
"consolaz.ttf",
"segoeui.ttf",
"segoeuib.ttf",
"segoeuii.ttf",
"segoeuiz.ttf",
"seguiemj.ttf",
"seguisym.ttf",
];
#[cfg(target_os = "ios")]
const FONT_FILES: &[&str] = &[
"/System/Library/Fonts/Core/HelveticaNeue.ttc",
"/System/Library/Fonts/CoreUI/Menlo.ttc",
"/System/Library/Fonts/CoreAddition/AppleColorEmoji-160px.ttc",
"/System/Library/Fonts/Core/AppleSymbols.ttf",
];
#[cfg(target_os = "android")]
const FONT_FILES: &[&str] =
&["/system/fonts/Roboto-Regular.ttf", "/system/fonts/DroidSansMono.ttf", "/system/fonts/NotoColorEmoji.ttf"];
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios", target_os = "android")))]
const FONT_FILES: &[&str] = &[];
#[cfg(any(target_os = "macos", target_os = "ios"))]
const FAMILIES: (&str, &str) = ("Helvetica Neue", "Menlo");
#[cfg(target_os = "windows")]
const FAMILIES: (&str, &str) = ("Segoe UI", "Consolas");
#[cfg(target_os = "android")]
const FAMILIES: (&str, &str) = ("Roboto", "Droid Sans Mono");
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios", target_os = "android")))]
const FAMILIES: (&str, &str) = ("DejaVu Sans", "DejaVu Sans Mono");
pub(crate) fn system_path(path: &str) -> PathBuf {
#[cfg(windows)]
{
let windir = std::env::var_os("WINDIR").map_or_else(|| PathBuf::from("C:\\Windows"), PathBuf::from);
return windir.join("Fonts").join(path);
}
#[cfg(not(windows))]
match std::env::var_os("IPHONE_SIMULATOR_ROOT") {
Some(root) if cfg!(target_os = "ios") => std::path::Path::new(&root).join(path.trim_start_matches('/')),
_ => PathBuf::from(path),
}
}
pub fn build_font_system() -> FontSystem {
let mut database = fontdb::Database::new();
for path in FONT_FILES {
if let Err(error) = database.load_font_file(system_path(path)) {
eprintln!("no se pudo cargar la fuente {path}: {error}");
}
}
if database.is_empty() {
database.load_system_fonts();
}
database.set_sans_serif_family(FAMILIES.0);
database.set_monospace_family(FAMILIES.1);
FontSystem::new_with_locale_and_db("en-US".to_owned(), database)
}