Repository navigation
fix(fonts): support Hebrew UI fallback alongside Arabic #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nocturney
wants to merge
9
commits into
storytold:main
Choose a base branch
from
nocturney:fix/ui-fallback-hebrew-20261111
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5c2e8a3
fix(fonts): add Hebrew system UI fallback alongside existing Arabic
nocturney 72e4f9a
style(fonts): rustfmt Hebrew fallback code
nocturney 124afab
fix(fonts): add verified macOS Hebrew fallback faces
7ab0dc9
fix(fonts): accept Hebrew and Arabic OS locale variants
fc21d59
fix(fonts): include local DejaVu Hebrew fallback on FreeBSD
ba18dc2
test(fonts): preserve Hebrew and Arabic fallbacks across locale switches
58d6606
fix(fonts): recognize legacy iw Hebrew locale tags
9b7f336
test(fonts): verify native Hebrew glyphs in egui UI families
7f5a4cd
test(i18n): verify active Arabic and Hebrew glyph families across lan…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,92 +1,212 @@ | ||
| //! Arabic fallback fonts for the UI. egui's default fonts have no Arabic glyphs, so translated | ||
| //! UIs need fallback fonts from the operating system. | ||
| //! | ||
| //! Windows: Segoe UI, Tahoma, Arial | ||
| //! macOS: SF Arabic, Geeza Pro, Arial Unicode | ||
| //! Linux: Noto Sans Arabic, DejaVu Sans | ||
| //! System fallback fonts for Arabic and Hebrew UI text. | ||
| //! Both scripts are requested when the language picker is visible, so their | ||
| //! native-script language names can be drawn before the user selects them. | ||
| //! No font binaries are bundled with MaterialCraft by this module. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use egui::{FontData, FontFamily}; | ||
|
|
||
| /// Candidate system font files: (path, face index in a collection). | ||
| /// Existing system font candidates, ordered by preference; Hebrew and Arabic | ||
| /// have separate lists because an Arabic-only face cannot draw Hebrew. | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| fn system_files() -> Vec<(std::path::PathBuf, u32)> { | ||
| fn system_files(hebrew: bool) -> Vec<(std::path::PathBuf, u32)> { | ||
| use std::path::PathBuf; | ||
| let mut v: Vec<(PathBuf, u32)> = Vec::new(); | ||
| if cfg!(windows) { | ||
| let fonts = PathBuf::from(std::env::var("WINDIR").unwrap_or_else(|_| "C:\\Windows".into())).join("Fonts"); | ||
| let files: &[&str] = &["segoeui.ttf", "tahoma.ttf", "arial.ttf"]; | ||
| let files: &[&str] = if hebrew { &["segoeui.ttf", "arial.ttf", "david.ttf", "tahoma.ttf"] } else { &["segoeui.ttf", "tahoma.ttf", "arial.ttf"] }; | ||
| v.extend(files.iter().map(|f| (fonts.join(f), 0))); | ||
| } else if cfg!(target_os = "macos") { | ||
| let files: &[&str] = &["/System/Library/Fonts/SFArabic.ttf", "/System/Library/Fonts/Supplemental/GeezaPro.ttc"]; | ||
| let files: &[&str] = if hebrew { | ||
| &[ | ||
| "/System/Library/Fonts/SFHebrew.ttf", | ||
| "/System/Library/Fonts/ArialHB.ttc", | ||
| "/System/Library/Fonts/SFHebrewRounded.ttf", | ||
| "/System/Library/Fonts/Supplemental/Arial Unicode.ttf", | ||
| ] | ||
| } else { | ||
| &["/System/Library/Fonts/SFArabic.ttf", "/System/Library/Fonts/GeezaPro.ttc", "/System/Library/Fonts/Supplemental/Arial Unicode.ttf"] | ||
| }; | ||
| v.extend(files.iter().map(|f| (PathBuf::from(f), 0))); | ||
| v.push((PathBuf::from("/System/Library/Fonts/Supplemental/Arial Unicode.ttf"), 0)); | ||
| } else { | ||
| for f in [ | ||
| "/usr/share/fonts/truetype/noto/NotoSansArabic-Regular.ttf", | ||
| "/usr/share/fonts/noto/NotoSansArabic-Regular.ttf", | ||
| "/usr/share/fonts/google-noto/NotoSansArabic-Regular.ttf", | ||
| "/usr/local/share/fonts/noto/NotoSansArabic-Regular.ttf", | ||
| "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", | ||
| "/usr/share/fonts/dejavu/DejaVuSans.ttf", | ||
| "/usr/local/share/fonts/dejavu/DejaVuSans.ttf", | ||
| ] { | ||
| v.push((PathBuf::from(f), 0)); | ||
| } | ||
| let files: &[&str] = if hebrew { | ||
| &[ | ||
| "/usr/share/fonts/truetype/noto/NotoSansHebrew-Regular.ttf", | ||
| "/usr/share/fonts/noto/NotoSansHebrew-Regular.ttf", | ||
| "/usr/share/fonts/google-noto/NotoSansHebrew-Regular.ttf", | ||
| "/usr/local/share/fonts/noto/NotoSansHebrew-Regular.ttf", | ||
| "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", | ||
| "/usr/share/fonts/dejavu/DejaVuSans.ttf", | ||
| "/usr/local/share/fonts/dejavu/DejaVuSans.ttf", | ||
| ] | ||
| } else { | ||
| &[ | ||
| "/usr/share/fonts/truetype/noto/NotoSansArabic-Regular.ttf", | ||
| "/usr/share/fonts/noto/NotoSansArabic-Regular.ttf", | ||
| "/usr/share/fonts/google-noto/NotoSansArabic-Regular.ttf", | ||
| "/usr/local/share/fonts/noto/NotoSansArabic-Regular.ttf", | ||
| "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", | ||
| "/usr/share/fonts/dejavu/DejaVuSans.ttf", | ||
| "/usr/local/share/fonts/dejavu/DejaVuSans.ttf", | ||
| ] | ||
| }; | ||
| v.extend(files.iter().map(|f| (PathBuf::from(f), 0))); | ||
| } | ||
| v | ||
| } | ||
|
|
||
| /// Fallback fonts manager for the egui context. | ||
| /// OS locale tags may include region/encoding suffixes, whereas Preferences | ||
| /// normally stores a two-letter language code. | ||
| fn matches_language(language: &str, code: &str) -> bool { | ||
| language.split(['-', '_', '.']).next().is_some_and(|part| part.eq_ignore_ascii_case(code)) | ||
| } | ||
|
|
||
| /// Loaded system faces persist as egui fallbacks when the user changes language. | ||
| #[derive(Default)] | ||
| pub struct UiFonts { | ||
| installed: bool, | ||
| data: Option<Arc<FontData>>, | ||
| attempted_arabic: bool, | ||
| attempted_hebrew: bool, | ||
| arabic: Option<Arc<FontData>>, | ||
| hebrew: Option<Arc<FontData>>, | ||
| } | ||
|
|
||
| impl UiFonts { | ||
| /// Update fallback fonts according to active language and whether the preferences dialog is open. | ||
| /// Install a font if its script is selected, or both while the picker is open. | ||
| pub fn update(&mut self, ctx: &egui::Context, language: &str, preferences_open: bool) { | ||
| let want_arabic = language == "ar" || preferences_open; | ||
| if !want_arabic || self.installed { | ||
| let want_arabic = matches_language(language, "ar") || preferences_open; | ||
| let want_hebrew = matches_language(language, "he") || preferences_open; | ||
| let mut changed = false; | ||
| if want_arabic && !self.attempted_arabic { | ||
| self.attempted_arabic = true; | ||
| self.arabic = Self::load_system(false); | ||
| changed = true; | ||
| } | ||
| if want_hebrew && !self.attempted_hebrew { | ||
| self.attempted_hebrew = true; | ||
| self.hebrew = Self::load_system(true); | ||
| changed = true; | ||
| } | ||
| if !changed { | ||
| return; | ||
| } | ||
| let Some(data) = self.load_arabic() else { return }; | ||
|
|
||
| let mut defs = crate::theme::font_definitions(); | ||
| let name = "script-Arab".to_string(); | ||
| defs.font_data.insert(name.clone(), data); | ||
| defs.families.entry(FontFamily::Proportional).or_default().push(name.clone()); | ||
| defs.families.entry(FontFamily::Monospace).or_default().push(name); | ||
| ctx.set_fonts(defs); | ||
| self.installed = true; | ||
| let mut installed_any = false; | ||
| for (name, data) in [("script-Arab", self.arabic.as_ref()), ("script-Hebr", self.hebrew.as_ref())] { | ||
| if let Some(data) = data { | ||
| let name = name.to_string(); | ||
| defs.font_data.insert(name.clone(), data.clone()); | ||
| defs.families.entry(FontFamily::Proportional).or_default().push(name.clone()); | ||
| defs.families.entry(FontFamily::Monospace).or_default().push(name); | ||
| installed_any = true; | ||
| } | ||
| } | ||
| if installed_any { | ||
| ctx.set_fonts(defs); | ||
| } | ||
| } | ||
|
|
||
| /// Whether an Arabic fallback font has been found and loaded. | ||
| /// Whether an Arabic fallback was found and loaded. | ||
| pub fn covers_arabic(&self) -> bool { | ||
| self.data.is_some() | ||
| self.arabic.is_some() | ||
| } | ||
|
|
||
| fn load_arabic(&mut self) -> Option<Arc<FontData>> { | ||
| if let Some(d) = &self.data { | ||
| return Some(d.clone()); | ||
| } | ||
| /// Whether a Hebrew fallback was found and loaded. | ||
| pub fn covers_hebrew(&self) -> bool { | ||
| self.hebrew.is_some() | ||
| } | ||
|
|
||
| fn load_system(hebrew: bool) -> Option<Arc<FontData>> { | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| for (path, index) in system_files() { | ||
| let Ok(meta) = std::fs::metadata(&path) else { continue }; | ||
| for (path, index) in system_files(hebrew) { | ||
| let Ok(meta) = std::fs::metadata(&path) else { | ||
| continue; | ||
| }; | ||
| if !meta.is_file() || meta.len() == 0 || meta.len() > (128 << 20) { | ||
| continue; | ||
| } | ||
| if let Ok(bytes) = std::fs::read(&path) { | ||
| let mut data = FontData::from_owned(bytes); | ||
| data.index = index; | ||
| let arc = Arc::new(data); | ||
| self.data = Some(arc.clone()); | ||
| log::info!("UI font fallback for Arabic: {}", path.display()); | ||
| return Some(arc); | ||
| let name = if hebrew { "Hebrew" } else { "Arabic" }; | ||
| log::info!("UI font fallback for {}: {}", name, path.display()); | ||
| return Some(Arc::new(data)); | ||
| } | ||
| } | ||
| log::warn!("no font found for Arabic text; system fonts may show boxes"); | ||
| let name = if hebrew { "Hebrew" } else { "Arabic" }; | ||
| log::warn!("no font found for {} UI text; system fonts may show boxes", name); | ||
| None | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(test, target_os = "macos"))] | ||
| mod tests { | ||
| use super::{UiFonts, system_files}; | ||
|
|
||
| #[test] | ||
| fn hebrew_native_font_fallback_loads_from_macos() { | ||
| let candidates = system_files(true); | ||
| assert!(candidates.iter().any(|(path, _)| path.ends_with("SFHebrew.ttf"))); | ||
| assert!(candidates.iter().any(|(path, _)| path.ends_with("ArialHB.ttc"))); | ||
|
|
||
| let mut fonts = UiFonts::default(); | ||
| fonts.update(&egui::Context::default(), "he-IL", false); | ||
| assert!(fonts.covers_hebrew(), "missing Hebrew system UI fallback for he-IL"); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod locale_tests { | ||
| use super::matches_language; | ||
|
|
||
| #[test] | ||
| fn fallback_accepts_os_locale_variants_without_matching_other_languages() { | ||
| for locale in ["he", "he-IL", "he_IL.UTF-8", "HE-il"] { | ||
| assert!(matches_language(locale, "he"), "{locale}"); | ||
| } | ||
| assert!(matches_language("ar-EG", "ar")); | ||
| assert!(!matches_language("en-US", "he")); | ||
| assert!(!matches_language("hi", "he")); | ||
| assert!(!matches_language("ar-EG", "he")); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(test, target_os = "macos"))] | ||
| mod language_switch_tests { | ||
| use super::UiFonts; | ||
| use std::sync::Arc; | ||
|
|
||
| #[test] | ||
| fn switching_languages_preserves_both_script_faces() { | ||
| let ctx = egui::Context::default(); | ||
| let mut fonts = UiFonts::default(); | ||
|
|
||
| // English alone should not force loading either system script. | ||
| fonts.update(&ctx, "en-US", false); | ||
| assert!(!fonts.covers_arabic()); | ||
| assert!(!fonts.covers_hebrew()); | ||
|
|
||
| fonts.update(&ctx, "ar-EG", false); | ||
| assert!(fonts.covers_arabic()); | ||
| assert!(!fonts.covers_hebrew()); | ||
| let first_arabic = fonts.arabic.as_ref().unwrap().clone(); | ||
|
|
||
| fonts.update(&ctx, "he-IL", false); | ||
| assert!(fonts.covers_hebrew()); | ||
| assert!(fonts.covers_arabic()); | ||
| let first_hebrew = fonts.hebrew.as_ref().unwrap().clone(); | ||
| assert!(Arc::ptr_eq(&first_arabic, fonts.arabic.as_ref().unwrap())); | ||
|
|
||
| // A non-RTL language must not discard previously loaded fallback faces. | ||
| fonts.update(&ctx, "en-US", false); | ||
| assert!(Arc::ptr_eq(&first_arabic, fonts.arabic.as_ref().unwrap())); | ||
| assert!(Arc::ptr_eq(&first_hebrew, fonts.hebrew.as_ref().unwrap())); | ||
|
|
||
| // Before choosing a script, the language picker must show both names. | ||
| let mut picker_fonts = UiFonts::default(); | ||
| picker_fonts.update(&egui::Context::default(), "en-US", true); | ||
| assert!(picker_fonts.covers_hebrew()); | ||
| assert!(picker_fonts.covers_arabic()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.