use std::collections::HashMap;
use cosmic_text::{
Family, Weight,
fontdb::{Database, ID, Query, Stretch},
skrifa::{FontRef, MetadataProvider, raw::types::Tag},
};
use crate::layout::text_style::{FontStyle, FontWeight, cosmic_family};
pub(crate) fn match_face_weight(db: &Database, family: &str, weight: FontWeight, style: FontStyle) -> Weight {
let requested = weight.to_cosmic();
[cosmic_family(family), Family::SansSerif]
.iter()
.find_map(|family| {
let id = db.query(&Query {
families: std::slice::from_ref(family),
weight: requested,
stretch: Stretch::Normal,
style: style.to_cosmic(),
})?;
if let Some((lightest, heaviest)) = weight_axis(db, id) {
return Some(Weight((f32::from(requested.0)).clamp(lightest, heaviest).round() as u16));
}
db.face(id).map(|face| face.weight)
})
.unwrap_or(requested)
}
fn weight_axis(db: &Database, id: ID) -> Option<(f32, f32)> {
db.with_face_data(id, |data, index| {
let face = FontRef::from_index(data, index).ok()?;
let axis = face.axes().iter().find(|axis| axis.tag() == Tag::new(b"wght"))?;
Some((axis.min_value(), axis.max_value()))
})?
}
#[derive(Default)]
pub(crate) struct FaceWeights {
resolved: HashMap<Box<str>, HashMap<(u16, FontStyle), Weight>>,
}
impl FaceWeights {
pub(crate) fn resolve(&mut self, db: &Database, family: &str, weight: FontWeight, style: FontStyle) -> Weight {
let key = (weight.value(), style);
if let Some(resolved) = self.resolved.get(family).and_then(|weights| weights.get(&key)) {
return *resolved;
}
let resolved = match_face_weight(db, family, weight, style);
self.resolved.entry(family.into()).or_default().insert(key, resolved);
resolved
}
pub(crate) fn clear(&mut self) {
self.resolved.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
const SYSTEM_FONT: &str = "/System/Library/Fonts/SFNS.ttf";
#[test]
fn a_variable_face_is_matched_at_the_weight_asked_for() {
if !std::path::Path::new(SYSTEM_FONT).exists() {
return;
}
let mut db = Database::new();
db.load_font_file(SYSTEM_FONT).expect("the system font loads");
let family = db.faces().next().expect("a face").families[0].0.clone();
assert_eq!(
match_face_weight(&db, &family, FontWeight::Bold, FontStyle::Normal),
Weight(700),
"not the one weight the face names"
);
assert_eq!(
match_face_weight(&db, &family, FontWeight::Normal, FontStyle::Normal),
Weight(400)
);
}
}