use std::sync::OnceLock;
pub fn hud_font() -> Option<&'static fontdue::Font> {
static FONT: OnceLock<Option<fontdue::Font>> = OnceLock::new();
FONT.get_or_init(|| {
const CANDIDATES: &[&str] = &[
"/usr/share/fonts/liberation/LiberationSans-Bold.ttf",
"/usr/share/fonts/liberation/LiberationSans-Regular.ttf",
"/usr/share/fonts/TTF/DejaVuSans-Bold.ttf",
"/usr/share/fonts/TTF/DejaVuSans.ttf",
"/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf",
"/usr/share/fonts/dejavu/DejaVuSans.ttf",
"/usr/share/fonts/noto/NotoSans-Regular.ttf",
"/System/Library/Fonts/SFNS.ttf",
"/System/Library/Fonts/SFNSRounded.ttf",
"/System/Library/Fonts/Supplemental/Arial Bold.ttf",
"/System/Library/Fonts/Supplemental/Arial.ttf",
"/Library/Fonts/Arial Bold.ttf",
"/Library/Fonts/Arial.ttf",
];
for path in CANDIDATES {
if let Ok(bytes) = std::fs::read(path) {
if let Ok(font) = fontdue::Font::from_bytes(
bytes.as_slice(),
fontdue::FontSettings::default(),
) {
log::info!("hud font: {path}");
return Some(font);
}
}
}
log::warn!("hud font: no usable system font found; pills will render empty");
None
})
.as_ref()
}
pub fn measure_text_width(font: &fontdue::Font, text: &str, px_size: f32) -> f32 {
let mut w = 0.0;
for ch in text.chars() {
let m = font.metrics(ch, px_size);
w += m.advance_width.max(0.0);
}
w
}