//! The bidirectional algorithm against Unicode's own tests, and shaping //! through the fonts the system carries. use pinnacle_text::bidi; use pinnacle_text::shape; use pinnacle_ttf::Font; /// Runs the conformance lines of a `BidiCharacterTest.txt`, returning how /// many there were and the first failures. fn conformance(text: &str) -> (usize, Vec) { let mut count = 0; let mut failures = Vec::new(); for line in text .lines() .filter(|l| !l.starts_with('#') && !l.trim().is_empty()) { let f: Vec<&str> = line.split(';').collect(); let chars: Vec = f[0] .split_whitespace() .map(|h| u32::from_str_radix(h, 16).unwrap()) .collect(); let level = match f[1] { "0" => Some(0), "1" => Some(1), _ => None, }; let want_para: u8 = f[2].parse().unwrap(); let want: Vec> = f[3].split_whitespace().map(|l| l.parse().ok()).collect(); let order: Vec = f[4].split_whitespace().map(|i| i.parse().unwrap()).collect(); count += 1; let p = bidi::resolve(&chars, level); let got: Vec> = (0..chars.len()) .map(|i| (!p.removed[i]).then_some(p.levels[i])) .collect(); let got_order = bidi::visual_order(&p.levels, &p.removed); if p.level != want_para || got != want || got_order != order { if failures.len() < 10 { failures.push(format!( "{line}\n got para {} levels {got:?} order {got_order:?}", p.level )); } } } (count, failures) } #[test] fn bidi_conformance() { let sample = include_str!("bidi-sample.txt"); let (count, failures) = conformance(sample); assert!(count > 3000); assert!( failures.is_empty(), "{} of {count} failed:\n{}", failures.len(), failures.join("\n") ); // The whole file, when it is at hand. if let Ok(path) = std::env::var("PINNACLE_BIDI_TEST") { let all = std::fs::read_to_string(path).unwrap(); let (count, failures) = conformance(&all); assert!( failures.is_empty(), "{} failed of {count}:\n{}", failures.len(), failures.join("\n") ); } } fn font(name: &str) -> &'static Font<'static> { let path = format!("{}/../fonts/{name}", env!("CARGO_MANIFEST_DIR")); let data: &'static [u8] = std::fs::read(&path).unwrap().leak(); Box::leak(Box::new(Font::parse(data).expect(name))) } struct Faces(Vec<&'static Font<'static>>); impl Faces { fn system() -> Faces { Faces(vec![ font("AdwaitaSans-Regular.ttf"), font("NotoSansArabic-Regular.ttf"), font("NotoSansHebrew-Regular.ttf"), font("DroidSansFallbackFull.ttf"), font("NotoEmoji-Regular.ttf"), ]) } fn shape(&self, text: &str) -> shape::Line { let get = |i: usize| self.0.get(i).copied(); shape::shape(text, 20.0, &get, self.0.len(), Some(4)) } } fn glyphs(line: &shape::Line) -> Vec<(usize, u16)> { line.glyphs.iter().map(|g| (g.face, g.glyph)).collect() } /// HarfBuzz 11's shaping of these lines (its glyphs, their x and y from the /// line's start in font units, y down, and the line's width), made with /// uharfbuzz when the shaper was written: what `shape` must match. const HARFBUZZ: &[(&str, &str, &[(u16, i32, i32)], i32)] = &[ ( "NotoSansArabic-Regular.ttf", "بيت", &[ (287, 345, 165), (15, 0, 0), (318, 1132, 5), (18, 1093, 0), (316, 1497, 3), (19, 1466, 0), ], 1735, ), ( "NotoSansArabic-Regular.ttf", "لا", &[(10, 0, 0), (73, 363, 0)], 582, ), ( "NotoSansArabic-Regular.ttf", "بَ", &[(370, 324, 160), (316, 410, 23), (14, 0, 0)], 993, ), ( "NotoSansArabic-Regular.ttf", "سلام عليكم", &[ (77, 0, 0), (60, 562, 0), (318, 1109, 5), (18, 1070, 0), (70, 1443, 0), (49, 1743, 0), (3, 2246, 0), (74, 2506, 0), (11, 2990, 0), (71, 3369, 0), (37, 3589, 0), ], 4373, ), ( "NotoSansArabic-Regular.ttf", "مرحبا بالعالم", &[ (77, 0, 0), (72, 562, 0), (9, 822, 0), (48, 1113, 0), (72, 1634, 0), (9, 1894, 0), (316, 2216, 3), (19, 2185, 0), (3, 2454, 0), (9, 2714, 0), (316, 3099, 3), (16, 3005, 0), (27, 3348, 0), (31, 3897, 0), (79, 4293, 0), ], 4818, ), ( "NotoSansArabic-Regular.ttf", "العربية", &[ (287, 101, -65), (85, 0, 0), (318, 525, 5), (18, 486, 0), (316, 890, 3), (19, 859, 0), (31, 1098, 0), (48, 1494, 0), (72, 2015, 0), (8, 2275, 0), ], 2513, ), ( "NotoSansArabic-Regular.ttf", "شُكْرًا", &[ (8, 0, 0), (374, 213, 106), (31, 213, 0), (378, 664, -147), (60, 609, 0), (371, 1374, -186), (292, 1403, 131), (37, 1117, 0), ], 1901, ), // HarfBuzz does no bidi, and drew the digits right to left; they run // left to right inside the Arabic, as Unicode's algorithm says. ( "NotoSansArabic-Regular.ttf", "في ٢٠٢٦", &[ (130, 0, 0), (128, 572, 0), (130, 1144, 0), (134, 1716, 0), (3, 2288, 0), (318, 2733, 232), (104, 2548, 0), (282, 3431, -38), (53, 3284, 0), ], 3731, ), ( "NotoSansHebrew-Regular.ttf", "שָׁלוֹם", &[ (23, 0, 0), (46, 766, 0), (124, 674, 0), (55, 975, 0), (79, 1724, 0), (100, 2036, 0), (96, 1497, 0), ], 2227, ), ( "NotoSansHebrew-Regular.ttf", "עִבְרִית", &[ (107, 0, 0), (138, 665, 0), (45, 1279, 0), (86, 960, 0), (95, 1650, 0), (12, 1458, 0), (45, 2230, 0), (10, 2030, 0), ], 2623, ), ("NotoEmoji-Regular.ttf", "👍🏽", &[(578, 0, 0)], 2600), ("NotoEmoji-Regular.ttf", "👨‍👩‍👧", &[(1485, 0, 0)], 2600), ("NotoEmoji-Regular.ttf", "🇳🇱", &[(1778, 0, 0)], 2600), ]; /// Each line shaped alone in its font, at one pixel a font unit, as /// HarfBuzz shaped it: the same glyphs in the same order at the same /// places. Arabic's joined forms, its dots and vowels as marks on their /// letters, lam-alef, Hebrew's points, Arabic-Indic digits left to right /// inside right-to-left text, emoji with skin tones, families and flags. #[test] fn shapes_as_harfbuzz_does() { let mut wrong = Vec::new(); for &(file, text, want, width) in HARFBUZZ { let face = font(file); let get = |i: usize| (i == 0).then_some(face); let emoji = file.contains("Emoji").then_some(0); let line = shape::shape(text, face.units_per_em(), &get, 1, emoji); let mut got: Vec<(u16, i32, i32)> = line .glyphs .iter() .map(|g| (g.glyph, g.x.round() as i32, g.y.round() as i32)) .collect(); // Compared as places, not as the order they are drawn in: two marks // on one letter may come either way. let mut want = want.to_vec(); got.sort_unstable_by_key(|&(g, x, y)| (x, y, g)); want.sort_unstable_by_key(|&(g, x, y)| (x, y, g)); if got != want || (line.width.round() as i32 - width).abs() > 1 { wrong.push(format!( "{text}: want {want:?} width {width}\n got {got:?} width {}", line.width )); } } assert!(wrong.is_empty(), "{}", wrong.join("\n")); } #[test] fn mixed_directions_reorder() { let faces = Faces::system(); let line = faces.shape("abc שלום 12"); assert!(!line.rtl); // a, b, c first; the number after the Hebrew word belongs to its // right-to-left run, so it comes first in it, then the word reversed. let clusters: Vec = line.glyphs.iter().map(|g| g.cluster).collect(); assert_eq!(clusters, [0, 1, 2, 3, 9, 10, 8, 7, 6, 5, 4]); assert!(line.glyphs[7..11].iter().all(|g| g.face == 2)); // A right-to-left paragraph puts the Latin word on the left, the // Hebrew on the right. let line = faces.shape("שלום abc"); assert!(line.rtl); let clusters: Vec = line.glyphs.iter().map(|g| g.cluster).collect(); assert_eq!(clusters, [5, 6, 7, 4, 3, 2, 1, 0]); } #[test] fn chinese_and_emoji_find_their_fonts() { let faces = Faces::system(); let line = faces.shape("中文 ok"); assert_eq!(line.glyphs[0].face, 3); assert_eq!(line.glyphs[1].face, 3); assert_eq!(line.glyphs.last().unwrap().face, 0); let line = faces.shape("hi 😀"); assert_eq!(line.glyphs.last().unwrap().face, 4); // A thumb with a skin tone is one picture, as is a family joined by // ZWJs. let line = faces.shape("👍🏽"); assert_eq!(line.glyphs.len(), 1, "{line:?}"); let line = faces.shape("👨\u{200d}👩\u{200d}👧"); assert_eq!(line.glyphs.len(), 1, "{line:?}"); // The selector asks for the emoji of a character the sans has too. let line = faces.shape("\u{2764}\u{fe0f}"); assert_eq!(glyphs(&line).len(), 1); assert_eq!(line.glyphs[0].face, 4); } #[test] fn latin_is_as_it_was() { let faces = Faces::system(); let sans = faces.0[0]; let line = faces.shape("Pinnacle, Café"); let want: Vec<(usize, u16)> = "Pinnacle, Café" .chars() .map(|c| (0, sans.glyph_index(c))) .collect(); assert_eq!(glyphs(&line), want); let advances: f32 = "Pinnacle, Café" .chars() .map(|c| sans.glyph_advance(sans.glyph_index(c), 20.0)) .sum(); assert!((line.width - advances).abs() < 0.01); }