mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
proof: keep zero-width joiners inside Indic conjuncts
A ZWJ or ZWNJ after a virama (choosing how a conjunct is drawn) ended the letter run, so a Gujarati word with one was hyphenated as two short parts and got no break. In a language with Indic rules a joiner is now part of the word, and the akshara rules keep it inside its conjunct and never break next to it. Other languages still end a run at a joiner. Follow-up to #3901 (CodeRabbit review). Part of storytold/vectorcraft#4947. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
078f2cb357
commit
b42509ee63
3 files changed
+47
-8
No files matched your search
@@ -134,6 +134,13 @@ fn is_word_char(c: char) -> bool {
|
||||
c.is_alphabetic() || is_apostrophe(c) || unicode_normalization::char::is_combining_mark(c)
|
||||
}
|
||||
|
||||
/// [`is_word_char`] in `lang`. In a language with Indic rules a zero-width joiner or non-joiner
|
||||
/// is part of the word too: it shapes a conjunct inside an akshara ([`crate::indic`]). In any
|
||||
/// other language it still ends a letter run (Persian's ZWNJ between a word's parts).
|
||||
fn is_word_char_in(c: char, lang: ProofLang) -> bool {
|
||||
is_word_char(c) || (crate::indic::is_joiner(c) && crate::indic::Script::of(lang.built_in()).is_some())
|
||||
}
|
||||
|
||||
/// Unrestricted break points of one lowercase core (letters and `'` only, no leading/trailing `'`).
|
||||
fn core_breaks(core: &[char], lang: ProofLang) -> (Vec<usize>, Source) {
|
||||
let n = core.len();
|
||||
@@ -321,12 +328,12 @@ pub fn word_breaks_in(word: &str, lang: ProofLang) -> (Vec<usize>, Source) {
|
||||
let mut src = Source::None;
|
||||
let mut i = 0;
|
||||
while i < chars.len() {
|
||||
if !is_word_char(chars[i]) {
|
||||
if !is_word_char_in(chars[i], lang) {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let mut j = i;
|
||||
while j < chars.len() && is_word_char(chars[j]) {
|
||||
while j < chars.len() && is_word_char_in(chars[j], lang) {
|
||||
j += 1;
|
||||
}
|
||||
let (mut a, mut b) = (i, j);
|
||||
@@ -380,7 +387,7 @@ pub fn hyphen_points_in(word: &str, lim: &Limits, lang: ProofLang) -> Vec<usize>
|
||||
let mut i = 0;
|
||||
while i < chars.len() {
|
||||
let mut j = i;
|
||||
while chars.get(j).is_some_and(|c| is_word_char(*c)) {
|
||||
while chars.get(j).is_some_and(|c| is_word_char_in(*c, lang)) {
|
||||
j += 1;
|
||||
}
|
||||
let (mut a, mut b) = (i, j);
|
||||
|
||||
@@ -208,4 +208,6 @@ fn gujarati_breaks_between_aksharas_within_the_limits() {
|
||||
assert_eq!(gu("અમદાવાદ"), "અમ-દા-વાદ");
|
||||
assert_eq!(gu("«ગુજરાતી»"), "«ગુ-જ-રા-તી»", "punctuation around the word");
|
||||
assert_eq!(gu("Typography"), hyphenate_word_in("Typography", &lim, ProofLang::En));
|
||||
// A joiner in a conjunct doesn't cut the word in two (each part then too short to break).
|
||||
assert_eq!(gu("પુસ્\u{200D}તક"), "પુ-સ્\u{200D}તક");
|
||||
}
|
||||
@@ -7,7 +7,9 @@
|
||||
//! independent vowel that does not follow a virama: `ગુ-જ-રા-તી`, `વિ-દ્યા-ર્થી`, `સં-સ્કૃત`,
|
||||
//! `हि-न्दी`. A vowel sign, a mark or the second consonant of a conjunct never starts a line.
|
||||
//! Nor does a dead consonant — one ending in a virama with no consonant after it (`વાક્`) — since
|
||||
//! it has no vowel of its own and is the end of the syllable before.
|
||||
//! it has no vowel of its own and is the end of the syllable before. A zero-width joiner or
|
||||
//! non-joiner after a virama (`સ્` U+200D `ત`) only chooses how the conjunct is drawn: it stays
|
||||
//! inside the akshara, and no break goes next to a joiner.
|
||||
//!
|
||||
//! 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
|
||||
@@ -44,6 +46,14 @@ enum Kind {
|
||||
/// A vowel sign, nukta, anusvara, candrabindu, visarga or other mark (and the avagraha):
|
||||
/// belongs to the akshara before it.
|
||||
Mark,
|
||||
/// A zero-width joiner or non-joiner: shapes the conjunct around it, in the same akshara.
|
||||
Joiner,
|
||||
}
|
||||
|
||||
/// Is `c` a zero-width joiner or non-joiner (U+200D, U+200C)? Inside an Indic word it shapes a
|
||||
/// conjunct, so it is part of the word (as in Unicode's word boundaries, UAX #29).
|
||||
pub fn is_joiner(c: char) -> bool {
|
||||
matches!(c, '\u{200C}' | '\u{200D}')
|
||||
}
|
||||
|
||||
impl Script {
|
||||
@@ -66,6 +76,9 @@ impl Script {
|
||||
/// What `c` is in this script, or `None` for a character outside the script's letters and
|
||||
/// marks (another script, a digit, a danda, `ॐ`, the abbreviation sign…).
|
||||
fn kind(self, c: char) -> Option<Kind> {
|
||||
if is_joiner(c) {
|
||||
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 {
|
||||
@@ -94,7 +107,8 @@ impl Script {
|
||||
})
|
||||
}
|
||||
|
||||
/// Is `c` one of this script's letters or marks (the characters [`points`] splits)?
|
||||
/// Is `c` one of this script's letters or marks, or a joiner (the characters [`points`]
|
||||
/// splits)?
|
||||
pub fn is_letter(self, c: char) -> bool {
|
||||
self.kind(c).is_some()
|
||||
}
|
||||
@@ -109,7 +123,7 @@ pub fn points(script: Script, word: &[char]) -> Vec<usize> {
|
||||
};
|
||||
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 (`ક્`, `ન્ત્`) has none.
|
||||
// consonant cluster ending in a virama (`ક્`, `ન્ત્`, with or without a joiner) has none.
|
||||
let live = |i: usize| {
|
||||
let mut j = i;
|
||||
loop {
|
||||
@@ -117,19 +131,24 @@ pub fn points(script: Script, word: &[char]) -> Vec<usize> {
|
||||
return true;
|
||||
}
|
||||
j += 1;
|
||||
while at(j) == Some(Kind::Mark) {
|
||||
while matches!(at(j), Some(Kind::Mark | Kind::Joiner)) {
|
||||
j += 1;
|
||||
}
|
||||
if at(j) != Some(Kind::Virama) {
|
||||
return true;
|
||||
}
|
||||
j += 1;
|
||||
while at(j) == Some(Kind::Joiner) {
|
||||
j += 1;
|
||||
}
|
||||
if at(j) != Some(Kind::Consonant) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
(1..word.len()).filter(|&i| matches!(at(i), Some(Kind::Vowel | Kind::Consonant)) && at(i - 1) != Some(Kind::Virama) && live(i)).collect()
|
||||
(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()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -213,6 +232,17 @@ mod tests {
|
||||
assert_eq!(hi("ગુજરાતી"), "ગુજરાતી", "Gujarati is not Devanagari");
|
||||
}
|
||||
|
||||
/// CodeRabbit on craft-libs#3901: a joiner after a virama stays inside its conjunct, the break
|
||||
/// before the conjunct remains, and no break goes next to a joiner.
|
||||
#[test]
|
||||
fn joiners_stay_inside_the_akshara() {
|
||||
assert_eq!(gu("પુસ્\u{200D}તક"), "પુ-સ્\u{200D}ત-ક");
|
||||
assert_eq!(gu("પુસ્\u{200C}તક"), "પુ-સ્\u{200C}ત-ક");
|
||||
assert_eq!(gu("વાક્\u{200D}"), "વાક્\u{200D}", "still a dead consonant");
|
||||
assert_eq!(gu("ક\u{200D}ખ"), "ક\u{200D}ખ", "a joiner outside a conjunct");
|
||||
assert_eq!(hi("क्\u{200D}षमा"), "क्\u{200D}ष-मा");
|
||||
}
|
||||
|
||||
/// Only Gujarati is mapped to a script so far.
|
||||
#[test]
|
||||
fn languages_map_to_their_script() {
|
||||
|
||||
Reference in new issue
Block a user