Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Text: fall back to Thai-capable fonts for missing glyphs (#1909) (#2030)

David Mann51598bb061ff
diff --git a/crates/text/src/fonts.rs b/crates/text/src/fonts.rs
index c11fe8026..0b9eff484 100644
--- a/crates/text/src/fonts.rs
+++ b/crates/text/src/fonts.rs
@@ -32,9 +32,14 @@ pub static BUNDLED: &[(&str, &[u8])] = &[
("JetBrainsMono-Regular.ttf", JETBRAINS_MONO_REGULAR),
];
-/// Families tried (if installed) after the requested one, for missing glyphs. The CJK 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 Thai and CJK
+/// families ([`THAI_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"];
/// 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"];
@@ -42,6 +47,7 @@ const FALLBACK_LAST: &[&str] = &["Arial Unicode MS", "Apple Color Emoji", "Segoe
/// Every fallback family candidate, CJK ordered for `order` (see [`crate::cjk::script_order`]).
pub fn fallback_candidates(order: &[crate::cjk::CjkScript; 4]) -> Vec<&'static str> {
let mut v = FALLBACK_CANDIDATES.to_vec();
+ v.extend_from_slice(THAI_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/tests.rs b/crates/text/src/tests.rs
index bbb21dc08..24a5bcbaf 100644
--- a/crates/text/src/tests.rs
+++ b/crates/text/src/tests.rs
@@ -1140,3 +1140,39 @@ fn word_and_line_navigation() {
assert_eq!(jumped, char_index(text, l.lines[1].range.end), "own column");
assert!(jumped > kept);
}
+
+/// Thai text sample with above/below marks (sara i, mai ek, mai tho, mai han-akat, sara u).
+const THAI_SAMPLE: &str = "ภาษาไทย สวัสดีครับ ผู้ที่น้ำ";
+
+#[test]
+fn thai_in_latin_font_falls_back_to_installed_thai_font() {
+ // #1909: Thai typed in a Latin-only font (a newly chosen font has no PostScript name to find
+ // the original Thai face) must fall back to an installed Thai-capable font, not .notdef.
+ let mut e = TextEngine::with_system_fonts();
+ let Some(thai) = fonts::THAI_FAMILIES.iter().find(|f| e.fonts.has_family(f)) else {
+ eprintln!("skipped: no Thai-capable font installed");
+ return;
+ };
+ let l = e.layout(&point(THAI_SAMPLE, 24.0), 72.0);
+ assert!(!l.glyphs.is_empty());
+ assert!(l.glyphs.iter().all(|g| g.id != 0), "Thai drawn with .notdef although {thai} is installed");
+ // Latin next to Thai keeps the chosen font; only the Thai clusters fall back.
+ let l = e.layout(&point("Thai ไทย", 24.0), 72.0);
+ assert!(l.glyphs.iter().all(|g| g.id != 0));
+ let (first, last) = (l.glyphs.first().map(|g| g.face), l.glyphs.last().map(|g| g.face));
+ assert_ne!(first, last, "Latin and Thai drawn with the same face");
+}
+
+#[test]
+fn thai_fallback_candidates_cover_every_platform() {
+ // Logic-level half of #1909 (runs without Thai fonts): Windows, macOS and Linux each have a
+ // Thai-capable family 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 ["Leelawadee UI", "Tahoma", "Thonburi", "Noto Sans Thai"] {
+ 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");
+ }
+ }
+}