diff --git a/crates/text/src/fonts.rs b/crates/text/src/fonts.rs index 0db728017e..0323533074 100644 --- a/crates/text/src/fonts.rs +++ b/crates/text/src/fonts.rs @@ -499,7 +499,7 @@ pub fn guess_from_postscript(ps: &str) -> ResolvedFont { } #[cfg(not(target_arch = "wasm32"))] -fn collect_font_files(dir: &std::path::Path, depth: u32, out: &mut Vec) { +pub(crate) fn collect_font_files(dir: &std::path::Path, depth: u32, out: &mut Vec) { let Ok(rd) = std::fs::read_dir(dir) else { return; }; @@ -516,7 +516,7 @@ fn collect_font_files(dir: &std::path::Path, depth: u32, out: &mut Vec Vec { +pub(crate) fn system_font_dirs() -> Vec { use std::path::PathBuf; let home = std::env::var_os("HOME").map(PathBuf::from); let mut v = Vec::new(); diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs index a63b3d9c06..173b1c84c1 100644 --- a/crates/text/src/lib.rs +++ b/crates/text/src/lib.rs @@ -26,6 +26,7 @@ pub mod psd; pub mod psd_styles; pub mod raster; pub mod render; +pub mod scripts; pub mod served; pub mod spell; pub mod warp; diff --git a/crates/text/src/scripts.rs b/crates/text/src/scripts.rs new file mode 100644 index 0000000000..b6e0aae74f --- /dev/null +++ b/crates/text/src/scripts.rs @@ -0,0 +1,366 @@ +//! UI fallback fonts for the non-CJK scripts the bundled Inter / JetBrains Mono don't draw +//! (Thai, Arabic, Hebrew, the Indic scripts, ...): which characters belong to which script, and +//! which installed font files to try for it. +//! +//! Candidates come from the file names in the platform font directories (the same directories +//! [`crate::fonts::FontDb`] scans), listed once per process and only when first asked for, so +//! nothing is read at startup and no path is hard-coded per distribution. Noto fonts are matched +//! by their family-derived file names (`NotoSansThai-Regular.ttf`, `NotoSansThai[wght].ttf`), +//! platform fonts by their known file names (Leelawadee UI, Nirmala UI, Segoe UI, Geeza Pro, +//! Kohinoor, DejaVu Sans, Lohit, ...). A file name is only a hint: callers check that a face +//! really maps the character ([`covering_face`]) before using it. CJK has its own, +//! locale-ordered lists ([`crate::cjk`]). + +/// A script that needs a UI fallback font. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Script { + Thai, + Lao, + Khmer, + Myanmar, + Tibetan, + Arabic, + Hebrew, + Armenian, + Georgian, + Ethiopic, + Devanagari, + Bengali, + Gurmukhi, + Gujarati, + Oriya, + Tamil, + Telugu, + Kannada, + Malayalam, + Sinhala, +} + +impl Script { + pub const ALL: [Script; 20] = [ + Script::Thai, + Script::Lao, + Script::Khmer, + Script::Myanmar, + Script::Tibetan, + Script::Arabic, + Script::Hebrew, + Script::Armenian, + Script::Georgian, + Script::Ethiopic, + Script::Devanagari, + Script::Bengali, + Script::Gurmukhi, + Script::Gujarati, + Script::Oriya, + Script::Tamil, + Script::Telugu, + Script::Kannada, + Script::Malayalam, + Script::Sinhala, + ]; + + /// Font file names for this script, most preferred first: lowercase, without extension, + /// spaces, underscores or style suffix (`NotoSansThai-Bold.ttf` and `NotoSansThai[wght].ttf` + /// are both `notosansthai`). Noto first (the usual Linux font), then Windows, macOS and + /// other Linux families. + fn file_names(self) -> &'static [&'static str] { + use Script::*; + match self { + Thai => &[ + "notosansthai", + "notosansthailooped", + "notosansthaiui", + "notoserifthai", + "leelawui", + "leelawad", + "tahoma", + "thonburi", + "sukhumvitset", + "ayuthaya", + "loma", + "garuda", + "waree", + "kinnari", + "norasi", + ], + Lao => &["notosanslao", "notosanslaoui", "notoseriflao", "leelawui", "laosangammn", "laomn", "phetsarathot", "saysetthaot"], + Khmer => &["notosanskhmer", "notosanskhmerui", "notoserifkhmer", "leelawui", "daunpenh", "khmersangammn", "khmermn", "khmeros", "khmerossys"], + Myanmar => &["notosansmyanmar", "notosansmyanmarui", "notoserifmyanmar", "mmrtext", "myanmarsangammn", "myanmarmn", "padauk"], + Tibetan => &["notoseriftibetan", "himalaya", "kailasa", "jomolhari", "tibetanmachineuni"], + Arabic => &[ + "notosansarabic", + "notosansarabicui", + "notonaskharabic", + "notonaskharabicui", + "notokufiarabic", + "segoeui", + "tahoma", + "geezapro", + "sfarabic", + "dejavusans", + "kacstone", + "kacstbook", + "amiri", + "arial", + ], + Hebrew => &["notosanshebrew", "notoserifhebrew", "segoeui", "arialhb", "sfhebrew", "dejavusans", "arial", "freesans", "culmus"], + Armenian => &["notosansarmenian", "notoserifarmenian", "segoeui", "sylfaen", "mshtakan", "dejavusans", "freesans"], + Georgian => &["notosansgeorgian", "notoserifgeorgian", "segoeui", "sylfaen", "dejavusans", "freesans"], + Ethiopic => &["notosansethiopic", "notoserifethiopic", "ebrima", "kefa", "abyssinicasil"], + Devanagari => &[ + "notosansdevanagari", + "notosansdevanagariui", + "notoserifdevanagari", + "nirmala", + "mangal", + "kohinoor", + "kohinoordevanagari", + "itfdevanagari", + "devanagarimt", + "devanagarisangammn", + "lohitdevanagari", + "lohit-devanagari", + "gargi", + "freesans", + ], + Bengali => &[ + "notosansbengali", + "notosansbengaliui", + "notoserifbengali", + "nirmala", + "vrinda", + "kohinoorbangla", + "banglasangammn", + "banglamn", + "lohitbengali", + "lohit-bengali", + "lohit-assamese", + ], + Gurmukhi => &[ + "notosansgurmukhi", + "notosansgurmukhiui", + "notoserifgurmukhi", + "nirmala", + "raavi", + "gurmukhisangammn", + "gurmukhimn", + "lohit-gurmukhi", + "lohit-punjabi", + ], + Gujarati => &[ + "notosansgujarati", + "notosansgujaratiui", + "notoserifgujarati", + "nirmala", + "shruti", + "kohinoorgujarati", + "gujaratisangammn", + "gujaratimt", + "lohit-gujarati", + ], + Oriya => &["notosansoriya", "notosansoriyaui", "notoseriforiya", "nirmala", "kalinga", "oriyasangammn", "oriyamn", "lohit-odia", "lohit-oriya"], + Tamil => &["notosanstamil", "notosanstamilui", "notoseriftamil", "nirmala", "latha", "tamilsangammn", "tamilmn", "lohit-tamil"], + Telugu => { + &["notosanstelugu", "notosansteluguui", "notoseriftelugu", "nirmala", "gautami", "kohinoortelugu", "telugusangammn", "telugumn", "lohit-telugu"] + } + Kannada => &["notosanskannada", "notosanskannadaui", "notoserifkannada", "nirmala", "tunga", "kannadasangammn", "kannadamn", "lohit-kannada"], + Malayalam => { + &["notosansmalayalam", "notosansmalayalamui", "notoserifmalayalam", "nirmala", "kartika", "malayalamsangammn", "malayalammn", "lohit-malayalam"] + } + Sinhala => &["notosanssinhala", "notosanssinhalaui", "notoserifsinhala", "nirmala", "iskpota", "sinhalasangammn", "sinhalamn", "lklug"], + } + } +} + +/// The script of a character that needs a non-CJK fallback font, or `None`. +pub fn classify(c: char) -> Option