use std::collections::BTreeMap;
use std::sync::OnceLock;
use iced::advanced::graphics::text::cosmic_text::fontdb::{self, Family as DbFamily};
use iced::advanced::graphics::text::font_system;
use iced::font::{Style, Weight};
use iced::Font;
use crate::host_font;
pub const UI: Font = Font::DEFAULT;
pub const UI_BOLD: Font = Font {
weight: Weight::Bold,
..Font::DEFAULT
};
pub const UI_ITALIC: Font = Font {
style: Style::Italic,
..Font::DEFAULT
};
pub const MONO: Font = Font::MONOSPACE;
pub const DISPLAY: u32 = 36;
pub const PAGE: u32 = 22;
pub const TITLE: u32 = 16;
pub const BODY: u32 = 14;
pub const META: u32 = 12;
pub const CODE: u32 = 12;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FontFace {
Ui,
Mono,
}
impl FontFace {
pub fn font(self) -> Font {
match self {
Self::Ui => UI,
Self::Mono => MONO,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeRole {
Display,
Page,
Title,
Body,
Meta,
Code,
}
impl TypeRole {
pub fn m3(self) -> crate::m3::TypeRole {
match self {
Self::Display => crate::m3::TypeRole::DisplaySmall,
Self::Page => crate::m3::TypeRole::TitleLarge,
Self::Title => crate::m3::TypeRole::TitleMedium,
Self::Body => crate::m3::TypeRole::BodyMedium,
Self::Meta => crate::m3::TypeRole::LabelMedium,
Self::Code => crate::m3::TypeRole::BodySmall,
}
}
pub fn size(self) -> u32 {
self.m3().scale().size as u32
}
pub fn font(self) -> Font {
match self {
Self::Code => MONO,
Self::Display | Self::Title | Self::Page => UI_BOLD,
Self::Body | Self::Meta => UI,
}
}
}
pub fn install_platform_faces() {
static ONCE: OnceLock<()> = OnceLock::new();
ONCE.get_or_init(|| {
let lock = font_system();
let mut system = lock.write().expect("font system");
let db = system.raw().db_mut();
let covers = faces_from_db(db);
let sans_current = db.family_name(&DbFamily::SansSerif).to_string();
let mono_current = db.family_name(&DbFamily::Monospace).to_string();
let sans = select_family(
&covers,
FamilyKind::Ui,
&host_font::ui_preferences(),
&sans_current,
);
let mono = select_family(
&covers,
FamilyKind::Mono,
&host_font::mono_preferences(),
&mono_current,
);
db.set_sans_serif_family(sans);
db.set_monospace_family(mono);
});
}
pub fn sans_family() -> String {
let lock = font_system();
let mut system = lock.write().expect("font system");
system
.raw()
.db()
.family_name(&DbFamily::SansSerif)
.to_string()
}
pub fn bind_sans_family(name: impl AsRef<str>) {
let name = name.as_ref();
if name.is_empty() {
return;
}
let lock = font_system();
let mut system = lock.write().expect("font system");
system.raw().db_mut().set_sans_serif_family(name);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FamilyKind {
Ui,
Mono,
}
#[derive(Debug, Clone)]
struct FaceCover {
families: Vec<String>,
monospaced: bool,
weight: u16,
post_script_name: String,
}
fn faces_from_db(db: &fontdb::Database) -> Vec<FaceCover> {
db.faces()
.map(|face| FaceCover {
families: face.families.iter().map(|(n, _)| n.clone()).collect(),
monospaced: face.monospaced,
weight: face.weight.0,
post_script_name: face.post_script_name.clone(),
})
.collect()
}
fn family_index(faces: &[FaceCover]) -> BTreeMap<String, FamilyCoverage> {
let mut map: BTreeMap<String, FamilyCoverage> = BTreeMap::new();
for face in faces {
if face.post_script_name.contains("Emoji") {
continue;
}
let Some(primary) = face.families.first() else {
continue;
};
let entry = map.entry(primary.clone()).or_default();
entry.monospaced |= face.monospaced;
if face.weight == 400 {
entry.has_normal = true;
}
if face.weight == 700 {
entry.has_bold = true;
}
for alt in &face.families {
if alt == primary {
continue;
}
let alt_entry = map.entry(alt.clone()).or_default();
alt_entry.monospaced |= face.monospaced;
if face.weight == 400 {
alt_entry.has_normal = true;
}
if face.weight == 700 {
alt_entry.has_bold = true;
}
alt_entry.canonical = Some(primary.clone());
}
}
map
}
#[derive(Debug, Clone, Default)]
struct FamilyCoverage {
monospaced: bool,
has_normal: bool,
has_bold: bool,
canonical: Option<String>,
}
impl FamilyCoverage {
fn usable(&self, kind: FamilyKind) -> bool {
match kind {
FamilyKind::Ui => !self.monospaced && self.has_normal && self.has_bold,
FamilyKind::Mono => self.monospaced && self.has_normal,
}
}
fn bind_name<'a>(&'a self, key: &'a str) -> &'a str {
self.canonical.as_deref().unwrap_or(key)
}
}
fn select_family(
faces: &[FaceCover],
kind: FamilyKind,
preferences: &[String],
current: &str,
) -> String {
let index = family_index(faces);
let mut tried = Vec::new();
let mut consider = |name: &str| -> Option<String> {
if name.is_empty() || tried.iter().any(|t| t == name) {
return None;
}
tried.push(name.to_string());
let cover = index.get(name)?;
if cover.usable(kind) {
Some(cover.bind_name(name).to_string())
} else {
None
}
};
for pref in preferences {
if let Some(name) = consider(pref) {
return name;
}
}
if let Some(name) = consider(current) {
return name;
}
for (name, cover) in &index {
if cover.canonical.is_some() {
continue;
}
if cover.usable(kind) {
return name.clone();
}
}
current.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
static FONT_DB: Mutex<()> = Mutex::new(());
fn cover(family: &str, mono: bool, weight: u16) -> FaceCover {
FaceCover {
families: vec![family.into()],
monospaced: mono,
weight,
post_script_name: format!("{family}-{weight}"),
}
}
fn cover_alias(primary: &str, alias: &str, mono: bool, weight: u16) -> FaceCover {
FaceCover {
families: vec![primary.into(), alias.into()],
monospaced: mono,
weight,
post_script_name: format!("{primary}-{weight}"),
}
}
#[test]
fn scale_is_ordered() {
assert!(TypeRole::Display.size() > TypeRole::Page.size());
assert!(TypeRole::Page.size() > TypeRole::Title.size());
assert!(TypeRole::Title.size() > TypeRole::Body.size());
assert!(TypeRole::Body.size() > TypeRole::Meta.size());
assert!(TypeRole::Code.size() >= TypeRole::Meta.size());
assert_eq!(UI, Font::DEFAULT);
assert_eq!(MONO, Font::MONOSPACE);
assert_eq!(TypeRole::Display.size(), DISPLAY);
assert_eq!(TypeRole::Display.font(), UI_BOLD);
for role in [
TypeRole::Display,
TypeRole::Page,
TypeRole::Title,
TypeRole::Body,
TypeRole::Meta,
TypeRole::Code,
] {
assert_eq!(role.size() as f32, role.m3().scale().size);
}
assert_eq!(TypeRole::Page.size(), PAGE);
assert_eq!(TypeRole::Title.size(), TITLE);
assert_eq!(TypeRole::Body.size(), BODY);
assert_eq!(TypeRole::Meta.size(), META);
assert_eq!(TypeRole::Code.size(), CODE);
assert_eq!(TypeRole::Code.font(), MONO);
assert_eq!(TypeRole::Title.font(), UI_BOLD);
assert_eq!(TypeRole::Page.font(), UI_BOLD);
assert_eq!(TypeRole::Body.font(), UI);
assert_eq!(TypeRole::Meta.font(), UI);
assert_eq!(FontFace::Ui.font(), UI);
assert_eq!(FontFace::Mono.font(), MONO);
let _ = UI_ITALIC;
}
#[test]
fn bind_sans_family_sets_the_named_face() {
let _g = FONT_DB.lock().expect("font db");
bind_sans_family("");
assert!(!sans_family().is_empty());
bind_sans_family("Noto Sans");
assert_eq!(sans_family(), "Noto Sans");
}
#[test]
fn prefers_os_list_when_family_has_normal_and_bold() {
let faces = vec![
cover("Missing", false, 400),
cover("GoodSans", false, 400),
cover("GoodSans", false, 700),
cover("Other", false, 400),
cover("Other", false, 700),
cover("MonoA", true, 400),
];
let name = select_family(
&faces,
FamilyKind::Ui,
&["NoSuch".into(), "GoodSans".into()],
"Missing",
);
assert_eq!(name, "GoodSans");
}
#[test]
fn rejects_ui_family_without_bold_weight() {
let faces = vec![
cover("SystemOnly", false, 400),
cover("HasBold", false, 400),
cover("HasBold", false, 700),
];
let name = select_family(&faces, FamilyKind::Ui, &["SystemOnly".into()], "SystemOnly");
assert_eq!(name, "HasBold");
}
#[test]
fn keeps_current_when_already_usable() {
let faces = vec![
cover("Current", false, 400),
cover("Current", false, 700),
cover("Other", false, 400),
cover("Other", false, 700),
];
let name = select_family(&faces, FamilyKind::Ui, &[], "Current");
assert_eq!(name, "Current");
}
#[test]
fn mono_needs_monospaced_normal_only() {
let faces = vec![
cover("Prop", false, 400),
cover("Prop", false, 700),
cover("Code", true, 400),
];
let name = select_family(&faces, FamilyKind::Mono, &["Prop".into()], "MissingMono");
assert_eq!(name, "Code");
}
#[test]
fn resolves_alias_to_primary_family_name() {
let faces = vec![
cover_alias("System Font", ".SF NS", false, 400),
cover("RealSans", false, 400),
cover("RealSans", false, 700),
];
let name = select_family(&faces, FamilyKind::Ui, &[".SF NS".into()], "System Font");
assert_eq!(name, "RealSans");
}
#[test]
fn bold_alias_counts_toward_ui_family() {
let faces = vec![
cover_alias("System Font", ".SF NS", false, 400),
cover_alias("System Font", ".SF NS", false, 700),
];
let name = select_family(&faces, FamilyKind::Ui, &[".SF NS".into()], "other");
assert_eq!(name, "System Font");
}
#[test]
fn last_resort_keeps_current_when_nothing_usable() {
let faces = vec![cover("Thin", false, 400)];
let name = select_family(&faces, FamilyKind::Ui, &[], "Thin");
assert_eq!(name, "Thin");
}
#[test]
fn family_index_skips_emoji_and_empty_name_lists() {
let faces = vec![
FaceCover {
families: vec![],
monospaced: false,
weight: 400,
post_script_name: "Empty-400".into(),
},
FaceCover {
families: vec!["EmojiOne".into()],
monospaced: false,
weight: 400,
post_script_name: "EmojiOneColor".into(),
},
cover("Keep", false, 400),
cover("Keep", false, 700),
];
let idx = family_index(&faces);
assert!(!idx.contains_key("EmojiOne"));
assert!(idx.contains_key("Keep"));
assert_eq!(
select_family(&faces, FamilyKind::Ui, &["Keep".into()], "x"),
"Keep"
);
}
#[test]
fn consider_skips_empty_and_duplicate_preferences() {
let faces = vec![
cover("A", false, 400),
cover("A", false, 700),
cover("B", false, 400),
cover("B", false, 700),
];
let name = select_family(
&faces,
FamilyKind::Ui,
&["".into(), "A".into(), "A".into(), "B".into()],
"B",
);
assert_eq!(name, "A");
}
#[test]
fn install_binds_usable_ui_and_mono_families() {
let _g = FONT_DB.lock().expect("font db");
install_platform_faces();
let lock = font_system();
let mut system = lock.write().expect("font system");
let db = system.raw().db();
let sans_name = db.family_name(&DbFamily::SansSerif).to_string();
let mono_name = db.family_name(&DbFamily::Monospace).to_string();
assert!(!sans_name.is_empty());
assert!(!mono_name.is_empty());
let normal = fontdb::Query {
families: &[DbFamily::SansSerif],
weight: fontdb::Weight::NORMAL,
stretch: fontdb::Stretch::Normal,
style: fontdb::Style::Normal,
};
let bold = fontdb::Query {
families: &[DbFamily::SansSerif],
weight: fontdb::Weight::BOLD,
stretch: fontdb::Stretch::Normal,
style: fontdb::Style::Normal,
};
let mono = fontdb::Query {
families: &[DbFamily::Monospace],
weight: fontdb::Weight::NORMAL,
stretch: fontdb::Stretch::Normal,
style: fontdb::Style::Normal,
};
let normal_id = db.query(&normal).expect("sans normal face");
let bold_id = db.query(&bold).expect("sans bold face");
let mono_id = db.query(&mono).expect("mono face");
let normal_face = db.face(normal_id).expect("normal face info");
let bold_face = db.face(bold_id).expect("bold face info");
let mono_face = db.face(mono_id).expect("mono face info");
assert!(!normal_face.monospaced);
assert!(!bold_face.monospaced);
assert!(mono_face.monospaced);
assert_eq!(bold_face.weight.0, 700);
}
}