Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

Find Windows' fonts wherever Windows is, and Bengali fonts on Windows 10 and Linux

VRonGitHub11776deb5418
diff --git a/apps/rustcord/src/style.rs b/apps/rustcord/src/style.rs
index fde3017..d79a014 100644
--- a/apps/rustcord/src/style.rs
+++ b/apps/rustcord/src/style.rs
@@ -650,7 +650,7 @@ impl Theme {
/// is 11 MB) then live in file-backed pages that are only read in when a
/// glyph needs them, instead of being copied into the heap. Fonts are
/// installed once per run, so the mapping is leaked on purpose.
-fn font_file(path: &str) -> Option<FontData> {
+fn font_file(path: impl AsRef<std::path::Path>) -> Option<FontData> {
let file = std::fs::File::open(path).ok()?;
// SAFETY: Windows' own font files aren't modified while they're in use;
// the mapping is never unmapped (egui keeps the bytes for the whole run).
@@ -659,6 +659,11 @@ fn font_file(path: &str) -> Option<FontData> {
Some(FontData::from_static(bytes))
}
+/// Windows' font folder, `%SystemRoot%\Fonts` (Windows needn't be on C:).
+fn windows_fonts() -> std::path::PathBuf {
+ std::env::var_os("SystemRoot").map_or_else(|| "C:\\Windows".into(), std::path::PathBuf::from).join("Fonts")
+}
+
/// rrsans (gg sans) compiled into the exe (build.rs sets `rustcord_gg_sans` when
/// the files are in `apps/rustcord/fonts/rrsans`): regular, semibold (RustCord's
/// "bold": Discord's weight for names and headings) and mono.
@@ -702,7 +707,7 @@ fn find_gg_sans() -> (Option<std::path::PathBuf>, Option<std::path::PathBuf>) {
dirs.push(std::path::PathBuf::from(local).join("Microsoft\\Windows\\Fonts"));
}
if cfg!(windows) {
- dirs.push("C:\\Windows\\Fonts".into());
+ dirs.push(windows_fonts());
}
let (mut regular, mut heavy): (Option<(u8, std::path::PathBuf)>, Option<(u8, std::path::PathBuf)>) = (None, None);
for dir in dirs {
@@ -853,11 +858,12 @@ pub fn install_fonts(ctx: &egui::Context) {
use_system_text_antialiasing();
let mut fonts = FontDefinitions::default();
let (gg_regular, gg_heavy) = find_gg_sans();
+ // (On Windows, files in its font folder.)
let candidates: &[(&str, &str, bool)] = if cfg!(windows) {
&[
- ("segoe-ui", "C:\\Windows\\Fonts\\segoeui.ttf", true),
- ("segoe-symbol", "C:\\Windows\\Fonts\\seguisym.ttf", false),
- ("segoe-emoji", "C:\\Windows\\Fonts\\seguiemj.ttf", false),
+ ("segoe-ui", "segoeui.ttf", true),
+ ("segoe-symbol", "seguisym.ttf", false),
+ ("segoe-emoji", "seguiemj.ttf", false),
// Other scripts and the decorative characters servers put in
// channel and server names (・ ┃ ꒰ ୨ ᐢ ꕤ ༺ 𓍯 etc.); without these
// they show as boxes. The files are mapped, not read: only the
@@ -866,27 +872,29 @@ pub fn install_fonts(ctx: &egui::Context) {
// made once it draws a glyph). Yu Gothic covers Japanese and most
// Chinese characters; the fonts after Malgun (Korean) are asked,
// in this order, for whatever the ones before them lack.
- ("yu-gothic", "C:\\Windows\\Fonts\\YuGothR.ttc", false),
- ("malgun", "C:\\Windows\\Fonts\\malgun.ttf", false),
+ ("yu-gothic", "YuGothR.ttc", false),
+ ("malgun", "malgun.ttf", false),
// Yi (꒰ ꒱ ꒷) and ﹏.
- ("microsoft-yi-baiti", "C:\\Windows\\Fonts\\msyi.ttf", false),
+ ("microsoft-yi-baiti", "msyi.ttf", false),
// Canadian syllabics (ᐢ ᐟ ᘏ) and Cherokee.
- ("gadugi", "C:\\Windows\\Fonts\\gadugi.ttf", false),
+ ("gadugi", "gadugi.ttf", false),
// Vai (ꕤ ꔫ), N'Ko, Ethiopic, Tifinagh.
- ("ebrima", "C:\\Windows\\Fonts\\ebrima.ttf", false),
+ ("ebrima", "ebrima.ttf", false),
// Historic scripts (𓍯 𐭩), the Bamum supplement (𖦹), Syriac marks.
- ("segoe-ui-historic", "C:\\Windows\\Fonts\\seguihis.ttf", false),
- // Indian scripts beyond the digits Segoe UI has (ৎ ൃ).
- ("nirmala", "C:\\Windows\\Fonts\\Nirmala.ttc", false),
+ ("segoe-ui-historic", "seguihis.ttf", false),
+ // Indian scripts beyond the digits Segoe UI has (ৎ ൃ): Nirmala UI,
+ // a .ttf on Windows 10 and in a collection on Windows 11.
+ ("nirmala-ttf", "Nirmala.ttf", false),
+ ("nirmala", "Nirmala.ttc", false),
// Tibetan (༄ ༺ ༻), Tai Le (ᥴ), Mongolian (ᡣ).
- ("himalaya", "C:\\Windows\\Fonts\\himalaya.ttf", false),
- ("tai-le", "C:\\Windows\\Fonts\\taile.ttf", false),
- ("mongolian-baiti", "C:\\Windows\\Fonts\\monbaiti.ttf", false),
+ ("himalaya", "himalaya.ttf", false),
+ ("tai-le", "taile.ttf", false),
+ ("mongolian-baiti", "monbaiti.ttf", false),
// Thai (none of the above has it): Leelawadee UI, which Chrome
// picks for Thai on Windows.
- ("leelawadee-ui", "C:\\Windows\\Fonts\\LeelawUI.ttf", false),
+ ("leelawadee-ui", "LeelawUI.ttf", false),
// Windows 11's catch-all for the rest (𐙚 ᭡ ꪆ ᰔ ᯓ 𖹭); Windows 10 lacks it.
- ("sans-serif-collection", "C:\\Windows\\Fonts\\SansSerifCollection.ttf", false),
+ ("sans-serif-collection", "SansSerifCollection.ttf", false),
]
} else if cfg!(target_os = "linux") {
// Symbols and other scripts, where each distribution keeps them
@@ -904,6 +912,11 @@ pub fn install_fonts(ctx: &egui::Context) {
("noto-thai-fedora", "/usr/share/fonts/google-noto/NotoSansThai-Regular.ttf", false),
// (Fedora installs the variable font by default.)
("noto-thai-fedora-vf", "/usr/share/fonts/google-noto-vf/NotoSansThai[wght].ttf", false),
+ // Bengali.
+ ("noto-bengali", "/usr/share/fonts/truetype/noto/NotoSansBengali-Regular.ttf", false),
+ ("noto-bengali-arch", "/usr/share/fonts/noto/NotoSansBengali-Regular.ttf", false),
+ ("noto-bengali-fedora", "/usr/share/fonts/google-noto/NotoSansBengali-Regular.ttf", false),
+ ("noto-bengali-fedora-vf", "/usr/share/fonts/google-noto-vf/NotoSansBengali[wght].ttf", false),
]
} else if cfg!(target_os = "macos") {
// Symbols, Chinese and Japanese (Hiragino Sans GB has kana too) and
@@ -921,7 +934,7 @@ pub fn install_fonts(ctx: &egui::Context) {
let mut primary_pos = 0;
// gg sans first (files on this PC, else the built-in copy); Segoe UI fills
// in anything it lacks (other scripts, symbols).
- let gg_file = gg_regular.as_ref().and_then(|p| font_file(&p.to_string_lossy()));
+ let gg_file = gg_regular.as_ref().and_then(font_file);
let gg = match (gg_file, EMBEDDED_GG_SANS) {
(Some(data), _) => {
log::info!("fonts: using gg sans from {}", gg_regular.as_ref().map(|p| p.display().to_string()).unwrap_or_default());
@@ -942,7 +955,7 @@ pub fn install_fonts(ctx: &egui::Context) {
// ("gg sans", "Noto Sans", "Helvetica Neue", Helvetica, Arial): gg sans
// has neither, and on Windows Arial is the first of the rest that does.
#[cfg(windows)]
- if let Some(data) = font_file("C:\\Windows\\Fonts\\arial.ttf") {
+ if let Some(data) = font_file(windows_fonts().join("arial.ttf")) {
fonts.font_data.insert(ARIAL_RTL.into(), data.only(hebrew_or_arabic));
fonts.families.entry(FontFamily::Proportional).or_default().insert(primary_pos, ARIAL_RTL.into());
fonts.families.entry(FontFamily::Monospace).or_default().push(ARIAL_RTL.into());
@@ -954,7 +967,8 @@ pub fn install_fonts(ctx: &egui::Context) {
fonts.families.entry(FontFamily::Monospace).or_default().insert(0, "gg-sans-mono".into());
}
for (name, path, primary) in candidates {
- let Some(data) = font_file(path) else { continue };
+ let file = if cfg!(windows) { windows_fonts().join(path) } else { std::path::PathBuf::from(path) };
+ let Some(data) = font_file(file) else { continue };
fonts.font_data.insert(name.to_string(), data);
let family = fonts.families.entry(FontFamily::Proportional).or_default();
if *primary {
@@ -974,21 +988,21 @@ pub fn install_fonts(ctx: &egui::Context) {
// falls back to the regular fonts) because egui panics on unknown families.
let mut bold = fonts.families.get(&FontFamily::Proportional).cloned().unwrap_or_default();
if cfg!(windows) {
- if let Some(data) = font_file("C:\\Windows\\Fonts\\segoeuib.ttf") {
+ if let Some(data) = font_file(windows_fonts().join("segoeuib.ttf")) {
fonts.font_data.insert("segoe-ui-bold".into(), data);
bold.insert(0, "segoe-ui-bold".into());
}
// Thai in bold; otherwise the regular Leelawadee UI is found first.
- if let Some(data) = font_file("C:\\Windows\\Fonts\\LeelaUIb.ttf") {
+ if let Some(data) = font_file(windows_fonts().join("LeelaUIb.ttf")) {
fonts.font_data.insert("leelawadee-ui-bold".into(), data.only(thai));
bold.insert(0, "leelawadee-ui-bold".into());
}
- if let Some(data) = font_file("C:\\Windows\\Fonts\\arialbd.ttf") {
+ if let Some(data) = font_file(windows_fonts().join("arialbd.ttf")) {
fonts.font_data.insert("arial-rtl-bold".into(), data.only(hebrew_or_arabic));
bold.insert(0, "arial-rtl-bold".into());
}
}
- let heavy = gg_heavy.as_ref().and_then(|p| font_file(&p.to_string_lossy())).or_else(|| EMBEDDED_GG_SANS.map(|(_, semibold, _)| FontData::from_static(semibold)));
+ let heavy = gg_heavy.as_ref().and_then(font_file).or_else(|| EMBEDDED_GG_SANS.map(|(_, semibold, _)| FontData::from_static(semibold)));
if let Some(data) = heavy {
fonts.font_data.insert("gg-sans-heavy".into(), data);
bold.insert(0, "gg-sans-heavy".into());
@@ -1053,10 +1067,11 @@ pub fn install_fonts(ctx: &egui::Context) {
fonts.font_data.insert("discord-icons-fallback".into(), FontData::from_static(DISCORD_ICONS_FALLBACK));
icons.push("discord-icons-fallback".into());
}
- for path in ["C:\\Windows\\Fonts\\SegoeIcons.ttf", "C:\\Windows\\Fonts\\segmdl2.ttf"] {
- if let Some(data) = font_file(path) {
- fonts.font_data.insert(path.into(), data);
- icons.push(path.to_string());
+ #[cfg(windows)]
+ for file in ["SegoeIcons.ttf", "segmdl2.ttf"] {
+ if let Some(data) = font_file(windows_fonts().join(file)) {
+ fonts.font_data.insert(file.into(), data);
+ icons.push(file.to_string());
}
}
icons.extend(fonts.families.get(&FontFamily::Proportional).cloned().unwrap_or_default());
@@ -1272,7 +1287,7 @@ mod tests {
super::install_fonts(&ctx);
let _ = ctx.run(Default::default(), |_| {});
let mut chars = String::from("꒰꒱꒷୨୧⊹⋆⟡ᐢᐟᘏꕤꔫ༄༺༻ৎ𓍯𐭩﹏・┃");
- if std::path::Path::new(r"C:\Windows\Fonts\SansSerifCollection.ttf").exists() {
+ if super::windows_fonts().join("SansSerifCollection.ttf").exists() {
chars.push_str("𐙚᭡ꪆᰔᯓ");
}
let missing: String = chars.chars().filter(|c| !ctx.fonts(|f| f.has_glyph(&FontId::proportional(16.0), *c))).collect();