From f0f80c016a0ca34a4041c432e57a75089aaaa0d0 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Mon, 2 Nov 2026 05:24:32 -0500 Subject: [PATCH 1/8] Load Actions: Mac Roman step names with accented letters or a degree sign read as Mac Roman (#10774) Co-Authored-By: Claude Fable 5.1 --- ROADMAP.md | 1 + crates/psd/src/codepage.rs | 115 +++++++++++++++++++++++++++++++------ 2 files changed, 99 insertions(+), 17 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 357be3920a3..3964efc6896 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -122,6 +122,7 @@ Detail and the next releases: [docs/roadmap.md](docs/roadmap.md). | Date | Entry | |---|---| +| 2026-11-02 | Load Actions: Mac Roman step names with é ä ö ü Ä or ° read as Mac Roman instead of windows-1252 or Cyrillic (#10774): windows-1252's rare 0x80–0x9F letters and a lone Cyrillic letter in a Latin word no longer count as common text, the degree sign does, and a Latin hint rules windows-1251 out | | 2026-10-17 | Object Finder classifies background by boundary coverage, retaining large enclosed foregrounds and rejecting surrounding backdrop (#6548); learned segmentation remains open (#2788) | | 2026-10-10 | Full re-measure against Photoshop 2026 27.11.0 (installed bundle inspected): breadth ~76%, ready ~45%, alpha. Progress docs reorganized (`parity.md` → `parity-checklist.md`, `parity-estimate.md` merged into `target-app-parity.md`). 0.6.0 released. | | 2026-10-09 | Remove tool, Rotate View, crop rotation; OpenRaster and Paint.NET; 39 Affinity documents in the corpus | diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index 75e0f7c468a..98769c78c3e 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -11,10 +11,15 @@ //! Roman). A candidate that cannot decode every name without errors is dropped. //! 2. Each survivor is scored by the share of non-ASCII bytes that fall in its common-text //! ranges (GB2312 hanzi, JIS level-1 kanji and kana, Big5 level 1, KS X 1001 Hangul, -//! letters, and each page's punctuation). The highest share wins. +//! letters, each page's punctuation, and the degree sign). The highest share wins. Two +//! single-byte readings don't count: the letters windows-1252 alone has at 0x80–0x9F (Š š Ž +//! ž Ÿ ƒ ˆ), where Mac Roman keeps its accented lowercase (Mac "Opacité" is 1252 +//! "OpacitŽ"), and a windows-1251 Cyrillic letter in a name whose letters are mostly ASCII +//! (Mac "Höhe" is 1251 "Hшhe"; "CMYKканал" and "Копiя" still count). //! 3. A tie is broken by the script of a hint (text the file stores as UTF-16, so it decodes //! fine): kana → Shift_JIS; Hangul → EUC-KR; Cyrillic → windows-1251; Han → GBK, then -//! Big5, then Shift_JIS; Latin letters → windows-1252, then Mac Roman. +//! Big5, then Shift_JIS; Latin letters → windows-1252, then Mac Roman. A hint with accented +//! Latin letters also rules windows-1251 out (step 1). //! 4. Still tied: windows-1252, flagged [`Guess::uncertain`]. //! 5. Mac Roman must also beat windows-1252 when the Latin-1 symbols that are no letter in //! Mac Roman (© ° « ± …) count as common text (1252 "Größe »" is Mac Roman "Grˆfle ª"). @@ -74,8 +79,10 @@ pub(crate) fn guess(legacy: &[&[u8]], hint: &str) -> Guess { } let mut best: Vec<&'static Encoding> = Vec::new(); let mut best_score = (0u64, 1u64); + // Names from a Photoshop whose UTF-16 text has accented Latin letters are not Cyrillic. + let latin_hint = hinted(hint).first() == Some(&WINDOWS_1252); for enc in candidates() { - if !sample.iter().all(|(name, truncated)| decodes_cleanly(enc, name, *truncated)) { + if (latin_hint && enc == WINDOWS_1251) || !sample.iter().all(|(name, truncated)| decodes_cleanly(enc, name, *truncated)) { continue; } let score = sample.iter().fold((0u64, 0u64), |(c, h), (name, _)| { @@ -196,9 +203,19 @@ fn decodes_cleanly(enc: &'static Encoding, bytes: &[u8], truncated: bool) -> boo /// many non-ASCII bytes it has. fn score(enc: &'static Encoding, name: &[u8]) -> (u64, u64) { if enc == WINDOWS_1252 || enc == WINDOWS_1251 || enc == MACINTOSH { - // Letters and typographic punctuation are common, as the double-byte pages' punctuation - // rows are. - return single_byte_score(enc, name, |ch| ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch)); + // Letters, typographic punctuation and the degree sign ("Rotation 90°") are common, as + // the double-byte pages' punctuation rows are; not the letters windows-1252 alone has at + // 0x80–0x9F, where Mac Roman keeps its accented lowercase (Mac "Opacité" is 1252 + // "OpacitŽ"), nor a Cyrillic letter in a name whose letters are mostly ASCII (Mac "Höhe" + // is 1251 "Hшhe"; "CMYKканал" and Ukrainian "Копiя" keep counting). + let decoded = decode(name, enc); + let cyrillic_name = decoded.chars().filter(|c| is_cyrillic(*c)).count() >= decoded.chars().filter(|c| c.is_ascii_alphabetic()).count(); + let common = |ch: char| { + let letter = + ch.is_alphabetic() && !(enc == WINDOWS_1252 && RARE_1252_LETTERS.contains(&ch)) && !(enc == WINDOWS_1251 && is_cyrillic(ch) && !cyrillic_name); + letter || TYPOGRAPHIC.contains(&ch) || ch == '°' + }; + return single_byte_score(enc, name, common); } let (mut common, mut high) = (0u64, 0u64); let mut i = 0usize; @@ -233,11 +250,10 @@ fn score(enc: &'static Encoding, name: &[u8]) -> (u64, u64) { /// [`score`] for a single-byte page, where one byte is one character and a non-ASCII byte is a /// non-ASCII character: `common` decides which of them count. /// -/// Known limitation: windows-1251 has Cyrillic letters (І і Ј д) where windows-1252 has ² ³ £ ä, -/// so a windows-1252 name like "Fläche m²" or "Preis £5" scores higher as windows-1251 ("Flдche -/// mІ", "Preis Ј5"). Discounting a Cyrillic letter glued to a Latin word would fix those but -/// break ordinary Russian names such as "CMYKканал" or Ukrainian "Копiя" (Latin i), which turn a -/// whole set into windows-1252. +/// windows-1251 has Cyrillic letters (І і Ј д) where windows-1252 has ² ³ £ ä, so a windows-1252 +/// name like "Fläche m²" or "Preis £5" would score higher as windows-1251 ("Flдche mІ", "Preis +/// Ј5"); [`score`] counts a Cyrillic letter only in a name whose letters are mostly Cyrillic, +/// which keeps ordinary Russian names such as "CMYKканал" or Ukrainian "Копiя" (Latin i). fn single_byte_score(enc: &'static Encoding, name: &[u8], common: impl Fn(char) -> bool) -> (u64, u64) { decode(name, enc).chars().filter(|c| !c.is_ascii()).fold((0, 0), |(c, h), ch| (c.saturating_add(u64::from(common(ch))), h.saturating_add(1))) } @@ -246,16 +262,28 @@ fn single_byte_score(enc: &'static Encoding, name: &[u8], common: impl Fn(char) /// (but not those windows-1252 has only at 0x80–0x9F, where Mac Roman keeps its accented /// lowercase: Mac "Größe" is 1252 "Grš§e"), typographic punctuation and [`LATIN_SYMBOLS`]. fn is_latin_common(ch: char) -> bool { - (ch.is_alphabetic() && !['Š', 'š', 'Ž', 'ž', 'Œ', 'œ', 'Ÿ', 'ƒ', 'ˆ'].contains(&ch)) || TYPOGRAPHIC.contains(&ch) || LATIN_SYMBOLS.contains(&ch) + (ch.is_alphabetic() && !RARE_1252_LETTERS.contains(&ch) && ch != 'Œ' && ch != 'œ') || TYPOGRAPHIC.contains(&ch) || LATIN_SYMBOLS.contains(&ch) } -/// Latin-1 symbols people type in names (windows-1252 0xA1–0xB7) whose byte is not a letter in +/// The letters windows-1252 has at 0x80–0x9F and Mac Roman doesn't: there Mac Roman keeps its +/// accented lowercase (ä é ö ü ...), so these never decide against it. Œ and œ (0x8C, 0x9C: Mac +/// å, ú) are real French letters and keep counting in [`score`]; [`is_latin_common`] leaves them +/// out as well. +const RARE_1252_LETTERS: [char; 7] = ['Š', 'š', 'Ž', 'ž', 'Ÿ', 'ƒ', 'ˆ']; + +/// A Cyrillic letter (windows-1251's 0xC0–0xFF and the letters among 0x80–0xBF). +fn is_cyrillic(ch: char) -> bool { + ('\u{0400}'..='\u{04FF}').contains(&ch) && ch.is_alphabetic() +} + +/// Latin-1 symbols people type in names (windows-1252 0xA2–0xB7) whose byte is not a letter in /// Mac Roman. Left out because Mac Roman has a letter there: § ® ¹ » ¼ ½ ¾ ¿ (ß Æ π ª º Ω æ ø, /// as in Mac "Weiß", "Størrelse", "Æble"), € and ™ (0x80, 0x99: Ä, ô), and ¥ (0xA5, Mac Roman's -/// bullet in [`TYPOGRAPHIC`]: Mac "Ebene • Kopie"). So a windows-1252 name whose only non-ASCII -/// character is one of those ("Logo ®", "¿Tamaño?", "Preis ¥100") still reads as Mac Roman -/// ("Logo Æ"). -const LATIN_SYMBOLS: [char; 11] = ['¡', '¢', '£', '©', '«', '°', '±', '²', '³', '¶', '·']; +/// bullet in [`TYPOGRAPHIC`]: Mac "Ebene • Kopie"); and ¡ (0xA1), where Mac Roman has its degree +/// sign, the commoner of the two in names (Mac "Rotation 90°"). So a windows-1252 name whose only +/// non-ASCII character is one of those ("Logo ®", "¿Tamaño?", "Preis ¥100", "¡Hola!") still +/// reads as Mac Roman ("Logo Æ"). +const LATIN_SYMBOLS: [char; 10] = ['¢', '£', '©', '«', '°', '±', '²', '³', '¶', '·']; /// Quotes, dashes, ellipsis and bullet: windows-1252 and windows-1251 share them at 0x82–0x97; /// Mac Roman has them too. @@ -419,6 +447,59 @@ mod tests { } } + #[test] + fn mac_roman_accents_on_1252_letter_bytes_stay_mac_roman() { + // #10774: Mac Roman é ä ö ü Ä (0x8E 0x8A 0x9A 0x9F 0x80) are windows-1252 Ž Š š Ÿ € and + // windows-1251 Ћ Љ ш џ Ђ. The rare 1252 letters don't count, and a Cyrillic letter in a + // Latin word doesn't either, so Mac Roman wins with a Latin hint and without one. + let cases: [(&[&str], &str); 6] = [ + (&["Opacité 50%"], "Définir"), + (&["Opacité 50%"], "Set 1"), + (&["Sélection", "Créer un calque", "Luminosité/Contraste"], "Définir"), + (&["Höhe", "Grün", "Bär"], "Größe ändern"), + (&["Höhe"], "Set 1"), + (&["Ändern"], "Set 1"), + ]; + for (words, hint) in cases { + let g = guess_words(MACINTOSH, words, hint); + assert_eq!(g, Guess { encoding: MACINTOSH, uncertain: false }, "{words:?} with hint {hint:?}"); + let bytes = encoded(MACINTOSH, words); + for (b, w) in bytes.iter().zip(words) { + assert_eq!(decode(b, g.encoding), *w); + } + } + } + + #[test] + fn mac_roman_degree_sign_is_not_cyrillic() { + // #10774: Mac Roman ° (0xA1) is windows-1251 Ў and windows-1252 ¡; the degree sign is + // common text, a lone Cyrillic letter among Latin ones is not. + let cases: [(&[&str], &str); 4] = + [(&["Rotation 90°"], "Définir"), (&["Rotation 90°"], "Set 1"), (&["Rotation 90°"], "Größe ändern"), (&["Rotation 90°", "Arrière-plan"], "Définir")]; + for (words, hint) in cases { + let g = guess_words(MACINTOSH, words, hint); + assert_eq!(g, Guess { encoding: MACINTOSH, uncertain: false }, "{words:?} with hint {hint:?}"); + } + } + + #[test] + fn the_same_names_in_windows_1252_stay_windows_1252() { + for words in [&["Opacité 50%"][..], &["Höhe", "Grün", "Bär"], &["Rotation 90°"], &["Ändern"], &["Fläche m²"], &["Preis £5"]] { + assert_eq!(guess_words(WINDOWS_1252, words, "Set 1").encoding, WINDOWS_1252, "{words:?}"); + assert_eq!(guess_words(WINDOWS_1252, words, "Größe ändern"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); + } + } + + #[test] + fn cyrillic_names_with_latin_words_still_read_as_1251() { + // Mostly Cyrillic letters count; a Latin brand name or a Latin i in Ukrainian doesn't + // turn the set into something else. + let g = guess_words(WINDOWS_1251, &["CMYKканал", "Копiя", "Слой 1"], "Набор 1"); + assert_eq!(g, Guess { encoding: WINDOWS_1251, uncertain: false }); + let bytes = encoded(WINDOWS_1251, &["CMYKканал"]); + assert_eq!(decode(&bytes[0], g.encoding), "CMYKканал"); + } + #[test] fn symbols_on_mac_roman_letters_stay_ambiguous() { // Known limitation: 1252 "Logo ®" is Mac Roman "Logo Æ", and ® is not counted for From 80ba6fc873f535039d716ca4109fd26b0ab20861 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Mon, 2 Nov 2026 06:05:50 -0500 Subject: [PATCH 2/8] =?UTF-8?q?Load=20Actions:=20Cyrillic=20words=20and=20?= =?UTF-8?q?word-initial=20=C5=A0/=C5=BD=20count=20by=20their=20position,?= =?UTF-8?q?=20and=20a=20Latin=20hint=20no=20longer=20rules=20windows-1251?= =?UTF-8?q?=20out?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A Cyrillic step name in a Latin-hinted set (Ёлка in a French set), a Cyrillic word beside a Latin one (Layer Слой) and a windows-1252 name starting with a capital Š or Ž (Škoda) all read as before; the Mac Roman cases of #10774 still read as Mac Roman. Tests for each. Co-Authored-By: Claude Fable 5.1 --- crates/psd/src/codepage.rs | 87 +++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 19 deletions(-) diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index 98769c78c3e..7ba6b6dc67c 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -14,12 +14,13 @@ //! letters, each page's punctuation, and the degree sign). The highest share wins. Two //! single-byte readings don't count: the letters windows-1252 alone has at 0x80–0x9F (Š š Ž //! ž Ÿ ƒ ˆ), where Mac Roman keeps its accented lowercase (Mac "Opacité" is 1252 -//! "OpacitŽ"), and a windows-1251 Cyrillic letter in a name whose letters are mostly ASCII -//! (Mac "Höhe" is 1251 "Hшhe"; "CMYKканал" and "Копiя" still count). +//! "OpacitŽ"), unless a capital one starts a word ("Škoda"); and a windows-1251 Cyrillic +//! letter glued into an ASCII word (Mac "Höhe" is 1251 "Hшhe"), unless the name's letters +//! are mostly Cyrillic ("CMYKканал", "Копiя") or the letter is in a Cyrillic word of its own +//! ("Layer Слой"). //! 3. A tie is broken by the script of a hint (text the file stores as UTF-16, so it decodes //! fine): kana → Shift_JIS; Hangul → EUC-KR; Cyrillic → windows-1251; Han → GBK, then -//! Big5, then Shift_JIS; Latin letters → windows-1252, then Mac Roman. A hint with accented -//! Latin letters also rules windows-1251 out (step 1). +//! Big5, then Shift_JIS; Latin letters → windows-1252, then Mac Roman. //! 4. Still tied: windows-1252, flagged [`Guess::uncertain`]. //! 5. Mac Roman must also beat windows-1252 when the Latin-1 symbols that are no letter in //! Mac Roman (© ° « ± …) count as common text (1252 "Größe »" is Mac Roman "Grˆfle ª"). @@ -79,10 +80,8 @@ pub(crate) fn guess(legacy: &[&[u8]], hint: &str) -> Guess { } let mut best: Vec<&'static Encoding> = Vec::new(); let mut best_score = (0u64, 1u64); - // Names from a Photoshop whose UTF-16 text has accented Latin letters are not Cyrillic. - let latin_hint = hinted(hint).first() == Some(&WINDOWS_1252); for enc in candidates() { - if (latin_hint && enc == WINDOWS_1251) || !sample.iter().all(|(name, truncated)| decodes_cleanly(enc, name, *truncated)) { + if !sample.iter().all(|(name, truncated)| decodes_cleanly(enc, name, *truncated)) { continue; } let score = sample.iter().fold((0u64, 0u64), |(c, h), (name, _)| { @@ -204,18 +203,31 @@ fn decodes_cleanly(enc: &'static Encoding, bytes: &[u8], truncated: bool) -> boo fn score(enc: &'static Encoding, name: &[u8]) -> (u64, u64) { if enc == WINDOWS_1252 || enc == WINDOWS_1251 || enc == MACINTOSH { // Letters, typographic punctuation and the degree sign ("Rotation 90°") are common, as - // the double-byte pages' punctuation rows are; not the letters windows-1252 alone has at - // 0x80–0x9F, where Mac Roman keeps its accented lowercase (Mac "Opacité" is 1252 - // "OpacitŽ"), nor a Cyrillic letter in a name whose letters are mostly ASCII (Mac "Höhe" - // is 1251 "Hшhe"; "CMYKканал" and Ukrainian "Копiя" keep counting). - let decoded = decode(name, enc); - let cyrillic_name = decoded.chars().filter(|c| is_cyrillic(*c)).count() >= decoded.chars().filter(|c| c.is_ascii_alphabetic()).count(); - let common = |ch: char| { - let letter = - ch.is_alphabetic() && !(enc == WINDOWS_1252 && RARE_1252_LETTERS.contains(&ch)) && !(enc == WINDOWS_1251 && is_cyrillic(ch) && !cyrillic_name); - letter || TYPOGRAPHIC.contains(&ch) || ch == '°' - }; - return single_byte_score(enc, name, common); + // the double-byte pages' punctuation rows are, with two readings held back: the letters + // windows-1252 alone has at 0x80–0x9F, where Mac Roman keeps its accented lowercase (Mac + // "Opacité" is 1252 "OpacitŽ"), unless a capital one starts a word ("Škoda"); and a + // windows-1251 Cyrillic letter glued into an ASCII word (Mac "Höhe" is 1251 "Hшhe"), + // unless the name's letters are mostly Cyrillic ("CMYKканал", Ukrainian "Копiя") or the + // letter is in a Cyrillic word of its own ("Layer Слой"). + let chars: Vec = decode(name, enc).chars().collect(); + let cyrillic_name = chars.iter().filter(|c| is_cyrillic(**c)).count() >= chars.iter().filter(|c| c.is_ascii_alphabetic()).count(); + let (mut common, mut high) = (0u64, 0u64); + for (i, &ch) in chars.iter().enumerate() { + if ch.is_ascii() { + continue; + } + high += 1; + let counts = if enc == WINDOWS_1252 && RARE_1252_LETTERS.contains(&ch) { + let after_ascii = i.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); + ch.is_uppercase() && !after_ascii && chars.get(i + 1).is_some_and(|c| c.is_ascii_lowercase()) + } else if enc == WINDOWS_1251 && is_cyrillic(ch) { + cyrillic_name || cyrillic_word(&chars, i) + } else { + ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' + }; + common += u64::from(counts); + } + return (common, high); } let (mut common, mut high) = (0u64, 0u64); let mut i = 0usize; @@ -276,6 +288,21 @@ fn is_cyrillic(ch: char) -> bool { ('\u{0400}'..='\u{04FF}').contains(&ch) && ch.is_alphabetic() } +/// Whether the Cyrillic letter at `i` is in a Cyrillic word of its own: a run of two or more, +/// not glued to an ASCII letter or digit ("Слой" beside "Layer"; not "OpacitЋ" or "90Ў"). +fn cyrillic_word(chars: &[char], i: usize) -> bool { + let mut start = i; + while start > 0 && chars.get(start - 1).is_some_and(|c| is_cyrillic(*c)) { + start -= 1; + } + let mut end = i + 1; + while chars.get(end).is_some_and(|c| is_cyrillic(*c)) { + end += 1; + } + let glued = |k: Option| k.and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); + end - start >= 2 && !glued(start.checked_sub(1)) && !glued(Some(end)) +} + /// Latin-1 symbols people type in names (windows-1252 0xA2–0xB7) whose byte is not a letter in /// Mac Roman. Left out because Mac Roman has a letter there: § ® ¹ » ¼ ½ ¾ ¿ (ß Æ π ª º Ω æ ø, /// as in Mac "Weiß", "Størrelse", "Æble"), € and ™ (0x80, 0x99: Ä, ô), and ¥ (0xA5, Mac Roman's @@ -490,6 +517,28 @@ mod tests { } } + #[test] + fn cyrillic_names_in_latin_hinted_files_and_cyrillic_words_beside_latin_ones_stay_1251() { + // A Cyrillic step name in a set with a French name reads as it did; a Cyrillic word beside + // a Latin one counts as Cyrillic, and the hint settles the tie with windows-1252's letters. + assert_eq!(guess_words(WINDOWS_1251, &["Ёлка"], "Définir"), Guess { encoding: WINDOWS_1251, uncertain: false }); + assert_eq!(guess_words(WINDOWS_1251, &["Layer Слой"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); + assert_eq!(guess_words(WINDOWS_1251, &["Layer Слой", "Ёлка"], "Set 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); + // Alone and without a hint, "Layer Слой" is as ambiguous as before (every letter is one in + // windows-1252 too): the flagged fallback. + assert_eq!(guess_words(WINDOWS_1251, &["Layer Слой"], "Set 1"), Guess { encoding: WINDOWS_1252, uncertain: true }); + } + + #[test] + fn a_capital_rare_letter_starting_a_word_keeps_windows_1252() { + // windows-1252 Š Ž Ÿ (0x8A 0x8E 0x9F) are Mac Roman ä é ü: a capital one starting a word + // ("Škoda") counts, an accent inside a word (Mac "Opacité" as "OpacitŽ") does not. + for words in [&["Škoda"][..], &["Žaba"], &["Šibenik", "Žaba"]] { + assert_eq!(guess_words(WINDOWS_1252, words, "Größe ändern"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); + assert_eq!(guess_words(WINDOWS_1252, words, "Set 1").encoding, WINDOWS_1252, "{words:?}"); + } + } + #[test] fn cyrillic_names_with_latin_words_still_read_as_1251() { // Mostly Cyrillic letters count; a Latin brand name or a Latin i in Ukrainian doesn't From d75b2ab512aa89ed84b7056fe40a78633617913e Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Mon, 2 Nov 2026 07:24:05 -0500 Subject: [PATCH 3/8] Load Actions: the file's own alphabet settles a word-initial capital, and a digit doesn't glue a Cyrillic word MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A rare windows-1252 capital starting a word counts unless the file's Unicode text writes the Mac Roman letter on that byte and never this one (état, über); Слой1 reads as a Cyrillic word. Tests. Co-Authored-By: Claude Fable 5.1 --- crates/psd/src/codepage.rs | 51 ++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index 7ba6b6dc67c..60251ee223b 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -14,10 +14,11 @@ //! letters, each page's punctuation, and the degree sign). The highest share wins. Two //! single-byte readings don't count: the letters windows-1252 alone has at 0x80–0x9F (Š š Ž //! ž Ÿ ƒ ˆ), where Mac Roman keeps its accented lowercase (Mac "Opacité" is 1252 -//! "OpacitŽ"), unless a capital one starts a word ("Škoda"); and a windows-1251 Cyrillic -//! letter glued into an ASCII word (Mac "Höhe" is 1251 "Hшhe"), unless the name's letters -//! are mostly Cyrillic ("CMYKканал", "Копiя") or the letter is in a Cyrillic word of its own -//! ("Layer Слой"). +//! "OpacitŽ"), unless a capital one starts a word ("Škoda") in a file whose own Unicode text +//! doesn't write the Mac Roman letter on that byte instead (a French "état", a German "über"); +//! and a windows-1251 Cyrillic letter glued into an ASCII word (Mac "Höhe" is 1251 "Hшhe"), +//! unless the name's letters are mostly Cyrillic ("CMYKканал", "Копiя") or the letter is in a +//! Cyrillic word of its own ("Layer Слой", "Слой1"). //! 3. A tie is broken by the script of a hint (text the file stores as UTF-16, so it decodes //! fine): kana → Shift_JIS; Hangul → EUC-KR; Cyrillic → windows-1251; Han → GBK, then //! Big5, then Shift_JIS; Latin letters → windows-1252, then Mac Roman. @@ -78,6 +79,8 @@ pub(crate) fn guess(legacy: &[&[u8]], hint: &str) -> Guess { if sample.is_empty() { return Guess { encoding: UTF_8, uncertain: false }; } + // The accented letters the file's own Unicode text uses: what its writer's language writes. + let hint_letters: std::collections::BTreeSet = hint.chars().filter(|c| !c.is_ascii() && c.is_alphabetic()).flat_map(char::to_lowercase).collect(); let mut best: Vec<&'static Encoding> = Vec::new(); let mut best_score = (0u64, 1u64); for enc in candidates() { @@ -85,7 +88,7 @@ pub(crate) fn guess(legacy: &[&[u8]], hint: &str) -> Guess { continue; } let score = sample.iter().fold((0u64, 0u64), |(c, h), (name, _)| { - let (nc, nh) = score(enc, name); + let (nc, nh) = score(enc, name, &hint_letters); (c.saturating_add(nc), h.saturating_add(nh)) }); let score = if score.1 == 0 { (0, 1) } else { score }; @@ -143,7 +146,7 @@ pub(crate) fn reads_as_common(name: &[u8], enc: &'static Encoding) -> bool { if ![SHIFT_JIS, GBK, BIG5, EUC_KR].contains(&enc) { return false; } - let (common, high) = score(enc, name); + let (common, high) = score(enc, name, &std::collections::BTreeSet::new()); high > 0 && common == high && decodes_cleanly(enc, name, false) } @@ -199,13 +202,15 @@ fn decodes_cleanly(enc: &'static Encoding, bytes: &[u8], truncated: bool) -> boo } /// `(common, high)`: how many of a name's non-ASCII bytes `enc` reads as common text, and how -/// many non-ASCII bytes it has. -fn score(enc: &'static Encoding, name: &[u8]) -> (u64, u64) { +/// many non-ASCII bytes it has. `hint_letters` are the accented letters the file's own Unicode +/// text writes (lowercase), which settle a word-initial capital of windows-1252's 0x80–0x9F. +fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::BTreeSet) -> (u64, u64) { if enc == WINDOWS_1252 || enc == WINDOWS_1251 || enc == MACINTOSH { // Letters, typographic punctuation and the degree sign ("Rotation 90°") are common, as // the double-byte pages' punctuation rows are, with two readings held back: the letters // windows-1252 alone has at 0x80–0x9F, where Mac Roman keeps its accented lowercase (Mac - // "Opacité" is 1252 "OpacitŽ"), unless a capital one starts a word ("Škoda"); and a + // "Opacité" is 1252 "OpacitŽ"), unless a capital one starts a word ("Škoda") and the + // file's own text doesn't write the Mac Roman letter instead (a French "état"); and a // windows-1251 Cyrillic letter glued into an ASCII word (Mac "Höhe" is 1251 "Hшhe"), // unless the name's letters are mostly Cyrillic ("CMYKканал", Ukrainian "Копiя") or the // letter is in a Cyrillic word of its own ("Layer Слой"). @@ -219,7 +224,12 @@ fn score(enc: &'static Encoding, name: &[u8]) -> (u64, u64) { high += 1; let counts = if enc == WINDOWS_1252 && RARE_1252_LETTERS.contains(&ch) { let after_ascii = i.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - ch.is_uppercase() && !after_ascii && chars.get(i + 1).is_some_and(|c| c.is_ascii_lowercase()) + let word_initial = ch.is_uppercase() && !after_ascii && chars.get(i + 1).is_some_and(|c| c.is_ascii_lowercase()); + // The same byte as Mac Roman reads it: a letter the file's text writes, where it + // never writes this one, says the writer meant that ("état", not "Žtat"). + let written = |c: char| c.to_lowercase().all(|l| hint_letters.contains(&l)); + let mac = name.get(i..=i).map(|b| decode(b, MACINTOSH)).and_then(|s| s.chars().next()); + word_initial && !(mac.is_some_and(|m| m.is_alphabetic() && written(m)) && !written(ch)) } else if enc == WINDOWS_1251 && is_cyrillic(ch) { cyrillic_name || cyrillic_word(&chars, i) } else { @@ -289,7 +299,8 @@ fn is_cyrillic(ch: char) -> bool { } /// Whether the Cyrillic letter at `i` is in a Cyrillic word of its own: a run of two or more, -/// not glued to an ASCII letter or digit ("Слой" beside "Layer"; not "OpacitЋ" or "90Ў"). +/// not glued to an ASCII letter ("Слой" beside "Layer", "Слой1"; not "OpacitЋ"; a lone letter +/// after a digit, "90Ў", is no word). fn cyrillic_word(chars: &[char], i: usize) -> bool { let mut start = i; while start > 0 && chars.get(start - 1).is_some_and(|c| is_cyrillic(*c)) { @@ -299,7 +310,7 @@ fn cyrillic_word(chars: &[char], i: usize) -> bool { while chars.get(end).is_some_and(|c| is_cyrillic(*c)) { end += 1; } - let glued = |k: Option| k.and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); + let glued = |k: Option| k.and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphabetic()); end - start >= 2 && !glued(start.checked_sub(1)) && !glued(Some(end)) } @@ -523,6 +534,7 @@ mod tests { // a Latin one counts as Cyrillic, and the hint settles the tie with windows-1252's letters. assert_eq!(guess_words(WINDOWS_1251, &["Ёлка"], "Définir"), Guess { encoding: WINDOWS_1251, uncertain: false }); assert_eq!(guess_words(WINDOWS_1251, &["Layer Слой"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); + assert_eq!(guess_words(WINDOWS_1251, &["Layer Слой1"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }, "a digit after the word"); assert_eq!(guess_words(WINDOWS_1251, &["Layer Слой", "Ёлка"], "Set 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); // Alone and without a hint, "Layer Слой" is as ambiguous as before (every letter is one in // windows-1252 too): the flagged fallback. @@ -534,9 +546,22 @@ mod tests { // windows-1252 Š Ž Ÿ (0x8A 0x8E 0x9F) are Mac Roman ä é ü: a capital one starting a word // ("Škoda") counts, an accent inside a word (Mac "Opacité" as "OpacitŽ") does not. for words in [&["Škoda"][..], &["Žaba"], &["Šibenik", "Žaba"]] { - assert_eq!(guess_words(WINDOWS_1252, words, "Größe ändern"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); + assert_eq!(guess_words(WINDOWS_1252, words, "Nastavení"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); assert_eq!(guess_words(WINDOWS_1252, words, "Set 1").encoding, WINDOWS_1252, "{words:?}"); } + // In a file whose own text writes ä and never Š (a German Photoshop), the byte reads as + // the Mac Roman ä it would be there: the file's alphabet decides what the bytes can't. + assert_eq!(guess_words(WINDOWS_1252, &["Škoda"], "Größe ändern").encoding, MACINTOSH); + } + + #[test] + fn word_initial_mac_roman_accents_read_as_mac_roman_when_the_file_writes_them() { + // Mac é (0x8E) and ü (0x9F) are windows-1252 Ž and Ÿ, capitals that could start a word: + // in a file whose own text has é or ü and no Ž or Ÿ, they are the Mac Roman letters. + assert_eq!(guess_words(MACINTOSH, &["état"], "Définir"), Guess { encoding: MACINTOSH, uncertain: false }); + assert_eq!(guess_words(MACINTOSH, &["über Ebene", "Höhe"], "Größe ändern"), Guess { encoding: MACINTOSH, uncertain: false }); + // Without such text the capital reading stands, as before: the bytes alone can't tell. + assert_eq!(guess_words(MACINTOSH, &["éclair"], "Set 1").encoding, WINDOWS_1252); } #[test] From 946d2d9c7b4ecba5526423b01ae5807d8828765a Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Tue, 3 Nov 2026 16:55:18 -0500 Subject: [PATCH 4/8] =?UTF-8?q?Load=20Actions:=20an=20all-caps=20word-init?= =?UTF-8?q?ial=20=C5=A0=20or=20=C5=BD=20counts=20for=20windows-1252,=20and?= =?UTF-8?q?=20a=20digit=20before=20a=20Cyrillic=20run=20glues=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review: "ŠKODA" (a capital before an ASCII capital) counted for nothing and could read as Mac Roman "äKODA"; Mac Roman "Layer 90°é" read as windows-1251 "90ЎЋ", two Cyrillic letters right after a number. The word-initial rule now takes an ASCII letter of either case after the capital, and `cyrillic_word` treats a digit before the run as glue (a digit after still isn't: "Слой1"). Main's new fallback test tied Mac Roman "Ändern" with windows-1251, which this branch settles for Mac Roman; it ties "éäé" with "ЋЉЋ" instead and checks "Ändern" is certain. Co-Authored-By: Claude Fable 5.1 --- crates/psd/src/codepage.rs | 46 ++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index e00c79ff9a7..69a0e21532b 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -214,7 +214,7 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B // Letters, typographic punctuation and the degree sign ("Rotation 90°") are common, as // the double-byte pages' punctuation rows are, with two readings held back: the letters // windows-1252 alone has at 0x80–0x9F, where Mac Roman keeps its accented lowercase (Mac - // "Opacité" is 1252 "OpacitŽ"), unless a capital one starts a word ("Škoda") and the + // "Opacité" is 1252 "OpacitŽ"), unless a capital one starts a word ("Škoda", "ŠKODA") and the // file's own text doesn't write the Mac Roman letter instead (a French "état"); and a // windows-1251 Cyrillic letter glued into an ASCII word (Mac "Höhe" is 1251 "Hшhe"), // unless the name's letters are mostly Cyrillic ("CMYKканал", Ukrainian "Копiя") or the @@ -229,7 +229,9 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B high += 1; let counts = if enc == WINDOWS_1252 && RARE_1252_LETTERS.contains(&ch) { let after_ascii = i.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - let word_initial = ch.is_uppercase() && !after_ascii && chars.get(i + 1).is_some_and(|c| c.is_ascii_lowercase()); + // Followed by an ASCII letter of either case: Mac Roman's lowercase ä before a + // capital ("äKODA") is no word either. + let word_initial = ch.is_uppercase() && !after_ascii && chars.get(i + 1).is_some_and(|c| c.is_ascii_alphabetic()); // The same byte as Mac Roman reads it: a letter the file's text writes, where it // never writes this one, says the writer meant that ("état", not "Žtat"). let written = |c: char| c.to_lowercase().all(|l| hint_letters.contains(&l)); @@ -304,8 +306,9 @@ fn is_cyrillic(ch: char) -> bool { } /// Whether the Cyrillic letter at `i` is in a Cyrillic word of its own: a run of two or more, -/// not glued to an ASCII letter ("Слой" beside "Layer", "Слой1"; not "OpacitЋ"; a lone letter -/// after a digit, "90Ў", is no word). +/// not glued to an ASCII letter, and not right after a digit ("Слой" beside "Layer", "Слой1"; +/// not "OpacitЋ", nor "90ЎЋ", which is Mac Roman "90°é": a digit ends a number, not a word, so +/// what follows it without a space is its unit or suffix). fn cyrillic_word(chars: &[char], i: usize) -> bool { let mut start = i; while start > 0 && chars.get(start - 1).is_some_and(|c| is_cyrillic(*c)) { @@ -315,8 +318,9 @@ fn cyrillic_word(chars: &[char], i: usize) -> bool { while chars.get(end).is_some_and(|c| is_cyrillic(*c)) { end += 1; } - let glued = |k: Option| k.and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphabetic()); - end - start >= 2 && !glued(start.checked_sub(1)) && !glued(Some(end)) + let glued_before = start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); + let glued_after = chars.get(end).is_some_and(|c| c.is_ascii_alphabetic()); + end - start >= 2 && !glued_before && !glued_after } /// Latin-1 symbols people type in names (windows-1252 0xA2–0xB7) whose byte is not a letter in @@ -517,8 +521,16 @@ mod tests { fn mac_roman_degree_sign_is_not_cyrillic() { // #10774: Mac Roman ° (0xA1) is windows-1251 Ў and windows-1252 ¡; the degree sign is // common text, a lone Cyrillic letter among Latin ones is not. - let cases: [(&[&str], &str); 4] = - [(&["Rotation 90°"], "Définir"), (&["Rotation 90°"], "Set 1"), (&["Rotation 90°"], "Größe ändern"), (&["Rotation 90°", "Arrière-plan"], "Définir")]; + // "Layer 90°é" (review): ° and é are windows-1251 Ў and Ћ, two Cyrillic letters in a row, + // but glued to the number, so no word of their own. + let cases: [(&[&str], &str); 6] = [ + (&["Rotation 90°"], "Définir"), + (&["Rotation 90°"], "Set 1"), + (&["Rotation 90°"], "Größe ändern"), + (&["Rotation 90°", "Arrière-plan"], "Définir"), + (&["Layer 90°é"], "Set 1"), + (&["Layer 90°é"], "Définir"), + ]; for (words, hint) in cases { let g = guess_words(MACINTOSH, words, hint); assert_eq!(g, Guess { encoding: MACINTOSH, uncertain: false }, "{words:?} with hint {hint:?}"); @@ -549,8 +561,9 @@ mod tests { #[test] fn a_capital_rare_letter_starting_a_word_keeps_windows_1252() { // windows-1252 Š Ž Ÿ (0x8A 0x8E 0x9F) are Mac Roman ä é ü: a capital one starting a word - // ("Škoda") counts, an accent inside a word (Mac "Opacité" as "OpacitŽ") does not. - for words in [&["Škoda"][..], &["Žaba"], &["Šibenik", "Žaba"]] { + // ("Škoda", all caps "ŠKODA") counts, an accent inside a word (Mac "Opacité" as + // "OpacitŽ") does not. + for words in [&["Škoda"][..], &["ŠKODA"], &["Žaba"], &["Šibenik", "Žaba"], &["ŽUPA", "Set"]] { assert_eq!(guess_words(WINDOWS_1252, words, "Nastavení"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); assert_eq!(guess_words(WINDOWS_1252, words, "Set 1").encoding, WINDOWS_1252, "{words:?}"); } @@ -669,15 +682,20 @@ mod tests { #[test] fn uncertain_fallback_stays_among_the_highest_scoring_candidates() { - // Mac Roman "Ändern": Mac Roman and windows-1251 both score every non-ASCII byte - // as a letter. Windows-1252 reads "€ndern", so it must not win their unresolved tie. - let names: &[&[u8]] = &[b"\x80ndern"]; + // Mac Roman "éäé" (0x8E 0x8A 0x8E) is windows-1251 "ЋЉЋ", three Cyrillic letters in a + // row, a word of their own: both pages score every non-ASCII byte as a letter (an odd + // count of bytes, so no double-byte page reads it). Windows-1252 reads "ŽŠŽ", no + // word-initial capital among them, so it must not win their unresolved tie. (Mac Roman + // "Ändern" no longer ties: its lone Cyrillic letter glued into a Latin word, 1251 + // "Ђndern", doesn't count, #10774.) + let names: &[&[u8]] = &[b"\x8e\x8a\x8e"]; for hint in ["Set 1", "保存"] { let g = guess(names, hint); assert!([MACINTOSH, WINDOWS_1251].contains(&g.encoding), "{hint}: {g:?}"); assert!(g.uncertain, "{hint}: {g:?}"); - assert_eq!(score(g.encoding, names[0]), (1, 1)); + assert_eq!(score(g.encoding, names[0], &Default::default()), (3, 3)); } + assert_eq!(guess(&[b"\x80ndern"], "Set 1"), Guess { encoding: MACINTOSH, uncertain: false }, "Mac Roman \"Ändern\" is certain now"); } #[test] From f80955ce0d06732bc2bbeb8676218fda9964de9f Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Tue, 3 Nov 2026 17:47:32 -0500 Subject: [PATCH 5/8] =?UTF-8?q?Load=20Actions:=20a=20lowercase=20rare=20le?= =?UTF-8?q?tter=20opening=20a=20word=20counts=20for=20windows-1252,=20and?= =?UTF-8?q?=20so=20does=20=C2=A1=20opening=20a=20word?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review: "šuma" (0x9A u m a) read as Mac Roman "öuma", certain, where main tied it and read windows-1252; and "¡Hola!" read as Mac Roman "°Hola!". The word-initial rule for windows-1252's 0x80–0x9F letters no longer asks for a capital (the file's own alphabet still decides: a file that writes ö and never š reads the byte as Mac Roman ö), and ¡ counts as common text for windows-1252 where it opens a word of two letters or more (`opens_word`, in `score` and in the final windows-1252/Mac Roman comparison), while Mac Roman's degree sign on the same byte, after a number ("Rotation 90°") or before a single letter ("20 °C"), stays as it was. Co-Authored-By: Claude Fable 5.1 --- crates/psd/src/codepage.rs | 111 +++++++++++++++++++++++++------------ 1 file changed, 77 insertions(+), 34 deletions(-) diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index 69a0e21532b..9cd316f4f41 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -11,11 +11,12 @@ //! Roman). A candidate that cannot decode every name without errors is dropped. //! 2. Each survivor is scored by the share of non-ASCII bytes that fall in its common-text //! ranges (GB2312 hanzi, JIS level-1 kanji and kana, Big5 level 1, KS X 1001 Hangul, -//! letters, each page's punctuation, and the degree sign). The highest share wins. Two -//! single-byte readings don't count: the letters windows-1252 alone has at 0x80–0x9F (Š š Ž -//! ž Ÿ ƒ ˆ), where Mac Roman keeps its accented lowercase (Mac "Opacité" is 1252 -//! "OpacitŽ"), unless a capital one starts a word ("Škoda") in a file whose own Unicode text -//! doesn't write the Mac Roman letter on that byte instead (a French "état", a German "über"); +//! letters, each page's punctuation, the degree sign, and ¡ opening a word: "¡Hola!"). The +//! highest share wins. Two single-byte readings don't count: the letters windows-1252 alone +//! has at 0x80–0x9F (Š š Ž ž Ÿ ƒ ˆ), where Mac Roman keeps its accented lowercase (Mac +//! "Opacité" is 1252 "OpacitŽ"), unless one starts a word ("Škoda", "šuma") in a file whose +//! own Unicode text doesn't write the Mac Roman letter on that byte instead (a French "état", +//! a German "über", "öffnen"); //! and a windows-1251 Cyrillic letter glued into an ASCII word (Mac "Höhe" is 1251 "Hшhe"), //! unless the name's letters are mostly Cyrillic ("CMYKканал", "Копiя") or the letter is in a //! Cyrillic word of its own ("Layer Слой", "Слой1"). @@ -208,17 +209,18 @@ fn decodes_cleanly(enc: &'static Encoding, bytes: &[u8], truncated: bool) -> boo /// `(common, high)`: how many of a name's non-ASCII bytes `enc` reads as common text, and how /// many non-ASCII bytes it has. `hint_letters` are the accented letters the file's own Unicode -/// text writes (lowercase), which settle a word-initial capital of windows-1252's 0x80–0x9F. +/// text writes (lowercase), which settle a word-initial letter of windows-1252's 0x80–0x9F. fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::BTreeSet) -> (u64, u64) { if enc == WINDOWS_1252 || enc == WINDOWS_1251 || enc == MACINTOSH { - // Letters, typographic punctuation and the degree sign ("Rotation 90°") are common, as - // the double-byte pages' punctuation rows are, with two readings held back: the letters - // windows-1252 alone has at 0x80–0x9F, where Mac Roman keeps its accented lowercase (Mac - // "Opacité" is 1252 "OpacitŽ"), unless a capital one starts a word ("Škoda", "ŠKODA") and the - // file's own text doesn't write the Mac Roman letter instead (a French "état"); and a - // windows-1251 Cyrillic letter glued into an ASCII word (Mac "Höhe" is 1251 "Hшhe"), - // unless the name's letters are mostly Cyrillic ("CMYKканал", Ukrainian "Копiя") or the - // letter is in a Cyrillic word of its own ("Layer Слой"). + // Letters, typographic punctuation, the degree sign ("Rotation 90°") and ¡ opening a word + // ("¡Hola!") are common, as the double-byte pages' punctuation rows are, with two readings + // held back: the letters windows-1252 alone has at 0x80–0x9F, where Mac Roman keeps its + // accented lowercase (Mac "Opacité" is 1252 "OpacitŽ"), unless one starts a word ("Škoda", + // "ŠKODA", "šuma") and the file's own text doesn't write the Mac Roman letter instead (a + // French "état", a German "öffnen"); and a windows-1251 Cyrillic letter glued into an ASCII + // word (Mac "Höhe" is 1251 "Hшhe"), unless the name's letters are mostly Cyrillic + // ("CMYKканал", Ukrainian "Копiя") or the letter is in a Cyrillic word of its own ("Layer + // Слой"). let chars: Vec = decode(name, enc).chars().collect(); let cyrillic_name = chars.iter().filter(|c| is_cyrillic(**c)).count() >= chars.iter().filter(|c| c.is_ascii_alphabetic()).count(); let (mut common, mut high) = (0u64, 0u64); @@ -227,11 +229,12 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B continue; } high += 1; + let (after_ascii, letters_after) = position(&chars, i); let counts = if enc == WINDOWS_1252 && RARE_1252_LETTERS.contains(&ch) { - let after_ascii = i.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - // Followed by an ASCII letter of either case: Mac Roman's lowercase ä before a - // capital ("äKODA") is no word either. - let word_initial = ch.is_uppercase() && !after_ascii && chars.get(i + 1).is_some_and(|c| c.is_ascii_alphabetic()); + // Opening a word, capital or not ("Škoda", "šuma"), before an ASCII letter of + // either case: Mac Roman's lowercase ä before a capital ("äKODA") is no word + // either. + let word_initial = !after_ascii && letters_after >= 1; // The same byte as Mac Roman reads it: a letter the file's text writes, where it // never writes this one, says the writer meant that ("état", not "Žtat"). let written = |c: char| c.to_lowercase().all(|l| hint_letters.contains(&l)); @@ -240,7 +243,7 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B } else if enc == WINDOWS_1251 && is_cyrillic(ch) { cyrillic_name || cyrillic_word(&chars, i) } else { - ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' + ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' || (ch == '¡' && opens_word(after_ascii, letters_after)) }; common += u64::from(counts); } @@ -283,15 +286,39 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B /// name like "Fläche m²" or "Preis £5" would score higher as windows-1251 ("Flдche mІ", "Preis /// Ј5"); [`score`] counts a Cyrillic letter only in a name whose letters are mostly Cyrillic, /// which keeps ordinary Russian names such as "CMYKканал" or Ukrainian "Копiя" (Latin i). -fn single_byte_score(enc: &'static Encoding, name: &[u8], common: impl Fn(char) -> bool) -> (u64, u64) { - decode(name, enc).chars().filter(|c| !c.is_ascii()).fold((0, 0), |(c, h), ch| (c.saturating_add(u64::from(common(ch))), h.saturating_add(1))) +fn single_byte_score(enc: &'static Encoding, name: &[u8], common: impl Fn(&[char], usize) -> bool) -> (u64, u64) { + let chars: Vec = decode(name, enc).chars().collect(); + (0..chars.len()) + .filter(|i| chars.get(*i).is_some_and(|c| !c.is_ascii())) + .fold((0, 0), |(c, h), i| (c.saturating_add(u64::from(common(&chars, i))), h.saturating_add(1))) } -/// Whether `ch` is common in Western names, for weighing windows-1252 against Mac Roman: letters -/// (but not those windows-1252 has only at 0x80–0x9F, where Mac Roman keeps its accented -/// lowercase: Mac "Größe" is 1252 "Grš§e"), typographic punctuation and [`LATIN_SYMBOLS`]. -fn is_latin_common(ch: char) -> bool { - (ch.is_alphabetic() && !RARE_1252_LETTERS.contains(&ch) && ch != 'Œ' && ch != 'œ') || TYPOGRAPHIC.contains(&ch) || LATIN_SYMBOLS.contains(&ch) +/// Whether the character at `i` of `chars` is common in Western names, for weighing windows-1252 +/// against Mac Roman: letters (but not those windows-1252 has only at 0x80–0x9F, where Mac Roman +/// keeps its accented lowercase: Mac "Größe" is 1252 "Grš§e"), typographic punctuation, +/// [`LATIN_SYMBOLS`], and ¡ opening a word ("¡Hola!"), as in [`score`]. +fn is_latin_common(chars: &[char], i: usize) -> bool { + let Some(&ch) = chars.get(i) else { return false }; + let (after_ascii, letters_after) = position(chars, i); + (ch.is_alphabetic() && !RARE_1252_LETTERS.contains(&ch) && ch != 'Œ' && ch != 'œ') + || TYPOGRAPHIC.contains(&ch) + || LATIN_SYMBOLS.contains(&ch) + || (ch == '¡' && opens_word(after_ascii, letters_after)) +} + +/// Where the character at `i` of `chars` sits: right after an ASCII letter or digit (inside a word +/// or a number), and how many ASCII letters follow it. +fn position(chars: &[char], i: usize) -> (bool, usize) { + let after_ascii = i.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); + let letters_after = chars.get(i + 1..).map_or(0, |rest| rest.iter().take_while(|c| c.is_ascii_alphabetic()).count()); + (after_ascii, letters_after) +} + +/// Whether ¡ there opens a Spanish word ("¡Hola!", "¡Atención!"): two letters or more follow it +/// and no letter or digit precedes it. Mac Roman's degree sign on the same byte follows a number +/// ("Rotation 90°") or stands before a single letter ("20 °C"). +fn opens_word(after_ascii: bool, letters_after: usize) -> bool { + !after_ascii && letters_after >= 2 } /// The letters windows-1252 has at 0x80–0x9F and Mac Roman doesn't: there Mac Roman keeps its @@ -327,9 +354,10 @@ fn cyrillic_word(chars: &[char], i: usize) -> bool { /// Mac Roman. Left out because Mac Roman has a letter there: § ® ¹ » ¼ ½ ¾ ¿ (ß Æ π ª º Ω æ ø, /// as in Mac "Weiß", "Størrelse", "Æble"), € and ™ (0x80, 0x99: Ä, ô), and ¥ (0xA5, Mac Roman's /// bullet in [`TYPOGRAPHIC`]: Mac "Ebene • Kopie"); and ¡ (0xA1), where Mac Roman has its degree -/// sign, the commoner of the two in names (Mac "Rotation 90°"). So a windows-1252 name whose only -/// non-ASCII character is one of those ("Logo ®", "¿Tamaño?", "Preis ¥100", "¡Hola!") still -/// reads as Mac Roman ("Logo Æ"). +/// sign, the commoner of the two in names (Mac "Rotation 90°", "20 °C"), which counts only where +/// it opens a word of two letters or more ("¡Hola!"; [`opens_word`], in [`score`] and +/// [`is_latin_common`]). So a windows-1252 name whose only non-ASCII character is one of the +/// others ("Logo ®", "¿Tamaño?", "Preis ¥100") still reads as Mac Roman ("Logo Æ"). const LATIN_SYMBOLS: [char; 10] = ['¢', '£', '©', '«', '°', '±', '²', '³', '¶', '·']; /// Quotes, dashes, ellipsis and bullet: windows-1252 and windows-1251 share them at 0x82–0x97; @@ -559,17 +587,32 @@ mod tests { } #[test] - fn a_capital_rare_letter_starting_a_word_keeps_windows_1252() { - // windows-1252 Š Ž Ÿ (0x8A 0x8E 0x9F) are Mac Roman ä é ü: a capital one starting a word - // ("Škoda", all caps "ŠKODA") counts, an accent inside a word (Mac "Opacité" as - // "OpacitŽ") does not. - for words in [&["Škoda"][..], &["ŠKODA"], &["Žaba"], &["Šibenik", "Žaba"], &["ŽUPA", "Set"]] { + fn a_rare_letter_starting_a_word_keeps_windows_1252() { + // windows-1252 Š Ž Ÿ š ž (0x8A 0x8E 0x9F 0x9A 0x9E) are Mac Roman ä é ü ö û: one starting + // a word ("Škoda", all caps "ŠKODA", lowercase "šuma") counts, an accent inside a word (Mac + // "Opacité" as "OpacitŽ") does not. + for words in [&["Škoda"][..], &["ŠKODA"], &["Žaba"], &["Šibenik", "Žaba"], &["ŽUPA", "Set"], &["šuma"], &["žaba"]] { assert_eq!(guess_words(WINDOWS_1252, words, "Nastavení"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); assert_eq!(guess_words(WINDOWS_1252, words, "Set 1").encoding, WINDOWS_1252, "{words:?}"); } // In a file whose own text writes ä and never Š (a German Photoshop), the byte reads as // the Mac Roman ä it would be there: the file's alphabet decides what the bytes can't. assert_eq!(guess_words(WINDOWS_1252, &["Škoda"], "Größe ändern").encoding, MACINTOSH); + assert_eq!(guess_words(WINDOWS_1252, &["šuma"], "Größe ändern").encoding, MACINTOSH, "the file writes ö and never š"); + } + + #[test] + fn an_inverted_exclamation_opening_a_word_reads_as_windows_1252() { + // windows-1252 ¡ (0xA1) is Mac Roman °: ¡ opening a word of two letters or more ("¡Hola!") + // counts for windows-1252 (review); after a number ("90°") or before a single letter + // ("20 °C") the byte stays the degree sign. + assert_eq!(guess_words(WINDOWS_1252, &["¡Hola!"], "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }); + assert_eq!(guess_words(WINDOWS_1252, &["¡Hola!"], "Set 1").encoding, WINDOWS_1252); + assert_eq!(guess_words(WINDOWS_1252, &["Capa ¡Atención!"], "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }); + for words in [&["Rotation 90°"][..], &["Temperatur 20 °C"]] { + assert_eq!(guess_words(MACINTOSH, words, "Configuración"), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?}"); + assert_eq!(guess_words(MACINTOSH, words, "Größe ändern"), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?}"); + } } #[test] From 47ee3f69602735b31a87f286f30b87435e5208a2 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Tue, 3 Nov 2026 18:55:03 -0500 Subject: [PATCH 6/8] Gradient Overlay drag: no drag on an empty layer, and the origin follows the selected instance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review: an empty layer gave bounds of 0 x 0, so the canvas took the drag from the Hand tool and wrote offsets of up to ±4 layer widths from a short drag, for a gradient that paints nothing; and the drag's one origin stayed when `selected` changed under a held press, so the next instance moved from the previous origin and the whole travel since the press. `gradient_layer_bounds` returns `None` for empty bounds, and `__gradientDrag` keeps the instance id, the origin and the pointer total at that point, taken afresh when the id differs: offsets are the origin plus the travel since. Co-Authored-By: Claude Fable 5.1 --- crates/psd/src/codepage.rs | 90 +++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 30 deletions(-) diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index 9cd316f4f41..d0f370d4fbc 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -11,7 +11,7 @@ //! Roman). A candidate that cannot decode every name without errors is dropped. //! 2. Each survivor is scored by the share of non-ASCII bytes that fall in its common-text //! ranges (GB2312 hanzi, JIS level-1 kanji and kana, Big5 level 1, KS X 1001 Hangul, -//! letters, each page's punctuation, the degree sign, and ¡ opening a word: "¡Hola!"). The +//! letters, each page's punctuation, the degree sign, and ¡ opening an exclamation: "¡Hola!"). The //! highest share wins. Two single-byte readings don't count: the letters windows-1252 alone //! has at 0x80–0x9F (Š š Ž ž Ÿ ƒ ˆ), where Mac Roman keeps its accented lowercase (Mac //! "Opacité" is 1252 "OpacitŽ"), unless one starts a word ("Škoda", "šuma") in a file whose @@ -212,15 +212,15 @@ fn decodes_cleanly(enc: &'static Encoding, bytes: &[u8], truncated: bool) -> boo /// text writes (lowercase), which settle a word-initial letter of windows-1252's 0x80–0x9F. fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::BTreeSet) -> (u64, u64) { if enc == WINDOWS_1252 || enc == WINDOWS_1251 || enc == MACINTOSH { - // Letters, typographic punctuation, the degree sign ("Rotation 90°") and ¡ opening a word - // ("¡Hola!") are common, as the double-byte pages' punctuation rows are, with two readings + // Letters, typographic punctuation, the degree sign ("Rotation 90°") and ¡ opening an + // exclamation ("¡Hola!") are common, as the double-byte pages' punctuation rows are, with two readings // held back: the letters windows-1252 alone has at 0x80–0x9F, where Mac Roman keeps its // accented lowercase (Mac "Opacité" is 1252 "OpacitŽ"), unless one starts a word ("Škoda", // "ŠKODA", "šuma") and the file's own text doesn't write the Mac Roman letter instead (a // French "état", a German "öffnen"); and a windows-1251 Cyrillic letter glued into an ASCII // word (Mac "Höhe" is 1251 "Hшhe"), unless the name's letters are mostly Cyrillic // ("CMYKканал", Ukrainian "Копiя") or the letter is in a Cyrillic word of its own ("Layer - // Слой"). + // Слой"); a lone one right after a digit (Mac "90°" alone is 1251 "90Ў") never counts. let chars: Vec = decode(name, enc).chars().collect(); let cyrillic_name = chars.iter().filter(|c| is_cyrillic(**c)).count() >= chars.iter().filter(|c| c.is_ascii_alphabetic()).count(); let (mut common, mut high) = (0u64, 0u64); @@ -241,9 +241,9 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B let mac = name.get(i..=i).map(|b| decode(b, MACINTOSH)).and_then(|s| s.chars().next()); word_initial && !(mac.is_some_and(|m| m.is_alphabetic() && written(m)) && !written(ch)) } else if enc == WINDOWS_1251 && is_cyrillic(ch) { - cyrillic_name || cyrillic_word(&chars, i) + (cyrillic_name && !lone_after_digit(&chars, i)) || cyrillic_word(&chars, i) } else { - ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' || (ch == '¡' && opens_word(after_ascii, letters_after)) + ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' || (ch == '¡' && opens_exclamation(&chars, i)) }; common += u64::from(counts); } @@ -296,29 +296,30 @@ fn single_byte_score(enc: &'static Encoding, name: &[u8], common: impl Fn(&[char /// Whether the character at `i` of `chars` is common in Western names, for weighing windows-1252 /// against Mac Roman: letters (but not those windows-1252 has only at 0x80–0x9F, where Mac Roman /// keeps its accented lowercase: Mac "Größe" is 1252 "Grš§e"), typographic punctuation, -/// [`LATIN_SYMBOLS`], and ¡ opening a word ("¡Hola!"), as in [`score`]. +/// [`LATIN_SYMBOLS`], and ¡ opening an exclamation ("¡Hola!"), as in [`score`]. fn is_latin_common(chars: &[char], i: usize) -> bool { let Some(&ch) = chars.get(i) else { return false }; - let (after_ascii, letters_after) = position(chars, i); (ch.is_alphabetic() && !RARE_1252_LETTERS.contains(&ch) && ch != 'Œ' && ch != 'œ') || TYPOGRAPHIC.contains(&ch) || LATIN_SYMBOLS.contains(&ch) - || (ch == '¡' && opens_word(after_ascii, letters_after)) + || (ch == '¡' && opens_exclamation(chars, i)) } /// Where the character at `i` of `chars` sits: right after an ASCII letter or digit (inside a word -/// or a number), and how many ASCII letters follow it. +/// or a number), and how many letters (accented ones too: "¡Ánimo!") follow it directly. fn position(chars: &[char], i: usize) -> (bool, usize) { let after_ascii = i.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - let letters_after = chars.get(i + 1..).map_or(0, |rest| rest.iter().take_while(|c| c.is_ascii_alphabetic()).count()); + let letters_after = chars.get(i + 1..).map_or(0, |rest| rest.iter().take_while(|c| c.is_alphabetic()).count()); (after_ascii, letters_after) } -/// Whether ¡ there opens a Spanish word ("¡Hola!", "¡Atención!"): two letters or more follow it -/// and no letter or digit precedes it. Mac Roman's degree sign on the same byte follows a number -/// ("Rotation 90°") or stands before a single letter ("20 °C"). -fn opens_word(after_ascii: bool, letters_after: usize) -> bool { - !after_ascii && letters_after >= 2 +/// Whether ¡ at `i` opens a Spanish exclamation ("¡Hola!", "Capa ¡Atención!", "¡Ánimo!"): no letter +/// or digit right before it, two letters or more after it, and the closing "!" later in the name. +/// Mac Roman's degree sign on the same byte follows a number ("Rotation 90°") or stands before a +/// unit ("20 °C", "20 °Celsius"), with no "!" to close. +fn opens_exclamation(chars: &[char], i: usize) -> bool { + let (after_ascii, letters_after) = position(chars, i); + !after_ascii && letters_after >= 2 && chars.get(i + 1..).is_some_and(|rest| rest.contains(&'!')) } /// The letters windows-1252 has at 0x80–0x9F and Mac Roman doesn't: there Mac Roman keeps its @@ -337,6 +338,14 @@ fn is_cyrillic(ch: char) -> bool { /// not "OpacitЋ", nor "90ЎЋ", which is Mac Roman "90°é": a digit ends a number, not a word, so /// what follows it without a space is its unit or suffix). fn cyrillic_word(chars: &[char], i: usize) -> bool { + let (start, end) = cyrillic_run(chars, i); + let glued_before = start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); + let glued_after = chars.get(end).is_some_and(|c| c.is_ascii_alphabetic()); + end - start >= 2 && !glued_before && !glued_after +} + +/// The run of Cyrillic letters around `i` (`start..end`). +fn cyrillic_run(chars: &[char], i: usize) -> (usize, usize) { let mut start = i; while start > 0 && chars.get(start - 1).is_some_and(|c| is_cyrillic(*c)) { start -= 1; @@ -345,9 +354,15 @@ fn cyrillic_word(chars: &[char], i: usize) -> bool { while chars.get(end).is_some_and(|c| is_cyrillic(*c)) { end += 1; } - let glued_before = start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - let glued_after = chars.get(end).is_some_and(|c| c.is_ascii_alphabetic()); - end - start >= 2 && !glued_before && !glued_after + (start, end) +} + +/// Whether the Cyrillic letter at `i` stands alone right after a digit: a unit or a symbol misread +/// (Mac Roman "90°" is windows-1251 "90Ў"), whatever the rest of the name; a run after a digit is +/// a Russian unit ("5шт") and still counts. +fn lone_after_digit(chars: &[char], i: usize) -> bool { + let (start, end) = cyrillic_run(chars, i); + end - start == 1 && start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_digit()) } /// Latin-1 symbols people type in names (windows-1252 0xA2–0xB7) whose byte is not a letter in @@ -355,7 +370,7 @@ fn cyrillic_word(chars: &[char], i: usize) -> bool { /// as in Mac "Weiß", "Størrelse", "Æble"), € and ™ (0x80, 0x99: Ä, ô), and ¥ (0xA5, Mac Roman's /// bullet in [`TYPOGRAPHIC`]: Mac "Ebene • Kopie"); and ¡ (0xA1), where Mac Roman has its degree /// sign, the commoner of the two in names (Mac "Rotation 90°", "20 °C"), which counts only where -/// it opens a word of two letters or more ("¡Hola!"; [`opens_word`], in [`score`] and +/// it opens an exclamation closed by "!" ("¡Hola!"; [`opens_exclamation`], in [`score`] and /// [`is_latin_common`]). So a windows-1252 name whose only non-ASCII character is one of the /// others ("Logo ®", "¿Tamaño?", "Preis ¥100") still reads as Mac Roman ("Logo Æ"). const LATIN_SYMBOLS: [char; 10] = ['¢', '£', '©', '«', '°', '±', '²', '³', '¶', '·']; @@ -602,17 +617,32 @@ mod tests { } #[test] - fn an_inverted_exclamation_opening_a_word_reads_as_windows_1252() { - // windows-1252 ¡ (0xA1) is Mac Roman °: ¡ opening a word of two letters or more ("¡Hola!") - // counts for windows-1252 (review); after a number ("90°") or before a single letter - // ("20 °C") the byte stays the degree sign. - assert_eq!(guess_words(WINDOWS_1252, &["¡Hola!"], "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }); - assert_eq!(guess_words(WINDOWS_1252, &["¡Hola!"], "Set 1").encoding, WINDOWS_1252); - assert_eq!(guess_words(WINDOWS_1252, &["Capa ¡Atención!"], "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }); - for words in [&["Rotation 90°"][..], &["Temperatur 20 °C"]] { - assert_eq!(guess_words(MACINTOSH, words, "Configuración"), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?}"); - assert_eq!(guess_words(MACINTOSH, words, "Größe ändern"), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?}"); + fn an_inverted_exclamation_opening_an_exclamation_reads_as_windows_1252() { + // windows-1252 ¡ (0xA1) is Mac Roman °: ¡ opening an exclamation, two letters or more + // after it (accented ones too) and the closing "!" later ("¡Hola!", "¡Ánimo!"), counts for + // windows-1252 (review); after a number ("90°"), before a unit ("20 °C", "20 °Celsius") or + // without a "!" the byte stays the degree sign. + for words in [&["¡Hola!"][..], &["Capa ¡Atención!"], &["¡Ánimo!"]] { + assert_eq!(guess_words(WINDOWS_1252, words, "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); + assert_eq!(guess_words(WINDOWS_1252, words, "Set 1").encoding, WINDOWS_1252, "{words:?}"); } + for words in [&["Rotation 90°"][..], &["Temperatur 20 °C"], &["20 °Celsius"]] { + for hint in ["Configuración", "Größe ändern", "Set 1"] { + assert_eq!(guess_words(MACINTOSH, words, hint), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?} with hint {hint:?}"); + } + } + } + + #[test] + fn a_lone_cyrillic_letter_after_a_digit_is_no_cyrillic_name() { + // Mac Roman "90°" alone has no ASCII letter, so its one windows-1251 letter (Ў) would make + // the name "mostly Cyrillic" (review): a lone Cyrillic letter right after a digit never + // counts. A one-letter Cyrillic name and a Russian unit after a digit still do. + for hint in ["Set 1", "Définir"] { + assert_eq!(guess_words(MACINTOSH, &["90°"], hint), Guess { encoding: MACINTOSH, uncertain: false }, "hint {hint:?}"); + } + assert_eq!(guess_words(WINDOWS_1251, &["Я"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); + assert_eq!(guess_words(WINDOWS_1251, &["5шт"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); } #[test] From f3ce2e2b9e3c33d2fce170f971af507024eb413a Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Tue, 3 Nov 2026 19:07:10 -0500 Subject: [PATCH 7/8] Revert "Gradient Overlay drag: no drag on an empty layer, and the origin follows the selected instance" This reverts commit 47ee3f69602735b31a87f286f30b87435e5208a2. --- crates/psd/src/codepage.rs | 90 +++++++++++++------------------------- 1 file changed, 30 insertions(+), 60 deletions(-) diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index d0f370d4fbc..9cd316f4f41 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -11,7 +11,7 @@ //! Roman). A candidate that cannot decode every name without errors is dropped. //! 2. Each survivor is scored by the share of non-ASCII bytes that fall in its common-text //! ranges (GB2312 hanzi, JIS level-1 kanji and kana, Big5 level 1, KS X 1001 Hangul, -//! letters, each page's punctuation, the degree sign, and ¡ opening an exclamation: "¡Hola!"). The +//! letters, each page's punctuation, the degree sign, and ¡ opening a word: "¡Hola!"). The //! highest share wins. Two single-byte readings don't count: the letters windows-1252 alone //! has at 0x80–0x9F (Š š Ž ž Ÿ ƒ ˆ), where Mac Roman keeps its accented lowercase (Mac //! "Opacité" is 1252 "OpacitŽ"), unless one starts a word ("Škoda", "šuma") in a file whose @@ -212,15 +212,15 @@ fn decodes_cleanly(enc: &'static Encoding, bytes: &[u8], truncated: bool) -> boo /// text writes (lowercase), which settle a word-initial letter of windows-1252's 0x80–0x9F. fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::BTreeSet) -> (u64, u64) { if enc == WINDOWS_1252 || enc == WINDOWS_1251 || enc == MACINTOSH { - // Letters, typographic punctuation, the degree sign ("Rotation 90°") and ¡ opening an - // exclamation ("¡Hola!") are common, as the double-byte pages' punctuation rows are, with two readings + // Letters, typographic punctuation, the degree sign ("Rotation 90°") and ¡ opening a word + // ("¡Hola!") are common, as the double-byte pages' punctuation rows are, with two readings // held back: the letters windows-1252 alone has at 0x80–0x9F, where Mac Roman keeps its // accented lowercase (Mac "Opacité" is 1252 "OpacitŽ"), unless one starts a word ("Škoda", // "ŠKODA", "šuma") and the file's own text doesn't write the Mac Roman letter instead (a // French "état", a German "öffnen"); and a windows-1251 Cyrillic letter glued into an ASCII // word (Mac "Höhe" is 1251 "Hшhe"), unless the name's letters are mostly Cyrillic // ("CMYKканал", Ukrainian "Копiя") or the letter is in a Cyrillic word of its own ("Layer - // Слой"); a lone one right after a digit (Mac "90°" alone is 1251 "90Ў") never counts. + // Слой"). let chars: Vec = decode(name, enc).chars().collect(); let cyrillic_name = chars.iter().filter(|c| is_cyrillic(**c)).count() >= chars.iter().filter(|c| c.is_ascii_alphabetic()).count(); let (mut common, mut high) = (0u64, 0u64); @@ -241,9 +241,9 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B let mac = name.get(i..=i).map(|b| decode(b, MACINTOSH)).and_then(|s| s.chars().next()); word_initial && !(mac.is_some_and(|m| m.is_alphabetic() && written(m)) && !written(ch)) } else if enc == WINDOWS_1251 && is_cyrillic(ch) { - (cyrillic_name && !lone_after_digit(&chars, i)) || cyrillic_word(&chars, i) + cyrillic_name || cyrillic_word(&chars, i) } else { - ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' || (ch == '¡' && opens_exclamation(&chars, i)) + ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' || (ch == '¡' && opens_word(after_ascii, letters_after)) }; common += u64::from(counts); } @@ -296,30 +296,29 @@ fn single_byte_score(enc: &'static Encoding, name: &[u8], common: impl Fn(&[char /// Whether the character at `i` of `chars` is common in Western names, for weighing windows-1252 /// against Mac Roman: letters (but not those windows-1252 has only at 0x80–0x9F, where Mac Roman /// keeps its accented lowercase: Mac "Größe" is 1252 "Grš§e"), typographic punctuation, -/// [`LATIN_SYMBOLS`], and ¡ opening an exclamation ("¡Hola!"), as in [`score`]. +/// [`LATIN_SYMBOLS`], and ¡ opening a word ("¡Hola!"), as in [`score`]. fn is_latin_common(chars: &[char], i: usize) -> bool { let Some(&ch) = chars.get(i) else { return false }; + let (after_ascii, letters_after) = position(chars, i); (ch.is_alphabetic() && !RARE_1252_LETTERS.contains(&ch) && ch != 'Œ' && ch != 'œ') || TYPOGRAPHIC.contains(&ch) || LATIN_SYMBOLS.contains(&ch) - || (ch == '¡' && opens_exclamation(chars, i)) + || (ch == '¡' && opens_word(after_ascii, letters_after)) } /// Where the character at `i` of `chars` sits: right after an ASCII letter or digit (inside a word -/// or a number), and how many letters (accented ones too: "¡Ánimo!") follow it directly. +/// or a number), and how many ASCII letters follow it. fn position(chars: &[char], i: usize) -> (bool, usize) { let after_ascii = i.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - let letters_after = chars.get(i + 1..).map_or(0, |rest| rest.iter().take_while(|c| c.is_alphabetic()).count()); + let letters_after = chars.get(i + 1..).map_or(0, |rest| rest.iter().take_while(|c| c.is_ascii_alphabetic()).count()); (after_ascii, letters_after) } -/// Whether ¡ at `i` opens a Spanish exclamation ("¡Hola!", "Capa ¡Atención!", "¡Ánimo!"): no letter -/// or digit right before it, two letters or more after it, and the closing "!" later in the name. -/// Mac Roman's degree sign on the same byte follows a number ("Rotation 90°") or stands before a -/// unit ("20 °C", "20 °Celsius"), with no "!" to close. -fn opens_exclamation(chars: &[char], i: usize) -> bool { - let (after_ascii, letters_after) = position(chars, i); - !after_ascii && letters_after >= 2 && chars.get(i + 1..).is_some_and(|rest| rest.contains(&'!')) +/// Whether ¡ there opens a Spanish word ("¡Hola!", "¡Atención!"): two letters or more follow it +/// and no letter or digit precedes it. Mac Roman's degree sign on the same byte follows a number +/// ("Rotation 90°") or stands before a single letter ("20 °C"). +fn opens_word(after_ascii: bool, letters_after: usize) -> bool { + !after_ascii && letters_after >= 2 } /// The letters windows-1252 has at 0x80–0x9F and Mac Roman doesn't: there Mac Roman keeps its @@ -338,14 +337,6 @@ fn is_cyrillic(ch: char) -> bool { /// not "OpacitЋ", nor "90ЎЋ", which is Mac Roman "90°é": a digit ends a number, not a word, so /// what follows it without a space is its unit or suffix). fn cyrillic_word(chars: &[char], i: usize) -> bool { - let (start, end) = cyrillic_run(chars, i); - let glued_before = start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - let glued_after = chars.get(end).is_some_and(|c| c.is_ascii_alphabetic()); - end - start >= 2 && !glued_before && !glued_after -} - -/// The run of Cyrillic letters around `i` (`start..end`). -fn cyrillic_run(chars: &[char], i: usize) -> (usize, usize) { let mut start = i; while start > 0 && chars.get(start - 1).is_some_and(|c| is_cyrillic(*c)) { start -= 1; @@ -354,15 +345,9 @@ fn cyrillic_run(chars: &[char], i: usize) -> (usize, usize) { while chars.get(end).is_some_and(|c| is_cyrillic(*c)) { end += 1; } - (start, end) -} - -/// Whether the Cyrillic letter at `i` stands alone right after a digit: a unit or a symbol misread -/// (Mac Roman "90°" is windows-1251 "90Ў"), whatever the rest of the name; a run after a digit is -/// a Russian unit ("5шт") and still counts. -fn lone_after_digit(chars: &[char], i: usize) -> bool { - let (start, end) = cyrillic_run(chars, i); - end - start == 1 && start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_digit()) + let glued_before = start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); + let glued_after = chars.get(end).is_some_and(|c| c.is_ascii_alphabetic()); + end - start >= 2 && !glued_before && !glued_after } /// Latin-1 symbols people type in names (windows-1252 0xA2–0xB7) whose byte is not a letter in @@ -370,7 +355,7 @@ fn lone_after_digit(chars: &[char], i: usize) -> bool { /// as in Mac "Weiß", "Størrelse", "Æble"), € and ™ (0x80, 0x99: Ä, ô), and ¥ (0xA5, Mac Roman's /// bullet in [`TYPOGRAPHIC`]: Mac "Ebene • Kopie"); and ¡ (0xA1), where Mac Roman has its degree /// sign, the commoner of the two in names (Mac "Rotation 90°", "20 °C"), which counts only where -/// it opens an exclamation closed by "!" ("¡Hola!"; [`opens_exclamation`], in [`score`] and +/// it opens a word of two letters or more ("¡Hola!"; [`opens_word`], in [`score`] and /// [`is_latin_common`]). So a windows-1252 name whose only non-ASCII character is one of the /// others ("Logo ®", "¿Tamaño?", "Preis ¥100") still reads as Mac Roman ("Logo Æ"). const LATIN_SYMBOLS: [char; 10] = ['¢', '£', '©', '«', '°', '±', '²', '³', '¶', '·']; @@ -617,32 +602,17 @@ mod tests { } #[test] - fn an_inverted_exclamation_opening_an_exclamation_reads_as_windows_1252() { - // windows-1252 ¡ (0xA1) is Mac Roman °: ¡ opening an exclamation, two letters or more - // after it (accented ones too) and the closing "!" later ("¡Hola!", "¡Ánimo!"), counts for - // windows-1252 (review); after a number ("90°"), before a unit ("20 °C", "20 °Celsius") or - // without a "!" the byte stays the degree sign. - for words in [&["¡Hola!"][..], &["Capa ¡Atención!"], &["¡Ánimo!"]] { - assert_eq!(guess_words(WINDOWS_1252, words, "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); - assert_eq!(guess_words(WINDOWS_1252, words, "Set 1").encoding, WINDOWS_1252, "{words:?}"); + fn an_inverted_exclamation_opening_a_word_reads_as_windows_1252() { + // windows-1252 ¡ (0xA1) is Mac Roman °: ¡ opening a word of two letters or more ("¡Hola!") + // counts for windows-1252 (review); after a number ("90°") or before a single letter + // ("20 °C") the byte stays the degree sign. + assert_eq!(guess_words(WINDOWS_1252, &["¡Hola!"], "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }); + assert_eq!(guess_words(WINDOWS_1252, &["¡Hola!"], "Set 1").encoding, WINDOWS_1252); + assert_eq!(guess_words(WINDOWS_1252, &["Capa ¡Atención!"], "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }); + for words in [&["Rotation 90°"][..], &["Temperatur 20 °C"]] { + assert_eq!(guess_words(MACINTOSH, words, "Configuración"), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?}"); + assert_eq!(guess_words(MACINTOSH, words, "Größe ändern"), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?}"); } - for words in [&["Rotation 90°"][..], &["Temperatur 20 °C"], &["20 °Celsius"]] { - for hint in ["Configuración", "Größe ändern", "Set 1"] { - assert_eq!(guess_words(MACINTOSH, words, hint), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?} with hint {hint:?}"); - } - } - } - - #[test] - fn a_lone_cyrillic_letter_after_a_digit_is_no_cyrillic_name() { - // Mac Roman "90°" alone has no ASCII letter, so its one windows-1251 letter (Ў) would make - // the name "mostly Cyrillic" (review): a lone Cyrillic letter right after a digit never - // counts. A one-letter Cyrillic name and a Russian unit after a digit still do. - for hint in ["Set 1", "Définir"] { - assert_eq!(guess_words(MACINTOSH, &["90°"], hint), Guess { encoding: MACINTOSH, uncertain: false }, "hint {hint:?}"); - } - assert_eq!(guess_words(WINDOWS_1251, &["Я"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); - assert_eq!(guess_words(WINDOWS_1251, &["5шт"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); } #[test] From 037a8c51672779df208c11e5e8e25afae7e956f9 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Tue, 3 Nov 2026 19:11:04 -0500 Subject: [PATCH 8/8] =?UTF-8?q?Load=20Actions:=20a=20lone=20Cyrillic=20let?= =?UTF-8?q?ter=20after=20a=20digit=20never=20counts,=20and=20=C2=A1=20coun?= =?UTF-8?q?ts=20only=20where=20it=20opens=20an=20exclamation=20closed=20by?= =?UTF-8?q?=20"!"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review: Mac Roman "90°" alone has no ASCII letter, so its one windows-1251 letter made the name "mostly Cyrillic" and the fallback showed "90Ў"; "¡Ánimo!" counted nothing for windows-1252 since only ASCII letters were counted after ¡; and Mac Roman "20 °Celsius" tied with windows-1252 "20 ¡Celsius". A lone Cyrillic letter right after a digit never counts (a run after a digit, "5шт", still does); the letters after a character include accented ones; and ¡ counts where it opens an exclamation, two letters or more after it and the closing "!" later in the name, which the degree sign before a unit never has. Re-lands 47ee3f696, which went out under another PR's message by mistake and is reverted just before this, with one test case less: without any hint a lone Mac Roman degree sign before an ASCII letter is also a Big5 punctuation pair, a tie the fallback flags, so the Mac Roman cases keep their Latin hints. Co-Authored-By: Claude Fable 5.1 --- crates/psd/src/codepage.rs | 93 ++++++++++++++++++++++++++------------ 1 file changed, 63 insertions(+), 30 deletions(-) diff --git a/crates/psd/src/codepage.rs b/crates/psd/src/codepage.rs index 9cd316f4f41..609beb98bc8 100644 --- a/crates/psd/src/codepage.rs +++ b/crates/psd/src/codepage.rs @@ -11,7 +11,7 @@ //! Roman). A candidate that cannot decode every name without errors is dropped. //! 2. Each survivor is scored by the share of non-ASCII bytes that fall in its common-text //! ranges (GB2312 hanzi, JIS level-1 kanji and kana, Big5 level 1, KS X 1001 Hangul, -//! letters, each page's punctuation, the degree sign, and ¡ opening a word: "¡Hola!"). The +//! letters, each page's punctuation, the degree sign, and ¡ opening an exclamation: "¡Hola!"). The //! highest share wins. Two single-byte readings don't count: the letters windows-1252 alone //! has at 0x80–0x9F (Š š Ž ž Ÿ ƒ ˆ), where Mac Roman keeps its accented lowercase (Mac //! "Opacité" is 1252 "OpacitŽ"), unless one starts a word ("Škoda", "šuma") in a file whose @@ -212,15 +212,15 @@ fn decodes_cleanly(enc: &'static Encoding, bytes: &[u8], truncated: bool) -> boo /// text writes (lowercase), which settle a word-initial letter of windows-1252's 0x80–0x9F. fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::BTreeSet) -> (u64, u64) { if enc == WINDOWS_1252 || enc == WINDOWS_1251 || enc == MACINTOSH { - // Letters, typographic punctuation, the degree sign ("Rotation 90°") and ¡ opening a word - // ("¡Hola!") are common, as the double-byte pages' punctuation rows are, with two readings + // Letters, typographic punctuation, the degree sign ("Rotation 90°") and ¡ opening an + // exclamation ("¡Hola!") are common, as the double-byte pages' punctuation rows are, with two readings // held back: the letters windows-1252 alone has at 0x80–0x9F, where Mac Roman keeps its // accented lowercase (Mac "Opacité" is 1252 "OpacitŽ"), unless one starts a word ("Škoda", // "ŠKODA", "šuma") and the file's own text doesn't write the Mac Roman letter instead (a // French "état", a German "öffnen"); and a windows-1251 Cyrillic letter glued into an ASCII // word (Mac "Höhe" is 1251 "Hшhe"), unless the name's letters are mostly Cyrillic // ("CMYKканал", Ukrainian "Копiя") or the letter is in a Cyrillic word of its own ("Layer - // Слой"). + // Слой"); a lone one right after a digit (Mac "90°" alone is 1251 "90Ў") never counts. let chars: Vec = decode(name, enc).chars().collect(); let cyrillic_name = chars.iter().filter(|c| is_cyrillic(**c)).count() >= chars.iter().filter(|c| c.is_ascii_alphabetic()).count(); let (mut common, mut high) = (0u64, 0u64); @@ -241,9 +241,9 @@ fn score(enc: &'static Encoding, name: &[u8], hint_letters: &std::collections::B let mac = name.get(i..=i).map(|b| decode(b, MACINTOSH)).and_then(|s| s.chars().next()); word_initial && !(mac.is_some_and(|m| m.is_alphabetic() && written(m)) && !written(ch)) } else if enc == WINDOWS_1251 && is_cyrillic(ch) { - cyrillic_name || cyrillic_word(&chars, i) + (cyrillic_name && !lone_after_digit(&chars, i)) || cyrillic_word(&chars, i) } else { - ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' || (ch == '¡' && opens_word(after_ascii, letters_after)) + ch.is_alphabetic() || TYPOGRAPHIC.contains(&ch) || ch == '°' || (ch == '¡' && opens_exclamation(&chars, i)) }; common += u64::from(counts); } @@ -296,29 +296,30 @@ fn single_byte_score(enc: &'static Encoding, name: &[u8], common: impl Fn(&[char /// Whether the character at `i` of `chars` is common in Western names, for weighing windows-1252 /// against Mac Roman: letters (but not those windows-1252 has only at 0x80–0x9F, where Mac Roman /// keeps its accented lowercase: Mac "Größe" is 1252 "Grš§e"), typographic punctuation, -/// [`LATIN_SYMBOLS`], and ¡ opening a word ("¡Hola!"), as in [`score`]. +/// [`LATIN_SYMBOLS`], and ¡ opening an exclamation ("¡Hola!"), as in [`score`]. fn is_latin_common(chars: &[char], i: usize) -> bool { let Some(&ch) = chars.get(i) else { return false }; - let (after_ascii, letters_after) = position(chars, i); (ch.is_alphabetic() && !RARE_1252_LETTERS.contains(&ch) && ch != 'Œ' && ch != 'œ') || TYPOGRAPHIC.contains(&ch) || LATIN_SYMBOLS.contains(&ch) - || (ch == '¡' && opens_word(after_ascii, letters_after)) + || (ch == '¡' && opens_exclamation(chars, i)) } /// Where the character at `i` of `chars` sits: right after an ASCII letter or digit (inside a word -/// or a number), and how many ASCII letters follow it. +/// or a number), and how many letters (accented ones too: "¡Ánimo!") follow it directly. fn position(chars: &[char], i: usize) -> (bool, usize) { let after_ascii = i.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - let letters_after = chars.get(i + 1..).map_or(0, |rest| rest.iter().take_while(|c| c.is_ascii_alphabetic()).count()); + let letters_after = chars.get(i + 1..).map_or(0, |rest| rest.iter().take_while(|c| c.is_alphabetic()).count()); (after_ascii, letters_after) } -/// Whether ¡ there opens a Spanish word ("¡Hola!", "¡Atención!"): two letters or more follow it -/// and no letter or digit precedes it. Mac Roman's degree sign on the same byte follows a number -/// ("Rotation 90°") or stands before a single letter ("20 °C"). -fn opens_word(after_ascii: bool, letters_after: usize) -> bool { - !after_ascii && letters_after >= 2 +/// Whether ¡ at `i` opens a Spanish exclamation ("¡Hola!", "Capa ¡Atención!", "¡Ánimo!"): no letter +/// or digit right before it, two letters or more after it, and the closing "!" later in the name. +/// Mac Roman's degree sign on the same byte follows a number ("Rotation 90°") or stands before a +/// unit ("20 °C", "20 °Celsius"), with no "!" to close. +fn opens_exclamation(chars: &[char], i: usize) -> bool { + let (after_ascii, letters_after) = position(chars, i); + !after_ascii && letters_after >= 2 && chars.get(i + 1..).is_some_and(|rest| rest.contains(&'!')) } /// The letters windows-1252 has at 0x80–0x9F and Mac Roman doesn't: there Mac Roman keeps its @@ -337,6 +338,14 @@ fn is_cyrillic(ch: char) -> bool { /// not "OpacitЋ", nor "90ЎЋ", which is Mac Roman "90°é": a digit ends a number, not a word, so /// what follows it without a space is its unit or suffix). fn cyrillic_word(chars: &[char], i: usize) -> bool { + let (start, end) = cyrillic_run(chars, i); + let glued_before = start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); + let glued_after = chars.get(end).is_some_and(|c| c.is_ascii_alphabetic()); + end - start >= 2 && !glued_before && !glued_after +} + +/// The run of Cyrillic letters around `i` (`start..end`). +fn cyrillic_run(chars: &[char], i: usize) -> (usize, usize) { let mut start = i; while start > 0 && chars.get(start - 1).is_some_and(|c| is_cyrillic(*c)) { start -= 1; @@ -345,9 +354,15 @@ fn cyrillic_word(chars: &[char], i: usize) -> bool { while chars.get(end).is_some_and(|c| is_cyrillic(*c)) { end += 1; } - let glued_before = start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_alphanumeric()); - let glued_after = chars.get(end).is_some_and(|c| c.is_ascii_alphabetic()); - end - start >= 2 && !glued_before && !glued_after + (start, end) +} + +/// Whether the Cyrillic letter at `i` stands alone right after a digit: a unit or a symbol misread +/// (Mac Roman "90°" is windows-1251 "90Ў"), whatever the rest of the name; a run after a digit is +/// a Russian unit ("5шт") and still counts. +fn lone_after_digit(chars: &[char], i: usize) -> bool { + let (start, end) = cyrillic_run(chars, i); + end - start == 1 && start.checked_sub(1).and_then(|k| chars.get(k)).is_some_and(|c| c.is_ascii_digit()) } /// Latin-1 symbols people type in names (windows-1252 0xA2–0xB7) whose byte is not a letter in @@ -355,7 +370,7 @@ fn cyrillic_word(chars: &[char], i: usize) -> bool { /// as in Mac "Weiß", "Størrelse", "Æble"), € and ™ (0x80, 0x99: Ä, ô), and ¥ (0xA5, Mac Roman's /// bullet in [`TYPOGRAPHIC`]: Mac "Ebene • Kopie"); and ¡ (0xA1), where Mac Roman has its degree /// sign, the commoner of the two in names (Mac "Rotation 90°", "20 °C"), which counts only where -/// it opens a word of two letters or more ("¡Hola!"; [`opens_word`], in [`score`] and +/// it opens an exclamation closed by "!" ("¡Hola!"; [`opens_exclamation`], in [`score`] and /// [`is_latin_common`]). So a windows-1252 name whose only non-ASCII character is one of the /// others ("Logo ®", "¿Tamaño?", "Preis ¥100") still reads as Mac Roman ("Logo Æ"). const LATIN_SYMBOLS: [char; 10] = ['¢', '£', '©', '«', '°', '±', '²', '³', '¶', '·']; @@ -602,17 +617,35 @@ mod tests { } #[test] - fn an_inverted_exclamation_opening_a_word_reads_as_windows_1252() { - // windows-1252 ¡ (0xA1) is Mac Roman °: ¡ opening a word of two letters or more ("¡Hola!") - // counts for windows-1252 (review); after a number ("90°") or before a single letter - // ("20 °C") the byte stays the degree sign. - assert_eq!(guess_words(WINDOWS_1252, &["¡Hola!"], "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }); - assert_eq!(guess_words(WINDOWS_1252, &["¡Hola!"], "Set 1").encoding, WINDOWS_1252); - assert_eq!(guess_words(WINDOWS_1252, &["Capa ¡Atención!"], "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }); - for words in [&["Rotation 90°"][..], &["Temperatur 20 °C"]] { - assert_eq!(guess_words(MACINTOSH, words, "Configuración"), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?}"); - assert_eq!(guess_words(MACINTOSH, words, "Größe ändern"), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?}"); + fn an_inverted_exclamation_opening_an_exclamation_reads_as_windows_1252() { + // windows-1252 ¡ (0xA1) is Mac Roman °: ¡ opening an exclamation, two letters or more + // after it (accented ones too) and the closing "!" later ("¡Hola!", "¡Ánimo!"), counts for + // windows-1252 (review); after a number ("90°"), before a unit ("20 °C", "20 °Celsius") or + // without a "!" the byte stays the degree sign. + for words in [&["¡Hola!"][..], &["Capa ¡Atención!"], &["¡Ánimo!"]] { + assert_eq!(guess_words(WINDOWS_1252, words, "Configuración"), Guess { encoding: WINDOWS_1252, uncertain: false }, "{words:?}"); + assert_eq!(guess_words(WINDOWS_1252, words, "Set 1").encoding, WINDOWS_1252, "{words:?}"); } + // (With a Latin hint: without any, a lone 0xA1 before an ASCII letter is also a Big5 + // punctuation pair, a tie the fallback flags.) + for words in [&["Rotation 90°"][..], &["Temperatur 20 °C"], &["20 °Celsius"]] { + for hint in ["Configuración", "Größe ändern"] { + assert_eq!(guess_words(MACINTOSH, words, hint), Guess { encoding: MACINTOSH, uncertain: false }, "{words:?} with hint {hint:?}"); + } + } + assert_eq!(guess_words(MACINTOSH, &["Rotation 90°"], "Set 1"), Guess { encoding: MACINTOSH, uncertain: false }); + } + + #[test] + fn a_lone_cyrillic_letter_after_a_digit_is_no_cyrillic_name() { + // Mac Roman "90°" alone has no ASCII letter, so its one windows-1251 letter (Ў) would make + // the name "mostly Cyrillic" (review): a lone Cyrillic letter right after a digit never + // counts. A one-letter Cyrillic name and a Russian unit after a digit still do. + for hint in ["Set 1", "Définir"] { + assert_eq!(guess_words(MACINTOSH, &["90°"], hint), Guess { encoding: MACINTOSH, uncertain: false }, "hint {hint:?}"); + } + assert_eq!(guess_words(WINDOWS_1251, &["Я"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); + assert_eq!(guess_words(WINDOWS_1251, &["5шт"], "Набор 1"), Guess { encoding: WINDOWS_1251, uncertain: false }); } #[test]