Medlar/crates/app/src/fonts.rs
2026-11-25 00:28:08 -06:00

48 lines
1.8 KiB
Rust

//! Rubik (variable, embedded) in three weights, with egui's and the system's fonts behind it for other scripts.
use crate::tokens;
use eframe::egui::{self, FontData, FontDefinitions, FontFamily, FontId, TextStyle};
use eframe::epaint::text::{FontTweak, VariationCoords};
use std::sync::Arc;
const MED: &str = "rubik-500";
const SEMI: &str = "rubik-600";
static RUBIK: &[u8] = include_bytes!("../../../assets/Rubik.ttf");
pub fn install(ctx: &egui::Context) {
let mut defs = FontDefinitions::default();
let fallback = defs.families.get(&FontFamily::Proportional).cloned().unwrap_or_default();
for (key, family, weight) in [
("rubik", FontFamily::Proportional, 400.0),
(MED, FontFamily::Name(MED.into()), 500.0),
(SEMI, FontFamily::Name(SEMI.into()), 600.0),
] {
let tweak = FontTweak { coords: VariationCoords::new([(b"wght", weight)]), ..FontTweak::default() };
defs.font_data.insert(key.into(), Arc::new(FontData::from_static(RUBIK).tweak(tweak)));
let names = std::iter::once(key.to_string()).chain(fallback.iter().cloned()).collect();
defs.families.insert(family, names);
}
ctx.set_fonts(defs);
ctx.all_styles_mut(|s| {
s.text_styles = [
(TextStyle::Small, ui(tokens::text::LABEL)),
(TextStyle::Body, ui(tokens::text::BODY)),
(TextStyle::Button, med(tokens::text::BODY)),
(TextStyle::Heading, semi(tokens::text::TITLE)),
(TextStyle::Monospace, FontId::monospace(tokens::text::BODY)),
]
.into();
});
}
pub fn ui(size: f32) -> FontId {
FontId::proportional(size)
}
pub fn med(size: f32) -> FontId {
FontId::new(size, FontFamily::Name(MED.into()))
}
pub fn semi(size: f32) -> FontId {
FontId::new(size, FontFamily::Name(SEMI.into()))
}