Merge pull request #4908 from ruchirguitar/gc-4131-indic-fonts

Indian-script fallback fonts: Gujarati and Hindi text draws instead of boxes
This commit is contained in:
BFlat authored and GitHub committed 2026-11-30 01:21:46 +07:00
commit 5f22c7c01c
4 files changed
+232 -10

No files matched your search

+2 -2
View File
@@ -26,8 +26,8 @@ mod font_setup;
pub use font_fallback::symbol_face_installed;
#[cfg(test)]
use font_fallback::{
PREFERRED_ARABIC, arabic, cjk_han_japanese, cjk_han_simplified, cjk_hangul, fallback_faces, hebrew, lao, load_faces, preferred_file,
symbols_more, thai,
Indic, PREFERRED_ARABIC, arabic, cjk_han_japanese, cjk_han_simplified, cjk_hangul, fallback_faces, hebrew, indic, lao, load_faces,
preferred_file, symbols_more, thai,
};
#[cfg(all(test, unix, not(target_os = "macos")))]
use font_fallback::{covers_arabic, fontconfig_arabic, parse_fc_match};
+96
View File
@@ -127,6 +127,15 @@ pub(super) fn fallback_faces(han: HanOrder) -> Vec<(&'static str, Vec<(String, u
("sys-lao", lao()),
("sys-hebrew", hebrew()),
("sys-arabic", arabic()),
("sys-devanagari", indic(Indic::Devanagari)),
("sys-bengali", indic(Indic::Bengali)),
("sys-gurmukhi", indic(Indic::Gurmukhi)),
("sys-gujarati", indic(Indic::Gujarati)),
("sys-odia", indic(Indic::Odia)),
("sys-tamil", indic(Indic::Tamil)),
("sys-telugu", indic(Indic::Telugu)),
("sys-kannada", indic(Indic::Kannada)),
("sys-malayalam", indic(Indic::Malayalam)),
("sys-symbols", symbols()),
("sys-symbols-2", symbols_more()),
]
@@ -330,6 +339,93 @@ pub(super) fn arabic() -> Vec<(String, u32)> {
v
}
/// The Indian scripts with a fallback face. epaint shapes every run with harfrust, so a face with
/// the script's OpenType tables is all conjuncts and reordered vowel signs need.
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Indic {
Devanagari,
Bengali,
Gurmukhi,
Gujarati,
Odia,
Tamil,
Telugu,
Kannada,
Malayalam,
}
/// Indian-script faces. Windows' Nirmala UI covers all nine scripts (loaded once, see
/// [`load_fallback`]), with the older per-script faces behind it; macOS ships Kohinoor, Noto and
/// Sangam MN faces; Linux and the BSDs usually have Noto Sans or Lohit.
/// A script's faces: Windows file, macOS files with the Regular face's index, Noto name, Lohit
/// directory and file.
#[cfg(not(target_arch = "wasm32"))]
type IndicFaces = (&'static str, &'static [(&'static str, u32)], &'static str, (&'static str, &'static str));
#[cfg(not(target_arch = "wasm32"))]
pub(super) fn indic(script: Indic) -> Vec<(String, u32)> {
let mac_sys = "/System/Library/Fonts";
let mac_sup = "/System/Library/Fonts/Supplemental";
let win = windows_fonts();
// (Windows face, macOS faces with the Regular face's index, Noto name, Lohit directory and file)
let (win_face, mac, noto, lohit): IndicFaces = match script {
Indic::Devanagari => (
"mangal.ttf",
&[("{sys}/Kohinoor.ttc", 0), ("{sup}/Devanagari Sangam MN.ttc", 0), ("{sup}/ITFDevanagari.ttc", 0)],
"Devanagari",
("lohit-devanagari", "Lohit-Devanagari"),
),
Indic::Bengali => {
("vrinda.ttf", &[("{sys}/KohinoorBangla.ttc", 0), ("{sup}/Bangla Sangam MN.ttc", 0)], "Bengali", ("lohit-bengali", "Lohit-Bengali"))
}
Indic::Gurmukhi => {
("raavi.ttf", &[("{sup}/Gurmukhi Sangam MN.ttc", 0), ("{sup}/Gurmukhi MN.ttc", 0)], "Gurmukhi", ("lohit-punjabi", "Lohit-Gurmukhi"))
}
Indic::Gujarati => (
"shruti.ttf",
&[("{sys}/KohinoorGujarati.ttc", 3), ("{sup}/Gujarati Sangam MN.ttc", 0)],
"Gujarati",
("lohit-gujarati", "Lohit-Gujarati"),
),
Indic::Odia => ("kalinga.ttf", &[("{sys}/NotoSansOriya.ttc", 0), ("{sup}/Oriya Sangam MN.ttc", 0)], "Oriya", ("lohit-oriya", "Lohit-Odia")),
Indic::Tamil => ("latha.ttf", &[("{sup}/Tamil Sangam MN.ttc", 0), ("{sup}/Tamil MN.ttc", 0)], "Tamil", ("lohit-tamil", "Lohit-Tamil")),
Indic::Telugu => {
("gautami.ttf", &[("{sys}/KohinoorTelugu.ttc", 0), ("{sup}/Telugu Sangam MN.ttc", 0)], "Telugu", ("lohit-telugu", "Lohit-Telugu"))
}
Indic::Kannada => {
("tunga.ttf", &[("{sys}/NotoSansKannada.ttc", 3), ("{sup}/Kannada Sangam MN.ttc", 0)], "Kannada", ("lohit-kannada", "Lohit-Kannada"))
}
Indic::Malayalam => (
"kartika.ttf",
&[("{sup}/Malayalam Sangam MN.ttc", 0), ("{sup}/Malayalam MN.ttc", 0)],
"Malayalam",
("lohit-malayalam", "Lohit-Malayalam"),
),
};
let mut v: Vec<(String, u32)> = vec![(format!("{win}\\Nirmala.ttc"), 0), (format!("{win}\\Nirmala.ttf"), 0), (format!("{win}\\{win_face}"), 0)];
v.extend(mac.iter().map(|(m, index)| (m.replace("{sys}", mac_sys).replace("{sup}", mac_sup), *index)));
let mut linux: Vec<String> = Vec::new();
for d in ["truetype/noto", "opentype/noto", "google-noto", "noto", "TTF"] {
linux.push(format!("{d}/NotoSans{noto}-Regular.ttf"));
}
// Fedora ships the variable Noto Sans faces; their default instance is Regular.
linux.push(format!("google-noto-vf/NotoSans{noto}[wght].ttf"));
let (dir, file) = lohit;
for d in ["truetype/", ""] {
linux.push(format!("{d}{dir}/{file}.ttf"));
}
// The system fonts, then the same layouts under a Flatpak sandbox's read-only host mounts.
for root in ["/usr/share/fonts", "/run/host/fonts"] {
v.extend(linux.iter().map(|f| (format!("{root}/{f}"), 0)));
}
for root in ["/usr/local/share/fonts", "/run/host/local-fonts"] {
v.push((format!("{root}/noto/NotoSans{noto}-Regular.ttf"), 0));
v.push((format!("{root}/{dir}/{file}.ttf"), 0));
}
v
}
/// Symbol faces behind the UI font. Interface fonts such as Noto Sans and Cantarell (and egui's
/// built-in Ubuntu) lack arrows, check marks, geometric shapes and keyboard symbols (▾ ✓ ✕ ▲ ↳ ⇧),
/// which then render as empty boxes (#197, #54). DejaVu Sans covers most of them; the paths cover
+129 -3
View File
@@ -122,11 +122,49 @@ fn language_reorders_only_the_han_faces() {
let keys = |han| fallback_faces(han).into_iter().map(|(k, _)| k).collect::<Vec<_>>();
assert_eq!(
keys(HanOrder::Japanese),
["sys-cjk-han-ja", "sys-cjk-han-zh", "sys-cjk-hangul", "sys-thai", "sys-lao", "sys-hebrew", "sys-arabic", "sys-symbols", "sys-symbols-2"]
[
"sys-cjk-han-ja",
"sys-cjk-han-zh",
"sys-cjk-hangul",
"sys-thai",
"sys-lao",
"sys-hebrew",
"sys-arabic",
"sys-devanagari",
"sys-bengali",
"sys-gurmukhi",
"sys-gujarati",
"sys-odia",
"sys-tamil",
"sys-telugu",
"sys-kannada",
"sys-malayalam",
"sys-symbols",
"sys-symbols-2"
]
);
assert_eq!(
keys(HanOrder::Chinese),
["sys-cjk-han-zh", "sys-cjk-han-ja", "sys-cjk-hangul", "sys-thai", "sys-lao", "sys-hebrew", "sys-arabic", "sys-symbols", "sys-symbols-2"]
[
"sys-cjk-han-zh",
"sys-cjk-han-ja",
"sys-cjk-hangul",
"sys-thai",
"sys-lao",
"sys-hebrew",
"sys-arabic",
"sys-devanagari",
"sys-bengali",
"sys-gurmukhi",
"sys-gujarati",
"sys-odia",
"sys-tamil",
"sys-telugu",
"sys-kannada",
"sys-malayalam",
"sys-symbols",
"sys-symbols-2"
]
);
assert_eq!(HanOrder::of(Language::Ja), HanOrder::Japanese);
for l in [Language::En, Language::Zh, Language::Ko, Language::Ru, Language::PtBr, Language::De, Language::Cs, Language::Fr, Language::Tr] {
@@ -185,7 +223,7 @@ fn fallback_covers_its_script() {
// face or the optional craft-fonts input provides one; the printed skip is a recorded
// gate, and a present face that is not wired in fails.
type Case = (&'static str, Vec<(String, u32)>, &'static str);
let cases: [Case; 11] = [
let cases: [Case; 20] = [
("sys-cjk-han-ja", cjk_han_japanese(), "カテゴリ"),
// Chinese uses a system Han face or the native-only permitted LXGW craft face; the
// barred Noto CJK collection never supplies it.
@@ -195,6 +233,16 @@ fn fallback_covers_its_script() {
("sys-lao", lao(), "ພາສາລາວ"),
("sys-hebrew", hebrew(), "עברית"),
("sys-arabic", arabic(), "العربية١٢٣"),
// Indian scripts, each with a conjunct or a vowel sign the shaper has to place.
("sys-devanagari", indic(Indic::Devanagari), "हिन्दी क्षेत्र"),
("sys-bengali", indic(Indic::Bengali), "বাংলা ক্ষেত্র"),
("sys-gurmukhi", indic(Indic::Gurmukhi), "ਪੰਜਾਬੀ"),
("sys-gujarati", indic(Indic::Gujarati), "ગુજરાતી"),
("sys-odia", indic(Indic::Odia), "ଓଡ଼ିଆ"),
("sys-tamil", indic(Indic::Tamil), "தமிழ் கொண்டு"),
("sys-telugu", indic(Indic::Telugu), "తెలుగు"),
("sys-kannada", indic(Indic::Kannada), "ಕನ್ನಡ"),
("sys-malayalam", indic(Indic::Malayalam), "മലയാളം"),
("craft-noto-sans-arabic", Vec::new(), "مرحبا"),
("craft-noto-sans-georgian", Vec::new(), "ქართული"),
("ui", candidates(UI), "Русский"),
@@ -489,3 +537,81 @@ fn fedora_package_folders_are_searched_after_the_other_linux_layouts() {
let noto = mono.iter().position(|p| p == "/usr/share/fonts/google-noto-vf/NotoSansMono[wght].ttf");
assert!(noto.is_some() && noto.unwrap_or(0) + 1 == mono.len(), "Fedora's default Noto Sans Mono must be the last resort: {mono:?}");
}
#[test]
fn indic_text_is_shaped_not_drawn_letter_by_letter() {
// epaint shapes each run with harfrust, so with a face in the chain a conjunct is one glyph
// and a split vowel sign is drawn on both sides of its consonant. Drawn per character, क्ष
// would show three glyphs (क, a visible virama, ष) and கொ two.
let ctx = egui::Context::default();
ctx.set_fonts(font_definitions());
let mut out = ctx.run_ui(egui::RawInput::default(), |_ui| {});
out.textures_delta.clear(); // nothing consumes the atlas in a headless test
let drawn = |text: &str| {
let galley = ctx.fonts_mut(|f| f.layout_no_wrap(text.to_owned(), FontId::proportional(14.0), Color32::BLACK));
galley.rows.iter().flat_map(|r| r.glyphs.iter()).filter(|g| g.uv_rect.size.x > 0.0).count()
};
let has = |script| indic(script).iter().any(|(p, _)| std::path::Path::new(p).exists());
if has(Indic::Devanagari) {
assert_eq!(drawn("क्ष"), 1, "क + virama + ष forms the conjunct क्ष");
}
if has(Indic::Tamil) {
// Shaped, the split vowel sign is either drawn on both sides of க (3 glyphs) or ligated with
// it (1, Nirmala UI); one glyph per code point (2) means no shaping.
assert_ne!(drawn("கொ"), 2, "the vowel sign ொ is shaped around க");
}
}
#[test]
fn indic_faces_are_regular_weight() {
// A family collection's face order is not alphabetical by weight (macOS's Kohinoor Gujarati
// starts with Bold, Noto Sans Kannada with Thin), so each candidate names its Regular face.
use skrifa::MetadataProvider;
let scripts = [
Indic::Devanagari,
Indic::Bengali,
Indic::Gurmukhi,
Indic::Gujarati,
Indic::Odia,
Indic::Tamil,
Indic::Telugu,
Indic::Kannada,
Indic::Malayalam,
];
for script in scripts {
for (path, index) in indic(script) {
let Ok(bytes) = std::fs::read(&path) else { continue };
let Ok(font) = skrifa::FontRef::from_index(&bytes, index) else { continue };
let weight = font.attributes().weight.value();
assert!((350.0..=500.0).contains(&weight), "{script:?}: {path} face {index} has weight {weight}");
}
}
}
#[test]
fn indic_faces_reach_fedora_and_flatpak_layouts() {
// Fedora installs only the variable Noto Sans faces; a Flatpak sees the host's fonts under
// /run/host. Missing either leaves the interface in tofu boxes there.
let cases = [
(Indic::Devanagari, "Devanagari"),
(Indic::Bengali, "Bengali"),
(Indic::Gurmukhi, "Gurmukhi"),
(Indic::Gujarati, "Gujarati"),
(Indic::Odia, "Oriya"),
(Indic::Tamil, "Tamil"),
(Indic::Telugu, "Telugu"),
(Indic::Kannada, "Kannada"),
(Indic::Malayalam, "Malayalam"),
];
for (script, noto) in cases {
let paths: Vec<String> = indic(script).into_iter().map(|(path, _)| path).collect();
for expected in [
format!("/usr/share/fonts/google-noto-vf/NotoSans{noto}[wght].ttf"),
format!("/run/host/fonts/google-noto-vf/NotoSans{noto}[wght].ttf"),
format!("/run/host/fonts/truetype/noto/NotoSans{noto}-Regular.ttf"),
format!("/run/host/local-fonts/noto/NotoSans{noto}-Regular.ttf"),
] {
assert!(paths.contains(&expected), "{script:?} doesn't probe {expected}");
}
}
}
+5 -5
View File
@@ -75,12 +75,12 @@ Cell text now uses shared Unicode bidi layout while retaining logical source ind
embedded faces tagged with CJK scripts share the Latin baseline in cells, editors and titles
(#1265, #2621); other script, emoji and symbol fallbacks retain their alignment.
Lao falls back to Lao Sangam MN / Lao MN (macOS),
Leelawadee UI / DokChampa (Windows) or Noto Sans Lao / Phetsarath OT (Linux). egui uses harfrust shaping and Unicode bidi layout; script-specific coverage
beyond Arabic still needs verification. **Sheet Right-to-Left**
Leelawadee UI / DokChampa (Windows) or Noto Sans Lao / Phetsarath OT (Linux). Arabic falls back to a system Arabic face, fontconfig's
Arabic choice or craft-fonts' Noto Sans Arabic; fontconfig's choice leads these fallbacks, but a UI font with Arabic of its
own (DejaVu Sans in the Flatpak) still draws interface Arabic (#229). There is no complex-text shaping: Arabic and
Devanagari don't join or reorder, and there is no bidirectional layout. **Sheet Right-to-Left**
own (DejaVu Sans in the Flatpak) still draws interface Arabic (#229). Indian scripts fall back to Nirmala UI and the
per-script faces (Windows), Kohinoor, Noto and Sangam MN (macOS) or Noto Sans and Lohit (Linux, BSD, Flatpak). egui uses
harfrust shaping and Unicode bidi layout, so conjuncts and reordered vowel signs render wherever a face is present
(`theme::tests::indic_text_is_shaped_not_drawn_letter_by_letter`). **Sheet Right-to-Left**
(`view.rightToLeft`, Page Layout › Sheet Options, XLSX `rightToLeft`, `.xls` WINDOW2 fRightToLeft) mirrors the grid (#146):
column A and the row headers on the right, the vertical scrollbar on the left; hit-testing,
arrow keys, editor, popups and floating objects follow; text follows its cell reading direction inside its box,
@@ -107,7 +107,7 @@ separators, number/date formats, machine translation with agent review, and scri
| English | en | ~2,200 / ~2,200 (100%, source) | yes | Latin: yes | yes | full | — |
| Simplified Chinese (Mandarin) | zh-Hans | 266 (~12%) | no | CJK system-font fallback (#38); IME unverified; PDF can't export | no | menus only | 8–13 h |
| Spanish | es | 0 (0%) | no | Latin: yes | no | none | 6–10 h |
| Hindi | hi | 0 (0%) | no | Devanagari: **no shaping** | no | none | 12–18 h |
| Hindi | hi | 0 (0%) | no | Devanagari: shaped, system faces | no | none | 12–18 h |
| Arabic | ar | 0 (0%) | no | shared shaping/bidi; no Arabic UI catalog or mirrored shell; PDF export: shaped and joined, follows cell direction, mirrors RTL sheets; Sheet RTL mirrors the grid (#146) | no | none | 25–35 h |
| French | fr | 0 (0%) | no | Latin: yes | no | none | 6–10 h |
| Portuguese | pt-BR | 943 (~43%) | dialogs and tooltips (dialogs take effect with #669) | Latin: yes | no | menus, File view, dialogs | 2–4 h |