//! Complex scripts that reorder and stack (#604): Myanmar, Khmer, Devanagari. Cluster and line //! breaking tests don't depend on installed fonts (a missing font draws .notdef boxes, but the //! clusters and breaks are the same); shaping tests use a system font when one is installed. use super::*; use wordcraft_doc::{Pos, StoryRef}; fn lay(doc: &Document) -> DocLayout { let mut c = LayoutCache::new(); layout(doc, &mut c, &LayoutOptions::default()) } /// Body paragraph `i`'s layout. fn para_layout(l: &DocLayout, i: u32) -> &ParaLayout { for p in &l.pages { for it in &p.items { if let Placed::Lines { story: StoryRef::Body, path, para, .. } = it && path.0 == vec![i] { return para; } } } panic!("paragraph {i} not laid out"); } /// "မြန်မာစာ": the syllables မြ | န် | မာ | စာ. const MYANMAR: &str = "\u{1019}\u{103C}\u{1014}\u{103A}\u{1019}\u{102C}\u{1005}\u{102C}"; #[test] fn myanmar_syllables_are_single_clusters() { let l = lay(&Document::from_text(MYANMAR)); let pl = para_layout(&l, 0); let starts: Vec = pl.clusters.iter().map(|c| c.start).collect(); // Grapheme clusters would also start at the aa signs (bytes 15 and 21), splitting မာ and စာ. assert_eq!(starts, vec![0, 6, 12, 18]); // The caret can't land inside a syllable: every caret offset is a cluster start or the end. let offs = pl.line_offsets(0); assert!(!offs.contains(&15) && !offs.contains(&21), "{offs:?}"); // Stacked consonants (virama) and Khmer subscripts (coeng) too. let l = lay(&Document::from_text("\u{1019}\u{1039}\u{1018}\u{102C} \u{1781}\u{17D2}\u{1798}\u{17C2}\u{179A}")); let pl = para_layout(&l, 0); let starts: Vec = pl.clusters.iter().map(|c| c.start).collect(); assert_eq!(starts, vec![0, 12, 13, 25]); } #[test] fn myanmar_without_spaces_wraps_between_syllables() { let text = MYANMAR.repeat(80); let l = lay(&Document::from_text(&text)); let pl = para_layout(&l, 0); assert!(pl.lines.len() > 1, "a long Myanmar paragraph wraps"); let breaks = wordcraft_doc::cluster::myanmar_breaks(&text); // A line may end before each syllable: before မာ and စာ, never before န် (a final). for c in &pl.clusters { assert_eq!(c.break_after, breaks.contains(&c.end), "break after {}", c.end); } assert!(pl.clusters.iter().any(|c| c.break_after && c.end == 12) && !pl.clusters.iter().any(|c| c.break_after && c.end == 6)); for line in pl.lines.iter().skip(1) { let start = pl.clusters[line.c0].start; assert!(breaks.contains(&start), "line starts at syllable {start}"); } // Lines are full: a syllable break, not an emergency break after one cluster. assert!(pl.lines[0].c1 - pl.lines[0].c0 > 10); } /// A face with Myanmar letters (an installed system font), or `None` to skip. fn myanmar_face() -> Option { wordcraft_fonts::FontDb::global().fallback_for('\u{1000}', 0).map(|f| wordcraft_fonts::FaceRef::of(&f)).filter(|f| f.has_layout_tables()) } #[test] fn myanmar_prevowel_is_drawn_before_its_consonant() { let Some(face) = myanmar_face() else { eprintln!("skipped: no Myanmar font with OpenType tables installed"); return; }; // ကေ: in memory consonant first, on screen the vowel sign ေ first. let ka = face.glyph_for('\u{1000}'); let g = wordcraft_fonts::shape(&face, "\u{1000}\u{1031}", &[], |c| c); assert_eq!(g.len(), 2, "{g:?}"); assert_eq!(g[1].gid, ka, "the consonant is drawn second: {g:?}"); assert!(g.iter().all(|x| x.cluster == 0), "one cluster: {g:?}"); // Itemised by script: the same syllable after Latin text in the same face shapes the same // (shaped as one Latin run, the vowel sign would stay after the consonant). if face.covers('H') { let mixed = wordcraft_fonts::shape(&face, "Hi \u{1000}\u{1031}", &[], |c| c); let tail: Vec = mixed.iter().filter(|x| x.cluster >= 3).map(|x| x.gid).collect(); assert_eq!(tail, g.iter().map(|x| x.gid).collect::>()); } } #[test] fn myanmar_in_a_latin_font_falls_back_to_a_regular_myanmar_face() { if myanmar_face().is_none() { eprintln!("skipped: no Myanmar font with OpenType tables installed"); return; } let l = lay(&Document::from_text(MYANMAR)); let pl = para_layout(&l, 0); let faces: Vec<_> = pl.clusters.iter().map(|c| pl.styles[c.style as usize].face).collect(); assert!(faces.windows(2).all(|w| w[0].id() == w[1].id()), "one face for the word"); let f = faces[0]; assert!(f.has_layout_tables(), "{} can reorder Myanmar", f.family); assert!(!f.italic && (f.weight - 400.0).abs() <= 100.0, "a regular weight, not {} {}", f.family, f.style); assert!(pl.glyphs.iter().all(|g| g.gid != 0), "no missing glyphs"); // The caret after the first syllable sits after its glyphs. let x6 = l.caret(&Pos::body(0, 6)).map(|c| c.x).unwrap_or(0.0); let x0 = l.caret(&Pos::body(0, 0)).map(|c| c.x).unwrap_or(0.0); assert!(x6 > x0); }