use std::path::Path;
use eframe::egui::{self, FontData, FontDefinitions, FontFamily};
const CANDIDATES: &[&str] = &[
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf",
"/usr/share/fonts/TTF/DejaVuSansMono.ttf",
"/usr/share/fonts/dejavu/DejaVuSansMono.ttf",
"/usr/share/fonts/dejavu-sans-mono-fonts/DejaVuSansMono.ttf",
"/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf",
"/usr/share/fonts/liberation-mono/LiberationMono-Regular.ttf",
"/Library/Fonts/Menlo.ttc",
"/System/Library/Fonts/Menlo.ttc",
"C:\\Windows\\Fonts\\consola.ttf",
];
pub fn install(ctx: &egui::Context) {
let Some((name, bytes)) = load(std::env::var("RCMD_EGUI_FONT").ok().as_deref()) else {
return;
};
let mut fonts = FontDefinitions::default();
fonts
.font_data
.insert(name.clone(), FontData::from_owned(bytes).into());
fonts
.families
.entry(FontFamily::Monospace)
.or_default()
.insert(0, name.clone());
fonts
.families
.entry(FontFamily::Proportional)
.or_default()
.insert(0, name);
ctx.set_fonts(fonts);
}
fn load(override_path: Option<&str>) -> Option<(String, Vec<u8>)> {
let mut paths: Vec<&str> = Vec::new();
if let Some(path) = override_path {
paths.push(path);
}
paths.extend(CANDIDATES);
for path in paths {
let path = Path::new(path);
if let Ok(bytes) = std::fs::read(path) {
let name = path
.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| "system-mono".to_string());
return Some((name, bytes));
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_override_that_is_not_there_falls_through() {
let _ = load(Some("/nonexistent/font/that/is/not/here.ttf"));
}
}