diff --git a/crates/proof/README.md b/crates/proof/README.md index 273dd23f81..8d47f29779 100644 --- a/crates/proof/README.md +++ b/crates/proof/README.md @@ -20,7 +20,7 @@ craft-proof = { git = "https://github.com/storytold/craft-libs", rev = " | German | — | — | TeX patterns (embedded, or a Language pack) | | Bulgarian | — | — | syllable rules | | Gujarati, Hindi | the installed Hunspell dictionary (Hindi: nukta letters spelled either way) | generic checks | akshara rules (or a Language pack) | -| Marathi (mr) | its Language pack or Hunspell dictionary, as any pack language | — | akshara rules (or a Language pack) | +| Marathi (mr), Bengali (bn), Assamese (as), Punjabi in Gurmukhi (pa), Odia (or), Tamil (ta), Telugu (te), Kannada (kn), Malayalam (ml) | its Language pack or Hunspell dictionary, as any pack language | — | akshara rules (or a Language pack) | Grammar rules, suggestion tuning and the rule-based hyphenation are code: they work in every build. diff --git a/crates/proof/src/hyphen.rs b/crates/proof/src/hyphen.rs index f8569a194c..ac4d5f76ee 100644 --- a/crates/proof/src/hyphen.rs +++ b/crates/proof/src/hyphen.rs @@ -15,9 +15,10 @@ //! //! **Bulgarian**: the Bulgarian syllable rules ([`crate::bg`]); no pattern file or word list. //! -//! **Gujarati, Hindi and Marathi**: breaks between aksharas, never inside a conjunct or between a -//! consonant and its vowel sign ([`crate::indic`], rules from the script's Unicode encoding); no -//! pattern file. +//! **Indic languages** (Gujarati, Hindi, Marathi, Bengali, Assamese, Punjabi, Odia, Tamil, Telugu, +//! Kannada, Malayalam): breaks between aksharas, never inside a conjunct or between a consonant +//! and its vowel sign ([`crate::indic`], rules from the script's Unicode encoding); no pattern +//! file. //! //! **German**: the TeX German patterns for the reformed spelling ([`Patterns::de`]), also for //! Swiss text, whose `ss` the patterns split like any double consonant (`Stras-se`). @@ -120,7 +121,7 @@ pub enum Source { Patterns, /// A heuristic for a language without a dictionary or patterns. Heuristic, - /// A language's syllable rules (Bulgarian [`crate::bg`], Turkish, Gujarati, Hindi and Marathi [`crate::indic`]). + /// A language's syllable rules (Bulgarian [`crate::bg`], Turkish, the Indic languages [`crate::indic`]). Rules, /// No break points: the word is too short or no source applies. None, @@ -167,7 +168,7 @@ fn core_breaks(core: &[char], lang: ProofLang) -> (Vec, Source) { if lang == ProofLang::Bg && core.iter().all(|&c| crate::bg::is_letter(c)) { return (crate::bg::points(core), Source::Rules); } - // Gujarati, Hindi and Marathi words break between aksharas ([`crate::indic`]); other words in their runs + // Words of the Indic languages break between aksharas ([`crate::indic`]); other words in their runs // (`WordCraft`) are hyphenated as English. if let Some(script) = crate::indic::Script::of(lang) && core.iter().all(|&c| script.is_letter(c)) @@ -242,8 +243,8 @@ fn composed_chars(core: &[char]) -> (Vec, Vec>) { (nfc, starts) } -/// Does a pack's `pats` hyphenate `core` in `lang`? Russian, Bulgarian, Turkish, Gujarati, Hindi -/// and Marathi: a word in their script (the guard their rules use; for the Indic ones, the script's +/// Does a pack's `pats` hyphenate `core` in `lang`? Russian, Bulgarian, Turkish and the Indic +/// languages: a word in their script (the guard their rules use; for the Indic ones, the script's /// letters and marks and the joiners inside a conjunct, [`crate::indic`]) whose letters the /// patterns know, so a word with a letter the pack never mentions goes on to the fallback rules. Polish, Portuguese, Italian /// and German: every word, as their embedded patterns do. Any other language: a word whose letters diff --git a/crates/proof/src/hyphen/tests.rs b/crates/proof/src/hyphen/tests.rs index 124c265c45..994fcda21f 100644 --- a/crates/proof/src/hyphen/tests.rs +++ b/crates/proof/src/hyphen/tests.rs @@ -244,3 +244,24 @@ fn marathi_breaks_between_aksharas() { assert_eq!(hyphenate_word_in("महाराष्ट्र", &Limits::default(), mr), "महा-रा-ष्ट्र"); assert_eq!(word_breaks_in("महाराष्ट्र", mr).1, Source::Rules); } + +/// storytold/vectorcraft#4947: the other Indic pack languages break between aksharas within the +/// paragraph's limits, where they used to fall to the English rules and get no break at all. +#[test] +fn indic_pack_languages_break_between_aksharas() { + let cases = [ + ("bn-IN", "বিদ্যাল\u{09AF}\u{09BC}", "বি-দ্যা-ল\u{09AF}\u{09BC}"), + ("as", "অসমী\u{09AF}\u{09BC}া", "অস-মী-\u{09AF}\u{09BC}া"), + ("pa", "ਗੁਰਮੁਖੀ", "ਗੁ-ਰ-ਮੁ-ਖੀ"), + ("or", "ବିଦ୍ୟାଳୟ", "ବି-ଦ୍ୟା-ଳୟ"), + ("ta-IN", "தமிழ்நாடு", "தமிழ்-நா-டு"), + ("te", "తెలుగు", "తె-లు-గు"), + ("kn", "ಕನ್ನಡಿಗ", "ಕನ್ನ-ಡಿಗ"), + ("ml", "മലയാളം", "മല-യാ-ളം"), + ]; + for (tag, word, want) in cases { + let lang = ProofLang::from_tag(Some(tag)); + assert_eq!(hyphenate_word_in(word, &Limits::default(), lang), want, "{tag}"); + assert_eq!(word_breaks_in(word, lang).1, Source::Rules, "{tag}"); + } +} diff --git a/crates/proof/src/indic.rs b/crates/proof/src/indic.rs index 313e9c38bd..35a8116fd7 100644 --- a/crates/proof/src/indic.rs +++ b/crates/proof/src/indic.rs @@ -14,9 +14,20 @@ //! The rules come from the scripts' Unicode encoding alone; no pattern file or word list is //! read (and none is borrowed). The Brahmic blocks share one layout (the ISCII order), so a //! script is its block's start and a few script-specific code points (`Script::kind`). -//! Gujarati ([`ProofLang::Gu`]), Hindi ([`ProofLang::Hi`]) and Marathi (the pack language `mr`) -//! use them, Hindi and Marathi with [`Script::Devanagari`]; -//! another language written in one of these scripts is one more line in [`Script::of`]. +//! Gujarati ([`ProofLang::Gu`]) and Hindi ([`ProofLang::Hi`]) use them, and so do these pack +//! languages: Marathi (`mr`), Bengali (`bn`) and Assamese (`as`), Punjabi in Gurmukhi (`pa`), +//! Odia (`or`), Tamil (`ta`), Telugu (`te`), Kannada (`kn`) and Malayalam (`ml`). Another +//! language written in one of these scripts is one more line in [`Script::of`]. +//! +//! A dead consonant written as a letter of its own — Bengali's khanda ta (`ৎ`), Malayalam's chillus +//! (`ൻ`, `ർ`), Telugu's and Kannada's nakaara pollu — ends the syllable before, as a mark does: +//! `উৎ-সব`, `കർ-മ്മം`. So does a chillu or khanda ta written the older way, as its consonant, the +//! virama and a zero-width joiner (`ന്` U+200D, `ত্` U+200D): the one place a break follows a +//! joiner. Gurmukhi's addak (`ੱ`) and tippi (`ੰ`) are marks too: `ਪੱ-ਕਾ`. +//! +//! **Tamil** joins no conjuncts: a consonant with a visible virama (pulli) closes the syllable +//! before it, so the break goes after the pulli, not before the consonant (`பக்-கம்`, `தமிழ்-நா-டு`). +//! Its two conjuncts, KSSA (`க்ஷ`) and SHRII (`ஸ்ரீ`), stay whole. //! //! A single akshara left at the end or start of a line is ruled out by the paragraph's //! [`Limits`](crate::hyphen::Limits), which count letters (vowel signs and anusvara count, @@ -30,8 +41,22 @@ use crate::ProofLang; pub enum Script { /// Devanagari (U+0900..=U+097F): Hindi, Marathi, Nepali… Devanagari, + /// Bengali (U+0980..=U+09FF): Bengali, Assamese. + Bengali, + /// Gurmukhi (U+0A00..=U+0A7F): Punjabi. + Gurmukhi, /// Gujarati (U+0A80..=U+0AFF). Gujarati, + /// Odia (U+0B00..=U+0B7F). + Odia, + /// Tamil (U+0B80..=U+0BFF). + Tamil, + /// Telugu (U+0C00..=U+0C7F). + Telugu, + /// Kannada (U+0C80..=U+0CFF). + Kannada, + /// Malayalam (U+0D00..=U+0D7F). + Malayalam, } /// What a code point is in an akshara. @@ -48,6 +73,9 @@ enum Kind { Mark, /// A zero-width joiner or non-joiner: shapes the conjunct around it, in the same akshara. Joiner, + /// A sign written before the consonant it joins (Malayalam's dot reph, Kannada's jihvamuliya + /// and upadhmaniya), so it starts an akshara with that consonant and no break follows it. + Reph, } /// Is `c` a zero-width joiner or non-joiner (U+200D, U+200C)? Inside an Indic word it shapes a @@ -62,9 +90,20 @@ impl Script { match lang { ProofLang::Gu => Some(Script::Gujarati), ProofLang::Hi => Some(Script::Devanagari), - // Marathi has no proofing language of its own: `mr`, `mr-IN` and `mar` are pack - // languages, spelled from their pack or Hunspell dictionary. - ProofLang::Pack(l) if matches!(l.primary(), "mr" | "mar") => Some(Script::Devanagari), + // The others have no proofing language of their own: their tags (`mr`, `mr-IN`, + // `mar`…) are pack languages, spelled from their pack or Hunspell dictionary. + ProofLang::Pack(l) => match l.primary() { + "mr" | "mar" => Some(Script::Devanagari), + "bn" | "ben" | "as" | "asm" => Some(Script::Bengali), + // Punjabi in Gurmukhi, its default script; Shahmukhi (`pa-Arab`) is not Brahmic. + "pa" | "pan" if crate::lang::effective_script(l.as_str()).is_none_or(|s| s == "guru") => Some(Script::Gurmukhi), + "or" | "ori" | "ory" => Some(Script::Odia), + "ta" | "tam" => Some(Script::Tamil), + "te" | "tel" => Some(Script::Telugu), + "kn" | "kan" => Some(Script::Kannada), + "ml" | "mal" => Some(Script::Malayalam), + _ => None, + }, _ => None, } } @@ -73,7 +112,14 @@ impl Script { fn base(self) -> u32 { match self { Script::Devanagari => 0x0900, + Script::Bengali => 0x0980, + Script::Gurmukhi => 0x0A00, Script::Gujarati => 0x0A80, + Script::Odia => 0x0B00, + Script::Tamil => 0x0B80, + Script::Telugu => 0x0C00, + Script::Kannada => 0x0C80, + Script::Malayalam => 0x0D00, } } @@ -84,29 +130,60 @@ impl Script { return Some(Kind::Joiner); } let o = u32::from(c).checked_sub(self.base()).filter(|&o| o < 0x80)?; - let dev = self == Script::Devanagari; - Some(match o { - // Candrabindu, anusvara, visarga (and Devanagari's inverted candrabindu). - 0x00..=0x03 => Kind::Mark, - 0x04..=0x14 => Kind::Vowel, - 0x15..=0x39 => Kind::Consonant, - // Devanagari's vowel signs OE and OOE, the nukta, the avagraha, the vowel signs. - 0x3A..=0x4C => Kind::Mark, - 0x4D => Kind::Virama, - // Devanagari's vowel signs PRISHTHAMATRA E and AW, stress signs and vowel signs. - 0x4E..=0x4F | 0x51..=0x57 if dev => Kind::Mark, - // Devanagari's consonants with nukta (`क़`, `ज़`, `फ़`…). - 0x58..=0x5F if dev => Kind::Consonant, - // Vocalic RR and LL. - 0x60..=0x61 => Kind::Vowel, - 0x62..=0x63 => Kind::Mark, - // Devanagari's vowels for Marathi, Kashmiri and Bihari languages, and consonants for - // Sindhi, Marwari and others. - 0x72..=0x77 if dev => Kind::Vowel, - 0x78..=0x7F if dev => Kind::Consonant, - // Gujarati ZHA, and the nuktas and signs for transliterating Arabic. - 0x79 => Kind::Consonant, - 0x7A..=0x7F => Kind::Mark, + self.own_kind(o).or(match o { + // Candrabindu, anusvara, visarga; the nukta, the avagraha and the vowel signs; vocalic + // L and LL vowel signs. + 0x00..=0x03 | 0x3A..=0x4C | 0x62..=0x63 => Some(Kind::Mark), + // The independent vowels, vocalic RR and LL among them. + 0x05..=0x14 | 0x60..=0x61 => Some(Kind::Vowel), + 0x15..=0x39 => Some(Kind::Consonant), + 0x4D => Some(Kind::Virama), + _ => None, + }) + } + + /// What the code point at offset `o` in the block is in this script where the scripts differ + /// from their shared layout; `None` where they don't. + fn own_kind(self, o: u32) -> Option { + use Script::{Bengali, Devanagari, Gujarati, Gurmukhi, Kannada, Malayalam, Odia, Tamil, Telugu}; + Some(match (self, o) { + // Short A, the vowels for Marathi, Kashmiri and Bihari languages; consonants with + // nukta (`क़`, `ज़`) and for Sindhi, Marwari and others; vowel signs PRISHTHAMATRA E + // and AW, stress signs and more vowel signs. + (Devanagari, 0x04 | 0x72..=0x77) => Kind::Vowel, + (Devanagari, 0x58..=0x5F | 0x78..=0x7F) => Kind::Consonant, + (Devanagari, 0x4E..=0x4F | 0x51..=0x57) => Kind::Mark, + // Khanda ta (a dead consonant), the AU length mark, the Vedic anusvara, the sandhi + // mark; RRA, RHA, YYA, and Assamese RA and WA (`ৰ`, `ৱ`). + (Bengali, 0x4E | 0x57 | 0x7C | 0x7E) => Kind::Mark, + (Bengali, 0x5C..=0x5D | 0x5F | 0x70..=0x71) => Kind::Consonant, + // Udaat, tippi, addak, yakash; consonants with nukta; the vowel bearers IRI and URA. + (Gurmukhi, 0x51 | 0x70..=0x71 | 0x75) => Kind::Mark, + (Gurmukhi, 0x59..=0x5E) => Kind::Consonant, + (Gurmukhi, 0x72..=0x73) => Kind::Vowel, + // ZHA, and the nuktas and signs for transliterating Arabic. + (Gujarati, 0x79) => Kind::Consonant, + (Gujarati, 0x7A..=0x7F) => Kind::Mark, + // Length marks; RRA, RHA, YYA, WA. + (Odia, 0x55..=0x57) => Kind::Mark, + (Odia, 0x5C..=0x5D | 0x5F | 0x71) => Kind::Consonant, + (Tamil, 0x57) => Kind::Mark, + // Combining anusvara above, length marks, nakaara pollu (a dead consonant); the + // archaic consonants. + (Telugu, 0x04 | 0x55..=0x56 | 0x5D) => Kind::Mark, + (Telugu, 0x58..=0x5A) => Kind::Consonant, + // Length marks, nakaara pollu, the anusvara above right; FA; jihvamuliya and + // upadhmaniya, which stack on the stop after them. + (Kannada, 0x55..=0x56 | 0x5D | 0x73) => Kind::Mark, + (Kannada, 0x5E) => Kind::Consonant, + (Kannada, 0x71..=0x72) => Kind::Reph, + // The Vedic anusvara, the chillus (dead consonants), the AU length mark; TTTA; the + // vertical-bar and circular viramas; the dot reph; II. + (Malayalam, 0x04 | 0x54..=0x57 | 0x7A..=0x7F) => Kind::Mark, + (Malayalam, 0x3A) => Kind::Consonant, + (Malayalam, 0x3B..=0x3C) => Kind::Virama, + (Malayalam, 0x4E) => Kind::Reph, + (Malayalam, 0x5F) => Kind::Vowel, _ => return None, }) } @@ -119,28 +196,50 @@ impl Script { } /// Break points of one `word` in `script` (char indices; a hyphen goes before that char): before -/// each akshara but the first, except a dead consonant. Empty for anything that is not entirely -/// the script's letters and marks. +/// each akshara but the first, except a dead consonant (in Tamil, after each pulli instead). +/// Empty for anything that is not entirely the script's letters and marks. pub fn points(script: Script, word: &[char]) -> Vec { - let Some(kinds) = word.iter().map(|&c| script.kind(c)).collect::>>() else { + let Some(mut kinds) = word.iter().map(|&c| script.kind(c)).collect::>>() else { return Vec::new(); }; + // A Malayalam chillu with a virama after it leads a conjunct (`ൻ്റ`), as its consonant would. + if script == Script::Malayalam { + for k in 0..word.len().saturating_sub(1) { + if matches!(word[k], '\u{0D54}'..='\u{0D56}' | '\u{0D7A}'..='\u{0D7F}') && kinds[k + 1] == Kind::Virama { + kinds[k] = Kind::Consonant; + } + } + } let at = |i: usize| kinds.get(i).copied(); // Has the akshara starting at `i` a vowel (independent, a sign, or the inherent one)? Only a // consonant cluster ending in a virama (`ક્`, `ન્ત્`, with or without a joiner) has none. + let legacy = |k: usize| legacy_dead_letter(script, word, &kinds, k); + // Malayalam's samvruthokaram, the U sign and a virama (`ക്കു്`), is a vowel: the virama after + // it kills nothing and joins nothing. + let vowel_virama = |v: usize| script == Script::Malayalam && v > 0 && word[v] == '\u{0D4D}' && word[v - 1] == '\u{0D41}'; + // Malayalam's vertical-bar and circular viramas only kill the vowel: they join no conjunct. + let killer = |v: usize| script == Script::Malayalam && matches!(word.get(v), Some('\u{0D3B}' | '\u{0D3C}')); let live = |i: usize| { - let mut j = i; + // A dot reph leads the cluster it joins. + let mut j = if at(i) == Some(Kind::Reph) { i + 1 } else { i }; loop { if at(j) != Some(Kind::Consonant) { return true; } + // A dead letter written the older way ends the cluster, whatever follows it. + if legacy(j) { + return false; + } j += 1; while matches!(at(j), Some(Kind::Mark | Kind::Joiner)) { j += 1; } - if at(j) != Some(Kind::Virama) { + if at(j) != Some(Kind::Virama) || vowel_virama(j) { return true; } + if killer(j) { + return false; + } j += 1; while at(j) == Some(Kind::Joiner) { j += 1; @@ -150,9 +249,48 @@ pub fn points(script: Script, word: &[char]) -> Vec { } } }; - (1..word.len()) - .filter(|&i| matches!(at(i), Some(Kind::Vowel | Kind::Consonant)) && !matches!(at(i - 1), Some(Kind::Virama | Kind::Joiner)) && live(i)) - .collect() + let starts = |i: usize| match script { + Script::Tamil => at(i - 1) != Some(Kind::Joiner) && tamil_starts(word, &kinds, i), + _ if legacy(i) => false, + _ => { + ((i >= 3 && legacy(i - 3)) + || vowel_virama(i - 1) + || killer(i - 1) + || !matches!(at(i - 1), Some(Kind::Virama | Kind::Joiner | Kind::Reph))) + && live(i) + } + }; + (1..word.len()).filter(|&i| matches!(at(i), Some(Kind::Vowel | Kind::Consonant | Kind::Reph)) && starts(i)).collect() +} + +/// Is a dead letter written the older way, as its consonant, the virama and a zero-width joiner +/// (`ന്` U+200D), at `k`: a Malayalam chillu (NNA, NA, RA, LA or LLA) or Bengali khanda ta (TA)? +/// Like the letter itself (`ൻ`, `ৎ`), it ends the syllable before. +fn legacy_dead_letter(script: Script, word: &[char], kinds: &[Kind], k: usize) -> bool { + let base = match script { + Script::Malayalam => matches!(word.get(k), Some('\u{0D23}' | '\u{0D28}' | '\u{0D30}' | '\u{0D32}' | '\u{0D33}')), + Script::Bengali => word.get(k) == Some(&'\u{09A4}'), + _ => false, + }; + base && kinds.get(k + 1) == Some(&Kind::Virama) && word.get(k + 2) == Some(&'\u{200D}') +} + +/// Does a syllable start at `i`, a consonant or vowel, in a Tamil word? Not at a consonant with a +/// pulli, which closes the syllable before (`பக்-கம்`), nor inside one of the two conjuncts Tamil +/// writes, KSSA (`க்ஷ`) and SHRII (`ஸ்ரீ`, `ஶ்ரீ`); a KSSA with a pulli closes the syllable too. +fn tamil_starts(word: &[char], kinds: &[Kind], i: usize) -> bool { + let conjunct = |k: usize| { + kinds.get(k + 1) == Some(&Kind::Virama) + && match (word.get(k).copied(), word.get(k + 2).copied()) { + // KA, SSA. + (Some('\u{0B95}'), Some('\u{0BB7}')) => true, + // SA or SHA, RA, and the vowel sign II. + (Some('\u{0BB8}' | '\u{0BB6}'), Some('\u{0BB0}')) => word.get(k + 3) == Some(&'\u{0BC0}'), + _ => false, + } + }; + let closes = kinds.get(i + 1) == Some(&Kind::Virama) && !(conjunct(i) && kinds.get(i + 3) != Some(&Kind::Virama)); + !closes && !(i >= 2 && conjunct(i - 2)) } #[cfg(test)] @@ -247,15 +385,105 @@ mod tests { assert_eq!(hi("क्\u{200D}षमा"), "क्\u{200D}ष-मा"); } - /// Gujarati, Hindi and Marathi (a pack language, by any of its tags) are mapped to their - /// scripts, other languages (another pack language among them) to none. + /// Bengali and Assamese: conjuncts stay whole; khanda ta ends the syllable before; Assamese RA. + #[test] + fn bengali_script_breaks_between_aksharas() { + let bn = |w: &str| split(Script::Bengali, w); + assert_eq!(bn("বাংলা"), "বাং-লা"); + assert_eq!(bn("বিদ্যাল\u{09AF}\u{09BC}"), "বি-দ্যা-ল-\u{09AF}\u{09BC}", "YA with nukta"); + assert_eq!(bn("বিদ্যাল\u{09DF}"), "বি-দ্যা-ল-\u{09DF}", "precomposed YYA"); + assert_eq!(bn("উৎসব"), "উৎ-স-ব"); + assert_eq!(bn("মহোত\u{09CD}\u{200D}সব"), "ম-হোত\u{09CD}\u{200D}-স-ব", "khanda ta written with a joiner"); + assert_eq!(bn("মহোৎসব"), "ম-হোৎ-স-ব", "as with the khanda ta letter"); + assert_eq!(bn("উদ\u{09CD}\u{200D}যম"), "উ-দ\u{09CD}\u{200D}য-ম", "DA has no khanda form: a conjunct"); + assert_eq!(bn("ৰাজ্য"), "ৰা-জ্য"); + } + + /// Gurmukhi: tippi and addak stay with the akshara before them. + #[test] + fn gurmukhi_breaks_between_aksharas() { + let pa = |w: &str| split(Script::Gurmukhi, w); + assert_eq!(pa("ਪੰਜਾਬੀ"), "ਪੰ-ਜਾ-ਬੀ"); + assert_eq!(pa("ਪੱਕਾ"), "ਪੱ-ਕਾ"); + assert_eq!(pa("ਗੁਰਮੁਖੀ"), "ਗੁ-ਰ-ਮੁ-ਖੀ"); + } + + /// Odia, Telugu and Kannada: the shared rules, conjuncts whole, nukta and anusvara attached. + #[test] + fn odia_telugu_and_kannada_break_between_aksharas() { + let or = |w: &str| split(Script::Odia, w); + assert_eq!(or("\u{0B13}\u{0B21}\u{0B3C}\u{0B3F}\u{0B06}"), "\u{0B13}-\u{0B21}\u{0B3C}\u{0B3F}-\u{0B06}"); + assert_eq!(or("ବିଦ୍ୟାଳୟ"), "ବି-ଦ୍ୟା-ଳ-ୟ"); + let te = |w: &str| split(Script::Telugu, w); + assert_eq!(te("తెలుగు"), "తె-లు-గు"); + assert_eq!(te("విద్యార్థి"), "వి-ద్యా-ర్థి"); + let kn = |w: &str| split(Script::Kannada, w); + assert_eq!(kn("ಕನ್ನಡ"), "ಕ-ನ್ನ-ಡ"); + assert_eq!(kn("ಸಂಸ್ಕೃತ"), "ಸಂ-ಸ್ಕೃ-ತ"); + assert_eq!(kn("ಅ\u{0CF1}ಕಿ"), "ಅ-\u{0CF1}ಕಿ", "jihvamuliya stacks on the stop after it"); + assert_eq!(kn("ಅ\u{0CF2}ಪ"), "ಅ-\u{0CF2}ಪ", "upadhmaniya too"); + } + + /// Malayalam: conjuncts whole; a chillu ends the syllable before. + #[test] + fn malayalam_breaks_between_aksharas() { + let ml = |w: &str| split(Script::Malayalam, w); + assert_eq!(ml("മലയാളം"), "മ-ല-യാ-ളം"); + assert_eq!(ml("അവൻ"), "അ-വൻ"); + assert_eq!(ml("കർമ്മം"), "കർ-മ്മം"); + assert_eq!(ml("വൻമരം"), "വൻ-മ-രം"); + assert_eq!(ml("വന്\u{200D}മരം"), "വന്\u{200D}-മ-രം", "a chillu written with a joiner"); + assert_eq!(ml("അവന്\u{200D}"), "അ-വന്\u{200D}"); + assert_eq!(ml("കല\u{200D}മ"), "ക-ല\u{200D}മ", "a joiner after no virama still binds"); + assert_eq!(ml("അസ്\u{200D}മ"), "അ-സ്\u{200D}മ", "SA has no chillu: a conjunct"); + assert_eq!(ml("പാര്\u{200D}ക്ക്"), "പാര്\u{200D}ക്ക്", "nor is a dead cluster carried over"); + assert_eq!(ml("അന്ന്\u{200D}മ"), "അന്ന്\u{200D}-മ", "a cluster ending in a chillu is dead"); + assert_eq!(ml("ഭാ\u{0D4E}യ"), "ഭാ-\u{0D4E}യ", "a dot reph leads its akshara"); + assert_eq!(ml("അക്\u{200D}കര"), "അ-ക്\u{200D}ക-ര", "KA has no legacy chillu: a conjunct"); + assert_eq!(ml("അവൾക്കു്"), "അ-വൾ-ക്കു്", "samvruthokaram is a vowel"); + assert_eq!(ml("അതു്മാത്രം"), "അ-തു്-മാ-ത്രം", "and joins no conjunct"); + assert_eq!(ml("കു\u{200D}മ"), "കു\u{200D}മ", "a joiner after the U sign still binds"); + assert_eq!(ml("അക\u{0D3B}മാ"), "അക\u{0D3B}-മാ", "the vertical-bar virama only kills the vowel"); + assert_eq!(ml("അക\u{0D3C}മാ"), "അക\u{0D3C}-മാ", "so does the circular virama"); + assert_eq!(ml("അവ\u{0D7B}\u{0D4D}\u{0D31}\u{0D46}"), "അ-വ-\u{0D7B}\u{0D4D}\u{0D31}\u{0D46}", "a chillu leading a conjunct"); + } + + /// Tamil: the break goes after a pulli, never before a consonant that carries one; KSSA is + /// one akshara. + #[test] + fn tamil_breaks_after_the_pulli() { + let ta = |w: &str| split(Script::Tamil, w); + assert_eq!(ta("தமிழ்"), "த-மிழ்"); + assert_eq!(ta("பக்கம்"), "பக்-கம்"); + assert_eq!(ta("தமிழ்நாடு"), "த-மிழ்-நா-டு"); + assert_eq!(ta("அக்ஷரம்"), "அ-க்ஷ-ரம்"); + assert_eq!(ta("லக்ஷ்மி"), "லக்ஷ்-மி", "a KSSA with a pulli closes the syllable"); + assert_eq!(ta("ஸ்ரீலங்கா"), "ஸ்ரீ-லங்-கா", "SHRII with SA"); + assert_eq!(ta("ஶ்ரீலங்கா"), "ஶ்ரீ-லங்-கா", "SHRII with SHA"); + } + + /// Gujarati, Hindi and the pack languages written in these scripts (by any of their tags) are + /// mapped to their scripts, other languages (another pack language among them) to none. #[test] fn languages_map_to_their_script() { assert_eq!(Script::of(ProofLang::Gu), Some(Script::Gujarati)); assert_eq!(Script::of(ProofLang::Hi), Some(Script::Devanagari)); - for tag in ["mr", "mr-IN", "mar"] { - assert_eq!(Script::of(ProofLang::from_tag(Some(tag))), Some(Script::Devanagari), "{tag}"); + let packs = [ + (Script::Devanagari, &["mr", "mr-IN", "mar"][..]), + (Script::Bengali, &["bn", "bn-BD", "ben", "as", "as-IN", "asm"]), + (Script::Gurmukhi, &["pa", "pa-IN", "pa-Guru", "pan"]), + (Script::Odia, &["or", "or-IN", "ori", "ory"]), + (Script::Tamil, &["ta", "ta-LK", "tam"]), + (Script::Telugu, &["te", "tel"]), + (Script::Kannada, &["kn", "kan"]), + (Script::Malayalam, &["ml", "mal"]), + ]; + for (script, tags) in packs { + for tag in tags { + assert_eq!(Script::of(ProofLang::from_tag(Some(tag))), Some(script), "{tag}"); + } } + assert_eq!(Script::of(ProofLang::from_tag(Some("pa-Arab"))), None, "Shahmukhi"); assert_eq!(Script::of(ProofLang::from_tag(Some("ne"))), None); assert_eq!(Script::of(ProofLang::En), None); assert_eq!(Script::of(ProofLang::Bg), None);