Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Fonts: egui's built-in Hack as the last fallback for symbols (#2956)

kezmdore5291dc8f77d
diff --git a/crates/ui-egui/src/fonts.rs b/crates/ui-egui/src/fonts.rs
index 14ec936eb1..1e96674c3d 100644
--- a/crates/ui-egui/src/fonts.rs
+++ b/crates/ui-egui/src/fonts.rs
@@ -51,6 +51,21 @@ pub const HANGUL_FALLBACK: &str = "hangul_fallback";
/// Name of the Thai fallback entry in [`egui::FontDefinitions`], last (#1902).
pub const THAI_FALLBACK: &str = "thai_fallback";
+/// egui's built-in monospace face, also the last fallback of the Proportional family (#2955).
+pub const SYMBOL_FALLBACK: &str = "Hack";
+
+/// `fonts` with egui's built-in Hack last in the Proportional family. Ubuntu-Light, NotoEmoji and
+/// emoji-icon-font lack common symbols such as ▲ ▼ ↑ ↓ → ⁻, which Hack has, so they no longer
+/// show as empty boxes where no system font fills in (#2955). Text the other faces draw keeps them.
+pub fn with_symbol_fallback(mut fonts: egui::FontDefinitions) -> egui::FontDefinitions {
+ if fonts.font_data.contains_key(SYMBOL_FALLBACK) {
+ let list = fonts.families.entry(egui::FontFamily::Proportional).or_default();
+ list.retain(|name| name != SYMBOL_FALLBACK);
+ list.push(SYMBOL_FALLBACK.to_owned());
+ }
+ fonts
+}
+
/// UI languages whose text needs the CJK font. The web app's bundled font covers Japanese only;
/// add Chinese or Korean here together with a font that covers them.
pub const CJK_LANGUAGES: &[&str] = &["ja", "zh", "ko"];
@@ -136,7 +151,7 @@ pub fn install_cjk_font_for(ctx: &egui::Context, bytes: Vec<u8>, language: &str)
if needs_cjk_font(language) {
promote_font_to_front(&mut fonts, CJK_FALLBACK);
}
- ctx.set_fonts(fonts);
+ ctx.set_fonts(with_symbol_fallback(fonts));
true
}
None => false,
@@ -540,13 +555,12 @@ pub fn setup_fonts_for(ctx: &egui::Context, language: &str, ui_font: &str) {
let hangul = HANGUL_FONT_CANDIDATES.iter().find_map(|path| try_read_font(std::path::Path::new(path)));
let thai = THAI_FONT_CANDIDATES.iter().find_map(|path| try_read_font(std::path::Path::new(path)));
let mut fonts = egui::FontDefinitions::default();
- // Set the fonts also when only the interface font changes, so going back to the default
- // takes the chosen one away.
- let mut found = !ui_font.trim().is_empty();
if let Some(font) = self::ui_font(ui_font) {
fonts = with_ui_font(fonts, font);
}
let order = fallback_order(language);
+ // Whether a system fallback was found.
+ let mut found = false;
for name in order {
let bytes = match name {
CJK_FALLBACK => cjk.clone(),
@@ -561,18 +575,18 @@ pub fn setup_fonts_for(ctx: &egui::Context, language: &str, ui_font: &str) {
found = true;
}
}
- if found {
- if needs_cjk_font(language) && ui_font.trim().is_empty() {
- // For a CJK UI (Japanese, Chinese, Korean) without a custom interface font,
- // promote the primary CJK font to the front of FontFamily::Proportional.
- // This ensures that ASCII characters (digits, units, English words) within mixed
- // labels (e.g. "4 個選択中", "2 mm") are drawn with the same font face as the
- // CJK glyphs, sharing identical line height, ascender, and baseline (#1070 follow-up).
- let primary = order[0];
- promote_font_to_front(&mut fonts, primary);
- }
- ctx.set_fonts(fonts);
- }
+ if found && needs_cjk_font(language) && ui_font.trim().is_empty() {
+ // For a CJK UI (Japanese, Chinese, Korean) without a custom interface font,
+ // promote the primary CJK font to the front of FontFamily::Proportional.
+ // This ensures that ASCII characters (digits, units, English words) within mixed
+ // labels (e.g. "4 個選択中", "2 mm") are drawn with the same font face as the
+ // CJK glyphs, sharing identical line height, ascender, and baseline (#1070 follow-up).
+ let primary = order[0];
+ promote_font_to_front(&mut fonts, primary);
+ }
+ // Set the fonts every time: the symbol fallback goes in even when no system fallback is
+ // found (#2955), and going back to the default interface font takes the chosen one away.
+ ctx.set_fonts(with_symbol_fallback(fonts));
}
/// Configure font definitions with system fallbacks for an English UI (all fallbacks are
@@ -609,13 +623,48 @@ fn find_system_chinese_font() -> Option<Vec<u8>> {
#[cfg(target_arch = "wasm32")]
pub fn setup_fonts_for(_ctx: &egui::Context, _language: &str, _ui_font: &str) {}
+/// On web only the symbol fallback goes in at start (#2955); the Japanese font loads later
+/// through [`install_cjk_font`], which keeps it.
#[cfg(target_arch = "wasm32")]
-pub fn setup_fonts(_ctx: &egui::Context) {}
+pub fn setup_fonts(ctx: &egui::Context) {
+ ctx.set_fonts(with_symbol_fallback(egui::FontDefinitions::default()));
+}
#[cfg(test)]
mod tests {
use super::*;
+ /// Whether a face of the Proportional family has `c`. Checked in the font data:
+ /// `Fonts::has_glyph` misses a glyph in the face that also gives the replacement glyph.
+ fn proportional_draws(fonts: &egui::FontDefinitions, c: char) -> bool {
+ fonts.families[&egui::FontFamily::Proportional]
+ .iter()
+ .filter_map(|name| fonts.font_data.get(name))
+ .any(|data| ttf_parser::Face::parse(&data.font, data.index).unwrap().glyph_index(c).is_some())
+ }
+
+ /// The symbols the built-in Latin faces lack draw through the built-in Hack (#2955), which
+ /// comes last and once, after the system fallbacks, and goes in with the desktop's fonts.
+ #[test]
+ fn symbols_fall_back_to_the_built_in_hack() {
+ let fonts = with_symbol_fallback(egui::FontDefinitions::default());
+ for c in "▲▼↑↓→⁻".chars() {
+ assert!(proportional_draws(&fonts, c), "{c:?} (U+{:04X}) has a face", c as u32);
+ }
+ let mut with_cjk = egui::FontDefinitions::default();
+ with_cjk.families.entry(egui::FontFamily::Proportional).or_default().push(CJK_FALLBACK.to_owned());
+ let fonts = with_symbol_fallback(with_symbol_fallback(with_cjk));
+ let list = &fonts.families[&egui::FontFamily::Proportional];
+ assert_eq!(list.last().map(String::as_str), Some(SYMBOL_FALLBACK), "{list:?}");
+ assert_eq!(list.iter().filter(|name| *name == SYMBOL_FALLBACK).count(), 1, "{list:?}");
+ assert_ne!(list.first().map(String::as_str), Some(SYMBOL_FALLBACK), "{list:?}");
+ let ctx = egui::Context::default();
+ setup_fonts_for(&ctx, "en", "");
+ ctx.run_ui(egui::RawInput::default(), |_| {}).textures_delta.clear();
+ let installed = ctx.fonts(|f| f.definitions().families[&egui::FontFamily::Proportional].clone());
+ assert_eq!(installed.last().map(String::as_str), Some(SYMBOL_FALLBACK), "{installed:?}");
+ }
+
#[test]
fn the_interface_font_comes_first_and_keeps_the_fallbacks() {
// #1290: the chosen font leads the Proportional family; the default face stays after it.