use valo::{Font, FontAttrs};
pub use valo::{FontCollection, FontDemand, FontId, FontSource};
use valo_system_fonts::SystemFonts;
pub use valo_system_fonts::fontmgr::{FontManager, Typeface};
pub struct SystemFontSource {
fonts: SystemFonts,
}
const CUPERTINO_TEXT: &str = "CupertinoSystemText";
const CUPERTINO_DISPLAY: &str = "CupertinoSystemDisplay";
const TEXT_SIZE: f32 = 17.0;
const DISPLAY_SIZE: f32 = 29.0;
impl SystemFontSource {
pub fn new(manager: Box<dyn FontManager>) -> SystemFontSource {
SystemFontSource {
fonts: SystemFonts::with_manager(manager),
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn platform() -> SystemFontSource {
SystemFontSource {
fonts: SystemFonts::load(),
}
}
pub fn set_locales(&mut self, locales: Vec<String>) {
self.fonts.set_locales(locales);
}
}
impl FontSource for SystemFontSource {
fn family(&mut self, name: &str) -> Vec<Font> {
match name {
CUPERTINO_TEXT => self.fonts.system_family(TEXT_SIZE),
CUPERTINO_DISPLAY => self.fonts.system_family(DISPLAY_SIZE),
_ => self.fonts.family(name),
}
}
fn face_for_codepoint(&mut self, codepoint: char, attrs: FontAttrs) -> Option<Font> {
self.fonts.face_for_codepoint(codepoint, attrs)
}
}
pub fn default_font_families() -> Vec<String> {
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
vec![CUPERTINO_TEXT.to_owned()]
}
#[cfg(target_os = "android")]
{
vec!["sans-serif".to_owned()]
}
#[cfg(target_os = "windows")]
{
vec!["Segoe UI".to_owned(), "Arial".to_owned()]
}
#[cfg(target_os = "linux")]
{
vec![
"Ubuntu".to_owned(),
"Adwaita Sans".to_owned(),
"Cantarell".to_owned(),
"DejaVu Sans".to_owned(),
"Liberation Sans".to_owned(),
"Arial".to_owned(),
]
}
#[cfg(target_arch = "wasm32")]
{
vec!["Roboto".to_owned()]
}
#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "windows",
target_os = "linux",
target_arch = "wasm32",
)))]
{
vec!["Arial".to_owned()]
}
}
pub fn set_default_font_manager(fonts: &mut FontCollection, mut source: Box<dyn FontSource>) {
for name in default_font_families() {
for mut font in source.family(&name) {
font.add_alias(&name);
let id = fonts.add(font);
fonts.add_fallback(id);
}
}
fonts.add_boxed_source(source);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_font_families_match_the_engine_on_this_os() {
let names = default_font_families();
assert!(!names.is_empty());
#[cfg(any(target_os = "macos", target_os = "ios"))]
assert_eq!(names, vec![CUPERTINO_TEXT]);
#[cfg(target_os = "android")]
assert_eq!(names, vec!["sans-serif"]);
}
}