use core::fmt;
use std::io;
pub use crcbl_font;
use crcbl_font::{FontMatch, FontQuery, SystemFonts};
use super::{Font, FontError, ReservedFamilyName, is_reserved_family};
use crate::tree::Ui;
#[derive(Debug)]
pub enum SystemFontError {
NotFound(String),
Read(io::Error),
Parse(FontError),
Reserved(ReservedFamilyName),
}
impl fmt::Display for SystemFontError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotFound(family) => write!(f, "the system has no font of the family `{family}`"),
Self::Read(error) => write!(f, "the system font cannot be read: {error}"),
Self::Parse(error) => write!(f, "the system font does not parse: {error}"),
Self::Reserved(error) => error.fmt(f),
}
}
}
impl std::error::Error for SystemFontError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::NotFound(_) => None,
Self::Read(error) => Some(error),
Self::Parse(error) => Some(error),
Self::Reserved(error) => Some(error),
}
}
}
impl Font {
pub fn from_system(face: &FontMatch) -> Result<Self, SystemFontError> {
let bytes = face.read().map_err(SystemFontError::Read)?;
Self::parse(bytes).map_err(SystemFontError::Parse)
}
}
impl Ui {
pub fn register_system_font(
&mut self,
name: &str,
fonts: &SystemFonts,
query: &FontQuery,
) -> Result<Font, SystemFontError> {
if is_reserved_family(name) {
return Err(SystemFontError::Reserved(ReservedFamilyName(
name.to_owned(),
)));
}
let face = fonts
.find(query)
.ok_or_else(|| SystemFontError::NotFound(query.family.clone()))?;
let font = Font::from_system(&face)?;
self.register_font(name, &font)
.map_err(SystemFontError::Reserved)?;
Ok(font)
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
fn committed(index: u32) -> FontMatch {
FontMatch {
path: PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fonts")
.join("AtkinsonHyperlegible-Regular.ttf"),
index,
family: "Atkinson Hyperlegible".to_owned(),
weight: 400,
style: crcbl_font::FontStyle::Normal,
}
}
#[test]
fn a_found_face_parses_as_the_font_its_file_holds() {
let font = Font::from_system(&committed(0)).expect("the committed font parses");
let compiled = Font::parse(crate::font::SANS_TTF).expect("parses");
assert_eq!(font.metrics(), compiled.metrics());
assert_eq!(font.weight(), 400.0);
assert!(matches!(
Font::from_system(&committed(1)),
Err(SystemFontError::Read(_))
));
let gone = FontMatch {
path: PathBuf::from("no such font file.ttf"),
..committed(0)
};
assert!(matches!(
Font::from_system(&gone),
Err(SystemFontError::Read(_))
));
}
#[test]
fn a_reserved_name_is_refused_before_the_lookup_and_a_miss_is_not_found() {
let fonts = SystemFonts::new();
let mut ui = Ui::new();
let query = FontQuery::new("sans-serif");
assert!(matches!(
ui.register_system_font("serif", &fonts, &query),
Err(SystemFontError::Reserved(ReservedFamilyName(name))) if name == "serif"
));
let missing = FontQuery::new("No Such Family Zq7");
assert!(matches!(
ui.register_system_font("body", &fonts, &missing),
Err(SystemFontError::NotFound(family)) if family == "No Such Family Zq7"
));
}
#[cfg(any(windows, target_os = "linux", target_os = "macos"))]
#[test]
fn the_systems_sans_serif_registers_under_a_name_of_its_own() {
let fonts = SystemFonts::new();
let query = FontQuery::new("sans-serif");
let found = fonts.find(&query).expect("the system has a sans-serif");
let mut ui = Ui::new();
let font = ui
.register_system_font("body", &fonts, &query)
.expect("the system's sans-serif registers");
assert_eq!(font.weight(), f32::from(found.weight));
assert_eq!(
font.is_italic(),
found.style != crcbl_font::FontStyle::Normal
);
}
}