PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Type: fall back to installed Khmer, Lao and Burmese fonts (#218) (#3259)
RaccoonPie-3141051265f75b9
diff --git a/crates/text/src/fonts.rs b/crates/text/src/fonts.rsindex a0cd5b867..59ba1db70 100644--- a/crates/text/src/fonts.rs+++ b/crates/text/src/fonts.rs@@ -55,14 +55,25 @@ fn inflate_font(deflated: &[u8]) -> Vec<u8> {}}-/// Families tried (if installed) after the requested one, for missing glyphs. The Thai and CJK-/// families ([`THAI_FAMILIES`], [`crate::cjk::families`]) go between these two lists, in the UI locale's script order.+/// Families tried (if installed) after the requested one, for missing glyphs. The Southeast Asian+/// and CJK families ([`THAI_FAMILIES`], [`KHMER_FAMILIES`], [`LAO_FAMILIES`], [`MYANMAR_FAMILIES`],+/// [`crate::cjk::families`]) go between these two lists, in the UI locale's script order.const FALLBACK_CANDIDATES: &[&str] = &["Noto Sans", "Segoe UI", "DejaVu Sans", "Geeza Pro", "Arial Hebrew", "Noto Sans Arabic", "Noto Sans Hebrew"];/// Thai-capable families: Windows (Leelawadee UI since Windows 8; Tahoma and Leelawadee on/// older systems), macOS (Thonburi, Sukhumvit Set) and Linux (Noto). Text in a font without Thai/// glyphs falls back to these, as with Photoshop's missing glyph protection (#1909).pub const THAI_FAMILIES: &[&str] =&["Leelawadee UI", "Leelawadee", "Tahoma", "Thonburi", "Sukhumvit Set", "Noto Sans Thai", "Noto Sans Thai Looped", "Noto Sans Thai UI"];+/// Khmer-capable families: macOS (Khmer Sangam MN, Khmer MN), Windows (`Khmer UI` on 8; from 8.1+/// `Leelawadee UI` in [`THAI_FAMILIES`] covers Khmer too) and Linux (Noto). Khmer in a font without+/// Khmer glyphs had no candidate to fall back to and drew .notdef boxes (#218).+pub const KHMER_FAMILIES: &[&str] = &["Khmer Sangam MN", "Khmer MN", "Khmer UI", "Noto Sans Khmer"];+/// Lao-capable families: macOS (Lao Sangam MN, Lao MN), Windows (`Lao UI` on 8; `Leelawadee UI` in+/// [`THAI_FAMILIES`] from 8.1) and Linux (Noto, static and looped forms).+pub const LAO_FAMILIES: &[&str] = &["Lao Sangam MN", "Lao MN", "Lao UI", "Noto Sans Lao", "Noto Sans Lao Looped"];+/// Myanmar/Burmese-capable families: macOS (Myanmar Sangam MN, Myanmar MN), Windows (Myanmar Text)+/// and Linux (Noto).+pub const MYANMAR_FAMILIES: &[&str] = &["Myanmar Sangam MN", "Myanmar MN", "Myanmar Text", "Noto Sans Myanmar"];/// Broad-coverage and emoji fonts, tried after the CJK script fonts so shared Han characters get/// the locale's forms instead of Arial Unicode's.const FALLBACK_LAST: &[&str] = &["Arial Unicode MS", "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji"];@@ -71,6 +82,9 @@ const FALLBACK_LAST: &[&str] = &["Arial Unicode MS", "Apple Color Emoji", "Segoepub fn fallback_candidates(order: &[crate::cjk::CjkScript; 4]) -> Vec<&'static str> {let mut v = FALLBACK_CANDIDATES.to_vec();v.extend_from_slice(THAI_FAMILIES);+ v.extend_from_slice(KHMER_FAMILIES);+ v.extend_from_slice(LAO_FAMILIES);+ v.extend_from_slice(MYANMAR_FAMILIES);for s in order {// craft-fonts' Japanese fonts (if built in) go ahead of the installed Japanese fonts, in// the Japanese slot of the locale order, so shared Han keeps the locale's forms.diff --git a/crates/text/src/served.rs b/crates/text/src/served.rsindex b4a1f83a7..73c5d7f30 100644--- a/crates/text/src/served.rs+++ b/crates/text/src/served.rs@@ -11,7 +11,7 @@//!//! Script fallback: the manifest's `scripts` field says which scripts a font is for. The first//! served family listed for a script the bundled fonts don't cover (Arabic, Hebrew, Devanagari,-//! Thai, Japanese, Chinese, Korean) is that script's fallback: text in that script asks for it+//! Thai, Khmer, Lao, Burmese, Japanese, Chinese, Korean) is that script's fallback: text in that script asks for it//! ([`request_for_text`], from layout), the font database puts it in the fallback stack once it//! arrives, and the web shell asks up front for the scripts of the browser's languages//! ([`request_for_languages`]). The manifest's SHA-256 becomes the fetch's Subresource Integrity@@ -63,7 +63,7 @@ fn is_relative_path(p: &str) -> bool {/// The scripts a served font can be the fallback for: those the bundled fonts (Inter, which covers/// Latin, Greek and Cyrillic) lack, as ISO 15924 codes.-const FALLBACK_SCRIPTS: &[&str] = &["Arab", "Hebr", "Deva", "Thai", "Jpan", "Hans", "Hant", "Kore"];+const FALLBACK_SCRIPTS: &[&str] = &["Arab", "Hebr", "Deva", "Thai", "Khmr", "Laoo", "Mymr", "Jpan", "Hans", "Hant", "Kore"];/// The script fallbacks in `fonts`: for each script in [`FALLBACK_SCRIPTS`], the first family in/// manifest order that lists it, as `(script, family)` in order of first appearance.@@ -96,6 +96,9 @@ pub fn script_of(c: char, order: &[crate::cjk::CjkScript; 4]) -> Option<&'static0x0600..=0x06FF | 0x0750..=0x077F | 0x0870..=0x08FF | 0xFB50..=0xFDFF | 0xFE70..=0xFEFF | 0x1EE00..=0x1EEFF => "Arab",0x0900..=0x097F | 0xA8E0..=0xA8FF => "Deva",0x0E00..=0x0E7F => "Thai",+ 0x1780..=0x17FF | 0x19E0..=0x19FF => "Khmr",+ 0x0E80..=0x0EFF => "Laoo",+ 0x1000..=0x109F | 0xA9E0..=0xA9FF | 0xAA60..=0xAA7F => "Mymr",_ => return None,})}@@ -111,6 +114,9 @@ pub fn language_script(tag: &str) -> Option<&'static str> {"he" | "iw" | "yi" => "Hebr","hi" | "mr" | "ne" | "sa" => "Deva","th" => "Thai",+ "km" => "Khmr",+ "lo" => "Laoo",+ "my" => "Mymr","ja" => "Jpan","ko" => "Kore","zh" if rest.iter().any(|r| matches!(*r, "hant" | "tw" | "hk" | "mo")) => "Hant",@@ -400,6 +406,11 @@ mod tests {('ש', Some("Hebr")),('क', Some("Deva")),('ก', Some("Thai")),+ ('ខ', Some("Khmr")),+ ('ៗ', Some("Khmr")),+ ('ລ', Some("Laoo")),+ ('မ', Some("Mymr")),+ ('ၡ', Some("Mymr")),('あ', Some("Jpan")),('한', Some("Kore")),] {@@ -419,6 +430,9 @@ mod tests {("ar-EG", Some("Arab")),("fa-IR", Some("Arab")),("he", Some("Hebr")),+ ("km-KH", Some("Khmr")),+ ("lo", Some("Laoo")),+ ("my-MM", Some("Mymr")),("ja-JP", Some("Jpan")),("zh-CN", Some("Hans")),("zh-TW", Some("Hant")),diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rsindex ea63b8551..986211bcb 100644--- a/crates/text/src/tests.rs+++ b/crates/text/src/tests.rs@@ -1407,6 +1407,61 @@ fn thai_fallback_candidates_cover_every_platform() {}}+/// The Southeast Asian scripts with their hard cases: Khmer coeng subscripts, the ah-sat mark and+/// numerals; Lao vowel and tone signs; Myanmar medials, stacks and dependent vowels (#218).+const SOUTHEAST_ASIAN: &[(&str, &str, &[&str])] = &[+ ("Khmer", "ភាសាខ្មែរ កម្ពុជា សួស្ដី រដ្ឋធម្ណុសាស្តៗ ១២៣", fonts::KHMER_FAMILIES),+ ("Lao", "ພາສາລາວ ສະບາຍດີ ຄ ໂລກ ຂອບໃຈ", fonts::LAO_FAMILIES),+ ("Myanmar", "မြန်မာဘာသာ မင်္ဂလာပါ ကျေးဇူးတင်ပါတယ်", fonts::MYANMAR_FAMILIES),+];++#[test]+fn southeast_asian_scripts_fall_back_to_installed_fonts() {+ // Detection and selection are tested apart, so a failure names its layer.+ for (name, sample, families) in SOUTHEAST_ASIAN {+ let mut e = TextEngine::with_system_fonts();+ let Some(hit) = families.iter().find(|f| e.fonts.has_family(f)) else {+ eprintln!("skipped: no {name}-capable font installed");+ continue;+ };+ // Layer 1, detection + loading + shaping: the installed family, asked for by name.+ let direct = TextLayer { text: (*sample).into(), font_family: hit.to_string(), size_pt: 24.0, ..Default::default() };+ let l = e.layout(&direct, 72.0);+ assert!(!l.glyphs.is_empty() && l.glyphs.iter().all(|g| g.id != 0), "{hit} is installed but shapes .notdef for {name}");+ // Layer 2, selection: the same text in a Latin-only font falls back instead of drawing+ // tofu, and the Latin around it keeps the chosen face.+ let mixed = format!("Latin {sample}");+ let l = e.layout(&point(&mixed, 24.0), 72.0);+ assert!(l.glyphs.iter().all(|g| g.id != 0), "{name} in a Latin font drawn with .notdef although {hit} is installed");+ let latin = l.glyphs.first().map(|g| g.face);+ assert!(l.glyphs.iter().any(|g| Some(g.face) != latin), "Latin and {name} drawn with the same face");+ }+}++#[test]+fn southeast_asian_fallback_candidates_cover_every_platform() {+ // Logic-level half of #218 (runs without these fonts): each script has a family for Windows,+ // macOS and Linux in the fallback candidates, ahead of the broad last-resort fonts.+ for order in [crate::cjk::script_order(None), crate::cjk::script_order(Some("ja"))] {+ let fb = fonts::fallback_candidates(&order);+ let last = fb.iter().position(|f| *f == "Arial Unicode MS").unwrap();+ for fam in [+ "Khmer UI",+ "Khmer Sangam MN",+ "Noto Sans Khmer",+ "Lao UI",+ "Lao Sangam MN",+ "Noto Sans Lao",+ "Myanmar Text",+ "Myanmar Sangam MN",+ "Noto Sans Myanmar",+ ] {+ let i = fb.iter().position(|f| *f == fam).unwrap_or_else(|| panic!("{fam} missing from {fb:?}"));+ assert!(i < last, "{fam} after the last-resort fonts");+ }+ }+}+#[test]fn a_served_script_fallback_joins_the_fallback_stack_once_it_arrives() {let Some(cairo) = crate::CRAFT_FONTS.iter().find(|f| f.family == "Cairo") else {