diff --git a/NOTICE b/NOTICE index 3b19745fa..882e2a764 100644 --- a/NOTICE +++ b/NOTICE @@ -130,6 +130,22 @@ list (author, source, license) is in ASSETS.md. AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +- Indic hyphenation patterns, embedded through the hypher crate (features "hindi", "marathi", + "bengali", "assamese", "gujarati", "panjabi", "oriya", "tamil", "telugu", "kannada", + "malayalam"): hyph-hi.tex, hyph-mr.tex, hyph-bn.tex, hyph-as.tex, hyph-gu.tex, hyph-pa.tex, + hyph-or.tex, hyph-ta.tex, hyph-te.tex, hyph-kn.tex and hyph-ml.tex 0.9.0 (2016-01-16) from + hyph-utf8, https://github.com/hyphenation/tex-hyphen. Copyright (C) 2016 Santhosh Thottingal. + Available under the MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this + software and associated documentation files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: The above copyright notice + and this permission notice shall be included in all copies or substantial portions of the + Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE + AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - German hyphenation patterns, embedded through the hypher crate (feature "german"): hyph-de-1996.tex 2024-02-28, German reformed orthography (2006), from hyph-utf8, https://github.com/typst/hypher/blob/d81cc506416bffef2a75ee2dc969d4b41bc36613/patterns/hyph-de-1996.tex. diff --git a/crates/compose/Cargo.toml b/crates/compose/Cargo.toml index 9f1f7dd39..48116b1fe 100644 --- a/crates/compose/Cargo.toml +++ b/crates/compose/Cargo.toml @@ -21,7 +21,7 @@ log = { workspace = true } regex = { workspace = true } fancy-regex = { workspace = true } miniz_oxide = "0.8" -hypher = { version = "0.1.8", default-features = false, features = ["alloc", "croatian", "dutch", "french", "german", "greek", "italian", "portuguese", "spanish", "norwegian", "danish"] } +hypher = { version = "0.1.8", default-features = false, features = ["alloc", "croatian", "dutch", "french", "german", "greek", "italian", "portuguese", "spanish", "norwegian", "danish", "hindi", "marathi", "bengali", "assamese", "gujarati", "panjabi", "oriya", "tamil", "telugu", "kannada", "malayalam"] } [dev-dependencies] designcraft-fonts = { workspace = true, features = ["testing"] } diff --git a/crates/compose/src/hyphen.rs b/crates/compose/src/hyphen.rs index 25f76097f..8046573f2 100644 --- a/crates/compose/src/hyphen.rs +++ b/crates/compose/src/hyphen.rs @@ -10,6 +10,10 @@ //! Flipo, Bernard Gaulle, Arthur Reutenauer), MIT; Italian is `hyph-it.tex` 4.9 from hyph-utf8 //! (Claudio Beccari), LPPL or MIT, taken under MIT. A word with an elision (`l'imprimerie`) //! hyphenates after its apostrophe. +//! Hindi, Marathi, Bengali, Assamese, Gujarati, Punjabi, Odia, Tamil, Telugu, Kannada and +//! Malayalam are Santhosh Thottingal's `hyph-{hi,mr,bn,as,gu,pa,or,ta,te,kn,ml}.tex` 0.9.0 from +//! hyph-utf8, MIT. Viramas, nuktas and the zero-width (non-)joiner count as word characters +//! ([`is_indic_joiner`]), so a word is not split at its conjuncts. //! German uses `hyph-de-1996.tex` (2024-02-28, Deutschsprachige Trennmustermannschaft, MIT): //! despite the filename, these are the reformed 2006 spelling patterns. Only the two 2006 reform //! names and German locale codes without a reform year or with 2006 use this set. Traditional, @@ -138,7 +142,21 @@ fn is_apostrophe(c: char) -> bool { } fn is_word_char(c: char) -> bool { - c.is_alphabetic() || is_apostrophe(c) + c.is_alphabetic() || is_apostrophe(c) || is_indic_joiner(c) +} + +/// Signs inside Indic words that Unicode doesn't class as alphabetic: each script's virama (which +/// joins consonants into conjuncts), its nukta, Malayalam's vertical and circular viramas, and the +/// zero-width joiner and non-joiner that select conjunct and chillu forms. Without them a word +/// such as विद्यार्थी splits at every conjunct into fragments too short to hyphenate. +fn is_indic_joiner(c: char) -> bool { + matches!( + c, + '\u{094D}' | '\u{09CD}' | '\u{0A4D}' | '\u{0ACD}' | '\u{0B4D}' | '\u{0BCD}' | '\u{0C4D}' | '\u{0CCD}' | '\u{0D4D}' // viramas + | '\u{0D3B}' | '\u{0D3C}' // Malayalam vertical and circular viramas + | '\u{093C}' | '\u{09BC}' | '\u{0A3C}' | '\u{0ABC}' | '\u{0B3C}' | '\u{0C3C}' | '\u{0CBC}' // nuktas + | '\u{200C}' | '\u{200D}' // ZWNJ, ZWJ + ) } /// Unrestricted break points of one lowercase core (letters and `'` only, no leading/trailing `'`). @@ -821,6 +839,64 @@ mod tests { } } + #[test] + fn indic_languages_use_the_hyph_utf8_patterns() { + for (name, code, lang) in [ + ("Hindi", "hi", hypher::Lang::Hindi), + ("Marathi", "mr", hypher::Lang::Marathi), + ("Bengali", "bn", hypher::Lang::Bengali), + ("Assamese", "as", hypher::Lang::Assamese), + ("Gujarati", "gu", hypher::Lang::Gujarati), + ("Punjabi", "pa", hypher::Lang::Panjabi), + ("Odia", "or", hypher::Lang::Oriya), + ("Oriya", "or_IN", hypher::Lang::Oriya), + ("Tamil", "ta", hypher::Lang::Tamil), + ("Telugu", "te", hypher::Lang::Telugu), + ("Kannada", "kn", hypher::Lang::Kannada), + ("Malayalam", "ml_IN", hypher::Lang::Malayalam), + ] { + assert_eq!(Lang::for_language(name), Some(Lang::Patterns(lang)), "{name}"); + assert_eq!(Lang::for_language(code), Some(Lang::Patterns(lang)), "{code}"); + } + assert_eq!(Lang::for_language("Sanskrit"), None, "no Sanskrit patterns are built in"); + } + + /// A virama joins consonants inside a word: the word is hyphenated whole, between syllables, + /// never inside a conjunct. Before viramas counted as word characters, विद्यार्थी was + /// hyphenated as the fragments वि, द, यार and थी, which gave it no break at all. + #[test] + fn indic_words_hyphenate_between_syllables_across_their_conjuncts() { + let lang = |name: &str| Lang::for_language(name).unwrap(); + assert_eq!(marked("विद्यार्थी", lang("Hindi")), "वि-द्या-र्थी"); + assert_eq!(marked("प्रधानमंत्री", lang("Hindi")), "प्र-धा-न-मं-त्री"); + assert_eq!(marked("महाराष्ट्र", lang("Marathi")), "महा-रा-ष्ट्र"); + assert_eq!(marked("വിദ്യാർത്ഥി", lang("Malayalam")), "വി-ദ്യാർ-ത്ഥി"); + assert_eq!(marked("തിരുവനന്തപുരം", lang("Malayalam")), "തി-രു-വ-ന-ന്ത-പു-രം"); + assert_eq!(marked("தொழில்நுட்பம்", lang("Tamil")), "தொ-ழில்-நுட்-பம்"); + // The default Before Last limit keeps the final য় with the ল before it. + assert_eq!(marked("বিশ্ববিদ্যালয়", lang("Bengali")), "বি-শ্ব-বি-দ্যা-লয়"); + assert_eq!(marked("విశ్వవిద్యాలయం", lang("Telugu")), "వి-శ్వ-వి-ద్యా-ల-యం"); + assert_eq!(marked("ವಿಶ್ವವಿದ್ಯಾಲಯ", lang("Kannada")), "ವಿ-ಶ್ವ-ವಿ-ದ್ಯಾ-ಲಯ"); + assert_eq!(marked("વિશ્વવિદ્યાલય", lang("Gujarati")), "વિ-શ્વ-વિ-દ્યા-લય"); + } + + /// A zero-width joiner or non-joiner (old-style Malayalam chillus, explicit half forms) and a + /// nukta stay inside the word they're in, so no break lands next to them. + #[test] + fn indic_joiners_and_nuktas_do_not_split_words() { + for c in ['\u{094D}', '\u{0D4D}', '\u{0BCD}', '\u{093C}', '\u{200C}', '\u{200D}'] { + assert!(is_word_char(c), "U+{:04X}", c as u32); + } + let hi = Lang::for_language("Hindi").unwrap(); + let (pts, _) = word_breaks_in("पढ़ना", hi); + assert!(!pts.contains(&2), "no break between ढ and its nukta: {pts:?}"); + assert!(pts.contains(&3), "पढ़-ना: {pts:?}"); + // Old chillu encoding (ന + virama + ZWJ): the word stays one word. + let ml = Lang::for_language("Malayalam").unwrap(); + let (pts, _) = word_breaks_in("അവന്\u{200D}റെ", ml); + assert!(pts.iter().all(|&p| !matches!("അവന്\u{200D}റെ".chars().nth(p), Some('\u{200D}' | '\u{0D4D}'))), "{pts:?}"); + } + #[test] fn norwegian_uses_the_hyph_no_patterns() { let nb = Lang::for_language("Norwegian: Bokmål").unwrap(); diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index df1736750..972184326 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -5965,13 +5965,18 @@ fn foreign_ranges( .collect() } -/// Leave out the hyphenation points after the glyphs that end at story bytes `at`. +/// Leave out the hyphenation points after the glyphs that end at story bytes `at`. A glyph +/// without bytes of its own (len 0) is part of the cluster before it and ends where that does. fn suppress_hyphens(hy: &mut [bool], glyphs: &[Glyph], at: &[usize]) { if at.is_empty() { return; } + let mut end = 0; for (h, g) in hy.iter_mut().zip(glyphs) { - if *h && at.contains(&(g.byte + g.len)) { + if g.len > 0 || end == 0 { + end = g.byte + g.len; + } + if *h && at.contains(&end) { *h = false; } } @@ -6095,13 +6100,17 @@ fn hyphenation_points( } }; if !pts.is_empty() { - // char index p → byte → the first glyph whose cluster ends at that byte. Both - // indexed once per word: looking each point up by scanning was quadratic in - // a long word's length. + // char index p → byte → the last glyph of the cluster that ends at that byte. + // A cluster's first glyph owns its bytes and the glyphs after it (len 0) are + // the rest of it: a Devanagari syllable such as वि is drawn as the ि sign + // before the व, so the break goes after the last of them, not between them. + // Both indexed once per word: looking each point up by scanning was quadratic + // in a long word's length. let char_bytes: Vec = word.char_indices().map(|(bi, _)| bi).collect(); let mut glyph_ending_at: HashMap = HashMap::new(); for k in (i..j).filter(|&k| glyphs[k].len > 0) { - glyph_ending_at.entry(glyphs[k].byte + glyphs[k].len).or_insert(k); + let last = (k + 1..j).take_while(|&m| glyphs[m].len == 0).last().unwrap_or(k); + glyph_ending_at.entry(glyphs[k].byte + glyphs[k].len).or_insert(last); } for &p in pts.iter() { let byte = a + char_bytes.get(p).copied().unwrap_or(word.len()); diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index 0a1f67184..fc031258b 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -5811,6 +5811,27 @@ fn croatian_text_gets_croatian_hyphenation() { } } +/// Hindi and Malayalam text set in its language hyphenates between syllables, never inside a +/// conjunct: each hyphenated line ends in a prefix the patterns allow. +#[test] +fn indian_language_text_hyphenates_between_syllables() { + for (language, word, prefixes) in [("Hindi", "विद्यार्थी", &["वि", "विद्या"][..]), ("Malayalam", "വിദ്യാർത്ഥി", &["വി", "വിദ്യാർ"][..])] + { + let text = [word; 8].join(" "); + let (mut d, sid, _) = doc_with(&text, Rect::new(36.0, 36.0, 76.0, 700.0), ParaAttrs::default()); + let st = d.story_mut(sid).unwrap(); + let n = st.len(); + st.format_chars(0..n, |f| f.over.language = Some(language.into())); + let cs = compose_story(&d, sid, &ComposeOptions::default()); + let breaks: Vec<&str> = all_lines(&cs).into_iter().filter(|l| l.hyphenated).map(|l| text[l.range.clone()].trim_end()).collect(); + assert!(!breaks.is_empty(), "{language} text is hyphenated"); + for b in breaks { + let tail = b.rsplit(' ').next().unwrap_or(b); + assert!(prefixes.contains(&tail), "{language}: {b:?} ends in a syllable prefix of {word}"); + } + } +} + #[test] fn cjk_aki_adds_space_without_scaling_outlines() { let (mut d, sid, _) = doc_with("AB", Rect::new(0.0, 0.0, 300.0, 100.0), ParaAttrs::default()); diff --git a/crates/doc/src/attrs/language.rs b/crates/doc/src/attrs/language.rs index a116bf754..e8b1ffe52 100644 --- a/crates/doc/src/attrs/language.rs +++ b/crates/doc/src/attrs/language.rs @@ -89,6 +89,17 @@ pub fn language_tag(language: &str) -> Option<&'static str> { "hebrew" => "he", "georgian" => "ka", "esperanto" => "eo", + "hindi" => "hi", + "marathi" => "mr", + "bengali" | "bangla" => "bn", + "assamese" => "as", + "gujarati" => "gu", + "punjabi" | "panjabi" => "pa", + "odia" | "oriya" => "or", + "tamil" => "ta", + "telugu" => "te", + "kannada" => "kn", + "malayalam" => "ml", _ => return None, }) } diff --git a/crates/doc/src/attrs/tests.rs b/crates/doc/src/attrs/tests.rs index 78c62b77b..fa6cf9649 100644 --- a/crates/doc/src/attrs/tests.rs +++ b/crates/doc/src/attrs/tests.rs @@ -94,6 +94,32 @@ fn language_names_map_to_bcp47_tags() { assert_eq!(language_tag(""), None); } +/// Indian languages by the names the Language menu and IDML use, so their text gets the font's +/// localized forms (Marathi ल and श) and hyphenates with its own patterns. +#[test] +fn indian_language_names_map_to_their_tags() { + for (name, tag) in [ + ("Hindi", "hi"), + ("Marathi", "mr"), + ("Bengali", "bn"), + ("Bangla", "bn"), + ("Assamese", "as"), + ("Gujarati", "gu"), + ("Punjabi", "pa"), + ("Panjabi", "pa"), + ("Odia", "or"), + ("Oriya", "or"), + ("Tamil", "ta"), + ("Telugu", "te"), + ("Kannada", "kn"), + ("Malayalam", "ml"), + ("$ID/Malayalam", "ml"), + ] { + assert_eq!(language_tag(name), Some(tag), "{name}"); + } + assert_eq!(language_tag("hi_IN"), Some("hi-IN")); +} + #[test] fn chinese_japanese_and_korean_names_map_in_their_common_spellings() { for (name, tag) in [ diff --git a/crates/ui-egui/src/panels/properties/text.rs b/crates/ui-egui/src/panels/properties/text.rs index f3a727b36..4eacfff8d 100644 --- a/crates/ui-egui/src/panels/properties/text.rs +++ b/crates/ui-egui/src/panels/properties/text.rs @@ -721,6 +721,17 @@ pub(crate) const LANGUAGES: &[&str] = &[ "Korean", "Arabic", "Hebrew", + "Hindi", + "Marathi", + "Bengali", + "Assamese", + "Gujarati", + "Punjabi", + "Odia", + "Tamil", + "Telugu", + "Kannada", + "Malayalam", "Esperanto", ];