From 695b2688d8c88aacf1e491c99044cfae2eb8ed05 Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 19:19:25 -0100 Subject: [PATCH 001/308] Proofing options reach the Russian and Turkish checks too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #3966 made Ignore words in UPPERCASE and Ignore words that contain numbers reach six languages' own checks, one copy at a time, and Russian and Turkish were left out. lib.rs still called `ru::is_correct(word)` and `tr::is_correct(word)`, which accept words with digits (both) and short words in capitals (Russian) whatever the options say, so turning either option off changed nothing in those languages. Both now have `is_correct_with_options`, like the others. Russian's rule for a part in capitals (`ИИ-консультант`) follows the option too, as Indonesian's does, and its digits rule keeps counting superscripts (`м²`). Turkish had no test dictionary, so its two dictionary tests returned early wherever none was installed, CI included; it has a synthetic one now, also in the dictionaries-folder test. Tests: proofing_options_reach_the_russian_check and proofing_options_reach_the_turkish_check. Calling the old checks from lib.rs fails each, and so does ungating the Russian part rule or the Turkish hyphen recursion. --- crates/proof/src/lib.rs | 9 ++++--- crates/proof/src/ru/mod.rs | 45 +++++++++++++++++++++++++++----- crates/proof/src/tr/mod.rs | 39 ++++++++++++++++++++++----- crates/proof/src/tr/suggest.rs | 5 +--- crates/proof/src/tr/test_dict.rs | 22 ++++++++++++++++ 5 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 crates/proof/src/tr/test_dict.rs diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 8d0baba5e7..47216d72ac 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -778,8 +778,8 @@ pub fn is_correct_in_with(word: &str, lang: ProofLang, opts: &Options) -> bool { ProofLang::Hr => accepted_by_options(word, opts) || hr::is_correct_with_options(word, opts), ProofLang::Pt => accepted_by_options(word, opts) || pt::is_correct_with_options(word, opts), ProofLang::It => accepted_by_options(word, opts) || it::is_correct_with_options(word, opts), - ProofLang::Tr => accepted_by_options(word, opts) || tr::is_correct(word), - ProofLang::Ru => accepted_by_options(word, opts) || ru::is_correct(word), + ProofLang::Tr => accepted_by_options(word, opts) || tr::is_correct_with_options(word, opts), + ProofLang::Ru => accepted_by_options(word, opts) || ru::is_correct_with_options(word, opts), ProofLang::Es | ProofLang::EsMx | ProofLang::Es419 | ProofLang::EsUy => { accepted_by_options(word, opts) || es::is_correct_with_options(word, lang, opts) } @@ -1075,6 +1075,7 @@ mod tests { ("pt_BR", pt::test_dict::AFF, pt::test_dict::DIC), ("it_IT", it::test_dict::AFF, it::test_dict::DIC), ("hr_HR", hr::test_dict::AFF, hr::test_dict::DIC), + ("tr_TR", tr::test_dict::AFF, tr::test_dict::DIC), ("ru_RU", ru::test_dict::AFF, ru::test_dict::DIC), ("cs_CZ", cs::test_dict::AFF, cs::test_dict::DIC), ("id_ID", id::test_dict::AFF, id::test_dict::DIC), @@ -1094,9 +1095,9 @@ mod tests { let before = dictionary_generation(); set_dictionaries_folder(Some(dir.clone())); assert!(dictionary_generation() > before, "cached checks are dropped"); - // Turkish has no test dictionary to put in the folder; Russian, Czech, Indonesian and the Spanishes follow it. + // Turkish follows the four below; Russian, Czech, Indonesian and the Spanishes follow it. let found = installed_dictionaries(); - assert_eq!(found.get(4).map(|(lang, _)| *lang), Some(ProofLang::Tr)); + assert_eq!(found.get(4), Some(&(ProofLang::Tr, Some(dir.join("tr_TR.dic"))))); assert_eq!( found[7..], [ diff --git a/crates/proof/src/ru/mod.rs b/crates/proof/src/ru/mod.rs index c92e27125f..d5035e6a31 100644 --- a/crates/proof/src/ru/mod.rs +++ b/crates/proof/src/ru/mod.rs @@ -130,18 +130,30 @@ pub fn is_correct(word: &str) -> bool { is_correct_with(&dictionary(), word) } +/// [`is_correct`] with the File › Options › Proofing choices: with Ignore words in UPPERCASE off, +/// short words in capitals are checked against the dictionary too, and with Ignore words that +/// contain numbers off, so are words with digits or superscripts. +pub fn is_correct_with_options(word: &str, opts: &crate::Options) -> bool { + check(&dictionary(), word, opts) +} + /// [`is_correct`] against a given dictionary (an empty one accepts everything). pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { + check(d, word, &crate::Options::default()) +} + +/// [`is_correct_with_options`] against a given dictionary. +fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { if d.is_empty() { return true; // no Russian dictionary installed: no spelling marks } let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '-'); let w = w.trim_matches('-'); let n = w.chars().count(); - if n <= 1 || w.chars().any(char::is_numeric) || !w.chars().any(char::is_alphabetic) { + if n <= 1 || (opts.ignore_numbers && w.chars().any(char::is_numeric)) || !w.chars().any(char::is_alphabetic) { return true; } - if w.chars().all(|c| !c.is_lowercase()) && n <= ABBREVIATION_LETTERS { + if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= ABBREVIATION_LETTERS { return true; } // Camel case (`ВКонтакте`, `МирКрасок`): a brand or an identifier, not a dictionary word. @@ -161,7 +173,7 @@ pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { return true; } if w.contains('-') { - return hyphenated(d, w); + return hyphenated(d, w, opts); } solid_compound(d, w) } @@ -340,7 +352,7 @@ fn known(d: &AffixDict, w: &str) -> bool { /// A hyphenated word that isn't in the dictionary whole: its parts, with the particles, /// prefixes and linking vowels Russian joins with a hyphen. -fn hyphenated(d: &AffixDict, w: &str) -> bool { +fn hyphenated(d: &AffixDict, w: &str, opts: &crate::Options) -> bool { let parts: Vec<&str> = w.split('-').collect(); if parts.iter().any(|p| p.is_empty()) || parts.len() > 4 { return false; @@ -353,7 +365,7 @@ fn hyphenated(d: &AffixDict, w: &str) -> bool { && lp.iter().skip(k).all(|p| PARTICLES.contains(p)) { let base = parts.get(..k).map(|b| b.join("-")).unwrap_or_default(); - return known(d, &base) || (base.contains('-') && hyphenated(d, &base)); + return known(d, &base) || (base.contains('-') && hyphenated(d, &base, opts)); } match lp.as_slice() { // `кое-кто`, `кой-какой`. @@ -369,7 +381,7 @@ fn hyphenated(d: &AffixDict, w: &str) -> bool { let last = parts.len() - 1; parts.iter().enumerate().all(|(i, p)| { // An abbreviation part (`ИИ-консультант`, `SMS-рассылка`) or a known word. - if known(d, p) || (p.chars().all(|c| !c.is_lowercase()) && p.chars().count() <= ABBREVIATION_LETTERS) { + if known(d, p) || (opts.ignore_uppercase && p.chars().all(|c| !c.is_lowercase()) && p.chars().count() <= ABBREVIATION_LETTERS) { return true; } // A first part with a linking vowel: the stem (`северо` → `север`) or its adjective @@ -497,4 +509,25 @@ mod tests { crate::add_word("Зкщфрусскоеслово"); assert!(is_correct("зкщфрусскоеслово")); } + + /// With Ignore words in UPPERCASE or Ignore words that contain numbers off, Russian checks + /// those words against the dictionary instead of accepting them. + #[test] + fn proofing_options_reach_the_russian_check() { + test_dict::install(); + let lang = crate::ProofLang::Ru; + let defaults = crate::Options::default(); + let check_caps = crate::Options { ignore_uppercase: false, ..defaults }; + let check_digits = crate::Options { ignore_numbers: false, ..defaults }; + assert!(crate::is_correct_in_with("ПРЕВЕТ", lang, &defaults), "short capitals are abbreviations by default"); + assert!(!crate::is_correct_in_with("ПРЕВЕТ", lang, &check_caps)); + assert!(crate::is_correct_in_with("ПРИВЕТ", lang, &check_caps)); + assert!(crate::is_correct_in_with("ИИ-консультант", lang, &defaults)); + assert!(!crate::is_correct_in_with("ИИ-консультант", lang, &check_caps), "a part in capitals is checked too"); + assert!(crate::is_correct_in_with("превет2", lang, &defaults)); + assert!(!crate::is_correct_in_with("превет2", lang, &check_digits)); + assert!(!crate::is_correct_in_with("м²", lang, &check_digits), "a superscript is a number too"); + assert!(crate::is_correct_in_with("2026", lang, &check_digits), "numbers alone stay correct"); + assert!(is_correct("ПРЕВЕТ"), "direct checks keep the defaults"); + } } diff --git a/crates/proof/src/tr/mod.rs b/crates/proof/src/tr/mod.rs index bcab1546bf..ab3fe92d72 100644 --- a/crates/proof/src/tr/mod.rs +++ b/crates/proof/src/tr/mod.rs @@ -15,6 +15,8 @@ pub mod dict; pub mod grammar; pub mod hyphen; pub mod suggest; +#[cfg(test)] +pub(crate) mod test_dict; use std::sync::{Arc, RwLock}; @@ -87,15 +89,26 @@ pub fn is_correct(word: &str) -> bool { is_correct_with(&dictionary(), word) } +/// [`is_correct`] with the File › Options › Proofing choices: with Ignore words that contain +/// numbers off, words with digits are checked against the dictionary too. +pub fn is_correct_with_options(word: &str, opts: &crate::Options) -> bool { + check(&dictionary(), word, opts) +} + /// [`is_correct`] against a given dictionary. pub fn is_correct_with(d: &TrDict, word: &str) -> bool { + check(d, word, &crate::Options::default()) +} + +/// [`is_correct_with_options`] against a given dictionary. +fn check(d: &TrDict, word: &str, opts: &crate::Options) -> bool { if d.is_empty() { return true; // No Turkish dictionary installed: no spelling marks } let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); let w = w.trim_matches(['\'', '’']); let n = w.chars().count(); - if n <= 1 || w.chars().any(|c| c.is_ascii_digit()) || !w.chars().any(char::is_alphabetic) { + if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { return true; } // Check user dictionary first @@ -120,7 +133,7 @@ pub fn is_correct_with(d: &TrDict, word: &str) -> bool { } // Compound words separated by hyphens (e.g., `afyon-karahisar`) - if w.contains('-') && w.split('-').all(|part| part.is_empty() || is_correct_with(d, part)) { + if w.contains('-') && w.split('-').all(|part| part.is_empty() || check(d, part, opts)) { return true; } @@ -146,11 +159,8 @@ mod tests { } #[test] - fn installed_dictionary_checks_words_if_present() { - // Machines without a Turkish Hunspell dictionary (CI, most desktops) have nothing to check. - if !has_dictionary() { - return; - } + fn installed_dictionary_checks_words() { + test_dict::install(); assert!(is_correct("kitap")); assert!(is_correct("kitaplar")); assert!(is_correct("evde")); @@ -165,4 +175,19 @@ mod tests { crate::add_word("benimbilmedigimkelime"); assert!(is_correct("benimbilmedigimkelime")); } + + /// With Ignore words that contain numbers off, Turkish checks words with digits against the + /// dictionary instead of accepting them, hyphen parts included. + #[test] + fn proofing_options_reach_the_turkish_check() { + test_dict::install(); + let lang = crate::ProofLang::Tr; + let defaults = crate::Options::default(); + let check_digits = crate::Options { ignore_numbers: false, ..defaults }; + assert!(crate::is_correct_in_with("kitapp2", lang, &defaults)); + assert!(!crate::is_correct_in_with("kitapp2", lang, &check_digits)); + assert!(!crate::is_correct_in_with("ev-kitapp2", lang, &check_digits)); + assert!(crate::is_correct_in_with("2026", lang, &check_digits), "numbers alone stay correct"); + assert!(is_correct("kitapp2"), "direct checks keep the defaults"); + } } diff --git a/crates/proof/src/tr/suggest.rs b/crates/proof/src/tr/suggest.rs index dedc1bb266..f91f8e3c9b 100644 --- a/crates/proof/src/tr/suggest.rs +++ b/crates/proof/src/tr/suggest.rs @@ -285,10 +285,7 @@ mod tests { #[test] fn typo_transposition_is_suggested() { - // Suggestions come from the installed Turkish dictionary; without one there are none. - if !super::super::has_dictionary() { - return; - } + crate::tr::test_dict::install(); let sugs = suggest("sorna", 3); assert!(sugs.contains(&"sonra".to_string()), "sorna should suggest sonra, got {sugs:?}"); diff --git a/crates/proof/src/tr/test_dict.rs b/crates/proof/src/tr/test_dict.rs new file mode 100644 index 0000000000..de4a13195a --- /dev/null +++ b/crates/proof/src/tr/test_dict.rs @@ -0,0 +1,22 @@ +//! A tiny synthetic Turkish Hunspell dictionary for tests (written for these tests; not taken from +//! any real dictionary). Real dictionaries are installed on the system, never bundled. + +/// No affix rules: the forms the tests look up are listed whole. +pub const AFF: &str = "SET UTF-8\n"; + +/// The words the Turkish tests expect to be known. +pub const DIC: &str = "7 +kitap +kitaplar +ev +evde +olacaklardan +sonra +geldin +"; + +pub fn install() { + if let Err(e) = super::install_dictionary(AFF, DIC) { + panic!("test dictionary: {e}"); + } +} From 281392ac154286573ba283ee3456411fb8f97540 Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 19:21:57 -0100 Subject: [PATCH 002/308] Croatian, Italian and Russian suggestions skip NOSUGGEST words #1048 (6e25d482c) taught affix.rs about NOSUGGEST, the dictionary flag for words that are correct but never offered (vulgarisms, rare forms), and patched the Polish and Portuguese copies of the suggestion engine; Czech's started with it. The Croatian, Italian and Russian copies never got it: they test candidates with `contains`, so a suggestion could be a word the dictionary says not to offer. They now use `contains_suggestible` and drop a suggestion with any part `can_suggest` refuses, as the others do. Tests: Croatian and Italian run the shared assert_nosuggest_is_honoured. Russian gets its own, since its MAP and REP tables are built in and the shared Latin-script check doesn't reach them. Each fails on the old code. --- crates/proof/src/hr/suggest.rs | 11 +++++++++-- crates/proof/src/it/suggest.rs | 12 ++++++++++-- crates/proof/src/ru/suggest.rs | 23 +++++++++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/crates/proof/src/hr/suggest.rs b/crates/proof/src/hr/suggest.rs index 875bf98c73..d6f8159aca 100644 --- a/crates/proof/src/hr/suggest.rs +++ b/crates/proof/src/hr/suggest.rs @@ -52,11 +52,11 @@ impl Collector<'_> { let mut words = Vec::new(); let mut cost = cost; for part in cand.split(' ') { - if self.d.contains(part) { + if self.d.contains_suggestible(part) { words.push(part.to_string()); } else { let c = capitalize(part); - if !self.d.contains(&c) { + if !self.d.contains_suggestible(&c) { return; } words.push(c); @@ -69,6 +69,9 @@ impl Collector<'_> { /// Keep a suggestion known to be correct. fn keep(&mut self, text: String, cost: u32) { + if !text.split(' ').all(|part| self.d.can_suggest(part)) { + return; + } self.seq += 1; let seq = self.seq; let e = self.found.entry(text).or_insert((cost, seq)); @@ -348,6 +351,10 @@ fn scan(col: &mut Collector, chars: &[char]) { mod tests { use super::*; #[test] + fn nosuggest_stems_and_forms_are_never_offered() { + crate::affix::tests::assert_nosuggest_is_honoured(suggest_in); + } + #[test] fn croatian_suggestions() { crate::hr::test_dict::install(); for (wrong, right) in [("kuca", "kuća"), ("racunalo", "računalo"), ("covjek", "čovjek"), ("rijec", "riječ"), ("skolla", "škola")] { diff --git a/crates/proof/src/it/suggest.rs b/crates/proof/src/it/suggest.rs index 500d1c09d8..6a043ff2b4 100644 --- a/crates/proof/src/it/suggest.rs +++ b/crates/proof/src/it/suggest.rs @@ -73,11 +73,11 @@ impl Collector<'_> { let mut words = Vec::new(); let mut cost = cost; for part in cand.split(' ') { - if self.d.contains(part) { + if self.d.contains_suggestible(part) { words.push(part.to_string()); } else { let c = capitalize(part); - if !self.d.contains(&c) { + if !self.d.contains_suggestible(&c) { return; } words.push(c); @@ -90,6 +90,9 @@ impl Collector<'_> { /// Keep a suggestion known to be correct. fn keep(&mut self, text: String, cost: u32) { + if !text.split(' ').all(|part| self.d.can_suggest(part)) { + return; + } self.seq += 1; let seq = self.seq; let e = self.found.entry(text).or_insert((cost, seq)); @@ -371,6 +374,11 @@ mod tests { assert!(s.iter().any(|x| x == want), "{word} → {s:?}, want {want}"); } + #[test] + fn nosuggest_stems_and_forms_are_never_offered() { + crate::affix::tests::assert_nosuggest_is_honoured(suggest_in); + } + #[test] fn italian_misspellings_get_the_right_suggestion() { crate::it::test_dict::install(); diff --git a/crates/proof/src/ru/suggest.rs b/crates/proof/src/ru/suggest.rs index cb4c3791df..24eeb7642b 100644 --- a/crates/proof/src/ru/suggest.rs +++ b/crates/proof/src/ru/suggest.rs @@ -161,11 +161,11 @@ impl Collector<'_> { let mut words = Vec::new(); let mut cost = cost; for part in cand.split(' ') { - if self.d.contains(part) { + if self.d.contains_suggestible(part) { words.push(part.to_string()); } else { let c = capitalize(part); - if !self.d.contains(&c) { + if !self.d.contains_suggestible(&c) { return; } words.push(c); @@ -178,6 +178,9 @@ impl Collector<'_> { /// Keep a suggestion known to be correct. fn keep(&mut self, text: String, cost: u32) { + if !text.split(' ').all(|part| self.d.can_suggest(part)) { + return; + } self.seq += 1; let seq = self.seq; let e = self.found.entry(text).or_insert((cost, seq)); @@ -458,6 +461,22 @@ mod tests { assert!(s.iter().any(|x| x == want), "{word} → {s:?}, want {want}"); } + /// A word the dictionary marks NOSUGGEST (a vulgarism, a rare form) is correct but never + /// offered, in any round or letter case. Russian's MAP and REP tables are its own, so the + /// shared Latin-script check doesn't reach it. + #[test] + fn nosuggest_words_are_never_offered() { + let offered = |aff: &str, input: &str| { + let d = AffixDict::from_hunspell(aff, "2\nпривет/N\nпредмет/N\n", &[]).unwrap(); + suggest_in(&d, input, 16) + }; + assert!(offered("SET UTF-8\n", "превет").contains(&"привет".into()), "offered without the flag declared"); + for input in ["превет", "ПРЕВЕТ", "предмот", "приветт", "привте", "привет"] { + let s = offered("SET UTF-8\nNOSUGGEST N\n", input); + assert!(!s.iter().any(|w| w.to_lowercase().contains("привет") || w.to_lowercase().contains("предмет")), "{input}: {s:?}"); + } + } + #[test] fn russian_misspellings_get_the_right_suggestion() { crate::ru::test_dict::install(); From d2f45ddd966f896090fd840246c280fb4cced2c7 Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 19:33:17 -0100 Subject: [PATCH 003/308] Proofing: one dictionary slot for every language Nine languages each kept their own copy of the same code: the installed dictionary behind a lock, read on first use and logged, replaced by install_dictionary, forgotten by reload_dictionary, and its file reported by installed_path. The copies had drifted already. Every one documented that a dictionary given with install_dictionary has no file and isn't reported, but only Russian and Czech did that (#3252); the other seven reported the disk file anyway. `DictSlot` (proof/src/slot.rs) is that code once. It is generic over the dictionary type, so Turkish's `TrDict` fits, and keeps the dictionary and its in-memory origin under one lock as #3252 made it. Each language keeps its functions as one-line wrappers over its slot, so no caller changes, and the log lines are the same. installed_path now follows the documented rule in every language. Nothing outside tests installs a dictionary in memory, but tests do, concurrently, so the dictionaries-folder tests in proof and engine could race on the reported path of a language another test installs. They now accept the folder's file or none there, and check that the folder is searched first with hunspell::find, which no install affects. Tests: proof 279, engine 927, layout 501 (lib); slot.rs has a test of its own. --- crates/engine/src/tests.rs | 18 +++-- crates/proof/src/cs/mod.rs | 60 +++------------ crates/proof/src/es/mod.rs | 71 ++++++----------- crates/proof/src/hr/mod.rs | 45 +++-------- crates/proof/src/id/mod.rs | 50 ++++-------- crates/proof/src/it/mod.rs | 44 +++-------- crates/proof/src/lib.rs | 50 ++++++------ crates/proof/src/pl/mod.rs | 52 ++++--------- crates/proof/src/pt/mod.rs | 44 +++-------- crates/proof/src/ru/mod.rs | 68 ++++------------ crates/proof/src/slot.rs | 150 ++++++++++++++++++++++++++++++++++++ crates/proof/src/tr/dict.rs | 15 ++++ crates/proof/src/tr/mod.rs | 57 ++++---------- 13 files changed, 338 insertions(+), 386 deletions(-) create mode 100644 crates/proof/src/slot.rs diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 7c6ad50e71..616d95557a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7056,14 +7056,22 @@ fn dictionaries_folder_option_is_saved_searched_and_reported() { assert_eq!(r["dictionariesFolder"], json!(dir.to_string_lossy())); let dicts = r["dictionaries"].as_array().unwrap(); let file = |lang: &str| dicts.iter().find(|d| d["language"] == lang).map(|d| d["file"].clone()); - assert_eq!(file("pl"), Some(json!(dir.join("pl_PL.dic").to_string_lossy()))); - assert_eq!(file("pt"), Some(json!(dir.join("pt_BR.dic").to_string_lossy()))); - assert_eq!(file("id"), Some(json!(dir.join("id_ID.dic").to_string_lossy()))); + // A language reports the folder's file, or none while a dictionary another test gave with + // install_dictionary() is in use (those tests run concurrently); the folder is searched first + // either way. + let reported = |got: Option<&serde_json::Value>, name: &str| { + let want = dir.join(format!("{name}.dic")); + assert!(got.is_some_and(|f| f.is_null() || *f == json!(want.to_string_lossy())), "{name}: {dicts:?}"); + assert_eq!(wordcraft_proof::hunspell::find(&[name]).map(|(_, dic)| dic), Some(want)); + }; + reported(file("pl").as_ref(), "pl_PL"); + reported(file("pt").as_ref(), "pt_BR"); + reported(file("id").as_ref(), "id_ID"); // Each Spanish variety reports its own file (Spain, Mexico, Latin America, Uruguay, in order). let spanish: Vec = dicts.iter().filter(|d| d["language"] == "es").map(|d| d["file"].clone()).collect(); assert_eq!(spanish.len(), 4); - assert_eq!(spanish.first(), Some(&json!(dir.join("es_ES.dic").to_string_lossy()))); - assert_eq!(spanish.last(), Some(&json!(dir.join("es_UY.dic").to_string_lossy()))); + reported(spanish.first(), "es_ES"); + reported(spanish.last(), "es_UY"); // Polish text is checked against the folder's dictionary. run(&mut s, "document.setText", json!({"text": "rzeka płynie żeka"})); run(&mut s, "select.all", json!({})); diff --git a/crates/proof/src/cs/mod.rs b/crates/proof/src/cs/mod.rs index fee7c561ce..55e21aef6d 100644 --- a/crates/proof/src/cs/mod.rs +++ b/crates/proof/src/cs/mod.rs @@ -7,83 +7,47 @@ pub(crate) mod suggest; #[cfg(test)] pub(crate) mod test_dict; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use crate::affix::AffixDict; +use crate::slot::DictSlot; pub use suggest::suggest; -/// The Czech dictionary and whether it came from [`install_dictionary`] rather than a file on -/// disk (it then has no file of its own to report from [`installed_path`], even if a matching -/// file also happens to exist): one lock, so a reload and an install can't interleave their two -/// fields into an inconsistent combination. -static DICT: RwLock = RwLock::new(DictState { dict: None, in_memory: false }); - -struct DictState { - dict: Option>, - in_memory: bool, -} - /// File names of a Czech Hunspell dictionary. const NAMES: &[&str] = &["cs_CZ", "cs-CZ", "cs"]; +/// The Czech dictionary, found on first use (see [`DictSlot`]). +static DICT: DictSlot = DictSlot::new(NAMES, "Czech"); + /// The Czech dictionary: the installed Hunspell `cs_CZ` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Czech spelling is off /// then; see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).dict.as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed()); - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.dict.get_or_insert(loaded).clone() -} - -fn load_installed() -> AffixDict { - let Some((aff, dic)) = crate::hunspell::find(NAMES) else { - crate::hunspell::clear_load_error(NAMES[0]); - log::info!("Czech spelling: no Hunspell cs_CZ dictionary installed"); - return AffixDict::default(); - }; - crate::hunspell::note_load(NAMES[0], &dic, crate::hunspell::load(&aff, &dic)).unwrap_or_else(|e| { - log::warn!("Czech spelling: {e}"); - AffixDict::default() - }) + DICT.get() } /// Use this Czech dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to /// UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { - let d = AffixDict::from_hunspell(aff, dic, &[])?; - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - slot.dict = Some(Arc::new(d)); - slot.in_memory = true; - Ok(()) + DICT.install(aff, dic) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - slot.dict = None; - slot.in_memory = false; + DICT.reload(); } -/// The `.dic` file of the installed Czech dictionary, searching the dictionaries folder and -/// the system locations now (File › Options › Proofing shows it, #3226); `None` when there is -/// none. A dictionary given with [`install_dictionary`] has no file and isn't reported, even if a -/// matching file also happens to exist. +/// The `.dic` file of the installed Czech dictionary (File › Options › Proofing shows it); +/// `None` when there is none, or when one given with [`install_dictionary`] is in use. pub fn installed_path() -> Option { - if DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).in_memory { - return None; - } - crate::hunspell::find(NAMES).map(|(_, dic)| dic) + DICT.installed_path() } /// Is a Czech dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { - !dictionary().is_empty() + DICT.has_dictionary() } /// Is `c` a letter of the Latin script (the only one the Czech dictionary covers)? diff --git a/crates/proof/src/es/mod.rs b/crates/proof/src/es/mod.rs index 94c6511abd..253642c83b 100644 --- a/crates/proof/src/es/mod.rs +++ b/crates/proof/src/es/mod.rs @@ -16,16 +16,19 @@ mod suggest; pub(crate) mod test_dict; pub mod voseo; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use crate::ProofLang; use crate::affix::AffixDict; +use crate::slot::DictSlot; pub use suggest::suggest; -/// The dictionary of each variety (Spain, Mexico, Latin America, Uruguay): `None` until first -/// use, then the installed one (or an empty one). -static DICTS: [RwLock>>; 4] = [const { RwLock::new(None) }; 4]; +/// The dictionary of each variety, found on first use (see [`DictSlot`]). +static SPAIN_DICT: DictSlot = DictSlot::new(SPAIN, "Spanish (Spain)"); +static MEXICO_DICT: DictSlot = DictSlot::new(MEXICO, "Spanish (Mexico)"); +static LATIN_AMERICA_DICT: DictSlot = DictSlot::new(LATIN_AMERICA, "Spanish (Latin America)"); +static URUGUAY_DICT: DictSlot = DictSlot::new(URUGUAY, "Spanish (Uruguay)"); /// File names of a Spanish Hunspell dictionary for Spain, most specific first (`es_ANY` is the /// generic one RLA-ES publishes). @@ -40,79 +43,53 @@ const LATIN_AMERICA: &[&str] = &["es_419", "es-419", "es_ANY", "es", "es_US", "e /// or generic one. const URUGUAY: &[&str] = &["es_UY", "es-UY", "es_AR", "es-AR", "es_419", "es-419", "es_ANY", "es"]; -/// `lang`'s variety: its slot in [`DICTS`], its English name (for the log) and its dictionary's -/// file names; Spain for any other language. -fn variety(lang: ProofLang) -> (usize, &'static str, &'static [&'static str]) { +/// `lang`'s variety's dictionary; Spain's for any other language. +fn slot(lang: ProofLang) -> &'static DictSlot { match lang { - ProofLang::EsMx => (1, "Spanish (Mexico)", MEXICO), - ProofLang::Es419 => (2, "Spanish (Latin America)", LATIN_AMERICA), - ProofLang::EsUy => (3, "Spanish (Uruguay)", URUGUAY), - _ => (0, "Spanish (Spain)", SPAIN), + ProofLang::EsMx => &MEXICO_DICT, + ProofLang::Es419 => &LATIN_AMERICA_DICT, + ProofLang::EsUy => &URUGUAY_DICT, + _ => &SPAIN_DICT, } } /// The Hunspell name each variety's dictionary is known by (`es_ES`, `es_MX`, `es_419`, /// `es_UY`): the first of its file names, used for the status-bar hint and load errors. pub fn dictionary_name(lang: ProofLang) -> &'static str { - variety(lang).2.first().copied().unwrap_or("es_ES") + slot(lang).name() } /// The Spanish dictionary of `lang`'s variety: the installed one (read on first use), one given /// with [`install_dictionary`], or an empty one when none is available (spelling is off then; /// see [`has_dictionary`]). pub fn dictionary(lang: ProofLang) -> Arc { - let Some(slot) = DICTS.get(variety(lang).0) else { return Arc::new(AffixDict::default()) }; - if let Some(d) = slot.read().unwrap_or_else(|e| e.into_inner()).as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed(lang)); - let mut slot = slot.write().unwrap_or_else(|e| e.into_inner()); - // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.get_or_insert(loaded).clone() -} - -fn load_installed(lang: ProofLang) -> AffixDict { - let (_, name, files) = variety(lang); - let key = dictionary_name(lang); - let Some((aff, dic)) = crate::hunspell::find(files) else { - crate::hunspell::clear_load_error(key); - log::info!("{name} spelling: no Hunspell {key} dictionary installed"); - return AffixDict::default(); - }; - crate::hunspell::note_load(key, &dic, crate::hunspell::load(&aff, &dic)).unwrap_or_else(|e| { - log::warn!("{name} spelling: {e}"); - AffixDict::default() - }) + slot(lang).get() } /// Use this dictionary for `lang`'s variety from now on (Hunspell `.aff` and `.dic` text, /// decoded to UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(lang: ProofLang, aff: &str, dic: &str) -> Result<(), String> { - let d = AffixDict::from_hunspell(aff, dic, &[])?; - if let Some(slot) = DICTS.get(variety(lang).0) { - *slot.write().unwrap_or_else(|e| e.into_inner()) = Some(Arc::new(d)); - } - Ok(()) + slot(lang).install(aff, dic) } /// Forget every variety's loaded dictionary, so the next check looks for an installed one again /// (after [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - for slot in &DICTS { - *slot.write().unwrap_or_else(|e| e.into_inner()) = None; + for slot in [&SPAIN_DICT, &MEXICO_DICT, &LATIN_AMERICA_DICT, &URUGUAY_DICT] { + slot.reload(); } } /// The `.dic` file of the Hunspell dictionary `lang`'s variety finds in the dictionaries folder -/// or the system locations now (File › Options › Proofing shows it); `None` when there is none. -/// A dictionary given with [`install_dictionary`] has no file and isn't reported. +/// or the system locations now (File › Options › Proofing shows it); `None` when there is none, +/// or when one given with [`install_dictionary`] is in use. pub fn installed_path(lang: ProofLang) -> Option { - crate::hunspell::find(variety(lang).2).map(|(_, dic)| dic) + slot(lang).installed_path() } /// Is a dictionary available for `lang`'s variety (spelling is checked)? pub fn has_dictionary(lang: ProofLang) -> bool { - !dictionary(lang).is_empty() + slot(lang).has_dictionary() } /// Is `c` a letter of the Latin script (the only one the Spanish dictionaries cover)? @@ -200,7 +177,7 @@ mod tests { std::fs::write(dir.join(format!("{name}.aff")), test_dict::AFF).unwrap(); std::fs::write(dir.join(format!("{name}.dic")), test_dict::DIC).unwrap(); for lang in VARIETIES { - let found = crate::hunspell::find_in(&dir, variety(lang).2); + let found = crate::hunspell::find_in(&dir, slot(lang).names()); assert_eq!(found.is_some(), found_by.contains(&lang), "{name}: {lang:?}"); if let Some((aff, dic)) = found { assert!(is_correct_with(&crate::hunspell::load(&aff, &dic).unwrap(), "canción", lang)); @@ -215,7 +192,7 @@ mod tests { std::fs::write(dir.join(format!("{name}.dic")), test_dict::DIC).unwrap(); } for (lang, name) in [(es, "es_ES"), (mx, "es_MX"), (la, "es_419"), (uy, "es_UY")] { - assert_eq!(crate::hunspell::find_in(&dir, variety(lang).2).map(|(_, dic)| dic), Some(dir.join(format!("{name}.dic")))); + assert_eq!(crate::hunspell::find_in(&dir, slot(lang).names()).map(|(_, dic)| dic), Some(dir.join(format!("{name}.dic")))); assert_eq!(dictionary_name(lang), name); } std::fs::remove_dir_all(dir).unwrap(); diff --git a/crates/proof/src/hr/mod.rs b/crates/proof/src/hr/mod.rs index ee9d58e92a..6372a47d27 100644 --- a/crates/proof/src/hr/mod.rs +++ b/crates/proof/src/hr/mod.rs @@ -9,68 +9,47 @@ mod suggest; #[cfg(test)] pub(crate) mod test_dict; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use crate::affix::AffixDict; +use crate::slot::DictSlot; pub use suggest::suggest; -/// The Croatian dictionary: `None` until first use, then the installed one (or an empty one). -static DICT: RwLock>> = RwLock::new(None); - /// File names of a Croatian Hunspell dictionary. const NAMES: &[&str] = &["hr_HR", "hr-HR", "hr"]; +/// The Croatian dictionary, found on first use (see [`DictSlot`]). +static DICT: DictSlot = DictSlot::new(NAMES, "Croatian"); + /// The Croatian dictionary: the installed Hunspell `hr_HR` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Croatian spelling is off /// then; see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed()); - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.get_or_insert(loaded).clone() -} - -fn load_installed() -> AffixDict { - let Some((aff, dic)) = crate::hunspell::find(NAMES) else { - crate::hunspell::clear_load_error(NAMES[0]); - log::info!("Croatian spelling: no Hunspell hr_HR dictionary installed"); - return AffixDict::default(); - }; - - crate::hunspell::note_load(NAMES[0], &dic, crate::hunspell::load(&aff, &dic)).unwrap_or_else(|e| { - log::warn!("Croatian spelling: {e}"); - AffixDict::default() - }) + DICT.get() } /// Use this Croatian dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to /// UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { - let d = AffixDict::from_hunspell(aff, dic, &[])?; - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::new(d)); - Ok(()) + DICT.install(aff, dic) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = None; + DICT.reload(); } -/// The `.dic` file of the Hunspell `hr_HR` dictionary found in the dictionaries folder or the -/// system locations now (File › Options › Proofing shows it, #1063); `None` when there is none. -/// A dictionary given with [`install_dictionary`] has no file and isn't reported. +/// The `.dic` file of the installed Croatian dictionary (File › Options › Proofing shows it); +/// `None` when there is none, or when one given with [`install_dictionary`] is in use. pub fn installed_path() -> Option { - crate::hunspell::find(NAMES).map(|(_, dic)| dic) + DICT.installed_path() } /// Is a Croatian dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { - !dictionary().is_empty() + DICT.has_dictionary() } /// Is `c` a letter of the Latin script (the only one the Croatian dictionary covers)? diff --git a/crates/proof/src/id/mod.rs b/crates/proof/src/id/mod.rs index 012babc0e1..65b820dfc5 100644 --- a/crates/proof/src/id/mod.rs +++ b/crates/proof/src/id/mod.rs @@ -9,74 +9,54 @@ mod suggest; #[cfg(test)] pub(crate) mod test_dict; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use crate::affix::AffixDict; +use crate::slot::DictSlot; pub use suggest::suggest; -/// The Indonesian dictionary: `None` until first use, then the installed one (or an empty one). -static DICT: RwLock>> = RwLock::new(None); - /// File names of an Indonesian Hunspell dictionary (`in` is the old ISO 639 code, still used by /// Java locales). const NAMES: &[&str] = &["id_ID", "id-ID", "id", "in_ID"]; -/// Clitics PUEBI joins with a hyphen to a capitalized word, an abbreviation or a number -/// (`se-Indonesia`, `KTP-nya`, `Jakarta-lah`); they aren't dictionary words on their own. -const CLITICS: &[&str] = &["se", "nya", "ku", "mu", "lah", "kah", "pun"]; +/// The Indonesian dictionary, found on first use (see [`DictSlot`]). +static DICT: DictSlot = DictSlot::new(NAMES, "Indonesian"); /// The Indonesian dictionary: the installed Hunspell `id_ID` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Indonesian spelling is off /// then; see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed()); - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.get_or_insert(loaded).clone() -} - -fn load_installed() -> AffixDict { - let Some((aff, dic)) = crate::hunspell::find(NAMES) else { - crate::hunspell::clear_load_error(NAMES[0]); - log::info!("Indonesian spelling: no Hunspell id_ID dictionary installed"); - return AffixDict::default(); - }; - crate::hunspell::note_load(NAMES[0], &dic, crate::hunspell::load(&aff, &dic)).unwrap_or_else(|e| { - log::warn!("Indonesian spelling: {e}"); - AffixDict::default() - }) + DICT.get() } /// Use this Indonesian dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to /// UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { - let d = AffixDict::from_hunspell(aff, dic, &[])?; - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::new(d)); - Ok(()) + DICT.install(aff, dic) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = None; + DICT.reload(); } -/// The `.dic` file of the Hunspell `id_ID` dictionary found in the dictionaries folder or the -/// system locations now (File › Options › Proofing shows it); `None` when there is none. A -/// dictionary given with [`install_dictionary`] has no file and isn't reported. +/// The `.dic` file of the installed Indonesian dictionary (File › Options › Proofing shows it); +/// `None` when there is none, or when one given with [`install_dictionary`] is in use. pub fn installed_path() -> Option { - crate::hunspell::find(NAMES).map(|(_, dic)| dic) + DICT.installed_path() } /// Is an Indonesian dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { - !dictionary().is_empty() + DICT.has_dictionary() } +/// Clitics PUEBI joins with a hyphen to a capitalized word, an abbreviation or a number +/// (`se-Indonesia`, `KTP-nya`, `Jakarta-lah`); they aren't dictionary words on their own. +const CLITICS: &[&str] = &["se", "nya", "ku", "mu", "lah", "kah", "pun"]; + /// Is `c` a letter of the Latin script (the only one the Indonesian dictionary covers)? fn is_latin_letter(c: char) -> bool { c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) diff --git a/crates/proof/src/it/mod.rs b/crates/proof/src/it/mod.rs index 027006a697..168883e174 100644 --- a/crates/proof/src/it/mod.rs +++ b/crates/proof/src/it/mod.rs @@ -18,67 +18,47 @@ mod suggest; #[cfg(test)] pub(crate) mod test_dict; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use crate::affix::AffixDict; +use crate::slot::DictSlot; pub use suggest::suggest; -/// The Italian dictionary: `None` until first use, then the installed one (or an empty one). -static DICT: RwLock>> = RwLock::new(None); - /// File names of an Italian Hunspell dictionary (Italy first, then Switzerland). const NAMES: &[&str] = &["it_IT", "it_CH", "it-IT", "it"]; +/// The Italian dictionary, found on first use (see [`DictSlot`]). +static DICT: DictSlot = DictSlot::new(NAMES, "Italian"); + /// The Italian dictionary: the installed Hunspell `it_IT` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Italian spelling is off /// then; see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed()); - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.get_or_insert(loaded).clone() -} - -fn load_installed() -> AffixDict { - let Some((aff, dic)) = crate::hunspell::find(NAMES) else { - crate::hunspell::clear_load_error(NAMES[0]); - log::info!("Italian spelling: no Hunspell it_IT dictionary installed"); - return AffixDict::default(); - }; - crate::hunspell::note_load(NAMES[0], &dic, crate::hunspell::load(&aff, &dic)).unwrap_or_else(|e| { - log::warn!("Italian spelling: {e}"); - AffixDict::default() - }) + DICT.get() } /// Use this Italian dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to /// UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { - let d = AffixDict::from_hunspell(aff, dic, &[])?; - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::new(d)); - Ok(()) + DICT.install(aff, dic) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = None; + DICT.reload(); } -/// The `.dic` file of the Hunspell `it_IT` dictionary found in the dictionaries folder or the -/// system locations now (File › Options › Proofing shows it, #1063); `None` when there is none. -/// A dictionary given with [`install_dictionary`] has no file and isn't reported. +/// The `.dic` file of the installed Italian dictionary (File › Options › Proofing shows it); +/// `None` when there is none, or when one given with [`install_dictionary`] is in use. pub fn installed_path() -> Option { - crate::hunspell::find(NAMES).map(|(_, dic)| dic) + DICT.installed_path() } /// Is an Italian dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { - !dictionary().is_empty() + DICT.has_dictionary() } /// Is `c` a letter of the Latin script (the only one the Italian dictionary covers)? diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 47216d72ac..7469c4d9b3 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -65,6 +65,7 @@ pub mod patterns; pub mod pl; pub mod pt; pub mod ru; +mod slot; pub mod tr; pub mod vi; @@ -1095,36 +1096,35 @@ mod tests { let before = dictionary_generation(); set_dictionaries_folder(Some(dir.clone())); assert!(dictionary_generation() > before, "cached checks are dropped"); - // Turkish follows the four below; Russian, Czech, Indonesian and the Spanishes follow it. + // Every language is listed, Russian and Czech too (#3226: they were once missing, so + // neither showed up in File › Options › Proofing). A language reports the folder's file, + // or none while a dictionary given with install_dictionary() is in use (#3252 review); + // the languages' own tests install theirs concurrently, so an exact path would race them. + // The folder is searched first whatever is in use. let found = installed_dictionaries(); - assert_eq!(found.get(4), Some(&(ProofLang::Tr, Some(dir.join("tr_TR.dic"))))); + let names = ["pl_PL", "pt_BR", "it_IT", "hr_HR", "tr_TR", "ru_RU", "cs_CZ", "id_ID", "es_ES", "es_MX", "es_419", "es_UY"]; assert_eq!( - found[7..], + found.iter().map(|(lang, _)| *lang).collect::>(), [ - (ProofLang::Id, Some(dir.join("id_ID.dic"))), - (ProofLang::Es, Some(dir.join("es_ES.dic"))), - (ProofLang::EsMx, Some(dir.join("es_MX.dic"))), - (ProofLang::Es419, Some(dir.join("es_419.dic"))), - (ProofLang::EsUy, Some(dir.join("es_UY.dic"))), + ProofLang::Pl, + ProofLang::Pt, + ProofLang::It, + ProofLang::Hr, + ProofLang::Tr, + ProofLang::Ru, + ProofLang::Cs, + ProofLang::Id, + ProofLang::Es, + ProofLang::EsMx, + ProofLang::Es419, + ProofLang::EsUy ] ); - assert_eq!( - found[..4], - [ - (ProofLang::Pl, Some(dir.join("pl_PL.dic"))), - (ProofLang::Pt, Some(dir.join("pt_BR.dic"))), - (ProofLang::It, Some(dir.join("it_IT.dic"))), - (ProofLang::Hr, Some(dir.join("hr_HR.dic"))), - ] - ); - // #3226: Russian and Czech were missing from installed_dictionaries() entirely (no - // installed_path() in either module), so neither ever showed up in File › Options › - // Proofing, installed or not. Their languages, not their exact path, is what belongs - // here: unlike the four above, installed_path() for Russian and Czech also reflects - // whether install_dictionary() gave one directly (#3252 review), which ru's and cs's own - // test modules do, concurrently, in tests of their own — asserting an exact path here - // would race them for no reason this test needs to care about. - assert_eq!(found[5..7].iter().map(|(lang, _)| *lang).collect::>(), [ProofLang::Ru, ProofLang::Cs], "{found:?}"); + for ((lang, path), name) in found.iter().zip(names) { + let file = dir.join(format!("{name}.dic")); + assert!(path.is_none() || path.as_ref() == Some(&file), "{lang:?}: {path:?}"); + assert_eq!(hunspell::find(&[name]).map(|(_, dic)| dic), Some(file), "{lang:?}"); + } assert!(is_correct_in("chrząszcz", ProofLang::Pl) && !is_correct_in("chrzonszcz", ProofLang::Pl)); assert!(is_correct_in("tenés", ProofLang::EsUy) && !is_correct_in("prueva", ProofLang::EsUy)); assert!(is_correct_in("дом", ProofLang::Ru) && !is_correct_in("дум", ProofLang::Ru)); diff --git a/crates/proof/src/pl/mod.rs b/crates/proof/src/pl/mod.rs index 18c968feb0..4d79986d98 100644 --- a/crates/proof/src/pl/mod.rs +++ b/crates/proof/src/pl/mod.rs @@ -17,67 +17,47 @@ mod suggest; #[cfg(test)] pub(crate) mod test_dict; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use crate::affix::AffixDict; +use crate::slot::DictSlot; pub use suggest::suggest; -/// The Polish dictionary: `None` until first use, then the installed one (or an empty one). -static DICT: RwLock>> = RwLock::new(None); - /// File names of a Polish Hunspell dictionary. const NAMES: &[&str] = &["pl_PL", "pl-PL", "pl"]; +/// The Polish dictionary, found on first use (see [`DictSlot`]). +static DICT: DictSlot = DictSlot::new(NAMES, "Polish"); + /// The Polish dictionary: the installed Hunspell `pl_PL` (read on first use), one given with -/// [`install_dictionary`], or an empty one when none is available (Polish spelling is off then; -/// see [`has_dictionary`]). +/// [`install_dictionary`], or an empty one when none is available (Polish spelling is off +/// then; see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed()); - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.get_or_insert(loaded).clone() + DICT.get() } -fn load_installed() -> AffixDict { - let Some((aff, dic)) = crate::hunspell::find(NAMES) else { - crate::hunspell::clear_load_error(NAMES[0]); - log::info!("Polish spelling: no Hunspell pl_PL dictionary installed"); - return AffixDict::default(); - }; - crate::hunspell::note_load(NAMES[0], &dic, crate::hunspell::load(&aff, &dic)).unwrap_or_else(|e| { - log::warn!("Polish spelling: {e}"); - AffixDict::default() - }) -} - -/// Use this Polish dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to UTF-8), -/// for example one the user picked. On error the current dictionary stays. +/// Use this Polish dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to +/// UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { - let d = AffixDict::from_hunspell(aff, dic, &[])?; - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::new(d)); - Ok(()) + DICT.install(aff, dic) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = None; + DICT.reload(); } -/// The `.dic` file of the installed Polish dictionary, searching the dictionaries folder and -/// the system locations now (File › Options › Proofing shows it, #1063); `None` when there is -/// none. A dictionary given with [`install_dictionary`] has no file and isn't reported. +/// The `.dic` file of the installed Polish dictionary (File › Options › Proofing shows it); +/// `None` when there is none, or when one given with [`install_dictionary`] is in use. pub fn installed_path() -> Option { - crate::hunspell::find(NAMES).map(|(_, dic)| dic) + DICT.installed_path() } /// Is a Polish dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { - !dictionary().is_empty() + DICT.has_dictionary() } /// Endings Polish adds after an apostrophe to foreign names whose last letter isn't pronounced diff --git a/crates/proof/src/pt/mod.rs b/crates/proof/src/pt/mod.rs index de41982590..860cf45796 100644 --- a/crates/proof/src/pt/mod.rs +++ b/crates/proof/src/pt/mod.rs @@ -16,67 +16,47 @@ mod suggest; #[cfg(test)] pub(crate) mod test_dict; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use crate::affix::AffixDict; +use crate::slot::DictSlot; pub use suggest::suggest; -/// The Portuguese dictionary: `None` until first use, then the installed one (or an empty one). -static DICT: RwLock>> = RwLock::new(None); - /// File names of a Portuguese Hunspell dictionary. const NAMES: &[&str] = &["pt_BR", "pt-BR", "pt"]; +/// The Portuguese dictionary, found on first use (see [`DictSlot`]). +static DICT: DictSlot = DictSlot::new(NAMES, "Portuguese"); + /// The Portuguese dictionary: the installed Hunspell `pt_BR` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Portuguese spelling is off /// then; see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed()); - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.get_or_insert(loaded).clone() -} - -fn load_installed() -> AffixDict { - let Some((aff, dic)) = crate::hunspell::find(NAMES) else { - crate::hunspell::clear_load_error(NAMES[0]); - log::info!("Portuguese spelling: no Hunspell pt_BR dictionary installed"); - return AffixDict::default(); - }; - crate::hunspell::note_load(NAMES[0], &dic, crate::hunspell::load(&aff, &dic)).unwrap_or_else(|e| { - log::warn!("Portuguese spelling: {e}"); - AffixDict::default() - }) + DICT.get() } /// Use this Portuguese dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to /// UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { - let d = AffixDict::from_hunspell(aff, dic, &[])?; - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::new(d)); - Ok(()) + DICT.install(aff, dic) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = None; + DICT.reload(); } -/// The `.dic` file of the installed Portuguese dictionary, searching the dictionaries folder and -/// the system locations now (File › Options › Proofing shows it, #1063); `None` when there is -/// none. A dictionary given with [`install_dictionary`] has no file and isn't reported. +/// The `.dic` file of the installed Portuguese dictionary (File › Options › Proofing shows it); +/// `None` when there is none, or when one given with [`install_dictionary`] is in use. pub fn installed_path() -> Option { - crate::hunspell::find(NAMES).map(|(_, dic)| dic) + DICT.installed_path() } /// Is a Portuguese dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { - !dictionary().is_empty() + DICT.has_dictionary() } /// Is `c` a letter of the Latin script (the only one the Portuguese dictionary covers)? diff --git a/crates/proof/src/ru/mod.rs b/crates/proof/src/ru/mod.rs index d5035e6a31..e46bf305f7 100644 --- a/crates/proof/src/ru/mod.rs +++ b/crates/proof/src/ru/mod.rs @@ -28,83 +28,47 @@ mod suggest; #[cfg(test)] pub(crate) mod test_dict; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use crate::affix::AffixDict; +use crate::slot::DictSlot; pub use suggest::suggest; -/// The Russian dictionary and whether it came from [`install_dictionary`] rather than a file on -/// disk (it then has no file of its own to report from [`installed_path`], even if a matching -/// file also happens to exist): one lock, so a reload and an install can't interleave their two -/// fields into an inconsistent combination. -static DICT: RwLock = RwLock::new(DictState { dict: None, in_memory: false }); - -struct DictState { - dict: Option>, - in_memory: bool, -} - /// File names of a Russian Hunspell dictionary. const NAMES: &[&str] = &["ru_RU", "ru-RU", "ru"]; +/// The Russian dictionary, found on first use (see [`DictSlot`]). +static DICT: DictSlot = DictSlot::new(NAMES, "Russian"); + /// The Russian dictionary: the installed Hunspell `ru_RU` (read on first use), one given with -/// [`install_dictionary`], or an empty one when none is available (Russian spelling is off then; -/// see [`has_dictionary`]). +/// [`install_dictionary`], or an empty one when none is available (Russian spelling is off +/// then; see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).dict.as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed()); - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.dict.get_or_insert(loaded).clone() + DICT.get() } -fn load_installed() -> AffixDict { - let Some((aff, dic)) = crate::hunspell::find(NAMES) else { - crate::hunspell::clear_load_error(NAMES[0]); - log::info!("Russian spelling: no Hunspell ru_RU dictionary installed"); - return AffixDict::default(); - }; - crate::hunspell::note_load(NAMES[0], &dic, crate::hunspell::load(&aff, &dic)).unwrap_or_else(|e| { - log::warn!("Russian spelling: {e}"); - AffixDict::default() - }) -} - -/// Use this Russian dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to UTF-8), -/// for example one the user picked. On error the current dictionary stays. +/// Use this Russian dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to +/// UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { - let d = AffixDict::from_hunspell(aff, dic, &[])?; - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - slot.dict = Some(Arc::new(d)); - slot.in_memory = true; - Ok(()) + DICT.install(aff, dic) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - slot.dict = None; - slot.in_memory = false; + DICT.reload(); } -/// The `.dic` file of the installed Russian dictionary, searching the dictionaries folder and -/// the system locations now (File › Options › Proofing shows it, #3226); `None` when there is -/// none. A dictionary given with [`install_dictionary`] has no file and isn't reported, even if a -/// matching file also happens to exist. +/// The `.dic` file of the installed Russian dictionary (File › Options › Proofing shows it); +/// `None` when there is none, or when one given with [`install_dictionary`] is in use. pub fn installed_path() -> Option { - if DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).in_memory { - return None; - } - crate::hunspell::find(NAMES).map(|(_, dic)| dic) + DICT.installed_path() } /// Is a Russian dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { - !dictionary().is_empty() + DICT.has_dictionary() } /// Is `c` a letter of the Cyrillic script (the only one the Russian dictionary covers)? diff --git a/crates/proof/src/slot.rs b/crates/proof/src/slot.rs new file mode 100644 index 0000000000..3c0f2bd35b --- /dev/null +++ b/crates/proof/src/slot.rs @@ -0,0 +1,150 @@ +//! The installed Hunspell dictionary of a proofing language, in one shape for every language: +//! found on first use, replaced with [`DictSlot::install`] and forgotten with [`DictSlot::reload`]. + +use std::path::{Path, PathBuf}; +use std::sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard}; + +use crate::affix::AffixDict; + +/// A dictionary a language reads: the affix engine's ([`AffixDict`]) or Turkish's own. +pub(crate) trait Dictionary: Default + Send + Sync { + /// The installed `.aff` and `.dic` files, read. + fn load(aff: &Path, dic: &Path) -> Result; + /// Hunspell `.aff` and `.dic` text (decoded to UTF-8), parsed. + fn parse(aff: &str, dic: &str) -> Result; + /// No words: the language has no spelling marks. + fn is_empty(&self) -> bool; +} + +impl Dictionary for AffixDict { + fn load(aff: &Path, dic: &Path) -> Result { + crate::hunspell::load(aff, dic) + } + + fn parse(aff: &str, dic: &str) -> Result { + AffixDict::from_hunspell(aff, dic, &[]) + } + + fn is_empty(&self) -> bool { + AffixDict::is_empty(self) + } +} + +/// The dictionary in use and whether it came from [`DictSlot::install`] rather than a file on +/// disk (it then has no file to report, even if a matching file also happens to exist): one lock, +/// so a reload and an install can't interleave their two fields. +struct State { + dict: Option>, + in_memory: bool, +} + +/// One language's dictionary. +pub(crate) struct DictSlot { + /// File names the dictionary may have, the usual one first (also the key of its load error). + names: &'static [&'static str], + /// The language's English name, for the log. + english: &'static str, + state: RwLock>, +} + +impl DictSlot { + pub(crate) const fn new(names: &'static [&'static str], english: &'static str) -> Self { + DictSlot { names, english, state: RwLock::new(State { dict: None, in_memory: false }) } + } + + fn read(&self) -> RwLockReadGuard<'_, State> { + self.state.read().unwrap_or_else(PoisonError::into_inner) + } + + fn write(&self) -> RwLockWriteGuard<'_, State> { + self.state.write().unwrap_or_else(PoisonError::into_inner) + } + + /// The Hunspell name the dictionary is known by (`pl_PL`): the first of its file names. + pub(crate) fn name(&self) -> &'static str { + self.names.first().copied().unwrap_or_default() + } + + /// The file names the dictionary may have, the usual one first. + #[cfg(test)] + pub(crate) fn names(&self) -> &'static [&'static str] { + self.names + } + + /// The dictionary: the installed one (read on first use), one given with [`Self::install`], or + /// an empty one when none is available (the language's spelling is off then). + pub(crate) fn get(&self) -> Arc { + if let Some(d) = self.read().dict.as_ref() { + return d.clone(); + } + let loaded = Arc::new(self.load_installed()); + // Another thread (or an install) may have filled it meanwhile: that one wins. + self.write().dict.get_or_insert(loaded).clone() + } + + fn load_installed(&self) -> D { + let name = self.name(); + let Some((aff, dic)) = crate::hunspell::find(self.names) else { + crate::hunspell::clear_load_error(name); + log::info!("{} spelling: no Hunspell {name} dictionary installed", self.english); + return D::default(); + }; + crate::hunspell::note_load(name, &dic, D::load(&aff, &dic)).unwrap_or_else(|e| { + log::warn!("{} spelling: {e}", self.english); + D::default() + }) + } + + /// Use this dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to UTF-8), for + /// example one the user picked. On error the current dictionary stays. + pub(crate) fn install(&self, aff: &str, dic: &str) -> Result<(), String> { + let d = D::parse(aff, dic)?; + let mut slot = self.write(); + slot.dict = Some(Arc::new(d)); + slot.in_memory = true; + Ok(()) + } + + /// Forget the dictionary, so the next check looks for an installed one again (after + /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). + pub(crate) fn reload(&self) { + let mut slot = self.write(); + slot.dict = None; + slot.in_memory = false; + } + + /// The `.dic` file of the installed dictionary, searching the dictionaries folder and the + /// system locations now (File › Options › Proofing shows it); `None` when there is none, or + /// when the dictionary in use was given with [`Self::install`] and so has no file. + pub(crate) fn installed_path(&self) -> Option { + if self.read().in_memory { + return None; + } + crate::hunspell::find(self.names).map(|(_, dic)| dic) + } + + /// Is a dictionary available (spelling is checked)? + pub(crate) fn has_dictionary(&self) -> bool { + !self.get().is_empty() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + static SLOT: DictSlot = DictSlot::new(&["zz_SLOT_TEST"], "Test"); + + #[test] + fn an_installed_dictionary_has_no_file_until_reloaded() { + assert!(!SLOT.has_dictionary(), "nothing installed under this name"); + assert!(SLOT.install("FLAG long\n", "1\nkot\n").is_err(), "unsupported files are refused"); + assert!(!SLOT.has_dictionary(), "a refused install keeps the current one"); + SLOT.install("", "1\nkot\n").unwrap(); + assert!(SLOT.has_dictionary() && SLOT.get().check("kot")); + assert_eq!(SLOT.installed_path(), None); + SLOT.reload(); + assert!(!SLOT.has_dictionary(), "reloaded from disk, where there is none"); + assert_eq!(SLOT.name(), "zz_SLOT_TEST"); + } +} diff --git a/crates/proof/src/tr/dict.rs b/crates/proof/src/tr/dict.rs index ed57cbca7b..b47ba7c1eb 100644 --- a/crates/proof/src/tr/dict.rs +++ b/crates/proof/src/tr/dict.rs @@ -26,6 +26,21 @@ pub struct TrDict { max_suffix_len: usize, } +impl crate::slot::Dictionary for TrDict { + fn load(aff: &std::path::Path, dic: &std::path::Path) -> Result { + let read = |p: &std::path::Path| std::fs::read_to_string(p).map_err(|e| e.to_string()); + TrDict::from_hunspell(&read(aff)?, &read(dic)?) + } + + fn parse(aff: &str, dic: &str) -> Result { + TrDict::from_hunspell(aff, dic) + } + + fn is_empty(&self) -> bool { + TrDict::is_empty(self) + } +} + impl TrDict { /// Returns true if empty (no dictionary loaded). pub fn is_empty(&self) -> bool { diff --git a/crates/proof/src/tr/mod.rs b/crates/proof/src/tr/mod.rs index ab3fe92d72..eb5f0ea528 100644 --- a/crates/proof/src/tr/mod.rs +++ b/crates/proof/src/tr/mod.rs @@ -18,70 +18,45 @@ pub mod suggest; #[cfg(test)] pub(crate) mod test_dict; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; + +use crate::slot::DictSlot; pub use dict::TrDict; pub use suggest::suggest; -/// The Turkish dictionary: `None` until first use, then the installed one (or an empty one). -static DICT: RwLock>> = RwLock::new(None); - /// File names of a Turkish Hunspell dictionary. const NAMES: &[&str] = &["tr_TR", "tr-TR", "tr"]; +/// The Turkish dictionary, found on first use (see [`DictSlot`]). +static DICT: DictSlot = DictSlot::new(NAMES, "Turkish"); + /// The Turkish dictionary: the installed Hunspell `tr_TR` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available. pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(std::sync::PoisonError::into_inner).as_ref() { - return d.clone(); - } - let loaded = Arc::new(load_installed()); - let mut slot = DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner); - slot.get_or_insert(loaded).clone() + DICT.get() } -fn load_installed() -> TrDict { - let Some((aff, dic)) = crate::hunspell::find(NAMES) else { - crate::hunspell::clear_load_error(NAMES[0]); - log::info!("Turkish spelling: no Hunspell tr_TR dictionary installed"); - return TrDict::default(); - }; - let read = |p: &std::path::Path| std::fs::read_to_string(p); - match (read(&aff), read(&dic)) { - (Ok(aff_text), Ok(dic_text)) => crate::hunspell::note_load(NAMES[0], &dic, TrDict::from_hunspell(&aff_text, &dic_text)).unwrap_or_else(|e| { - log::warn!("Turkish spelling: {e}"); - TrDict::default() - }), - (a, d) => { - let reason = a.err().or(d.err()).map_or_else(String::new, |e| e.to_string()); - let _ = crate::hunspell::note_load::<()>(NAMES[0], &dic, Err(reason)); - TrDict::default() - } - } -} - -/// Use this Turkish dictionary from now on. +/// Use this Turkish dictionary from now on (Hunspell `.aff` and `.dic` text). On error the +/// current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { - let d = TrDict::from_hunspell(aff, dic)?; - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(Arc::new(d)); - Ok(()) + DICT.install(aff, dic) } -/// Reload the dictionary on next check. +/// Forget the loaded dictionary, so the next check looks for an installed one again. pub fn reload_dictionary() { - *DICT.write().unwrap_or_else(std::sync::PoisonError::into_inner) = None; + DICT.reload(); } -/// The `.dic` file of the Hunspell `tr_TR` dictionary found in the dictionaries folder or the -/// system locations now (File › Options › Proofing shows it, #1063); `None` when there is none. -/// A dictionary given with [`install_dictionary`] has no file and isn't reported. +/// The `.dic` file of the installed Turkish dictionary (File › Options › Proofing shows it); +/// `None` when there is none, or when one given with [`install_dictionary`] is in use. pub fn installed_path() -> Option { - crate::hunspell::find(NAMES).map(|(_, dic)| dic) + DICT.installed_path() } /// Is a Turkish dictionary available? pub fn has_dictionary() -> bool { - !dictionary().is_empty() + DICT.has_dictionary() } /// Is `word` spelled correctly in Turkish? From 577d5d61f876e4c7bc9949b1b0a96e4654874cdc Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 19:48:47 -0100 Subject: [PATCH 004/308] Proof tests: the user-dictionary file test waits for the dictionary tests user_dictionary_persists_through_a_file saves the whole user dictionary to a file and loads it back. Run beside user_dictionary_and_ignore_all_match_both_normalization_forms, it could catch a word that test had added and bring it back after the test removed it, so the other test's last check failed now and then (once in about two dozen full runs here). It now holds DICTIONARY_TESTS, as the other test does. --- crates/proof/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 7469c4d9b3..ad516b2950 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -1059,7 +1059,9 @@ mod tests { use super::*; - /// Held by tests that swap or rely on the shared Spanish dictionary, so a folder reset can't land mid-test. + /// Held by tests that swap or rely on the shared Spanish dictionary, so a folder reset can't land + /// mid-test, and by those that save the whole user dictionary and load it back, which would + /// bring back a word another test just removed. static DICTIONARY_TESTS: std::sync::Mutex<()> = std::sync::Mutex::new(()); /// #1063: the dictionaries folder is searched before the system locations; the dictionaries @@ -1579,6 +1581,7 @@ mod tests { /// #40: the user dictionary survives a save and a fresh load. #[test] fn user_dictionary_persists_through_a_file() { + let _guard = DICTIONARY_TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner); let dir = std::env::temp_dir().join(format!("wc-dict-{}-{}", std::process::id(), "persist")); let _ = std::fs::remove_dir_all(&dir); // The folder is created on the first save. From e7eda2aff0462145bbb325a3d5c1f674ab1715fb Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 19:49:22 -0100 Subject: [PATCH 005/308] Proofing: one suggestion engine with per-language tuning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Czech's dictionary-driven ranking (case, MAP, REP, missing spaces, KEY, TRY edits, a stem scan) had five older copies. Polish, Portuguese, Croatian, Italian and Russian each kept one: 300 to 400 lines of the same rounds with a few lines of language knowledge mixed in. Fixes reached some copies only. NOSUGGEST (#1048) and the first-difference tie-break (#797) never got to all of them, and none had the KEY round or the bound on TRY letters. affix_suggest.rs is that ranking once. A language's `Tuning` holds what is its own: the short words that run into their neighbours (Polish, Portuguese, Croatian, Italian), Russian's split rule, the letter groups and confusions its dictionaries lack (Croatian, Russian), letters to try for a dictionary without a TRY line, and cost hooks for Polish final devoicing and Russian е/ё. Czech, Indonesian and Spanish use the dictionary's tables alone. Turkish keeps its own suggester, which reads another dictionary type with rounds of its own. The language tables add to the dictionary's instead of replacing them, and TRY letters are tried in the dictionary's order, commonest first, as the copies did (the Czech engine sorted them). Old and new compared on the test dictionaries, with every one-letter typo of each word plus the tests' misspellings, top eight suggestions: Czech, Croatian, Italian and Portuguese give the same for all 7,903 inputs. Polish differs for 44 of 3,244 and Russian for 33 of 2,346, all orderings among candidates of equal cost, from the tie-break they now share (`ie` → `nie` before `i`). New tests pin the moved knowledge: the tuning mechanism, Croatian's groups without MAP lines, and Russian's costs and split rule. Turning each knob off fails one. Proof: 283 tests. --- crates/proof/src/affix_suggest.rs | 493 ++++++++++++++++++++++++++++++ crates/proof/src/cs/suggest.rs | 358 +--------------------- crates/proof/src/es/suggest.rs | 8 +- crates/proof/src/hr/suggest.rs | 351 ++------------------- crates/proof/src/id/suggest.rs | 9 +- crates/proof/src/it/suggest.rs | 350 +-------------------- crates/proof/src/lib.rs | 1 + crates/proof/src/pl/suggest.rs | 361 ++-------------------- crates/proof/src/pt/suggest.rs | 356 +-------------------- crates/proof/src/ru/suggest.rs | 391 ++++-------------------- 10 files changed, 625 insertions(+), 2053 deletions(-) create mode 100644 crates/proof/src/affix_suggest.rs diff --git a/crates/proof/src/affix_suggest.rs b/crates/proof/src/affix_suggest.rs new file mode 100644 index 0000000000..1da3555956 --- /dev/null +++ b/crates/proof/src/affix_suggest.rs @@ -0,0 +1,493 @@ +//! Spelling suggestions from an affix dictionary, one engine for every language that reads one +//! (Czech, Croatian, Indonesian, Italian, Polish, Portuguese, Russian, Spanish). Candidates come +//! in rounds of rising cost, each checked against the dictionary: +//! 1. letter case (`warszawa` → `Warszawa`); +//! 2. missing or wrong diacritics and other MAP slips, a few letters changed (`zolw` → `żółw`): +//! adding a missing accent is the commonest slip and ranks first, while taking away one the +//! writer typed ranks after the known confusions; +//! 3. the REP table of common confusions (`żeka` → `rzeka`); +//! 4. a missing space (`napewno` → `na pewno`); +//! 5. keyboard neighbours from the KEY rows, then one typo: a letter left out, added, swapped with +//! the next or mistyped (TRY letters); +//! 6. when that finds fewer than asked for: the stems that start like the word (MAP relatives +//! folded) and share the most letter pairs with it, whose forms are ranked by an edit distance in +//! which a MAP slip costs half (two typos at most). +//! +//! The tables come from the dictionary; a language's [`Tuning`] adds the ones its dictionaries may +//! lack and the slips no table can say. The input is bounded ([`MAX_LETTERS`]), and so is every +//! round, so a call costs a few thousand dictionary lookups at most. + +use std::borrow::Cow; +use std::collections::{HashMap, HashSet}; + +use crate::affix::{AffixDict, Casing, capitalize, casing}; + +/// Longest word suggestions are looked for, in letters. +pub(crate) const MAX_LETTERS: usize = 40; +const MAX_MAP_VARIANTS: usize = 4096; +/// Stems the scan of round 6 expands. +const SCAN_STEMS: usize = 48; + +const COST_CASE: u32 = 0; +pub(crate) const COST_ADD_DIACRITIC: u32 = 4; +pub(crate) const COST_OTHER_MAP: u32 = 7; +const COST_DROP_DIACRITIC: u32 = 11; +const COST_REP: u32 = 10; +pub(crate) const COST_SPLIT: u32 = 12; +/// A missing space leaving a half shorter than three letters ([`Split::Anywhere`]). +const COST_SPLIT_SHORT: u32 = 17; +/// A missing space between two longer halves with no short word beside it ([`Split::Near`]). +pub(crate) const COST_SPLIT_LONG: u32 = 18; +const COST_KEY: u32 = 13; +const COST_SWAP: u32 = 14; +const COST_DROP_OR_ADD: u32 = 15; +pub(crate) const COST_MISTYPE: u32 = 16; +const COST_SCAN: u32 = 20; + +/// What a language adds to its dictionary's tables. +pub(crate) struct Tuning { + /// Where a missing space may go (round 4). + pub split: Split, + /// TRY letters for a dictionary without a TRY line. + pub alphabet: &'static str, + /// MAP groups the language adds to its dictionary's (each string is one group: `"cčć"`). + pub map: &'static [&'static str], + /// REP confusions the language adds to its dictionary's. + pub rep: &'static [(&'static str, &'static str)], + /// The cost of a REP confusion. + pub rep_cost: u32, + /// Most letters a MAP variant changes. + pub max_map_changes: usize, + /// The cost of changing letter `from` to its MAP relative `to`. + pub map_cost: fn(char, char) -> u32, + /// The cost of typing letter `c` at `i` of the word (`chars`, lower case) instead. + pub mistype_cost: fn(&[char], usize, char) -> u32, +} + +/// Where a missing space may go. +pub(crate) enum Split { + /// Anywhere, dearer when a half is shorter than three letters. + Anywhere, + /// After a short word that often runs into the next one (`na pewno`, `w ogóle`) or before one + /// that runs into the word before (`kocham cię`), dearer between two longer halves, and never + /// leaving a stray short half (`że ka`). + Near { leading: &'static [&'static str], trailing: &'static [&'static str] }, + /// The language's own rule: the cost of the halves `a b`, or `None` to skip them. + Custom(fn(&str, &str) -> Option), +} + +impl Tuning { + /// The dictionary's tables alone (Czech, Indonesian, Spanish). + pub(crate) const DEFAULT: Tuning = + Tuning { split: Split::Anywhere, alphabet: "", map: &[], rep: &[], rep_cost: COST_REP, max_map_changes: 4, map_cost, mistype_cost }; +} + +/// The cost of changing letter `from` to its MAP relative `to`. Adding a missing accent is the +/// commonest slip (typing without diacritics); an accent the writer did type is likelier right, +/// so taking one away costs more than a known confusion from the REP table. +pub(crate) fn map_cost(from: char, to: char) -> u32 { + match (from.is_ascii(), to.is_ascii()) { + (true, false) => COST_ADD_DIACRITIC, + (false, true) => COST_DROP_DIACRITIC, + _ => COST_OTHER_MAP, + } +} + +/// Any mistyped letter costs the same. +fn mistype_cost(_: &[char], _: usize, _: char) -> u32 { + COST_MISTYPE +} + +struct Collector<'a> { + d: &'a AffixDict, + /// The word, lower case. + input: String, + tested: HashSet, + /// Suggestion → (cost, order found). + found: HashMap, + seq: usize, +} + +impl Collector<'_> { + /// Check a lower-case candidate (a word, or two separated by a space) and keep it if every + /// word is in the dictionary, as written or capitalized (names). + fn offer(&mut self, cand: &str, cost: u32) { + if cand.is_empty() || cand == self.input || !self.tested.insert(cand.to_string()) { + return; + } + let mut words = Vec::new(); + let mut cost = cost; + for part in cand.split(' ') { + if self.d.contains_suggestible(part) { + words.push(part.to_string()); + } else { + let c = capitalize(part); + if !self.d.contains_suggestible(&c) { + return; + } + words.push(c); + // A name ranks after a common word at the same distance (`moze` → `może`, then + // `Mozę`). + cost += 1; + } + } + self.keep(words.join(" "), cost); + } + + /// Keep a suggestion known to be correct, unless the dictionary says never to offer it. + fn keep(&mut self, text: String, cost: u32) { + if !text.split(' ').all(|part| self.d.can_suggest(part)) { + return; + } + self.seq += 1; + let seq = self.seq; + let e = self.found.entry(text).or_insert((cost, seq)); + if cost < e.0 { + *e = (cost, seq); + } + } + + fn count_within(&self, cost: u32) -> usize { + self.found.values().filter(|(c, _)| *c <= cost).count() + } +} + +/// The MAP groups: the dictionary's, then the language's. +fn groups<'a>(d: &'a AffixDict, t: &Tuning) -> Cow<'a, [Vec]> { + if t.map.is_empty() { + return Cow::Borrowed(d.map()); + } + Cow::Owned(d.map().iter().cloned().chain(t.map.iter().map(|g| g.chars().collect())).collect()) +} + +/// The other members of every MAP group of `c` (its likely confusions), each once. +fn alternatives(c: char, map: &[Vec]) -> Vec { + let mut out: Vec = Vec::new(); + for x in map.iter().filter(|g| g.contains(&c)).flat_map(|g| g.iter().copied()) { + if x != c && !out.contains(&x) { + out.push(x); + } + } + out +} + +/// Every way of changing up to `t.max_map_changes` letters of `chars` to a MAP relative (`alts`, +/// letter by letter), with its cost, each written after `buf`. +fn map_variants(chars: &[char], alts: &[Vec], t: &Tuning, changes: usize, cost: u32, buf: &mut Vec, out: &mut Vec<(String, u32)>) { + if out.len() >= MAX_MAP_VARIANTS { + return; + } + let Some((&c, rest)) = chars.split_first() else { + if changes > 0 { + out.push((buf.iter().collect(), cost)); + } + return; + }; + let (here, later) = alts.split_first().map_or((&[][..], &[][..]), |(h, l)| (h.as_slice(), l)); + buf.push(c); + map_variants(rest, later, t, changes, cost, buf, out); + buf.pop(); + if changes < t.max_map_changes { + for &a in here { + buf.push(a); + map_variants(rest, later, t, changes + 1, cost + (t.map_cost)(c, a), buf, out); + buf.pop(); + } + } +} + +/// A letter's MAP representative (the first of its first group), lower case: `ż` → `z`, `ó` → `o`. +pub(crate) fn fold(c: char, map: &[Vec]) -> char { + let l = c.to_lowercase().next().unwrap_or(c); + map.iter().find(|g| g.contains(&l)).and_then(|g| g.first().copied()).unwrap_or(l) +} + +/// Optimal string alignment distance where a MAP slip (`a`/`ą`, `u`/`ó`) costs 1 and any other +/// edit 2; `limit + 1` once it is certainly above `limit`. +pub(crate) fn weighted_distance(a: &[char], b: &[char], related: &dyn Fn(char, char) -> bool, limit: u32) -> u32 { + let m = b.len(); + let over = limit + 1; + let mut prev2 = vec![0u32; m + 1]; + let mut prev: Vec = (0..=m as u32).map(|j| j * 2).collect(); + let mut cur = vec![0u32; m + 1]; + for (i, &ai) in a.iter().enumerate() { + if let Some(c) = cur.get_mut(0) { + *c = (i as u32 + 1) * 2; + } + let mut row_min = u32::MAX; + for (j, &bj) in b.iter().enumerate() { + let sub = if ai == bj { + 0 + } else if related(ai, bj) { + 1 + } else { + 2 + }; + let at = |v: &[u32], k: usize| v.get(k).copied().unwrap_or(u32::MAX / 4); + let mut v = (at(&prev, j + 1) + 2).min(at(&cur, j) + 2).min(at(&prev, j) + sub); + if i > 0 && j > 0 && a.get(i - 1) == Some(&bj) && b.get(j - 1) == Some(&ai) { + v = v.min(at(&prev2, j - 1) + 2); + } + if let Some(c) = cur.get_mut(j + 1) { + *c = v; + } + row_min = row_min.min(v); + } + if m > 0 && row_min > limit { + return over; + } + std::mem::swap(&mut prev2, &mut prev); + std::mem::swap(&mut prev, &mut cur); + } + prev.get(m).copied().unwrap_or(over).min(over) +} + +/// The cost of a missing space between `a` and `b`, or `None` when the split isn't offered. +fn split_cost(split: &Split, a: &str, b: &str) -> Option { + let long = |part: &str| part.chars().count() >= 3; + match split { + Split::Anywhere => Some(if long(a) && long(b) { COST_SPLIT } else { COST_SPLIT_SHORT }), + Split::Near { leading, trailing } => { + if (leading.contains(&a) && long(b)) || (long(a) && trailing.contains(&b)) { + Some(COST_SPLIT) + } else if long(a) && long(b) { + Some(COST_SPLIT_LONG) + } else { + None + } + } + Split::Custom(cost) => cost(a, b), + } +} + +/// Spelling suggestions for `word` from `d`, best first, at most `max`, in the word's letter case. +pub(crate) fn suggest_in(d: &AffixDict, word: &str, max: usize, t: &Tuning) -> Vec { + let word = word.trim(); + let n = word.chars().count(); + if n == 0 || n > MAX_LETTERS || max == 0 || d.is_empty() || word.contains(char::is_whitespace) { + return Vec::new(); + } + let case = casing(word); + let input = word.to_lowercase(); + let chars: Vec = input.chars().collect(); + let mut col = Collector { d, input: input.clone(), tested: HashSet::new(), found: HashMap::new(), seq: 0 }; + // 1. Letter case. + if d.contains(&input) && word != input { + col.keep(input.clone(), COST_CASE); + } else if d.contains(&capitalize(&input)) && word != capitalize(&input) { + col.keep(capitalize(&input), COST_CASE); + } + // 2. Accents and other MAP slips. + let map = groups(d, t); + let alts: Vec> = chars.iter().map(|&c| alternatives(c, &map)).collect(); + let mut variants = Vec::new(); + map_variants(&chars, &alts, t, 0, 0, &mut Vec::with_capacity(n), &mut variants); + variants.sort_by_key(|v| v.1); + for (v, cost) in &variants { + col.offer(v, *cost); + } + // 3. Common confusions. + let rep = d.rep().iter().map(|(from, to)| (from.as_str(), to.as_str())).chain(t.rep.iter().copied()); + for (from, to) in rep { + if from.is_empty() { + continue; + } + for (i, _) in input.match_indices(from) { + if let (Some(a), Some(b)) = (input.get(..i), input.get(i + from.len()..)) { + col.offer(&format!("{a}{to}{b}"), t.rep_cost); + } + } + } + // 4. A missing space; both halves must be dictionary words. + for (i, _) in input.char_indices().skip(1) { + let (Some(a), Some(b)) = (input.get(..i), input.get(i..)) else { continue }; + if let Some(cost) = split_cost(&t.split, a, b) { + col.offer(&format!("{a} {b}"), cost); + } + } + // 5. Keyboard neighbours from the dictionary's KEY rows. + for row in d.key().split('|').take(32) { + let keys: Vec = row.chars().take(128).collect(); + for pair in keys.windows(2) { + let [a, b] = pair else { continue }; + for (i, &c) in chars.iter().enumerate() { + let replacement = if c == *a { + Some(*b) + } else if c == *b { + Some(*a) + } else { + None + }; + if let Some(replacement) = replacement { + let mut v = chars.clone(); + if let Some(x) = v.get_mut(i) { + *x = replacement; + } + col.offer(&v.iter().collect::(), COST_KEY); + } + } + } + } + // One typo, with the TRY letters (the language's when the dictionary has none), bounded and + // each once, in their order: a TRY line lists the commonest letters first, so equal-cost + // candidates come out in that order. + let try_chars = if d.try_chars().is_empty() { t.alphabet } else { d.try_chars() }; + let mut letters: Vec = Vec::new(); + for c in try_chars.chars().filter(|c| c.is_lowercase()).take(128) { + if !letters.contains(&c) { + letters.push(c); + } + } + let s = |v: &[char]| v.iter().collect::(); + for i in 0..n.saturating_sub(1) { + if chars.get(i) != chars.get(i + 1) { + let mut v = chars.clone(); + v.swap(i, i + 1); + col.offer(&s(&v), COST_SWAP); + } + } + for i in 0..n { + let mut v = chars.clone(); + v.remove(i); + col.offer(&s(&v), COST_DROP_OR_ADD); + } + for i in 0..=n { + for &c in &letters { + let mut v = chars.clone(); + v.insert(i, c); + col.offer(&s(&v), COST_DROP_OR_ADD); + } + } + for i in 0..n { + for &c in &letters { + if chars.get(i) != Some(&c) { + let mut v = chars.clone(); + if let Some(x) = v.get_mut(i) { + *x = c; + } + col.offer(&s(&v), (t.mistype_cost)(&chars, i, c)); + } + } + } + // 6. Further off: scan the stems that start like the word. + if col.count_within(COST_MISTYPE) < max { + scan(&mut col, &chars, &map); + } + let mut found: Vec<(String, (u32, usize))> = col.found.into_iter().collect(); + // Among equal costs, the change nearest the word's start is the likelier intent + // (the first changed letter breaks ties). + let diff_at = |text: &str| chars.iter().zip(text.chars()).position(|(a, b)| *a != b).unwrap_or(n); + found.sort_by_key(|(text, (cost, seq))| (*cost, diff_at(text), *seq)); + let mut out: Vec = Vec::new(); + for (text, _) in found { + let t = match case { + Casing::Initial => capitalize(&text), + Casing::Upper if n > 1 => text.to_uppercase(), + _ => text, + }; + if t != word && !out.contains(&t) { + out.push(t); + } + if out.len() >= max { + break; + } + } + out +} + +/// Round 6: rank the stems starting with the word's first letter (or a MAP relative of it, either +/// case) by shared letter pairs, then keep the forms of the best within two typos. +fn scan(col: &mut Collector, chars: &[char], map: &[Vec]) { + let d = col.d; + let Some(&first) = chars.first() else { return }; + let folded: Vec = chars.iter().map(|&c| fold(c, map)).collect(); + let pairs: HashSet<(char, char)> = std::iter::once(' ').chain(folded.iter().copied()).zip(folded.iter().copied()).collect(); + let mut firsts: Vec = std::iter::once(first).chain(alternatives(first, map)).collect(); + let upper: Vec = firsts.iter().flat_map(|c| c.to_uppercase()).collect(); + firsts.extend(upper); + firsts.sort_unstable(); + firsts.dedup(); + let n = folded.len(); + let mut best: Vec<(u32, &str)> = Vec::new(); + for f in firsts { + let prefix = f.to_string(); + for stem in d.stems_with_prefix(&prefix) { + let sf: Vec = stem.chars().map(|c| fold(c, map)).collect(); + let shared = std::iter::once(' ').chain(sf.iter().copied()).zip(sf.iter().copied()).filter(|p| pairs.contains(p)).count(); + if shared < 2 { + continue; + } + // Shared pairs, less the stem's letters the word doesn't explain, less a length gap + // beyond what an ending explains. + let extra = sf.len().saturating_sub(shared); + let gap = sf.len().abs_diff(n).saturating_sub(4); + let score = (shared * 4).saturating_sub(extra * 3 + gap * 2) as u32; + if score == 0 { + continue; + } + if best.len() < SCAN_STEMS { + best.push((score, stem)); + } else if let Some(min) = best.iter_mut().min_by_key(|b| b.0) + && score > min.0 + { + *min = (score, stem); + } + } + } + best.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(b.1))); + let related = |a: char, b: char| fold(a, map) == fold(b, map); + let limit = 4; + for (_, stem) in best { + for form in d.forms(stem) { + let lf: Vec = form.to_lowercase().chars().collect(); + if lf.len().abs_diff(chars.len()) > 2 { + continue; + } + let dist = weighted_distance(&lf, chars, &related, limit); + if dist <= limit && lf.as_slice() != chars { + col.keep(form, COST_SCAN + dist * 3); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn an_extra_trailing_letter_is_dropped_before_a_short_word_split() { + let d = AffixDict::from_hunspell("", "2\npočítač\nz\n", &[]).unwrap(); + let suggestions = suggest_in(&d, "počítačz", 8, &Tuning::DEFAULT); + assert_eq!(suggestions.first().map(String::as_str), Some("počítač"), "{suggestions:?}"); + assert!(suggestions.contains(&"počítač z".into()), "{suggestions:?}"); + } + + #[test] + fn distance_weighs_map_slips_half() { + let map = vec![vec!['a', 'ą'], vec!['o', 'ó', 'u']]; + let related = |a: char, b: char| fold(a, &map) == fold(b, &map); + let c = |s: &str| s.chars().collect::>(); + assert_eq!(weighted_distance(&c("ktury"), &c("który"), &related, 9), 1); + assert_eq!(weighted_distance(&c("kot"), &c("kto"), &related, 9), 2); + assert_eq!(weighted_distance(&c("kot"), &c("kotek"), &related, 9), 4); + assert_eq!(weighted_distance(&c("kot"), &c("pies"), &related, 2), 3); + assert_eq!(weighted_distance(&c(""), &c("ab"), &related, 9), 4); + } + + /// A language's tables come after its dictionary's, a TRY alphabet stands in for a missing TRY + /// line, and a split rule decides which missing spaces are offered. + #[test] + fn tuning_adds_to_the_dictionary_tables() { + let d = AffixDict::from_hunspell("", "3\nčaj\nkot\nna\n", &[]).unwrap(); + let tuned = Tuning { map: &["cč"], alphabet: "t", rep: &[("xy", "kot")], ..Tuning::DEFAULT }; + assert!(!suggest_in(&d, "caj", 4, &Tuning::DEFAULT).contains(&"čaj".into())); + assert!(suggest_in(&d, "caj", 4, &tuned).contains(&"čaj".into()), "the language's MAP group"); + assert!(suggest_in(&d, "xy", 4, &tuned).contains(&"kot".into()), "the language's REP pair"); + assert!(suggest_in(&d, "ko", 4, &tuned).contains(&"kot".into()), "the language's TRY letters"); + let near = Tuning { split: Split::Near { leading: &["na"], trailing: &[] }, ..Tuning::DEFAULT }; + assert!(suggest_in(&d, "nakot", 4, &near).contains(&"na kot".into())); + assert!(!suggest_in(&d, "kotna", 4, &near).contains(&"kot na".into()), "no stray short half"); + assert!(suggest_in(&d, "kotna", 4, &Tuning::DEFAULT).contains(&"kot na".into())); + } +} diff --git a/crates/proof/src/cs/suggest.rs b/crates/proof/src/cs/suggest.rs index 4e5ecd17ca..05eba74a83 100644 --- a/crates/proof/src/cs/suggest.rs +++ b/crates/proof/src/cs/suggest.rs @@ -1,168 +1,8 @@ -//! Czech suggestions: bounded MAP variants, REP replacements, KEY neighbours, TRY edits -//! and a stem scan. All linguistic tables are read from the installed dictionary, so Indonesian -//! ([`crate::id`]) uses the same ranking with its own dictionary. +//! Czech suggestions: the shared ranking ([`crate::affix_suggest`]) with the installed +//! dictionary's own MAP/REP/TRY/KEY tables and nothing Czech hard-coded. -use std::collections::{HashMap, HashSet}; - -use crate::affix::{AffixDict, Casing, capitalize, casing}; - -/// Longest word suggestions are looked for, in letters (Spanish shares the bound). -pub(crate) const MAX_LETTERS: usize = 40; -const MAX_MAP_CHANGES: usize = 4; -const MAX_MAP_VARIANTS: usize = 4096; -/// Stems the scan of round 6 expands. -const SCAN_STEMS: usize = 48; - -const COST_CASE: u32 = 0; -const COST_ADD_DIACRITIC: u32 = 4; -const COST_OTHER_MAP: u32 = 7; -const COST_DROP_DIACRITIC: u32 = 11; -const COST_REP: u32 = 10; -const COST_SPLIT: u32 = 12; -const COST_SPLIT_RARE: u32 = 17; -const COST_KEY: u32 = 13; -const COST_SWAP: u32 = 14; -const COST_DROP_OR_ADD: u32 = 15; -const COST_MISTYPE: u32 = 16; -const COST_SCAN: u32 = 20; - -struct Collector<'a> { - d: &'a AffixDict, - /// The word, lower case. - input: String, - tested: HashSet, - /// Suggestion → (cost, order found). - found: HashMap, - seq: usize, -} - -impl Collector<'_> { - /// Check a lower-case candidate (a word, or two separated by a space) and keep it if every - /// word is in the dictionary, as written or capitalized (names). - fn offer(&mut self, cand: &str, cost: u32) { - if cand.is_empty() || cand == self.input || !self.tested.insert(cand.to_string()) { - return; - } - let mut words = Vec::new(); - let mut cost = cost; - for part in cand.split(' ') { - if self.d.contains_suggestible(part) { - words.push(part.to_string()); - } else { - let c = capitalize(part); - if !self.d.contains_suggestible(&c) { - return; - } - words.push(c); - // A name ranks after a common word at the same distance (a capitalized name). - cost += 1; - } - } - self.keep(words.join(" "), cost); - } - - /// Keep a suggestion known to be correct. - fn keep(&mut self, text: String, cost: u32) { - if !text.split(' ').all(|part| self.d.can_suggest(part)) { - return; - } - self.seq += 1; - let seq = self.seq; - let e = self.found.entry(text).or_insert((cost, seq)); - if cost < e.0 { - *e = (cost, seq); - } - } - - fn count_within(&self, cost: u32) -> usize { - self.found.values().filter(|(c, _)| *c <= cost).count() - } -} - -/// The MAP group of each letter (its other members are its likely confusions). -fn alternatives(c: char, map: &[Vec]) -> Vec { - map.iter().filter(|g| g.contains(&c)).flat_map(|g| g.iter().copied()).filter(|&x| x != c).collect() -} - -/// The cost of changing letter `from` to its MAP relative `to`. Adding a missing accent is the -/// commonest slip (typing without diacritics); an accent the writer did type -/// is likelier right, so taking one away costs more than a known confusion from the REP table. -fn map_cost(from: char, to: char) -> u32 { - match (from.is_ascii(), to.is_ascii()) { - (true, false) => COST_ADD_DIACRITIC, - (false, true) => COST_DROP_DIACRITIC, - _ => COST_OTHER_MAP, - } -} - -/// Every way of changing up to [`MAX_MAP_CHANGES`] letters to a MAP relative, with its cost. -fn map_variants(chars: &[char], alts: &[Vec], i: usize, changes: usize, cost: u32, buf: &mut Vec, out: &mut Vec<(String, u32)>) { - if out.len() >= MAX_MAP_VARIANTS { - return; - } - let Some(&c) = chars.get(i) else { - if changes > 0 { - out.push((buf.iter().collect(), cost)); - } - return; - }; - buf.push(c); - map_variants(chars, alts, i + 1, changes, cost, buf, out); - buf.pop(); - if changes < MAX_MAP_CHANGES { - for &a in alts.get(i).map(Vec::as_slice).unwrap_or_default() { - buf.push(a); - map_variants(chars, alts, i + 1, changes + 1, cost + map_cost(c, a), buf, out); - buf.pop(); - } - } -} - -/// A letter's MAP representative (the first of its group), lower case: as defined by the installed table. -fn fold(c: char, map: &[Vec]) -> char { - let l = c.to_lowercase().next().unwrap_or(c); - map.iter().find(|g| g.contains(&l)).and_then(|g| g.first().copied()).unwrap_or(l) -} - -/// Optimal string alignment distance where a MAP slip (dictionary-defined relatives) costs 1 and any other -/// edit 2; `limit + 1` once it is certainly above `limit`. -fn weighted_distance(a: &[char], b: &[char], related: &dyn Fn(char, char) -> bool, limit: u32) -> u32 { - let m = b.len(); - let over = limit + 1; - let mut prev2 = vec![0u32; m + 1]; - let mut prev: Vec = (0..=m as u32).map(|j| j * 2).collect(); - let mut cur = vec![0u32; m + 1]; - for (i, &ai) in a.iter().enumerate() { - if let Some(c) = cur.get_mut(0) { - *c = (i as u32 + 1) * 2; - } - let mut row_min = u32::MAX; - for (j, &bj) in b.iter().enumerate() { - let sub = if ai == bj { - 0 - } else if related(ai, bj) { - 1 - } else { - 2 - }; - let at = |v: &[u32], k: usize| v.get(k).copied().unwrap_or(u32::MAX / 4); - let mut v = (at(&prev, j + 1) + 2).min(at(&cur, j) + 2).min(at(&prev, j) + sub); - if i > 0 && j > 0 && a.get(i - 1) == Some(&bj) && b.get(j - 1) == Some(&ai) { - v = v.min(at(&prev2, j - 1) + 2); - } - if let Some(c) = cur.get_mut(j + 1) { - *c = v; - } - row_min = row_min.min(v); - } - if m > 0 && row_min > limit { - return over; - } - std::mem::swap(&mut prev2, &mut prev); - std::mem::swap(&mut prev, &mut cur); - } - prev.get(m).copied().unwrap_or(over).min(over) -} +use crate::affix::AffixDict; +use crate::affix_suggest::Tuning; /// Spelling suggestions for a Czech word, best first, at most `max`, in the word's letter case. pub fn suggest(word: &str, max: usize) -> Vec { @@ -171,201 +11,13 @@ pub fn suggest(word: &str, max: usize) -> Vec { /// [`suggest`] against a given dictionary. pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { - let word = word.trim(); - let n = word.chars().count(); - if n == 0 || n > MAX_LETTERS || max == 0 || d.is_empty() || word.contains(char::is_whitespace) { - return Vec::new(); - } - let case = casing(word); - let input = word.to_lowercase(); - let chars: Vec = input.chars().collect(); - let mut col = Collector { d, input: input.clone(), tested: HashSet::new(), found: HashMap::new(), seq: 0 }; - // 1. Letter case. - if d.contains(&input) && word != input { - col.keep(input.clone(), COST_CASE); - } else if d.contains(&capitalize(&input)) && word != capitalize(&input) { - col.keep(capitalize(&input), COST_CASE); - } - // 2. Accents and other MAP slips. - let map = d.map(); - let alts: Vec> = chars.iter().map(|&c| alternatives(c, map)).collect(); - let mut variants = Vec::new(); - map_variants(&chars, &alts, 0, 0, 0, &mut Vec::with_capacity(n), &mut variants); - variants.sort_by_key(|v| v.1); - for (v, cost) in &variants { - col.offer(v, *cost); - } - // 3. Common confusions. - for (from, to) in d.rep() { - if from.is_empty() { - continue; - } - for (i, _) in input.match_indices(from.as_str()) { - if let (Some(a), Some(b)) = (input.get(..i), input.get(i + from.len()..)) { - col.offer(&format!("{a}{to}{b}"), COST_REP); - } - } - } - // 4. A missing space; both halves must be dictionary words. - for (i, _) in input.char_indices().skip(1) { - let (Some(a), Some(b)) = (input.get(..i), input.get(i..)) else { continue }; - let long = |part: &str| part.chars().count() >= 3; - let cost = if long(a) && long(b) { COST_SPLIT } else { COST_SPLIT_RARE }; - col.offer(&format!("{a} {b}"), cost); - } - // 5. Keyboard neighbours from the installed dictionary's KEY rows. - for row in d.key().split('|').take(32) { - let keys: Vec = row.chars().take(128).collect(); - for pair in keys.windows(2) { - let [a, b] = pair else { continue }; - for (i, &c) in chars.iter().enumerate() { - let replacement = if c == *a { - Some(*b) - } else if c == *b { - Some(*a) - } else { - None - }; - if let Some(replacement) = replacement { - let mut v = chars.clone(); - if let Some(x) = v.get_mut(i) { - *x = replacement; - } - col.offer(&v.iter().collect::(), COST_KEY); - } - } - } - } - // One-letter edits use only the runtime TRY alphabet, with bounded work. - let mut letters: Vec = d.try_chars().chars().filter(|c| c.is_lowercase()).take(128).collect(); - letters.sort_unstable(); - letters.dedup(); - let s = |v: &[char]| v.iter().collect::(); - for i in 0..n.saturating_sub(1) { - if chars.get(i) != chars.get(i + 1) { - let mut v = chars.clone(); - v.swap(i, i + 1); - col.offer(&s(&v), COST_SWAP); - } - } - for i in 0..n { - let mut v = chars.clone(); - v.remove(i); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - for i in 0..=n { - for &c in &letters { - let mut v = chars.clone(); - v.insert(i, c); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - } - for i in 0..n { - for &c in &letters { - if chars.get(i) != Some(&c) { - let mut v = chars.clone(); - if let Some(x) = v.get_mut(i) { - *x = c; - } - col.offer(&s(&v), COST_MISTYPE); - } - } - } - // 6. Further off: scan the stems that start like the word. - if col.count_within(COST_MISTYPE) < max { - scan(&mut col, &chars); - } - let mut found: Vec<(String, (u32, usize))> = col.found.into_iter().collect(); - // Among equal costs, the change nearest the word's start is the likelier intent - // (the first changed letter breaks ties). - let diff_at = |text: &str| chars.iter().zip(text.chars()).position(|(a, b)| *a != b).unwrap_or(n); - found.sort_by_key(|(text, (cost, seq))| (*cost, diff_at(text), *seq)); - let mut out: Vec = Vec::new(); - for (text, _) in found { - let t = match case { - Casing::Initial => capitalize(&text), - Casing::Upper if n > 1 => text.to_uppercase(), - _ => text, - }; - if t != word && !out.contains(&t) { - out.push(t); - } - if out.len() >= max { - break; - } - } - out -} - -/// Round 6: rank the stems starting with the word's first letter (or a MAP relative of it, either -/// case) by shared letter pairs, then keep the forms of the best within two typos. -fn scan(col: &mut Collector, chars: &[char]) { - let d = col.d; - let map = d.map(); - let Some(&first) = chars.first() else { return }; - let folded: Vec = chars.iter().map(|&c| fold(c, map)).collect(); - let pairs: HashSet<(char, char)> = std::iter::once(' ').chain(folded.iter().copied()).zip(folded.iter().copied()).collect(); - let mut firsts: Vec = std::iter::once(first).chain(alternatives(first, map)).collect(); - let upper: Vec = firsts.iter().flat_map(|c| c.to_uppercase()).collect(); - firsts.extend(upper); - firsts.sort_unstable(); - firsts.dedup(); - let n = folded.len(); - let mut best: Vec<(u32, &str)> = Vec::new(); - for f in firsts { - let prefix = f.to_string(); - for stem in d.stems_with_prefix(&prefix) { - let sf: Vec = stem.chars().map(|c| fold(c, map)).collect(); - let shared = std::iter::once(' ').chain(sf.iter().copied()).zip(sf.iter().copied()).filter(|p| pairs.contains(p)).count(); - if shared < 2 { - continue; - } - // Shared pairs, less the stem's letters the word doesn't explain, less a length gap - // beyond what an ending explains. - let extra = sf.len().saturating_sub(shared); - let gap = sf.len().abs_diff(n).saturating_sub(4); - let score = (shared * 4).saturating_sub(extra * 3 + gap * 2) as u32; - if score == 0 { - continue; - } - if best.len() < SCAN_STEMS { - best.push((score, stem)); - } else if let Some(min) = best.iter_mut().min_by_key(|b| b.0) - && score > min.0 - { - *min = (score, stem); - } - } - } - best.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(b.1))); - let related = |a: char, b: char| fold(a, map) == fold(b, map); - let limit = 4; - for (_, stem) in best { - for form in d.forms(stem) { - let lf: Vec = form.to_lowercase().chars().collect(); - if lf.len().abs_diff(chars.len()) > 2 { - continue; - } - let dist = weighted_distance(&lf, chars, &related, limit); - if dist <= limit && lf.as_slice() != chars { - col.keep(form, COST_SCAN + dist * 3); - } - } - } + crate::affix_suggest::suggest_in(d, word, max, &Tuning::DEFAULT) } #[cfg(test)] mod tests { use super::*; - #[test] - fn an_extra_trailing_letter_is_dropped_before_a_short_word_split() { - let d = AffixDict::from_hunspell("", "2\npočítač\nz\n", &[]).unwrap(); - let suggestions = suggest_in(&d, "počítačz", 8); - assert_eq!(suggestions.first().map(String::as_str), Some("počítač"), "{suggestions:?}"); - assert!(suggestions.contains(&"počítač z".into()), "{suggestions:?}"); - } - #[test] fn suggestions_are_fast_enough() { crate::cs::test_dict::install(); diff --git a/crates/proof/src/es/suggest.rs b/crates/proof/src/es/suggest.rs index b42efa2ee3..0568ae8701 100644 --- a/crates/proof/src/es/suggest.rs +++ b/crates/proof/src/es/suggest.rs @@ -1,5 +1,5 @@ -//! Spanish suggestions. The ranking is the dictionary-driven one Czech uses -//! ([`crate::cs::suggest::suggest_in`]: case, MAP — the Spanish dictionaries map each vowel to +//! Spanish suggestions. The ranking is the shared dictionary-driven one +//! ([`crate::affix_suggest`]: case, MAP — the Spanish dictionaries map each vowel to //! its accented forms and `n` to `ñ`, so `tambien` → `también` comes first —, REP, missing spaces, //! KEY, TRY edits and a stem scan); every table comes from the variety's installed dictionary. //! In Uruguayan Spanish a `vos` form typed without its accent is offered with it first @@ -7,7 +7,7 @@ use crate::ProofLang; use crate::affix::{AffixDict, Casing, capitalize, casing}; -use crate::cs::suggest::MAX_LETTERS; +use crate::affix_suggest::{MAX_LETTERS, Tuning}; /// Spelling suggestions for a word in `lang`'s Spanish, best first, at most `max`, in the word's /// case. @@ -30,7 +30,7 @@ pub fn suggest_in(d: &AffixDict, word: &str, max: usize, lang: ProofLang) -> Vec _ => v, }); } - for s in crate::cs::suggest::suggest_in(d, word, max) { + for s in crate::affix_suggest::suggest_in(d, word, max, &Tuning::DEFAULT) { if !out.contains(&s) { out.push(s); } diff --git a/crates/proof/src/hr/suggest.rs b/crates/proof/src/hr/suggest.rs index d6f8159aca..d8c19b99bc 100644 --- a/crates/proof/src/hr/suggest.rs +++ b/crates/proof/src/hr/suggest.rs @@ -1,12 +1,11 @@ -//! Croatian spelling suggestions using dictionary MAP/REP rules, edit distance, -//! missing spaces and a bounded search. Proper Croatian corrections depend on -//! the actual installed dictionary entries. +//! Croatian spelling suggestions, best first: the shared ranking ([`crate::affix_suggest`]) with +//! the words Croatian runs into their neighbours (`neznam` → `ne znam`), the letter groups of +//! Croatian diacritics and letters to try, for a dictionary without MAP or TRY lines. -use std::collections::{HashMap, HashSet}; +use crate::affix::AffixDict; +use crate::affix_suggest::{Split, Tuning}; -use crate::affix::{AffixDict, Casing, capitalize, casing}; - -/// Short words often run into the next one (`não é`, `da água`, `para isso`). +/// Short words often run into the next one (`ne znam`, `na primjer`, `da li`). const LEADING_WORDS: &[&str] = &[ "a", "ali", "i", "ili", "da", "do", "od", "na", "ne", "ni", "o", "po", "pod", "pri", "sa", "s", "se", "su", "u", "uz", "za", "iz", "je", "će", "bi", "bih", "što", "koji", "koja", "koje", "kao", "kad", "dok", "bez", "pred", "nad", @@ -14,162 +13,13 @@ const LEADING_WORDS: &[&str] = &[ const TRAILING_WORDS: &[&str] = &["je", "su", "se", "ga", "mi", "ti", "mu", "joj", "ih", "im", "li", "će", "ću", "bi", "bih", "i", "a", "u", "na", "za", "od", "do"]; -const MAX_LETTERS: usize = 40; -const MAX_MAP_CHANGES: usize = 4; -const MAX_MAP_VARIANTS: usize = 4096; -/// Stems the scan of round 6 expands. -const SCAN_STEMS: usize = 48; - -const COST_CASE: u32 = 0; -const COST_ADD_DIACRITIC: u32 = 4; -const COST_OTHER_MAP: u32 = 7; -const COST_DROP_DIACRITIC: u32 = 11; -const COST_REP: u32 = 10; -const COST_SPLIT: u32 = 12; -const COST_SPLIT_RARE: u32 = 18; -const COST_SWAP: u32 = 14; -const COST_DROP_OR_ADD: u32 = 15; -const COST_MISTYPE: u32 = 16; -const COST_SCAN: u32 = 20; - -struct Collector<'a> { - d: &'a AffixDict, - /// The word, lower case. - input: String, - tested: HashSet, - /// Suggestion → (cost, order found). - found: HashMap, - seq: usize, -} - -impl Collector<'_> { - /// Check a lower-case candidate (a word, or two separated by a space) and keep it if every - /// word is in the dictionary, as written or capitalized (names). - fn offer(&mut self, cand: &str, cost: u32) { - if cand.is_empty() || cand == self.input || !self.tested.insert(cand.to_string()) { - return; - } - let mut words = Vec::new(); - let mut cost = cost; - for part in cand.split(' ') { - if self.d.contains_suggestible(part) { - words.push(part.to_string()); - } else { - let c = capitalize(part); - if !self.d.contains_suggestible(&c) { - return; - } - words.push(c); - // A name ranks after a common word at the same distance (`moça`, then `Moça`). - cost += 1; - } - } - self.keep(words.join(" "), cost); - } - - /// Keep a suggestion known to be correct. - fn keep(&mut self, text: String, cost: u32) { - if !text.split(' ').all(|part| self.d.can_suggest(part)) { - return; - } - self.seq += 1; - let seq = self.seq; - let e = self.found.entry(text).or_insert((cost, seq)); - if cost < e.0 { - *e = (cost, seq); - } - } - - fn count_within(&self, cost: u32) -> usize { - self.found.values().filter(|(c, _)| *c <= cost).count() - } -} - -/// The MAP group of each letter (its other members are its likely confusions). -fn alternatives(c: char, map: &[Vec]) -> Vec { - map.iter().filter(|g| g.contains(&c)).flat_map(|g| g.iter().copied()).filter(|&x| x != c).collect() -} - -/// The cost of changing letter `from` to its MAP relative `to`. Adding a missing accent is the -/// commonest slip (Croatian typed without accents: `coracao`); an accent the writer did type -/// is likelier right, so taking one away costs more than a known confusion from the REP table -/// (`seje` → `seja` before `sejé`). -fn map_cost(from: char, to: char) -> u32 { - match (from.is_ascii(), to.is_ascii()) { - (true, false) => COST_ADD_DIACRITIC, - (false, true) => COST_DROP_DIACRITIC, - _ => COST_OTHER_MAP, - } -} - -/// Every way of changing up to [`MAX_MAP_CHANGES`] letters to a MAP relative, with its cost. -fn map_variants(chars: &[char], alts: &[Vec], i: usize, changes: usize, cost: u32, buf: &mut Vec, out: &mut Vec<(String, u32)>) { - if out.len() >= MAX_MAP_VARIANTS { - return; - } - let Some(&c) = chars.get(i) else { - if changes > 0 { - out.push((buf.iter().collect(), cost)); - } - return; - }; - buf.push(c); - map_variants(chars, alts, i + 1, changes, cost, buf, out); - buf.pop(); - if changes < MAX_MAP_CHANGES { - for &a in alts.get(i).map(Vec::as_slice).unwrap_or_default() { - buf.push(a); - map_variants(chars, alts, i + 1, changes + 1, cost + map_cost(c, a), buf, out); - buf.pop(); - } - } -} - -/// A letter's MAP representative (the first of its group), lower case: `ã` → `a`, `ç` → `c`. -fn fold(c: char, map: &[Vec]) -> char { - let l = c.to_lowercase().next().unwrap_or(c); - map.iter().find(|g| g.contains(&l)).and_then(|g| g.first().copied()).unwrap_or(l) -} - -/// Optimal string alignment distance where a MAP slip (`a`/`á`, `c`/`ç`) costs 1 and any other -/// edit 2; `limit + 1` once it is certainly above `limit`. -fn weighted_distance(a: &[char], b: &[char], related: &dyn Fn(char, char) -> bool, limit: u32) -> u32 { - let m = b.len(); - let over = limit + 1; - let mut prev2 = vec![0u32; m + 1]; - let mut prev: Vec = (0..=m as u32).map(|j| j * 2).collect(); - let mut cur = vec![0u32; m + 1]; - for (i, &ai) in a.iter().enumerate() { - if let Some(c) = cur.get_mut(0) { - *c = (i as u32 + 1) * 2; - } - let mut row_min = u32::MAX; - for (j, &bj) in b.iter().enumerate() { - let sub = if ai == bj { - 0 - } else if related(ai, bj) { - 1 - } else { - 2 - }; - let at = |v: &[u32], k: usize| v.get(k).copied().unwrap_or(u32::MAX / 4); - let mut v = (at(&prev, j + 1) + 2).min(at(&cur, j) + 2).min(at(&prev, j) + sub); - if i > 0 && j > 0 && a.get(i - 1) == Some(&bj) && b.get(j - 1) == Some(&ai) { - v = v.min(at(&prev2, j - 1) + 2); - } - if let Some(c) = cur.get_mut(j + 1) { - *c = v; - } - row_min = row_min.min(v); - } - if m > 0 && row_min > limit { - return over; - } - std::mem::swap(&mut prev2, &mut prev); - std::mem::swap(&mut prev, &mut cur); - } - prev.get(m).copied().unwrap_or(over).min(over) -} +/// What Croatian adds to its dictionary's tables. +const TUNING: Tuning = Tuning { + split: Split::Near { leading: LEADING_WORDS, trailing: TRAILING_WORDS }, + alphabet: "aeiourntsjkvlmdpbgczčćšžđfh", + map: &["cčć", "dđ", "sš", "zž"], + ..Tuning::DEFAULT +}; /// Spelling suggestions for a Croatian word, best first, at most `max`, in the word's letter case. pub fn suggest(word: &str, max: usize) -> Vec { @@ -178,173 +28,7 @@ pub fn suggest(word: &str, max: usize) -> Vec { /// [`suggest`] against a given dictionary. pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { - let word = word.trim(); - let n = word.chars().count(); - if n == 0 || n > MAX_LETTERS || max == 0 || d.is_empty() || word.contains(char::is_whitespace) { - return Vec::new(); - } - let case = casing(word); - let input = word.to_lowercase(); - let chars: Vec = input.chars().collect(); - let mut col = Collector { d, input: input.clone(), tested: HashSet::new(), found: HashMap::new(), seq: 0 }; - // 1. Letter case. - if d.contains(&input) && word != input { - col.keep(input.clone(), COST_CASE); - } else if d.contains(&capitalize(&input)) && word != capitalize(&input) { - col.keep(capitalize(&input), COST_CASE); - } - // 2. Accents and other MAP slips. - let fallback_map = vec![vec!['c', 'č', 'ć'], vec!['d', 'đ'], vec!['s', 'š'], vec!['z', 'ž']]; - let map = if d.map().is_empty() { &fallback_map } else { d.map() }; - let alts: Vec> = chars.iter().map(|&c| alternatives(c, map)).collect(); - let mut variants = Vec::new(); - map_variants(&chars, &alts, 0, 0, 0, &mut Vec::with_capacity(n), &mut variants); - variants.sort_by_key(|v| v.1); - for (v, cost) in &variants { - col.offer(v, *cost); - } - // 3. Common confusions. - for (from, to) in d.rep() { - if from.is_empty() { - continue; - } - for (i, _) in input.match_indices(from.as_str()) { - if let (Some(a), Some(b)) = (input.get(..i), input.get(i + from.len()..)) { - col.offer(&format!("{a}{to}{b}"), COST_REP); - } - } - } - // 4. A missing space: cheap after a short leading word or before a short trailing one, - // dearer between two longer words, never leaving a stray short half. - for (i, _) in input.char_indices().skip(1) { - let (Some(a), Some(b)) = (input.get(..i), input.get(i..)) else { continue }; - let long = |p: &str| p.chars().count() >= 3; - let cost = if (LEADING_WORDS.contains(&a) && long(b)) || (long(a) && TRAILING_WORDS.contains(&b)) { - COST_SPLIT - } else if long(a) && long(b) { - COST_SPLIT_RARE - } else { - continue; - }; - col.offer(&format!("{a} {b}"), cost); - } - // 5. One typo. - let mut letters: Vec = d.try_chars().chars().filter(|c| c.is_lowercase()).collect(); - if letters.is_empty() { - letters = "aeiourntsjkvlmdpbgczčćšžđfh".chars().collect(); - } - let s = |v: &[char]| v.iter().collect::(); - for i in 0..n.saturating_sub(1) { - if chars.get(i) != chars.get(i + 1) { - let mut v = chars.clone(); - v.swap(i, i + 1); - col.offer(&s(&v), COST_SWAP); - } - } - for i in 0..n { - let mut v = chars.clone(); - v.remove(i); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - for i in 0..=n { - for &c in &letters { - let mut v = chars.clone(); - v.insert(i, c); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - } - for i in 0..n { - for &c in &letters { - if chars.get(i) != Some(&c) { - let mut v = chars.clone(); - if let Some(x) = v.get_mut(i) { - *x = c; - } - col.offer(&s(&v), COST_MISTYPE); - } - } - } - // 6. Further off: scan the stems that start like the word. - if col.count_within(COST_MISTYPE) < max { - scan(&mut col, &chars); - } - let mut found: Vec<(String, (u32, usize))> = col.found.into_iter().collect(); - // Among equal costs, the change nearest the word's start is the likelier intent - // (`saúde` before `saudê`: both add an accent, but at the first difference). - let diff_at = |text: &str| chars.iter().zip(text.chars()).position(|(a, b)| *a != b).unwrap_or(n); - found.sort_by_key(|(text, (cost, seq))| (*cost, diff_at(text), *seq)); - let mut out: Vec = Vec::new(); - for (text, _) in found { - let t = match case { - Casing::Initial => capitalize(&text), - Casing::Upper if n > 1 => text.to_uppercase(), - _ => text, - }; - if t != word && !out.contains(&t) { - out.push(t); - } - if out.len() >= max { - break; - } - } - out -} - -/// Round 6: rank the stems starting with the word's first letter (or a MAP relative of it, either -/// case) by shared letter pairs, then keep the forms of the best within two typos. -fn scan(col: &mut Collector, chars: &[char]) { - let d = col.d; - let map = d.map(); - let Some(&first) = chars.first() else { return }; - let folded: Vec = chars.iter().map(|&c| fold(c, map)).collect(); - let pairs: HashSet<(char, char)> = std::iter::once(' ').chain(folded.iter().copied()).zip(folded.iter().copied()).collect(); - let mut firsts: Vec = std::iter::once(first).chain(alternatives(first, map)).collect(); - let upper: Vec = firsts.iter().flat_map(|c| c.to_uppercase()).collect(); - firsts.extend(upper); - firsts.sort_unstable(); - firsts.dedup(); - let n = folded.len(); - let mut best: Vec<(u32, &str)> = Vec::new(); - for f in firsts { - let prefix = f.to_string(); - for stem in d.stems_with_prefix(&prefix) { - let sf: Vec = stem.chars().map(|c| fold(c, map)).collect(); - let shared = std::iter::once(' ').chain(sf.iter().copied()).zip(sf.iter().copied()).filter(|p| pairs.contains(p)).count(); - if shared < 2 { - continue; - } - // Shared pairs, less the stem's letters the word doesn't explain, less a length gap - // beyond what an ending explains. - let extra = sf.len().saturating_sub(shared); - let gap = sf.len().abs_diff(n).saturating_sub(4); - let score = (shared * 4).saturating_sub(extra * 3 + gap * 2) as u32; - if score == 0 { - continue; - } - if best.len() < SCAN_STEMS { - best.push((score, stem)); - } else if let Some(min) = best.iter_mut().min_by_key(|b| b.0) - && score > min.0 - { - *min = (score, stem); - } - } - } - best.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(b.1))); - let related = |a: char, b: char| fold(a, map) == fold(b, map); - let limit = 4; - for (_, stem) in best { - for form in d.forms(stem) { - let lf: Vec = form.to_lowercase().chars().collect(); - if lf.len().abs_diff(chars.len()) > 2 { - continue; - } - let dist = weighted_distance(&lf, chars, &related, limit); - if dist <= limit && lf.as_slice() != chars { - col.keep(form, COST_SCAN + dist * 3); - } - } - } + crate::affix_suggest::suggest_in(d, word, max, &TUNING) } #[cfg(test)] @@ -354,6 +38,13 @@ mod tests { fn nosuggest_stems_and_forms_are_never_offered() { crate::affix::tests::assert_nosuggest_is_honoured(suggest_in); } + /// A dictionary without MAP lines still gets the Croatian diacritic groups: `kuca` → `kuća` + /// first, before a typo that costs more (`kuha`). + #[test] + fn diacritics_without_map_lines() { + let d = AffixDict::from_hunspell("", "2\nkuća\nkuha\n", &[]).unwrap(); + assert_eq!(suggest_in(&d, "kuca", 1), ["kuća"]); + } #[test] fn croatian_suggestions() { crate::hr::test_dict::install(); diff --git a/crates/proof/src/id/suggest.rs b/crates/proof/src/id/suggest.rs index 562af9a823..b42efde98e 100644 --- a/crates/proof/src/id/suggest.rs +++ b/crates/proof/src/id/suggest.rs @@ -1,7 +1,6 @@ -//! Indonesian suggestions. The ranking is the dictionary-driven one Czech uses -//! ([`crate::cs::suggest::suggest_in`]: case, MAP, REP, missing spaces, KEY, TRY edits and a stem -//! scan); every table comes from the installed `id_ID` dictionary, so nothing Indonesian is -//! hard-coded here. +//! Indonesian suggestions: the shared ranking ([`crate::affix_suggest`]: case, MAP, REP, missing +//! spaces, KEY, TRY edits and a stem scan); every table comes from the installed `id_ID` +//! dictionary, so nothing Indonesian is hard-coded here. use crate::affix::AffixDict; @@ -12,7 +11,7 @@ pub fn suggest(word: &str, max: usize) -> Vec { /// [`suggest`] against a given dictionary. pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { - crate::cs::suggest::suggest_in(d, word, max) + crate::affix_suggest::suggest_in(d, word, max, &crate::affix_suggest::Tuning::DEFAULT) } #[cfg(test)] diff --git a/crates/proof/src/it/suggest.rs b/crates/proof/src/it/suggest.rs index 6a043ff2b4..55f8690919 100644 --- a/crates/proof/src/it/suggest.rs +++ b/crates/proof/src/it/suggest.rs @@ -1,25 +1,9 @@ -//! Italian spelling suggestions, best first. -//! -//! Candidates come in rounds of rising cost, each checked against the dictionary: -//! 1. letter case (`roma` → `Roma`); -//! 2. missing or wrong accents, from the dictionary's `MAP` groups (`caffe` → `caffè`, -//! `perche` → `perché`), up to four letters changed — Italian typed without accents is the -//! most common slip, so adding one ranks first, while taking away an accent the writer typed -//! ranks after the known confusions; -//! 3. the dictionary's `REP` table of common confusions (`caffé` → `caffè`); -//! 4. a missing space (`perme` → `per me`, `amilano` → `a milano`), cheaper when one half is a -//! short function word; -//! 5. one typo: a letter left out, added, swapped with the next or mistyped (`TRY` letters); -//! 6. when that finds fewer than asked for: the stems that start like the word (accents -//! folded) and share the most letter pairs with it, whose forms are ranked by an edit distance -//! in which an accent slip costs half (two typos at most). -//! -//! The input is bounded (40 letters), and so is every round, so a call costs ten-odd thousand -//! dictionary lookups at most. +//! Italian spelling suggestions, best first: the shared ranking ([`crate::affix_suggest`]) with the +//! words Italian runs into their neighbours (`perme` → `per me`, `amilano` → `a milano`) and +//! letters to try for a dictionary without a TRY line. -use std::collections::{HashMap, HashSet}; - -use crate::affix::{AffixDict, Casing, capitalize, casing}; +use crate::affix::AffixDict; +use crate::affix_suggest::{Split, Tuning}; /// Short words often run into the next one (`per me`, `a Milano`, `di Roma`). const LEADING_WORDS: &[&str] = &[ @@ -35,161 +19,12 @@ const TRAILING_WORDS: &[&str] = &[ "mi", "ti", "si", "ci", "vi", "ne", "lo", "la", "li", "le", "gli", "me", "te", "lui", "lei", "noi", "voi", "loro", "che", "cosa", "come", "dove", "è", ]; -const MAX_LETTERS: usize = 40; -const MAX_MAP_CHANGES: usize = 4; -const MAX_MAP_VARIANTS: usize = 4096; -/// Stems the scan of round 6 expands. -const SCAN_STEMS: usize = 48; -const COST_CASE: u32 = 0; -const COST_ADD_DIACRITIC: u32 = 4; -const COST_OTHER_MAP: u32 = 7; -const COST_DROP_DIACRITIC: u32 = 11; -const COST_REP: u32 = 10; -const COST_SPLIT: u32 = 12; -const COST_SPLIT_RARE: u32 = 18; -const COST_SWAP: u32 = 14; -const COST_DROP_OR_ADD: u32 = 15; -const COST_MISTYPE: u32 = 16; -const COST_SCAN: u32 = 20; - -struct Collector<'a> { - d: &'a AffixDict, - /// The word, lower case. - input: String, - tested: HashSet, - /// Suggestion → (cost, order found). - found: HashMap, - seq: usize, -} - -impl Collector<'_> { - /// Check a lower-case candidate (a word, or two separated by a space) and keep it if every - /// word is in the dictionary, as written or capitalized (names). - fn offer(&mut self, cand: &str, cost: u32) { - if cand.is_empty() || cand == self.input || !self.tested.insert(cand.to_string()) { - return; - } - let mut words = Vec::new(); - let mut cost = cost; - for part in cand.split(' ') { - if self.d.contains_suggestible(part) { - words.push(part.to_string()); - } else { - let c = capitalize(part); - if !self.d.contains_suggestible(&c) { - return; - } - words.push(c); - // A name ranks after a common word at the same distance. - cost += 1; - } - } - self.keep(words.join(" "), cost); - } - - /// Keep a suggestion known to be correct. - fn keep(&mut self, text: String, cost: u32) { - if !text.split(' ').all(|part| self.d.can_suggest(part)) { - return; - } - self.seq += 1; - let seq = self.seq; - let e = self.found.entry(text).or_insert((cost, seq)); - if cost < e.0 { - *e = (cost, seq); - } - } - - fn count_within(&self, cost: u32) -> usize { - self.found.values().filter(|(c, _)| *c <= cost).count() - } -} - -/// The MAP group of each letter (its other members are its likely confusions). -fn alternatives(c: char, map: &[Vec]) -> Vec { - map.iter().filter(|g| g.contains(&c)).flat_map(|g| g.iter().copied()).filter(|&x| x != c).collect() -} - -/// The cost of changing letter `from` to its MAP relative `to`. Adding a missing accent is the -/// commonest slip (Italian typed without accents: `citta`); an accent the writer did type -/// is likelier right, so taking one away costs more than a known confusion from the REP table. -fn map_cost(from: char, to: char) -> u32 { - match (from.is_ascii(), to.is_ascii()) { - (true, false) => COST_ADD_DIACRITIC, - (false, true) => COST_DROP_DIACRITIC, - _ => COST_OTHER_MAP, - } -} - -/// Every way of changing up to [`MAX_MAP_CHANGES`] letters to a MAP relative, with its cost. -fn map_variants(chars: &[char], alts: &[Vec], i: usize, changes: usize, cost: u32, buf: &mut Vec, out: &mut Vec<(String, u32)>) { - if out.len() >= MAX_MAP_VARIANTS { - return; - } - let Some(&c) = chars.get(i) else { - if changes > 0 { - out.push((buf.iter().collect(), cost)); - } - return; +/// What Italian adds to its dictionary's tables. +const TUNING: Tuning = + Tuning { + split: Split::Near { leading: LEADING_WORDS, trailing: TRAILING_WORDS }, alphabet: "aeiortnslcdupmghfbqvàèéìòóùz", ..Tuning::DEFAULT }; - buf.push(c); - map_variants(chars, alts, i + 1, changes, cost, buf, out); - buf.pop(); - if changes < MAX_MAP_CHANGES { - for &a in alts.get(i).map(Vec::as_slice).unwrap_or_default() { - buf.push(a); - map_variants(chars, alts, i + 1, changes + 1, cost + map_cost(c, a), buf, out); - buf.pop(); - } - } -} - -/// A letter's MAP representative (the first of its group), lower case: `à` → `a`. -fn fold(c: char, map: &[Vec]) -> char { - let l = c.to_lowercase().next().unwrap_or(c); - map.iter().find(|g| g.contains(&l)).and_then(|g| g.first().copied()).unwrap_or(l) -} - -/// Optimal string alignment distance where a MAP slip (`a`/`à`, `e`/`è`) costs 1 and any other -/// edit 2; `limit + 1` once it is certainly above `limit`. -fn weighted_distance(a: &[char], b: &[char], related: &dyn Fn(char, char) -> bool, limit: u32) -> u32 { - let m = b.len(); - let over = limit + 1; - let mut prev2 = vec![0u32; m + 1]; - let mut prev: Vec = (0..=m as u32).map(|j| j * 2).collect(); - let mut cur = vec![0u32; m + 1]; - for (i, &ai) in a.iter().enumerate() { - if let Some(c) = cur.get_mut(0) { - *c = (i as u32 + 1) * 2; - } - let mut row_min = u32::MAX; - for (j, &bj) in b.iter().enumerate() { - let sub = if ai == bj { - 0 - } else if related(ai, bj) { - 1 - } else { - 2 - }; - let at = |v: &[u32], k: usize| v.get(k).copied().unwrap_or(u32::MAX / 4); - let mut v = (at(&prev, j + 1) + 2).min(at(&cur, j) + 2).min(at(&prev, j) + sub); - if i > 0 && j > 0 && a.get(i - 1) == Some(&bj) && b.get(j - 1) == Some(&ai) { - v = v.min(at(&prev2, j - 1) + 2); - } - if let Some(c) = cur.get_mut(j + 1) { - *c = v; - } - row_min = row_min.min(v); - } - if m > 0 && row_min > limit { - return over; - } - std::mem::swap(&mut prev2, &mut prev); - std::mem::swap(&mut prev, &mut cur); - } - prev.get(m).copied().unwrap_or(over).min(over) -} /// Spelling suggestions for an Italian word, best first, at most `max`, in the word's letter case. pub fn suggest(word: &str, max: usize) -> Vec { @@ -198,176 +33,13 @@ pub fn suggest(word: &str, max: usize) -> Vec { /// [`suggest`] against a given dictionary. pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { - let word = word.trim(); - let n = word.chars().count(); - if n == 0 || n > MAX_LETTERS || max == 0 || d.is_empty() || word.contains(char::is_whitespace) { - return Vec::new(); - } - let case = casing(word); - let input = word.to_lowercase(); - let chars: Vec = input.chars().collect(); - let mut col = Collector { d, input: input.clone(), tested: HashSet::new(), found: HashMap::new(), seq: 0 }; - // 1. Letter case. - if d.contains(&input) && word != input { - col.keep(input.clone(), COST_CASE); - } else if d.contains(&capitalize(&input)) && word != capitalize(&input) { - col.keep(capitalize(&input), COST_CASE); - } - // 2. Accents and other MAP slips. - let map = d.map(); - let alts: Vec> = chars.iter().map(|&c| alternatives(c, map)).collect(); - let mut variants = Vec::new(); - map_variants(&chars, &alts, 0, 0, 0, &mut Vec::with_capacity(n), &mut variants); - variants.sort_by_key(|v| v.1); - for (v, cost) in &variants { - col.offer(v, *cost); - } - // 3. Common confusions. - for (from, to) in d.rep() { - if from.is_empty() { - continue; - } - for (i, _) in input.match_indices(from.as_str()) { - if let (Some(a), Some(b)) = (input.get(..i), input.get(i + from.len()..)) { - col.offer(&format!("{a}{to}{b}"), COST_REP); - } - } - } - // 4. A missing space: cheap after a short leading word or before a short trailing one, - // dearer between two longer words, never leaving a stray short half. - for (i, _) in input.char_indices().skip(1) { - let (Some(a), Some(b)) = (input.get(..i), input.get(i..)) else { continue }; - let long = |p: &str| p.chars().count() >= 3; - let cost = if (LEADING_WORDS.contains(&a) && long(b)) || (long(a) && TRAILING_WORDS.contains(&b)) { - COST_SPLIT - } else if long(a) && long(b) { - COST_SPLIT_RARE - } else { - continue; - }; - col.offer(&format!("{a} {b}"), cost); - } - // 5. One typo. - let mut letters: Vec = d.try_chars().chars().filter(|c| c.is_lowercase()).collect(); - if letters.is_empty() { - letters = "aeiortnslcdupmghfbqvàèéìòóùz".chars().collect(); - } - let s = |v: &[char]| v.iter().collect::(); - for i in 0..n.saturating_sub(1) { - if chars.get(i) != chars.get(i + 1) { - let mut v = chars.clone(); - v.swap(i, i + 1); - col.offer(&s(&v), COST_SWAP); - } - } - for i in 0..n { - let mut v = chars.clone(); - v.remove(i); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - for i in 0..=n { - for &c in &letters { - let mut v = chars.clone(); - v.insert(i, c); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - } - for i in 0..n { - for &c in &letters { - if chars.get(i) != Some(&c) { - let mut v = chars.clone(); - if let Some(x) = v.get_mut(i) { - *x = c; - } - col.offer(&s(&v), COST_MISTYPE); - } - } - } - // 6. Further off: scan the stems that start like the word. - if col.count_within(COST_MISTYPE) < max { - scan(&mut col, &chars); - } - let mut found: Vec<(String, (u32, usize))> = col.found.into_iter().collect(); - // Among equal costs, the change nearest the word's start is the likelier intent. - let diff_at = |text: &str| chars.iter().zip(text.chars()).position(|(a, b)| *a != b).unwrap_or(n); - found.sort_by_key(|(text, (cost, seq))| (*cost, diff_at(text), *seq)); - let mut out: Vec = Vec::new(); - for (text, _) in found { - let t = match case { - Casing::Initial => capitalize(&text), - Casing::Upper if n > 1 => text.to_uppercase(), - _ => text, - }; - if t != word && !out.contains(&t) { - out.push(t); - } - if out.len() >= max { - break; - } - } - out -} - -/// Round 6: rank the stems starting with the word's first letter (or a MAP relative of it, either -/// case) by shared letter pairs, then keep the forms of the best within two typos. -fn scan(col: &mut Collector, chars: &[char]) { - let d = col.d; - let map = d.map(); - let Some(&first) = chars.first() else { return }; - let folded: Vec = chars.iter().map(|&c| fold(c, map)).collect(); - let pairs: HashSet<(char, char)> = std::iter::once(' ').chain(folded.iter().copied()).zip(folded.iter().copied()).collect(); - let mut firsts: Vec = std::iter::once(first).chain(alternatives(first, map)).collect(); - let upper: Vec = firsts.iter().flat_map(|c| c.to_uppercase()).collect(); - firsts.extend(upper); - firsts.sort_unstable(); - firsts.dedup(); - let n = folded.len(); - let mut best: Vec<(u32, &str)> = Vec::new(); - for f in firsts { - let prefix = f.to_string(); - for stem in d.stems_with_prefix(&prefix) { - let sf: Vec = stem.chars().map(|c| fold(c, map)).collect(); - let shared = std::iter::once(' ').chain(sf.iter().copied()).zip(sf.iter().copied()).filter(|p| pairs.contains(p)).count(); - if shared < 2 { - continue; - } - // Shared pairs, less the stem's letters the word doesn't explain, less a length gap - // beyond what an ending explains. - let extra = sf.len().saturating_sub(shared); - let gap = sf.len().abs_diff(n).saturating_sub(4); - let score = (shared * 4).saturating_sub(extra * 3 + gap * 2) as u32; - if score == 0 { - continue; - } - if best.len() < SCAN_STEMS { - best.push((score, stem)); - } else if let Some(min) = best.iter_mut().min_by_key(|b| b.0) - && score > min.0 - { - *min = (score, stem); - } - } - } - best.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(b.1))); - let related = |a: char, b: char| fold(a, map) == fold(b, map); - let limit = 4; - for (_, stem) in best { - for form in d.forms(stem) { - let lf: Vec = form.to_lowercase().chars().collect(); - if lf.len().abs_diff(chars.len()) > 2 { - continue; - } - let dist = weighted_distance(&lf, chars, &related, limit); - if dist <= limit && lf.as_slice() != chars { - col.keep(form, COST_SCAN + dist * 3); - } - } - } + crate::affix_suggest::suggest_in(d, word, max, &TUNING) } #[cfg(test)] mod tests { use super::*; + use crate::affix_suggest::{fold, weighted_distance}; fn has(word: &str, want: &str) { let s = suggest(word, 8); diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index ad516b2950..4bf4da78b1 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -50,6 +50,7 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] pub mod affix; +mod affix_suggest; pub mod bg; pub mod cs; pub mod dict; diff --git a/crates/proof/src/pl/suggest.rs b/crates/proof/src/pl/suggest.rs index 0e32a1eaf2..7baeaa54fc 100644 --- a/crates/proof/src/pl/suggest.rs +++ b/crates/proof/src/pl/suggest.rs @@ -1,26 +1,9 @@ -//! Polish spelling suggestions, best first. -//! -//! Candidates come in rounds of rising cost, each checked against the dictionary: -//! 1. letter case (`warszawa` → `Warszawa`); -//! 2. missing or wrong diacritics, from the dictionary's `MAP` groups (`zolw` → `żółw`, `kturzy` -//! → `którzy`), up to four letters changed — Polish typed without diacritics is the most -//! common slip, so adding them ranks first, while taking away a diacritic the writer typed -//! ranks after the known confusions; -//! 3. the dictionary's `REP` table of common confusions (`żeka` → `rzeka`, `chuśtawka` → -//! `huśtawka`, `pszyjaciel` → `przyjaciel`); -//! 4. a missing space (`napewno` → `na pewno`, `wogóle` → `w ogóle`, `kochamcię` → `kocham -//! cię`), cheaper when one half is a short function word; -//! 5. one typo: a letter left out, added, swapped with the next or mistyped (`TRY` letters); -//! 6. when that finds fewer than asked for: the stems that start like the word (diacritics -//! folded) and share the most letter pairs with it, whose forms are ranked by an edit distance -//! in which a diacritic slip costs half (two typos at most). -//! -//! The input is bounded (40 letters), and so is every round, so a call costs a few thousand -//! dictionary lookups at most. +//! Polish spelling suggestions, best first: the shared ranking ([`crate::affix_suggest`]) with the +//! words Polish runs into their neighbours (`napewno` → `na pewno`, `kochamcię` → `kocham cię`), +//! letters to try for a dictionary without a TRY line, and final devoicing (`chlep` → `chleb`). -use std::collections::{HashMap, HashSet}; - -use crate::affix::{AffixDict, Casing, capitalize, casing}; +use crate::affix::AffixDict; +use crate::affix_suggest::{COST_ADD_DIACRITIC, COST_MISTYPE, Split, Tuning}; /// Short words often run into the next one (`na pewno`, `w ogóle`, `nie wiem`, `przede /// wszystkim`). @@ -32,162 +15,20 @@ const LEADING_WORDS: &[&str] = &[ /// Short words often run into the one before (`kocham cię`, `daj mi`, `bałem się`). const TRAILING_WORDS: &[&str] = &["się", "cię", "mi", "mu", "ci", "go", "ją", "je", "nam", "wam", "im", "by", "że", "no", "mnie", "tobie", "jej", "ich"]; -const MAX_LETTERS: usize = 40; -const MAX_MAP_CHANGES: usize = 4; -const MAX_MAP_VARIANTS: usize = 4096; -/// Stems the scan of round 6 expands. -const SCAN_STEMS: usize = 48; -const COST_CASE: u32 = 0; -const COST_ADD_DIACRITIC: u32 = 4; -const COST_OTHER_MAP: u32 = 7; -const COST_DROP_DIACRITIC: u32 = 11; -const COST_REP: u32 = 10; -const COST_SPLIT: u32 = 12; -const COST_SPLIT_RARE: u32 = 18; -const COST_SWAP: u32 = 14; -const COST_DROP_OR_ADD: u32 = 15; -const COST_MISTYPE: u32 = 16; -const COST_SCAN: u32 = 20; +/// What Polish adds to its dictionary's tables. +const TUNING: Tuning = Tuning { + split: Split::Near { leading: LEADING_WORDS, trailing: TRAILING_WORDS }, + alphabet: "aioeznrwcysptkmdłuljągbhęśćóżfńźvqx", + mistype_cost, + ..Tuning::DEFAULT +}; -struct Collector<'a> { - d: &'a AffixDict, - /// The word, lower case. - input: String, - tested: HashSet, - /// Suggestion → (cost, order found). - found: HashMap, - seq: usize, -} - -impl Collector<'_> { - /// Check a lower-case candidate (a word, or two separated by a space) and keep it if every - /// word is in the dictionary, as written or capitalized (names). - fn offer(&mut self, cand: &str, cost: u32) { - if cand.is_empty() || cand == self.input || !self.tested.insert(cand.to_string()) { - return; - } - let mut words = Vec::new(); - let mut cost = cost; - for part in cand.split(' ') { - if self.d.contains_suggestible(part) { - words.push(part.to_string()); - } else { - let c = capitalize(part); - if !self.d.contains_suggestible(&c) { - return; - } - words.push(c); - // A name ranks after a common word at the same distance (`moze` → `może`, then - // `Mozę`). - cost += 1; - } - } - self.keep(words.join(" "), cost); - } - - /// Keep a suggestion known to be correct. - fn keep(&mut self, text: String, cost: u32) { - if !text.split(' ').all(|part| self.d.can_suggest(part)) { - return; - } - self.seq += 1; - let seq = self.seq; - let e = self.found.entry(text).or_insert((cost, seq)); - if cost < e.0 { - *e = (cost, seq); - } - } - - fn count_within(&self, cost: u32) -> usize { - self.found.values().filter(|(c, _)| *c <= cost).count() - } -} - -/// The MAP group of each letter (its other members are its likely confusions). -fn alternatives(c: char, map: &[Vec]) -> Vec { - map.iter().filter(|g| g.contains(&c)).flat_map(|g| g.iter().copied()).filter(|&x| x != c).collect() -} - -/// The cost of changing letter `from` to its MAP relative `to`. Adding a missing diacritic is the -/// commonest slip (Polish typed on a keyboard without Polish letters: `zolw`); a diacritic the -/// writer did type is likelier right, so taking one away costs more than a known confusion from -/// the REP table (`żeka` → `rzeka` before `zeka`). -fn map_cost(from: char, to: char) -> u32 { - match (from.is_ascii(), to.is_ascii()) { - (true, false) => COST_ADD_DIACRITIC, - (false, true) => COST_DROP_DIACRITIC, - _ => COST_OTHER_MAP, - } -} - -/// Every way of changing up to [`MAX_MAP_CHANGES`] letters to a MAP relative, with its cost. -fn map_variants(chars: &[char], alts: &[Vec], i: usize, changes: usize, cost: u32, buf: &mut Vec, out: &mut Vec<(String, u32)>) { - if out.len() >= MAX_MAP_VARIANTS { - return; - } - let Some(&c) = chars.get(i) else { - if changes > 0 { - out.push((buf.iter().collect(), cost)); - } - return; - }; - buf.push(c); - map_variants(chars, alts, i + 1, changes, cost, buf, out); - buf.pop(); - if changes < MAX_MAP_CHANGES { - for &a in alts.get(i).map(Vec::as_slice).unwrap_or_default() { - buf.push(a); - map_variants(chars, alts, i + 1, changes + 1, cost + map_cost(c, a), buf, out); - buf.pop(); - } - } -} - -/// A letter's MAP representative (the first of its group), lower case: `ż` → `z`, `ó` → `o`. -fn fold(c: char, map: &[Vec]) -> char { - let l = c.to_lowercase().next().unwrap_or(c); - map.iter().find(|g| g.contains(&l)).and_then(|g| g.first().copied()).unwrap_or(l) -} - -/// Optimal string alignment distance where a MAP slip (`a`/`ą`, `u`/`ó`) costs 1 and any other -/// edit 2; `limit + 1` once it is certainly above `limit`. -fn weighted_distance(a: &[char], b: &[char], related: &dyn Fn(char, char) -> bool, limit: u32) -> u32 { - let m = b.len(); - let over = limit + 1; - let mut prev2 = vec![0u32; m + 1]; - let mut prev: Vec = (0..=m as u32).map(|j| j * 2).collect(); - let mut cur = vec![0u32; m + 1]; - for (i, &ai) in a.iter().enumerate() { - if let Some(c) = cur.get_mut(0) { - *c = (i as u32 + 1) * 2; - } - let mut row_min = u32::MAX; - for (j, &bj) in b.iter().enumerate() { - let sub = if ai == bj { - 0 - } else if related(ai, bj) { - 1 - } else { - 2 - }; - let at = |v: &[u32], k: usize| v.get(k).copied().unwrap_or(u32::MAX / 4); - let mut v = (at(&prev, j + 1) + 2).min(at(&cur, j) + 2).min(at(&prev, j) + sub); - if i > 0 && j > 0 && a.get(i - 1) == Some(&bj) && b.get(j - 1) == Some(&ai) { - v = v.min(at(&prev2, j - 1) + 2); - } - if let Some(c) = cur.get_mut(j + 1) { - *c = v; - } - row_min = row_min.min(v); - } - if m > 0 && row_min > limit { - return over; - } - std::mem::swap(&mut prev2, &mut prev); - std::mem::swap(&mut prev, &mut cur); - } - prev.get(m).copied().unwrap_or(over).min(over) +/// Final devoicing makes a voiced consonant at the end of a word sound voiceless, so writing it +/// voiceless (`chlep` for `chleb`) is a likelier slip than any other typo. +fn mistype_cost(chars: &[char], i: usize, c: char) -> u32 { + let last = i + 1 == chars.len(); + if last && matches!((chars.get(i), c), (Some('p'), 'b') | (Some('t'), 'd') | (Some('k'), 'g')) { COST_ADD_DIACRITIC - 1 } else { COST_MISTYPE } } /// Spelling suggestions for a Polish word, best first, at most `max`, in the word's letter case. @@ -197,177 +38,13 @@ pub fn suggest(word: &str, max: usize) -> Vec { /// [`suggest`] against a given dictionary. pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { - let word = word.trim(); - let n = word.chars().count(); - if n == 0 || n > MAX_LETTERS || max == 0 || d.is_empty() || word.contains(char::is_whitespace) { - return Vec::new(); - } - let case = casing(word); - let input = word.to_lowercase(); - let chars: Vec = input.chars().collect(); - let mut col = Collector { d, input: input.clone(), tested: HashSet::new(), found: HashMap::new(), seq: 0 }; - // 1. Letter case. - if d.contains(&input) && word != input { - col.keep(input.clone(), COST_CASE); - } else if d.contains(&capitalize(&input)) && word != capitalize(&input) { - col.keep(capitalize(&input), COST_CASE); - } - // 2. Diacritics and other MAP slips. - let map = d.map(); - let alts: Vec> = chars.iter().map(|&c| alternatives(c, map)).collect(); - let mut variants = Vec::new(); - map_variants(&chars, &alts, 0, 0, 0, &mut Vec::with_capacity(n), &mut variants); - variants.sort_by_key(|v| v.1); - for (v, cost) in &variants { - col.offer(v, *cost); - } - // 3. Common confusions. - for (from, to) in d.rep() { - if from.is_empty() { - continue; - } - for (i, _) in input.match_indices(from.as_str()) { - if let (Some(a), Some(b)) = (input.get(..i), input.get(i + from.len()..)) { - col.offer(&format!("{a}{to}{b}"), COST_REP); - } - } - } - // 4. A missing space: cheap after a short leading word or before a short trailing one, - // dearer between two longer words, never leaving a stray short half (`że ka`). - for (i, _) in input.char_indices().skip(1) { - let (Some(a), Some(b)) = (input.get(..i), input.get(i..)) else { continue }; - let long = |p: &str| p.chars().count() >= 3; - let cost = if (LEADING_WORDS.contains(&a) && long(b)) || (long(a) && TRAILING_WORDS.contains(&b)) { - COST_SPLIT - } else if long(a) && long(b) { - COST_SPLIT_RARE - } else { - continue; - }; - col.offer(&format!("{a} {b}"), cost); - } - // 5. One typo. - let mut letters: Vec = d.try_chars().chars().filter(|c| c.is_lowercase()).collect(); - if letters.is_empty() { - letters = "aioeznrwcysptkmdłuljągbhęśćóżfńźvqx".chars().collect(); - } - let s = |v: &[char]| v.iter().collect::(); - for i in 0..n.saturating_sub(1) { - if chars.get(i) != chars.get(i + 1) { - let mut v = chars.clone(); - v.swap(i, i + 1); - col.offer(&s(&v), COST_SWAP); - } - } - for i in 0..n { - let mut v = chars.clone(); - v.remove(i); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - for i in 0..=n { - for &c in &letters { - let mut v = chars.clone(); - v.insert(i, c); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - } - for i in 0..n { - for &c in &letters { - if chars.get(i) != Some(&c) { - let mut v = chars.clone(); - if let Some(x) = v.get_mut(i) { - *x = c; - } - // Final devoicing can make a voiced consonant sound voiceless. - let final_devoicing = i == n - 1 && matches!((chars[i], c), ('p', 'b') | ('t', 'd') | ('k', 'g')); - let cost = if final_devoicing { COST_ADD_DIACRITIC - 1 } else { COST_MISTYPE }; - col.offer(&s(&v), cost); - } - } - } - // 6. Further off: scan the stems that start like the word. - if col.count_within(COST_MISTYPE) < max { - scan(&mut col, &chars); - } - let mut found: Vec<(String, (u32, usize))> = col.found.into_iter().collect(); - found.sort_by_key(|(_, k)| *k); - let mut out: Vec = Vec::new(); - for (text, _) in found { - let t = match case { - Casing::Initial => capitalize(&text), - Casing::Upper if n > 1 => text.to_uppercase(), - _ => text, - }; - if t != word && !out.contains(&t) { - out.push(t); - } - if out.len() >= max { - break; - } - } - out -} - -/// Round 6: rank the stems starting with the word's first letter (or a MAP relative of it, either -/// case) by shared letter pairs, then keep the forms of the best within two typos. -fn scan(col: &mut Collector, chars: &[char]) { - let d = col.d; - let map = d.map(); - let Some(&first) = chars.first() else { return }; - let folded: Vec = chars.iter().map(|&c| fold(c, map)).collect(); - let pairs: HashSet<(char, char)> = std::iter::once(' ').chain(folded.iter().copied()).zip(folded.iter().copied()).collect(); - let mut firsts: Vec = std::iter::once(first).chain(alternatives(first, map)).collect(); - let upper: Vec = firsts.iter().flat_map(|c| c.to_uppercase()).collect(); - firsts.extend(upper); - firsts.sort_unstable(); - firsts.dedup(); - let n = folded.len(); - let mut best: Vec<(u32, &str)> = Vec::new(); - for f in firsts { - let prefix = f.to_string(); - for stem in d.stems_with_prefix(&prefix) { - let sf: Vec = stem.chars().map(|c| fold(c, map)).collect(); - let shared = std::iter::once(' ').chain(sf.iter().copied()).zip(sf.iter().copied()).filter(|p| pairs.contains(p)).count(); - if shared < 2 { - continue; - } - // Shared pairs, less the stem's letters the word doesn't explain, less a length gap - // beyond what an ending explains. - let extra = sf.len().saturating_sub(shared); - let gap = sf.len().abs_diff(n).saturating_sub(4); - let score = (shared * 4).saturating_sub(extra * 3 + gap * 2) as u32; - if score == 0 { - continue; - } - if best.len() < SCAN_STEMS { - best.push((score, stem)); - } else if let Some(min) = best.iter_mut().min_by_key(|b| b.0) - && score > min.0 - { - *min = (score, stem); - } - } - } - best.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(b.1))); - let related = |a: char, b: char| fold(a, map) == fold(b, map); - let limit = 4; - for (_, stem) in best { - for form in d.forms(stem) { - let lf: Vec = form.to_lowercase().chars().collect(); - if lf.len().abs_diff(chars.len()) > 2 { - continue; - } - let dist = weighted_distance(&lf, chars, &related, limit); - if dist <= limit && lf.as_slice() != chars { - col.keep(form, COST_SCAN + dist * 3); - } - } - } + crate::affix_suggest::suggest_in(d, word, max, &TUNING) } #[cfg(test)] mod tests { use super::*; + use crate::affix_suggest::{fold, weighted_distance}; fn has(word: &str, want: &str) { let s = suggest(word, 8); diff --git a/crates/proof/src/pt/suggest.rs b/crates/proof/src/pt/suggest.rs index 36f1c7b07f..f8f33bff69 100644 --- a/crates/proof/src/pt/suggest.rs +++ b/crates/proof/src/pt/suggest.rs @@ -1,26 +1,9 @@ -//! Portuguese spelling suggestions, best first. -//! -//! Candidates come in rounds of rising cost, each checked against the dictionary: -//! 1. letter case (`brasília` → `Brasília`); -//! 2. missing or wrong accents, from the dictionary's `MAP` groups (`coraçao` → `coração`, -//! `saude` → `saúde`), up to four letters changed — Portuguese typed without accents is the -//! most common slip, so adding one ranks first, while taking away an accent the writer typed -//! ranks after the known confusions; -//! 3. the dictionary's `REP` table of common confusions (`seje` → `seja`, `excessão` → -//! `exceção`, `previlégio` → `privilégio`); -//! 4. a missing space (`nãoé` → `não é`, `paraela` → `para ela`), cheaper when one half is a -//! short function word; -//! 5. one typo: a letter left out, added, swapped with the next or mistyped (`TRY` letters); -//! 6. when that finds fewer than asked for: the stems that start like the word (accents -//! folded) and share the most letter pairs with it, whose forms are ranked by an edit distance -//! in which an accent slip costs half (two typos at most). -//! -//! The input is bounded (40 letters), and so is every round, so a call costs ten-odd thousand -//! dictionary lookups at most. +//! Portuguese spelling suggestions, best first: the shared ranking ([`crate::affix_suggest`]) with +//! the words Portuguese runs into their neighbours (`nãoé` → `não é`, `paraela` → `para ela`) and +//! letters to try for a dictionary without a TRY line. -use std::collections::{HashMap, HashSet}; - -use crate::affix::{AffixDict, Casing, capitalize, casing}; +use crate::affix::AffixDict; +use crate::affix_suggest::{Split, Tuning}; /// Short words often run into the next one (`não é`, `da água`, `para isso`). const LEADING_WORDS: &[&str] = &[ @@ -35,162 +18,13 @@ const TRAILING_WORDS: &[&str] = &[ "me", "te", "se", "lhe", "nos", "vos", "lhes", "o", "a", "os", "as", "ele", "ela", "eles", "elas", "isso", "isto", "aquilo", "mim", "ti", "si", "eu", "tu", "que", "de", "em", "é", ]; -const MAX_LETTERS: usize = 40; -const MAX_MAP_CHANGES: usize = 4; -const MAX_MAP_VARIANTS: usize = 4096; -/// Stems the scan of round 6 expands. -const SCAN_STEMS: usize = 48; -const COST_CASE: u32 = 0; -const COST_ADD_DIACRITIC: u32 = 4; -const COST_OTHER_MAP: u32 = 7; -const COST_DROP_DIACRITIC: u32 = 11; -const COST_REP: u32 = 10; -const COST_SPLIT: u32 = 12; -const COST_SPLIT_RARE: u32 = 18; -const COST_SWAP: u32 = 14; -const COST_DROP_OR_ADD: u32 = 15; -const COST_MISTYPE: u32 = 16; -const COST_SCAN: u32 = 20; - -struct Collector<'a> { - d: &'a AffixDict, - /// The word, lower case. - input: String, - tested: HashSet, - /// Suggestion → (cost, order found). - found: HashMap, - seq: usize, -} - -impl Collector<'_> { - /// Check a lower-case candidate (a word, or two separated by a space) and keep it if every - /// word is in the dictionary, as written or capitalized (names). - fn offer(&mut self, cand: &str, cost: u32) { - if cand.is_empty() || cand == self.input || !self.tested.insert(cand.to_string()) { - return; - } - let mut words = Vec::new(); - let mut cost = cost; - for part in cand.split(' ') { - if self.d.contains_suggestible(part) { - words.push(part.to_string()); - } else { - let c = capitalize(part); - if !self.d.contains_suggestible(&c) { - return; - } - words.push(c); - // A name ranks after a common word at the same distance (`moça`, then `Moça`). - cost += 1; - } - } - self.keep(words.join(" "), cost); - } - - /// Keep a suggestion known to be correct. - fn keep(&mut self, text: String, cost: u32) { - if !text.split(' ').all(|part| self.d.can_suggest(part)) { - return; - } - self.seq += 1; - let seq = self.seq; - let e = self.found.entry(text).or_insert((cost, seq)); - if cost < e.0 { - *e = (cost, seq); - } - } - - fn count_within(&self, cost: u32) -> usize { - self.found.values().filter(|(c, _)| *c <= cost).count() - } -} - -/// The MAP group of each letter (its other members are its likely confusions). -fn alternatives(c: char, map: &[Vec]) -> Vec { - map.iter().filter(|g| g.contains(&c)).flat_map(|g| g.iter().copied()).filter(|&x| x != c).collect() -} - -/// The cost of changing letter `from` to its MAP relative `to`. Adding a missing accent is the -/// commonest slip (Portuguese typed without accents: `coracao`); an accent the writer did type -/// is likelier right, so taking one away costs more than a known confusion from the REP table -/// (`seje` → `seja` before `sejé`). -fn map_cost(from: char, to: char) -> u32 { - match (from.is_ascii(), to.is_ascii()) { - (true, false) => COST_ADD_DIACRITIC, - (false, true) => COST_DROP_DIACRITIC, - _ => COST_OTHER_MAP, - } -} - -/// Every way of changing up to [`MAX_MAP_CHANGES`] letters to a MAP relative, with its cost. -fn map_variants(chars: &[char], alts: &[Vec], i: usize, changes: usize, cost: u32, buf: &mut Vec, out: &mut Vec<(String, u32)>) { - if out.len() >= MAX_MAP_VARIANTS { - return; - } - let Some(&c) = chars.get(i) else { - if changes > 0 { - out.push((buf.iter().collect(), cost)); - } - return; - }; - buf.push(c); - map_variants(chars, alts, i + 1, changes, cost, buf, out); - buf.pop(); - if changes < MAX_MAP_CHANGES { - for &a in alts.get(i).map(Vec::as_slice).unwrap_or_default() { - buf.push(a); - map_variants(chars, alts, i + 1, changes + 1, cost + map_cost(c, a), buf, out); - buf.pop(); - } - } -} - -/// A letter's MAP representative (the first of its group), lower case: `ã` → `a`, `ç` → `c`. -fn fold(c: char, map: &[Vec]) -> char { - let l = c.to_lowercase().next().unwrap_or(c); - map.iter().find(|g| g.contains(&l)).and_then(|g| g.first().copied()).unwrap_or(l) -} - -/// Optimal string alignment distance where a MAP slip (`a`/`á`, `c`/`ç`) costs 1 and any other -/// edit 2; `limit + 1` once it is certainly above `limit`. -fn weighted_distance(a: &[char], b: &[char], related: &dyn Fn(char, char) -> bool, limit: u32) -> u32 { - let m = b.len(); - let over = limit + 1; - let mut prev2 = vec![0u32; m + 1]; - let mut prev: Vec = (0..=m as u32).map(|j| j * 2).collect(); - let mut cur = vec![0u32; m + 1]; - for (i, &ai) in a.iter().enumerate() { - if let Some(c) = cur.get_mut(0) { - *c = (i as u32 + 1) * 2; - } - let mut row_min = u32::MAX; - for (j, &bj) in b.iter().enumerate() { - let sub = if ai == bj { - 0 - } else if related(ai, bj) { - 1 - } else { - 2 - }; - let at = |v: &[u32], k: usize| v.get(k).copied().unwrap_or(u32::MAX / 4); - let mut v = (at(&prev, j + 1) + 2).min(at(&cur, j) + 2).min(at(&prev, j) + sub); - if i > 0 && j > 0 && a.get(i - 1) == Some(&bj) && b.get(j - 1) == Some(&ai) { - v = v.min(at(&prev2, j - 1) + 2); - } - if let Some(c) = cur.get_mut(j + 1) { - *c = v; - } - row_min = row_min.min(v); - } - if m > 0 && row_min > limit { - return over; - } - std::mem::swap(&mut prev2, &mut prev); - std::mem::swap(&mut prev, &mut cur); - } - prev.get(m).copied().unwrap_or(over).min(over) -} +/// What Portuguese adds to its dictionary's tables. +const TUNING: Tuning = Tuning { + split: Split::Near { leading: LEADING_WORDS, trailing: TRAILING_WORDS }, + alphabet: "aeiousrntmdlcpvgqbzfhjxçãõáéíóúâêôkw", + ..Tuning::DEFAULT +}; /// Spelling suggestions for a Portuguese word, best first, at most `max`, in the word's letter case. pub fn suggest(word: &str, max: usize) -> Vec { @@ -199,177 +33,13 @@ pub fn suggest(word: &str, max: usize) -> Vec { /// [`suggest`] against a given dictionary. pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { - let word = word.trim(); - let n = word.chars().count(); - if n == 0 || n > MAX_LETTERS || max == 0 || d.is_empty() || word.contains(char::is_whitespace) { - return Vec::new(); - } - let case = casing(word); - let input = word.to_lowercase(); - let chars: Vec = input.chars().collect(); - let mut col = Collector { d, input: input.clone(), tested: HashSet::new(), found: HashMap::new(), seq: 0 }; - // 1. Letter case. - if d.contains(&input) && word != input { - col.keep(input.clone(), COST_CASE); - } else if d.contains(&capitalize(&input)) && word != capitalize(&input) { - col.keep(capitalize(&input), COST_CASE); - } - // 2. Accents and other MAP slips. - let map = d.map(); - let alts: Vec> = chars.iter().map(|&c| alternatives(c, map)).collect(); - let mut variants = Vec::new(); - map_variants(&chars, &alts, 0, 0, 0, &mut Vec::with_capacity(n), &mut variants); - variants.sort_by_key(|v| v.1); - for (v, cost) in &variants { - col.offer(v, *cost); - } - // 3. Common confusions. - for (from, to) in d.rep() { - if from.is_empty() { - continue; - } - for (i, _) in input.match_indices(from.as_str()) { - if let (Some(a), Some(b)) = (input.get(..i), input.get(i + from.len()..)) { - col.offer(&format!("{a}{to}{b}"), COST_REP); - } - } - } - // 4. A missing space: cheap after a short leading word or before a short trailing one, - // dearer between two longer words, never leaving a stray short half. - for (i, _) in input.char_indices().skip(1) { - let (Some(a), Some(b)) = (input.get(..i), input.get(i..)) else { continue }; - let long = |p: &str| p.chars().count() >= 3; - let cost = if (LEADING_WORDS.contains(&a) && long(b)) || (long(a) && TRAILING_WORDS.contains(&b)) { - COST_SPLIT - } else if long(a) && long(b) { - COST_SPLIT_RARE - } else { - continue; - }; - col.offer(&format!("{a} {b}"), cost); - } - // 5. One typo. - let mut letters: Vec = d.try_chars().chars().filter(|c| c.is_lowercase()).collect(); - if letters.is_empty() { - letters = "aeiousrntmdlcpvgqbzfhjxçãõáéíóúâêôkw".chars().collect(); - } - let s = |v: &[char]| v.iter().collect::(); - for i in 0..n.saturating_sub(1) { - if chars.get(i) != chars.get(i + 1) { - let mut v = chars.clone(); - v.swap(i, i + 1); - col.offer(&s(&v), COST_SWAP); - } - } - for i in 0..n { - let mut v = chars.clone(); - v.remove(i); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - for i in 0..=n { - for &c in &letters { - let mut v = chars.clone(); - v.insert(i, c); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - } - for i in 0..n { - for &c in &letters { - if chars.get(i) != Some(&c) { - let mut v = chars.clone(); - if let Some(x) = v.get_mut(i) { - *x = c; - } - col.offer(&s(&v), COST_MISTYPE); - } - } - } - // 6. Further off: scan the stems that start like the word. - if col.count_within(COST_MISTYPE) < max { - scan(&mut col, &chars); - } - let mut found: Vec<(String, (u32, usize))> = col.found.into_iter().collect(); - // Among equal costs, the change nearest the word's start is the likelier intent - // (`saúde` before `saudê`: both add an accent, but at the first difference). - let diff_at = |text: &str| chars.iter().zip(text.chars()).position(|(a, b)| *a != b).unwrap_or(n); - found.sort_by_key(|(text, (cost, seq))| (*cost, diff_at(text), *seq)); - let mut out: Vec = Vec::new(); - for (text, _) in found { - let t = match case { - Casing::Initial => capitalize(&text), - Casing::Upper if n > 1 => text.to_uppercase(), - _ => text, - }; - if t != word && !out.contains(&t) { - out.push(t); - } - if out.len() >= max { - break; - } - } - out -} - -/// Round 6: rank the stems starting with the word's first letter (or a MAP relative of it, either -/// case) by shared letter pairs, then keep the forms of the best within two typos. -fn scan(col: &mut Collector, chars: &[char]) { - let d = col.d; - let map = d.map(); - let Some(&first) = chars.first() else { return }; - let folded: Vec = chars.iter().map(|&c| fold(c, map)).collect(); - let pairs: HashSet<(char, char)> = std::iter::once(' ').chain(folded.iter().copied()).zip(folded.iter().copied()).collect(); - let mut firsts: Vec = std::iter::once(first).chain(alternatives(first, map)).collect(); - let upper: Vec = firsts.iter().flat_map(|c| c.to_uppercase()).collect(); - firsts.extend(upper); - firsts.sort_unstable(); - firsts.dedup(); - let n = folded.len(); - let mut best: Vec<(u32, &str)> = Vec::new(); - for f in firsts { - let prefix = f.to_string(); - for stem in d.stems_with_prefix(&prefix) { - let sf: Vec = stem.chars().map(|c| fold(c, map)).collect(); - let shared = std::iter::once(' ').chain(sf.iter().copied()).zip(sf.iter().copied()).filter(|p| pairs.contains(p)).count(); - if shared < 2 { - continue; - } - // Shared pairs, less the stem's letters the word doesn't explain, less a length gap - // beyond what an ending explains. - let extra = sf.len().saturating_sub(shared); - let gap = sf.len().abs_diff(n).saturating_sub(4); - let score = (shared * 4).saturating_sub(extra * 3 + gap * 2) as u32; - if score == 0 { - continue; - } - if best.len() < SCAN_STEMS { - best.push((score, stem)); - } else if let Some(min) = best.iter_mut().min_by_key(|b| b.0) - && score > min.0 - { - *min = (score, stem); - } - } - } - best.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(b.1))); - let related = |a: char, b: char| fold(a, map) == fold(b, map); - let limit = 4; - for (_, stem) in best { - for form in d.forms(stem) { - let lf: Vec = form.to_lowercase().chars().collect(); - if lf.len().abs_diff(chars.len()) > 2 { - continue; - } - let dist = weighted_distance(&lf, chars, &related, limit); - if dist <= limit && lf.as_slice() != chars { - col.keep(form, COST_SCAN + dist * 3); - } - } - } + crate::affix_suggest::suggest_in(d, word, max, &TUNING) } #[cfg(test)] mod tests { use super::*; + use crate::affix_suggest::{fold, weighted_distance}; fn has(word: &str, want: &str) { let s = suggest(word, 8); diff --git a/crates/proof/src/ru/suggest.rs b/crates/proof/src/ru/suggest.rs index 24eeb7642b..135f4152f0 100644 --- a/crates/proof/src/ru/suggest.rs +++ b/crates/proof/src/ru/suggest.rs @@ -1,30 +1,14 @@ -//! Russian spelling suggestions, best first. -//! -//! Russian Hunspell dictionaries carry no `MAP` or `REP` tables, so the letter groups and the -//! common confusions are defined here. Candidates come in rounds of rising cost, each checked -//! against the dictionary: -//! 1. letter case (`москва` → `Москва`); -//! 2. letter slips within a group ([`MAP`]): `е`/`ё` first, then unstressed vowels (`о`/`а`, -//! `е`/`и`/`я`), voiced and voiceless pairs (`з`/`с`, `д`/`т`, `б`/`п`…) and `ь`/`ъ`, up to -//! three letters changed (`превет` → `привет`, `сдесь` → `здесь`); -//! 3. the common confusions of [`REP`] (`жы` → `жи`, `чя` → `ча`, `тся`/`ться`, a dropped or -//! doubled consonant, `здраствуй` → `здравствуй`); -//! 4. a missing space (`незнаю` → `не знаю`, `вобщем` → `в общем`), cheaper when one half is a -//! short function word; -//! 5. one typo: a letter left out, added, swapped with the next or mistyped; -//! 6. when that finds fewer than asked for: the stems that start like the word and share the -//! most letter pairs with it, whose forms are ranked by an edit distance in which a slip within -//! a letter group costs half (two typos at most). -//! -//! The input is bounded (40 letters), and so is every round. +//! Russian spelling suggestions, best first: the shared ranking ([`crate::affix_suggest`]) with +//! Russian's own letter groups and confusions, since Russian Hunspell dictionaries carry no `MAP` +//! or `REP` tables (`е`/`ё` first, then unstressed vowels, voiced and voiceless pairs, `жы` → +//! `жи`, `тся`/`ться`, dropped or doubled consonants; up to three letters changed), and its rule +//! for a missing space (`незнаю` → `не знаю`, `вобщем` → `в общем`). -use std::collections::{HashMap, HashSet}; - -use crate::affix::{AffixDict, Casing, capitalize, casing}; +use crate::affix::AffixDict; +use crate::affix_suggest::{COST_OTHER_MAP, COST_SPLIT, COST_SPLIT_LONG, Split, Tuning}; /// Letters confused with each other: a slip inside a group is cheaper than another typo. const MAP: &[&str] = &["её", "оа", "еия", "зс", "дт", "бп", "вф", "гк", "жш", "ьъ", "еэ", "ыи"]; - /// Common confusions: a written sequence and what was meant. const REP: &[(&str, &str)] = &[ ("жы", "жи"), @@ -67,7 +51,6 @@ const REP: &[(&str, &str)] = &[ ("ц", "тс"), ("тс", "ц"), ]; - /// Short words often run into the next one (`не знаю`, `в общем`, `на верху`, `потому что`). const LEADING_WORDS: &[&str] = &[ "не", @@ -123,161 +106,42 @@ const PREPOSITIONS: &[&str] = &[ const TRAILING_WORDS: &[&str] = &["ли", "же", "бы", "ж", "б", "что"]; /// The Russian alphabet, when the dictionary has no `TRY` line. const ALPHABET: &str = "оеаинтсрвлкмдпуяыьгзбчйхжшюцщэфъё"; -const MAX_LETTERS: usize = 40; -const MAX_MAP_CHANGES: usize = 3; -const MAX_MAP_VARIANTS: usize = 4096; -/// Stems the scan of round 6 expands. -const SCAN_STEMS: usize = 48; -const COST_CASE: u32 = 0; +/// `е` written for `ё`, or the other way round, is barely a slip. const COST_YO: u32 = 3; -const COST_MAP: u32 = 7; -const COST_REP: u32 = 9; -const COST_SPLIT: u32 = 12; +/// A one- or two-letter preposition run into the next word (`вдоме` → `в доме`). const COST_SPLIT_PREPOSITION: u32 = 17; -const COST_SPLIT_RARE: u32 = 18; -const COST_SWAP: u32 = 14; -const COST_DROP_OR_ADD: u32 = 15; -const COST_MISTYPE: u32 = 16; -const COST_SCAN: u32 = 20; -struct Collector<'a> { - d: &'a AffixDict, - /// The word, lower case. - input: String, - tested: HashSet, - /// Suggestion → (cost, order found). - found: HashMap, - seq: usize, -} +/// What Russian adds to its dictionary's tables. +const TUNING: Tuning = + Tuning { split: Split::Custom(split), alphabet: ALPHABET, map: MAP, rep: REP, rep_cost: 9, max_map_changes: 3, map_cost, ..Tuning::DEFAULT }; -impl Collector<'_> { - /// Check a lower-case candidate (a word, or two separated by a space) and keep it if every - /// word is in the dictionary, as written or capitalized (names). - fn offer(&mut self, cand: &str, cost: u32) { - if cand.is_empty() || cand == self.input || !self.tested.insert(cand.to_string()) { - return; - } - let mut words = Vec::new(); - let mut cost = cost; - for part in cand.split(' ') { - if self.d.contains_suggestible(part) { - words.push(part.to_string()); - } else { - let c = capitalize(part); - if !self.d.contains_suggestible(&c) { - return; - } - words.push(c); - // A name ranks after a common word at the same distance. - cost += 1; - } - } - self.keep(words.join(" "), cost); - } - - /// Keep a suggestion known to be correct. - fn keep(&mut self, text: String, cost: u32) { - if !text.split(' ').all(|part| self.d.can_suggest(part)) { - return; - } - self.seq += 1; - let seq = self.seq; - let e = self.found.entry(text).or_insert((cost, seq)); - if cost < e.0 { - *e = (cost, seq); - } - } - - fn count_within(&self, cost: u32) -> usize { - self.found.values().filter(|(c, _)| *c <= cost).count() - } -} - -/// The other members of each [`MAP`] group holding `c`. -fn alternatives(c: char) -> Vec { - let mut out: Vec = MAP.iter().filter(|g| g.contains(c)).flat_map(|g| g.chars()).filter(|&x| x != c).collect(); - out.sort_unstable(); - out.dedup(); - out -} - -/// The cost of changing letter `from` to its group relative `to`: `е`/`ё` is the cheapest slip -/// (`ё` is optional in ordinary writing). +/// The cost of changing `from` to `to` within a letter group: `е`/`ё` cheapest. fn map_cost(from: char, to: char) -> u32 { - if matches!((from, to), ('е', 'ё') | ('ё', 'е')) { COST_YO } else { COST_MAP } + if matches!((from, to), ('е', 'ё') | ('ё', 'е')) { COST_YO } else { COST_OTHER_MAP } } -/// Every way of changing up to [`MAX_MAP_CHANGES`] letters to a group relative, with its cost. -fn map_variants(chars: &[char], alts: &[Vec], i: usize, changes: usize, cost: u32, buf: &mut Vec, out: &mut Vec<(String, u32)>) { - if out.len() >= MAX_MAP_VARIANTS { - return; +/// A missing space: cheap after a short leading word or before a short trailing one, dearer after +/// a short preposition or between two longer words, never leaving a stray short half. +fn split(a: &str, b: &str) -> Option { + let long = |p: &str| p.chars().count() >= 3; + // A preposition takes a noun, not a verb (`с генерировать` isn't a fix for + // `сгенерировать`); after one or two letters a split is likelier wrong than a typo. + let preposition = PREPOSITIONS.contains(&a); + if preposition && ["ть", "ться", "ти", "чь"].iter().any(|e| b.ends_with(e)) { + return None; } - let Some(&c) = chars.get(i) else { - if changes > 0 { - out.push((buf.iter().collect(), cost)); - } - return; - }; - buf.push(c); - map_variants(chars, alts, i + 1, changes, cost, buf, out); - buf.pop(); - if changes < MAX_MAP_CHANGES { - for &a in alts.get(i).map(Vec::as_slice).unwrap_or_default() { - buf.push(a); - map_variants(chars, alts, i + 1, changes + 1, cost + map_cost(c, a), buf, out); - buf.pop(); - } + if preposition && a.chars().count() <= 2 && LEADING_WORDS.contains(&a) && long(b) { + Some(COST_SPLIT_PREPOSITION) + } else if (LEADING_WORDS.contains(&a) && long(b)) || (long(a) && TRAILING_WORDS.contains(&b)) { + Some(COST_SPLIT) + } else if long(a) && long(b) { + Some(COST_SPLIT_LONG) + } else { + None } } -/// A letter's group representative (the first of its first group), lower case: `ё` → `е`, -/// `з` → `з`, `с` → `з`. -fn fold(c: char) -> char { - let l = c.to_lowercase().next().unwrap_or(c); - MAP.iter().find(|g| g.contains(l)).and_then(|g| g.chars().next()).unwrap_or(l) -} - -/// Optimal string alignment distance where a slip within a letter group costs 1 and any other -/// edit 2; `limit + 1` once it is certainly above `limit`. -fn weighted_distance(a: &[char], b: &[char], limit: u32) -> u32 { - let m = b.len(); - let over = limit + 1; - let mut prev2 = vec![0u32; m + 1]; - let mut prev: Vec = (0..=m as u32).map(|j| j * 2).collect(); - let mut cur = vec![0u32; m + 1]; - for (i, &ai) in a.iter().enumerate() { - if let Some(c) = cur.get_mut(0) { - *c = (i as u32 + 1) * 2; - } - let mut row_min = u32::MAX; - for (j, &bj) in b.iter().enumerate() { - let sub = if ai == bj { - 0 - } else if fold(ai) == fold(bj) { - 1 - } else { - 2 - }; - let at = |v: &[u32], k: usize| v.get(k).copied().unwrap_or(u32::MAX / 4); - let mut v = (at(&prev, j + 1) + 2).min(at(&cur, j) + 2).min(at(&prev, j) + sub); - if i > 0 && j > 0 && a.get(i - 1) == Some(&bj) && b.get(j - 1) == Some(&ai) { - v = v.min(at(&prev2, j - 1) + 2); - } - if let Some(c) = cur.get_mut(j + 1) { - *c = v; - } - row_min = row_min.min(v); - } - if m > 0 && row_min > limit { - return over; - } - std::mem::swap(&mut prev2, &mut prev); - std::mem::swap(&mut prev, &mut cur); - } - prev.get(m).copied().unwrap_or(over).min(over) -} - /// Spelling suggestions for a Russian word, best first, at most `max`, in the word's letter case. pub fn suggest(word: &str, max: usize) -> Vec { suggest_in(&super::dictionary(), word, max) @@ -285,171 +149,7 @@ pub fn suggest(word: &str, max: usize) -> Vec { /// [`suggest`] against a given dictionary. pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { - let word = word.trim(); - let n = word.chars().count(); - if n == 0 || n > MAX_LETTERS || max == 0 || d.is_empty() || word.contains(char::is_whitespace) { - return Vec::new(); - } - let case = casing(word); - let input = word.to_lowercase(); - let chars: Vec = input.chars().collect(); - let mut col = Collector { d, input: input.clone(), tested: HashSet::new(), found: HashMap::new(), seq: 0 }; - // 1. Letter case. - if d.contains(&input) && word != input { - col.keep(input.clone(), COST_CASE); - } else if d.contains(&capitalize(&input)) && word != capitalize(&input) { - col.keep(capitalize(&input), COST_CASE); - } - // 2. Slips within a letter group. - let alts: Vec> = chars.iter().map(|&c| alternatives(c)).collect(); - let mut variants = Vec::new(); - map_variants(&chars, &alts, 0, 0, 0, &mut Vec::with_capacity(n), &mut variants); - variants.sort_by_key(|v| v.1); - for (v, cost) in &variants { - col.offer(v, *cost); - } - // 3. Common confusions. - for (from, to) in REP { - for (i, _) in input.match_indices(from) { - if let (Some(a), Some(b)) = (input.get(..i), input.get(i + from.len()..)) { - col.offer(&format!("{a}{to}{b}"), COST_REP); - } - } - } - // 4. A missing space: cheap after a short leading word or before a short trailing one, - // dearer between two longer words, never leaving a stray short half. - for (i, _) in input.char_indices().skip(1) { - let (Some(a), Some(b)) = (input.get(..i), input.get(i..)) else { continue }; - let long = |p: &str| p.chars().count() >= 3; - // A preposition takes a noun, not a verb (`с генерировать` isn't a fix for - // `сгенерировать`); after one or two letters a split is likelier wrong than a typo. - let preposition = PREPOSITIONS.contains(&a); - if preposition && ["ть", "ться", "ти", "чь"].iter().any(|e| b.ends_with(e)) { - continue; - } - let cost = if preposition && a.chars().count() <= 2 && LEADING_WORDS.contains(&a) && long(b) { - COST_SPLIT_PREPOSITION - } else if (LEADING_WORDS.contains(&a) && long(b)) || (long(a) && TRAILING_WORDS.contains(&b)) { - COST_SPLIT - } else if long(a) && long(b) { - COST_SPLIT_RARE - } else { - continue; - }; - col.offer(&format!("{a} {b}"), cost); - } - // 5. One typo. - let mut letters: Vec = d.try_chars().chars().filter(|c| c.is_lowercase()).collect(); - if letters.is_empty() { - letters = ALPHABET.chars().collect(); - } - let s = |v: &[char]| v.iter().collect::(); - for i in 0..n.saturating_sub(1) { - if chars.get(i) != chars.get(i + 1) { - let mut v = chars.clone(); - v.swap(i, i + 1); - col.offer(&s(&v), COST_SWAP); - } - } - for i in 0..n { - let mut v = chars.clone(); - v.remove(i); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - for i in 0..=n { - for &c in &letters { - let mut v = chars.clone(); - v.insert(i, c); - col.offer(&s(&v), COST_DROP_OR_ADD); - } - } - for i in 0..n { - for &c in &letters { - if chars.get(i) != Some(&c) { - let mut v = chars.clone(); - if let Some(x) = v.get_mut(i) { - *x = c; - } - col.offer(&s(&v), COST_MISTYPE); - } - } - } - // 6. Further off: scan the stems that start like the word. - if col.count_within(COST_MISTYPE) < max { - scan(&mut col, &chars); - } - let mut found: Vec<(String, (u32, usize))> = col.found.into_iter().collect(); - found.sort_by_key(|(_, k)| *k); - let mut out: Vec = Vec::new(); - for (text, _) in found { - let t = match case { - Casing::Initial => capitalize(&text), - Casing::Upper if n > 1 => text.to_uppercase(), - _ => text, - }; - if t != word && !out.contains(&t) { - out.push(t); - } - if out.len() >= max { - break; - } - } - out -} - -/// Round 6: rank the stems starting with the word's first letter (or a group relative of it, -/// either case) by shared letter pairs, then keep the forms of the best within two typos. -fn scan(col: &mut Collector, chars: &[char]) { - let d = col.d; - let Some(&first) = chars.first() else { return }; - let folded: Vec = chars.iter().map(|&c| fold(c)).collect(); - let pairs: HashSet<(char, char)> = std::iter::once(' ').chain(folded.iter().copied()).zip(folded.iter().copied()).collect(); - let mut firsts: Vec = std::iter::once(first).chain(alternatives(first)).collect(); - let upper: Vec = firsts.iter().flat_map(|c| c.to_uppercase()).collect(); - firsts.extend(upper); - firsts.sort_unstable(); - firsts.dedup(); - let n = folded.len(); - let mut best: Vec<(u32, &str)> = Vec::new(); - for f in firsts { - let prefix = f.to_string(); - for stem in d.stems_with_prefix(&prefix) { - let sf: Vec = stem.chars().map(fold).collect(); - let shared = std::iter::once(' ').chain(sf.iter().copied()).zip(sf.iter().copied()).filter(|p| pairs.contains(p)).count(); - if shared < 2 { - continue; - } - // Shared pairs, less the stem's letters the word doesn't explain, less a length gap - // beyond what an ending explains. - let extra = sf.len().saturating_sub(shared); - let gap = sf.len().abs_diff(n).saturating_sub(4); - let score = (shared * 4).saturating_sub(extra * 3 + gap * 2) as u32; - if score == 0 { - continue; - } - if best.len() < SCAN_STEMS { - best.push((score, stem)); - } else if let Some(min) = best.iter_mut().min_by_key(|b| b.0) - && score > min.0 - { - *min = (score, stem); - } - } - } - best.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(b.1))); - let limit = 4; - for (_, stem) in best { - for form in d.forms(stem) { - let lf: Vec = form.to_lowercase().chars().collect(); - if lf.len().abs_diff(chars.len()) > 2 { - continue; - } - let dist = weighted_distance(&lf, chars, limit); - if dist <= limit && lf.as_slice() != chars { - col.keep(form, COST_SCAN + dist * 3); - } - } - } + crate::affix_suggest::suggest_in(d, word, max, &TUNING) } #[cfg(test)] @@ -461,6 +161,20 @@ mod tests { assert!(s.iter().any(|x| x == want), "{word} → {s:?}, want {want}"); } + /// Russian's own costs: `е`/`ё` is the cheapest slip, so a `ё` and one more group slip + /// (made-up `нёснаю`) beat a missing space; a REP confusion beats a `ё` and a group slip; + /// a preposition is never split off a verb, and after a one-letter one a typo is likelier. + #[test] + fn russian_costs_rank_its_slips() { + let dict = |words: &[&str]| AffixDict::from_hunspell("", &format!("{}\n{}\n", words.len(), words.join("\n")), &[]).unwrap(); + assert_eq!(suggest_in(&dict(&["не", "знаю", "нёснаю"]), "незнаю", 1), ["нёснаю"]); + assert_eq!(suggest_in(&dict(&["еча", "ёчи"]), "ечя", 1), ["еча"]); + assert!(!suggest_in(&dict(&["с", "генерировать"]), "сгенерировать", 8).contains(&"с генерировать".into())); + let s = suggest_in(&dict(&["в", "доме"]), "вдоме", 4); + assert_eq!(s.first().map(String::as_str), Some("доме"), "{s:?}"); + assert!(s.contains(&"в доме".into()), "{s:?}"); + } + /// A word the dictionary marks NOSUGGEST (a vulgarism, a rare form) is correct but never /// offered, in any round or letter case. Russian's MAP and REP tables are its own, so the /// shared Latin-script check doesn't reach it. @@ -518,12 +232,15 @@ mod tests { #[test] fn distance_weighs_letter_groups_half() { + use crate::affix_suggest::{fold, weighted_distance}; + let map: Vec> = MAP.iter().map(|g| g.chars().collect()).collect(); + let related = |a: char, b: char| fold(a, &map) == fold(b, &map); let c = |s: &str| s.chars().collect::>(); - assert_eq!(weighted_distance(&c("сдесь"), &c("здесь"), 9), 1); - assert_eq!(weighted_distance(&c("ёлка"), &c("елка"), 9), 1); - assert_eq!(weighted_distance(&c("дом"), &c("дмо"), 9), 2); - assert_eq!(weighted_distance(&c("дом"), &c("домами"), 9), 6); - assert_eq!(weighted_distance(&c("дом"), &c("книга"), 2), 3); + assert_eq!(weighted_distance(&c("сдесь"), &c("здесь"), &related, 9), 1); + assert_eq!(weighted_distance(&c("ёлка"), &c("елка"), &related, 9), 1); + assert_eq!(weighted_distance(&c("дом"), &c("дмо"), &related, 9), 2); + assert_eq!(weighted_distance(&c("дом"), &c("домами"), &related, 9), 6); + assert_eq!(weighted_distance(&c("дом"), &c("книга"), &related, 2), 3); } #[test] From 9a8f4669fa9485853db9eadc668e05eb2f4dda2a Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 19:55:43 -0100 Subject: [PATCH 006/308] Proofing: one language table instead of nine lists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lib.rs named every dictionary language in nine places: reloading for a new folder, the installed list, load errors, the missing-dictionary hint, the loaded check, the spelling message, spelling, suggestions and grammar. The lists had drifted. dictionary_load_error had no Russian or Czech entry, so a Russian or Czech dictionary that failed to load showed no reason in File › Options › Proofing, though the status bar, which reads another list, said why. LANGUAGES holds one row per language: its dictionary slot, its spelling message, and its spelling, suggestion and grammar functions. The nine functions read the row, so a language now joins with its module, its ProofLang and one row. The per-language reload_dictionary and installed_path wrappers had no callers left, inside the crate or out, and are gone. Tests: every_dictionary_language_reports_its_load_error, which fails for Russian on the old code, and the_language_table_covers_the_proofed_languages (each ProofLang::PROOFED language but English has exactly one row). Proof 285, engine 927 and layout 501 (lib). --- crates/proof/src/cs/mod.rs | 14 +- crates/proof/src/es/mod.rs | 23 +--- crates/proof/src/hr/mod.rs | 14 +- crates/proof/src/id/mod.rs | 14 +- crates/proof/src/it/mod.rs | 14 +- crates/proof/src/lib.rs | 274 ++++++++++++++++++++++--------------- crates/proof/src/pl/mod.rs | 14 +- crates/proof/src/pt/mod.rs | 14 +- crates/proof/src/ru/mod.rs | 14 +- crates/proof/src/slot.rs | 31 +++++ crates/proof/src/tr/mod.rs | 13 +- 11 files changed, 208 insertions(+), 231 deletions(-) diff --git a/crates/proof/src/cs/mod.rs b/crates/proof/src/cs/mod.rs index 55e21aef6d..1d72dc913f 100644 --- a/crates/proof/src/cs/mod.rs +++ b/crates/proof/src/cs/mod.rs @@ -18,7 +18,7 @@ pub use suggest::suggest; const NAMES: &[&str] = &["cs_CZ", "cs-CZ", "cs"]; /// The Czech dictionary, found on first use (see [`DictSlot`]). -static DICT: DictSlot = DictSlot::new(NAMES, "Czech"); +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Czech"); /// The Czech dictionary: the installed Hunspell `cs_CZ` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Czech spelling is off @@ -33,18 +33,6 @@ pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { DICT.install(aff, dic) } -/// Forget the loaded dictionary, so the next check looks for an installed one again (after -/// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). -pub fn reload_dictionary() { - DICT.reload(); -} - -/// The `.dic` file of the installed Czech dictionary (File › Options › Proofing shows it); -/// `None` when there is none, or when one given with [`install_dictionary`] is in use. -pub fn installed_path() -> Option { - DICT.installed_path() -} - /// Is a Czech dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { DICT.has_dictionary() diff --git a/crates/proof/src/es/mod.rs b/crates/proof/src/es/mod.rs index 253642c83b..e16e9d6bdc 100644 --- a/crates/proof/src/es/mod.rs +++ b/crates/proof/src/es/mod.rs @@ -25,10 +25,10 @@ use crate::slot::DictSlot; pub use suggest::suggest; /// The dictionary of each variety, found on first use (see [`DictSlot`]). -static SPAIN_DICT: DictSlot = DictSlot::new(SPAIN, "Spanish (Spain)"); -static MEXICO_DICT: DictSlot = DictSlot::new(MEXICO, "Spanish (Mexico)"); -static LATIN_AMERICA_DICT: DictSlot = DictSlot::new(LATIN_AMERICA, "Spanish (Latin America)"); -static URUGUAY_DICT: DictSlot = DictSlot::new(URUGUAY, "Spanish (Uruguay)"); +pub(crate) static SPAIN_DICT: DictSlot = DictSlot::new(SPAIN, "Spanish (Spain)"); +pub(crate) static MEXICO_DICT: DictSlot = DictSlot::new(MEXICO, "Spanish (Mexico)"); +pub(crate) static LATIN_AMERICA_DICT: DictSlot = DictSlot::new(LATIN_AMERICA, "Spanish (Latin America)"); +pub(crate) static URUGUAY_DICT: DictSlot = DictSlot::new(URUGUAY, "Spanish (Uruguay)"); /// File names of a Spanish Hunspell dictionary for Spain, most specific first (`es_ANY` is the /// generic one RLA-ES publishes). @@ -72,21 +72,6 @@ pub fn install_dictionary(lang: ProofLang, aff: &str, dic: &str) -> Result<(), S slot(lang).install(aff, dic) } -/// Forget every variety's loaded dictionary, so the next check looks for an installed one again -/// (after [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). -pub fn reload_dictionary() { - for slot in [&SPAIN_DICT, &MEXICO_DICT, &LATIN_AMERICA_DICT, &URUGUAY_DICT] { - slot.reload(); - } -} - -/// The `.dic` file of the Hunspell dictionary `lang`'s variety finds in the dictionaries folder -/// or the system locations now (File › Options › Proofing shows it); `None` when there is none, -/// or when one given with [`install_dictionary`] is in use. -pub fn installed_path(lang: ProofLang) -> Option { - slot(lang).installed_path() -} - /// Is a dictionary available for `lang`'s variety (spelling is checked)? pub fn has_dictionary(lang: ProofLang) -> bool { slot(lang).has_dictionary() diff --git a/crates/proof/src/hr/mod.rs b/crates/proof/src/hr/mod.rs index 6372a47d27..036b723504 100644 --- a/crates/proof/src/hr/mod.rs +++ b/crates/proof/src/hr/mod.rs @@ -20,7 +20,7 @@ pub use suggest::suggest; const NAMES: &[&str] = &["hr_HR", "hr-HR", "hr"]; /// The Croatian dictionary, found on first use (see [`DictSlot`]). -static DICT: DictSlot = DictSlot::new(NAMES, "Croatian"); +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Croatian"); /// The Croatian dictionary: the installed Hunspell `hr_HR` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Croatian spelling is off @@ -35,18 +35,6 @@ pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { DICT.install(aff, dic) } -/// Forget the loaded dictionary, so the next check looks for an installed one again (after -/// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). -pub fn reload_dictionary() { - DICT.reload(); -} - -/// The `.dic` file of the installed Croatian dictionary (File › Options › Proofing shows it); -/// `None` when there is none, or when one given with [`install_dictionary`] is in use. -pub fn installed_path() -> Option { - DICT.installed_path() -} - /// Is a Croatian dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { DICT.has_dictionary() diff --git a/crates/proof/src/id/mod.rs b/crates/proof/src/id/mod.rs index 65b820dfc5..f1e44df452 100644 --- a/crates/proof/src/id/mod.rs +++ b/crates/proof/src/id/mod.rs @@ -21,7 +21,7 @@ pub use suggest::suggest; const NAMES: &[&str] = &["id_ID", "id-ID", "id", "in_ID"]; /// The Indonesian dictionary, found on first use (see [`DictSlot`]). -static DICT: DictSlot = DictSlot::new(NAMES, "Indonesian"); +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Indonesian"); /// The Indonesian dictionary: the installed Hunspell `id_ID` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Indonesian spelling is off @@ -36,18 +36,6 @@ pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { DICT.install(aff, dic) } -/// Forget the loaded dictionary, so the next check looks for an installed one again (after -/// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). -pub fn reload_dictionary() { - DICT.reload(); -} - -/// The `.dic` file of the installed Indonesian dictionary (File › Options › Proofing shows it); -/// `None` when there is none, or when one given with [`install_dictionary`] is in use. -pub fn installed_path() -> Option { - DICT.installed_path() -} - /// Is an Indonesian dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { DICT.has_dictionary() diff --git a/crates/proof/src/it/mod.rs b/crates/proof/src/it/mod.rs index 168883e174..1e606ad749 100644 --- a/crates/proof/src/it/mod.rs +++ b/crates/proof/src/it/mod.rs @@ -29,7 +29,7 @@ pub use suggest::suggest; const NAMES: &[&str] = &["it_IT", "it_CH", "it-IT", "it"]; /// The Italian dictionary, found on first use (see [`DictSlot`]). -static DICT: DictSlot = DictSlot::new(NAMES, "Italian"); +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Italian"); /// The Italian dictionary: the installed Hunspell `it_IT` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Italian spelling is off @@ -44,18 +44,6 @@ pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { DICT.install(aff, dic) } -/// Forget the loaded dictionary, so the next check looks for an installed one again (after -/// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). -pub fn reload_dictionary() { - DICT.reload(); -} - -/// The `.dic` file of the installed Italian dictionary (File › Options › Proofing shows it); -/// `None` when there is none, or when one given with [`install_dictionary`] is in use. -pub fn installed_path() -> Option { - DICT.installed_path() -} - /// Is an Italian dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { DICT.has_dictionary() diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 4bf4da78b1..0c51e2362f 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -159,57 +159,150 @@ pub fn ignore_word(w: &str) { } } +/// A language proofed against an installed Hunspell dictionary: all the functions below need to +/// know about it, so a language joins with one row in [`LANGUAGES`]. +struct Language { + lang: ProofLang, + slot: &'static dyn slot::Slot, + /// The spelling mark's message, in the language. + message: &'static str, + is_correct: fn(&str, ProofLang, &Options) -> bool, + suggest: fn(&str, usize, ProofLang) -> Vec, + /// Grammar issues; the flag says whether the first word must have a capital (not in a list + /// item). + grammar: fn(&str, ProofLang, bool) -> Vec, +} + +/// Every language proofed against a Hunspell dictionary, in the order File › Options › Proofing +/// lists them. English has a built-in word list and is handled apart. +static LANGUAGES: [Language; 12] = [ + Language { + lang: ProofLang::Pl, + slot: &pl::DICT, + message: "Możliwy błąd pisowni", + is_correct: |w, _, o| pl::is_correct_with_options(w, o), + suggest: |w, n, _| pl::suggest(w, n), + grammar: |t, _, c| pl::grammar::check_with(t, c), + }, + Language { + lang: ProofLang::Pt, + slot: &pt::DICT, + message: "Possível erro de ortografia", + is_correct: |w, _, o| pt::is_correct_with_options(w, o), + suggest: |w, n, _| pt::suggest(w, n), + grammar: |t, _, c| pt::grammar::check_with(t, c), + }, + Language { + lang: ProofLang::It, + slot: &it::DICT, + message: "Possibile errore di ortografia", + is_correct: |w, _, o| it::is_correct_with_options(w, o), + suggest: |w, n, _| it::suggest(w, n), + grammar: |t, _, c| it::grammar::check_with(t, c), + }, + Language { + lang: ProofLang::Hr, + slot: &hr::DICT, + message: "Moguća pravopisna pogreška", + is_correct: |w, _, o| hr::is_correct_with_options(w, o), + suggest: |w, n, _| hr::suggest(w, n), + grammar: |t, _, c| hr::grammar::check_with(t, c), + }, + Language { + lang: ProofLang::Tr, + slot: &tr::DICT, + message: "Olası yazım hatası", + is_correct: |w, _, o| tr::is_correct_with_options(w, o), + suggest: |w, n, _| tr::suggest(w, n), + grammar: |t, _, c| tr::grammar::check_with(t, c), + }, + Language { + lang: ProofLang::Ru, + slot: &ru::DICT, + message: "Возможна орфографическая ошибка", + is_correct: |w, _, o| ru::is_correct_with_options(w, o), + suggest: |w, n, _| ru::suggest(w, n), + grammar: |t, _, c| ru::grammar::check_with(t, c), + }, + // Czech, Indonesian and Spanish have no sentence-capital rule, so a list item needs no + // exception. + Language { + lang: ProofLang::Cs, + slot: &cs::DICT, + message: "Možná pravopisná chyba", + is_correct: |w, _, o| cs::is_correct_with_options(w, o), + suggest: |w, n, _| cs::suggest(w, n), + grammar: |t, _, _| cs::grammar::check(t), + }, + Language { + lang: ProofLang::Id, + slot: &id::DICT, + message: "Kemungkinan salah eja", + is_correct: |w, _, o| id::is_correct_with_options(w, o), + suggest: |w, n, _| id::suggest(w, n), + grammar: |t, _, _| id::grammar::check(t), + }, + Language { + lang: ProofLang::Es, + slot: &es::SPAIN_DICT, + message: "Posible error ortográfico", + is_correct: es::is_correct_with_options, + suggest: es::suggest, + grammar: |t, l, _| es::grammar::check(t, l), + }, + Language { + lang: ProofLang::EsMx, + slot: &es::MEXICO_DICT, + message: "Posible error ortográfico", + is_correct: es::is_correct_with_options, + suggest: es::suggest, + grammar: |t, l, _| es::grammar::check(t, l), + }, + Language { + lang: ProofLang::Es419, + slot: &es::LATIN_AMERICA_DICT, + message: "Posible error ortográfico", + is_correct: es::is_correct_with_options, + suggest: es::suggest, + grammar: |t, l, _| es::grammar::check(t, l), + }, + Language { + lang: ProofLang::EsUy, + slot: &es::URUGUAY_DICT, + message: "Posible error ortográfico", + is_correct: es::is_correct_with_options, + suggest: es::suggest, + grammar: |t, l, _| es::grammar::check(t, l), + }, +]; + +/// `lang`'s row in [`LANGUAGES`]; `None` for English and the languages WordCraft doesn't proof. +fn language(lang: ProofLang) -> Option<&'static Language> { + LANGUAGES.iter().find(|l| l.lang == lang) +} + /// Search `dir` for Hunspell dictionaries before the system locations (File › Options › /// Proofing › Dictionaries folder, #1063); `None` clears it. The dictionaries load again from /// the new search path, and text is checked again. pub fn set_dictionaries_folder(dir: Option) { hunspell::set_dictionary_dir(dir); - pl::reload_dictionary(); - pt::reload_dictionary(); - it::reload_dictionary(); - hr::reload_dictionary(); - tr::reload_dictionary(); - ru::reload_dictionary(); - cs::reload_dictionary(); - id::reload_dictionary(); - es::reload_dictionary(); + for l in &LANGUAGES { + l.slot.reload(); + } DICTIONARY_GENERATION.fetch_add(1, Ordering::Relaxed); } /// Each language that reads an installed Hunspell dictionary, with the `.dic` file found for it /// now (`None`: not installed, so the language has no spelling marks). pub fn installed_dictionaries() -> Vec<(ProofLang, Option)> { - vec![ - (ProofLang::Pl, pl::installed_path()), - (ProofLang::Pt, pt::installed_path()), - (ProofLang::It, it::installed_path()), - (ProofLang::Hr, hr::installed_path()), - (ProofLang::Tr, tr::installed_path()), - (ProofLang::Ru, ru::installed_path()), - (ProofLang::Cs, cs::installed_path()), - (ProofLang::Id, id::installed_path()), - (ProofLang::Es, es::installed_path(ProofLang::Es)), - (ProofLang::EsMx, es::installed_path(ProofLang::EsMx)), - (ProofLang::Es419, es::installed_path(ProofLang::Es419)), - (ProofLang::EsUy, es::installed_path(ProofLang::EsUy)), - ] + LANGUAGES.iter().map(|l| (l.lang, l.slot.installed_path())).collect() } /// Why the dictionary found for `lang` failed to load (unsupported encoding, too large, a parse /// error), if it was read and failed. Doesn't read anything itself: a dictionary nobody has /// checked yet reports `None` (#2830). pub fn dictionary_load_error(lang: ProofLang) -> Option { - let key = match lang { - ProofLang::Pl => "pl_PL", - ProofLang::Pt => "pt_BR", - ProofLang::It => "it_IT", - ProofLang::Hr => "hr_HR", - ProofLang::Tr => "tr_TR", - ProofLang::Id => "id_ID", - ProofLang::Es | ProofLang::EsMx | ProofLang::Es419 | ProofLang::EsUy => es::dictionary_name(lang), - _ => return None, - }; - hunspell::load_error(key).map(|(_, reason)| reason) + hunspell::load_error(language(lang)?.slot.name()).map(|(_, reason)| reason) } /// A number that changes whenever a word is added to or removed from the user dictionary, or the @@ -440,36 +533,13 @@ pub fn unreadable_dictionary(lang: Option<&str>) -> Option<(&'static str, std::p /// Did the Hunspell dictionary of `lang` load? `None` for languages that use none (English). fn dictionary_loaded(lang: ProofLang) -> Option { - Some(match lang { - ProofLang::Pl => pl::has_dictionary(), - ProofLang::Pt => pt::has_dictionary(), - ProofLang::It => it::has_dictionary(), - ProofLang::Hr => hr::has_dictionary(), - ProofLang::Tr => tr::has_dictionary(), - ProofLang::Ru => ru::has_dictionary(), - ProofLang::Cs => cs::has_dictionary(), - ProofLang::Id => id::has_dictionary(), - l if l.is_spanish() => es::has_dictionary(l), - _ => return None, - }) + language(lang).map(|l| l.slot.has_dictionary()) } /// The Hunspell name `lang` needs when `installed` is false (the first name its module looks for; /// a Spanish variety's is its own: `es_ES`, `es_MX`, `es_419`, `es_UY`). fn dictionary_needed(lang: ProofLang, installed: bool) -> Option<&'static str> { - match lang { - _ if installed => None, - ProofLang::Pl => Some("pl_PL"), - ProofLang::Pt => Some("pt_BR"), - ProofLang::It => Some("it_IT"), - ProofLang::Hr => Some("hr_HR"), - ProofLang::Tr => Some("tr_TR"), - ProofLang::Ru => Some("ru_RU"), - ProofLang::Cs => Some("cs_CZ"), - ProofLang::Id => Some("id_ID"), - l if l.is_spanish() => Some(es::dictionary_name(l)), - _ => None, - } + language(lang).filter(|_| !installed).map(|l| l.slot.name()) } /// Is `word` spelled correctly in the run's proofing language? A language without a dictionary @@ -739,18 +809,7 @@ fn spelling_issue(text: &str, a: usize, b: usize, lang: ProofLang, opts: &Option if (opts.ignore_internet && internet) || (lang.is_english() && vi::is_vietnamese_word(w)) || is_correct_in_with(w, lang, opts) { return None; } - let message = match lang { - ProofLang::Pl => "Możliwy błąd pisowni", - ProofLang::Hr => "Moguća pravopisna pogreška", - ProofLang::Pt => "Possível erro de ortografia", - ProofLang::It => "Possibile errore di ortografia", - ProofLang::Tr => "Olası yazım hatası", - ProofLang::Ru => "Возможна орфографическая ошибка", - ProofLang::Cs => "Možná pravopisná chyba", - ProofLang::Id => "Kemungkinan salah eja", - ProofLang::Es | ProofLang::EsMx | ProofLang::Es419 | ProofLang::EsUy => "Posible error ortográfico", - _ => "Possible spelling mistake", - }; + let message = language(lang).map_or("Possible spelling mistake", |l| l.message); Some(Issue { start: a, end: b, kind: IssueKind::Spelling, message: message.into(), suggestions: Vec::new(), lang }) } @@ -774,40 +833,21 @@ fn composed(word: &str) -> std::borrow::Cow<'_, str> { /// checked as its composed form ([`composed`]). pub fn is_correct_in_with(word: &str, lang: ProofLang, opts: &Options) -> bool { let word = &*composed(word); - match lang { - ProofLang::En | ProofLang::EnGb | ProofLang::EnCa => is_correct_english(word, lang, opts), - ProofLang::Pl => accepted_by_options(word, opts) || pl::is_correct_with_options(word, opts), - ProofLang::Hr => accepted_by_options(word, opts) || hr::is_correct_with_options(word, opts), - ProofLang::Pt => accepted_by_options(word, opts) || pt::is_correct_with_options(word, opts), - ProofLang::It => accepted_by_options(word, opts) || it::is_correct_with_options(word, opts), - ProofLang::Tr => accepted_by_options(word, opts) || tr::is_correct_with_options(word, opts), - ProofLang::Ru => accepted_by_options(word, opts) || ru::is_correct_with_options(word, opts), - ProofLang::Es | ProofLang::EsMx | ProofLang::Es419 | ProofLang::EsUy => { - accepted_by_options(word, opts) || es::is_correct_with_options(word, lang, opts) - } - ProofLang::Cs => accepted_by_options(word, opts) || cs::is_correct_with_options(word, opts), - ProofLang::Id => accepted_by_options(word, opts) || id::is_correct_with_options(word, opts), - ProofLang::Bg | ProofLang::De | ProofLang::Other => true, + if lang.is_english() { + return is_correct_english(word, lang, opts); } + // A language WordCraft doesn't proof (Bulgarian, German, any other) has no spelling marks. + language(lang).is_none_or(|l| accepted_by_options(word, opts) || (l.is_correct)(word, lang, opts)) } /// Spelling suggestions for a word misspelled in `lang` (best first, at most `max`), composed /// ([`composed`]) like the dictionaries' words. pub fn suggest_in(word: &str, max: usize, lang: ProofLang) -> Vec { let word = &*composed(word); - match lang { - ProofLang::En | ProofLang::EnGb | ProofLang::EnCa => suggest_english(word, max, lang), - ProofLang::Pl => pl::suggest(word, max), - ProofLang::Hr => hr::suggest(word, max), - ProofLang::Pt => pt::suggest(word, max), - ProofLang::It => it::suggest(word, max), - ProofLang::Tr => tr::suggest(word, max), - ProofLang::Ru => ru::suggest(word, max), - ProofLang::Cs => cs::suggest(word, max), - ProofLang::Id => id::suggest(word, max), - ProofLang::Es | ProofLang::EsMx | ProofLang::Es419 | ProofLang::EsUy => es::suggest(word, max, lang), - ProofLang::Bg | ProofLang::De | ProofLang::Other => Vec::new(), + if lang.is_english() { + return suggest_english(word, max, lang); } + language(lang).map_or_else(Vec::new, |l| (l.suggest)(word, max, lang)) } /// Grammar issues in a paragraph of text in `lang`: the English rules, the Polish ones @@ -824,18 +864,7 @@ fn grammar_in(text: &str, lang: ProofLang, capital_first: bool) -> Vec { match lang { ProofLang::En => grammar_en(text, capital_first), ProofLang::EnGb | ProofLang::EnCa => grammar_en(text, capital_first).into_iter().map(|i| Issue { lang, ..i }).collect(), - ProofLang::Pl => pl::grammar::check_with(text, capital_first), - ProofLang::Hr => hr::grammar::check_with(text, capital_first), - ProofLang::Pt => pt::grammar::check_with(text, capital_first), - ProofLang::It => it::grammar::check_with(text, capital_first), - ProofLang::Tr => tr::grammar::check_with(text, capital_first), - ProofLang::Ru => ru::grammar::check_with(text, capital_first), - // Czech has no sentence-capital rule, so a list item needs no exception. - ProofLang::Cs => cs::grammar::check(text), - ProofLang::Id => id::grammar::check(text), - // Spanish has no sentence-capital rule either. - ProofLang::Es | ProofLang::EsMx | ProofLang::Es419 | ProofLang::EsUy => es::grammar::check(text, lang), - ProofLang::Bg | ProofLang::De | ProofLang::Other => Vec::new(), + _ => language(lang).map_or_else(Vec::new, |l| (l.grammar)(text, lang, capital_first)), } } @@ -1133,6 +1162,33 @@ mod tests { assert!(is_correct_in("дом", ProofLang::Ru) && !is_correct_in("дум", ProofLang::Ru)); } + /// A dictionary that fails to load says why in File › Options › Proofing in every language + /// that reads one; Russian and Czech were missing from the list that named their dictionaries + /// there. + #[test] + fn every_dictionary_language_reports_its_load_error() { + let _guard = DICTIONARY_TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + for (lang, _) in installed_dictionaries() { + let name = dictionary_needed(lang, false).unwrap(); + let reason = format!("zq unreadable {name}"); + let _ = hunspell::note_load::<()>(name, std::path::Path::new("zq.dic"), Err(reason.clone())); + assert_eq!(dictionary_load_error(lang), Some(reason), "{lang:?}"); + hunspell::clear_load_error(name); + } + } + + /// Every proofed language other than English has a row in the language table, and only those. + #[test] + fn the_language_table_covers_the_proofed_languages() { + for l in ProofLang::PROOFED { + assert_eq!(language(l).is_some(), !l.is_english(), "{l:?}"); + } + for (i, row) in LANGUAGES.iter().enumerate() { + assert!(row.lang.is_proofed(), "{:?}", row.lang); + assert!(LANGUAGES.iter().skip(i + 1).all(|other| other.lang != row.lang), "{:?} twice", row.lang); + } + } + /// #1741: a list item may be a fragment, so its first word needs no capital in any language /// with the rule; a plain paragraph's does, and later sentences in the item are still checked. #[test] diff --git a/crates/proof/src/pl/mod.rs b/crates/proof/src/pl/mod.rs index 4d79986d98..30d6d966fe 100644 --- a/crates/proof/src/pl/mod.rs +++ b/crates/proof/src/pl/mod.rs @@ -28,7 +28,7 @@ pub use suggest::suggest; const NAMES: &[&str] = &["pl_PL", "pl-PL", "pl"]; /// The Polish dictionary, found on first use (see [`DictSlot`]). -static DICT: DictSlot = DictSlot::new(NAMES, "Polish"); +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Polish"); /// The Polish dictionary: the installed Hunspell `pl_PL` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Polish spelling is off @@ -43,18 +43,6 @@ pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { DICT.install(aff, dic) } -/// Forget the loaded dictionary, so the next check looks for an installed one again (after -/// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). -pub fn reload_dictionary() { - DICT.reload(); -} - -/// The `.dic` file of the installed Polish dictionary (File › Options › Proofing shows it); -/// `None` when there is none, or when one given with [`install_dictionary`] is in use. -pub fn installed_path() -> Option { - DICT.installed_path() -} - /// Is a Polish dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { DICT.has_dictionary() diff --git a/crates/proof/src/pt/mod.rs b/crates/proof/src/pt/mod.rs index 860cf45796..0110a8c183 100644 --- a/crates/proof/src/pt/mod.rs +++ b/crates/proof/src/pt/mod.rs @@ -27,7 +27,7 @@ pub use suggest::suggest; const NAMES: &[&str] = &["pt_BR", "pt-BR", "pt"]; /// The Portuguese dictionary, found on first use (see [`DictSlot`]). -static DICT: DictSlot = DictSlot::new(NAMES, "Portuguese"); +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Portuguese"); /// The Portuguese dictionary: the installed Hunspell `pt_BR` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Portuguese spelling is off @@ -42,18 +42,6 @@ pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { DICT.install(aff, dic) } -/// Forget the loaded dictionary, so the next check looks for an installed one again (after -/// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). -pub fn reload_dictionary() { - DICT.reload(); -} - -/// The `.dic` file of the installed Portuguese dictionary (File › Options › Proofing shows it); -/// `None` when there is none, or when one given with [`install_dictionary`] is in use. -pub fn installed_path() -> Option { - DICT.installed_path() -} - /// Is a Portuguese dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { DICT.has_dictionary() diff --git a/crates/proof/src/ru/mod.rs b/crates/proof/src/ru/mod.rs index e46bf305f7..67be708589 100644 --- a/crates/proof/src/ru/mod.rs +++ b/crates/proof/src/ru/mod.rs @@ -39,7 +39,7 @@ pub use suggest::suggest; const NAMES: &[&str] = &["ru_RU", "ru-RU", "ru"]; /// The Russian dictionary, found on first use (see [`DictSlot`]). -static DICT: DictSlot = DictSlot::new(NAMES, "Russian"); +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Russian"); /// The Russian dictionary: the installed Hunspell `ru_RU` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available (Russian spelling is off @@ -54,18 +54,6 @@ pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { DICT.install(aff, dic) } -/// Forget the loaded dictionary, so the next check looks for an installed one again (after -/// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). -pub fn reload_dictionary() { - DICT.reload(); -} - -/// The `.dic` file of the installed Russian dictionary (File › Options › Proofing shows it); -/// `None` when there is none, or when one given with [`install_dictionary`] is in use. -pub fn installed_path() -> Option { - DICT.installed_path() -} - /// Is a Russian dictionary available (spelling is checked)? pub fn has_dictionary() -> bool { DICT.has_dictionary() diff --git a/crates/proof/src/slot.rs b/crates/proof/src/slot.rs index 3c0f2bd35b..8b90a94a9a 100644 --- a/crates/proof/src/slot.rs +++ b/crates/proof/src/slot.rs @@ -30,6 +30,37 @@ impl Dictionary for AffixDict { } } +/// What every language's slot answers, whatever its dictionary type (the language table in +/// lib.rs holds them side by side). +pub(crate) trait Slot: Sync { + /// See [`DictSlot::name`]. + fn name(&self) -> &'static str; + /// See [`DictSlot::installed_path`]. + fn installed_path(&self) -> Option; + /// See [`DictSlot::has_dictionary`]. + fn has_dictionary(&self) -> bool; + /// See [`DictSlot::reload`]. + fn reload(&self); +} + +impl Slot for DictSlot { + fn name(&self) -> &'static str { + DictSlot::name(self) + } + + fn installed_path(&self) -> Option { + DictSlot::installed_path(self) + } + + fn has_dictionary(&self) -> bool { + DictSlot::has_dictionary(self) + } + + fn reload(&self) { + DictSlot::reload(self); + } +} + /// The dictionary in use and whether it came from [`DictSlot::install`] rather than a file on /// disk (it then has no file to report, even if a matching file also happens to exist): one lock, /// so a reload and an install can't interleave their two fields. diff --git a/crates/proof/src/tr/mod.rs b/crates/proof/src/tr/mod.rs index eb5f0ea528..f247336be3 100644 --- a/crates/proof/src/tr/mod.rs +++ b/crates/proof/src/tr/mod.rs @@ -29,7 +29,7 @@ pub use suggest::suggest; const NAMES: &[&str] = &["tr_TR", "tr-TR", "tr"]; /// The Turkish dictionary, found on first use (see [`DictSlot`]). -static DICT: DictSlot = DictSlot::new(NAMES, "Turkish"); +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Turkish"); /// The Turkish dictionary: the installed Hunspell `tr_TR` (read on first use), one given with /// [`install_dictionary`], or an empty one when none is available. @@ -43,17 +43,6 @@ pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { DICT.install(aff, dic) } -/// Forget the loaded dictionary, so the next check looks for an installed one again. -pub fn reload_dictionary() { - DICT.reload(); -} - -/// The `.dic` file of the installed Turkish dictionary (File › Options › Proofing shows it); -/// `None` when there is none, or when one given with [`install_dictionary`] is in use. -pub fn installed_path() -> Option { - DICT.installed_path() -} - /// Is a Turkish dictionary available? pub fn has_dictionary() -> bool { DICT.has_dictionary() From e0014ff1d9a2aed00ca1f464b79b642c01564c74 Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 19:58:30 -0100 Subject: [PATCH 007/308] Proofing: the spelling options are read in one place MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Czech, Croatian, Indonesian, Italian, Polish, Portuguese and Spanish began their spelling checks with the same thirteen lines, byte for byte: no dictionary means no marks, single letters and words without letters pass, words with digits and short words in capitals pass when the File › Options › Proofing choices say so, and words in another script pass. Each also had its own copy of the Latin-script test. These were the lines #3966 had to fix in six copies one by one, and where the Russian and Turkish copies stayed wrong until the first commit here. spelling.rs has them once (`word_to_check`), and each language goes on from there with its own rules (Polish apostrophe endings, Italian elisions, Indonesian clitics, Uruguayan `vos` forms). Tests: words_needing_no_lookup. Ignoring the uppercase option there fails it and all seven languages' proofing-options tests. Proof: 286 tests. --- crates/proof/src/cs/mod.rs | 21 ++------------- crates/proof/src/es/mod.rs | 21 ++------------- crates/proof/src/hr/mod.rs | 21 ++------------- crates/proof/src/id/mod.rs | 21 ++------------- crates/proof/src/it/mod.rs | 21 ++------------- crates/proof/src/lib.rs | 1 + crates/proof/src/pl/mod.rs | 21 ++------------- crates/proof/src/pt/mod.rs | 21 ++------------- crates/proof/src/spelling.rs | 52 ++++++++++++++++++++++++++++++++++++ 9 files changed, 67 insertions(+), 133 deletions(-) create mode 100644 crates/proof/src/spelling.rs diff --git a/crates/proof/src/cs/mod.rs b/crates/proof/src/cs/mod.rs index 1d72dc913f..9f8bbd0a7f 100644 --- a/crates/proof/src/cs/mod.rs +++ b/crates/proof/src/cs/mod.rs @@ -38,11 +38,6 @@ pub fn has_dictionary() -> bool { DICT.has_dictionary() } -/// Is `c` a letter of the Latin script (the only one the Czech dictionary covers)? -fn is_latin_letter(c: char) -> bool { - c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) -} - /// Is `word` spelled correctly in Czech? Numbers, single letters, short words in capitals /// (acronyms: `ČR`, `EU`), words with digits and words in other scripts are accepted, and so /// is every word when no Czech dictionary is installed. @@ -64,21 +59,9 @@ pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { /// [`is_correct_with_options`] against a given dictionary. fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { - if d.is_empty() { - return true; // no Czech dictionary installed: no spelling marks - } - let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); - let w = w.trim_matches(['\'', '’']); - let n = w.chars().count(); - if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { + let Some(w) = crate::spelling::word_to_check(d, word, opts) else { return true; - } - if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= 6 { - return true; - } - if !w.chars().any(is_latin_letter) { - return true; // other scripts: no dictionary - } + }; if w.contains('-') { return w.split('-').all(|p| p.chars().count() <= 1 || known(d, p)); } diff --git a/crates/proof/src/es/mod.rs b/crates/proof/src/es/mod.rs index e16e9d6bdc..5263bd1d90 100644 --- a/crates/proof/src/es/mod.rs +++ b/crates/proof/src/es/mod.rs @@ -77,11 +77,6 @@ pub fn has_dictionary(lang: ProofLang) -> bool { slot(lang).has_dictionary() } -/// Is `c` a letter of the Latin script (the only one the Spanish dictionaries cover)? -fn is_latin_letter(c: char) -> bool { - c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) -} - /// Is `word` spelled correctly in `lang`'s Spanish? Numbers, single letters, short words in /// capitals (acronyms: `ONU`, `CDMX`), words with digits and words in other scripts are accepted, /// and so is every word when no dictionary is installed for the variety. Hyphenated compounds @@ -105,21 +100,9 @@ pub fn is_correct_with(d: &AffixDict, word: &str, lang: ProofLang) -> bool { /// [`is_correct_with_options`] against a given dictionary. fn check(d: &AffixDict, word: &str, lang: ProofLang, opts: &crate::Options) -> bool { - if d.is_empty() { - return true; // no Spanish dictionary installed: no spelling marks - } - let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); - let w = w.trim_matches(['\'', '’']); - let n = w.chars().count(); - if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { + let Some(w) = crate::spelling::word_to_check(d, word, opts) else { return true; - } - if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= 6 { - return true; - } - if !w.chars().any(is_latin_letter) { - return true; // other scripts: no dictionary - } + }; if w.contains('-') { return known(d, w, lang) || w.split('-').all(|p| p.chars().count() <= 1 || known(d, p, lang)); } diff --git a/crates/proof/src/hr/mod.rs b/crates/proof/src/hr/mod.rs index 036b723504..53edf0674f 100644 --- a/crates/proof/src/hr/mod.rs +++ b/crates/proof/src/hr/mod.rs @@ -40,11 +40,6 @@ pub fn has_dictionary() -> bool { DICT.has_dictionary() } -/// Is `c` a letter of the Latin script (the only one the Croatian dictionary covers)? -fn is_latin_letter(c: char) -> bool { - c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) -} - /// Is `word` spelled correctly in Croatian? Numbers, single letters, short words in capitals /// (acronyms: `HRT`, `RH`), words with digits and words in other scripts are accepted, and so /// is every word when no Croatian dictionary is installed. @@ -66,21 +61,9 @@ pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { /// [`is_correct_with_options`] against a given dictionary. fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { - if d.is_empty() { - return true; // no Croatian dictionary installed: no spelling marks - } - let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); - let w = w.trim_matches(['\'', '’']); - let n = w.chars().count(); - if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { + let Some(w) = crate::spelling::word_to_check(d, word, opts) else { return true; - } - if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= 6 { - return true; - } - if !w.chars().any(is_latin_letter) { - return true; // other scripts: no dictionary - } + }; if w.contains('-') { return w.split('-').all(|p| p.chars().count() <= 1 || known(d, p)); } diff --git a/crates/proof/src/id/mod.rs b/crates/proof/src/id/mod.rs index f1e44df452..04248d6387 100644 --- a/crates/proof/src/id/mod.rs +++ b/crates/proof/src/id/mod.rs @@ -45,11 +45,6 @@ pub fn has_dictionary() -> bool { /// (`se-Indonesia`, `KTP-nya`, `Jakarta-lah`); they aren't dictionary words on their own. const CLITICS: &[&str] = &["se", "nya", "ku", "mu", "lah", "kah", "pun"]; -/// Is `c` a letter of the Latin script (the only one the Indonesian dictionary covers)? -fn is_latin_letter(c: char) -> bool { - c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) -} - /// Is `word` spelled correctly in Indonesian? Numbers, single letters, short words in capitals /// (acronyms: `DPR`, `KTP`), words with digits and words in other scripts are accepted, and so /// is every word when no Indonesian dictionary is installed. A hyphenated word is correct when @@ -73,21 +68,9 @@ pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { /// [`is_correct_with_options`] against a given dictionary. fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { - if d.is_empty() { - return true; // no Indonesian dictionary installed: no spelling marks - } - let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); - let w = w.trim_matches(['\'', '’']); - let n = w.chars().count(); - if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { + let Some(w) = crate::spelling::word_to_check(d, word, opts) else { return true; - } - if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= 6 { - return true; - } - if !w.chars().any(is_latin_letter) { - return true; // other scripts: no dictionary - } + }; if w.contains('-') { return known(d, w) || w.split('-').all(|p| { diff --git a/crates/proof/src/it/mod.rs b/crates/proof/src/it/mod.rs index 1e606ad749..ee5aaaee32 100644 --- a/crates/proof/src/it/mod.rs +++ b/crates/proof/src/it/mod.rs @@ -49,11 +49,6 @@ pub fn has_dictionary() -> bool { DICT.has_dictionary() } -/// Is `c` a letter of the Latin script (the only one the Italian dictionary covers)? -fn is_latin_letter(c: char) -> bool { - c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) -} - /// Proclitics (and verb clippings) that elide before a vowel or `h`: the word after the /// apostrophe is what gets checked (`l'amico`, `un'idea`, `c'è`, `m'hai`, `dov'è`, `anch'io`). const PROCLITICS: &[&str] = &[ @@ -93,21 +88,9 @@ pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { /// [`is_correct_with_options`] against a given dictionary. fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { - if d.is_empty() { - return true; // no Italian dictionary installed: no spelling marks - } - let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); - let w = w.trim_matches(['\'', '’']); - let n = w.chars().count(); - if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { + let Some(w) = crate::spelling::word_to_check(d, word, opts) else { return true; - } - if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= 6 { - return true; - } - if !w.chars().any(is_latin_letter) { - return true; // other scripts: no dictionary - } + }; let lower = w.to_lowercase(); if TRUNCATIONS.contains(&lower.as_str()) || left_to_grammar(&lower) { return true; diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 0c51e2362f..365229d703 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -67,6 +67,7 @@ pub mod pl; pub mod pt; pub mod ru; mod slot; +mod spelling; pub mod tr; pub mod vi; diff --git a/crates/proof/src/pl/mod.rs b/crates/proof/src/pl/mod.rs index 30d6d966fe..7cde9d0f98 100644 --- a/crates/proof/src/pl/mod.rs +++ b/crates/proof/src/pl/mod.rs @@ -52,11 +52,6 @@ pub fn has_dictionary() -> bool { /// (`Kennedy'ego`, `Wilde'a`, `Shakespeare'owi`). const APOSTROPHE_ENDINGS: &[&str] = &["a", "ach", "ami", "e", "ego", "em", "emu", "i", "m", "om", "owi", "owie", "u", "y", "ów", "ą", "ę"]; -/// Is `c` a letter of the Latin script (the only one the Polish dictionary covers)? -fn is_latin_letter(c: char) -> bool { - c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) -} - /// Is `word` spelled correctly in Polish? Numbers, single letters, short words in capitals /// (acronyms: `PKO`, `ZUS`), words with digits and words in other scripts are accepted, and so /// is every word when no Polish dictionary is installed. @@ -78,21 +73,9 @@ pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { /// [`is_correct_with_options`] against a given dictionary. fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { - if d.is_empty() { - return true; // no Polish dictionary installed: no spelling marks - } - let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); - let w = w.trim_matches(['\'', '’']); - let n = w.chars().count(); - if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { + let Some(w) = crate::spelling::word_to_check(d, word, opts) else { return true; - } - if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= 6 { - return true; - } - if !w.chars().any(is_latin_letter) { - return true; // other scripts: no dictionary - } + }; let w = w.replace('’', "'"); if w.contains('-') { return w.split('-').all(|p| p.chars().count() <= 1 || known(d, p)); diff --git a/crates/proof/src/pt/mod.rs b/crates/proof/src/pt/mod.rs index 0110a8c183..ea91dbdb17 100644 --- a/crates/proof/src/pt/mod.rs +++ b/crates/proof/src/pt/mod.rs @@ -47,11 +47,6 @@ pub fn has_dictionary() -> bool { DICT.has_dictionary() } -/// Is `c` a letter of the Latin script (the only one the Portuguese dictionary covers)? -fn is_latin_letter(c: char) -> bool { - c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) -} - /// Is `word` spelled correctly in Portuguese? Numbers, single letters, short words in capitals /// (acronyms: `USP`, `PT`), words with digits and words in other scripts are accepted, and so /// is every word when no Portuguese dictionary is installed. @@ -73,21 +68,9 @@ pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { /// [`is_correct_with_options`] against a given dictionary. fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { - if d.is_empty() { - return true; // no Portuguese dictionary installed: no spelling marks - } - let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); - let w = w.trim_matches(['\'', '’']); - let n = w.chars().count(); - if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { + let Some(w) = crate::spelling::word_to_check(d, word, opts) else { return true; - } - if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= 6 { - return true; - } - if !w.chars().any(is_latin_letter) { - return true; // other scripts: no dictionary - } + }; if w.contains('-') { return w.split('-').all(|p| p.chars().count() <= 1 || known(d, p)); } diff --git a/crates/proof/src/spelling.rs b/crates/proof/src/spelling.rs new file mode 100644 index 0000000000..ab5e520e9f --- /dev/null +++ b/crates/proof/src/spelling.rs @@ -0,0 +1,52 @@ +//! The start of every Latin-script dictionary language's spelling check (Czech, Croatian, +//! Indonesian, Italian, Polish, Portuguese, Spanish): the words that need no lookup, under the +//! File › Options › Proofing choices. Each language goes on from there with its own rules. + +use crate::Options; +use crate::affix::AffixDict; + +/// Is `c` a letter of the Latin script (the only one these dictionaries cover)? +fn latin(c: char) -> bool { + c.is_alphabetic() && (c < '\u{0250}' || ('\u{1E00}'..='\u{1EFF}').contains(&c)) +} + +/// `word` trimmed of quotes and apostrophes, when `d` must say whether it is right; `None` when it +/// is right without a lookup: there is no dictionary (no spelling marks), the word is a single +/// letter or has no letters, it has digits or is a short word in capitals (an acronym) and the +/// options skip those, or it is in another script. +pub(crate) fn word_to_check<'a>(d: &AffixDict, word: &'a str, opts: &Options) -> Option<&'a str> { + if d.is_empty() { + return None; + } + let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); + let w = w.trim_matches(['\'', '’']); + let n = w.chars().count(); + if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { + return None; + } + if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= 6 { + return None; + } + w.chars().any(latin).then_some(w) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn words_needing_no_lookup() { + let d = AffixDict::from_hunspell("", "1\nkot\n", &[]).unwrap(); + let defaults = Options::default(); + assert_eq!(word_to_check(&AffixDict::default(), "kot", &defaults), None, "no dictionary"); + for w in ["", "a", "2026", "PKO", "B2B", "日本語", "--"] { + assert_eq!(word_to_check(&d, w, &defaults), None, "{w}"); + } + assert_eq!(word_to_check(&d, "„kot”", &defaults), Some("kot")); + assert_eq!(word_to_check(&d, "'kot'", &defaults), Some("kot")); + let strict = Options { ignore_uppercase: false, ignore_numbers: false, ..defaults }; + assert_eq!(word_to_check(&d, "PKO", &strict), Some("PKO")); + assert_eq!(word_to_check(&d, "B2B", &strict), Some("B2B")); + assert_eq!(word_to_check(&d, "PRZEDSIĘBIORSTWO", &defaults), Some("PRZEDSIĘBIORSTWO"), "only short capitals are acronyms"); + } +} From 4dfdecf3d716101519f186125d9f5ab29eb39d10 Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 20:57:35 -0100 Subject: [PATCH 008/308] Proofing: a candidate found again at a lower cost ranks at that cost The suggestion rounds don't run in rising cost: a MAP diacritic drop (11) comes before a REP confusion (10, 9 for Russian). The engine checked each candidate once, at the first cost it was offered, so the same word found again more cheaply kept the dearer cost and could rank one place too low. Czech's engine always did this, and the merged engine took it over. The candidate set is now a map to the cheapest cost offered so far, and an offer that is no cheaper is skipped as before. Found by CodeRabbit on #4191. Tests: a_cheaper_later_offer_wins; skipping every repeat offer again fails it. Proof: 288 tests. --- crates/proof/src/affix_suggest.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/proof/src/affix_suggest.rs b/crates/proof/src/affix_suggest.rs index 1da3555956..ade16edd1d 100644 --- a/crates/proof/src/affix_suggest.rs +++ b/crates/proof/src/affix_suggest.rs @@ -102,7 +102,8 @@ struct Collector<'a> { d: &'a AffixDict, /// The word, lower case. input: String, - tested: HashSet, + /// Candidate → the cheapest cost it was offered at (rounds don't run in rising cost). + tested: HashMap, /// Suggestion → (cost, order found). found: HashMap, seq: usize, @@ -112,9 +113,16 @@ impl Collector<'_> { /// Check a lower-case candidate (a word, or two separated by a space) and keep it if every /// word is in the dictionary, as written or capitalized (names). fn offer(&mut self, cand: &str, cost: u32) { - if cand.is_empty() || cand == self.input || !self.tested.insert(cand.to_string()) { + if cand.is_empty() || cand == self.input { return; } + match self.tested.get_mut(cand) { + Some(prev) if *prev <= cost => return, + Some(prev) => *prev = cost, + None => { + self.tested.insert(cand.to_string(), cost); + } + } let mut words = Vec::new(); let mut cost = cost; for part in cand.split(' ') { @@ -270,7 +278,7 @@ pub(crate) fn suggest_in(d: &AffixDict, word: &str, max: usize, t: &Tuning) -> V let case = casing(word); let input = word.to_lowercase(); let chars: Vec = input.chars().collect(); - let mut col = Collector { d, input: input.clone(), tested: HashSet::new(), found: HashMap::new(), seq: 0 }; + let mut col = Collector { d, input: input.clone(), tested: HashMap::new(), found: HashMap::new(), seq: 0 }; // 1. Letter case. if d.contains(&input) && word != input { col.keep(input.clone(), COST_CASE); @@ -463,6 +471,18 @@ mod tests { assert!(suggestions.contains(&"počítač z".into()), "{suggestions:?}"); } + /// A candidate offered again at a lower cost (a REP confusion after a MAP diacritic drop) + /// ranks at the lower one. + #[test] + fn a_cheaper_later_offer_wins() { + let d = AffixDict::from_hunspell("", "1\ncafe\n", &[]).unwrap(); + let mut col = Collector { d: &d, input: "cafè".into(), tested: HashMap::new(), found: HashMap::new(), seq: 0 }; + col.offer("cafe", COST_DROP_DIACRITIC); + col.offer("cafe", COST_REP); + col.offer("cafe", COST_SCAN); + assert_eq!(col.found.get("cafe").map(|e| e.0), Some(COST_REP)); + } + #[test] fn distance_weighs_map_slips_half() { let map = vec![vec!['a', 'ą'], vec!['o', 'ó', 'u']]; From 4c9ff5db3584b2052a2940bf45da6e52f06b7578 Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 20:57:37 -0100 Subject: [PATCH 009/308] Proofing: a dictionary load from before a folder change is not kept MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A dictionary is read outside the lock. If the dictionaries folder changed while it was being read (File › Options › Proofing), the reload emptied the slot first and the late load then filled it with the old folder's dictionary. It stayed until the next reload. Every language's copy of the loader had this window; the shared slot now closes it in one place. The slot counts reloads. A load that started before a reload still answers the check that asked for it, but it isn't kept: the next check reads the new folder. Found by CodeRabbit on #4191. Tests: a_load_from_before_a_reload_is_not_kept; not counting reloads fails it. Proof: 288 tests. --- crates/proof/src/slot.rs | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/crates/proof/src/slot.rs b/crates/proof/src/slot.rs index 8b90a94a9a..6255ae5a5f 100644 --- a/crates/proof/src/slot.rs +++ b/crates/proof/src/slot.rs @@ -67,6 +67,8 @@ impl Slot for DictSlot { struct State { dict: Option>, in_memory: bool, + /// Counts reloads, so a load that started before one is not kept. + reloads: u64, } /// One language's dictionary. @@ -80,7 +82,7 @@ pub(crate) struct DictSlot { impl DictSlot { pub(crate) const fn new(names: &'static [&'static str], english: &'static str) -> Self { - DictSlot { names, english, state: RwLock::new(State { dict: None, in_memory: false }) } + DictSlot { names, english, state: RwLock::new(State { dict: None, in_memory: false, reloads: 0 }) } } fn read(&self) -> RwLockReadGuard<'_, State> { @@ -105,12 +107,25 @@ impl DictSlot { /// The dictionary: the installed one (read on first use), one given with [`Self::install`], or /// an empty one when none is available (the language's spelling is off then). pub(crate) fn get(&self) -> Arc { - if let Some(d) = self.read().dict.as_ref() { - return d.clone(); + let reloads = { + let s = self.read(); + if let Some(d) = s.dict.as_ref() { + return d.clone(); + } + s.reloads + }; + self.keep(reloads, Arc::new(self.load_installed())) + } + + /// Keep a dictionary loaded after `reloads` reloads. If the folder was reloaded since, the + /// load may have read the old folder: it serves this one check and the next one loads again. + fn keep(&self, reloads: u64, loaded: Arc) -> Arc { + let mut s = self.write(); + if s.reloads != reloads { + return s.dict.clone().unwrap_or(loaded); } - let loaded = Arc::new(self.load_installed()); // Another thread (or an install) may have filled it meanwhile: that one wins. - self.write().dict.get_or_insert(loaded).clone() + s.dict.get_or_insert(loaded).clone() } fn load_installed(&self) -> D { @@ -142,6 +157,7 @@ impl DictSlot { let mut slot = self.write(); slot.dict = None; slot.in_memory = false; + slot.reloads = slot.reloads.wrapping_add(1); } /// The `.dic` file of the installed dictionary, searching the dictionaries folder and the @@ -178,4 +194,17 @@ mod tests { assert!(!SLOT.has_dictionary(), "reloaded from disk, where there is none"); assert_eq!(SLOT.name(), "zz_SLOT_TEST"); } + + /// A load that started before a dictionaries-folder change is not kept after its reload. + #[test] + fn a_load_from_before_a_reload_is_not_kept() { + let slot: DictSlot = DictSlot::new(&["zz_SLOT_RELOAD_TEST"], "Test"); + let before = slot.read().reloads; + slot.reload(); + let _ = slot.keep(before, Arc::new(AffixDict::default())); + assert!(slot.read().dict.is_none(), "the next check loads from the new folder"); + let now = slot.read().reloads; + let _ = slot.keep(now, Arc::new(AffixDict::default())); + assert!(slot.read().dict.is_some(), "a load since the reload is kept"); + } } From b2787bf36ec3bc39d29b396be8e0adeb1f705cf8 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:00:13 +0100 Subject: [PATCH 010/308] M7: add a persistent language-aware spelling review dialog Keep compact context menus, but show a correction form for user spelling/editor commands. Advance without wrapping after change, ignore or dictionary actions; reject stale targets and retain programmatic command results. Add navigation/UI regressions and light/dark preview CI. cargo xtask ci was attempted on the preparation PC but cargo is absent; hosted validation is required before integration. --- .github/workflows/spelling-review.yml | 56 ++++++ ROADMAP.md | 4 +- crates/engine/src/cmd/review.rs | 35 +++- crates/ui-egui/src/canvas.rs | 32 --- crates/ui-egui/src/dialogs_review.rs | 4 + crates/ui-egui/src/dialogs_spelling.rs | 262 +++++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 16 +- docs/gaps.md | 5 +- docs/spelling-review.md | 24 +++ docs/ui-parity.md | 9 +- 10 files changed, 397 insertions(+), 50 deletions(-) create mode 100644 .github/workflows/spelling-review.yml create mode 100644 crates/ui-egui/src/dialogs_spelling.rs create mode 100644 docs/spelling-review.md diff --git a/.github/workflows/spelling-review.yml b/.github/workflows/spelling-review.yml new file mode 100644 index 0000000000..cd9e7981a0 --- /dev/null +++ b/.github/workflows/spelling-review.yml @@ -0,0 +1,56 @@ +name: Spelling review preview + +on: + pull_request: + paths: + - 'crates/ui-egui/src/dialogs_spelling.rs' + - 'crates/ui-egui/src/dialogs_review.rs' + - 'crates/engine/src/cmd/review.rs' + - '.github/workflows/spelling-review.yml' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: spelling-review-preview-${{ github.ref }} + cancel-in-progress: true + +jobs: + preview: + runs-on: ubuntu-24.04 + timeout-minutes: 45 + env: + CARGO_INCREMENTAL: '0' + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + - name: Install build and software-rendering dependencies + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcursor-dev libxrandr-dev \ + libxi-dev libgtk-3-dev libfontconfig1-dev libfreetype-dev libvulkan1 mesa-vulkan-drivers + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec + with: + components: rustfmt + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 + - name: Formatting + id: formatting + continue-on-error: true + run: cargo fmt --all -- --check + - name: Proofing navigation regression + run: cargo test --locked -p wordcraft-engine --lib spelling_review_dialog_navigation + - name: Correction form regressions + run: cargo test --locked -p wordcraft-ui-egui --lib spelling_review_dialog -- --skip preview + - name: Render light and dark correction forms + run: cargo test --locked -p wordcraft-ui-egui --lib spelling_review_dialog_preview -- --ignored --nocapture + - name: Save visual review artifacts + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 + with: + name: spelling-review-preview + path: spelling-review-*.png + if-no-files-found: error + - name: Require formatting success + if: steps.formatting.outcome != 'success' + run: exit 1 diff --git a/ROADMAP.md b/ROADMAP.md index b892e00e8f..dc9e3fe124 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (persistent spelling and grammar correction dialog); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-18 · **Change:** trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -311,6 +311,7 @@ Imported Word 97–2003 `.doc`/`.dot` files now ask for a supported DOCX target | 2026-10-17 | Non-overlapping body floating tables clear header tables. Original native controls distinguish never-overlap from allowed overlap. | | 2026-10-14 | Comment text being typed is no longer lost or misplaced (#1348): an edit or reply the document refuses (for example read-only protection) keeps the typed text in its field and says why on the status line; drafts belong to their document, so after New or Open a comment with the same number shows its own text; Save, Export, Print, Close, New and Open write pending comment text first; if the document refuses it, Save, Export and Print carry on with the stored comments and show why on the status line (a refused draft is dropped when Save is the only change), while Close, New and Open show the Save Changes prompt (Don't Save drops text the document refuses); Escape discards what was typed; in a document protected as read only or for forms the comment fields can't be typed in | |---|---| +| 2026-10-26 | Spelling & Grammar opens a persistent correction form with suggestions and editable replacement text. Changing, ignoring or adding a word advances to the next issue in the current story; completion stays visible. Right-click keeps its compact menu. | | 2026-10-18 | Explicit New/Open keeps the requested document visible when startup recovery discovery finishes; older copies stay available. Recover uses the regular theme. See docs/recovery.md. | | 2026-10-15 | A floating picture or shape in a table cell set to align centre or bottom no longer lands half its height above its line (#1887); it starts at the anchor line | | 2026-10-18 | Picture Format › Size (#2573): width and height in the interface unit, and a size typed in any unit works, pixels too (`300 px` keeps the picture's proportions, #1901), applied once typed (Enter, Tab or a click elsewhere; Esc cancels) in one undo step. The group's launcher opens a Size dialog: height and width, scale of the original size (or of the size it opened with), Lock aspect ratio, the original size with its pixels, and Reset. `picture.size {"original": true, "scale"?}` sizes from the original; sizes past 4000 pt that keep the proportions shrink as a whole. | @@ -643,6 +644,7 @@ Footer page-table height and body wrapping are corrected. Other pagination gaps | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Persistent Spelling & Grammar form uses shared language-aware review commands; Change, Ignore Once/All and Add to Dictionary advance without wrapping; stale targets are refused; readiness estimates unchanged | | 2026-10-22 | trivial | Progress log: one native window per open document | | 2026-10-22 | trivial | Progress log: every open document keeps its own recovery copy | | 2026-10-22 | trivial | Progress log: several open documents in the desktop window | diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index 9fcaa98a4a..4c168b2a06 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -66,6 +66,7 @@ pub fn specs() -> Vec { CommandSpec::new("review.changes", "Reviewing Pane", "Review › Tracking", list_changes).pure(), CommandSpec::new("review.spelling", "Spelling & Grammar", "Review › Proofing", next_issue) .key(super::mac_or("F7 / Alt+F7 / Mod+Alt+L", "F7 / Alt+F7")) + .params(r#"{"wrap"?: bool (default true; false stops at the end of the current story)}"#) .pure(), CommandSpec::new("review.issues", "Proofing Issues", "Review › Proofing", all_issues).pure(), CommandSpec::new("review.suggestions", "Spelling Suggestions", "Review › Proofing", suggestions).params(r#"{"pos"?: Pos}"#).pure(), @@ -989,15 +990,20 @@ fn issue_json(s: &Session, a: &Pos, b: &Pos, i: &wordcraft_proof::Issue) -> Valu } /// F7: select the next issue after the caret and return it with suggestions. -fn next_issue(s: &mut Session, _: &Value) -> CmdResult { +fn next_issue(s: &mut Session, v: &Value) -> CmdResult { let caret = s.sel.ordered().1; let story = caret.story; let paths = s.doc.para_paths(story); let start = paths.iter().position(|p| *p == caret.path).unwrap_or(0); - for k in 0..paths.len() { - let Some(path) = paths.get((start + k) % paths.len().max(1)) else { break }; + let wrap = p::bool(v, "wrap").unwrap_or(true); + // Scan forward first, including the rest of the caret's paragraph. A second pass + // optionally visits the earlier part; never select the first typo repeatedly in + // a single-paragraph story. Dialogs disable wrapping so Ignore Once can finish. + for (path, before) in paths.iter().skip(start).map(|path| (path, false)).chain( + paths.iter().take(if wrap { start.saturating_add(1) } else { 0 }).map(|path| (path, true)), + ) { for (a, b, i) in para_issues(s, story, path) { - if k == 0 && a.off < caret.off && start + k < paths.len() && paths.len() > 1 { + if *path == caret.path && (a.off < caret.off) != before { continue; } let r = issue_json(s, &a, &b, &i); @@ -2246,3 +2252,24 @@ mod tests { } } } + +#[cfg(test)] +mod spelling_navigation_tests { + use super::*; + use wordcraft_doc::Document; + + #[test] + fn spelling_review_dialog_navigation_moves_forward_and_wraps_only_when_requested() { + for text in ["The brwn fox and brwn dog.", "The brwn fox.\nThe brwn dog."] { + let mut s = Session::new(Document::from_text(text)); + let first = s.run("review.spelling", &json!({"wrap": false})).unwrap(); + assert_eq!(first["text"], "brwn"); + let first_selection = s.sel.clone(); + let second = s.run("review.spelling", &json!({"wrap": false})).unwrap(); + assert_eq!(second["text"], "brwn"); + assert_ne!(s.sel, first_selection); + assert_eq!(s.run("review.spelling", &json!({"wrap": false})).unwrap()["done"], true); + assert_eq!(s.run("review.spelling", &json!({})).unwrap()["start"], first["start"]); + } + } +} diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 9b9e47474c..2fb9ba4c49 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -6217,38 +6217,6 @@ mod tests { assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brown fox"); } - /// #3340: review.spelling (the Review tab's Spelling & Grammar button and the Home tab's - /// Editor button both run it) only moved the selection to the next issue — with no way to see - /// or apply a suggestion from there, just the generic selection toolbar, it looked broken. - /// Running it now also opens the same suggestions menu Shift+F10 and a Control-click already - /// open for the word at the caret. - #[test] - fn spelling_and_grammar_opens_suggestions_for_the_issue_it_selects() { - use egui_kittest::kittest::Queryable; - let mut a = WordApp::new(wordcraft_engine::Session::new(wordcraft_doc::Document::new()), Default::default()); - a.run("text.insert", json!({"text": "The quick brwn fox"})).unwrap(); - a.session.sel = wordcraft_engine::Selection::caret(Pos::body(0, 0)); - a.run("review.spelling", json!({})).unwrap(); - assert!(a.canvas.context_menu_from_keys, "queues the open the context menu already uses for Shift+F10"); - let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state( - |ui, app: &mut WordApp| { - let ctx = ui.ctx().clone(); - app.logic(&ctx); - app.ui(ui); - }, - a, - ); - for _ in 0..6 { - h.step(); - } - assert!(h.state().canvas.context_menu_open, "Spelling & Grammar opens the suggestions menu"); - h.get_by_label("brown").click(); - for _ in 0..3 { - h.step(); - } - assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brown fox"); - } - #[test] fn floating_picture_can_be_dragged_in_front_of_or_behind_text() { for wrap in ["inFrontOfText", "behindText"] { diff --git a/crates/ui-egui/src/dialogs_review.rs b/crates/ui-egui/src/dialogs_review.rs index b1f38ccb9e..5cb3d71645 100644 --- a/crates/ui-egui/src/dialogs_review.rs +++ b/crates/ui-egui/src/dialogs_review.rs @@ -17,6 +17,7 @@ use crate::theme::{Tokens, semibold}; #[derive(Clone, Debug, Serialize)] #[serde(tag = "form", rename_all = "camelCase")] pub enum ReviewDialog { + Spelling(Box), Compare(CompareForm), AutoCorrect(Box), } @@ -24,6 +25,7 @@ pub enum ReviewDialog { impl ReviewDialog { pub fn name(&self) -> &'static str { match self { + ReviewDialog::Spelling(_) => "spelling", ReviewDialog::Compare(f) if f.combine => "combine", ReviewDialog::Compare(_) => "compare", ReviewDialog::AutoCorrect(_) => "autoCorrect", @@ -33,6 +35,7 @@ impl ReviewDialog { /// The window title (English; the caller translates it). pub fn title(&self) -> &'static str { match self { + ReviewDialog::Spelling(_) => "Spelling & Grammar", ReviewDialog::Compare(f) if f.combine => "Combine Documents", ReviewDialog::Compare(_) => "Compare Documents", ReviewDialog::AutoCorrect(_) => "AutoCorrect", @@ -57,6 +60,7 @@ pub fn open(name: &str, app: &mut WordApp) -> Option { /// Draws the dialog; returns true to close it. pub fn body(app: &mut WordApp, ui: &mut Ui, d: &mut ReviewDialog) -> bool { match d { + ReviewDialog::Spelling(f) => crate::dialogs_spelling::body(app, ui, f), ReviewDialog::Compare(f) => compare_ui(app, ui, f), ReviewDialog::AutoCorrect(f) => autocorrect_ui(app, ui, f), } diff --git a/crates/ui-egui/src/dialogs_spelling.rs b/crates/ui-egui/src/dialogs_spelling.rs new file mode 100644 index 0000000000..9d3fa574cf --- /dev/null +++ b/crates/ui-egui/src/dialogs_spelling.rs @@ -0,0 +1,262 @@ +//! A persistent proofing form over the shared review commands, independent of language data. + +use egui::Ui; +use serde::Serialize; +use serde_json::{Value, json}; +use wordcraft_engine::Selection; + +use crate::WordApp; + +#[derive(Clone, Debug, Serialize)] +pub struct SpellingForm { + issue: Value, + replacement: String, + message: String, + #[serde(skip)] + document: u64, + #[serde(skip)] + epoch: u64, + #[serde(skip)] + selection: Selection, +} + +impl SpellingForm { + pub fn new(app: &WordApp, issue: Value) -> Self { + let replacement = issue.get("suggestions").and_then(Value::as_array).and_then(|a| a.first()).and_then(Value::as_str).unwrap_or("").into(); + Self { + issue, + replacement, + message: String::new(), + document: app.session.document_id(), + epoch: app.session.ime_body_epoch(), + selection: app.session.sel.clone(), + } + } + + fn done(&self) -> bool { + self.issue.get("done").and_then(Value::as_bool) == Some(true) + } + + // Other windows and commands can change a target while this form is open. + // Never apply a cached suggestion to a different selection or document body. + fn current(&self, app: &mut WordApp) -> bool { + self.document == app.session.document_id() + && self.epoch == app.session.ime_body_epoch() + && self.selection == app.session.sel + && app.session.run("review.suggestions", &json!({})).ok().as_ref() == Some(&self.issue) + } + + fn next(&mut self, app: &mut WordApp, wrap: bool) { + match app.execute("review.spelling", json!({"wrap": wrap})) { + Ok(issue) => *self = Self::new(app, issue), + Err(e) => self.message = e, + } + } + + fn act(&mut self, app: &mut WordApp, command: Option<&str>) { + if !self.current(app) { + self.message = tl!("The document or selection changed. Check again to continue.").into(); + return; + } + if let Some(command) = command { + let params = if command == "review.applySuggestion" { json!({"text": self.replacement}) } else { json!({}) }; + if let Err(e) = app.run(command, params) { + self.message = e; + return; + } + } + self.next(app, false); + } +} + +/// Changing or ignoring advances within the same form; Close and Escape dismiss it. +pub fn body(app: &mut WordApp, ui: &mut Ui, form: &mut SpellingForm) -> bool { + ui.set_width(420.0); + if form.done() { + ui.label(tl!("The spelling and grammar check is complete.")); + } else { + let spelling = form.issue.get("kind").and_then(Value::as_str) == Some("spelling"); + ui.horizontal_wrapped(|ui| { + ui.label(tl!(if spelling { "Spelling" } else { "Grammar" })); + ui.label(form.issue.get("lang").and_then(Value::as_str).unwrap_or("")); + }); + ui.label(egui::RichText::new(form.issue.get("text").and_then(Value::as_str).unwrap_or("")).strong()); + ui.label(tl!(form.issue.get("message").and_then(Value::as_str).unwrap_or(""))); + ui.add_space(8.0); + ui.label(tl!("Suggestions")); + let suggestions: Vec<&str> = form.issue.get("suggestions").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).collect(); + egui::ScrollArea::vertical().id_salt("spelling_suggestions").max_height(120.0).show(ui, |ui| { + if suggestions.is_empty() { + ui.label(egui::RichText::new(tl!("(no suggestions)")).italics()); + } + for suggestion in &suggestions { + if ui.selectable_label(form.replacement == *suggestion, *suggestion).clicked() { + form.replacement = (*suggestion).into(); + } + } + }); + let label = ui.label(tl!("Change to:")); + ui.add(egui::TextEdit::singleline(&mut form.replacement).char_limit(1024).desired_width(400.0)).labelled_by(label.id); + let can_change = (!form.replacement.is_empty() || suggestions.contains(&"")) && crate::widgets::enabled(app, "review.applySuggestion"); + let mut action = None; + ui.horizontal_wrapped(|ui| { + if ui.add_enabled(can_change, egui::Button::new(tl!("Change"))).clicked() { + action = Some(Some("review.applySuggestion")); + } + if ui.button(tl!("Ignore Once")).clicked() { + action = Some(None); + } + if spelling { + if ui.button(tl!("Ignore All")).clicked() { + action = Some(Some("review.ignoreAll")); + } + if ui.button(tl!("Add to Dictionary")).clicked() { + action = Some(Some("review.addToDictionary")); + } + } + }); + if let Some(command) = action { + form.act(app, command); + } + } + if !form.message.is_empty() { + ui.colored_label(ui.visuals().error_fg_color, &form.message); + } + ui.add_space(8.0); + let mut close = false; + ui.horizontal(|ui| { + if ui.button(tl!("Check Again")).clicked() { + form.next(app, true); + } + close = ui.button(tl!("Close")).clicked(); + }); + close || crate::dialogs::cancel_key_pressed(ui) +} + +#[cfg(test)] +mod tests { + use egui_kittest::kittest::Queryable; + use wordcraft_doc::{Document, Pos, StoryRef}; + use wordcraft_engine::Session; + + use super::*; + use crate::{Services, dialogs::Dialog, dialogs_review::ReviewDialog}; + + fn app(text: &str) -> WordApp { + WordApp::new(Session::new(Document::from_text(text)), Services::default()) + } + + fn frame(ui: &mut Ui, app: &mut WordApp) { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + } + + fn harness(text: &str) -> egui_kittest::Harness<'static, WordApp> { + let mut app = app(text); + app.run("review.spelling", json!({})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(frame, app); + h.run_steps(6); + h + } + + fn form(app: &WordApp) -> &SpellingForm { + match &app.dialog { + Some(Dialog::Review(d)) => match &**d { + ReviewDialog::Spelling(f) => f, + _ => panic!("spelling form expected"), + }, + _ => panic!("spelling dialog expected"), + } + } + + #[test] + fn spelling_review_dialog_changes_a_suggestion_and_undo_restores_the_word() { + let mut h = harness("The quick brwn fox."); + assert!(!h.state().canvas.context_menu_open); + h.get_by_label("brown").click(); + h.run_steps(2); + h.get_by_label("Change").click(); + h.run_steps(3); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brown fox."); + assert!(form(h.state()).done()); + h.get_by_label("Close").click(); + h.run_steps(2); + assert!(h.state().dialog.is_none()); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brwn fox."); + } + + #[test] + fn spelling_review_dialog_ignore_once_advances_and_finishes_without_changing_text() { + let text = "The brwn fox and brwn dog."; + let mut h = harness(text); + let first = h.state().session.sel.clone(); + h.get_by_label("Ignore Once").click(); + h.run_steps(3); + assert_ne!(h.state().session.sel, first); + assert_eq!(form(h.state()).issue["text"], "brwn"); + h.get_by_label("Ignore Once").click(); + h.run_steps(3); + assert!(form(h.state()).done()); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), text); + h.get_by_label("Check Again").click(); + h.run_steps(3); + assert_eq!(h.state().session.sel, first, "Ignore Once never adds a word to the dictionary"); + } + + #[test] + fn spelling_review_dialog_ignore_all_and_add_to_dictionary_advance() { + for (word, button) in [("zqproofingdialogignore", "Ignore All"), ("zqproofingdialogcustom", "Add to Dictionary")] { + let text = format!("The {word} fox and {word} dog."); + let mut h = harness(&text); + h.get_by_label(button).click(); + h.run_steps(3); + assert!(form(h.state()).done()); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), text); + assert!(h.state_mut().session.run("review.issues", &json!({})).unwrap().as_array().unwrap().is_empty()); + } + } + + #[test] + fn spelling_review_dialog_refuses_a_stale_selection_or_replaced_body() { + for replace in [false, true] { + let mut a = app("The brwn fox and brwn dog."); + let issue = a.execute("review.spelling", json!({})).unwrap(); + assert!(a.dialog.is_none(), "programmatic review calls never open a window"); + let mut f = SpellingForm::new(&a, issue); + f.replacement = "brown".into(); + if replace { + a.execute("document.setText", json!({"text": "The brwn fox and brwn dog."})).unwrap(); + } else { + a.session.sel = Selection::caret(Pos::body(0, 0)); + } + let before = a.session.doc.plain_text(StoryRef::Body); + f.act(&mut a, Some("review.applySuggestion")); + assert!(!f.message.is_empty()); + assert_eq!(a.session.doc.plain_text(StoryRef::Body), before); + } + } + + #[test] + fn spelling_review_dialog_shows_completion_without_issues_and_escape_closes() { + let mut h = harness("The quick brown fox."); + assert!(form(h.state()).done()); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.state().dialog.is_none()); + } + + #[test] + #[ignore = "offscreen visual review; requires a software or hardware graphics adapter"] + fn spelling_review_dialog_preview() { + let mut a = app("The quick brwn fox."); + a.run("review.spelling", json!({})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1000.0, 700.0)).wgpu().build_ui_state(frame, a); + for (name, visuals) in [("light", egui::Visuals::light()), ("dark", egui::Visuals::dark())] { + h.ctx.set_visuals(visuals); + h.run_steps(6); + h.render().unwrap().save(format!("spelling-review-{name}.png")).unwrap(); + } + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index dca58282f5..d91b6c67f0 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -45,6 +45,7 @@ pub mod dialogs_notes; pub mod dialogs_para; pub mod dialogs_refs; pub mod dialogs_review; +pub mod dialogs_spelling; pub mod dialogs_ribbon; pub mod dialogs_smart_art; pub mod dialogs_table_style; @@ -1114,15 +1115,12 @@ impl WordApp { if matches!(id, "insert.picture" | "insert.icon") { self.select_inserted_picture(&r); } - // review.spelling only moves the selection to the next issue; without this, the ribbon's - // Spelling & Grammar and Editor buttons looked broken — just a selection with no way to - // see or apply a suggestion (#3340). Reuse the context menu's suggestions, as Shift+F10 - // already does for the word at the caret. - if id == "review.spelling" - && let Ok(v) = &r - && v.get("done").and_then(serde_json::Value::as_bool) != Some(true) - { - self.canvas.context_menu_from_keys = true; + // User proofing opens a persistent correction form; scripts keep the engine's + // structured result. Right-click and Shift+F10 retain their compact suggestions. + if matches!(id, "review.spelling" | "review.editor") && let Ok(v) = &r { + self.dialog = Some(dialogs::Dialog::Review(Box::new(dialogs_review::ReviewDialog::Spelling(Box::new( + dialogs_spelling::SpellingForm::new(self, v.clone()), + ))))); } let r = system_clipboard::explain(self, id, r); // Check Accessibility shows its findings in the Accessibility pane, kept up to date there. diff --git a/docs/gaps.md b/docs/gaps.md index dd16596f79..7b66eeb1c0 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where WordCraft falls short of Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (persistent spelling and grammar correction dialog); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Highlight update:** 2026-10-25 · Highlight shortcut toggles the last chosen colour off when the whole selection has it; explicit colour commands remain setters (#4085). Readiness estimates unchanged. > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** trivial (#7: Custom Document Properties dialog, #2982; previously trivial: egui 0.37 migration: the interface catalogs stay shared and pinned by `craft-i18n.rev` at `9205108cfdced5177706264038848b68b2422d2b` (27 languages, English fallback); input-method handling has new regressions but managed CI passed, and native IME acceptance plus native-speaker catalog review remain open; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: small follow-ups from the field, table and AutoFormat batch, #4232; previously trivial: highlight target includes selected paragraph marks, #4085; previously trivial: full-target highlight check beyond 10,000 runs, #4085; previously trivial: Exact-spacing table contact uses the specified line height rather than paragraph-mark metrics (#3977); previously trivial: cu20we: restrict pre-spacing float origins to paragraph anchors; document cap helpers and regressions; previously trivial: a command that changes nothing is not an edit: no undo step, no unsaved changes, no relayout, and the redo stack survives it; previously minor: #8 narrowed: Spanish proofing (Spain, Mexico, Latin America, Uruguay) from the installed Hunspell dictionaries, #3360, #3553; previously trivial: gap 10: group connector rerouting preserves transformed free and unresolved endpoints under nonuniform scaling; obstacle routing still missing; readiness estimates unchanged; previously trivial: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: the ranked gap list held two rows each for #19, #20 and #21; each gap appears once now; a document with many relationships opens in the time its part takes, #3411; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 @@ -70,7 +70,7 @@ Text context menus now include the existing formatting mini toolbar (#2959), inc | 2 | **Word's native ink is not drawn from its strokes**; OLE objects show only their picture | B | Every native ink form is preserved: drawing-level ink with its optional picture fallback, direct run-level content parts, and ink members of groups and drawing canvases (kept in their place among the members, drawn from their fallback picture). Drawing canvases are read and written (#344); group shapes (`wpg`) read and written (#267). Charts and SmartArt are drawn (#292) and, with OLE objects, written back with all their parts on save (#319) | Pen notes from Word show only as their picture and can't be edited; OLE objects can't be opened | 45–70 to render and edit charts and SmartArt (#13) | [file-format-parity.md](file-format-parity.md) | | 5 | **Content controls** landed (#381): `w:sdt` kept, shown and edited (Developer tab, Design Mode, Properties). Move tracking landed (#400): `w:moveFrom`/`w:moveTo` with their named ranges round-trip, a tracked cut and paste of a paragraph or sentence records a move, either end accepts/rejects both, green double strike/underline markup and "Moved from/to" balloons; Track Changes Options has the Moves rows (Moved from / Moved to mark and colour) and `w:doNotTrackMoves` (Track moves off: a cut and paste is a deletion and an insertion) round-trips; still missing: drag-and-drop moves (no text drag yet). Formatting revisions landed (#41): `w:rPrChange`/`w:pPrChange`/`w:tblPrChange`/`w:trPrChange`/`w:tcPrChange`/`w:sectPrChange`/`w:numberingChange` round-trip, are recorded while tracking, accepted/rejected and shown in balloons; still missing: tracked style-definition changes (table, row, cell and section property changes now record in WordCraft too, #41 follow-up). Opaque custom XML backing stores now survive saves; binding evaluation remains absent. Replace All now checks content-control locks before editing any match; a refused batch preserves the document and undo history. | B | List-label pitch corrected in synthetic metric controls; Issues #41, #400; source grep | Legal and editorial review workflows; templates with form controls | 5–8 | [file-format-parity.md](file-format-parity.md) | | 6 | **Print dialog depth**: desktop printer choice, copies, collation, duplex and grayscale submit through craft-print; File › Print retains preview, page ranges and odd/even (#641) and scales pages to another paper size (#2108); driver tray/options and pages per sheet remain; web prints through the browser (#209) | B | `ui.print` → `apps/wordcraft/src/print.rs`; issue #15 | Everyone who prints | 5–8 | [hardware-parity.md](hardware-parity.md) | -| 7 | **Dialog depth**: 61 modal dialogs vs Word's ~100 (Custom Document Properties from File › Info, #2982; Drop Cap Options matches Word's menu and dialog; Split Cells and Convert Text to Table match Word's observed limits and defaults; Format Page Numbers carries Word's number formats and Continue from previous section / Start at, #1113) | B | `Dialog` enum in `crates/ui-egui/src/dialogs.rs`; Cell Margins (#1960) now opens with current defaults and applies only confirmed edits | Power users reach for dialogs the ribbon alone doesn't cover | 11–19 | [ui-parity.md](ui-parity.md) | +| 7 | **Dialog depth**: persistent spelling/grammar correction form added (shared review commands, current-story traversal; see [spelling-review.md](spelling-review.md)); 61 modal dialogs vs Word's ~100 (Custom Document Properties from File › Info, #2982; Drop Cap Options matches Word's menu and dialog; Split Cells and Convert Text to Table match Word's observed limits and defaults; Format Page Numbers carries Word's number formats and Continue from previous section / Start at, #1113) | B | `Dialog` enum in `crates/ui-egui/src/dialogs.rs`; Cell Margins (#1960) now opens with current defaults and applies only confirmed edits | Power users reach for dialogs the ribbon alone doesn't cover | 11–19 | [ui-parity.md](ui-parity.md) | | 9 | **Password-protected `.doc` files with an AES header in the Word 97-2003 format** can't be opened: [MS-OFFCRYPTO] defines AES only for encrypted packages, so these are refused with a clear message. Encrypted packages open under any Word extension (`.docx`, `.doc`, `.dot`…) with either agile (Word 2010+) or Standard (Word 2007, AES-ECB) encryption; protected `.docx` saves with agile encryption (#55, #281, gap 9) | | `crates/docx/src/crypt.rs` (agile and Standard); `crates/docbin/src/crypt.rs` opens XOR/RC4/RC4 CryptoAPI `.doc` with a password (#1079). All synthetic fixtures; untested on Word-written files | Corporate and legal users with old files | 0 (refused by design; nothing left to build) | [file-format-parity.md](file-format-parity.md) | | 8 | **Proofing covers English, Polish, Portuguese, Italian, Croatian, Turkish, Russian, Czech, Indonesian and Spanish only**; no dictionaries or grammar for the other ~41 languages Word proofs (text in them is left unmarked), and no hyphenation for them except Bulgarian and German | | `crates/proof` `ProofLang::PROOFED`: English (American, British-spelling and Canadian, each flagging the other's spellings, #1281), Polish, Portuguese (pt-BR via the installed VERO dictionary, #797), Italian (it-IT via the installed Hunspell dictionary), Croatian (hr-HR, 2026-10-13), Turkish (tr_TR spelling, grammar, TDK hyphenation, 2026-10-13) Russian (the installed `ru_RU` dictionary, 2026-10-12) Indonesian (the installed `id_ID` dictionary, #498, 2026-10-16) and Spanish (es-ES, es-MX, es-419 and es-UY via the installed RLA-ES dictionaries, Uruguayan `vos` forms, #3360, #3553, 2026-10-22), a language per run; Bulgarian (#826) and German (TeX patterns, 2026-10-14) have hyphenation only; open PRs add Danish (#723), Russian (#894) and Czech (#1048); Word ships 120 proofing tools; issues #25, #40, #100 open | Every writer outside English, Polish, Portuguese, Italian, Croatian, Turkish, Russian, Czech, Indonesian and Spanish | 20–40 | [localization-parity.md](localization-parity.md) | | 10 | **Objects**: text in a freely rotated text box (free rotation and flips landed, #332; live rotation preview while dragging fixed for pictures and shapes without text stories, #2277; text direction, alignment and linked text boxes landed, #369; contour wrap and wrap side landed, #354), shape effects, WordArt 3-D effects and connector routing around shapes (WordArt effects and warps and glued connectors landed, #416), dragging members inside a Drawing Canvas (the canvas landed, #344) | | [parity-checklist.md](parity-checklist.md) (Shape Format 11/11, Layout 22/22); group and ungroup landed (#267) | Newsletters, flyers, reports with diagrams | 20–30 | [target-app-parity.md](target-app-parity.md) | @@ -150,6 +150,7 @@ Small follow-ups from the 2026-10-25 field, table and AutoFormat batch (#4232): | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Persistent Spelling & Grammar form uses shared language-aware review commands; Change, Ignore Once/All and Add to Dictionary advance without wrapping; stale targets are refused; readiness estimates unchanged | | 2026-10-22 | trivial | View windows: several open documents and Switch Windows in the engine | | 2026-10-25 | trivial | egui 0.37 migration: interface catalogs remain pinned by `craft-i18n.rev` (`9205108…`; 27 languages, English fallback); IME surrounding deletions and composition resets are implemented with regressions, but managed CI passed; native IME acceptance and native-speaker catalog review remain open; no gap or readiness change | | 2026-10-17 | trivial | Gap 9 narrowed: Standard (Word 2007, AES-ECB) encrypted packages open with a password under any Word extension; AES headers in Word 97-2003 files are outside the public spec and refused with a clear message; estimate 3–5 → 0 h, since the remaining AES-in-`.doc` case is refused by design | diff --git a/docs/spelling-review.md b/docs/spelling-review.md new file mode 100644 index 0000000000..a00313c9a3 --- /dev/null +++ b/docs/spelling-review.md @@ -0,0 +1,24 @@ +# Spelling and grammar correction form + +> **Last updated:** 2026-10-26 · **Status:** implementation proposed; hosted checks and visual acceptance must be verified before integration. + +Review › Spelling & Grammar, Home › Editor and F7 open a persistent correction form. The issue's text, explanation and proofing language appear above the suggestions. Choose a suggestion or type replacement text in Change to, then choose Change. Each replacement uses the existing review command and is one undoable document edit. Ignore Once advances without changing the document or dictionaries. Ignore All lasts for the session; Add to Dictionary uses the existing personal dictionary. Those two actions are offered only for spelling issues. + +The pass begins at the next issue from the current selection and continues to the end of the current story (body, header, note or other active text). It does not traverse all document stories or restrict itself to a selected range. Check Again starts another search with wrapping; ignored-once occurrences can be revisited. Completion stays visible until Close or Escape. The compact right-click/Shift+F10 menu is retained. + +The form consumes language-aware engine results. It works with the existing proofing languages and is independent of French contribution #3185; adding an interface does not install a dictionary or add grammar rules. Interface labels use the shared translation lookup and fall back to English when a catalog lacks a key. No release date or completeness increase is implied. + +If another command changes the document body or selection while the form is open, an old suggestion is refused. Check Again obtains a fresh target. Read-only/protected document commands retain the engine's existing checks and display refusals in the form. + +## Verification + +- Engine regression: one- and two-paragraph navigation, forward-only completion and explicit wrapping. +- UI regressions: suggestion application and undo, Ignore Once progress/completion, Ignore All, personal dictionary addition, stale selection/body refusal, no-issue completion and Escape. +- Offscreen preview: the ignored spelling_review_dialog_preview test writes light/dark PNGs; inspect them before integration. +- Required repository gate: cargo xtask ci. Cargo is unavailable on the preparation PC, so hosted checks are required; test definitions alone are not evidence of success. + +## Revision history + +| Date | Change | Summary | +|---|---|---| +| 2026-10-26 | minor | Separate language-independent correction-form proposal and verification scope | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 20a5e62475..b2858e1e92 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity with Microsoft Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** trivial (Custom Document Properties dialog from File › Info › Properties › Advanced Properties…, #2982; previously trivial: View › Window › New Window opens another window on the document, each with its own caret and view; previously trivial: mail merge: First Record, Last Record and a record number box in Preview Results; Finish & Merge asks which records to merge or print, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: read only and comments-only protection grey out edits; previously trivial: the Font dialog previews "NaN" or "inf" in the Size box at 12 pt, #3663; previously trivial: File › Options disables the User name field while Restrict Editing is active; previously trivial: while editing a header or footer, a click in the other one edits it there without scrolling; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Insert Table of Figures dialog: any caption label, #3013; previously trivial: Home › Underline drop-down shows its lines, #2738; previously trivial: the text context menu includes the formatting mini toolbar, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: the scrollbar page tip stays on one line as the page number grows, #3341; previously trivial: File › Home and New: templates and recent documents are buttons named for screen readers, #3176; previously trivial: forms protection greys out the Developer tab's control buttons and Properties; previously trivial: AltGr doesn't open KeyTips, #2388; previously trivial: closing the header or footer puts the caret back where it was in the body, #3331; previously trivial: Format Page Numbers dialog: number format and Continue from previous section / Start at, #1113; previously trivial: inline pictures drag to a dashed insertion cursor and stay inline, #2233, #2234; previously trivial: Save Changes prompt: a text file saved over itself closes, #2376; previously trivial: the equation being edited is framed as in Word: an outline as tall as its line, a grip, an options strip that opens its menu, and its current argument shaded; previously trivial: the table handles show while the caret is in the table, #2428; previously trivial: legacy menu keys, #1697; previously trivial: Shape Fill › Gradient and the Format Shape pane, #1500; previously trivial: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: the text pointer over the page is drawn black or white on Windows, #1746; previously trivial: a click in the header/footer band from the body no longer scrolls to the body text, #2103; previously trivial: File › Recover follows the app theme, #3243; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: dragging the vertical scrollbar names the page at the top of the window, #2551; previously trivial: Add Font imports every selected font file, #1789); previously trivial (File › Close keeps the window, #1712; previously trivial: the Spike: Ctrl+F3 and Ctrl+Shift+F3; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: the tab strip never draws tabs over Comments / Editing / Share, #2026; previously trivial: Columns on selected text, #2024; previously trivial: Different First Page headers reachable from page 1, #2826; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: Read Mode keeps the keyboard, shows whole columns and keeps text clear of the page-turn bar, #1772); previously trivial: font list entries of tall-ascent fonts sit in their row, not cut off, #2570); previously trivial: Split Cells and Convert Text to Table dialogs follow Word's merged-cell limits and Initial column width, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout text fills the window width, #1876; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717)); previously trivial: comment balloons readable in the dark theme, #2045; previously trivial: Recently Used Fonts section at the top of the font list, #2068; previously trivial: web preferences kept between visits, #2132; previously trivial: dialog close control: a Windows-style box at the title bar's right end, the whole box clickable, red on hover, #1885); previously trivial (Modify Table Style formats every table style part, #714); previously trivial: a click on the ribbon chrome hands the keyboard to the page in the same frame, #1093 follow-up; previously trivial: Page Setup vertical alignment, #1320; previously trivial: the app opens on the Home tab every launch, as in Word; previously trivial: no document behind the Start screen, #1129; previously trivial: equation pictures share a few textures, #1002; previously trivial: selection bar in the left margin, #856; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously major: first version; ribbon, dialogs, shortcuts and on-canvas interaction measured from `crates/ui-egui` and the command registry) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (persistent spelling and grammar correction dialog); previously trivial (Custom Document Properties dialog from File › Info › Properties › Advanced Properties…, #2982; previously trivial: View › Window › New Window opens another window on the document, each with its own caret and view; previously trivial: mail merge: First Record, Last Record and a record number box in Preview Results; Finish & Merge asks which records to merge or print, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: read only and comments-only protection grey out edits; previously trivial: the Font dialog previews "NaN" or "inf" in the Size box at 12 pt, #3663; previously trivial: File › Options disables the User name field while Restrict Editing is active; previously trivial: while editing a header or footer, a click in the other one edits it there without scrolling; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Insert Table of Figures dialog: any caption label, #3013; previously trivial: Home › Underline drop-down shows its lines, #2738; previously trivial: the text context menu includes the formatting mini toolbar, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: the scrollbar page tip stays on one line as the page number grows, #3341; previously trivial: File › Home and New: templates and recent documents are buttons named for screen readers, #3176; previously trivial: forms protection greys out the Developer tab's control buttons and Properties; previously trivial: AltGr doesn't open KeyTips, #2388; previously trivial: closing the header or footer puts the caret back where it was in the body, #3331; previously trivial: Format Page Numbers dialog: number format and Continue from previous section / Start at, #1113; previously trivial: inline pictures drag to a dashed insertion cursor and stay inline, #2233, #2234; previously trivial: Save Changes prompt: a text file saved over itself closes, #2376; previously trivial: the equation being edited is framed as in Word: an outline as tall as its line, a grip, an options strip that opens its menu, and its current argument shaded; previously trivial: the table handles show while the caret is in the table, #2428; previously trivial: legacy menu keys, #1697; previously trivial: Shape Fill › Gradient and the Format Shape pane, #1500; previously trivial: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: the text pointer over the page is drawn black or white on Windows, #1746; previously trivial: a click in the header/footer band from the body no longer scrolls to the body text, #2103; previously trivial: File › Recover follows the app theme, #3243; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: dragging the vertical scrollbar names the page at the top of the window, #2551; previously trivial: Add Font imports every selected font file, #1789); previously trivial (File › Close keeps the window, #1712; previously trivial: the Spike: Ctrl+F3 and Ctrl+Shift+F3; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: the tab strip never draws tabs over Comments / Editing / Share, #2026; previously trivial: Columns on selected text, #2024; previously trivial: Different First Page headers reachable from page 1, #2826; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: Read Mode keeps the keyboard, shows whole columns and keeps text clear of the page-turn bar, #1772); previously trivial: font list entries of tall-ascent fonts sit in their row, not cut off, #2570); previously trivial: Split Cells and Convert Text to Table dialogs follow Word's merged-cell limits and Initial column width, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout text fills the window width, #1876; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717)); previously trivial: comment balloons readable in the dark theme, #2045; previously trivial: Recently Used Fonts section at the top of the font list, #2068; previously trivial: web preferences kept between visits, #2132; previously trivial: dialog close control: a Windows-style box at the title bar's right end, the whole box clickable, red on hover, #1885); previously trivial (Modify Table Style formats every table style part, #714); previously trivial: a click on the ribbon chrome hands the keyboard to the page in the same frame, #1093 follow-up; previously trivial: Page Setup vertical alignment, #1320; previously trivial: the app opens on the Home tab every launch, as in Word; previously trivial: no document behind the Start screen, #1129; previously trivial: equation pictures share a few textures, #1002; previously trivial: selection bar in the left margin, #856; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously major: first version; ribbon, dialogs, shortcuts and on-canvas interaction measured from `crates/ui-egui` and the command registry) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 Recovery discovery no longer interrupts an explicitly created or opened document. Older copies remain in File › Recover, whose buttons use the interface palette. See [local recovery](recovery.md). @@ -31,7 +31,7 @@ Document title opens Rename Document; the account badge opens Options › Genera | Modal dialogs | **56** (`Dialog` enum: Discard Recovery, Font, Enclose Characters, Paragraph, Tabs, Borders and Shading, Find, Go To, Insert Table, Table Properties, Page Setup, Link, Bookmark, Word Count, Zoom, Watermark, New Style, Modify Style, Manage Styles, Table Style, Commands, Icons, Templates and Add-ins, Organizer, Compare and Combine, AutoCorrect Options, Paste Special, About, Save Changes, Password, Encrypt with Password, Define New Multilevel List, Track Changes Options; Columns, Symbol with Special Characters, Field (#321); Field code (#339); mail merge Recipients (Filter and Sort, #778; Find in list and a merged-only view, #1576), Insert Merge Field, Find Recipient, Rules, Match Fields, Check for Errors; Caption with New Label and Numbering, Index, Mark Index Entry, Custom Table of Contents with Options and TOC styles, Source Manager, Create/Edit Source (#402); Cross-reference (#916); Line Numbers, Hyphenation with Manual Hyphenation, Language, Envelopes, Labels (#407); Page Setup has a Paper size list and custom width and height, opened from Layout › Size › More Paper Sizes… (#1433)) plus panes and menus for the rest | ~100 (Options panes…) | ~45% | 11–19 | | Task panes (Navigation, Styles, Comments, Reviewing, Format Picture/Shape, Thesaurus, Accessibility, Clipboard, Selection) | Navigation (long headings wrap onto further lines within the pane instead of expanding it, #759, #3015), Styles, Comments, Thesaurus, Restrict Editing, Accessibility (grouped findings, jump-to, fixes) and Alt Text (#386), Read Aloud, Zotero; Style Inspector (#236); Clipboard pane (#264) open | all | 70% | 4–6 | | Keyboard shortcuts | **152** commands bound in `CommandSpec::key`, 172 key combinations (counted from the registry on Linux, 2026-10-13); styles, spacing, symbols, fields, views, mail merge (#330); field keys Alt+F9, Shift+F9, Ctrl+Shift+F9, Ctrl+F11, Ctrl+Shift+F11, Shift+F11 (#339), Next Field on Alt+F1 and Alt+Shift+F1 (Word's other keys for it) since F11 went to full screen (#980; GNOME and KDE may keep Alt+F1 for their own overview or application menu, and then Next Field has no key there; Previous Field keeps Shift+F11); Alt+Shift+arrows (move paragraphs, promote/demote), Shift+F5 Go Back and Alt+X (#343); Outline view: Alt+Shift+Plus/Minus expand/collapse, Alt+Shift+1–9 and Alt+Shift+A Show Level (#493); Alt+Q to "Tell me" command search (#819, not on macOS); on macOS, Word for Mac's own keys: ⌘⇧S Save As, ⌘⌥E Insert Endnote, ⌘⌥A New Comment, ⌃H Replace, ⌘⌥R collapse the ribbon. The Spike: Ctrl+F3 cuts to it, Ctrl+Shift+F3 inserts it and empties it (⌘F3, ⌘⇧F3 on macOS). Missing: en dash on the numeric keypad Insert Hyperlink has Word's Link to: Existing File or Web Page / Place in This Document (top of the document, headings, bookmarks) (#1234). | ~250 default shortcuts | ~70% | 2–4 | -| Modal dialogs | **63** (`Dialog` enum: Discard Recovery, Font, Enclose Characters, Paragraph, Tabs, Borders and Shading, Find, Go To, Insert Table, Table Properties, Page Setup, Link, Bookmark, Word Count, Zoom, Watermark, New Style, Modify Style, Manage Styles, Table Style, Commands, Icons, Templates and Add-ins, Organizer, Custom Document Properties (File › Info › Properties › Advanced Properties › Custom, #2982), Compare and Combine, AutoCorrect Options, Paste Special, About, Save Changes, Password, Encrypt with Password, Define New Multilevel List, Track Changes Options; Columns, Symbol with Special Characters, Field (#321); Field code (#339); mail merge Recipients, Insert Merge Field, Find Recipient, Rules, Match Fields, Check for Errors; Caption with New Label and Numbering, Index, Mark Index Entry, Custom Table of Contents with Options and TOC styles, Table of Figures (#3013), Source Manager, Create/Edit Source (#402); Cross-reference (#916); Drop Cap with Position, Font, Lines to drop and Distance from text, opened from Insert › Drop Cap › Drop Cap Options… (#2601); Line Numbers, Hyphenation with Manual Hyphenation, Language, Envelopes, Labels (#407); Page Setup has a Paper size list and custom width and height, opened from Layout › Size › More Paper Sizes… (#1433); every dialog's close control is a box at the title bar's right end, the full title-bar height and 1.6× as wide, all of it clickable, red with a white ✕ on hover as a Windows caption button (#1885); Split Cells (columns, rows, merge before split; defaults follow the selection as in Word: twice its columns, its rows, Merge offered for several cells or one merged across columns; a merged cell splits into 1 to its height rows, a number that doesn't divide its height refused with a divisor message, more than its height with the range message; 1 × 1 changes nothing; parts split across columns are equal and other rows keep their grid lines) and Convert Text to Table (live column count, read-only rows; Initial column width Auto or 0 to 22 in, AutoFit to contents/window; Paragraphs/Commas/Tabs/Other "-" separators) (#2592); Size, from Picture Format › Size's launcher: height and width in the interface unit (a size typed in any unit, `300 px` too), scale of the original or the opening size, Lock aspect ratio, the original size with its pixels and Reset (#2573); Format Page Numbers, from Insert › Page Number ▸ Format Page Numbers…: the number format (1, 2, 3 / I, II, III / i, ii, iii / A, B, C / a, b, c) and Continue from previous section / Start at, #1113; Finish & Merge records (All, Current record, or From and To by record number), opened from Finish & Merge › Edit Individual Documents… and Print Documents… (#3028)) plus panes and menus for the rest | ~100 (Options panes…) | ~45% | 11–19 | +| Modal dialogs | **64** (`Dialog` enum: Discard Recovery, Font, Enclose Characters, Paragraph, Tabs, Borders and Shading, Find, Go To, Insert Table, Table Properties, Page Setup, Link, Bookmark, Word Count, Zoom, Watermark, New Style, Modify Style, Manage Styles, Table Style, Commands, Icons, Templates and Add-ins, Organizer, Spelling & Grammar, Custom Document Properties (File › Info › Properties › Advanced Properties › Custom, #2982), Compare and Combine, AutoCorrect Options, Paste Special, About, Save Changes, Password, Encrypt with Password, Define New Multilevel List, Track Changes Options; Columns, Symbol with Special Characters, Field (#321); Field code (#339); mail merge Recipients, Insert Merge Field, Find Recipient, Rules, Match Fields, Check for Errors; Caption with New Label and Numbering, Index, Mark Index Entry, Custom Table of Contents with Options and TOC styles, Table of Figures (#3013), Source Manager, Create/Edit Source (#402); Cross-reference (#916); Drop Cap with Position, Font, Lines to drop and Distance from text, opened from Insert › Drop Cap › Drop Cap Options… (#2601); Line Numbers, Hyphenation with Manual Hyphenation, Language, Envelopes, Labels (#407); Page Setup has a Paper size list and custom width and height, opened from Layout › Size › More Paper Sizes… (#1433); every dialog's close control is a box at the title bar's right end, the full title-bar height and 1.6× as wide, all of it clickable, red with a white ✕ on hover as a Windows caption button (#1885); Split Cells (columns, rows, merge before split; defaults follow the selection as in Word: twice its columns, its rows, Merge offered for several cells or one merged across columns; a merged cell splits into 1 to its height rows, a number that doesn't divide its height refused with a divisor message, more than its height with the range message; 1 × 1 changes nothing; parts split across columns are equal and other rows keep their grid lines) and Convert Text to Table (live column count, read-only rows; Initial column width Auto or 0 to 22 in, AutoFit to contents/window; Paragraphs/Commas/Tabs/Other "-" separators) (#2592); Size, from Picture Format › Size's launcher: height and width in the interface unit (a size typed in any unit, `300 px` too), scale of the original or the opening size, Lock aspect ratio, the original size with its pixels and Reset (#2573); Format Page Numbers, from Insert › Page Number ▸ Format Page Numbers…: the number format (1, 2, 3 / I, II, III / i, ii, iii / A, B, C / a, b, c) and Continue from previous section / Start at, #1113; Finish & Merge records (All, Current record, or From and To by record number), opened from Finish & Merge › Edit Individual Documents… and Print Documents… (#3028)) plus panes and menus for the rest | ~100 (Options panes…) | ~45% | 11–19 | | Task panes (Navigation, Styles, Comments, Reviewing, Format Picture/Shape, Thesaurus, Accessibility, Clipboard, Selection) | Navigation (long headings wrap onto further lines within the pane instead of expanding it, #759, #3015), Styles, Comments (balloons and cards read in both themes, #2045), Thesaurus, Restrict Editing, Accessibility (grouped findings, jump-to, fixes) and Alt Text (#386), Read Aloud, Zotero; Style Inspector (#236); Clipboard pane (#264) open | all | 70% | 4–6 | | Context menus (right-click) | text, spelling suggestions, fields (Update Field, Edit Field…, Toggle Field Codes, #339; Update Field on a table of contents entry updates the table, #1197, and after a save and reopen, #2182); right-click selects a picture, shape or text box and offers Insert Caption… (#1230) and Wrap Text (styles, Move with Text / Fix Position on Page, #513); tables, the items following the selection as in Word (Insert, Delete › Cells, Columns, Rows or Table whatever is selected (#1198), Merge and Split Cells, Distribute, AutoFit, Text Direction, Insert Caption…, Table Properties…; no Border Styles yet) | rich per context | 65% | 2–4 | | Task panes (Navigation, Styles, Comments, Reviewing, Format Picture/Shape, Thesaurus, Accessibility, Clipboard, Selection) | Navigation (long headings wrap onto further lines within the pane instead of expanding it, #759, #3015), Styles, Comments (times in the user's own time zone, in the pane and the balloons, #2322, #2337; typed comment and reply text stays with its document, is written before Save, Export, Print, Close, New or Open, Escape discards it, and the fields are read-only where comments can't change, #1348), Thesaurus, Restrict Editing, Accessibility (grouped findings, jump-to, fixes) and Alt Text (#386), Read Aloud, Zotero; Style Inspector (#236); Clipboard pane (#264) open | all | 70% | 4–6 | @@ -78,10 +78,15 @@ Find selects the Results tab, including when it prefills selected text. Editing Footnote and endnote editing keeps body and note text at normal opacity, on light and dark pages. Headers and footers still dim outside their editing context (#2545). +## Spelling and grammar correction + +Review › Spelling & Grammar, Home › Editor and F7 open the same persistent correction form. It shows the issue and its language, suggestions and an editable replacement, with Change, Ignore Once, Ignore All and Add to Dictionary. The last two apply to spelling only. Each action advances without wrapping; completion stays visible. Right-click and Shift+F10 keep the compact suggestions menu. The form is shared by all proofing languages and does not add dictionaries or grammar rules. See [spelling-review.md](spelling-review.md) for scope and verification. + ## Revision history | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Persistent Spelling & Grammar form uses shared language-aware review commands; Change, Ignore Once/All and Add to Dictionary advance without wrapping; stale targets are refused; readiness estimates unchanged | | 2026-10-22 | trivial | View › Window › New Window opens another window on the document, focused and cascaded; two windows on one document are titled `Name:1` and `Name:2` (one window: the plain name). Each window keeps its own selection, scroll position, zoom, view mode, ruler and panes; the text, undo history, unsaved state and file are the document's, so an edit or Undo in either window shows in both, and a caret in text the other window deleted moves to the nearest place left. Closing one of a document's windows asks nothing; closing its last asks about unsaved changes, and quitting asks once per document. Switch Windows lists every window. The web app keeps one window | | 2026-10-22 | trivial | One native window per open document on the desktop (the web app keeps one window): New and Open open the document in a window of its own, focused and cascaded from the last; Open replaces an untouched blank document in its window and brings an already-open file's window forward; each window keeps its own ribbon tab, File page, dialogs, panes and composition; View › Window › Switch Windows lists the windows and brings the chosen one forward; File › Close and the window's close button close the window (asking about unsaved changes) and the window focused before comes forward, while the last window stays open with no document; quitting asks Save / Don't Save / Cancel about each document in its own window | | 2026-10-22 | trivial | Several open documents in one window (one window per document comes next): View › Window › Switch Windows lists the open documents, the shown one marked and unsaved ones dotted, and shows the one chosen, with its own scroll position, zoom, comment drafts and AutoSave file; File › New, Open, Envelopes, Labels, Finish & Merge and personal templates open beside the open documents without asking about unsaved changes; File › Close shows the next open document; the window's close button asks Save / Don't Save / Cancel about each document with unsaved changes, showing it, and Cancel keeps the app open. Untitled documents are numbered (Document2) in the interface language | From f6f9d65b0e9fd33a5997eeafbd02fd959ba9e9f7 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:16:49 +0100 Subject: [PATCH 011/308] M7: follow the observed Word for Windows proofing pane Observe F7 in the installed Word with synthetic text, then replace the central correction form with a right-side task pane. Show sentence context, directly clickable suggestion cards and ignore/dictionary links. Use the existing pane host, palette and original icons; copy no Microsoft assets. Keep per-window state and clear targets on document switch. Adapt UI tests, pseudo-localization exemptions and light/dark previews. Record observed measurements and remaining feature differences. Local cargo xtask ci was attempted but cargo is absent; exact-head hosted checks and visual comparison remain required. --- .github/workflows/spelling-review.yml | 4 +- ROADMAP.md | 6 +- crates/ui-egui/src/control.rs | 1 + crates/ui-egui/src/dialogs_review.rs | 4 - crates/ui-egui/src/dialogs_spelling.rs | 262 ------------------ crates/ui-egui/src/documents.rs | 1 + crates/ui-egui/src/i18n/audit.rs | 4 + crates/ui-egui/src/lib.rs | 14 +- crates/ui-egui/src/panes.rs | 12 +- crates/ui-egui/src/proofing_pane.rs | 369 +++++++++++++++++++++++++ crates/ui-egui/src/window_ui.rs | 4 + docs/gaps.md | 6 +- docs/spelling-review.md | 29 +- docs/ui-parity.md | 10 +- 14 files changed, 430 insertions(+), 296 deletions(-) delete mode 100644 crates/ui-egui/src/dialogs_spelling.rs create mode 100644 crates/ui-egui/src/proofing_pane.rs diff --git a/.github/workflows/spelling-review.yml b/.github/workflows/spelling-review.yml index cd9e7981a0..5fcea383f8 100644 --- a/.github/workflows/spelling-review.yml +++ b/.github/workflows/spelling-review.yml @@ -3,8 +3,8 @@ name: Spelling review preview on: pull_request: paths: - - 'crates/ui-egui/src/dialogs_spelling.rs' - - 'crates/ui-egui/src/dialogs_review.rs' + - 'crates/ui-egui/src/proofing_pane.rs' + - 'crates/ui-egui/src/panes.rs' - 'crates/engine/src/cmd/review.rs' - '.github/workflows/spelling-review.yml' workflow_dispatch: diff --git a/ROADMAP.md b/ROADMAP.md index dc9e3fe124..95612af684 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (persistent spelling and grammar correction dialog); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-18 · **Change:** trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -311,7 +311,7 @@ Imported Word 97–2003 `.doc`/`.dot` files now ask for a supported DOCX target | 2026-10-17 | Non-overlapping body floating tables clear header tables. Original native controls distinguish never-overlap from allowed overlap. | | 2026-10-14 | Comment text being typed is no longer lost or misplaced (#1348): an edit or reply the document refuses (for example read-only protection) keeps the typed text in its field and says why on the status line; drafts belong to their document, so after New or Open a comment with the same number shows its own text; Save, Export, Print, Close, New and Open write pending comment text first; if the document refuses it, Save, Export and Print carry on with the stored comments and show why on the status line (a refused draft is dropped when Save is the only change), while Close, New and Open show the Save Changes prompt (Don't Save drops text the document refuses); Escape discards what was typed; in a document protected as read only or for forms the comment fields can't be typed in | |---|---| -| 2026-10-26 | Spelling & Grammar opens a persistent correction form with suggestions and editable replacement text. Changing, ignoring or adding a word advances to the next issue in the current story; completion stays visible. Right-click keeps its compact menu. | +| 2026-10-26 | Spelling & Grammar opens a right-side Proofing task pane observed in Word for Windows: sentence context, directly clickable suggestion cards and ignore/dictionary actions. Current-story traversal and stale-target checks are retained. Right-click keeps its compact menu. | | 2026-10-18 | Explicit New/Open keeps the requested document visible when startup recovery discovery finishes; older copies stay available. Recover uses the regular theme. See docs/recovery.md. | | 2026-10-15 | A floating picture or shape in a table cell set to align centre or bottom no longer lands half its height above its line (#1887); it starts at the anchor line | | 2026-10-18 | Picture Format › Size (#2573): width and height in the interface unit, and a size typed in any unit works, pixels too (`300 px` keeps the picture's proportions, #1901), applied once typed (Enter, Tab or a click elsewhere; Esc cancels) in one undo step. The group's launcher opens a Size dialog: height and width, scale of the original size (or of the size it opened with), Lock aspect ratio, the original size with its pixels, and Reset. `picture.size {"original": true, "scale"?}` sizes from the original; sizes past 4000 pt that keep the proportions shrink as a whole. | @@ -644,7 +644,7 @@ Footer page-table height and body wrapping are corrected. Other pagination gaps | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Persistent Spelling & Grammar form uses shared language-aware review commands; Change, Ignore Once/All and Add to Dictionary advance without wrapping; stale targets are refused; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; readiness estimates unchanged | | 2026-10-22 | trivial | Progress log: one native window per open document | | 2026-10-22 | trivial | Progress log: every open document keeps its own recovery copy | | 2026-10-22 | trivial | Progress log: several open documents in the desktop window | diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index efa28f266e..b26eed8ee6 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -56,6 +56,7 @@ pub fn inspect(app: &mut WordApp, ctx: &egui::Context) -> Value { json!({ "ui": app.prefs(), "view": app.session.view, + "proofing": app.spelling_review, "dialog": app.dialog.as_ref().map(|d| serde_json::to_value(d).unwrap_or_default()), "window": [r.width(), r.height()], "canvasRect": app.canvas.canvas_rect.map(|c| [c.left(), c.top(), c.width(), c.height()]), diff --git a/crates/ui-egui/src/dialogs_review.rs b/crates/ui-egui/src/dialogs_review.rs index 5cb3d71645..b1f38ccb9e 100644 --- a/crates/ui-egui/src/dialogs_review.rs +++ b/crates/ui-egui/src/dialogs_review.rs @@ -17,7 +17,6 @@ use crate::theme::{Tokens, semibold}; #[derive(Clone, Debug, Serialize)] #[serde(tag = "form", rename_all = "camelCase")] pub enum ReviewDialog { - Spelling(Box), Compare(CompareForm), AutoCorrect(Box), } @@ -25,7 +24,6 @@ pub enum ReviewDialog { impl ReviewDialog { pub fn name(&self) -> &'static str { match self { - ReviewDialog::Spelling(_) => "spelling", ReviewDialog::Compare(f) if f.combine => "combine", ReviewDialog::Compare(_) => "compare", ReviewDialog::AutoCorrect(_) => "autoCorrect", @@ -35,7 +33,6 @@ impl ReviewDialog { /// The window title (English; the caller translates it). pub fn title(&self) -> &'static str { match self { - ReviewDialog::Spelling(_) => "Spelling & Grammar", ReviewDialog::Compare(f) if f.combine => "Combine Documents", ReviewDialog::Compare(_) => "Compare Documents", ReviewDialog::AutoCorrect(_) => "AutoCorrect", @@ -60,7 +57,6 @@ pub fn open(name: &str, app: &mut WordApp) -> Option { /// Draws the dialog; returns true to close it. pub fn body(app: &mut WordApp, ui: &mut Ui, d: &mut ReviewDialog) -> bool { match d { - ReviewDialog::Spelling(f) => crate::dialogs_spelling::body(app, ui, f), ReviewDialog::Compare(f) => compare_ui(app, ui, f), ReviewDialog::AutoCorrect(f) => autocorrect_ui(app, ui, f), } diff --git a/crates/ui-egui/src/dialogs_spelling.rs b/crates/ui-egui/src/dialogs_spelling.rs deleted file mode 100644 index 9d3fa574cf..0000000000 --- a/crates/ui-egui/src/dialogs_spelling.rs +++ /dev/null @@ -1,262 +0,0 @@ -//! A persistent proofing form over the shared review commands, independent of language data. - -use egui::Ui; -use serde::Serialize; -use serde_json::{Value, json}; -use wordcraft_engine::Selection; - -use crate::WordApp; - -#[derive(Clone, Debug, Serialize)] -pub struct SpellingForm { - issue: Value, - replacement: String, - message: String, - #[serde(skip)] - document: u64, - #[serde(skip)] - epoch: u64, - #[serde(skip)] - selection: Selection, -} - -impl SpellingForm { - pub fn new(app: &WordApp, issue: Value) -> Self { - let replacement = issue.get("suggestions").and_then(Value::as_array).and_then(|a| a.first()).and_then(Value::as_str).unwrap_or("").into(); - Self { - issue, - replacement, - message: String::new(), - document: app.session.document_id(), - epoch: app.session.ime_body_epoch(), - selection: app.session.sel.clone(), - } - } - - fn done(&self) -> bool { - self.issue.get("done").and_then(Value::as_bool) == Some(true) - } - - // Other windows and commands can change a target while this form is open. - // Never apply a cached suggestion to a different selection or document body. - fn current(&self, app: &mut WordApp) -> bool { - self.document == app.session.document_id() - && self.epoch == app.session.ime_body_epoch() - && self.selection == app.session.sel - && app.session.run("review.suggestions", &json!({})).ok().as_ref() == Some(&self.issue) - } - - fn next(&mut self, app: &mut WordApp, wrap: bool) { - match app.execute("review.spelling", json!({"wrap": wrap})) { - Ok(issue) => *self = Self::new(app, issue), - Err(e) => self.message = e, - } - } - - fn act(&mut self, app: &mut WordApp, command: Option<&str>) { - if !self.current(app) { - self.message = tl!("The document or selection changed. Check again to continue.").into(); - return; - } - if let Some(command) = command { - let params = if command == "review.applySuggestion" { json!({"text": self.replacement}) } else { json!({}) }; - if let Err(e) = app.run(command, params) { - self.message = e; - return; - } - } - self.next(app, false); - } -} - -/// Changing or ignoring advances within the same form; Close and Escape dismiss it. -pub fn body(app: &mut WordApp, ui: &mut Ui, form: &mut SpellingForm) -> bool { - ui.set_width(420.0); - if form.done() { - ui.label(tl!("The spelling and grammar check is complete.")); - } else { - let spelling = form.issue.get("kind").and_then(Value::as_str) == Some("spelling"); - ui.horizontal_wrapped(|ui| { - ui.label(tl!(if spelling { "Spelling" } else { "Grammar" })); - ui.label(form.issue.get("lang").and_then(Value::as_str).unwrap_or("")); - }); - ui.label(egui::RichText::new(form.issue.get("text").and_then(Value::as_str).unwrap_or("")).strong()); - ui.label(tl!(form.issue.get("message").and_then(Value::as_str).unwrap_or(""))); - ui.add_space(8.0); - ui.label(tl!("Suggestions")); - let suggestions: Vec<&str> = form.issue.get("suggestions").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).collect(); - egui::ScrollArea::vertical().id_salt("spelling_suggestions").max_height(120.0).show(ui, |ui| { - if suggestions.is_empty() { - ui.label(egui::RichText::new(tl!("(no suggestions)")).italics()); - } - for suggestion in &suggestions { - if ui.selectable_label(form.replacement == *suggestion, *suggestion).clicked() { - form.replacement = (*suggestion).into(); - } - } - }); - let label = ui.label(tl!("Change to:")); - ui.add(egui::TextEdit::singleline(&mut form.replacement).char_limit(1024).desired_width(400.0)).labelled_by(label.id); - let can_change = (!form.replacement.is_empty() || suggestions.contains(&"")) && crate::widgets::enabled(app, "review.applySuggestion"); - let mut action = None; - ui.horizontal_wrapped(|ui| { - if ui.add_enabled(can_change, egui::Button::new(tl!("Change"))).clicked() { - action = Some(Some("review.applySuggestion")); - } - if ui.button(tl!("Ignore Once")).clicked() { - action = Some(None); - } - if spelling { - if ui.button(tl!("Ignore All")).clicked() { - action = Some(Some("review.ignoreAll")); - } - if ui.button(tl!("Add to Dictionary")).clicked() { - action = Some(Some("review.addToDictionary")); - } - } - }); - if let Some(command) = action { - form.act(app, command); - } - } - if !form.message.is_empty() { - ui.colored_label(ui.visuals().error_fg_color, &form.message); - } - ui.add_space(8.0); - let mut close = false; - ui.horizontal(|ui| { - if ui.button(tl!("Check Again")).clicked() { - form.next(app, true); - } - close = ui.button(tl!("Close")).clicked(); - }); - close || crate::dialogs::cancel_key_pressed(ui) -} - -#[cfg(test)] -mod tests { - use egui_kittest::kittest::Queryable; - use wordcraft_doc::{Document, Pos, StoryRef}; - use wordcraft_engine::Session; - - use super::*; - use crate::{Services, dialogs::Dialog, dialogs_review::ReviewDialog}; - - fn app(text: &str) -> WordApp { - WordApp::new(Session::new(Document::from_text(text)), Services::default()) - } - - fn frame(ui: &mut Ui, app: &mut WordApp) { - let ctx = ui.ctx().clone(); - app.logic(&ctx); - app.ui(ui); - } - - fn harness(text: &str) -> egui_kittest::Harness<'static, WordApp> { - let mut app = app(text); - app.run("review.spelling", json!({})).unwrap(); - let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(frame, app); - h.run_steps(6); - h - } - - fn form(app: &WordApp) -> &SpellingForm { - match &app.dialog { - Some(Dialog::Review(d)) => match &**d { - ReviewDialog::Spelling(f) => f, - _ => panic!("spelling form expected"), - }, - _ => panic!("spelling dialog expected"), - } - } - - #[test] - fn spelling_review_dialog_changes_a_suggestion_and_undo_restores_the_word() { - let mut h = harness("The quick brwn fox."); - assert!(!h.state().canvas.context_menu_open); - h.get_by_label("brown").click(); - h.run_steps(2); - h.get_by_label("Change").click(); - h.run_steps(3); - assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brown fox."); - assert!(form(h.state()).done()); - h.get_by_label("Close").click(); - h.run_steps(2); - assert!(h.state().dialog.is_none()); - h.state_mut().run("edit.undo", json!({})).unwrap(); - assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brwn fox."); - } - - #[test] - fn spelling_review_dialog_ignore_once_advances_and_finishes_without_changing_text() { - let text = "The brwn fox and brwn dog."; - let mut h = harness(text); - let first = h.state().session.sel.clone(); - h.get_by_label("Ignore Once").click(); - h.run_steps(3); - assert_ne!(h.state().session.sel, first); - assert_eq!(form(h.state()).issue["text"], "brwn"); - h.get_by_label("Ignore Once").click(); - h.run_steps(3); - assert!(form(h.state()).done()); - assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), text); - h.get_by_label("Check Again").click(); - h.run_steps(3); - assert_eq!(h.state().session.sel, first, "Ignore Once never adds a word to the dictionary"); - } - - #[test] - fn spelling_review_dialog_ignore_all_and_add_to_dictionary_advance() { - for (word, button) in [("zqproofingdialogignore", "Ignore All"), ("zqproofingdialogcustom", "Add to Dictionary")] { - let text = format!("The {word} fox and {word} dog."); - let mut h = harness(&text); - h.get_by_label(button).click(); - h.run_steps(3); - assert!(form(h.state()).done()); - assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), text); - assert!(h.state_mut().session.run("review.issues", &json!({})).unwrap().as_array().unwrap().is_empty()); - } - } - - #[test] - fn spelling_review_dialog_refuses_a_stale_selection_or_replaced_body() { - for replace in [false, true] { - let mut a = app("The brwn fox and brwn dog."); - let issue = a.execute("review.spelling", json!({})).unwrap(); - assert!(a.dialog.is_none(), "programmatic review calls never open a window"); - let mut f = SpellingForm::new(&a, issue); - f.replacement = "brown".into(); - if replace { - a.execute("document.setText", json!({"text": "The brwn fox and brwn dog."})).unwrap(); - } else { - a.session.sel = Selection::caret(Pos::body(0, 0)); - } - let before = a.session.doc.plain_text(StoryRef::Body); - f.act(&mut a, Some("review.applySuggestion")); - assert!(!f.message.is_empty()); - assert_eq!(a.session.doc.plain_text(StoryRef::Body), before); - } - } - - #[test] - fn spelling_review_dialog_shows_completion_without_issues_and_escape_closes() { - let mut h = harness("The quick brown fox."); - assert!(form(h.state()).done()); - h.key_press(egui::Key::Escape); - h.run_steps(2); - assert!(h.state().dialog.is_none()); - } - - #[test] - #[ignore = "offscreen visual review; requires a software or hardware graphics adapter"] - fn spelling_review_dialog_preview() { - let mut a = app("The quick brwn fox."); - a.run("review.spelling", json!({})).unwrap(); - let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1000.0, 700.0)).wgpu().build_ui_state(frame, a); - for (name, visuals) in [("light", egui::Visuals::light()), ("dark", egui::Visuals::dark())] { - h.ctx.set_visuals(visuals); - h.run_steps(6); - h.render().unwrap().save(format!("spelling-review-{name}.png")).unwrap(); - } - } -} diff --git a/crates/ui-egui/src/documents.rs b/crates/ui-egui/src/documents.rs index c843e19d2a..c7e4cd123f 100644 --- a/crates/ui-egui/src/documents.rs +++ b/crates/ui-egui/src/documents.rs @@ -49,6 +49,7 @@ impl WordApp { self.canvas.show_other_document(seen); self.controls = Default::default(); self.dialog = None; + self.spelling_review = None; self.colors = None; self.restrict = crate::dialogs_review::RestrictPane { open: self.restrict.open, ..Default::default() }; self.status_msg = None; diff --git a/crates/ui-egui/src/i18n/audit.rs b/crates/ui-egui/src/i18n/audit.rs index 4b0eb0c684..5da104c2bf 100644 --- a/crates/ui-egui/src/i18n/audit.rs +++ b/crates/ui-egui/src/i18n/audit.rs @@ -172,6 +172,10 @@ impl<'a> Exempt<'a> { if let Some(sugg) = app.canvas.context_issue.as_ref().and_then(|i| i.get("suggestions")).and_then(Value::as_array) { names.extend(sugg.iter().filter_map(Value::as_str).map(str::to_string)); } + // The proofing pane displays document context and dictionary suggestions, not UI wording. + if let Some(form) = &app.spelling_review { + names.extend(form.content_text()); + } Exempt { body: app.session.doc.plain_text(wordcraft_doc::StoryRef::Body), names, math: math_placeholders } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d91b6c67f0..9c2fe38c13 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -45,7 +45,6 @@ pub mod dialogs_notes; pub mod dialogs_para; pub mod dialogs_refs; pub mod dialogs_review; -pub mod dialogs_spelling; pub mod dialogs_ribbon; pub mod dialogs_smart_art; pub mod dialogs_table_style; @@ -74,6 +73,7 @@ pub mod paste_picture; pub mod previews; pub mod print_page; pub mod printing; +pub mod proofing_pane; pub mod read_aloud; pub mod ribbon; pub mod ribbon_fit; @@ -590,6 +590,8 @@ pub struct WordApp { pub colors: Option, /// The Restrict Editing pane (#412). pub restrict: dialogs_review::RestrictPane, + /// The language-aware spelling and grammar task pane. + pub spelling_review: Option, pub status_msg: Option<(String, f64)>, pub previews: previews::Previews, /// Media key of the picture a pending Change Picture replaces (#147). @@ -778,6 +780,7 @@ impl WordApp { dialog, colors, restrict, + spelling_review, status_msg, previews, change_picture_target, @@ -887,6 +890,7 @@ impl WordApp { dialog, colors, restrict, + spelling_review, status_msg, previews, change_picture_target, @@ -1115,12 +1119,10 @@ impl WordApp { if matches!(id, "insert.picture" | "insert.icon") { self.select_inserted_picture(&r); } - // User proofing opens a persistent correction form; scripts keep the engine's - // structured result. Right-click and Shift+F10 retain their compact suggestions. + // User proofing opens the persistent task pane; scripts keep structured results. + // Right-click and Shift+F10 retain their compact suggestions menu. if matches!(id, "review.spelling" | "review.editor") && let Ok(v) = &r { - self.dialog = Some(dialogs::Dialog::Review(Box::new(dialogs_review::ReviewDialog::Spelling(Box::new( - dialogs_spelling::SpellingForm::new(self, v.clone()), - ))))); + self.spelling_review = Some(proofing_pane::SpellingForm::new(self, v.clone())); } let r = system_clipboard::explain(self, id, r); // Check Accessibility shows its findings in the Accessibility pane, kept up to date there. diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 00c31190ef..c304ae8e18 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -30,6 +30,7 @@ pub enum Pane { AltText, FormatShape, RestrictEditing, + Proofing, Comments, } @@ -45,6 +46,7 @@ impl Pane { Self::AltText => "Alt Text", Self::FormatShape => "Format Shape", Self::RestrictEditing => "Restrict Editing", + Self::Proofing => "Proofing", Self::Comments => "Comments", } } @@ -60,6 +62,7 @@ impl Pane { Self::AltText => (Edge::End, 260.0), Self::FormatShape => (Edge::End, 280.0), Self::RestrictEditing => (Edge::End, 270.0), + Self::Proofing => (Edge::End, 400.0), Self::Comments => (Edge::End, 290.0), }; PaneState::docked(edge, width) @@ -77,11 +80,12 @@ impl Pane { Self::AltText => "altText", Self::FormatShape => "formatShape", Self::RestrictEditing => "restrictEditing", + Self::Proofing => "proofing", Self::Comments => "comments", } } - const ALL: [Self; 10] = [ + const ALL: [Self; 11] = [ Self::SmartArtText, Self::Navigation, Self::Clipboard, @@ -91,6 +95,7 @@ impl Pane { Self::AltText, Self::FormatShape, Self::RestrictEditing, + Self::Proofing, Self::Comments, ]; @@ -110,7 +115,7 @@ impl Pane { Self::AltText => Some("view.altTextPane"), Self::FormatShape => Some("view.formatShapePane"), Self::Comments => Some("view.commentsPane"), - Self::RestrictEditing => None, + Self::RestrictEditing | Self::Proofing => None, } } } @@ -163,6 +168,7 @@ pub fn open(app: &WordApp) -> Vec { (Pane::AltText, v.alt_text_pane), (Pane::FormatShape, v.format_shape_pane), (Pane::RestrictEditing, app.restrict.open), + (Pane::Proofing, app.spelling_review.is_some()), (Pane::Comments, v.comments_pane), ] .into_iter() @@ -289,6 +295,7 @@ impl PaneContent for Content<'_> { egui::ScrollArea::vertical().show(ui, |ui| crate::format_shape::pane(app, ui)); } Pane::RestrictEditing => crate::dialogs_review::restrict_pane(app, ui), + Pane::Proofing => crate::proofing_pane::show(app, ui), Pane::Comments => comments(app, ui), } } @@ -444,6 +451,7 @@ fn close(app: &mut WordApp, pane: Pane) { Some(id) => { let _ = app.run(id, json!({"value": false})); } + None if pane == Pane::Proofing => app.spelling_review = None, None => app.restrict.open = false, } } diff --git a/crates/ui-egui/src/proofing_pane.rs b/crates/ui-egui/src/proofing_pane.rs new file mode 100644 index 0000000000..bfbcb9a0ca --- /dev/null +++ b/crates/ui-egui/src/proofing_pane.rs @@ -0,0 +1,369 @@ +//! Language-aware proofing task pane, laid out from black-box observation of Word on Windows. + +use egui::{Align, Align2, Sense, Stroke, Ui, vec2}; +use serde::Serialize; +use serde_json::{Value, json}; +use wordcraft_engine::Selection; + +use crate::WordApp; +use crate::theme::{Tokens, regular, semibold}; + +#[derive(Clone, Debug, Serialize)] +pub struct SpellingForm { + issue: Value, + replacement: String, + message: String, + context_before: String, + context_after: String, + manual: bool, + #[serde(skip)] + document: u64, + #[serde(skip)] + epoch: u64, + #[serde(skip)] + selection: Selection, +} + +impl SpellingForm { + pub fn new(app: &WordApp, issue: Value) -> Self { + let replacement = issue.get("suggestions").and_then(Value::as_array).and_then(|a| a.first()).and_then(Value::as_str).unwrap_or("").into(); + let (a, b) = app.session.sel.ordered(); + let para = app.session.doc.para_at(&a); + let before = para.and_then(|p| p.text.get(..a.off)).unwrap_or("").rsplit(['.', '!', '?']).next().unwrap_or("").trim_start(); + let mut before: Vec = before.chars().rev().take(90).collect(); + before.reverse(); + let after = para.and_then(|p| p.text.get(b.off..)).unwrap_or("").split_inclusive(['.', '!', '?']).next().unwrap_or(""); + Self { + issue, + replacement, + message: String::new(), + context_before: before.into_iter().collect(), + context_after: after.chars().take(90).collect(), + manual: false, + document: app.session.document_id(), + epoch: app.session.ime_body_epoch(), + selection: app.session.sel.clone(), + } + } + + fn done(&self) -> bool { + self.issue.get("done").and_then(Value::as_bool) == Some(true) + } + + #[cfg(test)] + pub(crate) fn content_text(&self) -> Vec { + let mut text: Vec = self.issue.get("suggestions").and_then(Value::as_array).into_iter().flatten() + .filter_map(Value::as_str).map(str::to_string).collect(); + text.push(format!("{}{}{}", self.context_before, self.issue.get("text").and_then(Value::as_str).unwrap_or(""), self.context_after)); + text + } + + fn current(&self, app: &mut WordApp) -> bool { + self.document == app.session.document_id() + && self.epoch == app.session.ime_body_epoch() + && self.selection == app.session.sel + && app.session.run("review.suggestions", &json!({})).ok().as_ref() == Some(&self.issue) + } + + fn next(&mut self, app: &mut WordApp, wrap: bool) { + match app.execute("review.spelling", json!({"wrap": wrap})) { + Ok(issue) => *self = Self::new(app, issue), + Err(e) => self.message = e, + } + } + + fn act(&mut self, app: &mut WordApp, command: Option<&str>) { + if !self.current(app) { + self.message = tl!("The document or selection changed. Check again to continue.").into(); + return; + } + if let Some(command) = command { + let params = if command == "review.applySuggestion" { json!({"text": self.replacement}) } else { json!({}) }; + if let Err(e) = app.run(command, params) { + self.message = e; + return; + } + } + self.next(app, false); + } +} + +/// The shared task-pane host supplies the title, menu, Close button, docking and keyboard routing. +pub fn show(app: &mut WordApp, ui: &mut Ui) { + let Some(mut form) = app.spelling_review.take() else { return }; + egui::Frame::NONE.inner_margin(egui::Margin { left: 4, right: 16, top: 0, bottom: 0 }).show(ui, |ui| { + egui::ScrollArea::vertical().show(ui, |ui| body(app, ui, &mut form)); + }); + app.spelling_review = Some(form); +} + +fn action_link(ui: &mut Ui, text: &str) -> bool { + ui.add(egui::Button::new(egui::RichText::new(tl!(text)).color(Tokens::get(ui.ctx()).text).underline()).frame(false)).clicked() +} + +fn context(ui: &mut Ui, app: &mut WordApp, form: &mut SpellingForm, spelling: bool) { + let t = Tokens::get(ui.ctx()); + egui::Frame::NONE.fill(t.input).stroke(Stroke::new(1.0, t.input_border)).corner_radius(2.0).inner_margin(8).show(ui, |ui| { + ui.horizontal(|ui| { + let mut job = egui::text::LayoutJob::default(); + let format = egui::TextFormat { font_id: regular(12.0), color: t.text, ..Default::default() }; + job.append(&form.context_before, 0.0, format.clone()); + let marked = egui::TextFormat { underline: Stroke::new(1.0, if spelling { t.red } else { t.blue }), ..format.clone() }; + job.append(form.issue.get("text").and_then(Value::as_str).unwrap_or(""), 0.0, marked); + job.append(&form.context_after, 0.0, format); + ui.allocate_ui_with_layout(vec2((ui.available_width() - 36.0).max(20.0), 30.0), egui::Layout::top_down(Align::Min), |ui| { + ui.add(egui::Label::new(job).wrap()); + }); + let (rect, response) = ui.allocate_exact_size(vec2(28.0, 28.0), Sense::click()); + response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tl!("Read Aloud"))); + crate::icons::paint(ui.painter(), rect.shrink(4.0), "readAloud", t.icon, t.accent); + if response.on_hover_text(tl!("Read Aloud")).clicked() { + if form.current(app) { + if let Err(e) = app.run("review.readAloud", json!({})) { + form.message = e; + } + } else { + form.message = tl!("The document or selection changed. Check again to continue.").into(); + } + } + }); + }); +} + +fn body(app: &mut WordApp, ui: &mut Ui, form: &mut SpellingForm) { + let t = Tokens::get(ui.ctx()); + if form.done() { + ui.label(tl!("The spelling and grammar check is complete.")); + ui.add_space(8.0); + if action_link(ui, "Check Again") { + form.next(app, true); + } + return; + } + let spelling = form.issue.get("kind").and_then(Value::as_str) == Some("spelling"); + ui.label(egui::RichText::new(tl!(if spelling { "Spelling" } else { "Grammar" })).font(regular(14.0))) + .on_hover_text(form.issue.get("lang").and_then(Value::as_str).unwrap_or("")); + let message = if spelling { tl!("Not in Dictionary") } else { tl!(form.issue.get("message").and_then(Value::as_str).unwrap_or("")) }; + ui.label(egui::RichText::new(message).font(regular(11.0)).color(t.text_dim)); + ui.add_space(8.0); + context(ui, app, form, spelling); + ui.add_space(12.0); + ui.label(egui::RichText::new(tl!("Suggestions")).font(regular(11.0)).color(t.text_dim)); + let suggestions: Vec = form.issue.get("suggestions").and_then(Value::as_array).into_iter().flatten() + .filter_map(Value::as_str).map(str::to_string).collect(); + let mut change = None; + if suggestions.is_empty() { + ui.label(egui::RichText::new(tl!("(no suggestions)")).italics()); + } else { + egui::Frame::NONE.fill(t.input).stroke(Stroke::new(1.0, t.input_border)).corner_radius(2.0).show(ui, |ui| { + ui.spacing_mut().item_spacing.y = 0.0; + for (index, suggestion) in suggestions.iter().enumerate() { + ui.push_id(index, |ui| { + if index > 0 { + ui.separator(); + } + ui.horizontal(|ui| { + let width = (ui.available_width() - 34.0).max(20.0); + let (rect, response) = ui.allocate_exact_size(vec2(width, 64.0), Sense::click()); + response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, suggestion)); + if response.hovered() { + ui.painter().rect_filled(rect, 0.0, t.hover); + } + ui.painter().with_clip_rect(rect.shrink2(vec2(10.0, 0.0))).text( + rect.left_center() + vec2(12.0, 0.0), Align2::LEFT_CENTER, suggestion, semibold(12.5), t.text, + ); + if response.clicked() { + change = Some(suggestion.clone()); + } + ui.menu_button("⌄", |ui| { + if ui.button(tl!("Change…")).clicked() { + form.replacement.clone_from(suggestion); + form.manual = true; + ui.close(); + } + if spelling && ui.button(tl!("Add to AutoCorrect")).clicked() { + let word = form.issue.get("text").and_then(Value::as_str).unwrap_or(""); + let params = json!({"add": {"from": word, "to": suggestion}}); + if form.current(app) { + if let Err(e) = app.run("tools.autocorrect", params) { + form.message = e; + } + } else { + form.message = tl!("The document or selection changed. Check again to continue.").into(); + } + ui.close(); + } + if ui.button(tl!("AutoCorrect Options…")).clicked() { + let _ = app.run("tools.autocorrect", json!({})); + ui.close(); + } + }).response.on_hover_text(tl!("More options")); + }); + }); + } + }); + } + if let Some(replacement) = change { + form.replacement = replacement; + form.act(app, Some("review.applySuggestion")); + return; + } + ui.add_space(6.0); + if action_link(ui, "Ignore Once") { + form.act(app, None); + } else if spelling && action_link(ui, "Ignore All") { + form.act(app, Some("review.ignoreAll")); + } else if spelling && action_link(ui, "Add to Dictionary") { + form.act(app, Some("review.addToDictionary")); + } + if suggestions.is_empty() && action_link(ui, "Change…") { + form.manual = true; + } + if form.manual { + ui.add_space(8.0); + let label = ui.label(tl!("Change to:")); + ui.add(egui::TextEdit::singleline(&mut form.replacement).char_limit(1024)).labelled_by(label.id); + if ui.button(tl!("Change")).clicked() { + form.act(app, Some("review.applySuggestion")); + } + } + if !form.message.is_empty() { + ui.colored_label(ui.visuals().error_fg_color, &form.message); + if action_link(ui, "Check Again") { + form.next(app, true); + } + } +} + +#[cfg(test)] +mod tests { + use egui_kittest::kittest::Queryable; + use wordcraft_doc::{Document, Pos, StoryRef}; + use wordcraft_engine::Session; + + use super::*; + use crate::Services; + + fn app(text: &str) -> WordApp { + WordApp::new(Session::new(Document::from_text(text)), Services::default()) + } + + fn frame(ui: &mut Ui, app: &mut WordApp) { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + } + + fn harness(text: &str) -> egui_kittest::Harness<'static, WordApp> { + let mut app = app(text); + app.run("review.spelling", json!({})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state(frame, app); + h.run_steps(6); + h + } + + fn form(app: &WordApp) -> &SpellingForm { + app.spelling_review.as_ref().expect("proofing pane expected") + } + + #[test] + fn spelling_review_dialog_changes_a_suggestion_and_undo_restores_the_word() { + let mut h = harness("The quick brwn fox."); + assert!(!h.state().canvas.context_menu_open); + h.get_by_label("brown").click(); + h.run_steps(3); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brown fox."); + assert!(form(h.state()).done()); + h.get_by_label("Close").click(); + h.run_steps(2); + assert!(h.state().spelling_review.is_none()); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brwn fox."); + } + + #[test] + fn spelling_review_dialog_ignore_once_advances_and_finishes_without_changing_text() { + let text = "The brwn fox and brwn dog."; + let mut h = harness(text); + let first = h.state().session.sel.clone(); + h.get_by_label("Ignore Once").click(); + h.run_steps(3); + assert_ne!(h.state().session.sel, first); + assert_eq!(form(h.state()).issue["text"], "brwn"); + h.get_by_label("Ignore Once").click(); + h.run_steps(3); + assert!(form(h.state()).done()); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), text); + h.get_by_label("Check Again").click(); + h.run_steps(3); + assert_eq!(h.state().session.sel, first, "Ignore Once never adds a word to the dictionary"); + } + + #[test] + fn spelling_review_dialog_ignore_all_and_add_to_dictionary_advance() { + for (word, button) in [("zqproofingdialogignore", "Ignore All"), ("zqproofingdialogcustom", "Add to Dictionary")] { + let text = format!("The {word} fox and {word} dog."); + let mut h = harness(&text); + h.get_by_label(button).click(); + h.run_steps(3); + assert!(form(h.state()).done()); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), text); + assert!(h.state_mut().session.run("review.issues", &json!({})).unwrap().as_array().unwrap().is_empty()); + } + } + + #[test] + fn spelling_review_dialog_refuses_a_stale_selection_or_replaced_body() { + for replace in [false, true] { + let mut a = app("The brwn fox and brwn dog."); + let issue = a.execute("review.spelling", json!({})).unwrap(); + assert!(a.dialog.is_none() && a.spelling_review.is_none(), "programmatic calls never open a pane or window"); + let mut f = SpellingForm::new(&a, issue); + f.replacement = "brown".into(); + if replace { + a.execute("document.setText", json!({"text": "The brwn fox and brwn dog."})).unwrap(); + } else { + a.session.sel = Selection::caret(Pos::body(0, 0)); + } + let before = a.session.doc.plain_text(StoryRef::Body); + f.act(&mut a, Some("review.applySuggestion")); + assert!(!f.message.is_empty()); + assert_eq!(a.session.doc.plain_text(StoryRef::Body), before); + } + } + + #[test] + fn spelling_review_dialog_shows_completion_without_issues_and_close_returns_to_document() { + let mut h = harness("The quick brown fox."); + assert!(form(h.state()).done()); + h.get_by_label("Close").click(); + h.run_steps(2); + assert!(h.state().spelling_review.is_none()); + } + + #[test] + #[ignore = "offscreen visual review; requires a software or hardware graphics adapter"] + fn spelling_review_dialog_preview() { + let mut a = app("The quick brwn fox."); + a.run("review.spelling", json!({})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1000.0, 700.0)).wgpu().build_ui_state(frame, a); + for (name, visuals) in [("light", egui::Visuals::light()), ("dark", egui::Visuals::dark())] { + h.ctx.set_visuals(visuals); + h.run_steps(6); + h.render().unwrap().save(format!("spelling-review-{name}.png")).unwrap(); + } + } + + #[test] + fn spelling_review_dialog_pane_is_local_to_each_window_and_clears_on_document_switch() { + let mut a = app("The brwn fox."); + a.run("review.spelling", json!({})).unwrap(); + let mut other = crate::window_ui::WindowUi::new(a.session.document_id()); + a.swap_window(&mut other); + assert!(a.spelling_review.is_none()); + a.swap_window(&mut other); + assert_eq!(form(&a).issue["text"], "brwn"); + a.run("file.new", json!({})).unwrap(); + assert!(a.spelling_review.is_none()); + } +} diff --git a/crates/ui-egui/src/window_ui.rs b/crates/ui-egui/src/window_ui.rs index 9d938ee243..b28ffcc0ba 100644 --- a/crates/ui-egui/src/window_ui.rs +++ b/crates/ui-egui/src/window_ui.rs @@ -22,6 +22,7 @@ pub(crate) struct WindowUi { pub(crate) dialog: Option, pub(crate) colors: Option, pub(crate) restrict: dialogs_review::RestrictPane, + pub(crate) spelling_review: Option, pub(crate) status_msg: Option<(String, f64)>, pub(crate) previews: previews::Previews, pub(crate) change_picture_target: Option, @@ -91,6 +92,7 @@ impl WindowUi { dialog: None, colors: None, restrict: Default::default(), + spelling_review: None, status_msg: None, previews: Default::default(), change_picture_target: None, @@ -212,6 +214,7 @@ impl WordApp { dialog, colors, restrict, + spelling_review, status_msg, previews, change_picture_target, @@ -308,6 +311,7 @@ impl WordApp { dialog, colors, restrict, + spelling_review, status_msg, previews, change_picture_target, diff --git a/docs/gaps.md b/docs/gaps.md index 7b66eeb1c0..3fe0a67e25 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where WordCraft falls short of Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (persistent spelling and grammar correction dialog); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Highlight update:** 2026-10-25 · Highlight shortcut toggles the last chosen colour off when the whole selection has it; explicit colour commands remain setters (#4085). Readiness estimates unchanged. > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** trivial (#7: Custom Document Properties dialog, #2982; previously trivial: egui 0.37 migration: the interface catalogs stay shared and pinned by `craft-i18n.rev` at `9205108cfdced5177706264038848b68b2422d2b` (27 languages, English fallback); input-method handling has new regressions but managed CI passed, and native IME acceptance plus native-speaker catalog review remain open; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: small follow-ups from the field, table and AutoFormat batch, #4232; previously trivial: highlight target includes selected paragraph marks, #4085; previously trivial: full-target highlight check beyond 10,000 runs, #4085; previously trivial: Exact-spacing table contact uses the specified line height rather than paragraph-mark metrics (#3977); previously trivial: cu20we: restrict pre-spacing float origins to paragraph anchors; document cap helpers and regressions; previously trivial: a command that changes nothing is not an edit: no undo step, no unsaved changes, no relayout, and the redo stack survives it; previously minor: #8 narrowed: Spanish proofing (Spain, Mexico, Latin America, Uruguay) from the installed Hunspell dictionaries, #3360, #3553; previously trivial: gap 10: group connector rerouting preserves transformed free and unresolved endpoints under nonuniform scaling; obstacle routing still missing; readiness estimates unchanged; previously trivial: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: the ranked gap list held two rows each for #19, #20 and #21; each gap appears once now; a document with many relationships opens in the time its part takes, #3411; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 @@ -70,7 +70,7 @@ Text context menus now include the existing formatting mini toolbar (#2959), inc | 2 | **Word's native ink is not drawn from its strokes**; OLE objects show only their picture | B | Every native ink form is preserved: drawing-level ink with its optional picture fallback, direct run-level content parts, and ink members of groups and drawing canvases (kept in their place among the members, drawn from their fallback picture). Drawing canvases are read and written (#344); group shapes (`wpg`) read and written (#267). Charts and SmartArt are drawn (#292) and, with OLE objects, written back with all their parts on save (#319) | Pen notes from Word show only as their picture and can't be edited; OLE objects can't be opened | 45–70 to render and edit charts and SmartArt (#13) | [file-format-parity.md](file-format-parity.md) | | 5 | **Content controls** landed (#381): `w:sdt` kept, shown and edited (Developer tab, Design Mode, Properties). Move tracking landed (#400): `w:moveFrom`/`w:moveTo` with their named ranges round-trip, a tracked cut and paste of a paragraph or sentence records a move, either end accepts/rejects both, green double strike/underline markup and "Moved from/to" balloons; Track Changes Options has the Moves rows (Moved from / Moved to mark and colour) and `w:doNotTrackMoves` (Track moves off: a cut and paste is a deletion and an insertion) round-trips; still missing: drag-and-drop moves (no text drag yet). Formatting revisions landed (#41): `w:rPrChange`/`w:pPrChange`/`w:tblPrChange`/`w:trPrChange`/`w:tcPrChange`/`w:sectPrChange`/`w:numberingChange` round-trip, are recorded while tracking, accepted/rejected and shown in balloons; still missing: tracked style-definition changes (table, row, cell and section property changes now record in WordCraft too, #41 follow-up). Opaque custom XML backing stores now survive saves; binding evaluation remains absent. Replace All now checks content-control locks before editing any match; a refused batch preserves the document and undo history. | B | List-label pitch corrected in synthetic metric controls; Issues #41, #400; source grep | Legal and editorial review workflows; templates with form controls | 5–8 | [file-format-parity.md](file-format-parity.md) | | 6 | **Print dialog depth**: desktop printer choice, copies, collation, duplex and grayscale submit through craft-print; File › Print retains preview, page ranges and odd/even (#641) and scales pages to another paper size (#2108); driver tray/options and pages per sheet remain; web prints through the browser (#209) | B | `ui.print` → `apps/wordcraft/src/print.rs`; issue #15 | Everyone who prints | 5–8 | [hardware-parity.md](hardware-parity.md) | -| 7 | **Dialog depth**: persistent spelling/grammar correction form added (shared review commands, current-story traversal; see [spelling-review.md](spelling-review.md)); 61 modal dialogs vs Word's ~100 (Custom Document Properties from File › Info, #2982; Drop Cap Options matches Word's menu and dialog; Split Cells and Convert Text to Table match Word's observed limits and defaults; Format Page Numbers carries Word's number formats and Continue from previous section / Start at, #1113) | B | `Dialog` enum in `crates/ui-egui/src/dialogs.rs`; Cell Margins (#1960) now opens with current defaults and applies only confirmed edits | Power users reach for dialogs the ribbon alone doesn't cover | 11–19 | [ui-parity.md](ui-parity.md) | +| 7 | **Dialog depth**: Word-observed spelling/grammar task pane added (shared review commands, current-story traversal; see [spelling-review.md](spelling-review.md)); 61 modal dialogs vs Word's ~100 (Custom Document Properties from File › Info, #2982; Drop Cap Options matches Word's menu and dialog; Split Cells and Convert Text to Table match Word's observed limits and defaults; Format Page Numbers carries Word's number formats and Continue from previous section / Start at, #1113) | B | `Dialog` enum in `crates/ui-egui/src/dialogs.rs`; Cell Margins (#1960) now opens with current defaults and applies only confirmed edits | Power users reach for dialogs the ribbon alone doesn't cover | 11–19 | [ui-parity.md](ui-parity.md) | | 9 | **Password-protected `.doc` files with an AES header in the Word 97-2003 format** can't be opened: [MS-OFFCRYPTO] defines AES only for encrypted packages, so these are refused with a clear message. Encrypted packages open under any Word extension (`.docx`, `.doc`, `.dot`…) with either agile (Word 2010+) or Standard (Word 2007, AES-ECB) encryption; protected `.docx` saves with agile encryption (#55, #281, gap 9) | | `crates/docx/src/crypt.rs` (agile and Standard); `crates/docbin/src/crypt.rs` opens XOR/RC4/RC4 CryptoAPI `.doc` with a password (#1079). All synthetic fixtures; untested on Word-written files | Corporate and legal users with old files | 0 (refused by design; nothing left to build) | [file-format-parity.md](file-format-parity.md) | | 8 | **Proofing covers English, Polish, Portuguese, Italian, Croatian, Turkish, Russian, Czech, Indonesian and Spanish only**; no dictionaries or grammar for the other ~41 languages Word proofs (text in them is left unmarked), and no hyphenation for them except Bulgarian and German | | `crates/proof` `ProofLang::PROOFED`: English (American, British-spelling and Canadian, each flagging the other's spellings, #1281), Polish, Portuguese (pt-BR via the installed VERO dictionary, #797), Italian (it-IT via the installed Hunspell dictionary), Croatian (hr-HR, 2026-10-13), Turkish (tr_TR spelling, grammar, TDK hyphenation, 2026-10-13) Russian (the installed `ru_RU` dictionary, 2026-10-12) Indonesian (the installed `id_ID` dictionary, #498, 2026-10-16) and Spanish (es-ES, es-MX, es-419 and es-UY via the installed RLA-ES dictionaries, Uruguayan `vos` forms, #3360, #3553, 2026-10-22), a language per run; Bulgarian (#826) and German (TeX patterns, 2026-10-14) have hyphenation only; open PRs add Danish (#723), Russian (#894) and Czech (#1048); Word ships 120 proofing tools; issues #25, #40, #100 open | Every writer outside English, Polish, Portuguese, Italian, Croatian, Turkish, Russian, Czech, Indonesian and Spanish | 20–40 | [localization-parity.md](localization-parity.md) | | 10 | **Objects**: text in a freely rotated text box (free rotation and flips landed, #332; live rotation preview while dragging fixed for pictures and shapes without text stories, #2277; text direction, alignment and linked text boxes landed, #369; contour wrap and wrap side landed, #354), shape effects, WordArt 3-D effects and connector routing around shapes (WordArt effects and warps and glued connectors landed, #416), dragging members inside a Drawing Canvas (the canvas landed, #344) | | [parity-checklist.md](parity-checklist.md) (Shape Format 11/11, Layout 22/22); group and ungroup landed (#267) | Newsletters, flyers, reports with diagrams | 20–30 | [target-app-parity.md](target-app-parity.md) | @@ -150,7 +150,7 @@ Small follow-ups from the 2026-10-25 field, table and AutoFormat batch (#4232): | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Persistent Spelling & Grammar form uses shared language-aware review commands; Change, Ignore Once/All and Add to Dictionary advance without wrapping; stale targets are refused; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; readiness estimates unchanged | | 2026-10-22 | trivial | View windows: several open documents and Switch Windows in the engine | | 2026-10-25 | trivial | egui 0.37 migration: interface catalogs remain pinned by `craft-i18n.rev` (`9205108…`; 27 languages, English fallback); IME surrounding deletions and composition resets are implemented with regressions, but managed CI passed; native IME acceptance and native-speaker catalog review remain open; no gap or readiness change | | 2026-10-17 | trivial | Gap 9 narrowed: Standard (Word 2007, AES-ECB) encrypted packages open with a password under any Word extension; AES headers in Word 97-2003 files are outside the public spec and refused with a clear message; estimate 3–5 → 0 h, since the remaining AES-in-`.doc` case is refused by design | diff --git a/docs/spelling-review.md b/docs/spelling-review.md index a00313c9a3..5c5668f65e 100644 --- a/docs/spelling-review.md +++ b/docs/spelling-review.md @@ -1,24 +1,33 @@ -# Spelling and grammar correction form +# Spelling and grammar task pane -> **Last updated:** 2026-10-26 · **Status:** implementation proposed; hosted checks and visual acceptance must be verified before integration. +> **Last updated:** 2026-10-26 · **Status:** implementation proposed; hosted checks and rendered comparison remain required before integration. -Review › Spelling & Grammar, Home › Editor and F7 open a persistent correction form. The issue's text, explanation and proofing language appear above the suggestions. Choose a suggestion or type replacement text in Change to, then choose Change. Each replacement uses the existing review command and is one undoable document edit. Ignore Once advances without changing the document or dictionaries. Ignore All lasts for the session; Add to Dictionary uses the existing personal dictionary. Those two actions are offered only for spelling issues. +## Word reference -The pass begins at the next issue from the current selection and continues to the end of the current story (body, header, note or other active text). It does not traverse all document stories or restrict itself to a selected range. Check Again starts another search with wrapping; ignored-once occurrences can be revisited. Completion stays visible until Close or Escape. The compact right-click/Shift+F10 menu is retained. +Observed the actual Word for Windows interface on the preparation PC on 2026-10-26, using a newly created document with the synthetic sentence "Le chat mange une souri. Ce documant contient une faute." F7 selected "documant" and opened a right-side pane titled Vérification. Existing documents were left unchanged. The exact Word build was not recorded. -The form consumes language-aware engine results. It works with the existing proofing languages and is independent of French contribution #3185; adding an interface does not install a dictionary or add grammar rules. Interface labels use the shared translation lookup and fall back to English when a catalog lacks a key. No release date or completeness increase is implied. +At a 1920 × 1152 window, the pane occupied approximately 400 logical pixels on the right. Its body had a category heading and dictionary explanation, a framed sentence context approximately 48 px high, a Suggestions heading, and three bordered suggestion rows approximately 68 px high. Ignore Once, Ignore All and Add to Dictionary were text actions below the list. A suggestion's chevron opened Read Aloud, Spell Out, Change All and AutoCorrect actions. These are observations, not a copied screenshot or asset. -If another command changes the document body or selection while the form is open, an old suggestion is refused. Check Again obtains a fresh target. Read-only/protected document commands retain the engine's existing checks and display refusals in the form. +WordCraft uses this arrangement and its shared pane host, icons, typography and theme palette. Clicking a suggestion applies the correction directly and advances; the central modal proposal is superseded. The pane supports sentence context, Read Aloud through the existing command, manual replacement and AutoCorrect actions. Word's dictionary definitions/synonyms, Spell Out and Change All are not implemented here. Visual similarity must be assessed from the generated light/dark previews; implementation alone is not evidence of pixel identity. + +## Scope and behavior + +Review › Spelling & Grammar, Home › Editor and F7 open the task pane. The pass begins at the next issue from the current selection and continues to the end of the current story (body, header, note or other active text), not all document stories or only a selection. Each correction uses the existing review command and remains undoable. Ignore Once advances without changing the document or dictionaries. Ignore All lasts for the session; Add to Dictionary uses the personal dictionary. Those two actions are offered only for spelling issues. Completion remains visible; Check Again starts a wrapping search. The pane host supplies Close and docking. + +If another command changes the document body or selection, a cached suggestion is refused; Check Again obtains a fresh target. Read-only/protected document commands retain their checks. The pane is local to each window, clears on a document switch and does not alter the compact right-click menu. Programmatic review commands return structured results without opening UI. + +This work is independent of French contribution #3185. It consumes language-aware engine results and adds neither dictionaries nor grammar rules. Labels use the external shared translation lookup with English fallback for missing keys. No release date or readiness increase is implied. ## Verification -- Engine regression: one- and two-paragraph navigation, forward-only completion and explicit wrapping. -- UI regressions: suggestion application and undo, Ignore Once progress/completion, Ignore All, personal dictionary addition, stale selection/body refusal, no-issue completion and Escape. -- Offscreen preview: the ignored spelling_review_dialog_preview test writes light/dark PNGs; inspect them before integration. -- Required repository gate: cargo xtask ci. Cargo is unavailable on the preparation PC, so hosted checks are required; test definitions alone are not evidence of success. +- Engine: one-/two-paragraph navigation, forward-only completion and explicit wrapping. +- UI: direct suggestion correction and undo, Ignore Once progress/completion, Ignore All, personal dictionary addition, stale selection/body refusal, completion and Close, per-window isolation and document switching. +- Offscreen: the ignored spelling_review_dialog_preview test writes light/dark PNGs for comparison with the recorded Word arrangement. +- Required gate: cargo xtask ci. Cargo is unavailable on the preparation PC; hosted validation is required, and test definitions alone do not establish success. ## Revision history | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Actual Word for Windows observation replaces the modal proposal with a right-side Proofing pane, sentence context and directly clickable suggestion cards | | 2026-10-26 | minor | Separate language-independent correction-form proposal and verification scope | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index b2858e1e92..7c7a29fa89 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity with Microsoft Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (persistent spelling and grammar correction dialog); previously trivial (Custom Document Properties dialog from File › Info › Properties › Advanced Properties…, #2982; previously trivial: View › Window › New Window opens another window on the document, each with its own caret and view; previously trivial: mail merge: First Record, Last Record and a record number box in Preview Results; Finish & Merge asks which records to merge or print, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: read only and comments-only protection grey out edits; previously trivial: the Font dialog previews "NaN" or "inf" in the Size box at 12 pt, #3663; previously trivial: File › Options disables the User name field while Restrict Editing is active; previously trivial: while editing a header or footer, a click in the other one edits it there without scrolling; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Insert Table of Figures dialog: any caption label, #3013; previously trivial: Home › Underline drop-down shows its lines, #2738; previously trivial: the text context menu includes the formatting mini toolbar, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: the scrollbar page tip stays on one line as the page number grows, #3341; previously trivial: File › Home and New: templates and recent documents are buttons named for screen readers, #3176; previously trivial: forms protection greys out the Developer tab's control buttons and Properties; previously trivial: AltGr doesn't open KeyTips, #2388; previously trivial: closing the header or footer puts the caret back where it was in the body, #3331; previously trivial: Format Page Numbers dialog: number format and Continue from previous section / Start at, #1113; previously trivial: inline pictures drag to a dashed insertion cursor and stay inline, #2233, #2234; previously trivial: Save Changes prompt: a text file saved over itself closes, #2376; previously trivial: the equation being edited is framed as in Word: an outline as tall as its line, a grip, an options strip that opens its menu, and its current argument shaded; previously trivial: the table handles show while the caret is in the table, #2428; previously trivial: legacy menu keys, #1697; previously trivial: Shape Fill › Gradient and the Format Shape pane, #1500; previously trivial: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: the text pointer over the page is drawn black or white on Windows, #1746; previously trivial: a click in the header/footer band from the body no longer scrolls to the body text, #2103; previously trivial: File › Recover follows the app theme, #3243; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: dragging the vertical scrollbar names the page at the top of the window, #2551; previously trivial: Add Font imports every selected font file, #1789); previously trivial (File › Close keeps the window, #1712; previously trivial: the Spike: Ctrl+F3 and Ctrl+Shift+F3; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: the tab strip never draws tabs over Comments / Editing / Share, #2026; previously trivial: Columns on selected text, #2024; previously trivial: Different First Page headers reachable from page 1, #2826; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: Read Mode keeps the keyboard, shows whole columns and keeps text clear of the page-turn bar, #1772); previously trivial: font list entries of tall-ascent fonts sit in their row, not cut off, #2570); previously trivial: Split Cells and Convert Text to Table dialogs follow Word's merged-cell limits and Initial column width, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout text fills the window width, #1876; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717)); previously trivial: comment balloons readable in the dark theme, #2045; previously trivial: Recently Used Fonts section at the top of the font list, #2068; previously trivial: web preferences kept between visits, #2132; previously trivial: dialog close control: a Windows-style box at the title bar's right end, the whole box clickable, red on hover, #1885); previously trivial (Modify Table Style formats every table style part, #714); previously trivial: a click on the ribbon chrome hands the keyboard to the page in the same frame, #1093 follow-up; previously trivial: Page Setup vertical alignment, #1320; previously trivial: the app opens on the Home tab every launch, as in Word; previously trivial: no document behind the Start screen, #1129; previously trivial: equation pictures share a few textures, #1002; previously trivial: selection bar in the left margin, #856; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously major: first version; ribbon, dialogs, shortcuts and on-canvas interaction measured from `crates/ui-egui` and the command registry) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane); previously trivial (Custom Document Properties dialog from File › Info › Properties › Advanced Properties…, #2982; previously trivial: View › Window › New Window opens another window on the document, each with its own caret and view; previously trivial: mail merge: First Record, Last Record and a record number box in Preview Results; Finish & Merge asks which records to merge or print, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: read only and comments-only protection grey out edits; previously trivial: the Font dialog previews "NaN" or "inf" in the Size box at 12 pt, #3663; previously trivial: File › Options disables the User name field while Restrict Editing is active; previously trivial: while editing a header or footer, a click in the other one edits it there without scrolling; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Insert Table of Figures dialog: any caption label, #3013; previously trivial: Home › Underline drop-down shows its lines, #2738; previously trivial: the text context menu includes the formatting mini toolbar, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: the scrollbar page tip stays on one line as the page number grows, #3341; previously trivial: File › Home and New: templates and recent documents are buttons named for screen readers, #3176; previously trivial: forms protection greys out the Developer tab's control buttons and Properties; previously trivial: AltGr doesn't open KeyTips, #2388; previously trivial: closing the header or footer puts the caret back where it was in the body, #3331; previously trivial: Format Page Numbers dialog: number format and Continue from previous section / Start at, #1113; previously trivial: inline pictures drag to a dashed insertion cursor and stay inline, #2233, #2234; previously trivial: Save Changes prompt: a text file saved over itself closes, #2376; previously trivial: the equation being edited is framed as in Word: an outline as tall as its line, a grip, an options strip that opens its menu, and its current argument shaded; previously trivial: the table handles show while the caret is in the table, #2428; previously trivial: legacy menu keys, #1697; previously trivial: Shape Fill › Gradient and the Format Shape pane, #1500; previously trivial: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: the text pointer over the page is drawn black or white on Windows, #1746; previously trivial: a click in the header/footer band from the body no longer scrolls to the body text, #2103; previously trivial: File › Recover follows the app theme, #3243; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: dragging the vertical scrollbar names the page at the top of the window, #2551; previously trivial: Add Font imports every selected font file, #1789); previously trivial (File › Close keeps the window, #1712; previously trivial: the Spike: Ctrl+F3 and Ctrl+Shift+F3; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: the tab strip never draws tabs over Comments / Editing / Share, #2026; previously trivial: Columns on selected text, #2024; previously trivial: Different First Page headers reachable from page 1, #2826; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: Read Mode keeps the keyboard, shows whole columns and keeps text clear of the page-turn bar, #1772); previously trivial: font list entries of tall-ascent fonts sit in their row, not cut off, #2570); previously trivial: Split Cells and Convert Text to Table dialogs follow Word's merged-cell limits and Initial column width, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout text fills the window width, #1876; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717)); previously trivial: comment balloons readable in the dark theme, #2045; previously trivial: Recently Used Fonts section at the top of the font list, #2068; previously trivial: web preferences kept between visits, #2132; previously trivial: dialog close control: a Windows-style box at the title bar's right end, the whole box clickable, red on hover, #1885); previously trivial (Modify Table Style formats every table style part, #714); previously trivial: a click on the ribbon chrome hands the keyboard to the page in the same frame, #1093 follow-up; previously trivial: Page Setup vertical alignment, #1320; previously trivial: the app opens on the Home tab every launch, as in Word; previously trivial: no document behind the Start screen, #1129; previously trivial: equation pictures share a few textures, #1002; previously trivial: selection bar in the left margin, #856; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously major: first version; ribbon, dialogs, shortcuts and on-canvas interaction measured from `crates/ui-egui` and the command registry) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 Recovery discovery no longer interrupts an explicitly created or opened document. Older copies remain in File › Recover, whose buttons use the interface palette. See [local recovery](recovery.md). @@ -31,7 +31,7 @@ Document title opens Rename Document; the account badge opens Options › Genera | Modal dialogs | **56** (`Dialog` enum: Discard Recovery, Font, Enclose Characters, Paragraph, Tabs, Borders and Shading, Find, Go To, Insert Table, Table Properties, Page Setup, Link, Bookmark, Word Count, Zoom, Watermark, New Style, Modify Style, Manage Styles, Table Style, Commands, Icons, Templates and Add-ins, Organizer, Compare and Combine, AutoCorrect Options, Paste Special, About, Save Changes, Password, Encrypt with Password, Define New Multilevel List, Track Changes Options; Columns, Symbol with Special Characters, Field (#321); Field code (#339); mail merge Recipients (Filter and Sort, #778; Find in list and a merged-only view, #1576), Insert Merge Field, Find Recipient, Rules, Match Fields, Check for Errors; Caption with New Label and Numbering, Index, Mark Index Entry, Custom Table of Contents with Options and TOC styles, Source Manager, Create/Edit Source (#402); Cross-reference (#916); Line Numbers, Hyphenation with Manual Hyphenation, Language, Envelopes, Labels (#407); Page Setup has a Paper size list and custom width and height, opened from Layout › Size › More Paper Sizes… (#1433)) plus panes and menus for the rest | ~100 (Options panes…) | ~45% | 11–19 | | Task panes (Navigation, Styles, Comments, Reviewing, Format Picture/Shape, Thesaurus, Accessibility, Clipboard, Selection) | Navigation (long headings wrap onto further lines within the pane instead of expanding it, #759, #3015), Styles, Comments, Thesaurus, Restrict Editing, Accessibility (grouped findings, jump-to, fixes) and Alt Text (#386), Read Aloud, Zotero; Style Inspector (#236); Clipboard pane (#264) open | all | 70% | 4–6 | | Keyboard shortcuts | **152** commands bound in `CommandSpec::key`, 172 key combinations (counted from the registry on Linux, 2026-10-13); styles, spacing, symbols, fields, views, mail merge (#330); field keys Alt+F9, Shift+F9, Ctrl+Shift+F9, Ctrl+F11, Ctrl+Shift+F11, Shift+F11 (#339), Next Field on Alt+F1 and Alt+Shift+F1 (Word's other keys for it) since F11 went to full screen (#980; GNOME and KDE may keep Alt+F1 for their own overview or application menu, and then Next Field has no key there; Previous Field keeps Shift+F11); Alt+Shift+arrows (move paragraphs, promote/demote), Shift+F5 Go Back and Alt+X (#343); Outline view: Alt+Shift+Plus/Minus expand/collapse, Alt+Shift+1–9 and Alt+Shift+A Show Level (#493); Alt+Q to "Tell me" command search (#819, not on macOS); on macOS, Word for Mac's own keys: ⌘⇧S Save As, ⌘⌥E Insert Endnote, ⌘⌥A New Comment, ⌃H Replace, ⌘⌥R collapse the ribbon. The Spike: Ctrl+F3 cuts to it, Ctrl+Shift+F3 inserts it and empties it (⌘F3, ⌘⇧F3 on macOS). Missing: en dash on the numeric keypad Insert Hyperlink has Word's Link to: Existing File or Web Page / Place in This Document (top of the document, headings, bookmarks) (#1234). | ~250 default shortcuts | ~70% | 2–4 | -| Modal dialogs | **64** (`Dialog` enum: Discard Recovery, Font, Enclose Characters, Paragraph, Tabs, Borders and Shading, Find, Go To, Insert Table, Table Properties, Page Setup, Link, Bookmark, Word Count, Zoom, Watermark, New Style, Modify Style, Manage Styles, Table Style, Commands, Icons, Templates and Add-ins, Organizer, Spelling & Grammar, Custom Document Properties (File › Info › Properties › Advanced Properties › Custom, #2982), Compare and Combine, AutoCorrect Options, Paste Special, About, Save Changes, Password, Encrypt with Password, Define New Multilevel List, Track Changes Options; Columns, Symbol with Special Characters, Field (#321); Field code (#339); mail merge Recipients, Insert Merge Field, Find Recipient, Rules, Match Fields, Check for Errors; Caption with New Label and Numbering, Index, Mark Index Entry, Custom Table of Contents with Options and TOC styles, Table of Figures (#3013), Source Manager, Create/Edit Source (#402); Cross-reference (#916); Drop Cap with Position, Font, Lines to drop and Distance from text, opened from Insert › Drop Cap › Drop Cap Options… (#2601); Line Numbers, Hyphenation with Manual Hyphenation, Language, Envelopes, Labels (#407); Page Setup has a Paper size list and custom width and height, opened from Layout › Size › More Paper Sizes… (#1433); every dialog's close control is a box at the title bar's right end, the full title-bar height and 1.6× as wide, all of it clickable, red with a white ✕ on hover as a Windows caption button (#1885); Split Cells (columns, rows, merge before split; defaults follow the selection as in Word: twice its columns, its rows, Merge offered for several cells or one merged across columns; a merged cell splits into 1 to its height rows, a number that doesn't divide its height refused with a divisor message, more than its height with the range message; 1 × 1 changes nothing; parts split across columns are equal and other rows keep their grid lines) and Convert Text to Table (live column count, read-only rows; Initial column width Auto or 0 to 22 in, AutoFit to contents/window; Paragraphs/Commas/Tabs/Other "-" separators) (#2592); Size, from Picture Format › Size's launcher: height and width in the interface unit (a size typed in any unit, `300 px` too), scale of the original or the opening size, Lock aspect ratio, the original size with its pixels and Reset (#2573); Format Page Numbers, from Insert › Page Number ▸ Format Page Numbers…: the number format (1, 2, 3 / I, II, III / i, ii, iii / A, B, C / a, b, c) and Continue from previous section / Start at, #1113; Finish & Merge records (All, Current record, or From and To by record number), opened from Finish & Merge › Edit Individual Documents… and Print Documents… (#3028)) plus panes and menus for the rest | ~100 (Options panes…) | ~45% | 11–19 | +| Modal dialogs | **63** (`Dialog` enum: Discard Recovery, Font, Enclose Characters, Paragraph, Tabs, Borders and Shading, Find, Go To, Insert Table, Table Properties, Page Setup, Link, Bookmark, Word Count, Zoom, Watermark, New Style, Modify Style, Manage Styles, Table Style, Commands, Icons, Templates and Add-ins, Organizer, Custom Document Properties (File › Info › Properties › Advanced Properties › Custom, #2982), Compare and Combine, AutoCorrect Options, Paste Special, About, Save Changes, Password, Encrypt with Password, Define New Multilevel List, Track Changes Options; Columns, Symbol with Special Characters, Field (#321); Field code (#339); mail merge Recipients, Insert Merge Field, Find Recipient, Rules, Match Fields, Check for Errors; Caption with New Label and Numbering, Index, Mark Index Entry, Custom Table of Contents with Options and TOC styles, Table of Figures (#3013), Source Manager, Create/Edit Source (#402); Cross-reference (#916); Drop Cap with Position, Font, Lines to drop and Distance from text, opened from Insert › Drop Cap › Drop Cap Options… (#2601); Line Numbers, Hyphenation with Manual Hyphenation, Language, Envelopes, Labels (#407); Page Setup has a Paper size list and custom width and height, opened from Layout › Size › More Paper Sizes… (#1433); every dialog's close control is a box at the title bar's right end, the full title-bar height and 1.6× as wide, all of it clickable, red with a white ✕ on hover as a Windows caption button (#1885); Split Cells (columns, rows, merge before split; defaults follow the selection as in Word: twice its columns, its rows, Merge offered for several cells or one merged across columns; a merged cell splits into 1 to its height rows, a number that doesn't divide its height refused with a divisor message, more than its height with the range message; 1 × 1 changes nothing; parts split across columns are equal and other rows keep their grid lines) and Convert Text to Table (live column count, read-only rows; Initial column width Auto or 0 to 22 in, AutoFit to contents/window; Paragraphs/Commas/Tabs/Other "-" separators) (#2592); Size, from Picture Format › Size's launcher: height and width in the interface unit (a size typed in any unit, `300 px` too), scale of the original or the opening size, Lock aspect ratio, the original size with its pixels and Reset (#2573); Format Page Numbers, from Insert › Page Number ▸ Format Page Numbers…: the number format (1, 2, 3 / I, II, III / i, ii, iii / A, B, C / a, b, c) and Continue from previous section / Start at, #1113; Finish & Merge records (All, Current record, or From and To by record number), opened from Finish & Merge › Edit Individual Documents… and Print Documents… (#3028)) plus panes and menus for the rest | ~100 (Options panes…) | ~45% | 11–19 | | Task panes (Navigation, Styles, Comments, Reviewing, Format Picture/Shape, Thesaurus, Accessibility, Clipboard, Selection) | Navigation (long headings wrap onto further lines within the pane instead of expanding it, #759, #3015), Styles, Comments (balloons and cards read in both themes, #2045), Thesaurus, Restrict Editing, Accessibility (grouped findings, jump-to, fixes) and Alt Text (#386), Read Aloud, Zotero; Style Inspector (#236); Clipboard pane (#264) open | all | 70% | 4–6 | | Context menus (right-click) | text, spelling suggestions, fields (Update Field, Edit Field…, Toggle Field Codes, #339; Update Field on a table of contents entry updates the table, #1197, and after a save and reopen, #2182); right-click selects a picture, shape or text box and offers Insert Caption… (#1230) and Wrap Text (styles, Move with Text / Fix Position on Page, #513); tables, the items following the selection as in Word (Insert, Delete › Cells, Columns, Rows or Table whatever is selected (#1198), Merge and Split Cells, Distribute, AutoFit, Text Direction, Insert Caption…, Table Properties…; no Border Styles yet) | rich per context | 65% | 2–4 | | Task panes (Navigation, Styles, Comments, Reviewing, Format Picture/Shape, Thesaurus, Accessibility, Clipboard, Selection) | Navigation (long headings wrap onto further lines within the pane instead of expanding it, #759, #3015), Styles, Comments (times in the user's own time zone, in the pane and the balloons, #2322, #2337; typed comment and reply text stays with its document, is written before Save, Export, Print, Close, New or Open, Escape discards it, and the fields are read-only where comments can't change, #1348), Thesaurus, Restrict Editing, Accessibility (grouped findings, jump-to, fixes) and Alt Text (#386), Read Aloud, Zotero; Style Inspector (#236); Clipboard pane (#264) open | all | 70% | 4–6 | @@ -80,13 +80,15 @@ Footnote and endnote editing keeps body and note text at normal opacity, on ligh ## Spelling and grammar correction -Review › Spelling & Grammar, Home › Editor and F7 open the same persistent correction form. It shows the issue and its language, suggestions and an editable replacement, with Change, Ignore Once, Ignore All and Add to Dictionary. The last two apply to spelling only. Each action advances without wrapping; completion stays visible. Right-click and Shift+F10 keep the compact suggestions menu. The form is shared by all proofing languages and does not add dictionaries or grammar rules. See [spelling-review.md](spelling-review.md) for scope and verification. +Review › Spelling & Grammar, Home › Editor and F7 open a right-side Proofing task pane. Black-box observation of Word for Windows on 2026-10-26 established the arrangement: issue category, dictionary explanation, sentence context with the issue underlined, a bordered stack of suggestion cards, then Ignore Once, Ignore All and Add to Dictionary links. Clicking a suggestion applies it and advances. The pane uses WordCraft's existing title/docking/close host and its own icons and palette; no Microsoft assets are copied. The suggestion menu exposes supported manual replacement and AutoCorrect actions; Word's synonyms, spelling-out and Change All are not implemented here. No pixel-identical claim is made until the rendered comparison is inspected. + +Right-click and Shift+F10 keep the compact suggestions menu. The pane is shared by all proofing languages; it adds no dictionary or grammar rule. See [spelling-review.md](spelling-review.md) for scope, observation and verification. ## Revision history | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Persistent Spelling & Grammar form uses shared language-aware review commands; Change, Ignore Once/All and Add to Dictionary advance without wrapping; stale targets are refused; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; readiness estimates unchanged | | 2026-10-22 | trivial | View › Window › New Window opens another window on the document, focused and cascaded; two windows on one document are titled `Name:1` and `Name:2` (one window: the plain name). Each window keeps its own selection, scroll position, zoom, view mode, ruler and panes; the text, undo history, unsaved state and file are the document's, so an edit or Undo in either window shows in both, and a caret in text the other window deleted moves to the nearest place left. Closing one of a document's windows asks nothing; closing its last asks about unsaved changes, and quitting asks once per document. Switch Windows lists every window. The web app keeps one window | | 2026-10-22 | trivial | One native window per open document on the desktop (the web app keeps one window): New and Open open the document in a window of its own, focused and cascaded from the last; Open replaces an untouched blank document in its window and brings an already-open file's window forward; each window keeps its own ribbon tab, File page, dialogs, panes and composition; View › Window › Switch Windows lists the windows and brings the chosen one forward; File › Close and the window's close button close the window (asking about unsaved changes) and the window focused before comes forward, while the last window stays open with no document; quitting asks Save / Don't Save / Cancel about each document in its own window | | 2026-10-22 | trivial | Several open documents in one window (one window per document comes next): View › Window › Switch Windows lists the open documents, the shown one marked and unsaved ones dotted, and shows the one chosen, with its own scroll position, zoom, comment drafts and AutoSave file; File › New, Open, Envelopes, Labels, Finish & Merge and personal templates open beside the open documents without asking about unsaved changes; File › Close shows the next open document; the window's close button asks Save / Don't Save / Cancel about each document with unsaved changes, showing it, and Cancel keeps the app open. Untitled documents are numbered (Document2) in the interface language | From 9e4adaf3202e5ba0ffb095f07dbfcbcc00a443cb Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:21:10 +0100 Subject: [PATCH 012/308] M7: apply the hosted rustfmt corrections to the proofing pane Apply the six formatting hunks reported by the exact-head Printing/CI runs. No behavior change. cargo xtask ci was attempted locally but cargo is absent; hosted checks must validate this revision. --- crates/engine/src/cmd/review.rs | 9 ++++++--- crates/ui-egui/src/lib.rs | 4 +++- crates/ui-egui/src/proofing_pane.rs | 18 ++++++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index 4c168b2a06..873c7f674f 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -999,9 +999,12 @@ fn next_issue(s: &mut Session, v: &Value) -> CmdResult { // Scan forward first, including the rest of the caret's paragraph. A second pass // optionally visits the earlier part; never select the first typo repeatedly in // a single-paragraph story. Dialogs disable wrapping so Ignore Once can finish. - for (path, before) in paths.iter().skip(start).map(|path| (path, false)).chain( - paths.iter().take(if wrap { start.saturating_add(1) } else { 0 }).map(|path| (path, true)), - ) { + for (path, before) in paths + .iter() + .skip(start) + .map(|path| (path, false)) + .chain(paths.iter().take(if wrap { start.saturating_add(1) } else { 0 }).map(|path| (path, true))) + { for (a, b, i) in para_issues(s, story, path) { if *path == caret.path && (a.off < caret.off) != before { continue; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 9c2fe38c13..9b0eb7577f 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1121,7 +1121,9 @@ impl WordApp { } // User proofing opens the persistent task pane; scripts keep structured results. // Right-click and Shift+F10 retain their compact suggestions menu. - if matches!(id, "review.spelling" | "review.editor") && let Ok(v) = &r { + if matches!(id, "review.spelling" | "review.editor") + && let Ok(v) = &r + { self.spelling_review = Some(proofing_pane::SpellingForm::new(self, v.clone())); } let r = system_clipboard::explain(self, id, r); diff --git a/crates/ui-egui/src/proofing_pane.rs b/crates/ui-egui/src/proofing_pane.rs index bfbcb9a0ca..6710530be9 100644 --- a/crates/ui-egui/src/proofing_pane.rs +++ b/crates/ui-egui/src/proofing_pane.rs @@ -52,8 +52,8 @@ impl SpellingForm { #[cfg(test)] pub(crate) fn content_text(&self) -> Vec { - let mut text: Vec = self.issue.get("suggestions").and_then(Value::as_array).into_iter().flatten() - .filter_map(Value::as_str).map(str::to_string).collect(); + let mut text: Vec = + self.issue.get("suggestions").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).map(str::to_string).collect(); text.push(format!("{}{}{}", self.context_before, self.issue.get("text").and_then(Value::as_str).unwrap_or(""), self.context_after)); text } @@ -149,8 +149,8 @@ fn body(app: &mut WordApp, ui: &mut Ui, form: &mut SpellingForm) { context(ui, app, form, spelling); ui.add_space(12.0); ui.label(egui::RichText::new(tl!("Suggestions")).font(regular(11.0)).color(t.text_dim)); - let suggestions: Vec = form.issue.get("suggestions").and_then(Value::as_array).into_iter().flatten() - .filter_map(Value::as_str).map(str::to_string).collect(); + let suggestions: Vec = + form.issue.get("suggestions").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).map(str::to_string).collect(); let mut change = None; if suggestions.is_empty() { ui.label(egui::RichText::new(tl!("(no suggestions)")).italics()); @@ -170,7 +170,11 @@ fn body(app: &mut WordApp, ui: &mut Ui, form: &mut SpellingForm) { ui.painter().rect_filled(rect, 0.0, t.hover); } ui.painter().with_clip_rect(rect.shrink2(vec2(10.0, 0.0))).text( - rect.left_center() + vec2(12.0, 0.0), Align2::LEFT_CENTER, suggestion, semibold(12.5), t.text, + rect.left_center() + vec2(12.0, 0.0), + Align2::LEFT_CENTER, + suggestion, + semibold(12.5), + t.text, ); if response.clicked() { change = Some(suggestion.clone()); @@ -197,7 +201,9 @@ fn body(app: &mut WordApp, ui: &mut Ui, form: &mut SpellingForm) { let _ = app.run("tools.autocorrect", json!({})); ui.close(); } - }).response.on_hover_text(tl!("More options")); + }) + .response + .on_hover_text(tl!("More options")); }); }); } From 574cef34c96b24f4085e5e49a7fd7eaeb452dcfb Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:27:57 +0100 Subject: [PATCH 013/308] M7: use current egui accessibility roles for proofing controls Replace two deprecated WidgetType aliases with Role::Button, as required by the hosted Windows and macOS Clippy checks. Local cargo xtask ci was attempted but cargo is not installed on this machine. git diff --check passes. Hosted CI and the preview workflow must validate this revision. --- crates/ui-egui/src/proofing_pane.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/proofing_pane.rs b/crates/ui-egui/src/proofing_pane.rs index 6710530be9..2e82349ffb 100644 --- a/crates/ui-egui/src/proofing_pane.rs +++ b/crates/ui-egui/src/proofing_pane.rs @@ -115,7 +115,7 @@ fn context(ui: &mut Ui, app: &mut WordApp, form: &mut SpellingForm, spelling: bo ui.add(egui::Label::new(job).wrap()); }); let (rect, response) = ui.allocate_exact_size(vec2(28.0, 28.0), Sense::click()); - response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tl!("Read Aloud"))); + response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!("Read Aloud"))); crate::icons::paint(ui.painter(), rect.shrink(4.0), "readAloud", t.icon, t.accent); if response.on_hover_text(tl!("Read Aloud")).clicked() { if form.current(app) { @@ -165,7 +165,7 @@ fn body(app: &mut WordApp, ui: &mut Ui, form: &mut SpellingForm) { ui.horizontal(|ui| { let width = (ui.available_width() - 34.0).max(20.0); let (rect, response) = ui.allocate_exact_size(vec2(width, 64.0), Sense::click()); - response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, suggestion)); + response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, suggestion)); if response.hovered() { ui.painter().rect_filled(rect, 0.0, t.hover); } From 20205a50511e2f3a1c59d0a9c3acce33ec219b81 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:11:10 +0100 Subject: [PATCH 014/308] M7: repair baseline test imports and publish proofing previews The full gate found missing Page/Rgb imports in main's tests_linear.rs (also tracked by upstream #4401). Add the imports already validated with the French contribution. No layout behavior changes. The preview run rendered both images and passed all six correction tests, but upload searched the workspace root. Cargo tests run in the crate directory; collect the PNGs from crates/ui-egui instead. At parent 574cef, Printing passed on Linux, macOS and Windows, including UI tests and browser compatibility. Local cargo xtask ci was attempted but cargo is absent; git diff --check passes. This revision needs new hosted validation. --- .github/workflows/spelling-review.yml | 2 +- crates/layout/src/tests_linear.rs | 988 +++++++++++++------------- 2 files changed, 495 insertions(+), 495 deletions(-) diff --git a/.github/workflows/spelling-review.yml b/.github/workflows/spelling-review.yml index 5fcea383f8..a3ead7ce3f 100644 --- a/.github/workflows/spelling-review.yml +++ b/.github/workflows/spelling-review.yml @@ -49,7 +49,7 @@ jobs: uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 with: name: spelling-review-preview - path: spelling-review-*.png + path: crates/ui-egui/spelling-review-*.png if-no-files-found: error - name: Require formatting success if: steps.formatting.outcome != 'success' diff --git a/crates/layout/src/tests_linear.rs b/crates/layout/src/tests_linear.rs index 31b451d8dd..92c87c98d8 100644 --- a/crates/layout/src/tests_linear.rs +++ b/crates/layout/src/tests_linear.rs @@ -1,494 +1,494 @@ -//! Lookups that once rescanned the page, the paragraph or the document for every item they -//! looked up (#3692, #3706): an input 8x the size must cost about 8x as much, not 64x. -//! -//! The cost is the steps the lookups' walks count ([`crate::work`]), not a timing, so a busy -//! machine can't make a linear lookup look quadratic or the other way round. Only proofing text, -//! whose walk is the document model's, is timed. - -use super::tests::{SCALE, cost_ratio}; -use super::{ - Block, CharProps, DocLayout, Document, HeaderSet, HeightRule, InlineObject, LayoutCache, LayoutOptions, ParaLayout, Paragraph, Path, Placed, - SectionProps, SectionStart, StoryRef, display, layout, para, work, -}; -use wordcraft_doc::para::Run; -use wordcraft_doc::{Pos, Table}; - -fn lay(d: &Document, opts: &LayoutOptions) -> DocLayout { - layout(d, &mut LayoutCache::new(), opts) -} - -/// The most steps 8x the input may take, against the steps of the input: linear walks take -/// about 8x (a little more where a fixed cost is shared out), quadratic ones about 64x. -const MAX_STEP_RATIO: f64 = 1.5 * SCALE as f64; - -/// Asserts `f` takes about [`SCALE`] times the steps ([`crate::work`]) on `make(SCALE * n)` as on -/// `make(n)`, not [`SCALE`]² times. Making the inputs isn't counted. -pub(crate) fn assert_linear_steps(what: &str, n: usize, make: impl Fn(usize) -> T, f: impl Fn(&T)) { - let (small, big) = (make(n), make(SCALE * n)); - let (s, b) = (work::count(|| f(&small)), work::count(|| f(&big))); - let ratio = b as f64 / s.max(1) as f64; - eprintln!("{what}: {s} steps for n, {b} for {SCALE}n, ratio {ratio:.1}"); - assert!(s > 0, "{what}: no steps counted, so the walk this test watches no longer counts them (crate::work)"); - assert!(ratio <= MAX_STEP_RATIO, "{what}: {SCALE}x the input took {ratio:.1}x the steps ({s} → {b}): not linear"); -} - -/// [`assert_linear_steps`] for laying out `doc(n)`. -fn assert_layout_linear(what: &str, n: usize, doc: impl Fn(usize) -> Document, opts: &LayoutOptions) { - assert_linear_steps(what, n, doc, |d| drop(std::hint::black_box(lay(d, opts)))); -} - -/// The first laid-out body paragraph on page 0. -fn first_para(l: &DocLayout) -> Option<&ParaLayout> { - l.pages.first()?.items.iter().find_map(|it| match it { - Placed::Lines { para, story: StoryRef::Body, .. } => Some(para.as_ref()), - _ => None, - }) -} - -/// Finding 29: proofing text found each character's run from the paragraph's first run. -#[test] -fn proof_text_walks_the_runs_once() { - // One run per character, every other one deleted. - let para = |n: usize| { - let mut p = Paragraph::with_text(&"a".repeat(n), CharProps::default()); - p.runs = (0..n).map(|i| Run { len: 1, props: CharProps { del: (i % 2 == 1).then_some(0), ..Default::default() } }).collect(); - p - }; - assert_eq!(para::proof_text(¶(5)), "a\u{1}a\u{1}a"); - // The runs are the document model's, whose walk counts no steps, so this one is timed: the - // CPU time this thread ran (`cost_ratio`). At 50,000 runs the old walk took 64x as long for - // 8x the runs, and the new one about 8x; a limit of 24x stays far from both, and a ratio over - // it is measured again, twice, before the test fails, so a busy spell can't fail it alone. - let (small, big) = (para(50_000), para(SCALE * 50_000)); - let proof_8 = |p: &Paragraph| (0..8).for_each(|_| drop(std::hint::black_box(para::proof_text(p)))); - let mut ratios = Vec::new(); - for _ in 0..3 { - let (t1, tk, ratio) = cost_ratio(SCALE, || proof_8(&small), || proof_8(&big)); - eprintln!("proof_text: t(n) {t1:.4} s, t(8n) {tk:.4} s, ratio {ratio:.1}"); - if ratio < 24.0 { - return; - } - ratios.push(ratio); - } - panic!("proof_text: 8x the runs took {ratios:.1?}x the time, three times over: not linear"); -} - -/// A right-to-left paragraph whose one line holds `n` hidden letters: caret offsets with no width. -fn bidi_hidden(n: usize) -> Document { - let head = "سلام "; - let mut d = Document::from_text(&format!("{head}{} world", "x".repeat(n))); - let at = Pos::body(0, 0); - d.format_paragraphs(&at, &at, &|p| p.bidi = Some(true)).unwrap(); - d.format_range(&Pos::body(0, head.len()), &Pos::body(0, head.len() + n), &|c| c.hidden = Some(true)).unwrap(); - d -} - -/// Finding 5: a click on a bidirectional line and a visual caret step measured every caret -/// offset from the line's first cluster. -#[test] -fn bidi_clicks_and_caret_steps_take_linear_time() { - let opts = LayoutOptions { show_hidden: false, ..Default::default() }; - let laid = |n: usize| lay(&bidi_hidden(n), &opts); - let Some(pb) = first_para(&laid(SCALE * 5000)).map(|p| p.lines.len()) else { panic!("no paragraph") }; - assert_eq!(pb, 1, "the hidden letters stay on one line"); - assert_linear_steps("bidi hit test", 5000, laid, |l| { - let Some(p) = first_para(l) else { panic!("no paragraph") }; - std::hint::black_box(p.off_at_x(0, 1.0)); - std::hint::black_box(l.visual_step(&Pos::body(0, 0), true, 0)); - }); -} - -/// The carets of a line walked in one pass are where `x_of` puts each of them, on bidirectional -/// and left-to-right lines alike. -#[test] -fn line_carets_match_x_of() { - let docs = [bidi_hidden(20), Document::from_text("Plain left-to-right text, long enough to wrap. ".repeat(6).as_str()), { - let mut d = Document::from_text("سلام world 123 دنیا (مرحبا) end"); - let at = Pos::body(0, 0); - d.format_paragraphs(&at, &at, &|p| p.bidi = Some(true)).unwrap(); - d - }]; - for d in &docs { - for show_hidden in [false, true] { - let l = lay(d, &LayoutOptions { show_hidden, ..Default::default() }); - let Some(p) = first_para(&l) else { panic!("no paragraph") }; - for li in 0..p.lines.len() { - let want: Vec<_> = p.line_offsets(li).into_iter().map(|off| (off, p.x_of(li, off))).collect(); - assert!(!want.is_empty()); - assert_eq!(p.line_carets(li), want, "line {li}"); - } - } - } -} - -/// Finding 3: autofitting a table counted its grid's columns again for every spanning cell. -#[test] -fn autofit_tables_with_a_spanning_cell_in_every_row_lay_out_in_linear_time() { - // Rows with no cells, only a column skipped before and after them with a width: two - // spanning entries a row for next to no layout, so counting the grid per entry dominates. - let doc = |n: usize| { - let mut t = Table::new(n, 1, 468.0); - for row in t.rows.iter_mut().skip(1) { - row.cells.clear(); - row.props.grid_before = 1; - row.props.w_before = Some(10.0); - row.props.grid_after = 1; - row.props.w_after = Some(10.0); - } - let mut d = Document::from_text("x"); - d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap(); - d - }; - assert_layout_linear("spanning cells", 2000, doc, &LayoutOptions::default()); -} - -/// `n` continuous sections of an empty paragraph each, each with a different first page. The -/// first section names every header and footer; the others name them too, or (`inherit`) name -/// none and link to the previous section's. -fn sections(n: usize, inherit: bool) -> Document { - use wordcraft_doc::PartKind::{Footer, Header}; - let mut d = Document::from_text(&"\n".repeat(n.saturating_sub(1))); - let mut part = |k| d.add_part(k, vec![wordcraft_doc::para_block(Paragraph::with_text("Head", CharProps::default()))]); - let ids = [part(Header), part(Header), part(Header), part(Footer), part(Footer), part(Footer)]; - let named = SectionProps { - start: SectionStart::Continuous, - title_page: true, - headers: HeaderSet { default: Some(ids[0]), first: Some(ids[1]), even: Some(ids[2]) }, - footers: HeaderSet { default: Some(ids[3]), first: Some(ids[4]), even: Some(ids[5]) }, - ..Default::default() - }; - let linked = SectionProps { headers: HeaderSet::default(), footers: HeaderSet::default(), ..named.clone() }; - for i in 0..n.saturating_sub(1) { - let s = if i == 0 || !inherit { named.clone() } else { linked.clone() }; - d.para_mut(StoryRef::Body, &Path::top(i)).unwrap().section = Some(Box::new(s)); - } - d.last_section = if inherit { linked } else { named }; - d -} - -/// Finding 13: each section's headers and footers were found by walking back through the -/// sections before it, for every section and again for every page. Linked to the previous -/// section's, they must cost no more to lay out than named in each section. -#[test] -fn headers_inherited_through_many_sections_lay_out_in_linear_time() { - let l = lay(§ions(200, true), &LayoutOptions::default()); - assert!(l.pages.iter().all(|p| !p.header.is_empty() && !p.footer.is_empty()), "the header and footer are inherited"); - let opts = LayoutOptions::default(); - assert_layout_linear("inherited headers", 1000, |n| sections(n, true), &opts); - let steps = |inherit: bool| work::count(|| drop(std::hint::black_box(lay(§ions(1000, inherit), &opts)))); - let (named, inherited) = (steps(false), steps(true)); - assert!(inherited <= 2 * named, "1000 inheriting sections took {inherited} steps, named ones {named}"); -} - -/// A header of `n` short paragraphs. -fn tall_header(n: usize) -> (Document, u32) { - let mut d = Document::from_text("Body"); - let paras = (0..n).map(|i| wordcraft_doc::para_block(Paragraph::with_text(&format!("h{i}"), CharProps::default()))).collect(); - let id = d.add_part(wordcraft_doc::PartKind::Header, paras); - d.last_section.headers.default = Some(id); - (d, id) -} - -/// Finding 28: selecting across a header looked through the whole page for each paragraph. -#[test] -fn selecting_a_long_header_takes_linear_time() { - // The header laid out, and the selection from its first paragraph to its last. - let laid = |n: usize| { - let (d, id) = tall_header(n); - let l = lay(&d, &LayoutOptions::default()); - let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(n - 1), 2)); - (d, l, ends) - }; - let (d, l, (a, b)) = laid(SCALE * 300); - assert!(l.selection_rects(&d, &a, &b, 0).len() >= SCALE * 300, "a rectangle per paragraph"); - assert_linear_steps("header selection", 300, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0)))); -} - -/// An endnote of `n` paragraphs, a few lines each, continued over many pages. -fn long_endnote(n: usize) -> (Document, u32) { - let mut d = Document::from_text("Body"); - // One huge letter to a paragraph, one line high enough for a page, so the pages are as many as - // the paragraphs and each paragraph costs next to nothing. - let huge = CharProps { size: Some(400.0), ..Default::default() }; - let paras = (0..n).map(|_| wordcraft_doc::para_block(Paragraph::with_text("n", huge.clone()))).collect(); - let id = d.add_part(wordcraft_doc::PartKind::Endnote, paras); - let r = InlineObject::NoteRef { kind: wordcraft_doc::para::NoteKind::Endnote, id, custom: String::new() }; - d.insert_object(&Pos::body(0, 4), r, &Default::default()).unwrap(); - (d, id) -} - -/// #3866: selecting across an endnote over many pages probed the pages from the first for each -/// paragraph (paragraphs x pages). -#[test] -fn selecting_a_long_endnote_takes_linear_time() { - let laid = |n: usize| { - let (d, id) = long_endnote(n); - let l = lay(&d, &LayoutOptions::default()); - let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(n - 1), 2)); - (d, l, ends) - }; - let (d, l, (a, b)) = laid(SCALE * 20); - assert!(l.pages.len() >= SCALE * 20 / 2, "the endnote runs over many pages: {}", l.pages.len()); - assert!(l.selection_rects(&d, &a, &b, 0).len() >= SCALE * 20, "a rectangle per paragraph"); - assert_linear_steps("endnote selection", 20, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0)))); -} - -/// A table of `n` rows of 60 cells, each row exactly half a point high, so every cell clips its -/// (empty) paragraph and the whole table fits on the first page. -fn clipped_table(n: usize) -> Document { - let mut t = Table::new(n, 60, 468.0); - for row in &mut t.rows { - row.props.height = Some(0.5); - row.props.height_rule = HeightRule::Exact; - } - let mut d = Document::from_text("x"); - d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap(); - d -} - -/// Finding 1: drawing a line in a clipped cell looked through every clipped cell on the page. -#[test] -fn drawing_many_clipped_cells_takes_linear_time() { - let laid = |n: usize| { - let d = clipped_table(n); - let l = lay(&d, &LayoutOptions::default()); - (d, l) - }; - let opts = display::DisplayOptions::default(); - let draw = |(d, l): &(Document, DocLayout)| l.pages.first().map(|p| display::page_display(d, p, &opts)).unwrap_or_default(); - let clips = draw(&laid(SCALE * 16)).iter().filter(|i| matches!(i, display::Draw::Clip { .. })).count(); - assert_eq!(clips, SCALE * 16 * 60, "every cell's line is clipped, all on the first page"); - assert_linear_steps("clipped cells", 16, laid, |dl| drop(std::hint::black_box(draw(dl)))); -} - -/// `n` words, each inserted by its own author, at a size that fits them all on the first page. -fn tracked_words(n: usize) -> Document { - let mut d = Document::from_text("x"); - let p = Paragraph { - text: "word ".repeat(n), - runs: (0..n).map(|i| Run { len: 5, props: CharProps { ins: u32::try_from(i).ok(), size: Some(1.0), ..Default::default() } }).collect(), - ..Paragraph::with_text("", CharProps::default()) - }; - d.body = vec![wordcraft_doc::para_block(p)]; - d.revisions = (0..n).map(|i| wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, format!("Author {i}"), "")).collect(); - d -} - -/// Finding 30 (#3706): every tracked run ranked its author among all the document's revisions. -#[test] -fn drawing_tracked_changes_by_many_authors_takes_linear_time() { - let laid = |n: usize| { - let d = tracked_words(n); - let l = lay(&d, &LayoutOptions::default()); - (d, l) - }; - assert_eq!(laid(SCALE * 1000).1.pages.len(), 1); - let opts = display::DisplayOptions { markup: true, ..Default::default() }; - let draw = |(d, l): &(Document, DocLayout)| l.pages.first().map(|p| display::page_display(d, p, &opts)).unwrap_or_default(); - // Every run is drawn in its author's colour, so the authors are ranked at least once. - assert_linear_steps("tracked runs", 1000, laid, |dl| drop(std::hint::black_box(draw(dl)))); -} - -/// Each author keeps the colour of their first revision's place among the authors. -#[test] -fn tracked_changes_colour_by_author_in_order_of_appearance() { - let mut d = tracked_words(3); - // Words 0 and 2 by Ana, word 1 by Ben: Ana first, Ben second. - for (r, a) in d.revisions.iter_mut().zip(["Ana", "Ben", "Ana"]) { - r.author = a.into(); - } - let l = lay(&d, &LayoutOptions::default()); - let opts = display::DisplayOptions { markup: true, ..Default::default() }; - let colours: Vec<_> = display::page_display(&d, &l.pages[0], &opts) - .into_iter() - .filter_map(|i| if let display::Draw::Glyphs { color, .. } = i { Some(color) } else { None }) - .collect(); - let (ana, ben) = (display::revision_color(0), display::revision_color(1)); - assert_ne!(ana, ben); - assert_eq!(colours, [ana, ben, ana]); -} - -/// Finding 30 (#3706): every comment balloon ranked its author among all the comments. -#[test] -fn balloons_for_many_authors_lay_out_in_linear_time() { - let doc = |n: usize| { - let mut d = Document::from_text(&"word ".repeat(n)); - for i in (0..n).rev() { - let part = d.add_part(wordcraft_doc::PartKind::Comment, vec![wordcraft_doc::para_block(Paragraph::with_text("c", CharProps::default()))]); - let id = u32::try_from(i).unwrap(); - d.comments.insert(id, wordcraft_doc::Comment { author: format!("Author {i}"), part, ..Default::default() }); - let p = CharProps::default(); - d.insert_object(&Pos::body(0, 5 * i + 4), InlineObject::CommentEnd { id }, &p).unwrap(); - d.insert_object(&Pos::body(0, 5 * i), InlineObject::CommentStart { id }, &p).unwrap(); - } - d - }; - let opts = LayoutOptions { comment_balloons: true, ..Default::default() }; - assert_layout_linear("comment balloons", 120, doc, &opts); -} - -/// `n` one-word paragraphs, each inserted by its own author, small enough for one page. -fn tracked_paras(n: usize) -> Document { - let mut d = Document::from_text("x"); - d.body = (0..n) - .map(|i| { - let run = Run { len: 1, props: CharProps { ins: u32::try_from(i).ok(), size: Some(1.0), ..Default::default() } }; - let mut p = Paragraph { runs: vec![run], ..Paragraph::with_text("w", CharProps::default()) }; - p.mark.size = Some(1.0); - p.props.space_before = Some(0.0); - p.props.space_after = Some(0.0); - wordcraft_doc::para_block(p) - }) - .collect(); - d.revisions = (0..n).map(|i| wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, format!("Author {i}"), "")).collect(); - d -} - -/// Draws every item of the first page on its own, as the PDF export does. -fn draw_items((d, l): &(Document, DocLayout), opts: &display::DisplayOptions) { - let Some(p) = l.pages.first() else { return }; - let shown = display::ItemDisplay::new(d, p); - for it in p.header.iter().chain(&p.footer).chain(&p.items) { - std::hint::black_box(shown.draws(it, opts)); - } -} - -/// #3866: the PDF export drew each item with a `page_display` of its own, which ranked the revision -/// authors again for every item with a tracked change (lines x revisions). -#[test] -fn drawing_items_one_by_one_ranks_the_authors_once() { - let laid = |n: usize| { - let d = tracked_paras(n); - let l = lay(&d, &LayoutOptions::default()); - (d, l) - }; - assert_eq!(laid(SCALE * 40).1.pages.len(), 1); - let opts = display::DisplayOptions { markup: true, ..Default::default() }; - assert_linear_steps("items drawn one by one", 40, laid, |dl| draw_items(dl, &opts)); -} - -/// #3866: the same for the clipped cells, which each item's `page_display` collected again -/// (lines x clipped cells). -#[test] -fn drawing_items_one_by_one_collects_the_clipped_cells_once() { - let laid = |n: usize| { - let d = clipped_table(n); - let l = lay(&d, &LayoutOptions::default()); - (d, l) - }; - let opts = display::DisplayOptions::default(); - assert_linear_steps("clipped cells, items one by one", 16, laid, |dl| draw_items(dl, &opts)); -} - -/// #3866: a comment's balloon is in its author's colour, authors numbered in order of appearance. -#[test] -fn balloons_are_in_their_authors_colours() { - let mut d = Document::from_text("one two three"); - for (i, a) in ["Ana", "Ben", "Ana"].into_iter().enumerate() { - let part = d.add_part(wordcraft_doc::PartKind::Comment, vec![wordcraft_doc::para_block(Paragraph::with_text("c", CharProps::default()))]); - let id = u32::try_from(i).unwrap(); - d.comments.insert(id, wordcraft_doc::Comment { author: a.into(), part, ..Default::default() }); - let p = CharProps::default(); - d.insert_object(&Pos::body(0, 0), InlineObject::CommentEnd { id }, &p).unwrap(); - d.insert_object(&Pos::body(0, 0), InlineObject::CommentStart { id }, &p).unwrap(); - } - let l = lay(&d, &LayoutOptions { comment_balloons: true, ..Default::default() }); - // Each card's stripe is a 3 pt wide fill in the author's colour; the cards run down the page. - let mut stripes: Vec<_> = l.pages[0] - .items - .iter() - .filter_map(|it| match it { - Placed::Fill { rect, color } if rect.w == 3.0 => Some((rect.y, *color)), - _ => None, - }) - .collect(); - stripes.sort_by(|a, b| a.0.total_cmp(&b.0)); - let (ana, ben) = (display::revision_color(0), display::revision_color(1)); - assert_ne!(ana, ben); - assert_eq!(stripes.iter().map(|s| s.1).collect::>(), [ana, ben, ana]); -} - -/// A page with tracked changes by several authors in its body and header, and exact-height -/// tables, one nested in a cell of another, whose cells clip their lines. -fn mixed_page() -> (Document, DocLayout) { - let mut d = tracked_paras(6); - let mut hp = Paragraph::with_text("head", CharProps::default()); - hp.runs = vec![Run { len: 4, props: CharProps { ins: Some(2), ..Default::default() } }]; - let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(hp)]); - d.last_section.headers.default = Some(id); - let exact = |rows, cols| { - let mut t = Table::new(rows, cols, 200.0); - for row in &mut t.rows { - row.props.height = Some(0.5); - row.props.height_rule = HeightRule::Exact; - } - t - }; - // A cell with text in it holds a nested table. - let mut outer = exact(2, 2); - outer.rows[0].cells[0].blocks.push(std::sync::Arc::new(Block::Table(exact(2, 2)))); - d.insert_block(StoryRef::Body, &Path::top(1), Block::Table(outer)).unwrap(); - d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(exact(2, 3))).unwrap(); - let l = lay(&d, &LayoutOptions::default()); - (d, l) -} - -/// What drawing `it` on a page of just it (and every clipped cell, for a line) gave before the -/// page's authors and clipped cells were shared: a `page_display` of its own. -fn drawn_alone(d: &Document, page: &Page, it: &Placed, opts: &display::DisplayOptions) -> Vec { - let mut items = vec![it.clone()]; - if matches!(it, Placed::Lines { .. }) { - let cells = page.header.iter().chain(&page.footer).chain(&page.items).filter(|c| matches!(c, Placed::Cell { clip: true, .. })); - items.extend(cells.cloned()); - } - let one = Page { w: page.w, h: page.h, section: page.section, number: page.number, items, body: page.body, ..Default::default() }; - display::page_display(d, &one, opts) -} - -/// #3866: drawing the items of a page one by one with the page's shared authors and clipped -/// cells gives what a `page_display` of each alone gave. -#[test] -fn drawing_items_one_by_one_matches_a_page_display_of_each() { - let (d, l) = mixed_page(); - let page = &l.pages[0]; - let clipped = page.items.iter().filter(|c| matches!(c, Placed::Cell { clip: true, .. })).count(); - assert!(clipped >= 8, "clipped cells, nested ones among them: {clipped}"); - assert!(!page.header.is_empty(), "a header on the page"); - for markup in [true, false] { - let opts = display::DisplayOptions { markup, ..Default::default() }; - let shown = display::ItemDisplay::new(&d, page); - let (mut clips, mut coloured) = (0, 0); - for it in page.header.iter().chain(&page.footer).chain(&page.items) { - let (got, want) = (shown.draws(it, &opts), drawn_alone(&d, page, it, &opts)); - assert_eq!(format!("{got:?}"), format!("{want:?}"), "markup {markup}: {it:?}"); - clips += got.iter().filter(|x| matches!(x, display::Draw::Clip { .. })).count(); - coloured += got.iter().filter(|x| matches!(x, display::Draw::Glyphs { color, .. } if *color != Rgb(0, 0, 0))).count(); - } - assert!(clips > 0, "some lines are clipped"); - assert!(!markup || coloured > 0, "some tracked text is in an author's colour"); - } -} - -/// #3866: a header paragraph on the hinted page is found there, without a walk of every page of a -/// long document to find the first page that shows it. -#[test] -fn selecting_a_header_on_the_hinted_page_does_not_walk_every_page() { - let laid = |n: usize| { - let mut d = Document::from_text("n"); - let huge = CharProps { size: Some(400.0), ..Default::default() }; - d.body = (0..n).map(|_| wordcraft_doc::para_block(Paragraph::with_text("n", huge.clone()))).collect(); - let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(Paragraph::with_text("head", CharProps::default()))]); - d.last_section.headers.default = Some(id); - let l = lay(&d, &LayoutOptions::default()); - let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(0), 2)); - (d, l, ends) - }; - let steps = |n: usize| { - let (d, l, (a, b)) = laid(n); - assert!(l.pages.len() >= n, "a page to each paragraph: {}", l.pages.len()); - work::count(|| drop(std::hint::black_box(l.selection_rects(&d, &a, &b, 0)))) - }; - let (small, big) = (steps(20), steps(160)); - eprintln!("hinted header: {small} steps for 20 pages, {big} for 160"); - assert!(small > 0 && big <= small + 8, "the pages aren't walked: {small} → {big}"); -} +//! Lookups that once rescanned the page, the paragraph or the document for every item they +//! looked up (#3692, #3706): an input 8x the size must cost about 8x as much, not 64x. +//! +//! The cost is the steps the lookups' walks count ([`crate::work`]), not a timing, so a busy +//! machine can't make a linear lookup look quadratic or the other way round. Only proofing text, +//! whose walk is the document model's, is timed. + +use super::tests::{SCALE, cost_ratio}; +use super::{ + Block, CharProps, DocLayout, Document, HeaderSet, HeightRule, InlineObject, LayoutCache, LayoutOptions, Page, ParaLayout, Paragraph, Path, + Placed, Rgb, SectionProps, SectionStart, StoryRef, display, layout, para, work, +}; +use wordcraft_doc::para::Run; +use wordcraft_doc::{Pos, Table}; + +fn lay(d: &Document, opts: &LayoutOptions) -> DocLayout { + layout(d, &mut LayoutCache::new(), opts) +} + +/// The most steps 8x the input may take, against the steps of the input: linear walks take +/// about 8x (a little more where a fixed cost is shared out), quadratic ones about 64x. +const MAX_STEP_RATIO: f64 = 1.5 * SCALE as f64; + +/// Asserts `f` takes about [`SCALE`] times the steps ([`crate::work`]) on `make(SCALE * n)` as on +/// `make(n)`, not [`SCALE`]² times. Making the inputs isn't counted. +pub(crate) fn assert_linear_steps(what: &str, n: usize, make: impl Fn(usize) -> T, f: impl Fn(&T)) { + let (small, big) = (make(n), make(SCALE * n)); + let (s, b) = (work::count(|| f(&small)), work::count(|| f(&big))); + let ratio = b as f64 / s.max(1) as f64; + eprintln!("{what}: {s} steps for n, {b} for {SCALE}n, ratio {ratio:.1}"); + assert!(s > 0, "{what}: no steps counted, so the walk this test watches no longer counts them (crate::work)"); + assert!(ratio <= MAX_STEP_RATIO, "{what}: {SCALE}x the input took {ratio:.1}x the steps ({s} → {b}): not linear"); +} + +/// [`assert_linear_steps`] for laying out `doc(n)`. +fn assert_layout_linear(what: &str, n: usize, doc: impl Fn(usize) -> Document, opts: &LayoutOptions) { + assert_linear_steps(what, n, doc, |d| drop(std::hint::black_box(lay(d, opts)))); +} + +/// The first laid-out body paragraph on page 0. +fn first_para(l: &DocLayout) -> Option<&ParaLayout> { + l.pages.first()?.items.iter().find_map(|it| match it { + Placed::Lines { para, story: StoryRef::Body, .. } => Some(para.as_ref()), + _ => None, + }) +} + +/// Finding 29: proofing text found each character's run from the paragraph's first run. +#[test] +fn proof_text_walks_the_runs_once() { + // One run per character, every other one deleted. + let para = |n: usize| { + let mut p = Paragraph::with_text(&"a".repeat(n), CharProps::default()); + p.runs = (0..n).map(|i| Run { len: 1, props: CharProps { del: (i % 2 == 1).then_some(0), ..Default::default() } }).collect(); + p + }; + assert_eq!(para::proof_text(¶(5)), "a\u{1}a\u{1}a"); + // The runs are the document model's, whose walk counts no steps, so this one is timed: the + // CPU time this thread ran (`cost_ratio`). At 50,000 runs the old walk took 64x as long for + // 8x the runs, and the new one about 8x; a limit of 24x stays far from both, and a ratio over + // it is measured again, twice, before the test fails, so a busy spell can't fail it alone. + let (small, big) = (para(50_000), para(SCALE * 50_000)); + let proof_8 = |p: &Paragraph| (0..8).for_each(|_| drop(std::hint::black_box(para::proof_text(p)))); + let mut ratios = Vec::new(); + for _ in 0..3 { + let (t1, tk, ratio) = cost_ratio(SCALE, || proof_8(&small), || proof_8(&big)); + eprintln!("proof_text: t(n) {t1:.4} s, t(8n) {tk:.4} s, ratio {ratio:.1}"); + if ratio < 24.0 { + return; + } + ratios.push(ratio); + } + panic!("proof_text: 8x the runs took {ratios:.1?}x the time, three times over: not linear"); +} + +/// A right-to-left paragraph whose one line holds `n` hidden letters: caret offsets with no width. +fn bidi_hidden(n: usize) -> Document { + let head = "سلام "; + let mut d = Document::from_text(&format!("{head}{} world", "x".repeat(n))); + let at = Pos::body(0, 0); + d.format_paragraphs(&at, &at, &|p| p.bidi = Some(true)).unwrap(); + d.format_range(&Pos::body(0, head.len()), &Pos::body(0, head.len() + n), &|c| c.hidden = Some(true)).unwrap(); + d +} + +/// Finding 5: a click on a bidirectional line and a visual caret step measured every caret +/// offset from the line's first cluster. +#[test] +fn bidi_clicks_and_caret_steps_take_linear_time() { + let opts = LayoutOptions { show_hidden: false, ..Default::default() }; + let laid = |n: usize| lay(&bidi_hidden(n), &opts); + let Some(pb) = first_para(&laid(SCALE * 5000)).map(|p| p.lines.len()) else { panic!("no paragraph") }; + assert_eq!(pb, 1, "the hidden letters stay on one line"); + assert_linear_steps("bidi hit test", 5000, laid, |l| { + let Some(p) = first_para(l) else { panic!("no paragraph") }; + std::hint::black_box(p.off_at_x(0, 1.0)); + std::hint::black_box(l.visual_step(&Pos::body(0, 0), true, 0)); + }); +} + +/// The carets of a line walked in one pass are where `x_of` puts each of them, on bidirectional +/// and left-to-right lines alike. +#[test] +fn line_carets_match_x_of() { + let docs = [bidi_hidden(20), Document::from_text("Plain left-to-right text, long enough to wrap. ".repeat(6).as_str()), { + let mut d = Document::from_text("سلام world 123 دنیا (مرحبا) end"); + let at = Pos::body(0, 0); + d.format_paragraphs(&at, &at, &|p| p.bidi = Some(true)).unwrap(); + d + }]; + for d in &docs { + for show_hidden in [false, true] { + let l = lay(d, &LayoutOptions { show_hidden, ..Default::default() }); + let Some(p) = first_para(&l) else { panic!("no paragraph") }; + for li in 0..p.lines.len() { + let want: Vec<_> = p.line_offsets(li).into_iter().map(|off| (off, p.x_of(li, off))).collect(); + assert!(!want.is_empty()); + assert_eq!(p.line_carets(li), want, "line {li}"); + } + } + } +} + +/// Finding 3: autofitting a table counted its grid's columns again for every spanning cell. +#[test] +fn autofit_tables_with_a_spanning_cell_in_every_row_lay_out_in_linear_time() { + // Rows with no cells, only a column skipped before and after them with a width: two + // spanning entries a row for next to no layout, so counting the grid per entry dominates. + let doc = |n: usize| { + let mut t = Table::new(n, 1, 468.0); + for row in t.rows.iter_mut().skip(1) { + row.cells.clear(); + row.props.grid_before = 1; + row.props.w_before = Some(10.0); + row.props.grid_after = 1; + row.props.w_after = Some(10.0); + } + let mut d = Document::from_text("x"); + d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap(); + d + }; + assert_layout_linear("spanning cells", 2000, doc, &LayoutOptions::default()); +} + +/// `n` continuous sections of an empty paragraph each, each with a different first page. The +/// first section names every header and footer; the others name them too, or (`inherit`) name +/// none and link to the previous section's. +fn sections(n: usize, inherit: bool) -> Document { + use wordcraft_doc::PartKind::{Footer, Header}; + let mut d = Document::from_text(&"\n".repeat(n.saturating_sub(1))); + let mut part = |k| d.add_part(k, vec![wordcraft_doc::para_block(Paragraph::with_text("Head", CharProps::default()))]); + let ids = [part(Header), part(Header), part(Header), part(Footer), part(Footer), part(Footer)]; + let named = SectionProps { + start: SectionStart::Continuous, + title_page: true, + headers: HeaderSet { default: Some(ids[0]), first: Some(ids[1]), even: Some(ids[2]) }, + footers: HeaderSet { default: Some(ids[3]), first: Some(ids[4]), even: Some(ids[5]) }, + ..Default::default() + }; + let linked = SectionProps { headers: HeaderSet::default(), footers: HeaderSet::default(), ..named.clone() }; + for i in 0..n.saturating_sub(1) { + let s = if i == 0 || !inherit { named.clone() } else { linked.clone() }; + d.para_mut(StoryRef::Body, &Path::top(i)).unwrap().section = Some(Box::new(s)); + } + d.last_section = if inherit { linked } else { named }; + d +} + +/// Finding 13: each section's headers and footers were found by walking back through the +/// sections before it, for every section and again for every page. Linked to the previous +/// section's, they must cost no more to lay out than named in each section. +#[test] +fn headers_inherited_through_many_sections_lay_out_in_linear_time() { + let l = lay(§ions(200, true), &LayoutOptions::default()); + assert!(l.pages.iter().all(|p| !p.header.is_empty() && !p.footer.is_empty()), "the header and footer are inherited"); + let opts = LayoutOptions::default(); + assert_layout_linear("inherited headers", 1000, |n| sections(n, true), &opts); + let steps = |inherit: bool| work::count(|| drop(std::hint::black_box(lay(§ions(1000, inherit), &opts)))); + let (named, inherited) = (steps(false), steps(true)); + assert!(inherited <= 2 * named, "1000 inheriting sections took {inherited} steps, named ones {named}"); +} + +/// A header of `n` short paragraphs. +fn tall_header(n: usize) -> (Document, u32) { + let mut d = Document::from_text("Body"); + let paras = (0..n).map(|i| wordcraft_doc::para_block(Paragraph::with_text(&format!("h{i}"), CharProps::default()))).collect(); + let id = d.add_part(wordcraft_doc::PartKind::Header, paras); + d.last_section.headers.default = Some(id); + (d, id) +} + +/// Finding 28: selecting across a header looked through the whole page for each paragraph. +#[test] +fn selecting_a_long_header_takes_linear_time() { + // The header laid out, and the selection from its first paragraph to its last. + let laid = |n: usize| { + let (d, id) = tall_header(n); + let l = lay(&d, &LayoutOptions::default()); + let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(n - 1), 2)); + (d, l, ends) + }; + let (d, l, (a, b)) = laid(SCALE * 300); + assert!(l.selection_rects(&d, &a, &b, 0).len() >= SCALE * 300, "a rectangle per paragraph"); + assert_linear_steps("header selection", 300, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0)))); +} + +/// An endnote of `n` paragraphs, a few lines each, continued over many pages. +fn long_endnote(n: usize) -> (Document, u32) { + let mut d = Document::from_text("Body"); + // One huge letter to a paragraph, one line high enough for a page, so the pages are as many as + // the paragraphs and each paragraph costs next to nothing. + let huge = CharProps { size: Some(400.0), ..Default::default() }; + let paras = (0..n).map(|_| wordcraft_doc::para_block(Paragraph::with_text("n", huge.clone()))).collect(); + let id = d.add_part(wordcraft_doc::PartKind::Endnote, paras); + let r = InlineObject::NoteRef { kind: wordcraft_doc::para::NoteKind::Endnote, id, custom: String::new() }; + d.insert_object(&Pos::body(0, 4), r, &Default::default()).unwrap(); + (d, id) +} + +/// #3866: selecting across an endnote over many pages probed the pages from the first for each +/// paragraph (paragraphs x pages). +#[test] +fn selecting_a_long_endnote_takes_linear_time() { + let laid = |n: usize| { + let (d, id) = long_endnote(n); + let l = lay(&d, &LayoutOptions::default()); + let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(n - 1), 2)); + (d, l, ends) + }; + let (d, l, (a, b)) = laid(SCALE * 20); + assert!(l.pages.len() >= SCALE * 20 / 2, "the endnote runs over many pages: {}", l.pages.len()); + assert!(l.selection_rects(&d, &a, &b, 0).len() >= SCALE * 20, "a rectangle per paragraph"); + assert_linear_steps("endnote selection", 20, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0)))); +} + +/// A table of `n` rows of 60 cells, each row exactly half a point high, so every cell clips its +/// (empty) paragraph and the whole table fits on the first page. +fn clipped_table(n: usize) -> Document { + let mut t = Table::new(n, 60, 468.0); + for row in &mut t.rows { + row.props.height = Some(0.5); + row.props.height_rule = HeightRule::Exact; + } + let mut d = Document::from_text("x"); + d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap(); + d +} + +/// Finding 1: drawing a line in a clipped cell looked through every clipped cell on the page. +#[test] +fn drawing_many_clipped_cells_takes_linear_time() { + let laid = |n: usize| { + let d = clipped_table(n); + let l = lay(&d, &LayoutOptions::default()); + (d, l) + }; + let opts = display::DisplayOptions::default(); + let draw = |(d, l): &(Document, DocLayout)| l.pages.first().map(|p| display::page_display(d, p, &opts)).unwrap_or_default(); + let clips = draw(&laid(SCALE * 16)).iter().filter(|i| matches!(i, display::Draw::Clip { .. })).count(); + assert_eq!(clips, SCALE * 16 * 60, "every cell's line is clipped, all on the first page"); + assert_linear_steps("clipped cells", 16, laid, |dl| drop(std::hint::black_box(draw(dl)))); +} + +/// `n` words, each inserted by its own author, at a size that fits them all on the first page. +fn tracked_words(n: usize) -> Document { + let mut d = Document::from_text("x"); + let p = Paragraph { + text: "word ".repeat(n), + runs: (0..n).map(|i| Run { len: 5, props: CharProps { ins: u32::try_from(i).ok(), size: Some(1.0), ..Default::default() } }).collect(), + ..Paragraph::with_text("", CharProps::default()) + }; + d.body = vec![wordcraft_doc::para_block(p)]; + d.revisions = (0..n).map(|i| wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, format!("Author {i}"), "")).collect(); + d +} + +/// Finding 30 (#3706): every tracked run ranked its author among all the document's revisions. +#[test] +fn drawing_tracked_changes_by_many_authors_takes_linear_time() { + let laid = |n: usize| { + let d = tracked_words(n); + let l = lay(&d, &LayoutOptions::default()); + (d, l) + }; + assert_eq!(laid(SCALE * 1000).1.pages.len(), 1); + let opts = display::DisplayOptions { markup: true, ..Default::default() }; + let draw = |(d, l): &(Document, DocLayout)| l.pages.first().map(|p| display::page_display(d, p, &opts)).unwrap_or_default(); + // Every run is drawn in its author's colour, so the authors are ranked at least once. + assert_linear_steps("tracked runs", 1000, laid, |dl| drop(std::hint::black_box(draw(dl)))); +} + +/// Each author keeps the colour of their first revision's place among the authors. +#[test] +fn tracked_changes_colour_by_author_in_order_of_appearance() { + let mut d = tracked_words(3); + // Words 0 and 2 by Ana, word 1 by Ben: Ana first, Ben second. + for (r, a) in d.revisions.iter_mut().zip(["Ana", "Ben", "Ana"]) { + r.author = a.into(); + } + let l = lay(&d, &LayoutOptions::default()); + let opts = display::DisplayOptions { markup: true, ..Default::default() }; + let colours: Vec<_> = display::page_display(&d, &l.pages[0], &opts) + .into_iter() + .filter_map(|i| if let display::Draw::Glyphs { color, .. } = i { Some(color) } else { None }) + .collect(); + let (ana, ben) = (display::revision_color(0), display::revision_color(1)); + assert_ne!(ana, ben); + assert_eq!(colours, [ana, ben, ana]); +} + +/// Finding 30 (#3706): every comment balloon ranked its author among all the comments. +#[test] +fn balloons_for_many_authors_lay_out_in_linear_time() { + let doc = |n: usize| { + let mut d = Document::from_text(&"word ".repeat(n)); + for i in (0..n).rev() { + let part = d.add_part(wordcraft_doc::PartKind::Comment, vec![wordcraft_doc::para_block(Paragraph::with_text("c", CharProps::default()))]); + let id = u32::try_from(i).unwrap(); + d.comments.insert(id, wordcraft_doc::Comment { author: format!("Author {i}"), part, ..Default::default() }); + let p = CharProps::default(); + d.insert_object(&Pos::body(0, 5 * i + 4), InlineObject::CommentEnd { id }, &p).unwrap(); + d.insert_object(&Pos::body(0, 5 * i), InlineObject::CommentStart { id }, &p).unwrap(); + } + d + }; + let opts = LayoutOptions { comment_balloons: true, ..Default::default() }; + assert_layout_linear("comment balloons", 120, doc, &opts); +} + +/// `n` one-word paragraphs, each inserted by its own author, small enough for one page. +fn tracked_paras(n: usize) -> Document { + let mut d = Document::from_text("x"); + d.body = (0..n) + .map(|i| { + let run = Run { len: 1, props: CharProps { ins: u32::try_from(i).ok(), size: Some(1.0), ..Default::default() } }; + let mut p = Paragraph { runs: vec![run], ..Paragraph::with_text("w", CharProps::default()) }; + p.mark.size = Some(1.0); + p.props.space_before = Some(0.0); + p.props.space_after = Some(0.0); + wordcraft_doc::para_block(p) + }) + .collect(); + d.revisions = (0..n).map(|i| wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, format!("Author {i}"), "")).collect(); + d +} + +/// Draws every item of the first page on its own, as the PDF export does. +fn draw_items((d, l): &(Document, DocLayout), opts: &display::DisplayOptions) { + let Some(p) = l.pages.first() else { return }; + let shown = display::ItemDisplay::new(d, p); + for it in p.header.iter().chain(&p.footer).chain(&p.items) { + std::hint::black_box(shown.draws(it, opts)); + } +} + +/// #3866: the PDF export drew each item with a `page_display` of its own, which ranked the revision +/// authors again for every item with a tracked change (lines x revisions). +#[test] +fn drawing_items_one_by_one_ranks_the_authors_once() { + let laid = |n: usize| { + let d = tracked_paras(n); + let l = lay(&d, &LayoutOptions::default()); + (d, l) + }; + assert_eq!(laid(SCALE * 40).1.pages.len(), 1); + let opts = display::DisplayOptions { markup: true, ..Default::default() }; + assert_linear_steps("items drawn one by one", 40, laid, |dl| draw_items(dl, &opts)); +} + +/// #3866: the same for the clipped cells, which each item's `page_display` collected again +/// (lines x clipped cells). +#[test] +fn drawing_items_one_by_one_collects_the_clipped_cells_once() { + let laid = |n: usize| { + let d = clipped_table(n); + let l = lay(&d, &LayoutOptions::default()); + (d, l) + }; + let opts = display::DisplayOptions::default(); + assert_linear_steps("clipped cells, items one by one", 16, laid, |dl| draw_items(dl, &opts)); +} + +/// #3866: a comment's balloon is in its author's colour, authors numbered in order of appearance. +#[test] +fn balloons_are_in_their_authors_colours() { + let mut d = Document::from_text("one two three"); + for (i, a) in ["Ana", "Ben", "Ana"].into_iter().enumerate() { + let part = d.add_part(wordcraft_doc::PartKind::Comment, vec![wordcraft_doc::para_block(Paragraph::with_text("c", CharProps::default()))]); + let id = u32::try_from(i).unwrap(); + d.comments.insert(id, wordcraft_doc::Comment { author: a.into(), part, ..Default::default() }); + let p = CharProps::default(); + d.insert_object(&Pos::body(0, 0), InlineObject::CommentEnd { id }, &p).unwrap(); + d.insert_object(&Pos::body(0, 0), InlineObject::CommentStart { id }, &p).unwrap(); + } + let l = lay(&d, &LayoutOptions { comment_balloons: true, ..Default::default() }); + // Each card's stripe is a 3 pt wide fill in the author's colour; the cards run down the page. + let mut stripes: Vec<_> = l.pages[0] + .items + .iter() + .filter_map(|it| match it { + Placed::Fill { rect, color } if rect.w == 3.0 => Some((rect.y, *color)), + _ => None, + }) + .collect(); + stripes.sort_by(|a, b| a.0.total_cmp(&b.0)); + let (ana, ben) = (display::revision_color(0), display::revision_color(1)); + assert_ne!(ana, ben); + assert_eq!(stripes.iter().map(|s| s.1).collect::>(), [ana, ben, ana]); +} + +/// A page with tracked changes by several authors in its body and header, and exact-height +/// tables, one nested in a cell of another, whose cells clip their lines. +fn mixed_page() -> (Document, DocLayout) { + let mut d = tracked_paras(6); + let mut hp = Paragraph::with_text("head", CharProps::default()); + hp.runs = vec![Run { len: 4, props: CharProps { ins: Some(2), ..Default::default() } }]; + let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(hp)]); + d.last_section.headers.default = Some(id); + let exact = |rows, cols| { + let mut t = Table::new(rows, cols, 200.0); + for row in &mut t.rows { + row.props.height = Some(0.5); + row.props.height_rule = HeightRule::Exact; + } + t + }; + // A cell with text in it holds a nested table. + let mut outer = exact(2, 2); + outer.rows[0].cells[0].blocks.push(std::sync::Arc::new(Block::Table(exact(2, 2)))); + d.insert_block(StoryRef::Body, &Path::top(1), Block::Table(outer)).unwrap(); + d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(exact(2, 3))).unwrap(); + let l = lay(&d, &LayoutOptions::default()); + (d, l) +} + +/// What drawing `it` on a page of just it (and every clipped cell, for a line) gave before the +/// page's authors and clipped cells were shared: a `page_display` of its own. +fn drawn_alone(d: &Document, page: &Page, it: &Placed, opts: &display::DisplayOptions) -> Vec { + let mut items = vec![it.clone()]; + if matches!(it, Placed::Lines { .. }) { + let cells = page.header.iter().chain(&page.footer).chain(&page.items).filter(|c| matches!(c, Placed::Cell { clip: true, .. })); + items.extend(cells.cloned()); + } + let one = Page { w: page.w, h: page.h, section: page.section, number: page.number, items, body: page.body, ..Default::default() }; + display::page_display(d, &one, opts) +} + +/// #3866: drawing the items of a page one by one with the page's shared authors and clipped +/// cells gives what a `page_display` of each alone gave. +#[test] +fn drawing_items_one_by_one_matches_a_page_display_of_each() { + let (d, l) = mixed_page(); + let page = &l.pages[0]; + let clipped = page.items.iter().filter(|c| matches!(c, Placed::Cell { clip: true, .. })).count(); + assert!(clipped >= 8, "clipped cells, nested ones among them: {clipped}"); + assert!(!page.header.is_empty(), "a header on the page"); + for markup in [true, false] { + let opts = display::DisplayOptions { markup, ..Default::default() }; + let shown = display::ItemDisplay::new(&d, page); + let (mut clips, mut coloured) = (0, 0); + for it in page.header.iter().chain(&page.footer).chain(&page.items) { + let (got, want) = (shown.draws(it, &opts), drawn_alone(&d, page, it, &opts)); + assert_eq!(format!("{got:?}"), format!("{want:?}"), "markup {markup}: {it:?}"); + clips += got.iter().filter(|x| matches!(x, display::Draw::Clip { .. })).count(); + coloured += got.iter().filter(|x| matches!(x, display::Draw::Glyphs { color, .. } if *color != Rgb(0, 0, 0))).count(); + } + assert!(clips > 0, "some lines are clipped"); + assert!(!markup || coloured > 0, "some tracked text is in an author's colour"); + } +} + +/// #3866: a header paragraph on the hinted page is found there, without a walk of every page of a +/// long document to find the first page that shows it. +#[test] +fn selecting_a_header_on_the_hinted_page_does_not_walk_every_page() { + let laid = |n: usize| { + let mut d = Document::from_text("n"); + let huge = CharProps { size: Some(400.0), ..Default::default() }; + d.body = (0..n).map(|_| wordcraft_doc::para_block(Paragraph::with_text("n", huge.clone()))).collect(); + let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(Paragraph::with_text("head", CharProps::default()))]); + d.last_section.headers.default = Some(id); + let l = lay(&d, &LayoutOptions::default()); + let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(0), 2)); + (d, l, ends) + }; + let steps = |n: usize| { + let (d, l, (a, b)) = laid(n); + assert!(l.pages.len() >= n, "a page to each paragraph: {}", l.pages.len()); + work::count(|| drop(std::hint::black_box(l.selection_rects(&d, &a, &b, 0)))) + }; + let (small, big) = (steps(20), steps(160)); + eprintln!("hinted header: {small} steps for 20 pages, {big} for 160"); + assert!(small > 0 && big <= small + 8, "the pages aren't walked: {small} → {big}"); +} From 056b18b714615d918a21774227b89a43ddd179cf Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 12:57:39 +0100 Subject: [PATCH 015/308] M7: match proofing pane geometry after rendered comparison Inspect the successful 20205a5 light/dark artifacts against the observed Word arrangement. Fill the sentence frame across the pane and bound the suggestion list to roughly three cards, retaining all results through scrolling and keeping ignore/dictionary actions below the list visible. The direct-correction/undo regression now clicks the first visible engine suggestion rather than relying on an offscreen fourth card. Update comparison/progress records. Normalize the baseline test file's line endings so its final diff contains only the two missing imports. Parent 20205a5 passed full CI, three-platform Printing/UI checks, targeted proofing tests and preview generation. This layout revision needs new hosted validation and inspected PNGs. Local cargo xtask ci was attempted but cargo is absent; git diff --check passes. --- ROADMAP.md | 4 +- crates/layout/src/tests_linear.rs | 988 ++++++++++++++-------------- crates/ui-egui/src/proofing_pane.rs | 101 +-- docs/gaps.md | 4 +- docs/spelling-review.md | 5 +- docs/ui-parity.md | 4 +- 6 files changed, 557 insertions(+), 549 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 95612af684..5d3be77a19 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered comparison adjusts context width and suggestion scrolling); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-18 · **Change:** trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -644,7 +644,7 @@ Footer page-table height and body wrapping are corrected. Other pagination gaps | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; readiness estimates unchanged | | 2026-10-22 | trivial | Progress log: one native window per open document | | 2026-10-22 | trivial | Progress log: every open document keeps its own recovery copy | | 2026-10-22 | trivial | Progress log: several open documents in the desktop window | diff --git a/crates/layout/src/tests_linear.rs b/crates/layout/src/tests_linear.rs index 92c87c98d8..f5b9f78098 100644 --- a/crates/layout/src/tests_linear.rs +++ b/crates/layout/src/tests_linear.rs @@ -1,494 +1,494 @@ -//! Lookups that once rescanned the page, the paragraph or the document for every item they -//! looked up (#3692, #3706): an input 8x the size must cost about 8x as much, not 64x. -//! -//! The cost is the steps the lookups' walks count ([`crate::work`]), not a timing, so a busy -//! machine can't make a linear lookup look quadratic or the other way round. Only proofing text, -//! whose walk is the document model's, is timed. - -use super::tests::{SCALE, cost_ratio}; -use super::{ - Block, CharProps, DocLayout, Document, HeaderSet, HeightRule, InlineObject, LayoutCache, LayoutOptions, Page, ParaLayout, Paragraph, Path, - Placed, Rgb, SectionProps, SectionStart, StoryRef, display, layout, para, work, -}; -use wordcraft_doc::para::Run; -use wordcraft_doc::{Pos, Table}; - -fn lay(d: &Document, opts: &LayoutOptions) -> DocLayout { - layout(d, &mut LayoutCache::new(), opts) -} - -/// The most steps 8x the input may take, against the steps of the input: linear walks take -/// about 8x (a little more where a fixed cost is shared out), quadratic ones about 64x. -const MAX_STEP_RATIO: f64 = 1.5 * SCALE as f64; - -/// Asserts `f` takes about [`SCALE`] times the steps ([`crate::work`]) on `make(SCALE * n)` as on -/// `make(n)`, not [`SCALE`]² times. Making the inputs isn't counted. -pub(crate) fn assert_linear_steps(what: &str, n: usize, make: impl Fn(usize) -> T, f: impl Fn(&T)) { - let (small, big) = (make(n), make(SCALE * n)); - let (s, b) = (work::count(|| f(&small)), work::count(|| f(&big))); - let ratio = b as f64 / s.max(1) as f64; - eprintln!("{what}: {s} steps for n, {b} for {SCALE}n, ratio {ratio:.1}"); - assert!(s > 0, "{what}: no steps counted, so the walk this test watches no longer counts them (crate::work)"); - assert!(ratio <= MAX_STEP_RATIO, "{what}: {SCALE}x the input took {ratio:.1}x the steps ({s} → {b}): not linear"); -} - -/// [`assert_linear_steps`] for laying out `doc(n)`. -fn assert_layout_linear(what: &str, n: usize, doc: impl Fn(usize) -> Document, opts: &LayoutOptions) { - assert_linear_steps(what, n, doc, |d| drop(std::hint::black_box(lay(d, opts)))); -} - -/// The first laid-out body paragraph on page 0. -fn first_para(l: &DocLayout) -> Option<&ParaLayout> { - l.pages.first()?.items.iter().find_map(|it| match it { - Placed::Lines { para, story: StoryRef::Body, .. } => Some(para.as_ref()), - _ => None, - }) -} - -/// Finding 29: proofing text found each character's run from the paragraph's first run. -#[test] -fn proof_text_walks_the_runs_once() { - // One run per character, every other one deleted. - let para = |n: usize| { - let mut p = Paragraph::with_text(&"a".repeat(n), CharProps::default()); - p.runs = (0..n).map(|i| Run { len: 1, props: CharProps { del: (i % 2 == 1).then_some(0), ..Default::default() } }).collect(); - p - }; - assert_eq!(para::proof_text(¶(5)), "a\u{1}a\u{1}a"); - // The runs are the document model's, whose walk counts no steps, so this one is timed: the - // CPU time this thread ran (`cost_ratio`). At 50,000 runs the old walk took 64x as long for - // 8x the runs, and the new one about 8x; a limit of 24x stays far from both, and a ratio over - // it is measured again, twice, before the test fails, so a busy spell can't fail it alone. - let (small, big) = (para(50_000), para(SCALE * 50_000)); - let proof_8 = |p: &Paragraph| (0..8).for_each(|_| drop(std::hint::black_box(para::proof_text(p)))); - let mut ratios = Vec::new(); - for _ in 0..3 { - let (t1, tk, ratio) = cost_ratio(SCALE, || proof_8(&small), || proof_8(&big)); - eprintln!("proof_text: t(n) {t1:.4} s, t(8n) {tk:.4} s, ratio {ratio:.1}"); - if ratio < 24.0 { - return; - } - ratios.push(ratio); - } - panic!("proof_text: 8x the runs took {ratios:.1?}x the time, three times over: not linear"); -} - -/// A right-to-left paragraph whose one line holds `n` hidden letters: caret offsets with no width. -fn bidi_hidden(n: usize) -> Document { - let head = "سلام "; - let mut d = Document::from_text(&format!("{head}{} world", "x".repeat(n))); - let at = Pos::body(0, 0); - d.format_paragraphs(&at, &at, &|p| p.bidi = Some(true)).unwrap(); - d.format_range(&Pos::body(0, head.len()), &Pos::body(0, head.len() + n), &|c| c.hidden = Some(true)).unwrap(); - d -} - -/// Finding 5: a click on a bidirectional line and a visual caret step measured every caret -/// offset from the line's first cluster. -#[test] -fn bidi_clicks_and_caret_steps_take_linear_time() { - let opts = LayoutOptions { show_hidden: false, ..Default::default() }; - let laid = |n: usize| lay(&bidi_hidden(n), &opts); - let Some(pb) = first_para(&laid(SCALE * 5000)).map(|p| p.lines.len()) else { panic!("no paragraph") }; - assert_eq!(pb, 1, "the hidden letters stay on one line"); - assert_linear_steps("bidi hit test", 5000, laid, |l| { - let Some(p) = first_para(l) else { panic!("no paragraph") }; - std::hint::black_box(p.off_at_x(0, 1.0)); - std::hint::black_box(l.visual_step(&Pos::body(0, 0), true, 0)); - }); -} - -/// The carets of a line walked in one pass are where `x_of` puts each of them, on bidirectional -/// and left-to-right lines alike. -#[test] -fn line_carets_match_x_of() { - let docs = [bidi_hidden(20), Document::from_text("Plain left-to-right text, long enough to wrap. ".repeat(6).as_str()), { - let mut d = Document::from_text("سلام world 123 دنیا (مرحبا) end"); - let at = Pos::body(0, 0); - d.format_paragraphs(&at, &at, &|p| p.bidi = Some(true)).unwrap(); - d - }]; - for d in &docs { - for show_hidden in [false, true] { - let l = lay(d, &LayoutOptions { show_hidden, ..Default::default() }); - let Some(p) = first_para(&l) else { panic!("no paragraph") }; - for li in 0..p.lines.len() { - let want: Vec<_> = p.line_offsets(li).into_iter().map(|off| (off, p.x_of(li, off))).collect(); - assert!(!want.is_empty()); - assert_eq!(p.line_carets(li), want, "line {li}"); - } - } - } -} - -/// Finding 3: autofitting a table counted its grid's columns again for every spanning cell. -#[test] -fn autofit_tables_with_a_spanning_cell_in_every_row_lay_out_in_linear_time() { - // Rows with no cells, only a column skipped before and after them with a width: two - // spanning entries a row for next to no layout, so counting the grid per entry dominates. - let doc = |n: usize| { - let mut t = Table::new(n, 1, 468.0); - for row in t.rows.iter_mut().skip(1) { - row.cells.clear(); - row.props.grid_before = 1; - row.props.w_before = Some(10.0); - row.props.grid_after = 1; - row.props.w_after = Some(10.0); - } - let mut d = Document::from_text("x"); - d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap(); - d - }; - assert_layout_linear("spanning cells", 2000, doc, &LayoutOptions::default()); -} - -/// `n` continuous sections of an empty paragraph each, each with a different first page. The -/// first section names every header and footer; the others name them too, or (`inherit`) name -/// none and link to the previous section's. -fn sections(n: usize, inherit: bool) -> Document { - use wordcraft_doc::PartKind::{Footer, Header}; - let mut d = Document::from_text(&"\n".repeat(n.saturating_sub(1))); - let mut part = |k| d.add_part(k, vec![wordcraft_doc::para_block(Paragraph::with_text("Head", CharProps::default()))]); - let ids = [part(Header), part(Header), part(Header), part(Footer), part(Footer), part(Footer)]; - let named = SectionProps { - start: SectionStart::Continuous, - title_page: true, - headers: HeaderSet { default: Some(ids[0]), first: Some(ids[1]), even: Some(ids[2]) }, - footers: HeaderSet { default: Some(ids[3]), first: Some(ids[4]), even: Some(ids[5]) }, - ..Default::default() - }; - let linked = SectionProps { headers: HeaderSet::default(), footers: HeaderSet::default(), ..named.clone() }; - for i in 0..n.saturating_sub(1) { - let s = if i == 0 || !inherit { named.clone() } else { linked.clone() }; - d.para_mut(StoryRef::Body, &Path::top(i)).unwrap().section = Some(Box::new(s)); - } - d.last_section = if inherit { linked } else { named }; - d -} - -/// Finding 13: each section's headers and footers were found by walking back through the -/// sections before it, for every section and again for every page. Linked to the previous -/// section's, they must cost no more to lay out than named in each section. -#[test] -fn headers_inherited_through_many_sections_lay_out_in_linear_time() { - let l = lay(§ions(200, true), &LayoutOptions::default()); - assert!(l.pages.iter().all(|p| !p.header.is_empty() && !p.footer.is_empty()), "the header and footer are inherited"); - let opts = LayoutOptions::default(); - assert_layout_linear("inherited headers", 1000, |n| sections(n, true), &opts); - let steps = |inherit: bool| work::count(|| drop(std::hint::black_box(lay(§ions(1000, inherit), &opts)))); - let (named, inherited) = (steps(false), steps(true)); - assert!(inherited <= 2 * named, "1000 inheriting sections took {inherited} steps, named ones {named}"); -} - -/// A header of `n` short paragraphs. -fn tall_header(n: usize) -> (Document, u32) { - let mut d = Document::from_text("Body"); - let paras = (0..n).map(|i| wordcraft_doc::para_block(Paragraph::with_text(&format!("h{i}"), CharProps::default()))).collect(); - let id = d.add_part(wordcraft_doc::PartKind::Header, paras); - d.last_section.headers.default = Some(id); - (d, id) -} - -/// Finding 28: selecting across a header looked through the whole page for each paragraph. -#[test] -fn selecting_a_long_header_takes_linear_time() { - // The header laid out, and the selection from its first paragraph to its last. - let laid = |n: usize| { - let (d, id) = tall_header(n); - let l = lay(&d, &LayoutOptions::default()); - let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(n - 1), 2)); - (d, l, ends) - }; - let (d, l, (a, b)) = laid(SCALE * 300); - assert!(l.selection_rects(&d, &a, &b, 0).len() >= SCALE * 300, "a rectangle per paragraph"); - assert_linear_steps("header selection", 300, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0)))); -} - -/// An endnote of `n` paragraphs, a few lines each, continued over many pages. -fn long_endnote(n: usize) -> (Document, u32) { - let mut d = Document::from_text("Body"); - // One huge letter to a paragraph, one line high enough for a page, so the pages are as many as - // the paragraphs and each paragraph costs next to nothing. - let huge = CharProps { size: Some(400.0), ..Default::default() }; - let paras = (0..n).map(|_| wordcraft_doc::para_block(Paragraph::with_text("n", huge.clone()))).collect(); - let id = d.add_part(wordcraft_doc::PartKind::Endnote, paras); - let r = InlineObject::NoteRef { kind: wordcraft_doc::para::NoteKind::Endnote, id, custom: String::new() }; - d.insert_object(&Pos::body(0, 4), r, &Default::default()).unwrap(); - (d, id) -} - -/// #3866: selecting across an endnote over many pages probed the pages from the first for each -/// paragraph (paragraphs x pages). -#[test] -fn selecting_a_long_endnote_takes_linear_time() { - let laid = |n: usize| { - let (d, id) = long_endnote(n); - let l = lay(&d, &LayoutOptions::default()); - let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(n - 1), 2)); - (d, l, ends) - }; - let (d, l, (a, b)) = laid(SCALE * 20); - assert!(l.pages.len() >= SCALE * 20 / 2, "the endnote runs over many pages: {}", l.pages.len()); - assert!(l.selection_rects(&d, &a, &b, 0).len() >= SCALE * 20, "a rectangle per paragraph"); - assert_linear_steps("endnote selection", 20, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0)))); -} - -/// A table of `n` rows of 60 cells, each row exactly half a point high, so every cell clips its -/// (empty) paragraph and the whole table fits on the first page. -fn clipped_table(n: usize) -> Document { - let mut t = Table::new(n, 60, 468.0); - for row in &mut t.rows { - row.props.height = Some(0.5); - row.props.height_rule = HeightRule::Exact; - } - let mut d = Document::from_text("x"); - d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap(); - d -} - -/// Finding 1: drawing a line in a clipped cell looked through every clipped cell on the page. -#[test] -fn drawing_many_clipped_cells_takes_linear_time() { - let laid = |n: usize| { - let d = clipped_table(n); - let l = lay(&d, &LayoutOptions::default()); - (d, l) - }; - let opts = display::DisplayOptions::default(); - let draw = |(d, l): &(Document, DocLayout)| l.pages.first().map(|p| display::page_display(d, p, &opts)).unwrap_or_default(); - let clips = draw(&laid(SCALE * 16)).iter().filter(|i| matches!(i, display::Draw::Clip { .. })).count(); - assert_eq!(clips, SCALE * 16 * 60, "every cell's line is clipped, all on the first page"); - assert_linear_steps("clipped cells", 16, laid, |dl| drop(std::hint::black_box(draw(dl)))); -} - -/// `n` words, each inserted by its own author, at a size that fits them all on the first page. -fn tracked_words(n: usize) -> Document { - let mut d = Document::from_text("x"); - let p = Paragraph { - text: "word ".repeat(n), - runs: (0..n).map(|i| Run { len: 5, props: CharProps { ins: u32::try_from(i).ok(), size: Some(1.0), ..Default::default() } }).collect(), - ..Paragraph::with_text("", CharProps::default()) - }; - d.body = vec![wordcraft_doc::para_block(p)]; - d.revisions = (0..n).map(|i| wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, format!("Author {i}"), "")).collect(); - d -} - -/// Finding 30 (#3706): every tracked run ranked its author among all the document's revisions. -#[test] -fn drawing_tracked_changes_by_many_authors_takes_linear_time() { - let laid = |n: usize| { - let d = tracked_words(n); - let l = lay(&d, &LayoutOptions::default()); - (d, l) - }; - assert_eq!(laid(SCALE * 1000).1.pages.len(), 1); - let opts = display::DisplayOptions { markup: true, ..Default::default() }; - let draw = |(d, l): &(Document, DocLayout)| l.pages.first().map(|p| display::page_display(d, p, &opts)).unwrap_or_default(); - // Every run is drawn in its author's colour, so the authors are ranked at least once. - assert_linear_steps("tracked runs", 1000, laid, |dl| drop(std::hint::black_box(draw(dl)))); -} - -/// Each author keeps the colour of their first revision's place among the authors. -#[test] -fn tracked_changes_colour_by_author_in_order_of_appearance() { - let mut d = tracked_words(3); - // Words 0 and 2 by Ana, word 1 by Ben: Ana first, Ben second. - for (r, a) in d.revisions.iter_mut().zip(["Ana", "Ben", "Ana"]) { - r.author = a.into(); - } - let l = lay(&d, &LayoutOptions::default()); - let opts = display::DisplayOptions { markup: true, ..Default::default() }; - let colours: Vec<_> = display::page_display(&d, &l.pages[0], &opts) - .into_iter() - .filter_map(|i| if let display::Draw::Glyphs { color, .. } = i { Some(color) } else { None }) - .collect(); - let (ana, ben) = (display::revision_color(0), display::revision_color(1)); - assert_ne!(ana, ben); - assert_eq!(colours, [ana, ben, ana]); -} - -/// Finding 30 (#3706): every comment balloon ranked its author among all the comments. -#[test] -fn balloons_for_many_authors_lay_out_in_linear_time() { - let doc = |n: usize| { - let mut d = Document::from_text(&"word ".repeat(n)); - for i in (0..n).rev() { - let part = d.add_part(wordcraft_doc::PartKind::Comment, vec![wordcraft_doc::para_block(Paragraph::with_text("c", CharProps::default()))]); - let id = u32::try_from(i).unwrap(); - d.comments.insert(id, wordcraft_doc::Comment { author: format!("Author {i}"), part, ..Default::default() }); - let p = CharProps::default(); - d.insert_object(&Pos::body(0, 5 * i + 4), InlineObject::CommentEnd { id }, &p).unwrap(); - d.insert_object(&Pos::body(0, 5 * i), InlineObject::CommentStart { id }, &p).unwrap(); - } - d - }; - let opts = LayoutOptions { comment_balloons: true, ..Default::default() }; - assert_layout_linear("comment balloons", 120, doc, &opts); -} - -/// `n` one-word paragraphs, each inserted by its own author, small enough for one page. -fn tracked_paras(n: usize) -> Document { - let mut d = Document::from_text("x"); - d.body = (0..n) - .map(|i| { - let run = Run { len: 1, props: CharProps { ins: u32::try_from(i).ok(), size: Some(1.0), ..Default::default() } }; - let mut p = Paragraph { runs: vec![run], ..Paragraph::with_text("w", CharProps::default()) }; - p.mark.size = Some(1.0); - p.props.space_before = Some(0.0); - p.props.space_after = Some(0.0); - wordcraft_doc::para_block(p) - }) - .collect(); - d.revisions = (0..n).map(|i| wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, format!("Author {i}"), "")).collect(); - d -} - -/// Draws every item of the first page on its own, as the PDF export does. -fn draw_items((d, l): &(Document, DocLayout), opts: &display::DisplayOptions) { - let Some(p) = l.pages.first() else { return }; - let shown = display::ItemDisplay::new(d, p); - for it in p.header.iter().chain(&p.footer).chain(&p.items) { - std::hint::black_box(shown.draws(it, opts)); - } -} - -/// #3866: the PDF export drew each item with a `page_display` of its own, which ranked the revision -/// authors again for every item with a tracked change (lines x revisions). -#[test] -fn drawing_items_one_by_one_ranks_the_authors_once() { - let laid = |n: usize| { - let d = tracked_paras(n); - let l = lay(&d, &LayoutOptions::default()); - (d, l) - }; - assert_eq!(laid(SCALE * 40).1.pages.len(), 1); - let opts = display::DisplayOptions { markup: true, ..Default::default() }; - assert_linear_steps("items drawn one by one", 40, laid, |dl| draw_items(dl, &opts)); -} - -/// #3866: the same for the clipped cells, which each item's `page_display` collected again -/// (lines x clipped cells). -#[test] -fn drawing_items_one_by_one_collects_the_clipped_cells_once() { - let laid = |n: usize| { - let d = clipped_table(n); - let l = lay(&d, &LayoutOptions::default()); - (d, l) - }; - let opts = display::DisplayOptions::default(); - assert_linear_steps("clipped cells, items one by one", 16, laid, |dl| draw_items(dl, &opts)); -} - -/// #3866: a comment's balloon is in its author's colour, authors numbered in order of appearance. -#[test] -fn balloons_are_in_their_authors_colours() { - let mut d = Document::from_text("one two three"); - for (i, a) in ["Ana", "Ben", "Ana"].into_iter().enumerate() { - let part = d.add_part(wordcraft_doc::PartKind::Comment, vec![wordcraft_doc::para_block(Paragraph::with_text("c", CharProps::default()))]); - let id = u32::try_from(i).unwrap(); - d.comments.insert(id, wordcraft_doc::Comment { author: a.into(), part, ..Default::default() }); - let p = CharProps::default(); - d.insert_object(&Pos::body(0, 0), InlineObject::CommentEnd { id }, &p).unwrap(); - d.insert_object(&Pos::body(0, 0), InlineObject::CommentStart { id }, &p).unwrap(); - } - let l = lay(&d, &LayoutOptions { comment_balloons: true, ..Default::default() }); - // Each card's stripe is a 3 pt wide fill in the author's colour; the cards run down the page. - let mut stripes: Vec<_> = l.pages[0] - .items - .iter() - .filter_map(|it| match it { - Placed::Fill { rect, color } if rect.w == 3.0 => Some((rect.y, *color)), - _ => None, - }) - .collect(); - stripes.sort_by(|a, b| a.0.total_cmp(&b.0)); - let (ana, ben) = (display::revision_color(0), display::revision_color(1)); - assert_ne!(ana, ben); - assert_eq!(stripes.iter().map(|s| s.1).collect::>(), [ana, ben, ana]); -} - -/// A page with tracked changes by several authors in its body and header, and exact-height -/// tables, one nested in a cell of another, whose cells clip their lines. -fn mixed_page() -> (Document, DocLayout) { - let mut d = tracked_paras(6); - let mut hp = Paragraph::with_text("head", CharProps::default()); - hp.runs = vec![Run { len: 4, props: CharProps { ins: Some(2), ..Default::default() } }]; - let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(hp)]); - d.last_section.headers.default = Some(id); - let exact = |rows, cols| { - let mut t = Table::new(rows, cols, 200.0); - for row in &mut t.rows { - row.props.height = Some(0.5); - row.props.height_rule = HeightRule::Exact; - } - t - }; - // A cell with text in it holds a nested table. - let mut outer = exact(2, 2); - outer.rows[0].cells[0].blocks.push(std::sync::Arc::new(Block::Table(exact(2, 2)))); - d.insert_block(StoryRef::Body, &Path::top(1), Block::Table(outer)).unwrap(); - d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(exact(2, 3))).unwrap(); - let l = lay(&d, &LayoutOptions::default()); - (d, l) -} - -/// What drawing `it` on a page of just it (and every clipped cell, for a line) gave before the -/// page's authors and clipped cells were shared: a `page_display` of its own. -fn drawn_alone(d: &Document, page: &Page, it: &Placed, opts: &display::DisplayOptions) -> Vec { - let mut items = vec![it.clone()]; - if matches!(it, Placed::Lines { .. }) { - let cells = page.header.iter().chain(&page.footer).chain(&page.items).filter(|c| matches!(c, Placed::Cell { clip: true, .. })); - items.extend(cells.cloned()); - } - let one = Page { w: page.w, h: page.h, section: page.section, number: page.number, items, body: page.body, ..Default::default() }; - display::page_display(d, &one, opts) -} - -/// #3866: drawing the items of a page one by one with the page's shared authors and clipped -/// cells gives what a `page_display` of each alone gave. -#[test] -fn drawing_items_one_by_one_matches_a_page_display_of_each() { - let (d, l) = mixed_page(); - let page = &l.pages[0]; - let clipped = page.items.iter().filter(|c| matches!(c, Placed::Cell { clip: true, .. })).count(); - assert!(clipped >= 8, "clipped cells, nested ones among them: {clipped}"); - assert!(!page.header.is_empty(), "a header on the page"); - for markup in [true, false] { - let opts = display::DisplayOptions { markup, ..Default::default() }; - let shown = display::ItemDisplay::new(&d, page); - let (mut clips, mut coloured) = (0, 0); - for it in page.header.iter().chain(&page.footer).chain(&page.items) { - let (got, want) = (shown.draws(it, &opts), drawn_alone(&d, page, it, &opts)); - assert_eq!(format!("{got:?}"), format!("{want:?}"), "markup {markup}: {it:?}"); - clips += got.iter().filter(|x| matches!(x, display::Draw::Clip { .. })).count(); - coloured += got.iter().filter(|x| matches!(x, display::Draw::Glyphs { color, .. } if *color != Rgb(0, 0, 0))).count(); - } - assert!(clips > 0, "some lines are clipped"); - assert!(!markup || coloured > 0, "some tracked text is in an author's colour"); - } -} - -/// #3866: a header paragraph on the hinted page is found there, without a walk of every page of a -/// long document to find the first page that shows it. -#[test] -fn selecting_a_header_on_the_hinted_page_does_not_walk_every_page() { - let laid = |n: usize| { - let mut d = Document::from_text("n"); - let huge = CharProps { size: Some(400.0), ..Default::default() }; - d.body = (0..n).map(|_| wordcraft_doc::para_block(Paragraph::with_text("n", huge.clone()))).collect(); - let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(Paragraph::with_text("head", CharProps::default()))]); - d.last_section.headers.default = Some(id); - let l = lay(&d, &LayoutOptions::default()); - let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(0), 2)); - (d, l, ends) - }; - let steps = |n: usize| { - let (d, l, (a, b)) = laid(n); - assert!(l.pages.len() >= n, "a page to each paragraph: {}", l.pages.len()); - work::count(|| drop(std::hint::black_box(l.selection_rects(&d, &a, &b, 0)))) - }; - let (small, big) = (steps(20), steps(160)); - eprintln!("hinted header: {small} steps for 20 pages, {big} for 160"); - assert!(small > 0 && big <= small + 8, "the pages aren't walked: {small} → {big}"); -} +//! Lookups that once rescanned the page, the paragraph or the document for every item they +//! looked up (#3692, #3706): an input 8x the size must cost about 8x as much, not 64x. +//! +//! The cost is the steps the lookups' walks count ([`crate::work`]), not a timing, so a busy +//! machine can't make a linear lookup look quadratic or the other way round. Only proofing text, +//! whose walk is the document model's, is timed. + +use super::tests::{SCALE, cost_ratio}; +use super::{ + Block, CharProps, DocLayout, Document, HeaderSet, HeightRule, InlineObject, LayoutCache, LayoutOptions, Page, ParaLayout, Paragraph, Path, + Placed, Rgb, SectionProps, SectionStart, StoryRef, display, layout, para, work, +}; +use wordcraft_doc::para::Run; +use wordcraft_doc::{Pos, Table}; + +fn lay(d: &Document, opts: &LayoutOptions) -> DocLayout { + layout(d, &mut LayoutCache::new(), opts) +} + +/// The most steps 8x the input may take, against the steps of the input: linear walks take +/// about 8x (a little more where a fixed cost is shared out), quadratic ones about 64x. +const MAX_STEP_RATIO: f64 = 1.5 * SCALE as f64; + +/// Asserts `f` takes about [`SCALE`] times the steps ([`crate::work`]) on `make(SCALE * n)` as on +/// `make(n)`, not [`SCALE`]² times. Making the inputs isn't counted. +pub(crate) fn assert_linear_steps(what: &str, n: usize, make: impl Fn(usize) -> T, f: impl Fn(&T)) { + let (small, big) = (make(n), make(SCALE * n)); + let (s, b) = (work::count(|| f(&small)), work::count(|| f(&big))); + let ratio = b as f64 / s.max(1) as f64; + eprintln!("{what}: {s} steps for n, {b} for {SCALE}n, ratio {ratio:.1}"); + assert!(s > 0, "{what}: no steps counted, so the walk this test watches no longer counts them (crate::work)"); + assert!(ratio <= MAX_STEP_RATIO, "{what}: {SCALE}x the input took {ratio:.1}x the steps ({s} → {b}): not linear"); +} + +/// [`assert_linear_steps`] for laying out `doc(n)`. +fn assert_layout_linear(what: &str, n: usize, doc: impl Fn(usize) -> Document, opts: &LayoutOptions) { + assert_linear_steps(what, n, doc, |d| drop(std::hint::black_box(lay(d, opts)))); +} + +/// The first laid-out body paragraph on page 0. +fn first_para(l: &DocLayout) -> Option<&ParaLayout> { + l.pages.first()?.items.iter().find_map(|it| match it { + Placed::Lines { para, story: StoryRef::Body, .. } => Some(para.as_ref()), + _ => None, + }) +} + +/// Finding 29: proofing text found each character's run from the paragraph's first run. +#[test] +fn proof_text_walks_the_runs_once() { + // One run per character, every other one deleted. + let para = |n: usize| { + let mut p = Paragraph::with_text(&"a".repeat(n), CharProps::default()); + p.runs = (0..n).map(|i| Run { len: 1, props: CharProps { del: (i % 2 == 1).then_some(0), ..Default::default() } }).collect(); + p + }; + assert_eq!(para::proof_text(¶(5)), "a\u{1}a\u{1}a"); + // The runs are the document model's, whose walk counts no steps, so this one is timed: the + // CPU time this thread ran (`cost_ratio`). At 50,000 runs the old walk took 64x as long for + // 8x the runs, and the new one about 8x; a limit of 24x stays far from both, and a ratio over + // it is measured again, twice, before the test fails, so a busy spell can't fail it alone. + let (small, big) = (para(50_000), para(SCALE * 50_000)); + let proof_8 = |p: &Paragraph| (0..8).for_each(|_| drop(std::hint::black_box(para::proof_text(p)))); + let mut ratios = Vec::new(); + for _ in 0..3 { + let (t1, tk, ratio) = cost_ratio(SCALE, || proof_8(&small), || proof_8(&big)); + eprintln!("proof_text: t(n) {t1:.4} s, t(8n) {tk:.4} s, ratio {ratio:.1}"); + if ratio < 24.0 { + return; + } + ratios.push(ratio); + } + panic!("proof_text: 8x the runs took {ratios:.1?}x the time, three times over: not linear"); +} + +/// A right-to-left paragraph whose one line holds `n` hidden letters: caret offsets with no width. +fn bidi_hidden(n: usize) -> Document { + let head = "سلام "; + let mut d = Document::from_text(&format!("{head}{} world", "x".repeat(n))); + let at = Pos::body(0, 0); + d.format_paragraphs(&at, &at, &|p| p.bidi = Some(true)).unwrap(); + d.format_range(&Pos::body(0, head.len()), &Pos::body(0, head.len() + n), &|c| c.hidden = Some(true)).unwrap(); + d +} + +/// Finding 5: a click on a bidirectional line and a visual caret step measured every caret +/// offset from the line's first cluster. +#[test] +fn bidi_clicks_and_caret_steps_take_linear_time() { + let opts = LayoutOptions { show_hidden: false, ..Default::default() }; + let laid = |n: usize| lay(&bidi_hidden(n), &opts); + let Some(pb) = first_para(&laid(SCALE * 5000)).map(|p| p.lines.len()) else { panic!("no paragraph") }; + assert_eq!(pb, 1, "the hidden letters stay on one line"); + assert_linear_steps("bidi hit test", 5000, laid, |l| { + let Some(p) = first_para(l) else { panic!("no paragraph") }; + std::hint::black_box(p.off_at_x(0, 1.0)); + std::hint::black_box(l.visual_step(&Pos::body(0, 0), true, 0)); + }); +} + +/// The carets of a line walked in one pass are where `x_of` puts each of them, on bidirectional +/// and left-to-right lines alike. +#[test] +fn line_carets_match_x_of() { + let docs = [bidi_hidden(20), Document::from_text("Plain left-to-right text, long enough to wrap. ".repeat(6).as_str()), { + let mut d = Document::from_text("سلام world 123 دنیا (مرحبا) end"); + let at = Pos::body(0, 0); + d.format_paragraphs(&at, &at, &|p| p.bidi = Some(true)).unwrap(); + d + }]; + for d in &docs { + for show_hidden in [false, true] { + let l = lay(d, &LayoutOptions { show_hidden, ..Default::default() }); + let Some(p) = first_para(&l) else { panic!("no paragraph") }; + for li in 0..p.lines.len() { + let want: Vec<_> = p.line_offsets(li).into_iter().map(|off| (off, p.x_of(li, off))).collect(); + assert!(!want.is_empty()); + assert_eq!(p.line_carets(li), want, "line {li}"); + } + } + } +} + +/// Finding 3: autofitting a table counted its grid's columns again for every spanning cell. +#[test] +fn autofit_tables_with_a_spanning_cell_in_every_row_lay_out_in_linear_time() { + // Rows with no cells, only a column skipped before and after them with a width: two + // spanning entries a row for next to no layout, so counting the grid per entry dominates. + let doc = |n: usize| { + let mut t = Table::new(n, 1, 468.0); + for row in t.rows.iter_mut().skip(1) { + row.cells.clear(); + row.props.grid_before = 1; + row.props.w_before = Some(10.0); + row.props.grid_after = 1; + row.props.w_after = Some(10.0); + } + let mut d = Document::from_text("x"); + d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap(); + d + }; + assert_layout_linear("spanning cells", 2000, doc, &LayoutOptions::default()); +} + +/// `n` continuous sections of an empty paragraph each, each with a different first page. The +/// first section names every header and footer; the others name them too, or (`inherit`) name +/// none and link to the previous section's. +fn sections(n: usize, inherit: bool) -> Document { + use wordcraft_doc::PartKind::{Footer, Header}; + let mut d = Document::from_text(&"\n".repeat(n.saturating_sub(1))); + let mut part = |k| d.add_part(k, vec![wordcraft_doc::para_block(Paragraph::with_text("Head", CharProps::default()))]); + let ids = [part(Header), part(Header), part(Header), part(Footer), part(Footer), part(Footer)]; + let named = SectionProps { + start: SectionStart::Continuous, + title_page: true, + headers: HeaderSet { default: Some(ids[0]), first: Some(ids[1]), even: Some(ids[2]) }, + footers: HeaderSet { default: Some(ids[3]), first: Some(ids[4]), even: Some(ids[5]) }, + ..Default::default() + }; + let linked = SectionProps { headers: HeaderSet::default(), footers: HeaderSet::default(), ..named.clone() }; + for i in 0..n.saturating_sub(1) { + let s = if i == 0 || !inherit { named.clone() } else { linked.clone() }; + d.para_mut(StoryRef::Body, &Path::top(i)).unwrap().section = Some(Box::new(s)); + } + d.last_section = if inherit { linked } else { named }; + d +} + +/// Finding 13: each section's headers and footers were found by walking back through the +/// sections before it, for every section and again for every page. Linked to the previous +/// section's, they must cost no more to lay out than named in each section. +#[test] +fn headers_inherited_through_many_sections_lay_out_in_linear_time() { + let l = lay(§ions(200, true), &LayoutOptions::default()); + assert!(l.pages.iter().all(|p| !p.header.is_empty() && !p.footer.is_empty()), "the header and footer are inherited"); + let opts = LayoutOptions::default(); + assert_layout_linear("inherited headers", 1000, |n| sections(n, true), &opts); + let steps = |inherit: bool| work::count(|| drop(std::hint::black_box(lay(§ions(1000, inherit), &opts)))); + let (named, inherited) = (steps(false), steps(true)); + assert!(inherited <= 2 * named, "1000 inheriting sections took {inherited} steps, named ones {named}"); +} + +/// A header of `n` short paragraphs. +fn tall_header(n: usize) -> (Document, u32) { + let mut d = Document::from_text("Body"); + let paras = (0..n).map(|i| wordcraft_doc::para_block(Paragraph::with_text(&format!("h{i}"), CharProps::default()))).collect(); + let id = d.add_part(wordcraft_doc::PartKind::Header, paras); + d.last_section.headers.default = Some(id); + (d, id) +} + +/// Finding 28: selecting across a header looked through the whole page for each paragraph. +#[test] +fn selecting_a_long_header_takes_linear_time() { + // The header laid out, and the selection from its first paragraph to its last. + let laid = |n: usize| { + let (d, id) = tall_header(n); + let l = lay(&d, &LayoutOptions::default()); + let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(n - 1), 2)); + (d, l, ends) + }; + let (d, l, (a, b)) = laid(SCALE * 300); + assert!(l.selection_rects(&d, &a, &b, 0).len() >= SCALE * 300, "a rectangle per paragraph"); + assert_linear_steps("header selection", 300, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0)))); +} + +/// An endnote of `n` paragraphs, a few lines each, continued over many pages. +fn long_endnote(n: usize) -> (Document, u32) { + let mut d = Document::from_text("Body"); + // One huge letter to a paragraph, one line high enough for a page, so the pages are as many as + // the paragraphs and each paragraph costs next to nothing. + let huge = CharProps { size: Some(400.0), ..Default::default() }; + let paras = (0..n).map(|_| wordcraft_doc::para_block(Paragraph::with_text("n", huge.clone()))).collect(); + let id = d.add_part(wordcraft_doc::PartKind::Endnote, paras); + let r = InlineObject::NoteRef { kind: wordcraft_doc::para::NoteKind::Endnote, id, custom: String::new() }; + d.insert_object(&Pos::body(0, 4), r, &Default::default()).unwrap(); + (d, id) +} + +/// #3866: selecting across an endnote over many pages probed the pages from the first for each +/// paragraph (paragraphs x pages). +#[test] +fn selecting_a_long_endnote_takes_linear_time() { + let laid = |n: usize| { + let (d, id) = long_endnote(n); + let l = lay(&d, &LayoutOptions::default()); + let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(n - 1), 2)); + (d, l, ends) + }; + let (d, l, (a, b)) = laid(SCALE * 20); + assert!(l.pages.len() >= SCALE * 20 / 2, "the endnote runs over many pages: {}", l.pages.len()); + assert!(l.selection_rects(&d, &a, &b, 0).len() >= SCALE * 20, "a rectangle per paragraph"); + assert_linear_steps("endnote selection", 20, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0)))); +} + +/// A table of `n` rows of 60 cells, each row exactly half a point high, so every cell clips its +/// (empty) paragraph and the whole table fits on the first page. +fn clipped_table(n: usize) -> Document { + let mut t = Table::new(n, 60, 468.0); + for row in &mut t.rows { + row.props.height = Some(0.5); + row.props.height_rule = HeightRule::Exact; + } + let mut d = Document::from_text("x"); + d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(t)).unwrap(); + d +} + +/// Finding 1: drawing a line in a clipped cell looked through every clipped cell on the page. +#[test] +fn drawing_many_clipped_cells_takes_linear_time() { + let laid = |n: usize| { + let d = clipped_table(n); + let l = lay(&d, &LayoutOptions::default()); + (d, l) + }; + let opts = display::DisplayOptions::default(); + let draw = |(d, l): &(Document, DocLayout)| l.pages.first().map(|p| display::page_display(d, p, &opts)).unwrap_or_default(); + let clips = draw(&laid(SCALE * 16)).iter().filter(|i| matches!(i, display::Draw::Clip { .. })).count(); + assert_eq!(clips, SCALE * 16 * 60, "every cell's line is clipped, all on the first page"); + assert_linear_steps("clipped cells", 16, laid, |dl| drop(std::hint::black_box(draw(dl)))); +} + +/// `n` words, each inserted by its own author, at a size that fits them all on the first page. +fn tracked_words(n: usize) -> Document { + let mut d = Document::from_text("x"); + let p = Paragraph { + text: "word ".repeat(n), + runs: (0..n).map(|i| Run { len: 5, props: CharProps { ins: u32::try_from(i).ok(), size: Some(1.0), ..Default::default() } }).collect(), + ..Paragraph::with_text("", CharProps::default()) + }; + d.body = vec![wordcraft_doc::para_block(p)]; + d.revisions = (0..n).map(|i| wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, format!("Author {i}"), "")).collect(); + d +} + +/// Finding 30 (#3706): every tracked run ranked its author among all the document's revisions. +#[test] +fn drawing_tracked_changes_by_many_authors_takes_linear_time() { + let laid = |n: usize| { + let d = tracked_words(n); + let l = lay(&d, &LayoutOptions::default()); + (d, l) + }; + assert_eq!(laid(SCALE * 1000).1.pages.len(), 1); + let opts = display::DisplayOptions { markup: true, ..Default::default() }; + let draw = |(d, l): &(Document, DocLayout)| l.pages.first().map(|p| display::page_display(d, p, &opts)).unwrap_or_default(); + // Every run is drawn in its author's colour, so the authors are ranked at least once. + assert_linear_steps("tracked runs", 1000, laid, |dl| drop(std::hint::black_box(draw(dl)))); +} + +/// Each author keeps the colour of their first revision's place among the authors. +#[test] +fn tracked_changes_colour_by_author_in_order_of_appearance() { + let mut d = tracked_words(3); + // Words 0 and 2 by Ana, word 1 by Ben: Ana first, Ben second. + for (r, a) in d.revisions.iter_mut().zip(["Ana", "Ben", "Ana"]) { + r.author = a.into(); + } + let l = lay(&d, &LayoutOptions::default()); + let opts = display::DisplayOptions { markup: true, ..Default::default() }; + let colours: Vec<_> = display::page_display(&d, &l.pages[0], &opts) + .into_iter() + .filter_map(|i| if let display::Draw::Glyphs { color, .. } = i { Some(color) } else { None }) + .collect(); + let (ana, ben) = (display::revision_color(0), display::revision_color(1)); + assert_ne!(ana, ben); + assert_eq!(colours, [ana, ben, ana]); +} + +/// Finding 30 (#3706): every comment balloon ranked its author among all the comments. +#[test] +fn balloons_for_many_authors_lay_out_in_linear_time() { + let doc = |n: usize| { + let mut d = Document::from_text(&"word ".repeat(n)); + for i in (0..n).rev() { + let part = d.add_part(wordcraft_doc::PartKind::Comment, vec![wordcraft_doc::para_block(Paragraph::with_text("c", CharProps::default()))]); + let id = u32::try_from(i).unwrap(); + d.comments.insert(id, wordcraft_doc::Comment { author: format!("Author {i}"), part, ..Default::default() }); + let p = CharProps::default(); + d.insert_object(&Pos::body(0, 5 * i + 4), InlineObject::CommentEnd { id }, &p).unwrap(); + d.insert_object(&Pos::body(0, 5 * i), InlineObject::CommentStart { id }, &p).unwrap(); + } + d + }; + let opts = LayoutOptions { comment_balloons: true, ..Default::default() }; + assert_layout_linear("comment balloons", 120, doc, &opts); +} + +/// `n` one-word paragraphs, each inserted by its own author, small enough for one page. +fn tracked_paras(n: usize) -> Document { + let mut d = Document::from_text("x"); + d.body = (0..n) + .map(|i| { + let run = Run { len: 1, props: CharProps { ins: u32::try_from(i).ok(), size: Some(1.0), ..Default::default() } }; + let mut p = Paragraph { runs: vec![run], ..Paragraph::with_text("w", CharProps::default()) }; + p.mark.size = Some(1.0); + p.props.space_before = Some(0.0); + p.props.space_after = Some(0.0); + wordcraft_doc::para_block(p) + }) + .collect(); + d.revisions = (0..n).map(|i| wordcraft_doc::Revision::new(wordcraft_doc::RevisionKind::Insert, format!("Author {i}"), "")).collect(); + d +} + +/// Draws every item of the first page on its own, as the PDF export does. +fn draw_items((d, l): &(Document, DocLayout), opts: &display::DisplayOptions) { + let Some(p) = l.pages.first() else { return }; + let shown = display::ItemDisplay::new(d, p); + for it in p.header.iter().chain(&p.footer).chain(&p.items) { + std::hint::black_box(shown.draws(it, opts)); + } +} + +/// #3866: the PDF export drew each item with a `page_display` of its own, which ranked the revision +/// authors again for every item with a tracked change (lines x revisions). +#[test] +fn drawing_items_one_by_one_ranks_the_authors_once() { + let laid = |n: usize| { + let d = tracked_paras(n); + let l = lay(&d, &LayoutOptions::default()); + (d, l) + }; + assert_eq!(laid(SCALE * 40).1.pages.len(), 1); + let opts = display::DisplayOptions { markup: true, ..Default::default() }; + assert_linear_steps("items drawn one by one", 40, laid, |dl| draw_items(dl, &opts)); +} + +/// #3866: the same for the clipped cells, which each item's `page_display` collected again +/// (lines x clipped cells). +#[test] +fn drawing_items_one_by_one_collects_the_clipped_cells_once() { + let laid = |n: usize| { + let d = clipped_table(n); + let l = lay(&d, &LayoutOptions::default()); + (d, l) + }; + let opts = display::DisplayOptions::default(); + assert_linear_steps("clipped cells, items one by one", 16, laid, |dl| draw_items(dl, &opts)); +} + +/// #3866: a comment's balloon is in its author's colour, authors numbered in order of appearance. +#[test] +fn balloons_are_in_their_authors_colours() { + let mut d = Document::from_text("one two three"); + for (i, a) in ["Ana", "Ben", "Ana"].into_iter().enumerate() { + let part = d.add_part(wordcraft_doc::PartKind::Comment, vec![wordcraft_doc::para_block(Paragraph::with_text("c", CharProps::default()))]); + let id = u32::try_from(i).unwrap(); + d.comments.insert(id, wordcraft_doc::Comment { author: a.into(), part, ..Default::default() }); + let p = CharProps::default(); + d.insert_object(&Pos::body(0, 0), InlineObject::CommentEnd { id }, &p).unwrap(); + d.insert_object(&Pos::body(0, 0), InlineObject::CommentStart { id }, &p).unwrap(); + } + let l = lay(&d, &LayoutOptions { comment_balloons: true, ..Default::default() }); + // Each card's stripe is a 3 pt wide fill in the author's colour; the cards run down the page. + let mut stripes: Vec<_> = l.pages[0] + .items + .iter() + .filter_map(|it| match it { + Placed::Fill { rect, color } if rect.w == 3.0 => Some((rect.y, *color)), + _ => None, + }) + .collect(); + stripes.sort_by(|a, b| a.0.total_cmp(&b.0)); + let (ana, ben) = (display::revision_color(0), display::revision_color(1)); + assert_ne!(ana, ben); + assert_eq!(stripes.iter().map(|s| s.1).collect::>(), [ana, ben, ana]); +} + +/// A page with tracked changes by several authors in its body and header, and exact-height +/// tables, one nested in a cell of another, whose cells clip their lines. +fn mixed_page() -> (Document, DocLayout) { + let mut d = tracked_paras(6); + let mut hp = Paragraph::with_text("head", CharProps::default()); + hp.runs = vec![Run { len: 4, props: CharProps { ins: Some(2), ..Default::default() } }]; + let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(hp)]); + d.last_section.headers.default = Some(id); + let exact = |rows, cols| { + let mut t = Table::new(rows, cols, 200.0); + for row in &mut t.rows { + row.props.height = Some(0.5); + row.props.height_rule = HeightRule::Exact; + } + t + }; + // A cell with text in it holds a nested table. + let mut outer = exact(2, 2); + outer.rows[0].cells[0].blocks.push(std::sync::Arc::new(Block::Table(exact(2, 2)))); + d.insert_block(StoryRef::Body, &Path::top(1), Block::Table(outer)).unwrap(); + d.insert_block(StoryRef::Body, &Path::top(0), Block::Table(exact(2, 3))).unwrap(); + let l = lay(&d, &LayoutOptions::default()); + (d, l) +} + +/// What drawing `it` on a page of just it (and every clipped cell, for a line) gave before the +/// page's authors and clipped cells were shared: a `page_display` of its own. +fn drawn_alone(d: &Document, page: &Page, it: &Placed, opts: &display::DisplayOptions) -> Vec { + let mut items = vec![it.clone()]; + if matches!(it, Placed::Lines { .. }) { + let cells = page.header.iter().chain(&page.footer).chain(&page.items).filter(|c| matches!(c, Placed::Cell { clip: true, .. })); + items.extend(cells.cloned()); + } + let one = Page { w: page.w, h: page.h, section: page.section, number: page.number, items, body: page.body, ..Default::default() }; + display::page_display(d, &one, opts) +} + +/// #3866: drawing the items of a page one by one with the page's shared authors and clipped +/// cells gives what a `page_display` of each alone gave. +#[test] +fn drawing_items_one_by_one_matches_a_page_display_of_each() { + let (d, l) = mixed_page(); + let page = &l.pages[0]; + let clipped = page.items.iter().filter(|c| matches!(c, Placed::Cell { clip: true, .. })).count(); + assert!(clipped >= 8, "clipped cells, nested ones among them: {clipped}"); + assert!(!page.header.is_empty(), "a header on the page"); + for markup in [true, false] { + let opts = display::DisplayOptions { markup, ..Default::default() }; + let shown = display::ItemDisplay::new(&d, page); + let (mut clips, mut coloured) = (0, 0); + for it in page.header.iter().chain(&page.footer).chain(&page.items) { + let (got, want) = (shown.draws(it, &opts), drawn_alone(&d, page, it, &opts)); + assert_eq!(format!("{got:?}"), format!("{want:?}"), "markup {markup}: {it:?}"); + clips += got.iter().filter(|x| matches!(x, display::Draw::Clip { .. })).count(); + coloured += got.iter().filter(|x| matches!(x, display::Draw::Glyphs { color, .. } if *color != Rgb(0, 0, 0))).count(); + } + assert!(clips > 0, "some lines are clipped"); + assert!(!markup || coloured > 0, "some tracked text is in an author's colour"); + } +} + +/// #3866: a header paragraph on the hinted page is found there, without a walk of every page of a +/// long document to find the first page that shows it. +#[test] +fn selecting_a_header_on_the_hinted_page_does_not_walk_every_page() { + let laid = |n: usize| { + let mut d = Document::from_text("n"); + let huge = CharProps { size: Some(400.0), ..Default::default() }; + d.body = (0..n).map(|_| wordcraft_doc::para_block(Paragraph::with_text("n", huge.clone()))).collect(); + let id = d.add_part(wordcraft_doc::PartKind::Header, vec![wordcraft_doc::para_block(Paragraph::with_text("head", CharProps::default()))]); + d.last_section.headers.default = Some(id); + let l = lay(&d, &LayoutOptions::default()); + let ends = (Pos::new(StoryRef::Part(id), Path::top(0), 0), Pos::new(StoryRef::Part(id), Path::top(0), 2)); + (d, l, ends) + }; + let steps = |n: usize| { + let (d, l, (a, b)) = laid(n); + assert!(l.pages.len() >= n, "a page to each paragraph: {}", l.pages.len()); + work::count(|| drop(std::hint::black_box(l.selection_rects(&d, &a, &b, 0)))) + }; + let (small, big) = (steps(20), steps(160)); + eprintln!("hinted header: {small} steps for 20 pages, {big} for 160"); + assert!(small > 0 && big <= small + 8, "the pages aren't walked: {small} → {big}"); +} diff --git a/crates/ui-egui/src/proofing_pane.rs b/crates/ui-egui/src/proofing_pane.rs index 2e82349ffb..8eb37ef32e 100644 --- a/crates/ui-egui/src/proofing_pane.rs +++ b/crates/ui-egui/src/proofing_pane.rs @@ -103,7 +103,9 @@ fn action_link(ui: &mut Ui, text: &str) -> bool { fn context(ui: &mut Ui, app: &mut WordApp, form: &mut SpellingForm, spelling: bool) { let t = Tokens::get(ui.ctx()); + let width = ui.available_width(); egui::Frame::NONE.fill(t.input).stroke(Stroke::new(1.0, t.input_border)).corner_radius(2.0).inner_margin(8).show(ui, |ui| { + ui.set_min_width((width - 18.0).max(0.0)); ui.horizontal(|ui| { let mut job = egui::text::LayoutJob::default(); let format = egui::TextFormat { font_id: regular(12.0), color: t.text, ..Default::default() }; @@ -157,56 +159,58 @@ fn body(app: &mut WordApp, ui: &mut Ui, form: &mut SpellingForm) { } else { egui::Frame::NONE.fill(t.input).stroke(Stroke::new(1.0, t.input_border)).corner_radius(2.0).show(ui, |ui| { ui.spacing_mut().item_spacing.y = 0.0; - for (index, suggestion) in suggestions.iter().enumerate() { - ui.push_id(index, |ui| { - if index > 0 { - ui.separator(); - } - ui.horizontal(|ui| { - let width = (ui.available_width() - 34.0).max(20.0); - let (rect, response) = ui.allocate_exact_size(vec2(width, 64.0), Sense::click()); - response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, suggestion)); - if response.hovered() { - ui.painter().rect_filled(rect, 0.0, t.hover); + egui::ScrollArea::vertical().id_salt("proofing_suggestions").max_height(210.0).show(ui, |ui| { + for (index, suggestion) in suggestions.iter().enumerate() { + ui.push_id(index, |ui| { + if index > 0 { + ui.separator(); } - ui.painter().with_clip_rect(rect.shrink2(vec2(10.0, 0.0))).text( - rect.left_center() + vec2(12.0, 0.0), - Align2::LEFT_CENTER, - suggestion, - semibold(12.5), - t.text, - ); - if response.clicked() { - change = Some(suggestion.clone()); - } - ui.menu_button("⌄", |ui| { - if ui.button(tl!("Change…")).clicked() { - form.replacement.clone_from(suggestion); - form.manual = true; - ui.close(); + ui.horizontal(|ui| { + let width = (ui.available_width() - 34.0).max(20.0); + let (rect, response) = ui.allocate_exact_size(vec2(width, 64.0), Sense::click()); + response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, suggestion)); + if response.hovered() { + ui.painter().rect_filled(rect, 0.0, t.hover); } - if spelling && ui.button(tl!("Add to AutoCorrect")).clicked() { - let word = form.issue.get("text").and_then(Value::as_str).unwrap_or(""); - let params = json!({"add": {"from": word, "to": suggestion}}); - if form.current(app) { - if let Err(e) = app.run("tools.autocorrect", params) { - form.message = e; - } - } else { - form.message = tl!("The document or selection changed. Check again to continue.").into(); + ui.painter().with_clip_rect(rect.shrink2(vec2(10.0, 0.0))).text( + rect.left_center() + vec2(12.0, 0.0), + Align2::LEFT_CENTER, + suggestion, + semibold(12.5), + t.text, + ); + if response.clicked() { + change = Some(suggestion.clone()); + } + ui.menu_button("⌄", |ui| { + if ui.button(tl!("Change…")).clicked() { + form.replacement.clone_from(suggestion); + form.manual = true; + ui.close(); } - ui.close(); - } - if ui.button(tl!("AutoCorrect Options…")).clicked() { - let _ = app.run("tools.autocorrect", json!({})); - ui.close(); - } - }) - .response - .on_hover_text(tl!("More options")); + if spelling && ui.button(tl!("Add to AutoCorrect")).clicked() { + let word = form.issue.get("text").and_then(Value::as_str).unwrap_or(""); + let params = json!({"add": {"from": word, "to": suggestion}}); + if form.current(app) { + if let Err(e) = app.run("tools.autocorrect", params) { + form.message = e; + } + } else { + form.message = tl!("The document or selection changed. Check again to continue.").into(); + } + ui.close(); + } + if ui.button(tl!("AutoCorrect Options…")).clicked() { + let _ = app.run("tools.autocorrect", json!({})); + ui.close(); + } + }) + .response + .on_hover_text(tl!("More options")); + }); }); - }); - } + } + }); }); } if let Some(replacement) = change { @@ -276,9 +280,10 @@ mod tests { fn spelling_review_dialog_changes_a_suggestion_and_undo_restores_the_word() { let mut h = harness("The quick brwn fox."); assert!(!h.state().canvas.context_menu_open); - h.get_by_label("brown").click(); + let replacement = form(h.state()).issue["suggestions"][0].as_str().unwrap().to_string(); + h.get_by_label(&replacement).click(); h.run_steps(3); - assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), "The quick brown fox."); + assert_eq!(h.state().session.doc.plain_text(StoryRef::Body), format!("The quick {replacement} fox.")); assert!(form(h.state()).done()); h.get_by_label("Close").click(); h.run_steps(2); diff --git a/docs/gaps.md b/docs/gaps.md index 3fe0a67e25..1e1616b0be 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where WordCraft falls short of Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered comparison adjusts context width and suggestion scrolling); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Highlight update:** 2026-10-25 · Highlight shortcut toggles the last chosen colour off when the whole selection has it; explicit colour commands remain setters (#4085). Readiness estimates unchanged. > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** trivial (#7: Custom Document Properties dialog, #2982; previously trivial: egui 0.37 migration: the interface catalogs stay shared and pinned by `craft-i18n.rev` at `9205108cfdced5177706264038848b68b2422d2b` (27 languages, English fallback); input-method handling has new regressions but managed CI passed, and native IME acceptance plus native-speaker catalog review remain open; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: small follow-ups from the field, table and AutoFormat batch, #4232; previously trivial: highlight target includes selected paragraph marks, #4085; previously trivial: full-target highlight check beyond 10,000 runs, #4085; previously trivial: Exact-spacing table contact uses the specified line height rather than paragraph-mark metrics (#3977); previously trivial: cu20we: restrict pre-spacing float origins to paragraph anchors; document cap helpers and regressions; previously trivial: a command that changes nothing is not an edit: no undo step, no unsaved changes, no relayout, and the redo stack survives it; previously minor: #8 narrowed: Spanish proofing (Spain, Mexico, Latin America, Uruguay) from the installed Hunspell dictionaries, #3360, #3553; previously trivial: gap 10: group connector rerouting preserves transformed free and unresolved endpoints under nonuniform scaling; obstacle routing still missing; readiness estimates unchanged; previously trivial: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: the ranked gap list held two rows each for #19, #20 and #21; each gap appears once now; a document with many relationships opens in the time its part takes, #3411; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 @@ -150,7 +150,7 @@ Small follow-ups from the 2026-10-25 field, table and AutoFormat batch (#4232): | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; readiness estimates unchanged | | 2026-10-22 | trivial | View windows: several open documents and Switch Windows in the engine | | 2026-10-25 | trivial | egui 0.37 migration: interface catalogs remain pinned by `craft-i18n.rev` (`9205108…`; 27 languages, English fallback); IME surrounding deletions and composition resets are implemented with regressions, but managed CI passed; native IME acceptance and native-speaker catalog review remain open; no gap or readiness change | | 2026-10-17 | trivial | Gap 9 narrowed: Standard (Word 2007, AES-ECB) encrypted packages open with a password under any Word extension; AES headers in Word 97-2003 files are outside the public spec and refused with a clear message; estimate 3–5 → 0 h, since the remaining AES-in-`.doc` case is refused by design | diff --git a/docs/spelling-review.md b/docs/spelling-review.md index 5c5668f65e..c9ed3fe1e9 100644 --- a/docs/spelling-review.md +++ b/docs/spelling-review.md @@ -1,6 +1,6 @@ # Spelling and grammar task pane -> **Last updated:** 2026-10-26 · **Status:** implementation proposed; hosted checks and rendered comparison remain required before integration. +> **Last updated:** 2026-10-26 · **Status:** first hosted gate and three-platform UI checks passed; rendered comparison found layout gaps now being corrected and revalidated. ## Word reference @@ -25,9 +25,12 @@ This work is independent of French contribution #3185. It consumes language-awar - Offscreen: the ignored spelling_review_dialog_preview test writes light/dark PNGs for comparison with the recorded Word arrangement. - Required gate: cargo xtask ci. Cargo is unavailable on the preparation PC; hosted validation is required, and test definitions alone do not establish success. +The first light/dark artifacts at `20205a5` were inspected: the sentence frame shrank to its text and the unbounded suggestion list pushed actions below the viewport. The revised layout fills the pane width and scrolls the suggestion list after approximately three cards, keeping the actions below it visible. A new preview and hosted gate must validate this revision; exact pixel parity is not claimed. + ## Revision history | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Inspect first successful light/dark artifacts; fill the sentence frame and bound suggestion-list height, retaining all suggestions through scrolling | | 2026-10-26 | minor | Actual Word for Windows observation replaces the modal proposal with a right-side Proofing pane, sentence context and directly clickable suggestion cards | | 2026-10-26 | minor | Separate language-independent correction-form proposal and verification scope | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 7c7a29fa89..1ff1cb2820 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity with Microsoft Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane); previously trivial (Custom Document Properties dialog from File › Info › Properties › Advanced Properties…, #2982; previously trivial: View › Window › New Window opens another window on the document, each with its own caret and view; previously trivial: mail merge: First Record, Last Record and a record number box in Preview Results; Finish & Merge asks which records to merge or print, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: read only and comments-only protection grey out edits; previously trivial: the Font dialog previews "NaN" or "inf" in the Size box at 12 pt, #3663; previously trivial: File › Options disables the User name field while Restrict Editing is active; previously trivial: while editing a header or footer, a click in the other one edits it there without scrolling; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Insert Table of Figures dialog: any caption label, #3013; previously trivial: Home › Underline drop-down shows its lines, #2738; previously trivial: the text context menu includes the formatting mini toolbar, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: the scrollbar page tip stays on one line as the page number grows, #3341; previously trivial: File › Home and New: templates and recent documents are buttons named for screen readers, #3176; previously trivial: forms protection greys out the Developer tab's control buttons and Properties; previously trivial: AltGr doesn't open KeyTips, #2388; previously trivial: closing the header or footer puts the caret back where it was in the body, #3331; previously trivial: Format Page Numbers dialog: number format and Continue from previous section / Start at, #1113; previously trivial: inline pictures drag to a dashed insertion cursor and stay inline, #2233, #2234; previously trivial: Save Changes prompt: a text file saved over itself closes, #2376; previously trivial: the equation being edited is framed as in Word: an outline as tall as its line, a grip, an options strip that opens its menu, and its current argument shaded; previously trivial: the table handles show while the caret is in the table, #2428; previously trivial: legacy menu keys, #1697; previously trivial: Shape Fill › Gradient and the Format Shape pane, #1500; previously trivial: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: the text pointer over the page is drawn black or white on Windows, #1746; previously trivial: a click in the header/footer band from the body no longer scrolls to the body text, #2103; previously trivial: File › Recover follows the app theme, #3243; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: dragging the vertical scrollbar names the page at the top of the window, #2551; previously trivial: Add Font imports every selected font file, #1789); previously trivial (File › Close keeps the window, #1712; previously trivial: the Spike: Ctrl+F3 and Ctrl+Shift+F3; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: the tab strip never draws tabs over Comments / Editing / Share, #2026; previously trivial: Columns on selected text, #2024; previously trivial: Different First Page headers reachable from page 1, #2826; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: Read Mode keeps the keyboard, shows whole columns and keeps text clear of the page-turn bar, #1772); previously trivial: font list entries of tall-ascent fonts sit in their row, not cut off, #2570); previously trivial: Split Cells and Convert Text to Table dialogs follow Word's merged-cell limits and Initial column width, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout text fills the window width, #1876; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717)); previously trivial: comment balloons readable in the dark theme, #2045; previously trivial: Recently Used Fonts section at the top of the font list, #2068; previously trivial: web preferences kept between visits, #2132; previously trivial: dialog close control: a Windows-style box at the title bar's right end, the whole box clickable, red on hover, #1885); previously trivial (Modify Table Style formats every table style part, #714); previously trivial: a click on the ribbon chrome hands the keyboard to the page in the same frame, #1093 follow-up; previously trivial: Page Setup vertical alignment, #1320; previously trivial: the app opens on the Home tab every launch, as in Word; previously trivial: no document behind the Start screen, #1129; previously trivial: equation pictures share a few textures, #1002; previously trivial: selection bar in the left margin, #856; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously major: first version; ribbon, dialogs, shortcuts and on-canvas interaction measured from `crates/ui-egui` and the command registry) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered comparison adjusts context width and suggestion scrolling); previously trivial (Custom Document Properties dialog from File › Info › Properties › Advanced Properties…, #2982; previously trivial: View › Window › New Window opens another window on the document, each with its own caret and view; previously trivial: mail merge: First Record, Last Record and a record number box in Preview Results; Finish & Merge asks which records to merge or print, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: read only and comments-only protection grey out edits; previously trivial: the Font dialog previews "NaN" or "inf" in the Size box at 12 pt, #3663; previously trivial: File › Options disables the User name field while Restrict Editing is active; previously trivial: while editing a header or footer, a click in the other one edits it there without scrolling; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Insert Table of Figures dialog: any caption label, #3013; previously trivial: Home › Underline drop-down shows its lines, #2738; previously trivial: the text context menu includes the formatting mini toolbar, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: the scrollbar page tip stays on one line as the page number grows, #3341; previously trivial: File › Home and New: templates and recent documents are buttons named for screen readers, #3176; previously trivial: forms protection greys out the Developer tab's control buttons and Properties; previously trivial: AltGr doesn't open KeyTips, #2388; previously trivial: closing the header or footer puts the caret back where it was in the body, #3331; previously trivial: Format Page Numbers dialog: number format and Continue from previous section / Start at, #1113; previously trivial: inline pictures drag to a dashed insertion cursor and stay inline, #2233, #2234; previously trivial: Save Changes prompt: a text file saved over itself closes, #2376; previously trivial: the equation being edited is framed as in Word: an outline as tall as its line, a grip, an options strip that opens its menu, and its current argument shaded; previously trivial: the table handles show while the caret is in the table, #2428; previously trivial: legacy menu keys, #1697; previously trivial: Shape Fill › Gradient and the Format Shape pane, #1500; previously trivial: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: the text pointer over the page is drawn black or white on Windows, #1746; previously trivial: a click in the header/footer band from the body no longer scrolls to the body text, #2103; previously trivial: File › Recover follows the app theme, #3243; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: dragging the vertical scrollbar names the page at the top of the window, #2551; previously trivial: Add Font imports every selected font file, #1789); previously trivial (File › Close keeps the window, #1712; previously trivial: the Spike: Ctrl+F3 and Ctrl+Shift+F3; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: the tab strip never draws tabs over Comments / Editing / Share, #2026; previously trivial: Columns on selected text, #2024; previously trivial: Different First Page headers reachable from page 1, #2826; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: Read Mode keeps the keyboard, shows whole columns and keeps text clear of the page-turn bar, #1772); previously trivial: font list entries of tall-ascent fonts sit in their row, not cut off, #2570); previously trivial: Split Cells and Convert Text to Table dialogs follow Word's merged-cell limits and Initial column width, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout text fills the window width, #1876; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717)); previously trivial: comment balloons readable in the dark theme, #2045; previously trivial: Recently Used Fonts section at the top of the font list, #2068; previously trivial: web preferences kept between visits, #2132; previously trivial: dialog close control: a Windows-style box at the title bar's right end, the whole box clickable, red on hover, #1885); previously trivial (Modify Table Style formats every table style part, #714); previously trivial: a click on the ribbon chrome hands the keyboard to the page in the same frame, #1093 follow-up; previously trivial: Page Setup vertical alignment, #1320; previously trivial: the app opens on the Home tab every launch, as in Word; previously trivial: no document behind the Start screen, #1129; previously trivial: equation pictures share a few textures, #1002; previously trivial: selection bar in the left margin, #856; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously major: first version; ribbon, dialogs, shortcuts and on-canvas interaction measured from `crates/ui-egui` and the command registry) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 Recovery discovery no longer interrupts an explicitly created or opened document. Older copies remain in File › Recover, whose buttons use the interface palette. See [local recovery](recovery.md). @@ -88,7 +88,7 @@ Right-click and Shift+F10 keep the compact suggestions menu. The pane is shared | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; readiness estimates unchanged | | 2026-10-22 | trivial | View › Window › New Window opens another window on the document, focused and cascaded; two windows on one document are titled `Name:1` and `Name:2` (one window: the plain name). Each window keeps its own selection, scroll position, zoom, view mode, ruler and panes; the text, undo history, unsaved state and file are the document's, so an edit or Undo in either window shows in both, and a caret in text the other window deleted moves to the nearest place left. Closing one of a document's windows asks nothing; closing its last asks about unsaved changes, and quitting asks once per document. Switch Windows lists every window. The web app keeps one window | | 2026-10-22 | trivial | One native window per open document on the desktop (the web app keeps one window): New and Open open the document in a window of its own, focused and cascaded from the last; Open replaces an untouched blank document in its window and brings an already-open file's window forward; each window keeps its own ribbon tab, File page, dialogs, panes and composition; View › Window › Switch Windows lists the windows and brings the chosen one forward; File › Close and the window's close button close the window (asking about unsaved changes) and the window focused before comes forward, while the last window stays open with no document; quitting asks Save / Don't Save / Cancel about each document in its own window | | 2026-10-22 | trivial | Several open documents in one window (one window per document comes next): View › Window › Switch Windows lists the open documents, the shown one marked and unsaved ones dotted, and shows the one chosen, with its own scroll position, zoom, comment drafts and AutoSave file; File › New, Open, Envelopes, Labels, Finish & Merge and personal templates open beside the open documents without asking about unsaved changes; File › Close shows the next open document; the window's close button asks Save / Don't Save / Cancel about each document with unsaved changes, showing it, and Cancel keeps the app open. Untitled documents are numbered (Document2) in the interface language | From 057350ef6ec9d966d7e224b41fdef144100aee65 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:48:32 +0100 Subject: [PATCH 016/308] M7: keep the proofing Read Aloud control at the context edge The 056b18b preview confirms full-width context and visible actions, but the text child still shrinks to its contents and pulls the Read Aloud control inward. Keep that child at its allocated width so the control aligns at the right edge as observed in Word. All five workflows passed at parent 056b18b; both light/dark artifacts were downloaded and inspected. This alignment revision needs new hosted checks and preview inspection. Local cargo xtask ci was attempted but cargo is absent; git diff --check passes. --- crates/ui-egui/src/proofing_pane.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/proofing_pane.rs b/crates/ui-egui/src/proofing_pane.rs index 8eb37ef32e..e63f9e018f 100644 --- a/crates/ui-egui/src/proofing_pane.rs +++ b/crates/ui-egui/src/proofing_pane.rs @@ -113,7 +113,9 @@ fn context(ui: &mut Ui, app: &mut WordApp, form: &mut SpellingForm, spelling: bo let marked = egui::TextFormat { underline: Stroke::new(1.0, if spelling { t.red } else { t.blue }), ..format.clone() }; job.append(form.issue.get("text").and_then(Value::as_str).unwrap_or(""), 0.0, marked); job.append(&form.context_after, 0.0, format); - ui.allocate_ui_with_layout(vec2((ui.available_width() - 36.0).max(20.0), 30.0), egui::Layout::top_down(Align::Min), |ui| { + let text_width = (ui.available_width() - 36.0).max(20.0); + ui.allocate_ui_with_layout(vec2(text_width, 30.0), egui::Layout::top_down(Align::Min), |ui| { + ui.set_min_width(text_width); ui.add(egui::Label::new(job).wrap()); }); let (rect, response) = ui.allocate_exact_size(vec2(28.0, 28.0), Sense::click()); From 62f905084852b4d24381a776c9f605a1264f2ca6 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:53:47 +0100 Subject: [PATCH 017/308] M7: resolve exact proofing selections and address pane review Give the exact selected issue priority over overlapping grammar when querying the focus, including reversed selections. Keep point lookup for explicit other positions. Add command and pane regressions for overlapping repeated misspellings, correction and undo. Include command/window/document/canvas integration paths in preview workflow triggers. Preserve historical three-platform evidence while recording f126's successful gate/previews/Windows/Linux and outstanding macOS validation after pre-run cancellations. These address the three CodeRabbit findings on official #4471. Local cargo xtask ci was attempted but cargo is absent; git diff --check passes. New hosted checks must validate this SHA before any claim of success. --- .github/workflows/spelling-review.yml | 5 ++++ ROADMAP.md | 4 +-- crates/engine/src/cmd/review.rs | 36 ++++++++++++++++++++++++++- crates/ui-egui/src/proofing_pane.rs | 16 ++++++++++++ docs/gaps.md | 4 +-- docs/spelling-review.md | 7 +++--- docs/ui-parity.md | 4 +-- 7 files changed, 66 insertions(+), 10 deletions(-) diff --git a/.github/workflows/spelling-review.yml b/.github/workflows/spelling-review.yml index a3ead7ce3f..df2cfa951b 100644 --- a/.github/workflows/spelling-review.yml +++ b/.github/workflows/spelling-review.yml @@ -5,6 +5,11 @@ on: paths: - 'crates/ui-egui/src/proofing_pane.rs' - 'crates/ui-egui/src/panes.rs' + - 'crates/ui-egui/src/lib.rs' + - 'crates/ui-egui/src/window_ui.rs' + - 'crates/ui-egui/src/control.rs' + - 'crates/ui-egui/src/documents.rs' + - 'crates/ui-egui/src/canvas.rs' - 'crates/engine/src/cmd/review.rs' - '.github/workflows/spelling-review.yml' workflow_dispatch: diff --git a/ROADMAP.md b/ROADMAP.md index 5d3be77a19..54a5ddb164 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered comparison adjusts context width and suggestion scrolling); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered layout and overlapping-issue selection review); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-18 · **Change:** trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; restructured to the craftrules progress-docs standard) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -644,7 +644,7 @@ Footer page-table height and body wrapping are corrected. Other pagination gaps | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; exact selected spelling takes priority over overlapping grammar, with command/pane regressions and integration workflow paths; readiness estimates unchanged | | 2026-10-22 | trivial | Progress log: one native window per open document | | 2026-10-22 | trivial | Progress log: every open document keeps its own recovery copy | | 2026-10-22 | trivial | Progress log: several open documents in the desktop window | diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index 873c7f674f..bdfdd27119 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -976,7 +976,16 @@ fn para_issues(s: &Session, story: StoryRef, path: &wordcraft_doc::Path) -> Vec< /// The issue under a position. fn issue_at(s: &Session, at: &Pos) -> Option<(Pos, Pos, wordcraft_proof::Issue)> { - para_issues(s, at.story, &at.path).into_iter().find(|(a, b, _)| a.off <= at.off && at.off <= b.off) + let issues = para_issues(s, at.story, &at.path); + let selected = s.sel.ordered(); + // F7 can select a spelling issue inside a larger grammar issue. Keep that + // exact target for either selection direction before falling back to a point. + if at == &s.sel.focus + && let Some(issue) = issues.iter().find(|(a, b, _)| a == &selected.0 && b == &selected.1) + { + return Some(issue.clone()); + } + issues.into_iter().find(|(a, b, _)| a.off <= at.off && at.off <= b.off) } fn issue_json(s: &Session, a: &Pos, b: &Pos, i: &wordcraft_proof::Issue) -> Value { @@ -2275,4 +2284,29 @@ mod spelling_navigation_tests { assert_eq!(s.run("review.spelling", &json!({})).unwrap()["start"], first["start"]); } } + #[test] + fn spelling_review_dialog_navigation_resolves_selected_spelling_before_overlapping_grammar() { + for reverse in [false, true] { + let mut s = Session::new(Document::from_text("A sentense sentense.")); + s.sel = Selection::caret(Pos::body(0, 11)); + let selected = s.run("review.spelling", &json!({"wrap": false})).unwrap(); + assert_eq!(selected["kind"], "spelling"); + assert_eq!(selected["start"]["off"], 11); + assert_eq!(selected["text"], "sentense"); + let issues = s.run("review.issues", &json!({})).unwrap(); + assert!(issues.as_array().unwrap().iter().any(|i| i["kind"] == "grammar" && i["text"] == "sentense sentense")); + if reverse { + std::mem::swap(&mut s.sel.anchor, &mut s.sel.focus); + } + assert_eq!(s.run("review.suggestions", &json!({})).unwrap(), selected); + let elsewhere = s.run("review.suggestions", &json!({"pos": {"block": 0, "off": 5}})).unwrap(); + assert_eq!(elsewhere["kind"], "spelling"); + assert_eq!(elsewhere["start"]["off"], 2, "an explicit other position keeps point lookup"); + s.run("review.applySuggestion", &json!({"text": "sentence"})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A sentense sentence."); + s.run("edit.undo", &json!({})).unwrap(); + assert_eq!(s.doc.plain_text(StoryRef::Body), "A sentense sentense."); + } + } + } diff --git a/crates/ui-egui/src/proofing_pane.rs b/crates/ui-egui/src/proofing_pane.rs index e63f9e018f..91cf5dac0a 100644 --- a/crates/ui-egui/src/proofing_pane.rs +++ b/crates/ui-egui/src/proofing_pane.rs @@ -325,6 +325,22 @@ mod tests { } } + #[test] + fn spelling_review_dialog_selected_spelling_stays_actionable_when_grammar_overlaps() { + let mut a = app("A sentense sentense."); + a.session.sel = Selection::caret(Pos::body(0, 11)); + a.run("review.spelling", json!({"wrap": false})).unwrap(); + let mut f = a.spelling_review.take().unwrap(); + assert_eq!(f.issue["kind"], "spelling"); + assert!(f.current(&mut a), "the selected typo is current despite overlapping grammar"); + f.replacement = "sentence".into(); + f.act(&mut a, Some("review.applySuggestion")); + assert!(f.message.is_empty()); + assert_eq!(a.session.doc.plain_text(StoryRef::Body), "A sentense sentence."); + a.run("edit.undo", json!({})).unwrap(); + assert_eq!(a.session.doc.plain_text(StoryRef::Body), "A sentense sentense."); + } + #[test] fn spelling_review_dialog_refuses_a_stale_selection_or_replaced_body() { for replace in [false, true] { diff --git a/docs/gaps.md b/docs/gaps.md index 1e1616b0be..5fd16566e5 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where WordCraft falls short of Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered comparison adjusts context width and suggestion scrolling); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered layout and overlapping-issue selection review); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Highlight update:** 2026-10-25 · Highlight shortcut toggles the last chosen colour off when the whole selection has it; explicit colour commands remain setters (#4085). Readiness estimates unchanged. > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** trivial (#7: Custom Document Properties dialog, #2982; previously trivial: egui 0.37 migration: the interface catalogs stay shared and pinned by `craft-i18n.rev` at `9205108cfdced5177706264038848b68b2422d2b` (27 languages, English fallback); input-method handling has new regressions but managed CI passed, and native IME acceptance plus native-speaker catalog review remain open; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: small follow-ups from the field, table and AutoFormat batch, #4232; previously trivial: highlight target includes selected paragraph marks, #4085; previously trivial: full-target highlight check beyond 10,000 runs, #4085; previously trivial: Exact-spacing table contact uses the specified line height rather than paragraph-mark metrics (#3977); previously trivial: cu20we: restrict pre-spacing float origins to paragraph anchors; document cap helpers and regressions; previously trivial: a command that changes nothing is not an edit: no undo step, no unsaved changes, no relayout, and the redo stack survives it; previously minor: #8 narrowed: Spanish proofing (Spain, Mexico, Latin America, Uruguay) from the installed Hunspell dictionaries, #3360, #3553; previously trivial: gap 10: group connector rerouting preserves transformed free and unresolved endpoints under nonuniform scaling; obstacle routing still missing; readiness estimates unchanged; previously trivial: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: the ranked gap list held two rows each for #19, #20 and #21; each gap appears once now; a document with many relationships opens in the time its part takes, #3411; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 @@ -150,7 +150,7 @@ Small follow-ups from the 2026-10-25 field, table and AutoFormat batch (#4232): | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; exact selected spelling takes priority over overlapping grammar, with command/pane regressions and integration workflow paths; readiness estimates unchanged | | 2026-10-22 | trivial | View windows: several open documents and Switch Windows in the engine | | 2026-10-25 | trivial | egui 0.37 migration: interface catalogs remain pinned by `craft-i18n.rev` (`9205108…`; 27 languages, English fallback); IME surrounding deletions and composition resets are implemented with regressions, but managed CI passed; native IME acceptance and native-speaker catalog review remain open; no gap or readiness change | | 2026-10-17 | trivial | Gap 9 narrowed: Standard (Word 2007, AES-ECB) encrypted packages open with a password under any Word extension; AES headers in Word 97-2003 files are outside the public spec and refused with a clear message; estimate 3–5 → 0 h, since the remaining AES-in-`.doc` case is refused by design | diff --git a/docs/spelling-review.md b/docs/spelling-review.md index c9ed3fe1e9..dd695e9b4b 100644 --- a/docs/spelling-review.md +++ b/docs/spelling-review.md @@ -1,6 +1,6 @@ # Spelling and grammar task pane -> **Last updated:** 2026-10-26 · **Status:** first hosted gate and three-platform UI checks passed; rendered comparison found layout gaps now being corrected and revalidated. +> **Last updated:** 2026-10-26 · **Status:** historical three-platform checks passed; at f1264f3 the revised hosted gate, previews, Windows and Linux passed, while macOS remained unvalidated after pre-run cancellations. Overlap-review corrections now require new hosted checks. ## Word reference @@ -20,17 +20,18 @@ This work is independent of French contribution #3185. It consumes language-awar ## Verification -- Engine: one-/two-paragraph navigation, forward-only completion and explicit wrapping. +- Engine: one-/two-paragraph navigation, forward-only completion, explicit wrapping, exact spelling selection within overlapping grammar in either selection direction, explicit-position fallback, correction and undo. - UI: direct suggestion correction and undo, Ignore Once progress/completion, Ignore All, personal dictionary addition, stale selection/body refusal, completion and Close, per-window isolation and document switching. - Offscreen: the ignored spelling_review_dialog_preview test writes light/dark PNGs for comparison with the recorded Word arrangement. - Required gate: cargo xtask ci. Cargo is unavailable on the preparation PC; hosted validation is required, and test definitions alone do not establish success. -The first light/dark artifacts at `20205a5` were inspected: the sentence frame shrank to its text and the unbounded suggestion list pushed actions below the viewport. The revised layout fills the pane width and scrolls the suggestion list after approximately three cards, keeping the actions below it visible. A new preview and hosted gate must validate this revision; exact pixel parity is not claimed. +The first light/dark artifacts at `20205a5` were inspected: the sentence frame shrank to its text and the unbounded suggestion list pushed actions below the viewport. The revised layout fills the pane width and scrolls the suggestion list after approximately three cards, keeping the actions below it visible. At `f1264f3`, the revised light/dark previews and full hosted gate passed; Windows and Linux UI checks passed, while macOS remained unvalidated after pre-run cancellations. Both PNGs were inspected, including the right-aligned Read Aloud control. The following review revision gives the exact selected spelling range priority over an overlapping grammar issue and requires new hosted checks. Exact pixel parity is not claimed. ## Revision history | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Resolve selected spelling before overlapping grammar, add command/pane regressions, include pane-integration workflow paths and distinguish validated history from pending review corrections | | 2026-10-26 | minor | Inspect first successful light/dark artifacts; fill the sentence frame and bound suggestion-list height, retaining all suggestions through scrolling | | 2026-10-26 | minor | Actual Word for Windows observation replaces the modal proposal with a right-side Proofing pane, sentence context and directly clickable suggestion cards | | 2026-10-26 | minor | Separate language-independent correction-form proposal and verification scope | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 1ff1cb2820..bf3999acc3 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity with Microsoft Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered comparison adjusts context width and suggestion scrolling); previously trivial (Custom Document Properties dialog from File › Info › Properties › Advanced Properties…, #2982; previously trivial: View › Window › New Window opens another window on the document, each with its own caret and view; previously trivial: mail merge: First Record, Last Record and a record number box in Preview Results; Finish & Merge asks which records to merge or print, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: read only and comments-only protection grey out edits; previously trivial: the Font dialog previews "NaN" or "inf" in the Size box at 12 pt, #3663; previously trivial: File › Options disables the User name field while Restrict Editing is active; previously trivial: while editing a header or footer, a click in the other one edits it there without scrolling; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Insert Table of Figures dialog: any caption label, #3013; previously trivial: Home › Underline drop-down shows its lines, #2738; previously trivial: the text context menu includes the formatting mini toolbar, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: the scrollbar page tip stays on one line as the page number grows, #3341; previously trivial: File › Home and New: templates and recent documents are buttons named for screen readers, #3176; previously trivial: forms protection greys out the Developer tab's control buttons and Properties; previously trivial: AltGr doesn't open KeyTips, #2388; previously trivial: closing the header or footer puts the caret back where it was in the body, #3331; previously trivial: Format Page Numbers dialog: number format and Continue from previous section / Start at, #1113; previously trivial: inline pictures drag to a dashed insertion cursor and stay inline, #2233, #2234; previously trivial: Save Changes prompt: a text file saved over itself closes, #2376; previously trivial: the equation being edited is framed as in Word: an outline as tall as its line, a grip, an options strip that opens its menu, and its current argument shaded; previously trivial: the table handles show while the caret is in the table, #2428; previously trivial: legacy menu keys, #1697; previously trivial: Shape Fill › Gradient and the Format Shape pane, #1500; previously trivial: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: the text pointer over the page is drawn black or white on Windows, #1746; previously trivial: a click in the header/footer band from the body no longer scrolls to the body text, #2103; previously trivial: File › Recover follows the app theme, #3243; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: dragging the vertical scrollbar names the page at the top of the window, #2551; previously trivial: Add Font imports every selected font file, #1789); previously trivial (File › Close keeps the window, #1712; previously trivial: the Spike: Ctrl+F3 and Ctrl+Shift+F3; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: the tab strip never draws tabs over Comments / Editing / Share, #2026; previously trivial: Columns on selected text, #2024; previously trivial: Different First Page headers reachable from page 1, #2826; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: Read Mode keeps the keyboard, shows whole columns and keeps text clear of the page-turn bar, #1772); previously trivial: font list entries of tall-ascent fonts sit in their row, not cut off, #2570); previously trivial: Split Cells and Convert Text to Table dialogs follow Word's merged-cell limits and Initial column width, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout text fills the window width, #1876; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717)); previously trivial: comment balloons readable in the dark theme, #2045; previously trivial: Recently Used Fonts section at the top of the font list, #2068; previously trivial: web preferences kept between visits, #2132; previously trivial: dialog close control: a Windows-style box at the title bar's right end, the whole box clickable, red on hover, #1885); previously trivial (Modify Table Style formats every table style part, #714); previously trivial: a click on the ribbon chrome hands the keyboard to the page in the same frame, #1093 follow-up; previously trivial: Page Setup vertical alignment, #1320; previously trivial: the app opens on the Home tab every launch, as in Word; previously trivial: no document behind the Start screen, #1129; previously trivial: equation pictures share a few textures, #1002; previously trivial: selection bar in the left margin, #856; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously major: first version; ribbon, dialogs, shortcuts and on-canvas interaction measured from `crates/ui-egui` and the command registry) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-26 · **Change:** minor (Word-observed spelling and grammar task pane; rendered layout and overlapping-issue selection review); previously trivial (Custom Document Properties dialog from File › Info › Properties › Advanced Properties…, #2982; previously trivial: View › Window › New Window opens another window on the document, each with its own caret and view; previously trivial: mail merge: First Record, Last Record and a record number box in Preview Results; Finish & Merge asks which records to merge or print, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: read only and comments-only protection grey out edits; previously trivial: the Font dialog previews "NaN" or "inf" in the Size box at 12 pt, #3663; previously trivial: File › Options disables the User name field while Restrict Editing is active; previously trivial: while editing a header or footer, a click in the other one edits it there without scrolling; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Insert Table of Figures dialog: any caption label, #3013; previously trivial: Home › Underline drop-down shows its lines, #2738; previously trivial: the text context menu includes the formatting mini toolbar, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: the scrollbar page tip stays on one line as the page number grows, #3341; previously trivial: File › Home and New: templates and recent documents are buttons named for screen readers, #3176; previously trivial: forms protection greys out the Developer tab's control buttons and Properties; previously trivial: AltGr doesn't open KeyTips, #2388; previously trivial: closing the header or footer puts the caret back where it was in the body, #3331; previously trivial: Format Page Numbers dialog: number format and Continue from previous section / Start at, #1113; previously trivial: inline pictures drag to a dashed insertion cursor and stay inline, #2233, #2234; previously trivial: Save Changes prompt: a text file saved over itself closes, #2376; previously trivial: the equation being edited is framed as in Word: an outline as tall as its line, a grip, an options strip that opens its menu, and its current argument shaded; previously trivial: the table handles show while the caret is in the table, #2428; previously trivial: legacy menu keys, #1697; previously trivial: Shape Fill › Gradient and the Format Shape pane, #1500; previously trivial: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: the text pointer over the page is drawn black or white on Windows, #1746; previously trivial: a click in the header/footer band from the body no longer scrolls to the body text, #2103; previously trivial: File › Recover follows the app theme, #3243; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: dragging the vertical scrollbar names the page at the top of the window, #2551; previously trivial: Add Font imports every selected font file, #1789); previously trivial (File › Close keeps the window, #1712; previously trivial: the Spike: Ctrl+F3 and Ctrl+Shift+F3; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: the tab strip never draws tabs over Comments / Editing / Share, #2026; previously trivial: Columns on selected text, #2024; previously trivial: Different First Page headers reachable from page 1, #2826; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: Read Mode keeps the keyboard, shows whole columns and keeps text clear of the page-turn bar, #1772); previously trivial: font list entries of tall-ascent fonts sit in their row, not cut off, #2570); previously trivial: Split Cells and Convert Text to Table dialogs follow Word's merged-cell limits and Initial column width, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout text fills the window width, #1876; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717)); previously trivial: comment balloons readable in the dark theme, #2045; previously trivial: Recently Used Fonts section at the top of the font list, #2068; previously trivial: web preferences kept between visits, #2132; previously trivial: dialog close control: a Windows-style box at the title bar's right end, the whole box clickable, red on hover, #1885); previously trivial (Modify Table Style formats every table style part, #714); previously trivial: a click on the ribbon chrome hands the keyboard to the page in the same frame, #1093 follow-up; previously trivial: Page Setup vertical alignment, #1320; previously trivial: the app opens on the Home tab every launch, as in Word; previously trivial: no document behind the Start screen, #1129; previously trivial: equation pictures share a few textures, #1002; previously trivial: selection bar in the left margin, #856; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously major: first version; ribbon, dialogs, shortcuts and on-canvas interaction measured from `crates/ui-egui` and the command registry) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 Recovery discovery no longer interrupts an explicitly created or opened document. Older copies remain in File › Recover, whose buttons use the interface palette. See [local recovery](recovery.md). @@ -88,7 +88,7 @@ Right-click and Shift+F10 keep the compact suggestions menu. The pane is shared | Date | Change | Summary | |---|---|---| -| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; readiness estimates unchanged | +| 2026-10-26 | minor | Word-observed Proofing task pane uses shared review commands; clicking a suggestion corrects and advances; context, suggestion cards and ignore/dictionary links follow the Windows reference; inspected previews prompted a full-width context and scrollable three-card list; exact selected spelling takes priority over overlapping grammar, with command/pane regressions and integration workflow paths; readiness estimates unchanged | | 2026-10-22 | trivial | View › Window › New Window opens another window on the document, focused and cascaded; two windows on one document are titled `Name:1` and `Name:2` (one window: the plain name). Each window keeps its own selection, scroll position, zoom, view mode, ruler and panes; the text, undo history, unsaved state and file are the document's, so an edit or Undo in either window shows in both, and a caret in text the other window deleted moves to the nearest place left. Closing one of a document's windows asks nothing; closing its last asks about unsaved changes, and quitting asks once per document. Switch Windows lists every window. The web app keeps one window | | 2026-10-22 | trivial | One native window per open document on the desktop (the web app keeps one window): New and Open open the document in a window of its own, focused and cascaded from the last; Open replaces an untouched blank document in its window and brings an already-open file's window forward; each window keeps its own ribbon tab, File page, dialogs, panes and composition; View › Window › Switch Windows lists the windows and brings the chosen one forward; File › Close and the window's close button close the window (asking about unsaved changes) and the window focused before comes forward, while the last window stays open with no document; quitting asks Save / Don't Save / Cancel about each document in its own window | | 2026-10-22 | trivial | Several open documents in one window (one window per document comes next): View › Window › Switch Windows lists the open documents, the shown one marked and unsaved ones dotted, and shows the one chosen, with its own scroll position, zoom, comment drafts and AutoSave file; File › New, Open, Envelopes, Labels, Finish & Merge and personal templates open beside the open documents without asking about unsaved changes; File › Close shows the next open document; the window's close button asks Save / Don't Save / Cancel about each document with unsaved changes, showing it, and Cancel keeps the app open. Untitled documents are numbered (Document2) in the interface language | From 0cd06c786c2ac1194674744bf6f3abac773676bf Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:57:22 +0100 Subject: [PATCH 018/308] M7: normalize regression module spacing Remove the extra blank line before the new navigation regression module's closing brace. No behavior change. Local cargo xtask ci was attempted but cargo is absent; git diff --check passes. Hosted checks must validate this new SHA. --- crates/engine/src/cmd/review.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index bdfdd27119..2d37805285 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -2308,5 +2308,4 @@ mod spelling_navigation_tests { assert_eq!(s.doc.plain_text(StoryRef::Body), "A sentense sentense."); } } - } From 46a3067e2077a100358f32e614374f0c088eb587 Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Wed, 14 Oct 2026 23:59:12 +0200 Subject: [PATCH 019/308] proof: flag aliases, single-byte flags, ICONV, IGNORE and KEEPCASE in affix dictionaries (#735) The Hungarian Hunspell dictionary declares SET UTF-8 but writes its flags as single bytes, uses AF aliases and 158 distinct flags. Read flag fields byte by byte unless FLAG UTF-8 is set, expand AF aliases, skip AM, apply ICONV and IGNORE, honour KEEPCASE, and widen flag masks to 256 bits. Compounding and NEEDAFFIX stay unsupported for now. --- crates/proof/src/affix.rs | 454 ++++++++++++++++++++++++++--------- crates/proof/src/hunspell.rs | 186 +++++++++++++- 2 files changed, 530 insertions(+), 110 deletions(-) diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 4ec068466e..4ce915afa8 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -11,10 +11,14 @@ //! also be stored in WordCraft's compact format ([`AffixDict::to_bytes`]): the rules as text //! lines and the stems front-coded, raw-deflated together (the Polish dictionary is under 1 MB). //! -//! Supported: one-character flags (at most 128 distinct, any Unicode character — `FLAG UTF-8`), -//! `PFX`/`SFX` with strip, append and condition (`.`, letters, `[abc]`, `[^abc]`), cross -//! products, continuation classes on suffixes (at most two suffixes in a row, as in Hunspell), -//! `FORBIDDENWORD`, `NOSUGGEST`, and the suggestion tables `TRY`, `KEY`, `MAP` and `REP`. Rejected: compounding, +//! Supported: one-character flags (at most 256 distinct, any Unicode character — `FLAG UTF-8`), +//! flag aliases (`AF`: a number standing for a set of flags), `PFX`/`SFX` with strip, append and +//! condition (`.`, letters, `[abc]`, `[^abc]`), cross products, continuation classes on suffixes +//! (at most two suffixes in a row, as in Hunspell), `FORBIDDENWORD`, `NOSUGGEST`, `KEEPCASE` (the +//! stem is a word only in its own letter case), input conversion (`ICONV`: ligatures such as `fi` +//! read as `fi`) and ignored characters (`IGNORE`), and the suggestion tables `TRY`, `KEY`, `MAP` +//! and `REP`. +//! Morphological data (`AM` aliases, the fields after a stem) is skipped. Rejected: compounding, //! long or numeric flags, `NEEDAFFIX` and the other directives that change which words are correct. //! //! Lookups never panic: stems sit in one string, found through an open-addressing hash table, and @@ -33,8 +37,6 @@ const EMPTY: u32 = u32::MAX; /// Directives that change which words are correct in ways this engine doesn't implement. const UNSUPPORTED: &[&str] = &[ - "AF", - "AM", "CHECKSHARPS", "CIRCUMFIX", "COMPLEXPREFIXES", @@ -46,17 +48,78 @@ const UNSUPPORTED: &[&str] = &[ "COMPOUNDPERMITFLAG", "COMPOUNDRULE", "FULLSTRIP", - "ICONV", - "IGNORE", - "KEEPCASE", - "LEMMA_PRESENT", "NEEDAFFIX", "OCONV", "ONLYINCOMPOUND", "PSEUDOROOT", - "SUBSTANDARD", ]; +/// Most distinct flags a dictionary may use: every single-byte flag (the Hungarian dictionary +/// uses 158). +const MAX_FLAGS: usize = 256; + +/// A set of flags, one bit per distinct flag character of the dictionary. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +struct Mask([u64; MAX_FLAGS / 64]); + +impl Mask { + const EMPTY: Mask = Mask([0; MAX_FLAGS / 64]); + + /// The set holding flag number `i` alone (empty when `i` is out of range). + fn bit(i: usize) -> Mask { + let mut m = Mask::EMPTY; + if let Some(w) = m.0.get_mut(i / 64) { + *w = 1 << (i % 64); + } + m + } + + /// The number of the lowest flag in the set, if any. + fn first(self) -> Option { + self.0.iter().enumerate().find(|(_, w)| **w != 0).map(|(i, w)| i * 64 + w.trailing_zeros() as usize) + } + + fn is_empty(self) -> bool { + self.0.iter().all(|&w| w == 0) + } + + /// Do the two sets share a flag? + fn meets(self, other: Mask) -> bool { + !(self & other).is_empty() + } + + /// Does this set hold every flag of `other`? + fn covers(self, other: Mask) -> bool { + (self & other) == other + } +} + +impl std::ops::BitAnd for Mask { + type Output = Mask; + fn bitand(mut self, o: Mask) -> Mask { + for (a, b) in self.0.iter_mut().zip(o.0) { + *a &= b; + } + self + } +} + +impl std::ops::BitOr for Mask { + type Output = Mask; + fn bitor(mut self, o: Mask) -> Mask { + self |= o; + self + } +} + +impl std::ops::BitOrAssign for Mask { + fn bitor_assign(&mut self, o: Mask) { + for (a, b) in self.0.iter_mut().zip(o.0) { + *a |= b; + } + } +} + /// One character class of a rule's condition. #[derive(Clone, Debug, PartialEq, Eq)] enum Cond { @@ -130,14 +193,14 @@ const MAX_FORMS_WORK: usize = 1 << 18; #[derive(Clone, Debug)] struct Rule { /// Bit of the rule's flag in the stems' flag masks. - bit: u128, + bit: Mask, flag: char, cross: bool, strip: Box, add: Box, /// Continuation classes (`SFX A 0 able/B`): flags the derived form carries, licensing /// further affixes. Chained application is bounded by [`MAX_CHAIN`]. - cont: u128, + cont: Mask, cond: Box<[Cond]>, cond_text: Box, } @@ -174,11 +237,11 @@ pub struct AffixDict { /// Per stem: index into `sets`. flags: Vec, /// Distinct flag masks. - sets: Vec, + sets: Vec, /// The `FORBIDDENWORD` flag's bit; stems carrying it are not words (0: none declared). - forbidden: u128, + forbidden: Mask, /// Stems with NOSUGGEST are correct, but none of their forms are suggested. - no_suggest: u128, + no_suggest: Mask, /// Open-addressing hash table of stem indices (`EMPTY` = free). table: Vec, /// Flag char of each bit. @@ -195,6 +258,12 @@ pub struct AffixDict { key: String, map: Vec>, rep: Vec<(String, String)>, + /// The `KEEPCASE` flag's bit: such stems are words only in their own letter case (0: none). + keepcase: Mask, + /// `ICONV`: input conversions applied before a lookup, `(from, to)`. + iconv: Vec<(String, String)>, + /// `IGNORE`: characters removed from input words (and from the stems and affixes). + ignore: Box<[char]>, /// Provenance and licence lines, kept through [`AffixDict::to_bytes`]. comments: Vec, } @@ -263,6 +332,49 @@ struct Rules { /// The `FORBIDDENWORD` flag: stems carrying it are listed but never correct. forbidden: Option, no_suggest: Option, + /// The `KEEPCASE` flag. + keepcase: Option, + iconv: Vec<(String, String)>, + ignore: Vec, + /// `AF` flag aliases: alias `n` (1-based) stands for `aliases[n - 1]`. + aliases: Vec, +} + +impl Rules { + /// Flags as written in a `.dic` entry or a continuation class: an `AF` alias number when the + /// dictionary declares aliases, otherwise the flag characters themselves. + fn flags<'a>(&'a self, written: &'a str) -> Result<&'a str, String> { + if self.aliases.is_empty() || written.is_empty() || !written.bytes().all(|b| b.is_ascii_digit()) { + return Ok(written); + } + written + .parse::() + .ok() + .and_then(|n| n.checked_sub(1)) + .and_then(|i| self.aliases.get(i)) + .map(String::as_str) + .ok_or_else(|| format!("flag alias {written} is not defined")) + } + + /// `w` without the `IGNORE` characters. + fn strip_ignored(&self, w: &str) -> String { + if self.ignore.is_empty() { w.to_string() } else { w.chars().filter(|c| !self.ignore.contains(c)).collect() } + } +} + +/// The fields of an `.aff`/`.dic` line: Hunspell separates them with spaces and tabs only, so a +/// flag byte read as U+0085 or U+00A0 (see [`crate::hunspell::decode`]) stays a flag. +fn split_fields(line: &str) -> impl Iterator { + line.split([' ', '\t']).filter(|f| !f.is_empty()) +} + +/// The one flag character a directive (`FORBIDDENWORD w`) names. +fn one_flag(arg: &str, dir: &str, n: usize) -> Result { + let mut it = arg.chars().filter(|&c| c != EMOJI_SELECTOR); + match (it.next(), it.next()) { + (Some(c), None) if !c.is_whitespace() => Ok(c), + _ => Err(format!("line {}: {dir} needs one flag character", n + 1)), + } } /// A rule as parsed from text, before flags become bits: `(flag, cross, strip, add, cont, cond)`. @@ -304,8 +416,10 @@ fn parse_aff(aff: &str) -> Result { let mut cross: HashMap<(bool, char), bool> = HashMap::new(); let mut map_header = false; let mut rep_header = false; + let mut af_header = false; + let mut iconv_header = false; for (n, line) in aff.lines().enumerate() { - let f: Vec<&str> = line.split_whitespace().collect(); + let f: Vec<&str> = split_fields(line).collect(); let Some(&dir) = f.first() else { continue }; if dir.starts_with('#') { continue; @@ -314,25 +428,33 @@ fn parse_aff(aff: &str) -> Result { match dir { "SET" | "LANG" | "NAME" | "VERSION" | "HOME" | "WORDCHARS" | "MAXNGRAMSUGS" | "MAXDIFF" | "ONLYMAXDIFF" | "NOSPLITSUGS" | "SUGSWITHDOTS" | "WARN" | "FORBIDWARN" | "MAXCPDSUGS" => {} + // Morphological aliases, and flags that only matter for morphological generation + // and suggestions: the words they mark are correct. + "AM" | "SUBSTANDARD" | "LEMMA_PRESENT" => {} + "AF" => { + if !af_header && f.len() == 2 && at(1).parse::().is_ok() { + af_header = true; + } else { + r.aliases.push(at(1).to_string()); + } + } + "ICONV" => { + if !iconv_header && f.len() == 2 && at(1).parse::().is_ok() { + iconv_header = true; + } else if f.len() >= 3 { + r.iconv.push((at(1).to_string(), at(2).to_string())); + } + } + "IGNORE" => r.ignore = at(1).chars().collect(), + "KEEPCASE" => r.keepcase = Some(one_flag(at(1), dir, n)?), // Per-character flag modes (`FLAG UTF-8`, as VERO's pt_BR dictionary uses) work // with the one-character parsing here; only the multi-character encodings differ. "FLAG" if !matches!(at(1), "UTF-8" | "DEFAULT" | "utf-8" | "default" | "UTF8" | "") => { return Err(format!("line {}: FLAG {} is not supported (one-character flags only)", n + 1, at(1))); } "FLAG" => {} - directive @ ("FORBIDDENWORD" | "NOSUGGEST") => { - let mut it = at(1).chars().filter(|&c| c != EMOJI_SELECTOR); - match (it.next(), it.next()) { - (Some(c), None) if !c.is_whitespace() => { - if directive == "FORBIDDENWORD" { - r.forbidden = Some(c); - } else { - r.no_suggest = Some(c); - } - } - _ => return Err(format!("line {}: {directive} needs one flag character", n + 1)), - } - } + "FORBIDDENWORD" => r.forbidden = Some(one_flag(at(1), dir, n)?), + "NOSUGGEST" => r.no_suggest = Some(one_flag(at(1), dir, n)?), "TRY" => r.try_chars = cap_try(at(1)), "KEY" => r.key = at(1).to_string(), "MAP" => { @@ -383,6 +505,19 @@ fn parse_aff(aff: &str) -> Result { _ => {} } } + // Aliases may be declared after the rules that use them; resolve once all are read. + let resolve = |rules: &mut Vec, r: &Rules| -> Result<(), String> { + for rule in rules.iter_mut() { + rule.4 = r.flags(&rule.4)?.to_string(); + rule.2 = r.strip_ignored(&rule.2); + rule.3 = r.strip_ignored(&rule.3); + } + Ok(()) + }; + let (mut pfx, mut sfx) = (std::mem::take(&mut r.prefixes), std::mem::take(&mut r.suffixes)); + resolve(&mut pfx, &r)?; + resolve(&mut sfx, &r)?; + (r.prefixes, r.suffixes) = (pfx, sfx); Ok(r) } @@ -397,7 +532,7 @@ impl AffixDict { let rules = parse_aff(aff)?; let mut stems: Vec<(String, String)> = Vec::new(); for (n, line) in dic.lines().enumerate() { - let Some(tok) = line.split_whitespace().next() else { continue }; + let Some(tok) = split_fields(line).next() else { continue }; if n == 0 && tok.parse::().is_ok() { continue; } @@ -420,9 +555,11 @@ impl AffixDict { word.push(c); } } + let word = rules.strip_ignored(&word); if word.is_empty() || word.len() > MAX_WORD_BYTES || word.contains(['\n', '/']) { continue; } + let flags = rules.flags(&flags).map_err(|e| format!(".dic line {}: {e}", n + 1))?.to_string(); stems.push((word, flags)); } AffixDict::build(rules, stems, comments.iter().map(ToString::to_string).collect()) @@ -434,11 +571,12 @@ impl AffixDict { flag_chars.extend(stems.iter().flat_map(|s| s.1.chars())); flag_chars.sort_unstable(); flag_chars.dedup(); - if flag_chars.len() > 128 { - return Err(format!("{} distinct flags; at most 128 are supported", flag_chars.len())); + if flag_chars.len() > MAX_FLAGS { + return Err(format!("{} distinct flags; at most {MAX_FLAGS} are supported", flag_chars.len())); } - let bit_of = |c: char| flag_chars.iter().position(|&x| x == c).map_or(0, |i| 1u128 << i); - let forbidden = rules.forbidden.map_or(0, bit_of); + let bit_of = |c: char| flag_chars.iter().position(|&x| x == c).map_or(Mask::EMPTY, Mask::bit); + let forbidden = rules.forbidden.map_or(Mask::EMPTY, bit_of); + let keepcase = rules.keepcase.map_or(Mask::EMPTY, bit_of); // Stems sorted by bytes; homonyms (the same word twice) are merged. stems.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes())); let mut d = AffixDict { @@ -446,7 +584,10 @@ impl AffixDict { offs: Vec::with_capacity(stems.len() + 1), flag_chars: flag_chars.clone(), forbidden, - no_suggest: rules.no_suggest.map_or(0, bit_of), + no_suggest: rules.no_suggest.map_or(Mask::EMPTY, bit_of), + keepcase, + iconv: rules.iconv, + ignore: rules.ignore.into_boxed_slice(), try_chars: rules.try_chars, key: rules.key, map: rules.map, @@ -455,16 +596,16 @@ impl AffixDict { ..AffixDict::default() }; d.offs.push(0); - let mut set_ids: HashMap = HashMap::new(); + let mut set_ids: HashMap = HashMap::new(); let mut i = 0; while i < stems.len() { let Some((word, _)) = stems.get(i) else { break }; - let mut mask = 0u128; + let mut mask = Mask::EMPTY; let mut j = i; while let Some((w, f)) = stems.get(j) && w == word { - mask |= f.chars().map(bit_of).fold(0, |a, b| a | b); + mask |= f.chars().map(bit_of).fold(Mask::EMPTY, |a, b| a | b); j += 1; } let next_id = u16::try_from(d.sets.len()).map_err(|_| "too many distinct flag sets".to_string())?; @@ -478,7 +619,7 @@ impl AffixDict { i = j; } let mk = |(flag, cross, strip, add, cont, cond): RawRule| -> Result { - let cont = cont.chars().fold(0u128, |m, c| m | bit_of(c)); + let cont = cont.chars().fold(Mask::EMPTY, |m, c| m | bit_of(c)); Ok(Rule { bit: bit_of(flag), flag, cross, strip: strip.into(), add: add.into(), cont, cond: parse_cond(&cond)?, cond_text: cond.into() }) }; d.prefixes = rules.prefixes.into_iter().map(mk).collect::>()?; @@ -493,7 +634,7 @@ impl AffixDict { // Each (appended, stripped) pair's group, found by hash rather than by scanning the // appended string's groups: a dictionary with many strips per suffix stays linear. let mut group_of: FxMap<(&str, &str), usize> = FxMap::default(); - let mut by_flag: Vec> = vec![Vec::new(); 128]; + let mut by_flag: Vec> = vec![Vec::new(); MAX_FLAGS]; for (i, r) in self.suffixes.iter().enumerate() { let groups = idx.entry(r.add.clone()).or_default(); let Ok(i) = u32::try_from(i) else { break }; @@ -504,9 +645,7 @@ impl AffixDict { groups.push(SfxGroup { strip: r.strip.clone(), rules: vec![i] }); } } - if r.bit != 0 - && let Some(list) = by_flag.get_mut(r.bit.trailing_zeros() as usize) - { + if let Some(list) = r.bit.first().and_then(|b| by_flag.get_mut(b)) { list.push(i); } } @@ -551,12 +690,12 @@ impl AffixDict { } /// The flag characters of `mask`, in flag order (how the compact format stores flags). - fn flag_chars_for(&self, mask: u128) -> String { - self.flag_chars.iter().enumerate().filter(|&(i, _)| mask & (1u128 << i) != 0).map(|(_, c)| c).collect() + fn flag_chars_for(&self, mask: Mask) -> String { + self.flag_chars.iter().enumerate().filter(|&(i, _)| mask.meets(Mask::bit(i))).map(|(_, c)| c).collect() } /// The character of `mask` when it holds exactly one flag. - fn flag_char(&self, mask: u128) -> Option { + fn flag_char(&self, mask: Mask) -> Option { let s = self.flag_chars_for(mask); let mut it = s.chars(); match (it.next(), it.next()) { @@ -589,11 +728,12 @@ impl AffixDict { } /// Flag mask of a stem, if `w` is one. - fn stem_flags(&self, w: &str) -> Option { - self.stem_flags_excluding(w, self.forbidden) + fn stem_flags(&self, w: &str) -> Option { + self.stem_flags_except(w, Mask::EMPTY) } - fn stem_flags_excluding(&self, w: &str, excluded: u128) -> Option { + /// Flag mask of a stem, if `w` is one and carries none of the flags in `deny`. + fn stem_flags_except(&self, w: &str, deny: Mask) -> Option { if self.table.is_empty() { return None; } @@ -607,8 +747,9 @@ impl AffixDict { if self.word(t as usize) == Some(w) { let id = *self.flags.get(t as usize)?; let mask = self.sets.get(usize::from(id)).copied(); - // All derivations use this lookup: exclusions apply to stems and their forms. - return mask.filter(|m| m & excluded == 0); + // A stem carrying FORBIDDENWORD is listed but is not a word: no direct hit, + // and (this is the only stem lookup) no affixed form is built on it either. + return mask.filter(|m| !m.meets(self.forbidden | deny)); } slot = (slot + 1) & mask; } @@ -621,8 +762,8 @@ impl AffixDict { /// The rules of a group that could lead to `w` are merged into one mask of their flags and /// followed once, not once per rule: the answer is the same, but thousands of rules of one /// flag cost one walk, not one each (#3613). - fn form_has_flag(&self, w: &str, flags: u128, depth: u8, excluded: u128) -> bool { - if self.stem_flags_excluding(w, excluded).is_some_and(|f| f & flags != 0) { + fn form_has_flag(&self, w: &str, flags: Mask, depth: u8, deny: Mask) -> bool { + if self.stem_flags_except(w, deny).is_some_and(|f| f.meets(flags)) { return true; } if depth == 0 { @@ -641,9 +782,9 @@ impl AffixDict { .rules .iter() .filter_map(|&i| self.suffixes.get(i as usize)) - .filter(|r| r.cont & flags != 0 && r.cond_matches(&root, true)) - .fold(0u128, |m, r| m | r.bit); - if inner != 0 && self.form_has_flag(&root, inner, depth - 1, excluded) { + .filter(|r| r.cont.meets(flags) && r.cond_matches(&root, true)) + .fold(Mask::EMPTY, |m, r| m | r.bit); + if !inner.is_empty() && self.form_has_flag(&root, inner, depth - 1, deny) { return true; } } @@ -651,21 +792,50 @@ impl AffixDict { false } - /// Is `w` exactly (case and all) a stem or an affixed form of one? + /// Is `w` exactly (case and all) a stem or an affixed form of one? `ICONV` and `IGNORE` + /// apply first. pub fn contains(&self, w: &str) -> bool { - self.contains_excluding(w, self.forbidden) + self.contains_except(&self.normalize(w), Mask::EMPTY) } /// Exactly spelled dictionary words eligible for suggestions (NOSUGGEST stems excluded). pub fn contains_suggestible(&self, w: &str) -> bool { - self.contains_excluding(w, self.forbidden | self.no_suggest) + self.contains_except(&self.normalize(w), self.no_suggest) } - fn contains_excluding(&self, w: &str, excluded: u128) -> bool { + /// `w` after the `ICONV` conversions (at each position the longest match wins) and without + /// the `IGNORE` characters. + fn normalize<'a>(&self, w: &'a str) -> std::borrow::Cow<'a, str> { + if self.iconv.is_empty() && self.ignore.is_empty() { + return std::borrow::Cow::Borrowed(w); + } + let mut out = String::with_capacity(w.len()); + let mut rest = w; + while let Some(c) = rest.chars().next() { + let hit = self.iconv.iter().filter(|(from, _)| !from.is_empty() && rest.starts_with(from.as_str())).max_by_key(|(from, _)| from.len()); + match hit { + Some((from, to)) => { + out.extend(to.chars().filter(|c| !self.ignore.contains(c))); + rest = rest.get(from.len()..).unwrap_or_default(); + } + None => { + if !self.ignore.contains(&c) { + out.push(c); + } + rest = rest.get(c.len_utf8()..).unwrap_or_default(); + } + } + } + std::borrow::Cow::Owned(out) + } + + /// [`AffixDict::contains`] on normalized input, where stems carrying a flag in `deny` don't + /// count (case variants of `KEEPCASE` stems). + fn contains_except(&self, w: &str, deny: Mask) -> bool { if w.is_empty() || w.len() > MAX_WORD_BYTES { return false; } - if self.stem_flags_excluding(w, excluded).is_some() || self.suffixed(w, 0, excluded) { + if self.stem_flags_except(w, deny).is_some() || self.suffixed(w, Mask::EMPTY, deny) { return true; } let mut root = String::with_capacity(w.len() + 8); @@ -680,13 +850,13 @@ impl AffixDict { if !r.cond_matches(&root, false) { continue; } - if self.stem_flags_excluding(&root, excluded).is_some_and(|f| f & r.bit != 0) { + if self.stem_flags_except(&root, deny).is_some_and(|f| f.meets(r.bit)) { return true; } - if self.form_has_flag(&root, r.bit, INNER_CHAIN, excluded) { + if self.form_has_flag(&root, r.bit, INNER_CHAIN, deny) { return true; } - if r.cross && self.suffixed(&root, r.bit, excluded) { + if r.cross && self.suffixed(&root, r.bit, deny) { return true; } } @@ -695,7 +865,7 @@ impl AffixDict { /// Is `w` a stem with one suffix? With `need` set (a prefix was taken off), the stem must /// carry those flags too and the suffix must allow cross products. - fn suffixed(&self, w: &str, need: u128, excluded: u128) -> bool { + fn suffixed(&self, w: &str, need: Mask, deny: Mask) -> bool { let mut root = String::with_capacity(w.len() + 8); // Each split point from the end: the appended string is `w[b..]`, at most the longest // suffix, and something must stay before it. @@ -710,18 +880,18 @@ impl AffixDict { let rules = || g.rules.iter().filter_map(|&i| self.suffixes.get(i as usize)); // The stem path: `root` is a stem carrying the rule's flag (and the prefix's, // when a cross product is being checked). - if let Some(f) = self.stem_flags_excluding(&root, excluded) - && f & need == need - && rules().any(|r| f & r.bit != 0 && (need == 0 || r.cross) && r.cond_matches(&root, true)) + if let Some(f) = self.stem_flags_except(&root, deny) + && f.covers(need) + && rules().any(|r| f.meets(r.bit) && (need.is_empty() || r.cross) && r.cond_matches(&root, true)) { return true; } // Continuation classes: `root` need not be a stem carrying this rule's flag — // it may be a form that gained the flag from another rule's continuation. The // matching rules' flags are looked for together, in one walk. - if need == 0 { - let flags = rules().filter(|r| r.cond_matches(&root, true)).fold(0u128, |m, r| m | r.bit); - if flags != 0 && self.form_has_flag(&root, flags, INNER_CHAIN, excluded) { + if need.is_empty() { + let flags = rules().filter(|r| r.cond_matches(&root, true)).fold(Mask::EMPTY, |m, r| m | r.bit); + if !flags.is_empty() && self.form_has_flag(&root, flags, INNER_CHAIN, deny) { return true; } } @@ -732,26 +902,29 @@ impl AffixDict { /// Is `w` correct, Hunspell style: as written, or — for a capitalized word or one in capitals /// — in lower case (and capitalized), since a sentence may start with any word and headings - /// may be in capitals. Lower-case input never matches a capitalized stem (`warszawa`). + /// may be in capitals. Lower-case input never matches a capitalized stem (`warszawa`), and a + /// `KEEPCASE` stem matches only as written. pub fn check(&self, w: &str) -> bool { - self.check_excluding(w, self.forbidden) + self.check_except(w, Mask::EMPTY) } /// Whether a suggestion is correct in its displayed case and has an eligible stem. pub fn can_suggest(&self, w: &str) -> bool { - self.check_excluding(w, self.forbidden | self.no_suggest) + self.check_except(w, self.no_suggest) } - fn check_excluding(&self, w: &str, excluded: u128) -> bool { - if self.contains_excluding(w, excluded) { + fn check_except(&self, w: &str, deny: Mask) -> bool { + let w = self.normalize(w); + if self.contains_except(&w, deny) { return true; } - match casing(w) { + let deny = deny | self.keepcase; + match casing(&w) { Casing::Lower | Casing::Mixed => false, - Casing::Initial => self.contains_excluding(&w.to_lowercase(), excluded), + Casing::Initial => self.contains_except(&w.to_lowercase(), deny), Casing::Upper => { let lower = w.to_lowercase(); - self.contains_excluding(&capitalize(&lower), excluded) || self.contains_excluding(&lower, excluded) + self.contains_except(&capitalize(&lower), deny) || self.contains_except(&lower, deny) } } } @@ -766,11 +939,11 @@ impl AffixDict { let mut cross_sfx = Vec::new(); // Suffix forms queued with the continuation classes they carry, for chained expansion; // each (form, classes) pair is expanded once. - let mut chain: Vec<(String, u128)> = Vec::new(); - let mut queued: std::collections::HashSet<(String, u128)> = std::collections::HashSet::new(); + let mut chain: Vec<(String, Mask)> = Vec::new(); + let mut queued: std::collections::HashSet<(String, Mask)> = std::collections::HashSet::new(); let mut work = 0usize; for r in &self.suffixes { - if f & r.bit == 0 || !r.cond_matches(stem, true) { + if !f.meets(r.bit) || !r.cond_matches(stem, true) { continue; } let Some(base) = stem.strip_suffix(&*r.strip) else { continue }; @@ -778,7 +951,7 @@ impl AffixDict { if r.cross { cross_sfx.push(form.clone()); } - if r.cont != 0 && queued.insert((form.clone(), r.cont)) { + if !r.cont.is_empty() && queued.insert((form.clone(), r.cont)) { chain.push((form.clone(), r.cont)); } out.push(form); @@ -804,7 +977,7 @@ impl AffixDict { } let Some(base) = form.strip_suffix(&*r.strip) else { continue }; let next = format!("{base}{}", r.add); - if r.cont != 0 && queued.insert((next.clone(), r.cont)) { + if !r.cont.is_empty() && queued.insert((next.clone(), r.cont)) { chain.push((next.clone(), r.cont)); } out.push(next); @@ -815,7 +988,7 @@ impl AffixDict { } } for r in &self.prefixes { - if f & r.bit == 0 || !r.cond_matches(stem, false) { + if !f.meets(r.bit) || !r.cond_matches(stem, false) { continue; } let Some(rest) = stem.strip_prefix(&*r.strip) else { continue }; @@ -839,11 +1012,11 @@ impl AffixDict { } /// The suffix rules carrying any flag of `mask`, in rule order. - fn suffixes_with(&self, mask: u128) -> Vec { + fn suffixes_with(&self, mask: Mask) -> Vec { let mut rules: Vec = Vec::new(); let mut lists = 0; for (bit, list) in self.sfx_by_flag.iter().enumerate() { - if bit < 128 && mask & (1u128 << bit) != 0 && !list.is_empty() { + if mask.meets(Mask::bit(bit)) && !list.is_empty() { rules.extend_from_slice(list); lists += 1; } @@ -904,13 +1077,19 @@ impl AffixDict { for (a, b) in &self.rep { out.push_str(&format!("REP {} {}\n", a.replace(' ', "_"), b.replace(' ', "_"))); } - for (directive, mask) in [("FORBIDDENWORD", self.forbidden), ("NOSUGGEST", self.no_suggest)] { - if mask != 0 + for (directive, mask) in [("FORBIDDENWORD", self.forbidden), ("NOSUGGEST", self.no_suggest), ("KEEPCASE", self.keepcase)] { + if !mask.is_empty() && let Some(c) = self.flag_char(mask) { out.push_str(&format!("{directive} {c}\n")); } } + for (a, b) in &self.iconv { + out.push_str(&format!("ICONV {a} {b}\n")); + } + if !self.ignore.is_empty() { + out.push_str(&format!("IGNORE {}\n", self.ignore.iter().collect::())); + } let z = |s: &str| if s.is_empty() { "0".to_string() } else { s.to_string() }; for (kind, rules) in [("PFX", &self.prefixes), ("SFX", &self.suffixes)] { for r in rules { @@ -934,8 +1113,8 @@ impl AffixDict { let p = prev.iter().zip(b).take_while(|(x, y)| x == y).count().min(255); raw.push(p as u8); raw.extend_from_slice(b.get(p..).unwrap_or_default()); - let mask = self.flags.get(i).and_then(|&id| self.sets.get(usize::from(id))).copied().unwrap_or(0); - if mask != 0 { + let mask = self.flags.get(i).and_then(|&id| self.sets.get(usize::from(id))).copied().unwrap_or_default(); + if !mask.is_empty() { raw.push(b'/'); raw.extend_from_slice(self.flag_chars_for(mask).as_bytes()); } @@ -962,26 +1141,23 @@ impl AffixDict { comments.push(c.to_string()); continue; } - let f: Vec<&str> = line.split_whitespace().collect(); + let f: Vec<&str> = split_fields(line).collect(); let at = |k: usize| f.get(k).copied().unwrap_or(""); match at(0) { "TRY" => rules.try_chars = cap_try(at(1)), "KEY" => rules.key = at(1).to_string(), - "MAP" => rules.map.push(at(1).chars().collect()), - "REP" => rules.rep.push((at(1).replace('_', " "), at(2).replace('_', " "))), - directive @ ("FORBIDDENWORD" | "NOSUGGEST") => { - let mut it = at(1).chars(); - match (it.next(), it.next()) { - (Some(c), None) if !c.is_whitespace() => { - if directive == "FORBIDDENWORD" { - rules.forbidden = Some(c); - } else { - rules.no_suggest = Some(c); - } - } - _ => return Err(format!("affix dictionary: bad {directive} in `{line}`")), + directive @ ("FORBIDDENWORD" | "NOSUGGEST" | "KEEPCASE") => { + let c = one_flag(at(1), directive, 0).map_err(|_| format!("affix dictionary: bad {directive} in `{line}`"))?; + match directive { + "FORBIDDENWORD" => rules.forbidden = Some(c), + "NOSUGGEST" => rules.no_suggest = Some(c), + _ => rules.keepcase = Some(c), } } + "ICONV" if f.len() == 3 => rules.iconv.push((at(1).to_string(), at(2).to_string())), + "IGNORE" => rules.ignore = at(1).chars().collect(), + "MAP" => rules.map.push(at(1).chars().collect()), + "REP" => rules.rep.push((at(1).replace('_', " "), at(2).replace('_', " "))), kind @ ("PFX" | "SFX") => { let mut fc = at(1).chars(); let (Some(flag), None) = (fc.next(), fc.next()) else { return Err(format!("affix dictionary: bad flag in `{line}`")) }; @@ -1223,7 +1399,7 @@ pub(crate) mod tests { #[test] fn more_than_64_distinct_flags_load() { // VERO pt_BR uses 106 distinct flags (FLAG UTF-8). Build one with every basic Latin - // letter, the digits and eight accented letters: 70 flags, 64 < n <= 128. + // letter, the digits and eight accented letters: 70 flags, more than 64. let mut flags = String::new(); let mut aff = String::from("SET UTF-8\nFLAG UTF-8\n"); let chars: Vec = ('a'..='z').chain('A'..='Z').chain('0'..='9').chain(['á', 'é', 'í', 'ó', 'ú', 'ç', 'ã', 'õ']).collect(); @@ -1239,6 +1415,24 @@ pub(crate) mod tests { assert!(!d.contains("verboß"), "ß is not a flag and not a stem"); } + #[test] + fn more_than_128_distinct_flags_load() { + // The Hungarian dictionary uses 158 single-byte flags: 200 flags here, U+0080 up. + let flags: String = (0x80u32..0x80 + 200).filter_map(char::from_u32).collect(); + let mut aff = String::from("SET UTF-8\n"); + for (i, c) in flags.chars().enumerate() { + aff.push_str(&format!("SFX {c} N 1\nSFX {c} 0 x{i} .\n")); + } + let half: String = flags.chars().step_by(2).collect(); + let d = AffixDict::from_hunspell(&aff, &format!("1\nszó/{half}\n"), &[]).unwrap(); + assert!(d.contains("szóx0") && d.contains("szóx198") && d.contains("szóx130")); + assert!(!d.contains("szóx1") && !d.contains("szóx199") && !d.contains("szóx131")); + let back = AffixDict::from_bytes(&d.to_bytes()).unwrap(); + assert!(back.contains("szóx198") && !back.contains("szóx199")); + let too_many: String = (0x100u32..0x100 + 257).filter_map(char::from_u32).collect(); + assert!(AffixDict::from_hunspell("SET UTF-8\n", &format!("1\nszó/{too_many}\n"), &[]).is_err()); + } + #[test] fn forbidden_words_are_rejected() { // VERO pt_BR marks spellings it must reject (flag ý under FLAG UTF-8) as stems carrying @@ -1364,6 +1558,52 @@ pub(crate) mod tests { assert!(d.contains("kot") && d.contains("koty")); } + #[test] + fn flag_aliases_stand_for_flag_sets() { + // `AF`: `/1` in the `.dic` and in a continuation class means the first alias's flags. + // Morphological aliases (`AM`) and the fields after a stem are skipped. + let aff = "SET UTF-8\nAF 2\nAF ab # 1\nAF c # 2\nAM 1\nAM po:noun\nSFX a Y 1\nSFX a 0 ok/2 . 1\nSFX b Y 1\nSFX b 0 at . 1\nSFX c Y 1\nSFX c 0 ban . 1\n"; + let d = AffixDict::from_hunspell(aff, "1\nház/1\t1\n", &[]).unwrap(); + assert!(d.contains("ház") && d.contains("házok") && d.contains("házat") && d.contains("házokban")); + assert!(!d.contains("házban"), "the stem has a and b, not c"); + assert!(AffixDict::from_hunspell(aff, "1\nház/3\n", &[]).is_err(), "alias 3 is not defined"); + let back = AffixDict::from_bytes(&d.to_bytes()).unwrap(); + assert!(back.contains("házokban") && !back.contains("házban"), "aliases are expanded in the compact format"); + } + + #[test] + fn iconv_and_ignore_normalize_input() { + // Ligatures read as their letters; `IGNORE` characters vanish from input and stems. + let aff = "SET UTF-8\nICONV 2\nICONV fi fi\nICONV ffl ffl\nIGNORE ()\nSFX a Y 1\nSFX a 0 s .\n"; + let d = AffixDict::from_hunspell(aff, "2\nfine/a\nwaf(fle)/a\n", &[]).unwrap(); + assert!(d.check("fine") && d.check("fines") && d.check("Fine") && d.contains("fine")); + assert!(d.check("waffle") && d.check("waffles") && d.check("wa(ffle)")); + assert!(!d.check("finer")); + let back = AffixDict::from_bytes(&d.to_bytes()).unwrap(); + assert!(back.check("fines") && back.check("waffle") && back.check("waf(fle)")); + } + + #[test] + fn keepcase_stems_match_only_as_written() { + let aff = "SET UTF-8\nKEEPCASE k\nSFX a Y 1\nSFX a 0 s .\n"; + let d = AffixDict::from_hunspell(aff, "3\npH/k\nkm/ka\nkot/a\n", &[]).unwrap(); + assert!(d.check("pH") && d.check("km") && d.check("kms")); + assert!(!d.check("PH") && !d.check("Km") && !d.check("KM") && !d.check("Kms")); + assert!(d.check("Kot") && d.check("KOTS"), "other stems still follow the sentence and heading case"); + let back = AffixDict::from_bytes(&d.to_bytes()).unwrap(); + assert!(back.check("km") && !back.check("Km")); + } + + #[test] + fn fields_split_on_spaces_and_tabs_only() { + // A flag byte read as U+00A0 (no-break space) or U+0085 is a flag, not a separator. + let aff = "SET UTF-8\nSFX \u{a0} Y 1\nSFX \u{a0} 0 y .\nSFX \u{85} Y 1\nSFX \u{85} 0 em .\n"; + let d = AffixDict::from_hunspell(aff, "1\nkot/\u{a0}\u{85}\n", &[]).unwrap(); + assert!(d.contains("koty") && d.contains("kotem")); + let back = AffixDict::from_bytes(&d.to_bytes()).unwrap(); + assert!(back.contains("koty") && back.contains("kotem")); + } + #[test] fn unsupported_and_broken_input_is_an_error() { assert!(AffixDict::from_hunspell("FLAG long\n", DIC, &[]).is_err()); diff --git a/crates/proof/src/hunspell.rs b/crates/proof/src/hunspell.rs index 3449c8cab7..d9937a14c5 100644 --- a/crates/proof/src/hunspell.rs +++ b/crates/proof/src/hunspell.rs @@ -218,13 +218,27 @@ fn decode_high(bytes: &[u8], high: &str) -> String { /// (UTF-8 when there is none; ISO 8859-2 is the other one Polish dictionaries use, ISO 8859-1 the /// one the original Portuguese VERO uses, ISO 8859-9 the one older Turkish dictionaries use, KOI8-R /// and Windows-1251 the ones older Russian dictionaries use). +/// +/// Under a UTF-8 `SET` without `FLAG UTF-8`, flags are still single bytes, as in Hunspell: the +/// Hungarian dictionary writes its affix flags and flag aliases as bytes above 0x7F that aren't +/// UTF-8. Those fields are read byte by byte (each byte becomes U+0000..U+00FF, one flag per +/// byte); the words, affixes and conditions around them are UTF-8. pub fn decode(aff: &[u8], dic: &[u8]) -> Result<(String, String), String> { let head = String::from_utf8_lossy(aff.get(..aff.len().min(8192)).unwrap_or_default()).to_string(); - let set = head.lines().find_map(|l| l.trim_start_matches('\u{feff}').strip_prefix("SET ")).map(str::trim).unwrap_or("UTF-8").to_ascii_uppercase(); + let directive = |name: &str| { + head.lines().find_map(|l| { + let l = l.trim_start_matches('\u{feff}'); + l.strip_prefix(name).filter(|rest| rest.starts_with([' ', '\t'])).map(|rest| rest.trim().to_ascii_uppercase()) + }) + }; + let set = directive("SET").unwrap_or_else(|| "UTF-8".into()); match set.as_str() { "UTF-8" | "UTF8" => { - let text = |b: &[u8], what: &str| String::from_utf8(b.to_vec()).map_err(|e| format!("{what}: not UTF-8: {e}")); - Ok((text(aff, ".aff")?, text(dic, ".dic")?)) + if matches!(directive("FLAG").as_deref(), Some("UTF-8" | "UTF8")) { + let text = |b: &[u8], what: &str| String::from_utf8(b.to_vec()).map_err(|e| format!("{what}: not UTF-8: {e}")); + return Ok((text(aff, ".aff")?, text(dic, ".dic")?)); + } + Ok((decode_utf8_aff(aff)?, decode_utf8_dic(dic)?)) } "ISO8859-1" | "ISO-8859-1" => Ok((decode_latin1(aff), decode_latin1(dic))), "ISO8859-2" | "ISO-8859-2" => Ok((decode_latin2(aff), decode_latin2(dic))), @@ -235,6 +249,153 @@ pub fn decode(aff: &[u8], dic: &[u8]) -> Result<(String, String), String> { } } +/// Directives whose one argument is a flag. +const FLAG_DIRECTIVES: &[&str] = &[ + "CIRCUMFIX", + "COMPOUNDBEGIN", + "COMPOUNDEND", + "COMPOUNDFLAG", + "COMPOUNDFORBIDFLAG", + "COMPOUNDLAST", + "COMPOUNDFIRST", + "COMPOUNDMIDDLE", + "COMPOUNDPERMITFLAG", + "COMPOUNDROOT", + "FORBIDDENWORD", + "FORCEUCASE", + "GENERATE", + "HU_KOTOHANGZO", + "KEEPCASE", + "LEMMA_PRESENT", + "NEEDAFFIX", + "NOSUGGEST", + "ONLYINCOMPOUND", + "ONLYROOT", + "PSEUDOROOT", + "SUBSTANDARD", + "WARN", +]; + +/// Directives that only describe the dictionary; a non-UTF-8 byte in them (the Hungarian +/// dictionary's `NAME` is ISO 8859-2) is replaced instead of failing the load. +const METADATA_DIRECTIVES: &[&str] = &["NAME", "HOME", "VERSION", "LANG", "AM"]; + +/// One byte per char: a flag field read as single-byte flags. +fn bytes_as_flags(b: &[u8]) -> String { + b.iter().map(|&x| char::from(x)).collect() +} + +/// `text/flags`: the text as UTF-8 and the flags byte by byte (`\/` is a literal slash). +fn text_and_flags(field: &[u8], what: &str) -> Result { + let mut slash = None; + let mut i = 0; + while let Some(&b) = field.get(i) { + if b == b'\\' && field.get(i + 1) == Some(&b'/') { + i += 2; + continue; + } + if b == b'/' { + slash = Some(i); + break; + } + i += 1; + } + let (text, flags) = match slash { + Some(k) => (field.get(..k).unwrap_or_default(), field.get(k..).unwrap_or_default()), + None => (field, &[][..]), + }; + let mut out = std::str::from_utf8(text).map_err(|e| format!("{what}: not UTF-8: {e}"))?.to_string(); + out.push_str(&bytes_as_flags(flags)); + Ok(out) +} + +/// Split a line into fields on spaces and tabs, keeping the separators (so a line rebuilt from +/// its parts is the original). +fn fields(line: &[u8]) -> Vec<&[u8]> { + let mut out = Vec::new(); + let mut start = 0; + let mut in_sep = line.first().is_some_and(|b| matches!(b, b' ' | b'\t')); + for (i, b) in line.iter().enumerate() { + let sep = matches!(b, b' ' | b'\t'); + if sep != in_sep { + out.push(line.get(start..i).unwrap_or_default()); + start = i; + in_sep = sep; + } + } + out.push(line.get(start..).unwrap_or_default()); + out +} + +/// A UTF-8 `.aff` with single-byte flags (see [`decode`]). +fn decode_utf8_aff(aff: &[u8]) -> Result { + let mut out = String::with_capacity(aff.len()); + for (n, line) in aff.split(|&b| b == b'\n').enumerate() { + if n > 0 { + out.push('\n'); + } + if let Ok(s) = std::str::from_utf8(line) + && s.is_ascii() + { + out.push_str(s); + continue; + } + let parts = fields(line); + // Field index among the non-separator parts. + let words: Vec = + parts.iter().enumerate().filter(|(_, p)| !p.first().is_some_and(|b| matches!(b, b' ' | b'\t'))).map(|(i, _)| i).collect(); + let dir = words + .first() + .and_then(|&i| parts.get(i)) + .map(|p| String::from_utf8_lossy(p).trim_start_matches('\u{feff}').to_string()) + .unwrap_or_default(); + let what = format!(".aff line {}", n + 1); + if dir.starts_with('#') || METADATA_DIRECTIVES.contains(&dir.as_str()) { + out.push_str(&String::from_utf8_lossy(line)); + continue; + } + // A trailing `# comment` (the Hungarian `AF` lines number themselves) is never a word or + // a flag; from there on the line is kept as text. + let comment_at = words.iter().skip(1).position(|&w| parts.get(w).is_some_and(|f| f.first() == Some(&b'#'))).map(|k| k + 1); + for (i, p) in parts.iter().enumerate() { + let field = words.iter().position(|&w| w == i); + let in_comment = comment_at.is_some_and(|c| words.get(c).is_some_and(|&w| i >= w)); + let decoded = match (dir.as_str(), field) { + _ if in_comment => String::from_utf8_lossy(p).into_owned(), + ("AF" | "PFX" | "SFX" | "COMPOUNDRULE", Some(1)) => bytes_as_flags(p), + ("PFX" | "SFX", Some(3)) | ("CHECKCOMPOUNDPATTERN", Some(1 | 2)) => text_and_flags(p, &what)?, + (d, Some(1)) if FLAG_DIRECTIVES.contains(&d) => bytes_as_flags(p), + _ => std::str::from_utf8(p).map_err(|e| format!("{what}: not UTF-8: {e}"))?.to_string(), + }; + out.push_str(&decoded); + } + } + Ok(out) +} + +/// A UTF-8 `.dic` with single-byte flags: the word is UTF-8, the flags after `/` are bytes. +fn decode_utf8_dic(dic: &[u8]) -> Result { + let mut out = String::with_capacity(dic.len()); + for (n, line) in dic.split(|&b| b == b'\n').enumerate() { + if n > 0 { + out.push('\n'); + } + if let Ok(s) = std::str::from_utf8(line) + && s.split_once('/').is_none_or(|(_, flags)| flags.is_ascii()) + { + out.push_str(s); + continue; + } + let what = format!(".dic line {}", n + 1); + // The first field is `word/flags`; morphological fields after it are UTF-8 text. + let end = line.iter().position(|b| matches!(b, b' ' | b'\t')).unwrap_or(line.len()); + out.push_str(&text_and_flags(line.get(..end).unwrap_or_default(), &what)?); + let rest = line.get(end..).unwrap_or_default(); + out.push_str(std::str::from_utf8(rest).map_err(|e| format!("{what}: not UTF-8: {e}"))?); + } + Ok(out) +} + #[cfg(test)] mod tests { use super::*; @@ -280,6 +441,25 @@ mod tests { assert!(decode(b"SET ISO-8859-1\n", b"\xe7").is_ok()); } + #[test] + fn utf8_dictionaries_keep_single_byte_flags() { + // The Hungarian dictionary is `SET UTF-8` but writes flags as single bytes above 0x7F, + // which aren't UTF-8; each byte is one flag. Its `NAME` line is ISO 8859-2. + let aff = b"SET UTF-8\nNAME Magyar helyes\xedr\xe1si\nAF 1\nAF \xd5a # 1\nSFX \xd5 Y 1\nSFX \xd5 0 ok/\xe9 . 7\nSFX \xe9 Y 1\nSFX \xe9 0 ban .\nSFX a Y 1\nSFX a 0 at .\n"; + let dic = "2\nház/1\t1\nkert/\u{e9}\n".as_bytes(); + let (a, d) = decode(aff, dic).unwrap(); + assert!(a.contains("AF \u{d5}a # 1") && a.contains("SFX \u{d5} 0 ok/\u{e9} . 7")); + assert!(d.contains("kert/\u{c3}\u{a9}"), "a UTF-8 é in the flags is two one-byte flags"); + let dict = crate::affix::AffixDict::from_hunspell(&a, &d, &[]).unwrap(); + assert!(dict.check("ház") && dict.check("házok") && dict.check("házokban") && dict.check("házat")); + assert!(!dict.check("házban")); + // `FLAG UTF-8` keeps the whole file UTF-8, and words must still be UTF-8. + let (_, d) = decode(b"SET UTF-8\nFLAG UTF-8\n", "kert/\u{e9}\n".as_bytes()).unwrap(); + assert_eq!(d, "kert/\u{e9}\n"); + assert!(decode(b"SET UTF-8\nSFX a Y 1\nSFX a 0 \xe9 .\n", b"").is_err(), "an affix must be UTF-8"); + assert!(decode(b"SET UTF-8\n", b"1\nh\xe1z/a\n").is_err(), "a stem must be UTF-8"); + } + #[test] fn decodes_russian_single_byte_encodings() { // `ёлка` in KOI8-R (0xA3 0xCC 0xCB 0xC1) and in Windows-1251 (0xB8 0xEB 0xEA 0xE0). From 7ebfcf0ee2e8153a8e9fca67f9e4bb98901ac208 Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Sun, 18 Oct 2026 18:08:15 +0200 Subject: [PATCH 020/308] proof: .aff comments start after a directive's fields, .dic entries end where Hunspell ends them --- crates/proof/src/hunspell.rs | 79 ++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/crates/proof/src/hunspell.rs b/crates/proof/src/hunspell.rs index d9937a14c5..608e94777e 100644 --- a/crates/proof/src/hunspell.rs +++ b/crates/proof/src/hunspell.rs @@ -355,8 +355,11 @@ fn decode_utf8_aff(aff: &[u8]) -> Result { continue; } // A trailing `# comment` (the Hungarian `AF` lines number themselves) is never a word or - // a flag; from there on the line is kept as text. - let comment_at = words.iter().skip(1).position(|&w| parts.get(w).is_some_and(|f| f.first() == Some(&b'#'))).map(|k| k + 1); + // a flag; from there on the line is kept as text. It can only start after the directive's + // own fields, since a field such as an affix condition may itself begin with `#`. + let field_text = |k: usize| words.get(k).and_then(|&w| parts.get(w)).map(|p| String::from_utf8_lossy(p).into_owned()); + let first_free = comment_field(&dir, field_text); + let comment_at = words.iter().skip(first_free).position(|&w| parts.get(w).is_some_and(|f| f.first() == Some(&b'#'))).map(|k| k + first_free); for (i, p) in parts.iter().enumerate() { let field = words.iter().position(|&w| w == i); let in_comment = comment_at.is_some_and(|c| words.get(c).is_some_and(|&w| i >= w)); @@ -373,6 +376,43 @@ fn decode_utf8_aff(aff: &[u8]) -> Result { Ok(out) } +/// The first field of an `.aff` line where a trailing `# comment` may start: after the +/// directive's own fields. A table's count line (`REP 12`, `PFX A Y 3`) has fewer fields than its +/// rows (`REP a b`, `PFX A 0 x .`). `field(k)` is the line's `k`-th field. +fn comment_field(dir: &str, field: impl Fn(usize) -> Option) -> usize { + let number = |k: usize| field(k).is_some_and(|f| f.parse::().is_ok()); + match dir { + "PFX" | "SFX" if field(2).is_some_and(|f| f == "Y" || f == "N") && number(3) => 4, + "PFX" | "SFX" => 5, + "REP" | "ICONV" | "OCONV" | "PHONE" | "MAP" | "BREAK" | "AF" | "AM" | "COMPOUNDRULE" | "CHECKCOMPOUNDPATTERN" + if number(1) && field(2).is_none_or(|f| f.starts_with('#')) => + { + 2 + } + "REP" | "ICONV" | "OCONV" | "PHONE" => 3, + "CHECKCOMPOUNDPATTERN" => 4, + _ => 2, + } +} + +/// Where the `word/flags` entry of a `.dic` line ends, as Hunspell reads it: at a tab or at the +/// first morphological field (a space or tab, two characters and `:`, as in ` po:noun`), whichever +/// comes first, so an entry may hold spaces (`ad hoc`); with neither, at the first space. +pub(crate) fn dic_entry_end(line: &[u8]) -> usize { + let blank = |b: &u8| matches!(b, b' ' | b'\t'); + let morph = line.windows(4).position(|w| matches!(w, [a, b, c, b':'] if blank(a) && !blank(b) && !blank(c))); + let tab = line.iter().position(|&b| b == b'\t'); + let end = match (morph, tab) { + (Some(m), Some(t)) => m.min(t), + (m, t) => match m.or(t) { + Some(e) => e, + None => line.iter().position(|&b| b == b' ').unwrap_or(line.len()), + }, + }; + // Spaces before the morphological field aren't part of the entry. + line.get(..end).map_or(end, |e| e.iter().rposition(|b| !blank(b)).map_or(0, |k| k + 1)) +} + /// A UTF-8 `.dic` with single-byte flags: the word is UTF-8, the flags after `/` are bytes. fn decode_utf8_dic(dic: &[u8]) -> Result { let mut out = String::with_capacity(dic.len()); @@ -387,8 +427,9 @@ fn decode_utf8_dic(dic: &[u8]) -> Result { continue; } let what = format!(".dic line {}", n + 1); - // The first field is `word/flags`; morphological fields after it are UTF-8 text. - let end = line.iter().position(|b| matches!(b, b' ' | b'\t')).unwrap_or(line.len()); + // The first field is `word/flags` (it may hold spaces, see [`dic_entry_end`]); + // morphological fields after it are UTF-8 text. + let end = dic_entry_end(line); out.push_str(&text_and_flags(line.get(..end).unwrap_or_default(), &what)?); let rest = line.get(end..).unwrap_or_default(); out.push_str(std::str::from_utf8(rest).map_err(|e| format!("{what}: not UTF-8: {e}"))?); @@ -471,6 +512,36 @@ mod tests { assert_eq!(decode_high(&[0x98], CP1251_HIGH), "\u{fffd}"); } + #[test] + fn comments_start_after_the_directive_fields() { + let f = |line: &'static str| move |k: usize| line.split_whitespace().nth(k).map(str::to_string); + assert_eq!(comment_field("PFX", f("PFX A Y 3 # count")), 4); + assert_eq!(comment_field("SFX", f("SFX A 0 # .")), 5, "a rule's condition may be `#`"); + assert_eq!(comment_field("AF", f("AF 1234 # aliases")), 2); + assert_eq!(comment_field("AF", f("AF Aa # 1")), 2); + assert_eq!(comment_field("REP", f("REP 12")), 2); + assert_eq!(comment_field("REP", f("REP # x")), 3); + assert_eq!(comment_field("CHECKCOMPOUNDPATTERN", f("CHECKCOMPOUNDPATTERN a b c")), 4); + assert_eq!(comment_field("KEEPCASE", f("KEEPCASE k")), 2); + // A byte flag after an append text that begins with `#` stays a flag, not a comment. + let (a, _) = decode(b"SET UTF-8\nSFX \xd5 Y 1 # header\nSFX \xd5 0 #/\xe9 . # rule\n", b"").unwrap(); + assert!(a.contains("SFX \u{d5} 0 #/\u{e9} . # rule"), "{a}"); + } + + #[test] + fn dic_entries_end_where_hunspell_ends_them() { + let end = |l: &str| l.get(..dic_entry_end(l.as_bytes())).unwrap().to_string(); + assert_eq!(end("word/A po:noun\tts:x"), "word/A", "the morphological field comes before the tab"); + assert_eq!(end("ad hoc/B\tpo:adv"), "ad hoc/B", "a tab ends a phrase"); + assert_eq!(end("ad hoc/B po:adv"), "ad hoc/B"); + assert_eq!(end("word/A 123"), "word/A", "neither: the first space"); + assert_eq!(end("word"), "word"); + assert_eq!(end("a:b/A"), "a:b/A", "a colon inside the word isn't a field"); + // Single-byte flags of a phrase entry are read byte by byte, then the UTF-8 morphology. + let (_, d) = decode(b"SET UTF-8\n", b"1\nad hoc/\xe9\tpo:adv\n").unwrap(); + assert_eq!(d, "1\nad hoc/\u{e9}\tpo:adv\n"); + } + #[test] fn libreoffice_extension_dirs_follow_the_language() { // LibreOffice's IDs are per dictionary: Polish is `dict-pl`, Brazilian Portuguese From efa570a9bee6253f2b96583b11cba1e14163658b Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:53:01 +0200 Subject: [PATCH 021/308] proof: a one-flag field keeps its first byte, a directive reads only its first argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without FLAG UTF-8, Hunspell reads a field that names one flag (PFX/SFX, KEEPCASE, FORBIDDENWORD and the like) through decode_flag, which takes its first byte: SFX é is the flag 0xC3. Such a field no longer fails the load. SYLLABLENUM lists flags and is read byte by byte like AF. SET and FLAG read only their first argument, so FLAG UTF-8 with a trailing comment keeps Unicode flags. --- crates/proof/src/hunspell.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/crates/proof/src/hunspell.rs b/crates/proof/src/hunspell.rs index 608e94777e..934520bebd 100644 --- a/crates/proof/src/hunspell.rs +++ b/crates/proof/src/hunspell.rs @@ -228,7 +228,11 @@ pub fn decode(aff: &[u8], dic: &[u8]) -> Result<(String, String), String> { let directive = |name: &str| { head.lines().find_map(|l| { let l = l.trim_start_matches('\u{feff}'); - l.strip_prefix(name).filter(|rest| rest.starts_with([' ', '\t'])).map(|rest| rest.trim().to_ascii_uppercase()) + // Only the first argument counts, as in Hunspell: `FLAG UTF-8 # Unicode flags` is UTF-8. + l.strip_prefix(name) + .filter(|rest| rest.starts_with([' ', '\t'])) + .and_then(|rest| rest.split([' ', '\t']).find(|f| !f.is_empty())) + .map(str::to_ascii_uppercase) }) }; let set = directive("SET").unwrap_or_else(|| "UTF-8".into()); @@ -365,9 +369,12 @@ fn decode_utf8_aff(aff: &[u8]) -> Result { let in_comment = comment_at.is_some_and(|c| words.get(c).is_some_and(|&w| i >= w)); let decoded = match (dir.as_str(), field) { _ if in_comment => String::from_utf8_lossy(p).into_owned(), - ("AF" | "PFX" | "SFX" | "COMPOUNDRULE", Some(1)) => bytes_as_flags(p), + ("AF" | "COMPOUNDRULE" | "SYLLABLENUM", Some(1)) => bytes_as_flags(p), ("PFX" | "SFX", Some(3)) | ("CHECKCOMPOUNDPATTERN", Some(1 | 2)) => text_and_flags(p, &what)?, - (d, Some(1)) if FLAG_DIRECTIVES.contains(&d) => bytes_as_flags(p), + // A field that names one flag keeps its first byte, as Hunspell's `decode_flag` + // does: `SFX é` is the flag 0xC3, the first of the two flags `kert/é` carries. + ("PFX" | "SFX", Some(1)) => bytes_as_flags(p.get(..1).unwrap_or_default()), + (d, Some(1)) if FLAG_DIRECTIVES.contains(&d) => bytes_as_flags(p.get(..1).unwrap_or_default()), _ => std::str::from_utf8(p).map_err(|e| format!("{what}: not UTF-8: {e}"))?.to_string(), }; out.push_str(&decoded); @@ -501,6 +508,28 @@ mod tests { assert!(decode(b"SET UTF-8\n", b"1\nh\xe1z/a\n").is_err(), "a stem must be UTF-8"); } + #[test] + fn single_flag_fields_keep_their_first_byte() { + // Without `FLAG UTF-8`, a UTF-8 `é` (0xC3 0xA9) in a field that names one flag is the + // flag 0xC3; in a `.dic` flag list it is the two flags 0xC3 and 0xA9. + let aff = "SET UTF-8\nKEEPCASE é\nSFX é Y 1\nSFX é 0 ok .\nSYLLABLENUM é\n".as_bytes(); + let (a, d) = decode(aff, "2\nkert/é\nNATO/é\n".as_bytes()).unwrap(); + assert!(a.contains("KEEPCASE \u{c3}\n") && a.contains("SFX \u{c3} Y 1") && a.contains("SFX \u{c3} 0 ok ."), "{a}"); + assert!(a.contains("SYLLABLENUM \u{c3}\u{a9}"), "SYLLABLENUM lists flags: {a}"); + assert!(d.contains("kert/\u{c3}\u{a9}")); + let dict = crate::affix::AffixDict::from_hunspell(&a, &d, &[]).unwrap(); + assert!(dict.check("kert") && dict.check("kertok")); + assert!(dict.check("NATO") && !dict.check("Nato"), "KEEPCASE é reaches NATO/é"); + } + + #[test] + fn a_comment_after_flag_utf8_keeps_unicode_flags() { + let (_, d) = decode(b"SET UTF-8\nFLAG UTF-8 # Unicode flags\n", "kert/\u{e9}\n".as_bytes()).unwrap(); + assert_eq!(d, "kert/\u{e9}\n"); + let (_, d) = decode(b"SET\tUTF-8 # comment\nFLAG\t\tUTF-8\n", "kert/\u{e9}\n".as_bytes()).unwrap(); + assert_eq!(d, "kert/\u{e9}\n", "repeated spaces and tabs separate the argument"); + } + #[test] fn decodes_russian_single_byte_encodings() { // `ёлка` in KOI8-R (0xA3 0xCC 0xCB 0xC1) and in Windows-1251 (0xB8 0xEB 0xEA 0xE0). From 9b91b64d60365f2706c468d116dbb9a068a20682 Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:47:25 +0200 Subject: [PATCH 022/308] proof: test that unsupported directives are refused and morphology-only flags load COMPOUNDFIRST and COMPOUNDLAST, the older names of COMPOUNDBEGIN and COMPOUNDEND, are refused like them until compounding is supported. --- crates/proof/src/affix.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 4ce915afa8..77364511f5 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -42,7 +42,9 @@ const UNSUPPORTED: &[&str] = &[ "COMPLEXPREFIXES", "COMPOUNDBEGIN", "COMPOUNDEND", + "COMPOUNDFIRST", "COMPOUNDFLAG", + "COMPOUNDLAST", "COMPOUNDMIDDLE", "COMPOUNDMIN", "COMPOUNDPERMITFLAG", @@ -1594,6 +1596,30 @@ pub(crate) mod tests { assert!(back.check("km") && !back.check("Km")); } + #[test] + fn morphology_only_flags_leave_words_correct() { + // `SUBSTANDARD` and `LEMMA_PRESENT` only steer morphological generation: the stems and + // rules they mark are still correct. + let aff = "SET UTF-8\nSUBSTANDARD x\nLEMMA_PRESENT l\nSFX a Y 2\nSFX a 0 y .\nSFX a 0 em/x .\n"; + let d = AffixDict::from_hunspell(aff, "2\nkot/a\npies/axl\n", &[]).unwrap(); + assert!(d.check("kot") && d.check("koty") && d.check("kotem")); + assert!(d.check("pies") && d.check("piesy") && d.check("piesem")); + } + + #[test] + fn unsupported_directives_are_refused_and_the_rest_load() { + // A dictionary that needs a directive outside this engine fails to load rather than + // accepting the wrong words. + for dir in UNSUPPORTED { + assert!(AffixDict::from_hunspell(&format!("SET UTF-8\n{dir} X\n"), DIC, &[]).is_err(), "{dir} should be refused"); + } + // The directives this engine reads, with no other change to the dictionary. + for line in ["AF 1\nAF ab", "AM 1\nAM po:noun", "ICONV 1\nICONV fi fi", "IGNORE ()", "KEEPCASE k", "SUBSTANDARD x", "LEMMA_PRESENT l"] { + let d = AffixDict::from_hunspell(&format!("{AFF}{line}\n"), DIC, &[]).unwrap_or_else(|e| panic!("{line}: {e}")); + assert!(d.check("koty") && d.check("niekoty") && !d.check("kotx"), "{line}"); + } + } + #[test] fn fields_split_on_spaces_and_tabs_only() { // A flag byte read as U+00A0 (no-break space) or U+0085 is a flag, not a separator. From bb023684f26035a83dcd1de4a6059a3f222848d4 Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:01:57 +0200 Subject: [PATCH 023/308] proof: SUBSTANDARD stems are correct but never suggested, as with NOSUGGEST --- crates/proof/src/affix.rs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 77364511f5..4f15bd56e8 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -244,6 +244,8 @@ pub struct AffixDict { forbidden: Mask, /// Stems with NOSUGGEST are correct, but none of their forms are suggested. no_suggest: Mask, + /// Stems with SUBSTANDARD are correct too, and likewise never suggested. + substandard: Mask, /// Open-addressing hash table of stem indices (`EMPTY` = free). table: Vec, /// Flag char of each bit. @@ -334,6 +336,7 @@ struct Rules { /// The `FORBIDDENWORD` flag: stems carrying it are listed but never correct. forbidden: Option, no_suggest: Option, + substandard: Option, /// The `KEEPCASE` flag. keepcase: Option, iconv: Vec<(String, String)>, @@ -430,9 +433,10 @@ fn parse_aff(aff: &str) -> Result { match dir { "SET" | "LANG" | "NAME" | "VERSION" | "HOME" | "WORDCHARS" | "MAXNGRAMSUGS" | "MAXDIFF" | "ONLYMAXDIFF" | "NOSPLITSUGS" | "SUGSWITHDOTS" | "WARN" | "FORBIDWARN" | "MAXCPDSUGS" => {} - // Morphological aliases, and flags that only matter for morphological generation - // and suggestions: the words they mark are correct. - "AM" | "SUBSTANDARD" | "LEMMA_PRESENT" => {} + // Morphological aliases, and a flag that only matters for morphological generation: + // the words it marks are correct. + "AM" | "LEMMA_PRESENT" => {} + "SUBSTANDARD" => r.substandard = Some(one_flag(at(1), dir, n)?), "AF" => { if !af_header && f.len() == 2 && at(1).parse::().is_ok() { af_header = true; @@ -587,6 +591,7 @@ impl AffixDict { flag_chars: flag_chars.clone(), forbidden, no_suggest: rules.no_suggest.map_or(Mask::EMPTY, bit_of), + substandard: rules.substandard.map_or(Mask::EMPTY, bit_of), keepcase, iconv: rules.iconv, ignore: rules.ignore.into_boxed_slice(), @@ -800,9 +805,10 @@ impl AffixDict { self.contains_except(&self.normalize(w), Mask::EMPTY) } - /// Exactly spelled dictionary words eligible for suggestions (NOSUGGEST stems excluded). + /// Exactly spelled dictionary words eligible for suggestions (NOSUGGEST and SUBSTANDARD + /// stems excluded). pub fn contains_suggestible(&self, w: &str) -> bool { - self.contains_except(&self.normalize(w), self.no_suggest) + self.contains_except(&self.normalize(w), self.no_suggest | self.substandard) } /// `w` after the `ICONV` conversions (at each position the longest match wins) and without @@ -912,7 +918,7 @@ impl AffixDict { /// Whether a suggestion is correct in its displayed case and has an eligible stem. pub fn can_suggest(&self, w: &str) -> bool { - self.check_except(w, self.no_suggest) + self.check_except(w, self.no_suggest | self.substandard) } fn check_except(&self, w: &str, deny: Mask) -> bool { @@ -1079,7 +1085,9 @@ impl AffixDict { for (a, b) in &self.rep { out.push_str(&format!("REP {} {}\n", a.replace(' ', "_"), b.replace(' ', "_"))); } - for (directive, mask) in [("FORBIDDENWORD", self.forbidden), ("NOSUGGEST", self.no_suggest), ("KEEPCASE", self.keepcase)] { + for (directive, mask) in + [("FORBIDDENWORD", self.forbidden), ("NOSUGGEST", self.no_suggest), ("SUBSTANDARD", self.substandard), ("KEEPCASE", self.keepcase)] + { if !mask.is_empty() && let Some(c) = self.flag_char(mask) { @@ -1148,11 +1156,12 @@ impl AffixDict { match at(0) { "TRY" => rules.try_chars = cap_try(at(1)), "KEY" => rules.key = at(1).to_string(), - directive @ ("FORBIDDENWORD" | "NOSUGGEST" | "KEEPCASE") => { + directive @ ("FORBIDDENWORD" | "NOSUGGEST" | "SUBSTANDARD" | "KEEPCASE") => { let c = one_flag(at(1), directive, 0).map_err(|_| format!("affix dictionary: bad {directive} in `{line}`"))?; match directive { "FORBIDDENWORD" => rules.forbidden = Some(c), "NOSUGGEST" => rules.no_suggest = Some(c), + "SUBSTANDARD" => rules.substandard = Some(c), _ => rules.keepcase = Some(c), } } @@ -1597,13 +1606,17 @@ pub(crate) mod tests { } #[test] - fn morphology_only_flags_leave_words_correct() { - // `SUBSTANDARD` and `LEMMA_PRESENT` only steer morphological generation: the stems and - // rules they mark are still correct. + fn substandard_and_lemma_present_words_are_correct() { + // `SUBSTANDARD` and `LEMMA_PRESENT` steer morphological generation, and a SUBSTANDARD + // stem is never suggested; the stems and rules they mark are still correct. let aff = "SET UTF-8\nSUBSTANDARD x\nLEMMA_PRESENT l\nSFX a Y 2\nSFX a 0 y .\nSFX a 0 em/x .\n"; let d = AffixDict::from_hunspell(aff, "2\nkot/a\npies/axl\n", &[]).unwrap(); - assert!(d.check("kot") && d.check("koty") && d.check("kotem")); - assert!(d.check("pies") && d.check("piesy") && d.check("piesem")); + for dict in [&d, &AffixDict::from_bytes(&d.to_bytes()).unwrap()] { + assert!(dict.check("kot") && dict.check("koty") && dict.check("kotem")); + assert!(dict.check("pies") && dict.check("piesy") && dict.check("Piesem")); + assert!(dict.can_suggest("koty") && dict.can_suggest("kotem") && dict.contains_suggestible("kot")); + assert!(!dict.can_suggest("piesy") && !dict.can_suggest("Pies") && !dict.contains_suggestible("pies")); + } } #[test] From 74449b87fb63e707950d68f94f5991330394add7 Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Wed, 21 Oct 2026 22:02:51 +0200 Subject: [PATCH 024/308] proof: read count comments, phrase entries and undefined aliases as Hunspell does (#735) Checked against the Hunspell 1.7 command line: - The first AF and ICONV line is the count even with a trailing comment (AF 2 # aliases), so alias numbers no longer shift and ICONV 1 # no longer turns 1 into #. - A .dic entry ends where Hunspell ends it (dic_entry_end), so ad hoc/B is one entry and ad alone is no word. - An undefined alias in a .dic entry reads as no flags: the stem loads without affixes instead of failing the whole dictionary. - IGNORE characters are removed from the appended text only; a stripped text that holds one never matches a stem, as in Hunspell. - FLAG UTF-8 and SET are found anywhere in the .aff, not only in the first 8 KB. Also: normalize borrows the input when no conversion or ignored character applies, the UTF-8 .aff reader counts fields as it goes and builds its error context only on error, and the single-byte flag reader is decode_latin1. --- crates/proof/src/affix.rs | 81 ++++++++++++++++++++++++++++++++---- crates/proof/src/hunspell.rs | 67 +++++++++++++++++------------ 2 files changed, 114 insertions(+), 34 deletions(-) diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 4f15bd56e8..2d3a7717b2 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -266,7 +266,7 @@ pub struct AffixDict { keepcase: Mask, /// `ICONV`: input conversions applied before a lookup, `(from, to)`. iconv: Vec<(String, String)>, - /// `IGNORE`: characters removed from input words (and from the stems and affixes). + /// `IGNORE`: characters removed from input words (and from the stems and appended texts). ignore: Box<[char]>, /// Provenance and licence lines, kept through [`AffixDict::to_bytes`]. comments: Vec, @@ -361,6 +361,12 @@ impl Rules { .ok_or_else(|| format!("flag alias {written} is not defined")) } + /// The flags of a `.dic` entry. An undefined alias reads as no flags, as in Hunspell: the + /// stem is still a word, without affixes. + fn dic_flags<'a>(&'a self, written: &'a str) -> &'a str { + self.flags(written).unwrap_or("") + } + /// `w` without the `IGNORE` characters. fn strip_ignored(&self, w: &str) -> String { if self.ignore.is_empty() { w.to_string() } else { w.chars().filter(|c| !self.ignore.contains(c)).collect() } @@ -437,15 +443,17 @@ fn parse_aff(aff: &str) -> Result { // the words it marks are correct. "AM" | "LEMMA_PRESENT" => {} "SUBSTANDARD" => r.substandard = Some(one_flag(at(1), dir, n)?), + // The first `AF` or `ICONV` line is the table's count, as in Hunspell, even with a + // trailing comment (`AF 1559 # aliases`). "AF" => { - if !af_header && f.len() == 2 && at(1).parse::().is_ok() { + if !af_header && at(1).parse::().is_ok() { af_header = true; } else { r.aliases.push(at(1).to_string()); } } "ICONV" => { - if !iconv_header && f.len() == 2 && at(1).parse::().is_ok() { + if !iconv_header && at(1).parse::().is_ok() { iconv_header = true; } else if f.len() >= 3 { r.iconv.push((at(1).to_string(), at(2).to_string())); @@ -512,10 +520,11 @@ fn parse_aff(aff: &str) -> Result { } } // Aliases may be declared after the rules that use them; resolve once all are read. + // Hunspell removes `IGNORE` characters from the appended text only: a stripped text that + // holds one never matches a stem (stems lose them too), so the rule never applies. let resolve = |rules: &mut Vec, r: &Rules| -> Result<(), String> { for rule in rules.iter_mut() { rule.4 = r.flags(&rule.4)?.to_string(); - rule.2 = r.strip_ignored(&rule.2); rule.3 = r.strip_ignored(&rule.3); } Ok(()) @@ -538,7 +547,12 @@ impl AffixDict { let rules = parse_aff(aff)?; let mut stems: Vec<(String, String)> = Vec::new(); for (n, line) in dic.lines().enumerate() { - let Some(tok) = split_fields(line).next() else { continue }; + // The entry may hold spaces (`ad hoc/B\tpo:adv`); it ends where Hunspell ends it. + let line = line.trim_start_matches([' ', '\t']); + let tok = line.get(..crate::hunspell::dic_entry_end(line.as_bytes())).unwrap_or_default(); + if tok.is_empty() { + continue; + } if n == 0 && tok.parse::().is_ok() { continue; } @@ -565,7 +579,7 @@ impl AffixDict { if word.is_empty() || word.len() > MAX_WORD_BYTES || word.contains(['\n', '/']) { continue; } - let flags = rules.flags(&flags).map_err(|e| format!(".dic line {}: {e}", n + 1))?.to_string(); + let flags = rules.dic_flags(&flags).to_string(); stems.push((word, flags)); } AffixDict::build(rules, stems, comments.iter().map(ToString::to_string).collect()) @@ -814,7 +828,8 @@ impl AffixDict { /// `w` after the `ICONV` conversions (at each position the longest match wins) and without /// the `IGNORE` characters. fn normalize<'a>(&self, w: &'a str) -> std::borrow::Cow<'a, str> { - if self.iconv.is_empty() && self.ignore.is_empty() { + let converts = |rest: &str| self.iconv.iter().any(|(from, _)| !from.is_empty() && rest.contains(from.as_str())); + if !w.contains(&*self.ignore) && !converts(w) { return std::borrow::Cow::Borrowed(w); } let mut out = String::with_capacity(w.len()); @@ -1577,7 +1592,10 @@ pub(crate) mod tests { let d = AffixDict::from_hunspell(aff, "1\nház/1\t1\n", &[]).unwrap(); assert!(d.contains("ház") && d.contains("házok") && d.contains("házat") && d.contains("házokban")); assert!(!d.contains("házban"), "the stem has a and b, not c"); - assert!(AffixDict::from_hunspell(aff, "1\nház/3\n", &[]).is_err(), "alias 3 is not defined"); + // An undefined alias reads as no flags, as in Hunspell: the stem loads, without affixes. + let undefined = AffixDict::from_hunspell(aff, "2\nház/3\nkert/1\n", &[]).unwrap(); + assert!(undefined.check("ház") && !undefined.check("házok"), "alias 3 is not defined"); + assert!(undefined.check("kertok"), "the other entries keep their aliases"); let back = AffixDict::from_bytes(&d.to_bytes()).unwrap(); assert!(back.contains("házokban") && !back.contains("házban"), "aliases are expanded in the compact format"); } @@ -1594,6 +1612,53 @@ pub(crate) mod tests { assert!(back.check("fines") && back.check("waffle") && back.check("waf(fle)")); } + #[test] + fn count_lines_may_carry_a_comment() { + // Hunspell reads the first `AF` and `ICONV` line as the count, comment and all: `AF 2 #` + // is no alias, and `ICONV 1 # …` doesn't turn `1` into `#`. + let aff = "SET UTF-8\nAF 2 # aliases\nAF a # 1\nAF b # 2\nICONV 1 # ligatures\nICONV q y\nSFX a Y 1\nSFX a 0 ok .\nSFX b Y 1\nSFX b 0 at .\n"; + let d = AffixDict::from_hunspell(aff, "3\nhaz/1\nkert/2\na1\n", &[]).unwrap(); + assert!(d.check("hazok") && !d.check("hazat") && d.check("kertat") && !d.check("kertok"), "alias 1 is `a`"); + assert!(d.check("a1") && !d.check("a#")); + // The UTF-8 reader keeps such a line as it is, so both readers agree on it. + let (a, dic) = crate::hunspell::decode(aff.as_bytes(), "1\nhaz/1\n".as_bytes()).unwrap(); + assert!(a.contains("AF 2 # aliases") && a.contains("ICONV 1 # ligatures"), "{a}"); + assert!(AffixDict::from_hunspell(&a, &dic, &[]).unwrap().check("hazok")); + } + + #[test] + fn dic_entries_may_hold_spaces() { + // `ad hoc/B` is one entry, as in Hunspell; `ad` alone is no word. + let aff = "SET UTF-8\nSFX B Y 1\nSFX B 0 s .\n"; + let d = AffixDict::from_hunspell(aff, "3\nad hoc/B\tpo:adv\nword/B po:noun\n kot/B 12\n", &[]).unwrap(); + assert!(d.contains("ad hoc") && d.contains("ad hocs")); + assert!(!d.contains("ad") && !d.contains("hoc") && !d.contains("ads")); + assert!(d.contains("words") && d.contains("kots") && !d.contains("kot/B")); + } + + #[test] + fn ignored_characters_leave_the_stripped_text_alone() { + // Hunspell removes `IGNORE` characters from the appended text, not from the stripped + // one: `SFX a bx c` never matches the stem `abx` (stored as `ab`), so `ac` is wrong, + // while `SFX a b cx` appends `c` (checked with the Hunspell 1.7 command line). + let strip = AffixDict::from_hunspell("SET UTF-8\nIGNORE x\nSFX a Y 1\nSFX a bx c .\n", "1\nabx/a\n", &[]).unwrap(); + assert!(strip.check("ab") && strip.check("abx") && !strip.check("ac")); + let add = AffixDict::from_hunspell("SET UTF-8\nIGNORE x\nSFX a Y 1\nSFX a b cx .\n", "1\nab/a\n", &[]).unwrap(); + assert!(add.check("ac") && add.check("acx")); + for d in [&strip, &add] { + let back = AffixDict::from_bytes(&d.to_bytes()).unwrap(); + assert_eq!(back.check("ac"), d.check("ac")); + } + } + + #[test] + fn normalizing_borrows_when_nothing_changes() { + let d = AffixDict::from_hunspell("SET UTF-8\nICONV 1\nICONV fi fi\nIGNORE ()\n", "1\nfine\n", &[]).unwrap(); + assert!(matches!(d.normalize("fine"), std::borrow::Cow::Borrowed("fine"))); + assert_eq!(d.normalize("(fi)ne"), "fine"); + assert!(matches!(dict().normalize("kot"), std::borrow::Cow::Borrowed("kot"))); + } + #[test] fn keepcase_stems_match_only_as_written() { let aff = "SET UTF-8\nKEEPCASE k\nSFX a Y 1\nSFX a 0 s .\n"; diff --git a/crates/proof/src/hunspell.rs b/crates/proof/src/hunspell.rs index 934520bebd..7152f7a4c8 100644 --- a/crates/proof/src/hunspell.rs +++ b/crates/proof/src/hunspell.rs @@ -224,15 +224,16 @@ fn decode_high(bytes: &[u8], high: &str) -> String { /// UTF-8. Those fields are read byte by byte (each byte becomes U+0000..U+00FF, one flag per /// byte); the words, affixes and conditions around them are UTF-8. pub fn decode(aff: &[u8], dic: &[u8]) -> Result<(String, String), String> { - let head = String::from_utf8_lossy(aff.get(..aff.len().min(8192)).unwrap_or_default()).to_string(); + // The whole file is searched: a `FLAG UTF-8` after the rules still decides how the `.dic` + // flags are read, as in Hunspell. let directive = |name: &str| { - head.lines().find_map(|l| { - let l = l.trim_start_matches('\u{feff}'); + aff.split(|&b| b == b'\n').find_map(|l| { + let l = l.strip_prefix("\u{feff}".as_bytes()).unwrap_or(l); // Only the first argument counts, as in Hunspell: `FLAG UTF-8 # Unicode flags` is UTF-8. - l.strip_prefix(name) - .filter(|rest| rest.starts_with([' ', '\t'])) - .and_then(|rest| rest.split([' ', '\t']).find(|f| !f.is_empty())) - .map(str::to_ascii_uppercase) + l.strip_prefix(name.as_bytes()) + .filter(|rest| rest.first().is_some_and(|b| matches!(b, b' ' | b'\t'))) + .and_then(|rest| rest.split(|b| matches!(b, b' ' | b'\t' | b'\r')).find(|f| !f.is_empty())) + .map(|f| String::from_utf8_lossy(f).to_ascii_uppercase()) }) }; let set = directive("SET").unwrap_or_else(|| "UTF-8".into()); @@ -284,13 +285,9 @@ const FLAG_DIRECTIVES: &[&str] = &[ /// dictionary's `NAME` is ISO 8859-2) is replaced instead of failing the load. const METADATA_DIRECTIVES: &[&str] = &["NAME", "HOME", "VERSION", "LANG", "AM"]; -/// One byte per char: a flag field read as single-byte flags. -fn bytes_as_flags(b: &[u8]) -> String { - b.iter().map(|&x| char::from(x)).collect() -} - -/// `text/flags`: the text as UTF-8 and the flags byte by byte (`\/` is a literal slash). -fn text_and_flags(field: &[u8], what: &str) -> Result { +/// `text/flags`: the text as UTF-8 and the flags byte by byte (`\/` is a literal slash). `what` +/// names the line in an error. +fn text_and_flags(field: &[u8], what: impl Fn() -> String) -> Result { let mut slash = None; let mut i = 0; while let Some(&b) = field.get(i) { @@ -308,8 +305,8 @@ fn text_and_flags(field: &[u8], what: &str) -> Result { Some(k) => (field.get(..k).unwrap_or_default(), field.get(k..).unwrap_or_default()), None => (field, &[][..]), }; - let mut out = std::str::from_utf8(text).map_err(|e| format!("{what}: not UTF-8: {e}"))?.to_string(); - out.push_str(&bytes_as_flags(flags)); + let mut out = std::str::from_utf8(text).map_err(|e| format!("{}: not UTF-8: {e}", what()))?.to_string(); + out.push_str(&decode_latin1(flags)); Ok(out) } @@ -353,7 +350,7 @@ fn decode_utf8_aff(aff: &[u8]) -> Result { .and_then(|&i| parts.get(i)) .map(|p| String::from_utf8_lossy(p).trim_start_matches('\u{feff}').to_string()) .unwrap_or_default(); - let what = format!(".aff line {}", n + 1); + let what = || format!(".aff line {}", n + 1); if dir.starts_with('#') || METADATA_DIRECTIVES.contains(&dir.as_str()) { out.push_str(&String::from_utf8_lossy(line)); continue; @@ -364,18 +361,23 @@ fn decode_utf8_aff(aff: &[u8]) -> Result { let field_text = |k: usize| words.get(k).and_then(|&w| parts.get(w)).map(|p| String::from_utf8_lossy(p).into_owned()); let first_free = comment_field(&dir, field_text); let comment_at = words.iter().skip(first_free).position(|&w| parts.get(w).is_some_and(|f| f.first() == Some(&b'#'))).map(|k| k + first_free); + // The field index of the next non-separator part. + let mut next_field = 0; for (i, p) in parts.iter().enumerate() { - let field = words.iter().position(|&w| w == i); + let field = (!p.first().is_some_and(|b| matches!(b, b' ' | b'\t'))).then(|| { + next_field += 1; + next_field - 1 + }); let in_comment = comment_at.is_some_and(|c| words.get(c).is_some_and(|&w| i >= w)); let decoded = match (dir.as_str(), field) { _ if in_comment => String::from_utf8_lossy(p).into_owned(), - ("AF" | "COMPOUNDRULE" | "SYLLABLENUM", Some(1)) => bytes_as_flags(p), - ("PFX" | "SFX", Some(3)) | ("CHECKCOMPOUNDPATTERN", Some(1 | 2)) => text_and_flags(p, &what)?, + ("AF" | "COMPOUNDRULE" | "SYLLABLENUM", Some(1)) => decode_latin1(p), + ("PFX" | "SFX", Some(3)) | ("CHECKCOMPOUNDPATTERN", Some(1 | 2)) => text_and_flags(p, what)?, // A field that names one flag keeps its first byte, as Hunspell's `decode_flag` // does: `SFX é` is the flag 0xC3, the first of the two flags `kert/é` carries. - ("PFX" | "SFX", Some(1)) => bytes_as_flags(p.get(..1).unwrap_or_default()), - (d, Some(1)) if FLAG_DIRECTIVES.contains(&d) => bytes_as_flags(p.get(..1).unwrap_or_default()), - _ => std::str::from_utf8(p).map_err(|e| format!("{what}: not UTF-8: {e}"))?.to_string(), + ("PFX" | "SFX", Some(1)) => decode_latin1(p.get(..1).unwrap_or_default()), + (d, Some(1)) if FLAG_DIRECTIVES.contains(&d) => decode_latin1(p.get(..1).unwrap_or_default()), + _ => std::str::from_utf8(p).map_err(|e| format!("{}: not UTF-8: {e}", what()))?.to_string(), }; out.push_str(&decoded); } @@ -433,13 +435,13 @@ fn decode_utf8_dic(dic: &[u8]) -> Result { out.push_str(s); continue; } - let what = format!(".dic line {}", n + 1); + let what = || format!(".dic line {}", n + 1); // The first field is `word/flags` (it may hold spaces, see [`dic_entry_end`]); // morphological fields after it are UTF-8 text. let end = dic_entry_end(line); - out.push_str(&text_and_flags(line.get(..end).unwrap_or_default(), &what)?); + out.push_str(&text_and_flags(line.get(..end).unwrap_or_default(), what)?); let rest = line.get(end..).unwrap_or_default(); - out.push_str(std::str::from_utf8(rest).map_err(|e| format!("{what}: not UTF-8: {e}"))?); + out.push_str(std::str::from_utf8(rest).map_err(|e| format!("{}: not UTF-8: {e}", what()))?); } Ok(out) } @@ -522,6 +524,19 @@ mod tests { assert!(dict.check("NATO") && !dict.check("Nato"), "KEEPCASE é reaches NATO/é"); } + #[test] + fn flag_utf8_counts_anywhere_in_the_aff() { + // A `FLAG UTF-8` after a long rule table still makes the `.dic` flags Unicode. + let mut aff = b"SET UTF-8\nSFX a Y 400\n".to_vec(); + for _ in 0..400 { + aff.extend_from_slice(b"SFX a 0 aaaaaaaaaaaaaaaaaaaaaaaa .\n"); + } + aff.extend_from_slice(b"FLAG UTF-8\r\n"); + assert!(aff.len() > 8192); + let (_, d) = decode(&aff, "kert/\u{e9}\n".as_bytes()).unwrap(); + assert_eq!(d, "kert/\u{e9}\n"); + } + #[test] fn a_comment_after_flag_utf8_keeps_unicode_flags() { let (_, d) = decode(b"SET UTF-8\nFLAG UTF-8 # Unicode flags\n", "kert/\u{e9}\n".as_bytes()).unwrap(); From 3051fab79246566278898522978b13d3d951882f Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:09:52 +0200 Subject: [PATCH 025/308] proof: .dic lines and alias numbers read as Hunspell reads them (#735) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Checked against the Hunspell 1.7 command line: - Without a tab or a morphological field the whole line is the entry, so a phrase with single-byte flags (tök jó/396 in hu_HU) no longer fails the load or loses its alias; leading blanks belong to the entry. - With an AF table a flag field is the alias its leading digits number (fal/2x is alias 2, lab/b has none); an undefined alias, in a .dic entry or a continuation class, reads as no flags instead of failing the dictionary; a table without its count line is ignored. - A REP count line may carry a comment, like AF and ICONV. normalize copies the input only from its first change on, and SET and FLAG are found in one pass over the .aff. --- crates/proof/src/affix.rs | 103 ++++++++++++++++++++++------------- crates/proof/src/hunspell.rs | 61 ++++++++++++++------- 2 files changed, 107 insertions(+), 57 deletions(-) diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 2d3a7717b2..3fbb756f66 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -343,28 +343,27 @@ struct Rules { ignore: Vec, /// `AF` flag aliases: alias `n` (1-based) stands for `aliases[n - 1]`. aliases: Vec, + /// The `AF` table opened with its count line, so flag fields are alias numbers. A table + /// without one is rejected, as in Hunspell: its lines are ignored. + aliases_on: bool, } impl Rules { - /// Flags as written in a `.dic` entry or a continuation class: an `AF` alias number when the - /// dictionary declares aliases, otherwise the flag characters themselves. - fn flags<'a>(&'a self, written: &'a str) -> Result<&'a str, String> { - if self.aliases.is_empty() || written.is_empty() || !written.bytes().all(|b| b.is_ascii_digit()) { - return Ok(written); + /// Flags as written in a `.dic` entry or a continuation class: the flag characters + /// themselves, or with an `AF` table the alias its leading digits number, as Hunspell reads + /// them (`2x` is alias 2). A field without digits or an undefined alias reads as no flags: + /// the stem is still a word, the rule still applies, without the flags (Hunspell warns). + fn flags<'a>(&'a self, written: &'a str) -> &'a str { + if !self.aliases_on { + return written; } + let digits = written.bytes().take_while(u8::is_ascii_digit).count(); written - .parse::() - .ok() + .get(..digits) + .and_then(|d| d.parse::().ok()) .and_then(|n| n.checked_sub(1)) .and_then(|i| self.aliases.get(i)) - .map(String::as_str) - .ok_or_else(|| format!("flag alias {written} is not defined")) - } - - /// The flags of a `.dic` entry. An undefined alias reads as no flags, as in Hunspell: the - /// stem is still a word, without affixes. - fn dic_flags<'a>(&'a self, written: &'a str) -> &'a str { - self.flags(written).unwrap_or("") + .map_or("", String::as_str) } /// `w` without the `IGNORE` characters. @@ -446,9 +445,10 @@ fn parse_aff(aff: &str) -> Result { // The first `AF` or `ICONV` line is the table's count, as in Hunspell, even with a // trailing comment (`AF 1559 # aliases`). "AF" => { - if !af_header && at(1).parse::().is_ok() { + if !af_header { af_header = true; - } else { + r.aliases_on = at(1).parse::().is_ok(); + } else if r.aliases_on { r.aliases.push(at(1).to_string()); } } @@ -479,7 +479,7 @@ fn parse_aff(aff: &str) -> Result { } } "REP" => { - if !rep_header && f.len() == 2 && at(1).parse::().is_ok() { + if !rep_header && at(1).parse::().is_ok() { rep_header = true; } else if f.len() >= 3 { r.rep.push((at(1).replace('_', " "), at(2).replace('_', " "))); @@ -522,16 +522,15 @@ fn parse_aff(aff: &str) -> Result { // Aliases may be declared after the rules that use them; resolve once all are read. // Hunspell removes `IGNORE` characters from the appended text only: a stripped text that // holds one never matches a stem (stems lose them too), so the rule never applies. - let resolve = |rules: &mut Vec, r: &Rules| -> Result<(), String> { + let resolve = |rules: &mut Vec, r: &Rules| { for rule in rules.iter_mut() { - rule.4 = r.flags(&rule.4)?.to_string(); + rule.4 = r.flags(&rule.4).to_string(); rule.3 = r.strip_ignored(&rule.3); } - Ok(()) }; let (mut pfx, mut sfx) = (std::mem::take(&mut r.prefixes), std::mem::take(&mut r.suffixes)); - resolve(&mut pfx, &r)?; - resolve(&mut sfx, &r)?; + resolve(&mut pfx, &r); + resolve(&mut sfx, &r); (r.prefixes, r.suffixes) = (pfx, sfx); Ok(r) } @@ -548,7 +547,7 @@ impl AffixDict { let mut stems: Vec<(String, String)> = Vec::new(); for (n, line) in dic.lines().enumerate() { // The entry may hold spaces (`ad hoc/B\tpo:adv`); it ends where Hunspell ends it. - let line = line.trim_start_matches([' ', '\t']); + // Leading blanks belong to it, as in Hunspell (` lab` is no `lab`). let tok = line.get(..crate::hunspell::dic_entry_end(line.as_bytes())).unwrap_or_default(); if tok.is_empty() { continue; @@ -579,7 +578,7 @@ impl AffixDict { if word.is_empty() || word.len() > MAX_WORD_BYTES || word.contains(['\n', '/']) { continue; } - let flags = rules.dic_flags(&flags).to_string(); + let flags = rules.flags(&flags).to_string(); stems.push((word, flags)); } AffixDict::build(rules, stems, comments.iter().map(ToString::to_string).collect()) @@ -828,28 +827,31 @@ impl AffixDict { /// `w` after the `ICONV` conversions (at each position the longest match wins) and without /// the `IGNORE` characters. fn normalize<'a>(&self, w: &'a str) -> std::borrow::Cow<'a, str> { - let converts = |rest: &str| self.iconv.iter().any(|(from, _)| !from.is_empty() && rest.contains(from.as_str())); - if !w.contains(&*self.ignore) && !converts(w) { + if self.iconv.is_empty() && self.ignore.is_empty() { return std::borrow::Cow::Borrowed(w); } - let mut out = String::with_capacity(w.len()); + // One pass: the input is copied only from the first change on. + let mut out: Option = None; let mut rest = w; while let Some(c) = rest.chars().next() { + let done = w.get(..w.len() - rest.len()).unwrap_or_default(); let hit = self.iconv.iter().filter(|(from, _)| !from.is_empty() && rest.starts_with(from.as_str())).max_by_key(|(from, _)| from.len()); match hit { Some((from, to)) => { - out.extend(to.chars().filter(|c| !self.ignore.contains(c))); + out.get_or_insert_with(|| done.to_string()).extend(to.chars().filter(|c| !self.ignore.contains(c))); rest = rest.get(from.len()..).unwrap_or_default(); } None => { - if !self.ignore.contains(&c) { - out.push(c); + if self.ignore.contains(&c) { + out.get_or_insert_with(|| done.to_string()); + } else if let Some(o) = out.as_mut() { + o.push(c); } rest = rest.get(c.len_utf8()..).unwrap_or_default(); } } } - std::borrow::Cow::Owned(out) + out.map_or(std::borrow::Cow::Borrowed(w), std::borrow::Cow::Owned) } /// [`AffixDict::contains`] on normalized input, where stems carrying a flag in `deny` don't @@ -1612,6 +1614,28 @@ pub(crate) mod tests { assert!(back.check("fines") && back.check("waffle") && back.check("waf(fle)")); } + #[test] + fn alias_numbers_are_read_as_hunspell_reads_them() { + // Checked with the Hunspell 1.7 command line. With an `AF` table a flag field is the alias + // its leading digits number: `fal/2x` is alias 2, `lab/b` has no flags. + let aff = "SET UTF-8\nAF 2\nAF B\nAF C\nSFX B Y 1\nSFX B 0 s .\nSFX C Y 1\nSFX C 0 x .\nSFX b Y 1\nSFX b 0 q .\n"; + let d = AffixDict::from_hunspell(aff, "3\nlab/b\nfal/2x\nkut/1\n", &[]).unwrap(); + assert!(d.check("lab") && !d.check("labq") && !d.check("labs")); + assert!(d.check("falx") && !d.check("fals") && d.check("kuts")); + // A table without its count line is rejected: no alias applies. + let broken = AffixDict::from_hunspell("SET UTF-8\nAF B\nAF C\nSFX B Y 1\nSFX B 0 s .\n", "2\nlab/1\nfal/2\n", &[]).unwrap(); + assert!(broken.check("lab") && !broken.check("labs") && !broken.check("fals")); + // An undefined alias in a continuation class leaves the rule in force, without it. + let cont = AffixDict::from_hunspell("SET UTF-8\nAF 1\nAF a\nSFX a Y 1\nSFX a 0 ok/7 .\n", "1\nhaz/1\n", &[]).unwrap(); + assert!(cont.check("haz") && cont.check("hazok")); + } + + #[test] + fn a_rep_count_line_may_carry_a_comment() { + let d = AffixDict::from_hunspell("SET UTF-8\nREP 1 # confusions\nREP x y\n", "1\nay\n", &[]).unwrap(); + assert_eq!(d.rep(), [("x".to_string(), "y".to_string())]); + } + #[test] fn count_lines_may_carry_a_comment() { // Hunspell reads the first `AF` and `ICONV` line as the count, comment and all: `AF 2 #` @@ -1630,10 +1654,14 @@ pub(crate) mod tests { fn dic_entries_may_hold_spaces() { // `ad hoc/B` is one entry, as in Hunspell; `ad` alone is no word. let aff = "SET UTF-8\nSFX B Y 1\nSFX B 0 s .\n"; - let d = AffixDict::from_hunspell(aff, "3\nad hoc/B\tpo:adv\nword/B po:noun\n kot/B 12\n", &[]).unwrap(); - assert!(d.contains("ad hoc") && d.contains("ad hocs")); - assert!(!d.contains("ad") && !d.contains("hoc") && !d.contains("ads")); + // Checked with the Hunspell 1.7 command line: without a tab or a morphological field the + // whole line is the entry (`kot/B 12` has the flag `B`), and leading blanks belong to it + // (` lab` is no `lab`). + let d = AffixDict::from_hunspell(aff, "5\nad hoc/B\tpo:adv\nword/B po:noun\nkot/B 12\n lab/B\nés így/B\n", &[]).unwrap(); + assert!(d.contains("ad hoc") && d.contains("ad hocs") && d.contains("és így") && d.contains("és ígys")); + assert!(!d.contains("ad") && !d.contains("hoc") && !d.contains("ads") && !d.contains("így")); assert!(d.contains("words") && d.contains("kots") && !d.contains("kot/B")); + assert!(!d.contains("lab") && !d.contains("labs")); } #[test] @@ -1691,8 +1719,9 @@ pub(crate) mod tests { for dir in UNSUPPORTED { assert!(AffixDict::from_hunspell(&format!("SET UTF-8\n{dir} X\n"), DIC, &[]).is_err(), "{dir} should be refused"); } - // The directives this engine reads, with no other change to the dictionary. - for line in ["AF 1\nAF ab", "AM 1\nAM po:noun", "ICONV 1\nICONV fi fi", "IGNORE ()", "KEEPCASE k", "SUBSTANDARD x", "LEMMA_PRESENT l"] { + // The directives this engine reads, with no other change to the dictionary (`AF` would + // turn the flags of `DIC` into alias numbers; it has tests of its own). + for line in ["AM 1\nAM po:noun", "ICONV 1\nICONV fi fi", "IGNORE ()", "KEEPCASE k", "SUBSTANDARD x", "LEMMA_PRESENT l"] { let d = AffixDict::from_hunspell(&format!("{AFF}{line}\n"), DIC, &[]).unwrap_or_else(|e| panic!("{line}: {e}")); assert!(d.check("koty") && d.check("niekoty") && !d.check("kotx"), "{line}"); } diff --git a/crates/proof/src/hunspell.rs b/crates/proof/src/hunspell.rs index 7152f7a4c8..d2d26416f2 100644 --- a/crates/proof/src/hunspell.rs +++ b/crates/proof/src/hunspell.rs @@ -224,22 +224,11 @@ fn decode_high(bytes: &[u8], high: &str) -> String { /// UTF-8. Those fields are read byte by byte (each byte becomes U+0000..U+00FF, one flag per /// byte); the words, affixes and conditions around them are UTF-8. pub fn decode(aff: &[u8], dic: &[u8]) -> Result<(String, String), String> { - // The whole file is searched: a `FLAG UTF-8` after the rules still decides how the `.dic` - // flags are read, as in Hunspell. - let directive = |name: &str| { - aff.split(|&b| b == b'\n').find_map(|l| { - let l = l.strip_prefix("\u{feff}".as_bytes()).unwrap_or(l); - // Only the first argument counts, as in Hunspell: `FLAG UTF-8 # Unicode flags` is UTF-8. - l.strip_prefix(name.as_bytes()) - .filter(|rest| rest.first().is_some_and(|b| matches!(b, b' ' | b'\t'))) - .and_then(|rest| rest.split(|b| matches!(b, b' ' | b'\t' | b'\r')).find(|f| !f.is_empty())) - .map(|f| String::from_utf8_lossy(f).to_ascii_uppercase()) - }) - }; - let set = directive("SET").unwrap_or_else(|| "UTF-8".into()); + let (set, flag) = directives(aff); + let set = set.unwrap_or_else(|| "UTF-8".into()); match set.as_str() { "UTF-8" | "UTF8" => { - if matches!(directive("FLAG").as_deref(), Some("UTF-8" | "UTF8")) { + if matches!(flag.as_deref(), Some("UTF-8" | "UTF8")) { let text = |b: &[u8], what: &str| String::from_utf8(b.to_vec()).map_err(|e| format!("{what}: not UTF-8: {e}")); return Ok((text(aff, ".aff")?, text(dic, ".dic")?)); } @@ -254,6 +243,32 @@ pub fn decode(aff: &[u8], dic: &[u8]) -> Result<(String, String), String> { } } +/// The first arguments of the `.aff`'s first `SET` and `FLAG` lines, in capitals, found in one pass +/// over the whole file: a `FLAG UTF-8` after the rules still decides how the `.dic` flags are read, +/// as in Hunspell. Only the first argument counts: `FLAG UTF-8 # Unicode flags` is UTF-8. +fn directives(aff: &[u8]) -> (Option, Option) { + let (mut set, mut flag) = (None, None); + for l in aff.split(|&b| b == b'\n') { + if set.is_some() && flag.is_some() { + break; + } + let l = l.strip_prefix("\u{feff}".as_bytes()).unwrap_or(l); + let slot = match l.first() { + Some(b'S') if set.is_none() => (&mut set, &b"SET"[..]), + Some(b'F') if flag.is_none() => (&mut flag, &b"FLAG"[..]), + _ => continue, + }; + let arg = l + .strip_prefix(slot.1) + .filter(|rest| rest.first().is_some_and(|b| matches!(b, b' ' | b'\t'))) + .and_then(|rest| rest.split(|b| matches!(b, b' ' | b'\t' | b'\r')).find(|f| !f.is_empty())); + if let Some(a) = arg { + *slot.0 = Some(String::from_utf8_lossy(a).to_ascii_uppercase()); + } + } + (set, flag) +} + /// Directives whose one argument is a flag. const FLAG_DIRECTIVES: &[&str] = &[ "CIRCUMFIX", @@ -406,7 +421,8 @@ fn comment_field(dir: &str, field: impl Fn(usize) -> Option) -> usize { /// Where the `word/flags` entry of a `.dic` line ends, as Hunspell reads it: at a tab or at the /// first morphological field (a space or tab, two characters and `:`, as in ` po:noun`), whichever -/// comes first, so an entry may hold spaces (`ad hoc`); with neither, at the first space. +/// comes first, so an entry may hold spaces (`ad hoc`); with neither, at the end of the line +/// (`kot/B 12` has the flags `B 12`, as in Hunspell). pub(crate) fn dic_entry_end(line: &[u8]) -> usize { let blank = |b: &u8| matches!(b, b' ' | b'\t'); let morph = line.windows(4).position(|w| matches!(w, [a, b, c, b':'] if blank(a) && !blank(b) && !blank(c))); @@ -415,10 +431,10 @@ pub(crate) fn dic_entry_end(line: &[u8]) -> usize { (Some(m), Some(t)) => m.min(t), (m, t) => match m.or(t) { Some(e) => e, - None => line.iter().position(|&b| b == b' ').unwrap_or(line.len()), + None => line.len(), }, }; - // Spaces before the morphological field aren't part of the entry. + // Blanks before the morphological field or the line end aren't part of the entry. line.get(..end).map_or(end, |e| e.iter().rposition(|b| !blank(b)).map_or(0, |k| k + 1)) } @@ -495,10 +511,10 @@ mod tests { fn utf8_dictionaries_keep_single_byte_flags() { // The Hungarian dictionary is `SET UTF-8` but writes flags as single bytes above 0x7F, // which aren't UTF-8; each byte is one flag. Its `NAME` line is ISO 8859-2. - let aff = b"SET UTF-8\nNAME Magyar helyes\xedr\xe1si\nAF 1\nAF \xd5a # 1\nSFX \xd5 Y 1\nSFX \xd5 0 ok/\xe9 . 7\nSFX \xe9 Y 1\nSFX \xe9 0 ban .\nSFX a Y 1\nSFX a 0 at .\n"; + let aff = b"SET UTF-8\nNAME Magyar helyes\xedr\xe1si\nAF 2\nAF \xd5a # 1\nAF \xe9 # 2\nSFX \xd5 Y 1\nSFX \xd5 0 ok/2 . 7\nSFX \xe9 Y 1\nSFX \xe9 0 ban .\nSFX a Y 1\nSFX a 0 at .\n"; let dic = "2\nház/1\t1\nkert/\u{e9}\n".as_bytes(); let (a, d) = decode(aff, dic).unwrap(); - assert!(a.contains("AF \u{d5}a # 1") && a.contains("SFX \u{d5} 0 ok/\u{e9} . 7")); + assert!(a.contains("AF \u{d5}a # 1") && a.contains("AF \u{e9} # 2") && a.contains("SFX \u{d5} 0 ok/2 . 7")); assert!(d.contains("kert/\u{c3}\u{a9}"), "a UTF-8 é in the flags is two one-byte flags"); let dict = crate::affix::AffixDict::from_hunspell(&a, &d, &[]).unwrap(); assert!(dict.check("ház") && dict.check("házok") && dict.check("házokban") && dict.check("házat")); @@ -578,12 +594,17 @@ mod tests { assert_eq!(end("word/A po:noun\tts:x"), "word/A", "the morphological field comes before the tab"); assert_eq!(end("ad hoc/B\tpo:adv"), "ad hoc/B", "a tab ends a phrase"); assert_eq!(end("ad hoc/B po:adv"), "ad hoc/B"); - assert_eq!(end("word/A 123"), "word/A", "neither: the first space"); + assert_eq!(end("word/A 123"), "word/A 123", "neither: the whole line, as in Hunspell"); + assert_eq!(end("ad hoc/B "), "ad hoc/B"); assert_eq!(end("word"), "word"); assert_eq!(end("a:b/A"), "a:b/A", "a colon inside the word isn't a field"); // Single-byte flags of a phrase entry are read byte by byte, then the UTF-8 morphology. let (_, d) = decode(b"SET UTF-8\n", b"1\nad hoc/\xe9\tpo:adv\n").unwrap(); assert_eq!(d, "1\nad hoc/\u{e9}\tpo:adv\n"); + // Without a tab or a morphological field the whole line is the entry: its byte flags + // don't fail the load (the Hungarian dictionary has such lines: `tök jó/396`). + let (_, d) = decode(b"SET UTF-8\n", b"1\nad hoc/\xe9\n").unwrap(); + assert_eq!(d, "1\nad hoc/\u{e9}\n"); } #[test] From a47412aef9faec369bf62ef2bb4eb120b0a5d442 Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Mon, 26 Oct 2026 09:46:25 +0100 Subject: [PATCH 026/308] proof: an AF count below 1 rejects the table, flags stay literal (#735) --- crates/proof/src/affix.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 3fbb756f66..7d4fc1821e 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -447,7 +447,8 @@ fn parse_aff(aff: &str) -> Result { "AF" => { if !af_header { af_header = true; - r.aliases_on = at(1).parse::().is_ok(); + // A count below 1 rejects the table too: flags stay literal (`AF 0`). + r.aliases_on = at(1).parse::().is_ok_and(|n| n > 0); } else if r.aliases_on { r.aliases.push(at(1).to_string()); } @@ -1625,6 +1626,9 @@ pub(crate) mod tests { // A table without its count line is rejected: no alias applies. let broken = AffixDict::from_hunspell("SET UTF-8\nAF B\nAF C\nSFX B Y 1\nSFX B 0 s .\n", "2\nlab/1\nfal/2\n", &[]).unwrap(); assert!(broken.check("lab") && !broken.check("labs") && !broken.check("fals")); + // `AF 0` is rejected the same way: `lab/B` keeps its literal flag. + let zero = AffixDict::from_hunspell("SET UTF-8\nAF 0\nSFX B Y 1\nSFX B 0 s .\n", "1\nlab/B\n", &[]).unwrap(); + assert!(zero.check("lab") && zero.check("labs")); // An undefined alias in a continuation class leaves the rule in force, without it. let cont = AffixDict::from_hunspell("SET UTF-8\nAF 1\nAF a\nSFX a Y 1\nSFX a 0 ok/7 .\n", "1\nhaz/1\n", &[]).unwrap(); assert!(cont.check("haz") && cont.check("hazok")); From e4765c999d723d3dde11ddb422465e64153c1954 Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:13:24 +0100 Subject: [PATCH 027/308] proof: numeric and long flags keep a UTF-8 .aff as text (#735) --- crates/proof/src/hunspell.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/crates/proof/src/hunspell.rs b/crates/proof/src/hunspell.rs index d2d26416f2..1afd903cd0 100644 --- a/crates/proof/src/hunspell.rs +++ b/crates/proof/src/hunspell.rs @@ -219,16 +219,18 @@ fn decode_high(bytes: &[u8], high: &str) -> String { /// one the original Portuguese VERO uses, ISO 8859-9 the one older Turkish dictionaries use, KOI8-R /// and Windows-1251 the ones older Russian dictionaries use). /// -/// Under a UTF-8 `SET` without `FLAG UTF-8`, flags are still single bytes, as in Hunspell: the -/// Hungarian dictionary writes its affix flags and flag aliases as bytes above 0x7F that aren't -/// UTF-8. Those fields are read byte by byte (each byte becomes U+0000..U+00FF, one flag per -/// byte); the words, affixes and conditions around them are UTF-8. +/// Under a UTF-8 `SET` with single-byte flags (no `FLAG` line, or `FLAG DEFAULT`), flags are bytes, +/// as in Hunspell: the Hungarian dictionary writes its affix flags and flag aliases as bytes above +/// 0x7F that aren't UTF-8. Those fields are read byte by byte (each byte becomes U+0000..U+00FF, +/// one flag per byte); the words, affixes and conditions around them are UTF-8. With any other +/// `FLAG` (`UTF-8`, `num`, `long`) flags are text and the whole file is UTF-8: `SFX 1234` keeps +/// its number (the Turkish dictionary's `FLAG num`). pub fn decode(aff: &[u8], dic: &[u8]) -> Result<(String, String), String> { let (set, flag) = directives(aff); let set = set.unwrap_or_else(|| "UTF-8".into()); match set.as_str() { "UTF-8" | "UTF8" => { - if matches!(flag.as_deref(), Some("UTF-8" | "UTF8")) { + if flag.as_deref().is_some_and(|f| f != "DEFAULT") { let text = |b: &[u8], what: &str| String::from_utf8(b.to_vec()).map_err(|e| format!("{what}: not UTF-8: {e}")); return Ok((text(aff, ".aff")?, text(dic, ".dic")?)); } @@ -540,6 +542,24 @@ mod tests { assert!(dict.check("NATO") && !dict.check("Nato"), "KEEPCASE é reaches NATO/é"); } + #[test] + fn numeric_and_long_flags_stay_text() { + // `FLAG num` (the Turkish dictionary) and `FLAG long`: no byte-wise flag reading, so a + // number with a non-ASCII rule or comment keeps every digit. + let aff = "SET UTF-8\nFLAG num\nFORBIDDENWORD 6000 # tiltott ő\nSFX 1234 Y 1\nSFX 1234 0 ları .\n"; + let (a, d) = decode(aff.as_bytes(), "1\nkitap/1234\n".as_bytes()).unwrap(); + assert_eq!(a, aff); + assert_eq!(d, "1\nkitap/1234\n"); + let tr = crate::tr::dict::TrDict::from_hunspell(&a, &d).unwrap(); + assert!(tr.contains("kitapları") && tr.contains("kitap")); + let long = "SET UTF-8\nFLAG long\nSFX Aa Y 1\nSFX Aa 0 ős .\n"; + assert_eq!(decode(long.as_bytes(), "1\nkő/Aa\n".as_bytes()).unwrap().0, long); + assert!(decode(b"SET UTF-8\nFLAG num\n", b"1\nh\xe1z/1\n").is_err(), "the .dic must be UTF-8"); + // `FLAG DEFAULT` is the single-byte default. + let (_, d) = decode(b"SET UTF-8\nFLAG default\n", b"1\nkert/\xe9\n").unwrap(); + assert_eq!(d, "1\nkert/\u{e9}\n"); + } + #[test] fn flag_utf8_counts_anywhere_in_the_aff() { // A `FLAG UTF-8` after a long rule table still makes the `.dic` flags Unicode. From fa657a1c41ae49066e966e8b99f79706e97e27a6 Mon Sep 17 00:00:00 2001 From: Skaywalker <115165402+Skaywalker@users.noreply.github.com> Date: Tue, 27 Oct 2026 14:21:39 +0100 Subject: [PATCH 028/308] proof: U+0085 and U+00A0 are flags in one-flag directives too (#735) --- crates/proof/src/affix.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 7d4fc1821e..c966cf1319 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -382,7 +382,9 @@ fn split_fields(line: &str) -> impl Iterator { fn one_flag(arg: &str, dir: &str, n: usize) -> Result { let mut it = arg.chars().filter(|&c| c != EMOJI_SELECTOR); match (it.next(), it.next()) { - (Some(c), None) if !c.is_whitespace() => Ok(c), + // Only the field separators of `split_fields` can't be a flag: a flag byte read as + // U+0085 or U+00A0 is one. + (Some(c), None) if !matches!(c, ' ' | '\t') => Ok(c), _ => Err(format!("line {}: {dir} needs one flag character", n + 1)), } } @@ -1739,6 +1741,13 @@ pub(crate) mod tests { assert!(d.contains("koty") && d.contains("kotem")); let back = AffixDict::from_bytes(&d.to_bytes()).unwrap(); assert!(back.contains("koty") && back.contains("kotem")); + // The same bytes as one-flag directives, as Hunspell reads them. + let aff = "SET UTF-8\nFORBIDDENWORD \u{a0}\nKEEPCASE \u{85}\nSFX a Y 1\nSFX a 0 s .\n"; + let d = AffixDict::from_hunspell(aff, "3\nkot/a\nrossz/\u{a0}\npH/\u{85}\n", &[]).unwrap(); + for dict in [&d, &AffixDict::from_bytes(&d.to_bytes()).unwrap()] { + assert!(dict.check("kots") && !dict.check("rossz")); + assert!(dict.check("pH") && !dict.check("PH")); + } } #[test] From 55dce8eebfeb453ebfb2a7d812de8895340fc5f8 Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Tue, 27 Oct 2026 17:13:05 +0100 Subject: [PATCH 029/308] ci: avoid persisting proofing-preview checkout credentials Set persist-credentials: false for preview tests and join the two newly added documentation blockquotes as requested by review. No Rust source change. Validation: independently reviewed exact workflow and Markdown diffs and tree hashes. Cargo fmt passed on the unchanged Rust source; cargo xtask ci was attempted but blocked by missing local dlltool.exe. Fresh hosted checks are required for this revision. --- .github/workflows/spelling-review.yml | 2 ++ ROADMAP.md | 2 +- docs/gaps.md | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/spelling-review.yml b/.github/workflows/spelling-review.yml index c23a77c036..3adc7cdc80 100644 --- a/.github/workflows/spelling-review.yml +++ b/.github/workflows/spelling-review.yml @@ -37,6 +37,8 @@ jobs: CARGO_INCREMENTAL: '0' steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + persist-credentials: false - name: Install build and software-rendering dependencies run: | sudo apt-get update diff --git a/ROADMAP.md b/ROADMAP.md index 9a7fdd2d7f..fee18576de 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -641,7 +641,7 @@ Footer page-table height and body wrapping are corrected. Other pagination gaps ## Revision history > 2026-10-27 · Technical synchronization: existing Proofing pane hooks and regression tests follow the extracted engine/UI modules on main `436d444`; compact context menus remain upstream. New-SHA validation is pending; readiness unchanged. - +> > 2026-10-23 · cu20we review follow-up: Only paragraph-relative box floats use the pre-spacing origin. Line-relative floats and page/margin fallbacks in cells and text boxes keep the text-top origin. Synthetic controls compare zero and 24 pt space-before across all four box contexts. New cap conversion helpers and regression tests document their contracts; readiness estimates unchanged. | Date | Change | Summary | diff --git a/docs/gaps.md b/docs/gaps.md index 42f27873c7..1f6a722234 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -143,7 +143,7 @@ Small follow-ups from the 2026-10-25 field, table and AutoFormat batch (#4232): ## Revision history > 2026-10-27 · Technical synchronization: existing Proofing pane hooks and regression tests follow the extracted engine/UI modules on main `436d444`; compact context menus remain upstream. New-SHA validation is pending; readiness unchanged. - +> > 2026-10-23 · cu20we review follow-up: Only paragraph-relative box floats use the pre-spacing origin. Line-relative floats and page/margin fallbacks in cells and text boxes keep the text-top origin. Synthetic controls compare zero and 24 pt space-before across all four box contexts. New cap conversion helpers and regression tests document their contracts; readiness estimates unchanged. > 2026-10-23 · cu20we: DrawingML flat, square and round line caps are imported, saved, displayed and exported to PDF; absent or unknown caps default to flat. VML end caps (including shapetype inheritance) are preserved through DOCX save. Ink and metafile stroke handling remain separate. Synthetic DOCX, raster and PDF regressions cover the change; no new native Word comparison. From 0d41b183010c55e87fb23c9ab37c6dda72e51e05 Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Thu, 29 Oct 2026 15:38:01 -0200 Subject: [PATCH 030/308] Engine tests: the dictionaries-folder test checks each reported file again The shared dictionary slot reports no file while a dictionary given with install_dictionary is in use, so this test had accepted null alongside the folder's file, for installs from tests running at the same time. That also let it pass if file.options stopped reporting the folder's file at all. The tests' install helpers and this test now take turns on one lock, and each language must report the folder's file again. An install from before the test doesn't matter: changing the folder reloads every slot. --- crates/engine/src/tests.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index aa1d995b6f..267934ad41 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -8038,9 +8038,18 @@ fn next_footnote_skips_endnotes() { assert_eq!(s.sel.focus.off, 9); } +/// Tests that install a test dictionary wait for the dictionaries-folder test, which changes the +/// folder and then reads what each language reports: an install in between reports no file. +static DICTIONARIES: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +fn dictionaries_turn() -> std::sync::MutexGuard<'static, ()> { + DICTIONARIES.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + /// A tiny synthetic Polish Hunspell dictionary (written for these tests). Real dictionaries are /// installed on the system, never bundled; every Polish test installs this same one. fn polish_test_dictionary() { + let _turn = dictionaries_turn(); wordcraft_proof::pl::install_dictionary(POLISH_TEST_AFF, POLISH_TEST_DIC).unwrap(); } @@ -8050,6 +8059,7 @@ const POLISH_TEST_DIC: &str = "12\npłynie\nrzeka\na\nto\nwiem\nże\ntak\nw\nog /// A tiny synthetic Portuguese Hunspell dictionary (written for these tests). Real dictionaries /// are installed on the system, never bundled; every Portuguese test installs this same one. fn portuguese_test_dictionary() { + let _turn = dictionaries_turn(); wordcraft_proof::pt::install_dictionary(PORTUGUESE_TEST_AFF, PORTUGUESE_TEST_DIC).unwrap(); } @@ -8063,6 +8073,7 @@ const PORTUGUESE_TEST_DIC: &str = "11\nsaudade\ncoração\ne\nele\ndisse\nnão\n /// running meanwhile see the same words; it stays set for the rest of the run for that reason. #[test] fn dictionaries_folder_option_is_saved_searched_and_reported() { + let _turn = dictionaries_turn(); let dir = std::env::temp_dir().join(format!("wc-engine-dictionaries-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); for (name, aff, dic) in [ @@ -8093,12 +8104,10 @@ fn dictionaries_folder_option_is_saved_searched_and_reported() { assert_eq!(r["dictionariesFolder"], json!(dir.to_string_lossy())); let dicts = r["dictionaries"].as_array().unwrap(); let file = |lang: &str| dicts.iter().find(|d| d["language"] == lang).map(|d| d["file"].clone()); - // A language reports the folder's file, or none while a dictionary another test gave with - // install_dictionary() is in use (those tests run concurrently); the folder is searched first - // either way. + // Each language reports the folder's file, which is searched first. let reported = |got: Option<&serde_json::Value>, name: &str| { let want = dir.join(format!("{name}.dic")); - assert!(got.is_some_and(|f| f.is_null() || *f == json!(want.to_string_lossy())), "{name}: {dicts:?}"); + assert_eq!(got, Some(&json!(want.to_string_lossy())), "{name}: {dicts:?}"); assert_eq!(wordcraft_proof::hunspell::find(&[name]).map(|(_, dic)| dic), Some(want)); }; reported(file("pl").as_ref(), "pl_PL"); @@ -8158,6 +8167,7 @@ fn portuguese_proofing_through_the_review_commands() { } fn indonesian_test_dictionary() { + let _turn = dictionaries_turn(); wordcraft_proof::id::install_dictionary(INDONESIAN_TEST_AFF, INDONESIAN_TEST_DIC).unwrap(); } @@ -8329,6 +8339,7 @@ const URUGUAYAN_TEST_DIC: &str = "10\nesto\nes\nuna\nprueba/S\ndel\ncorrector\nt fn spanish_test_dictionaries() { use wordcraft_proof::ProofLang; + let _turn = dictionaries_turn(); for (lang, dic) in [(ProofLang::Es, SPANISH_TEST_DIC), (ProofLang::EsUy, URUGUAYAN_TEST_DIC)] { wordcraft_proof::es::install_dictionary(lang, SPANISH_TEST_AFF, dic).unwrap(); } @@ -8457,6 +8468,7 @@ const ITALIAN_TEST_AFF: &str = "SET UTF-8\nTRY aeiortnsclcaffèéè\nMAP 2\nMAP const ITALIAN_TEST_DIC: &str = "6\ncasa\ncaffè\nroma\ndisse\ne\nper\n"; fn italian_test_dictionary() { + let _turn = dictionaries_turn(); wordcraft_proof::it::install_dictionary(ITALIAN_TEST_AFF, ITALIAN_TEST_DIC).unwrap(); } From 6e96b5470f448a51b7a7a9442886a6af5c9d77df Mon Sep 17 00:00:00 2001 From: totoz64 <47389794+totoz64@users.noreply.github.com> Date: Fri, 30 Oct 2026 10:44:42 +0100 Subject: [PATCH 031/308] PROOF.API: preserve explicit-position suggestion lookup --- crates/engine/src/cmd/review/proofing.rs | 9 +++++++-- crates/engine/src/cmd/review/tests.rs | 16 ++++++++++++++++ docs/spelling-review.md | 11 ++++++++--- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/review/proofing.rs b/crates/engine/src/cmd/review/proofing.rs index 14694278e4..2b3a2e4a9b 100644 --- a/crates/engine/src/cmd/review/proofing.rs +++ b/crates/engine/src/cmd/review/proofing.rs @@ -120,8 +120,13 @@ pub(super) fn all_issues(s: &mut Session, _: &Value) -> CmdResult { } pub(super) fn suggestions(s: &mut Session, v: &Value) -> CmdResult { - let at = v.get("pos").and_then(crate::cmd::parse_pos).unwrap_or_else(|| s.sel.focus.clone()); - match issue_at(s, &at) { + // Explicit positions are point lookups, even when they equal the selection focus. + // Only an implicit or unparseable position keeps the exact selected issue. + let issue = match v.get("pos").and_then(crate::cmd::parse_pos) { + Some(at) => para_issues(s, at.story, &at.path).into_iter().find(|(a, b, _)| a.off <= at.off && at.off <= b.off), + None => issue_at(s, &s.sel.focus), + }; + match issue { Some((a, b, i)) => Ok(issue_json(s, &a, &b, &i)), None => Ok(Value::Null), } diff --git a/crates/engine/src/cmd/review/tests.rs b/crates/engine/src/cmd/review/tests.rs index db350deea2..64dd983970 100644 --- a/crates/engine/src/cmd/review/tests.rs +++ b/crates/engine/src/cmd/review/tests.rs @@ -1095,6 +1095,22 @@ mod spelling_navigation_tests { std::mem::swap(&mut s.sel.anchor, &mut s.sel.focus); } assert_eq!(s.run("review.suggestions", &json!({})).unwrap(), selected); + let before_lookup = s.sel.clone(); + let focus = s.sel.focus.off; + let explicit = s.run("review.suggestions", &json!({"pos": {"block": 0, "off": focus}})).unwrap(); + assert_eq!(explicit["kind"], "grammar", "an explicit focus is a point lookup in either selection direction"); + assert_eq!(explicit["text"], "sentense sentense"); + assert_eq!(explicit["start"]["off"], 2); + assert_eq!(explicit["end"]["off"], 19); + assert_eq!(s.sel, before_lookup, "suggestion lookups do not move the selection"); + for pos in [serde_json::Value::Null, json!({"off": "invalid"})] { + assert_eq!( + s.run("review.suggestions", &json!({"pos": pos})).unwrap(), + selected, + "unparseable positions preserve implicit selection lookup" + ); + } + assert_eq!(s.run("review.suggestions", &json!({})).unwrap(), selected); let elsewhere = s.run("review.suggestions", &json!({"pos": {"block": 0, "off": 5}})).unwrap(); assert_eq!(elsewhere["kind"], "spelling"); assert_eq!(elsewhere["start"]["off"], 2, "an explicit other position keeps point lookup"); diff --git a/docs/spelling-review.md b/docs/spelling-review.md index 219cced518..968a79e0b5 100644 --- a/docs/spelling-review.md +++ b/docs/spelling-review.md @@ -1,6 +1,6 @@ # Spelling and grammar task pane -> **Last updated:** 2026-10-27 · **Status:** technical synchronization with main `436d444` prepared; validation on the new merge SHA is pending. Historical status: historical three-platform checks passed; at f1264f3 the revised hosted gate, previews, Windows and Linux passed, while macOS remained unvalidated after pre-run cancellations. Overlap-review corrections now require new hosted checks. +> **Last updated:** 2026-10-30 · **Status:** synchronized with main `3b7f7ed`; explicit-position API follow-up prepared, with validation of its new SHA pending. Historical status: historical three-platform checks passed; at f1264f3 the revised hosted gate, previews, Windows and Linux passed, while macOS remained unvalidated after pre-run cancellations. Overlap-review corrections now require new hosted checks. ## Word reference @@ -18,16 +18,20 @@ If another command changes the document body or selection, a cached suggestion i This work is independent of French contribution #3185. It consumes language-aware engine results and adds neither dictionaries nor grammar rules. Labels use the external shared translation lookup with English fallback for missing keys. No release date or readiness increase is implied. +## Technical synchronization and API follow-up (2026-10-30) + +The existing pane is merged with main `3b7f7ed10b2eb0ae3c39b71551d36ed2df1d1d8b`, retaining the upstream menus, pane host and per-window state. For `review.suggestions`, a successfully parsed explicit `pos` is a pure point lookup even when it equals the selection focus; an absent or unparseable `pos` keeps exact-selection priority. The follow-up preserves correction and undo; fresh hosted checks must validate its new SHA. Results for its parent `c077c155` do not validate the follow-up. + ## Technical synchronization (2026-10-27) The language-independent pane is rebased by a three-way merge with main `436d444ef0ee35c2e2b9fb44f87aa7d08d72641f`. Engine navigation and exact selected-range lookup live in `crates/engine/src/cmd/review/proofing.rs`; the existing regressions follow into `review/tests.rs`. The UI command hook follows into `app/commands.rs`, construction into `app.rs`, inspection into `control/handler.rs`, and pane state/closure into `panes/state.rs` and `panes/host.rs`. The upstream context-menu and dropdown implementations stay intact. Workflow path filters cover the extracted modules while retaining historical paths. No dictionary, grammar rule, new feature or readiness increase is introduced by this synchronization. ## Verification -- Engine: one-/two-paragraph navigation, forward-only completion, explicit wrapping, exact spelling selection within overlapping grammar in either selection direction, explicit-position fallback, correction and undo. +- Engine: one-/two-paragraph navigation, forward-only completion, explicit wrapping, exact spelling selection within overlapping grammar in either selection direction, explicit point lookup at the focus, absent/unparseable-position selection fallback, unchanged selection, correction and undo. - UI: direct suggestion correction and undo, Ignore Once progress/completion, Ignore All, personal dictionary addition, stale selection/body refusal, completion and Close, per-window isolation and document switching. - Offscreen: the ignored spelling_review_dialog_preview test writes light/dark PNGs for comparison with the recorded Word arrangement. -- Required gate: `cargo xtask ci`, plus the proofing preview workflow. No compilation or test was run in this resolution staging; the coordinator must validate the new merge SHA. Earlier hosted results do not validate this synchronization. +- Required gate: `cargo xtask ci`, plus the existing proofing preview workflow. Local formatting passed; the full gate was attempted but the offline cache lacks the locked `craft-update` dependency from craft-libs `04aa1703`. No local compilation or test success is claimed. The coordinator must validate the follow-up SHA; earlier hosted results validate their own SHAs only. The first light/dark artifacts at `20205a5` were inspected: the sentence frame shrank to its text and the unbounded suggestion list pushed actions below the viewport. The revised layout fills the pane width and scrolls the suggestion list after approximately three cards, keeping the actions below it visible. At `f1264f3`, the revised light/dark previews and full hosted gate passed; Windows and Linux UI checks passed, while macOS remained unvalidated after pre-run cancellations. Both PNGs were inspected, including the right-aligned Read Aloud control. The following review revision gives the exact selected spelling range priority over an overlapping grammar issue and requires new hosted checks. Exact pixel parity is not claimed. @@ -35,6 +39,7 @@ The first light/dark artifacts at `20205a5` were inspected: the sentence frame s | Date | Change | Summary | |---|---|---| +| 2026-10-30 | trivial | Merge the existing pane with main `3b7f7ed`; preserve explicit-position point lookup at the selection focus, implicit/parse-fallback selection priority and correction/undo; new follow-up SHA requires fresh hosted validation | | 2026-10-27 | trivial | Synchronize the existing pane with extracted engine/UI modules on main `436d444`; preserve selection/navigation/undo tests and compact menus, expand workflow path filters; new-SHA validation pending | | 2026-10-26 | minor | Resolve selected spelling before overlapping grammar, add command/pane regressions, include pane-integration workflow paths and distinguish validated history from pending review corrections | | 2026-10-26 | minor | Inspect first successful light/dark artifacts; fill the sentence frame and bound suggestion-list height, retaining all suggestions through scrolling | From 94362712735d0445c719e972e4a22f3ea49195d0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 22:58:55 +0000 Subject: [PATCH 032/308] Gap 19: ODT paragraph spacing, indents and line spacing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Read `fo:margin-*`, `fo:text-indent`, `fo:line-height` (percent, length, normal), `style:line-height-at-least` and `style:line-spacing` (leading, as a proportional approximation) from automatic and common paragraph styles (ODF 1.3 §20.204-209, §20.225, §20.317-318), and write them back from each paragraph's direct formatting. The default paragraph style and Standard give the document defaults, on ODF's initial values: an ODT paragraph without spacing has none, not Word's 8 pt after and 1.15 lines. Export writes the document's Normal spacing as the default paragraph style, so the copy reopens the same. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GBVQLyDnrG8Tn1yDQcUabC --- ROADMAP.md | 3 +- crates/formats/src/model/from_doc.rs | 9 +- crates/formats/src/odt.rs | 3 + crates/formats/src/odt/body.rs | 6 +- crates/formats/src/odt/import.rs | 27 +++- crates/formats/src/odt/package.rs | 19 ++- crates/formats/src/odt/styles.rs | 144 +++++++++++++++--- crates/formats/src/odt/tests.rs | 4 +- crates/formats/src/odt/tests_more.rs | 3 + crates/formats/src/odt/tests_more/spacing.rs | 146 +++++++++++++++++++ crates/formats/src/odt/writer.rs | 26 ++++ docs/file-format-parity.md | 5 +- 12 files changed, 353 insertions(+), 42 deletions(-) create mode 100644 crates/formats/src/odt/tests_more.rs create mode 100644 crates/formats/src/odt/tests_more/spacing.rs diff --git a/ROADMAP.md b/ROADMAP.md index 90c51e17dc..15114df637 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-29 · **Change:** trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -140,6 +140,7 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm ## Progress log +- 2026-10-31: ODT: paragraph spacing, indents and line spacing are read and written, and an ODT's own default spacing (none, single lines) replaces Word's 8 pt after and 1.15 lines, so LibreOffice documents keep their vertical rhythm on open and save (gap 19). Readiness unchanged. - 2026-10-31: DOCX: a paragraph naming a style the file doesn't define is formatted as Normal, as in Word, not with the document defaults alone, so a Pandoc footer naming an undefined `Footer` style is as tall as Word's and no longer pushes the last paragraph to the next page (#5077). Readiness unchanged. - 2026-10-31: A table of contents writes its page numbers as `PAGEREF` fields to the headings' bookmarks, as Word does, so updating the fields in Word moves the numbers with the headings; they were plain text (#5097). - 2026-10-31: Keytips reach the Backstage: Alt, F badges its pages with Word's letters, and Alt, F, P (or V) opens Print (#4960). diff --git a/crates/formats/src/model/from_doc.rs b/crates/formats/src/model/from_doc.rs index 120cf9d379..1ea0053093 100644 --- a/crates/formats/src/model/from_doc.rs +++ b/crates/formats/src/model/from_doc.rs @@ -123,6 +123,11 @@ fn media_ext(key: &str, data: &[u8]) -> String { } } +/// The spacing and indents a paragraph sets itself (its style's are not included). +fn direct_spacing(p: &wordcraft_doc::props::ParaProps) -> Spacing { + Spacing { before: p.space_before, after: p.space_after, line: p.line_spacing, left: p.indent_left, right: p.indent_right, first: p.indent_first } +} + /// A document paragraph as flow paragraphs: a page break character inside the text ends the /// paragraph, and the text after it starts a new one that carries `page_break`. pub fn flow_paras(doc: &Document, p: &Paragraph) -> Vec { @@ -142,7 +147,7 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, tracked: bool) -> Vec { page_break: p.props.page_break_before.unwrap_or(false), keep_next: p.props.keep_next.unwrap_or(false), inlines: Vec::new(), - spacing: Spacing::default(), + spacing: direct_spacing(&p.props), borders, section: None, mark_ins: None, @@ -173,7 +178,7 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, tracked: bool) -> Vec { page_break: true, keep_next: p.props.keep_next.unwrap_or(false), inlines: Vec::new(), - spacing: Spacing::default(), + spacing: direct_spacing(&p.props), borders, section: None, mark_ins: None, diff --git a/crates/formats/src/odt.rs b/crates/formats/src/odt.rs index aeb1ad0597..f70b226efd 100644 --- a/crates/formats/src/odt.rs +++ b/crates/formats/src/odt.rs @@ -56,3 +56,6 @@ mod page_parts; #[cfg(test)] mod tests; + +#[cfg(test)] +mod tests_more; diff --git a/crates/formats/src/odt/body.rs b/crates/formats/src/odt/body.rs index 628c7257a4..b8326fd7bf 100644 --- a/crates/formats/src/odt/body.rs +++ b/crates/formats/src/odt/body.rs @@ -302,8 +302,8 @@ impl Body<'_> { fn start_para(&mut self, style: Option<&str>, heading: Option) { self.end_para(); - let (mut kind, align, mut pb, rule, keep_next, base) = - style.map(|s| self.styles.para(s)).unwrap_or((Kind::Normal, None, false, false, false, Fmt::default())); + let look = style.map(|s| self.styles.para(s)).unwrap_or_default(); + let (mut kind, align, mut pb, rule, keep_next, base) = (look.kind, look.align, look.page_break, look.rule, look.keep_next, look.fmt); // The section break starts the new page; no page break besides. if let Some(next) = style.and_then(|s| self.page_section(s)) && self.break_section(next) @@ -313,7 +313,7 @@ impl Body<'_> { if let Some(h) = heading { kind = Kind::Heading(h.clamp(1, 6)); } - let mut p = Para { kind, align, page_break: pb, keep_next, ..Default::default() }; + let mut p = Para { kind, align, page_break: pb, keep_next, spacing: look.spacing, ..Default::default() }; if rule && kind == Kind::Normal { p.kind = Kind::Rule; } diff --git a/crates/formats/src/odt/import.rs b/crates/formats/src/odt/import.rs index 61d4460045..424c3da66e 100644 --- a/crates/formats/src/odt/import.rs +++ b/crates/formats/src/odt/import.rs @@ -110,11 +110,25 @@ pub fn parse(bytes: &[u8]) -> Result { /// [`parse`], with the last section's properties (the others are on the paragraphs that end them). fn parse_full(bytes: &[u8]) -> Result<(Flow, SectionProps), String> { - parse_tracked(bytes).map(|(f, last, ..)| (f, last)) + parse_tracked(bytes).map(|p| (p.flow, p.last)) } -/// [`parse_full`], plus whether change tracking is on (`None`: the document has no tracked changes). -fn parse_tracked(bytes: &[u8]) -> Result<(Flow, SectionProps, Option, Option, page_parts::Parts), String> { +/// What [`parse_tracked`] reads besides the flow. +struct Parsed { + flow: Flow, + /// The last section's properties. + last: SectionProps, + /// Whether change tracking is on (`None`: the document has no tracked changes). + track: Option, + /// The document's default font. + font: Option, + /// The document's default paragraph spacing. + spacing: model::Spacing, + parts: page_parts::Parts, +} + +/// [`parse_full`], plus what the document holds outside the flow. +fn parse_tracked(bytes: &[u8]) -> Result { let mut z = zip::ZipArchive::new(Cursor::new(bytes)).map_err(|e| format!("ODT: not a valid package ({e})"))?; if let Some(mt) = read_entry(&mut z, "mimetype") { let mt = String::from_utf8_lossy(&mt); @@ -154,17 +168,20 @@ fn parse_tracked(bytes: &[u8]) -> Result<(Flow, SectionProps, Option, Opti blocks.push(FBlock::Para(Para::default())); } let track = body.track; - Ok((Flow { blocks, meta }, last, track, styles.doc_font(), parts)) + Ok(Parsed { flow: Flow { blocks, meta }, last, track, font: styles.doc_font(), spacing: styles.doc_spacing(), parts }) } /// Parse an `.odt` package into a document. pub fn import(bytes: &[u8]) -> Result { model::begin_import(); - let (flow, last, track, font, parts) = parse_tracked(bytes)?; + let Parsed { flow, last, track, font, spacing, parts } = parse_tracked(bytes)?; let mut doc = model::to_doc(&flow); if font.is_some() { doc.styles.default_chr.font = font; } + let d = &mut doc.styles.default_para; + (d.space_before, d.space_after, d.line_spacing) = (spacing.before, spacing.after, spacing.line); + (d.indent_left, d.indent_right, d.indent_first) = (spacing.left, spacing.right, spacing.first); doc.last_section = last; page_parts::add(&mut doc, parts); if let Some(t) = track { diff --git a/crates/formats/src/odt/package.rs b/crates/formats/src/odt/package.rs index 74c6704a45..9e0bdbb2c6 100644 --- a/crates/formats/src/odt/package.rs +++ b/crates/formats/src/odt/package.rs @@ -1,4 +1,4 @@ -use super::writer::{NS, Writer, pt, x}; +use super::writer::{NS, Writer, pt, spacing_props, x}; use super::{Columns, Cursor, Document, FBlock, Meta, SectionProps, SectionStart, Write, model}; /// `style:columns` (ODF 1.3 §17.12) of equal columns with their gap and, when on, the line between. @@ -9,10 +9,12 @@ fn columns_xml(c: &Columns) -> String { /// One master page per entry of `pages` (name, setup), each with its own page layout `pm1`, `pm2`… /// (§16.5, §16.9); the first is the default one, "Standard". -fn styles_xml(pages: &[(String, wordcraft_doc::SectionProps)], body_font: &str) -> String { +/// `defaults` is the spacing of the document's Normal paragraphs, written as the default style's. +fn styles_xml(pages: &[(String, wordcraft_doc::SectionProps)], body_font: &str, defaults: &model::Spacing) -> String { let mut s = format!("\n"); s.push_str(&format!( - "", + "", + spacing_props(defaults), x(body_font) )); s.push_str(""); @@ -155,7 +157,16 @@ pub fn export(doc: &Document) -> Result, String> { w.automatic_styles() ); let body_font = doc.styles.default_chr.font.clone().unwrap_or_else(|| wordcraft_doc::styles::BODY_FONT.to_string()); - let styles = styles_xml(&pages, &body_font); + let normal = doc.styles.resolve_para(&Default::default()); + let defaults = model::Spacing { + before: Some(normal.space_before), + after: Some(normal.space_after), + line: Some(normal.line_spacing), + left: Some(normal.indent_left), + right: Some(normal.indent_right), + first: Some(normal.indent_first), + }; + let styles = styles_xml(&pages, &body_font, &defaults); let meta = meta_xml(&flow.meta); let mut manifest = String::from( "\n\n\n\n\n\n", diff --git a/crates/formats/src/odt/styles.rs b/crates/formats/src/odt/styles.rs index 38fe63150b..045d3792e4 100644 --- a/crates/formats/src/odt/styles.rs +++ b/crates/formats/src/odt/styles.rs @@ -1,5 +1,7 @@ use super::xml::{Attrs, Ev, events, get, length, local}; use super::{Align, Arc, Columns, Fmt, HashMap, Kind, Rgb, VAlign, model}; +use crate::model::Spacing; +use wordcraft_doc::props::LineSpacing; // --------------------------------------------------------------------------------------------- // Import @@ -66,6 +68,73 @@ struct StyleInfo { page_number: Option>, /// `style:columns` of a section style. columns: Option, + /// Margins, first-line indent and line spacing of `style:paragraph-properties`. + spacing: Spacing, +} + +/// Paragraph spacing, indents and line height of a `style:paragraph-properties` element (ODF 1.3 +/// §20.204–209, §20.225, §20.317–318) over `sp`. Percentages of the parent's margins aren't +/// followed (left as they were); spacing is clamped to 0..=1584 pt, indents to ±1584 pt. +fn paragraph_spacing(a: &Attrs, sp: &mut Spacing) { + let len = |k: &str| get(a, k).and_then(length); + let space = |k: &str| len(k).map(|v| v.clamp(0.0, MAX_LEN)); + let indent = |k: &str| len(k).map(|v| v.clamp(-MAX_LEN, MAX_LEN)); + if let Some(v) = get(a, "fo:margin").and_then(length) { + sp.before = Some(v.clamp(0.0, MAX_LEN)); + sp.after = sp.before; + sp.left = Some(v.clamp(-MAX_LEN, MAX_LEN)); + sp.right = sp.left; + } + sp.before = space("fo:margin-top").or(sp.before); + sp.after = space("fo:margin-bottom").or(sp.after); + sp.left = indent("fo:margin-left").or(sp.left); + sp.right = indent("fo:margin-right").or(sp.right); + sp.first = indent("fo:text-indent").or(sp.first); + if let Some(v) = get(a, "fo:line-height").map(str::trim) { + if v.eq_ignore_ascii_case("normal") { + sp.line = Some(LineSpacing::Multiple(1.0)); + } else if let Some(pc) = v.strip_suffix('%').and_then(|n| n.trim().parse::().ok()).filter(|n| n.is_finite() && *n > 0.0) { + // Word's range: a sixteenth of a line up to 132 lines. + sp.line = Some(LineSpacing::Multiple((pc / 100.0).clamp(0.06, 132.0))); + } else if let Some(h) = length(v).filter(|h| *h > 0.0) { + sp.line = Some(LineSpacing::Exactly(h.clamp(0.7, MAX_LEN))); + } + } + if let Some(h) = len("style:line-height-at-least").filter(|h| *h >= 0.0) { + sp.line = Some(LineSpacing::AtLeast(h.clamp(0.7, MAX_LEN))); + } + // Leading (extra space between lines) has no WordCraft mode: as a multiple of a 12 pt line. + if let Some(l) = len("style:line-spacing").filter(|l| *l >= 0.0) { + sp.line = Some(LineSpacing::Multiple(((LEADING_LINE + l.min(MAX_LEN)) / LEADING_LINE).clamp(0.06, 132.0))); + } +} + +/// Largest spacing or indent read, in points (22 inches, Word's page limit). +const MAX_LEN: f32 = 1584.0; + +/// Height of the single line `style:line-spacing` leading is added to (12 pt text). +const LEADING_LINE: f32 = 14.0; + +/// `over` laid on `base`: what `over` sets wins. +fn overlay_spacing(base: &mut Spacing, over: &Spacing) { + base.before = over.before.or(base.before); + base.after = over.after.or(base.after); + base.line = over.line.or(base.line); + base.left = over.left.or(base.left); + base.right = over.right.or(base.right); + base.first = over.first.or(base.first); +} + +/// How a paragraph style looks ([`Styles::para`]). +#[derive(Clone, Debug, Default)] +pub(super) struct ParaLook { + pub(super) kind: Kind, + pub(super) align: Option, + pub(super) page_break: bool, + pub(super) rule: bool, + pub(super) keep_next: bool, + pub(super) fmt: Fmt, + pub(super) spacing: Spacing, } /// Columns of a `style:columns` element (ODF 1.3 §17.12): count and gap, clamped as the RTF and @@ -138,12 +207,40 @@ fn text_properties(a: &Attrs, style: &mut StyleInfo) { } } +/// A paragraph style's `style:paragraph-properties` (ODF 1.3 §17.6) into `s`. +fn paragraph_properties(a: &Attrs, s: &mut StyleInfo) { + s.align = match get(a, "fo:text-align").map(|v| v.trim().to_ascii_lowercase()).as_deref() { + Some("center") => Some(Align::Center), + Some("end") | Some("right") => Some(Align::Right), + Some("justify") => Some(Align::Justify), + Some("start") | Some("left") => Some(Align::Left), + _ => s.align, + }; + if get(a, "fo:break-before").is_some_and(|b| b.trim().eq_ignore_ascii_case("page")) { + s.page_break = true; + } + match get(a, "fo:keep-with-next") { + Some("always") => s.keep_next = Some(true), + Some("auto") => s.keep_next = Some(false), + _ => {} + } + if let Some(v) = get(a, "style:page-number") { + s.page_number = Some(v.trim().parse::().ok().map(|n| n.min(32_767))); + } + if get(a, "fo:border-bottom").is_some_and(|b| !b.trim().eq_ignore_ascii_case("none")) { + s.border_bottom = true; + } + paragraph_spacing(a, &mut s.spacing); +} + #[derive(Default)] pub(super) struct Styles { map: HashMap, pub(super) lists: HashMap>, /// Font of `style:default-style` for paragraphs (the document default, ODF 1.3 §16.4). default_font: Option, + /// Spacing of `style:default-style` for paragraphs. + default_spacing: Spacing, } impl Styles { @@ -172,6 +269,7 @@ impl Styles { "text-properties" if in_default && cur.is_none() => { self.default_font = default_font(&a); } + "paragraph-properties" if in_default && cur.is_none() => paragraph_spacing(&a, &mut self.default_spacing), "text-properties" => { if let Some((_, s)) = &mut cur { text_properties(&a, s); @@ -208,27 +306,7 @@ impl Styles { } "paragraph-properties" => { if let Some((_, s)) = &mut cur { - s.align = match get(&a, "fo:text-align").map(|v| v.trim().to_ascii_lowercase()).as_deref() { - Some("center") => Some(Align::Center), - Some("end") | Some("right") => Some(Align::Right), - Some("justify") => Some(Align::Justify), - Some("start") | Some("left") => Some(Align::Left), - _ => s.align, - }; - if get(&a, "fo:break-before").is_some_and(|b| b.trim().eq_ignore_ascii_case("page")) { - s.page_break = true; - } - match get(&a, "fo:keep-with-next") { - Some("always") => s.keep_next = Some(true), - Some("auto") => s.keep_next = Some(false), - _ => {} - } - if let Some(v) = get(&a, "style:page-number") { - s.page_number = Some(v.trim().parse::().ok().map(|n| n.min(32_767))); - } - if get(&a, "fo:border-bottom").is_some_and(|b| !b.trim().eq_ignore_ascii_case("none")) { - s.border_bottom = true; - } + paragraph_properties(&a, s); } } "columns" if q.starts_with("style:") => { @@ -281,6 +359,24 @@ impl Styles { self.map.get("Standard").filter(|s| !s.auto).and_then(|s| s.fmt.font.as_deref().map(str::to_string)).or_else(|| self.default_font.clone()) } + /// The document's default paragraph spacing: the default style's, with the Standard + /// paragraph style's over it, on ODF's initial values (no margins, single lines). + pub(super) fn doc_spacing(&self) -> Spacing { + let mut sp = Spacing { + before: Some(0.0), + after: Some(0.0), + line: Some(LineSpacing::Multiple(1.0)), + left: Some(0.0), + right: Some(0.0), + first: Some(0.0), + }; + overlay_spacing(&mut sp, &self.default_spacing); + if let Some(s) = self.map.get("Standard").filter(|s| !s.auto) { + overlay_spacing(&mut sp, &s.spacing); + } + sp + } + fn chain<'a>(&'a self, name: &'a str) -> Vec<(&'a str, &'a StyleInfo)> { let mut out = Vec::new(); let mut cur = Some(name); @@ -340,7 +436,7 @@ impl Styles { /// the style that gives the paragraph its kind and that style's ancestors: their formatting /// is the kind's own look (a heading's size and colour), not direct formatting. `Standard`, /// the default paragraph style, holds document defaults and is left out too. - pub(super) fn para(&self, name: &str) -> (Kind, Option, bool, bool, bool, Fmt) { + pub(super) fn para(&self, name: &str) -> ParaLook { let chain = self.chain(name); let mut kind = Kind::Normal; let mut kind_at = chain.len(); @@ -372,11 +468,13 @@ impl Styles { let pb = chain.iter().take_while(|(_, s)| s.auto).any(|(_, s)| s.page_break); let rule = chain.iter().any(|(_, s)| s.border_bottom); let mut f = Fmt::default(); + let mut spacing = Spacing::default(); for (_, s) in chain.iter().take(kind_at).rev().filter(|(n, s)| s.auto || *n != "Standard") { s.fmt.apply(&mut f); + overlay_spacing(&mut spacing, &s.spacing); } // Like the text formatting, the kind style's own `fo:keep-with-next` (a heading's) is the kind's look, not direct formatting. let keep_next = chain.iter().take(kind_at).find_map(|(_, s)| s.keep_next).unwrap_or(false); - (kind, align, pb, rule, keep_next, f) + ParaLook { kind, align, page_break: pb, rule, keep_next, fmt: f, spacing } } } diff --git a/crates/formats/src/odt/tests.rs b/crates/formats/src/odt/tests.rs index a73c806353..729334beab 100644 --- a/crates/formats/src/odt/tests.rs +++ b/crates/formats/src/odt/tests.rs @@ -183,10 +183,10 @@ fn object_without_mathml_falls_back_to_its_replacement_image() { assert!(imgs[0].data.as_slice() == png.as_slice()); } -const NS_T: &str = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0""#; +pub(super) const NS_T: &str = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0""#; /// A package with `content.xml` and, when given, `styles.xml`. -fn package(content: &str, styles: Option<&str>) -> Vec { +pub(super) fn package(content: &str, styles: Option<&str>) -> Vec { let mut z = zip::ZipWriter::new(Cursor::new(Vec::new())); let o = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); for (name, data) in diff --git a/crates/formats/src/odt/tests_more.rs b/crates/formats/src/odt/tests_more.rs new file mode 100644 index 0000000000..1906c58e61 --- /dev/null +++ b/crates/formats/src/odt/tests_more.rs @@ -0,0 +1,3 @@ +//! Tests for the ODT features added after the first reader and writer: one module per feature. + +mod spacing; diff --git a/crates/formats/src/odt/tests_more/spacing.rs b/crates/formats/src/odt/tests_more/spacing.rs new file mode 100644 index 0000000000..aae3ff201a --- /dev/null +++ b/crates/formats/src/odt/tests_more/spacing.rs @@ -0,0 +1,146 @@ +//! Paragraph spacing, indents and line spacing (ODF 1.3 §20.204 `fo:line-height`, §20.206–209 +//! `fo:margin-*`, §20.225 `fo:text-indent`, §20.317 `style:line-height-at-least`, §20.318 +//! `style:line-spacing`). + +use crate::odt::tests::{NS_T, package}; +use crate::odt::{export, import}; +use wordcraft_doc::props::{LineSpacing, ParaProps}; +use wordcraft_doc::{Document, Paragraph, para_block}; + +fn props(d: &Document) -> Vec { + d.body.iter().filter_map(|b| b.as_para()).map(|p| p.props.clone()).collect() +} + +fn content(auto: &str, body: &str) -> String { + format!( + r#"{auto}{body}"# + ) +} + +fn p_style(name: &str, parent: &str, props: &str) -> String { + format!( + r#""# + ) +} + +fn close(a: Option, b: f32) -> bool { + a.is_some_and(|a| (a - b).abs() < 0.01) +} + +#[test] +fn margins_indents_and_line_spacing_are_read() { + let auto = [ + p_style("P1", "Standard", r#"fo:margin-top="24pt" fo:margin-bottom="0.5cm""#), + p_style("P2", "Standard", r#"fo:margin-left="2cm" fo:margin-right="1cm" fo:text-indent="-1cm""#), + p_style("P3", "Standard", r#"fo:line-height="200%""#), + p_style("P4", "Standard", r#"fo:line-height="20pt""#), + p_style("P5", "Standard", r#"style:line-height-at-least="17pt""#), + p_style("P6", "Standard", r#"fo:line-height="normal""#), + ] + .concat(); + let body = (1..=6).map(|i| format!(r#"p{i}"#)).collect::(); + let d = import(&package(&content(&auto, &body), None)).unwrap(); + let p = props(&d); + assert!(close(p[0].space_before, 24.0) && close(p[0].space_after, 14.17), "{:?}", p[0]); + assert!(close(p[1].indent_left, 56.69) && close(p[1].indent_right, 28.35) && close(p[1].indent_first, -28.35), "{:?}", p[1]); + assert_eq!(p[2].line_spacing, Some(LineSpacing::Multiple(2.0))); + assert_eq!(p[3].line_spacing, Some(LineSpacing::Exactly(20.0))); + assert_eq!(p[4].line_spacing, Some(LineSpacing::AtLeast(17.0))); + assert_eq!(p[5].line_spacing, Some(LineSpacing::Multiple(1.0))); +} + +#[test] +fn spacing_is_inherited_through_common_styles() { + // LibreOffice's "Text body" style carries spacing that its paragraphs show. + let styles = format!( + r#"{}"#, + p_style("Text_20_body", "Standard", r#"fo:margin-top="0cm" fo:margin-bottom="0.247cm" fo:line-height="115%""#) + ); + let auto = p_style("P1", "Text_20_body", r#"fo:margin-left="1cm""#); + let body = r#"ab"#; + let d = import(&package(&content(&auto, body), Some(&styles))).unwrap(); + for p in props(&d) { + let r = d.styles.resolve_para(&p); + assert!((r.space_after - 7.0).abs() < 0.01 && r.space_before == 0.0, "{r:?}"); + assert_eq!(r.line_spacing, LineSpacing::Multiple(1.15)); + } + assert!(close(props(&d)[1].indent_left, 28.35)); +} + +#[test] +fn the_default_paragraph_style_gives_the_document_defaults() { + // An ODT paragraph with no spacing has none (ODF's initial values), not Word's 8 pt after + // and 1.15 lines. + let body = "plain"; + let d = import(&package(&content("", body), None)).unwrap(); + let r = d.styles.resolve_para(&props(&d)[0]); + assert_eq!((r.space_before, r.space_after, r.line_spacing), (0.0, 0.0, LineSpacing::Multiple(1.0))); + // The default style and Standard set them: Standard wins. + let styles = format!( + r#""# + ); + let d = import(&package(&content("", body), Some(&styles))).unwrap(); + let p = props(&d); + assert_eq!(p[0].space_after, None, "the defaults are not direct formatting"); + let r = d.styles.resolve_para(&p[0]); + assert_eq!((r.space_after, r.line_spacing), (3.0, LineSpacing::Multiple(1.5))); +} + +#[test] +fn hostile_spacing_values_are_clamped() { + let auto = [ + p_style("P1", "Standard", r#"fo:margin-top="-5pt" fo:margin-bottom="1e30pt" fo:margin-left="-1e30pt" fo:text-indent="NaNpt""#), + p_style("P2", "Standard", r#"fo:line-height="100000%""#), + p_style("P3", "Standard", r#"fo:line-height="-3pt""#), + p_style("P4", "Standard", r#"fo:line-height="0%" style:line-height-at-least="1e9pt""#), + p_style("P5", "Standard", r#"fo:margin-top="10%" fo:line-height="x""#), + ] + .concat(); + let body = (1..=5).map(|i| format!(r#"p{i}"#)).collect::(); + let d = import(&package(&content(&auto, &body), None)).unwrap(); + let p = props(&d); + assert_eq!(p[0].space_before, Some(0.0)); + assert_eq!(p[0].space_after, Some(1584.0)); + assert_eq!(p[0].indent_left, Some(-1584.0)); + assert_eq!(p[0].indent_first, None); + assert_eq!(p[1].line_spacing, Some(LineSpacing::Multiple(132.0))); + assert_eq!(p[2].line_spacing, None); + assert!(matches!(p[3].line_spacing, Some(LineSpacing::AtLeast(v)) if v == 1584.0), "{:?}", p[3].line_spacing); + assert_eq!((p[4].space_before, p[4].line_spacing), (None, None)); +} + +fn para(text: &str, props: ParaProps) -> std::sync::Arc { + let mut p = Paragraph::new(); + p.text = text.into(); + p.runs = vec![wordcraft_doc::para::Run { len: text.len(), props: Default::default() }]; + p.props = props; + para_block(p) +} + +#[test] +fn direct_spacing_round_trips() { + let mut d = Document::new(); + let want = vec![ + ParaProps { space_before: Some(18.0), space_after: Some(6.0), ..Default::default() }, + ParaProps { indent_left: Some(36.0), indent_right: Some(18.0), indent_first: Some(-18.0), ..Default::default() }, + ParaProps { line_spacing: Some(LineSpacing::Multiple(1.5)), ..Default::default() }, + ParaProps { line_spacing: Some(LineSpacing::Exactly(20.0)), ..Default::default() }, + ParaProps { line_spacing: Some(LineSpacing::AtLeast(15.0)), ..Default::default() }, + ParaProps::default(), + ]; + d.body = want.iter().enumerate().map(|(i, p)| para(&format!("p{i}"), p.clone())).collect(); + let back = import(&export(&d).unwrap()).unwrap(); + assert_eq!(props(&back), want); +} + +#[test] +fn the_document_defaults_round_trip() { + // A new document's Normal (8 pt after, 1.15 lines) is written as the default paragraph style, + // so the copy reopens with the same spacing. + let mut d = Document::new(); + d.body = vec![para("x", ParaProps::default())]; + let back = import(&export(&d).unwrap()).unwrap(); + let r = back.styles.resolve_para(&props(&back)[0]); + let o = d.styles.resolve_para(&ParaProps::default()); + assert_eq!((r.space_before, r.space_after, r.line_spacing), (o.space_before, o.space_after, o.line_spacing)); +} diff --git a/crates/formats/src/odt/writer.rs b/crates/formats/src/odt/writer.rs index 2b9392002e..73a29c8084 100644 --- a/crates/formats/src/odt/writer.rs +++ b/crates/formats/src/odt/writer.rs @@ -1,4 +1,5 @@ use super::{Align, FBlock, FTable, Fmt, HashMap, Inline, Kind, Para, RevMark, Rgb, VAlign, mime_of, model}; +use wordcraft_doc::props::LineSpacing; // --------------------------------------------------------------------------------------------- // Export @@ -115,6 +116,30 @@ fn text_props(f: &Fmt) -> String { p } +/// `style:paragraph-properties` attributes for the spacing, indents and line spacing `sp` sets +/// (ODF 1.3 §20.204–209, §20.225, §20.317). +pub(super) fn spacing_props(sp: &model::Spacing) -> String { + let mut p = String::new(); + for (k, v) in [ + ("fo:margin-top", sp.before), + ("fo:margin-bottom", sp.after), + ("fo:margin-left", sp.left), + ("fo:margin-right", sp.right), + ("fo:text-indent", sp.first), + ] { + if let Some(v) = v.filter(|v| v.is_finite()) { + p.push_str(&format!(" {k}=\"{}\"", pt(v))); + } + } + match sp.line { + Some(LineSpacing::Multiple(m)) if m.is_finite() => p.push_str(&format!(" fo:line-height=\"{}%\"", (m * 100.0).round().max(1.0))), + Some(LineSpacing::Exactly(h)) if h.is_finite() => p.push_str(&format!(" fo:line-height=\"{}\"", pt(h))), + Some(LineSpacing::AtLeast(h)) if h.is_finite() => p.push_str(&format!(" style:line-height-at-least=\"{}\"", pt(h))), + _ => {} + } + p +} + fn parent_style(k: Kind) -> &'static str { match k { Kind::Heading(1) => "Heading_20_1", @@ -171,6 +196,7 @@ impl Writer { if p.keep_next { props.push_str(" fo:keep-with-next=\"always\""); } + props.push_str(&spacing_props(&p.spacing)); if p.kind == Kind::Rule { props.push_str(" fo:border-bottom=\"0.75pt solid #a0a0a0\" fo:padding-bottom=\"1pt\""); } diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index c46d294de5..8f612b627a 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -1,6 +1,6 @@ # File-format parity with Microsoft Word -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-31 · **Change:** trivial (a paragraph naming an undefined style is formatted as Normal, as in Word, #5077); previously trivial (a hyperlinked TOC writes its page numbers as `PAGEREF _Toc… \h` fields, as Word does, #5097); previously trivial (HTML import ends the empty comments and at once, #5040); previously trivial (chart and SmartArt text the font lacks is drawn in a fallback face, so Chinese labels no longer show boxes, #4720); previously trivial (bibliography sources keep a corporate author (`b:Corporate`): read, written, cited and listed whole, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (note conversion preserves content with an ordered reference/run walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (built-in heading styles have their outline level without `w:outlineLvl`, #4696); previously trivial (DOCX comment ranges inside footnotes and endnotes are kept on read, #4548); previously trivial (RTF import and export keep Windows and Enhanced Metafile pictures (`\wmetafile`, `\emfblip`), #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (DOCX parts stored under another casing keep their embedded fonts through a save, and a private save of a kept glossary escapes the attribute values it rewrites; previously trivial: the `.doc` reader logs what its copy budget leaves out, once a kind per document; importers bound hostile expansion: font names, links, ODS repeats, LaTeX wrappers, Markdown delimiters, #3692/#3713; previously trivial: repeated DOCX style IDs: save/reopen regression; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: Arabic list labels: `arabicAlpha` and `arabicAbjad` draw the 28 Arabic letters (alphabetical and abjad order), doubling past the last letter; Hebrew, Hindi, Thai and Vietnamese still show decimal (`doc::section::tests::arabic_letters_count_in_both_orders`); previously trivial: localized TOC style ids read as the TOC styles, and a TOC in columns keeps its layout through Update Table, #3889; previously trivial: cu20we: outline caps; previously trivial: a relationship the markup names without the prefix is read as it wrote: an unqualified `id` on an element that carries a relationship names that relationship, and the writer keeps the attribute with the id the new package holds the part under; an ordinary `id` — a VML shape's, a drawing's — names no part; previously trivial: the reader takes the relationship ids the markup refers to from the relationship attributes of the main document it has already parsed, so a document with many relationships opens in the time its part takes, #3411; previously trivial: a table cell span from a `.json` file stays within 63 columns through Insert Column and Merge Cells, #3637; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: `majorBidi`/`minorBidi` theme fonts follow the settings' theme font language, as measured in Word; previously trivial: Word signature lines are kept on save, still signable; previously trivial: Times New Roman keeps Liberation Serif under the roman-font rule; previously trivial: shape gradients and transparency, #1500; previously trivial: a folder that keeps no Unix modes, such as a NAS share mounted by GVFS, still takes a save, #3003; previously trivial: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: table style band sizes written, a style without them unbanded as in Word, #3415; previously trivial: HTML export writes ruby (furigana, phonetic guides) and HTML import reads it back, #1681; previously trivial: RTF import decodes the East Asian multibyte code pages, #1653; previously trivial: PDF export: lam-alef drawn as two glyphs at one x extracts in reading order, #2310; previously trivial: native .json stores pictures and kept parts as base64, #586 follow-up; previously trivial: ODT headers and footers read from each master page, #863; previously trivial: RTF import compares heading and code formatting with the file's stylesheet; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: Word XML and web pages saved as `.doc` open by what they hold, #2853; previously trivial: a VML shape in a header cell is always placed on the page, user-checked in Word for Mac, #1775; previously trivial: `w:sym` characters keep their symbol font when the text's font changes and are written back as `w:sym`, #2036; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: two lines in one and horizontal in vertical (`w:eastAsianLayout`) kept on save; previously trivial: PDF outline targets follow leading page breaks; previously trivial: positional tabs `w:ptab` read and kept on save, #2367; previously trivial: DOCX: mixed right-to-left runs split and marked `w:rtl` with their language, #1543; previously trivial: `w:doNotTrackMoves` round-trips and turns move tracking off; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: DOCX glossary documents (building blocks) read, kept and written; previously trivial: cached field results keep their visible run formatting; previously trivial: cached REF results retain hyperlink destinations; previously trivial: CLI conversions can explicitly refresh fields; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: a pasted HTML `
` is an indented paragraph, not the Quote style, #1270); previously trivial: every table style part round-trips, #714; previously trivial: VML freeforms read and kept on save; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: `flowChartOr` draws its circle and cross spokes, as in Word; previously trivial: RTF: Mac OS Roman and DOS text, #1653; previously trivial: VML text boxes keep their internal margins, fit to their text and vertical alignment; hidden drawings not shown; previously trivial: PDF export draws horizontal and vertical lines inside groups, #1474; previously trivial: symbol-font text exported as Unicode, #776; previously trivial: text-box margins, Resize shape to fit text and freeforms in groups, #743; previously trivial: formatted paste from the system clipboard's HTML and RTF, #687; previously trivial: independent complex-script formatting inheritance and round-trip regression tests; previously trivial: import/export fixes for TXT, RTF, ODT, HTML and LaTeX, #433–#440; previously major: first version; every format Word reads or writes, measured from the readers and writers on origin/main) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4, plus Word for Windows' extra formats +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-31 · **Change:** trivial (ODT paragraph spacing, indents and line spacing read and written); previously trivial (a paragraph naming an undefined style is formatted as Normal, as in Word, #5077); previously trivial (a hyperlinked TOC writes its page numbers as `PAGEREF _Toc… \h` fields, as Word does, #5097); previously trivial (HTML import ends the empty comments and at once, #5040); previously trivial (chart and SmartArt text the font lacks is drawn in a fallback face, so Chinese labels no longer show boxes, #4720); previously trivial (bibliography sources keep a corporate author (`b:Corporate`): read, written, cited and listed whole, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (note conversion preserves content with an ordered reference/run walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (built-in heading styles have their outline level without `w:outlineLvl`, #4696); previously trivial (DOCX comment ranges inside footnotes and endnotes are kept on read, #4548); previously trivial (RTF import and export keep Windows and Enhanced Metafile pictures (`\wmetafile`, `\emfblip`), #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (DOCX parts stored under another casing keep their embedded fonts through a save, and a private save of a kept glossary escapes the attribute values it rewrites; previously trivial: the `.doc` reader logs what its copy budget leaves out, once a kind per document; importers bound hostile expansion: font names, links, ODS repeats, LaTeX wrappers, Markdown delimiters, #3692/#3713; previously trivial: repeated DOCX style IDs: save/reopen regression; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: Arabic list labels: `arabicAlpha` and `arabicAbjad` draw the 28 Arabic letters (alphabetical and abjad order), doubling past the last letter; Hebrew, Hindi, Thai and Vietnamese still show decimal (`doc::section::tests::arabic_letters_count_in_both_orders`); previously trivial: localized TOC style ids read as the TOC styles, and a TOC in columns keeps its layout through Update Table, #3889; previously trivial: cu20we: outline caps; previously trivial: a relationship the markup names without the prefix is read as it wrote: an unqualified `id` on an element that carries a relationship names that relationship, and the writer keeps the attribute with the id the new package holds the part under; an ordinary `id` — a VML shape's, a drawing's — names no part; previously trivial: the reader takes the relationship ids the markup refers to from the relationship attributes of the main document it has already parsed, so a document with many relationships opens in the time its part takes, #3411; previously trivial: a table cell span from a `.json` file stays within 63 columns through Insert Column and Merge Cells, #3637; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: `majorBidi`/`minorBidi` theme fonts follow the settings' theme font language, as measured in Word; previously trivial: Word signature lines are kept on save, still signable; previously trivial: Times New Roman keeps Liberation Serif under the roman-font rule; previously trivial: shape gradients and transparency, #1500; previously trivial: a folder that keeps no Unix modes, such as a NAS share mounted by GVFS, still takes a save, #3003; previously trivial: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: table style band sizes written, a style without them unbanded as in Word, #3415; previously trivial: HTML export writes ruby (furigana, phonetic guides) and HTML import reads it back, #1681; previously trivial: RTF import decodes the East Asian multibyte code pages, #1653; previously trivial: PDF export: lam-alef drawn as two glyphs at one x extracts in reading order, #2310; previously trivial: native .json stores pictures and kept parts as base64, #586 follow-up; previously trivial: ODT headers and footers read from each master page, #863; previously trivial: RTF import compares heading and code formatting with the file's stylesheet; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: Word XML and web pages saved as `.doc` open by what they hold, #2853; previously trivial: a VML shape in a header cell is always placed on the page, user-checked in Word for Mac, #1775; previously trivial: `w:sym` characters keep their symbol font when the text's font changes and are written back as `w:sym`, #2036; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: two lines in one and horizontal in vertical (`w:eastAsianLayout`) kept on save; previously trivial: PDF outline targets follow leading page breaks; previously trivial: positional tabs `w:ptab` read and kept on save, #2367; previously trivial: DOCX: mixed right-to-left runs split and marked `w:rtl` with their language, #1543; previously trivial: `w:doNotTrackMoves` round-trips and turns move tracking off; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: DOCX glossary documents (building blocks) read, kept and written; previously trivial: cached field results keep their visible run formatting; previously trivial: cached REF results retain hyperlink destinations; previously trivial: CLI conversions can explicitly refresh fields; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: a pasted HTML `
` is an indented paragraph, not the Quote style, #1270); previously trivial: every table style part round-trips, #714; previously trivial: VML freeforms read and kept on save; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: `flowChartOr` draws its circle and cross spokes, as in Word; previously trivial: RTF: Mac OS Roman and DOS text, #1653; previously trivial: VML text boxes keep their internal margins, fit to their text and vertical alignment; hidden drawings not shown; previously trivial: PDF export draws horizontal and vertical lines inside groups, #1474; previously trivial: symbol-font text exported as Unicode, #776; previously trivial: text-box margins, Resize shape to fit text and freeforms in groups, #743; previously trivial: formatted paste from the system clipboard's HTML and RTF, #687; previously trivial: independent complex-script formatting inheritance and round-trip regression tests; previously trivial: import/export fixes for TXT, RTF, ODT, HTML and LaTeX, #433–#440; previously major: first version; every format Word reads or writes, measured from the readers and writers on origin/main) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4, plus Word for Windows' extra formats Every format Word opens or saves, what WordCraft does with it, and how it's tested. Fidelity is the share of a typical real-world file's content and formatting that survives (read: shows correctly; @@ -57,7 +57,7 @@ Weights are the share of a Word user's file traffic (estimate). Read/write: ✅ | PDF export | 8% | W | — | ✅ | ~80% | 6–10 | `crates/pdf/src/tests.rs` | krilla: embedded subsets, selectable text incl. field results (#133); Hebrew and Arabic copy and search as whole words (right-to-left glyphs written in visual order, #1383, also with fonts that draw lam-alef as two glyphs at one x, #2310); All Caps and Small Caps text extracts as the capitals shown, as from Word's PDFs, and never changes the case of other text (#1473), links, outline, tagging. Outline destinations follow leading page breaks to the heading text. Fonts it can't subset become outlines (#132). Print Markup (#589): comments in balloons beside each page (scaled to its paper) and tracked changes; HTML/Markdown/text copies end with a comment list. Horizontal and vertical lines inside groups are drawn (#1474). No PDF/A option, no "best for printing/online" choice | | PDF open (PDF Reflow, Word for Windows) | 1% | R | ❌ | — | 0% | 30–50 | — | Converting a PDF into an editable document. A sibling app (PdfCraft) may supply the parser | | `.rtf` Rich Text Format | 3% | R/W | 🟡 | 🟡 | ~50% | 10–15 | `crates/formats/src/tests.rs` | ~1,500 lines: text, character/paragraph formatting, styles, lists, tables, pictures (`\shppict`, #154; Windows and Enhanced Metafiles, `\wmetafile` and `\emfblip`, read and written, #4259), fields, bookmarks. 8-bit text in the document's code page (`\ansicpg`) or the current font's (`\fcharset`, `\cpg`): Cyrillic, Central European, Greek, Turkish, Hebrew, Arabic, Baltic, Vietnamese, Thai (was Windows-1252 only), Mac OS Roman (`\mac`, `\fcharset77`) and DOS code pages (`\pc` 437, `\pca` 850, `\cpg` 437/737/850/852/857/866, `\fcharset254`) (#1653). East Asian multibyte code pages decoded as runs: 932 Shift-JIS (`\fcharset128`, Japanese), 936 GBK (134, Simplified Chinese), 949 UHC/windows-949 (129, Korean) and 950 Big5 (136, Traditional Chinese) through `\ansicpg`, `\cpg` or the font's charset, escaped or raw, a lead byte and its trail byte together; invalid sequences read as U+FFFD, and a run that starts `EF BB BF` is text, not a BOM (#1653). Pasted from the system clipboard where the platform offers it (Windows, Wayland) when there is no HTML (#687). Tracked insertions and deletions read and written (`\revtbl`, `\revised`/`\deleted`, authors, `\revdttm` dates), so Accept/Reject All work after opening or a round trip; tracked paragraph marks (`\par` inside `\revised`/`\deleted` groups, including a `\sect` inside a deletion) and character and paragraph formatting revisions (`\crauth`/`\crdate` with `\oldcprops`, `\prauth`/`\prdate` with `\oldpprops`) read and written; section and table formatting revisions (`\oldsprops`, `\oldtprops`) not yet. Sections on import (#1378): each `\sect` ends a section with its own page size and orientation, margins, header/footer distance, columns, break type, page-number restart, start and format, title page and headers/footers (those it leaves out link to the previous); export writes every section too (break type, page size and orientation, margins, header/footer distance, columns, page numbering, title page; not headers/footers). Thin: comments, notes, RTL | -| `.odt` OpenDocument Text | 2% | R/W | 🟡 | 🟡 | ~50% | 8–12 | `crates/formats/src/tests.rs`, `crates/formats/src/odt.rs` unit tests, `crates/engine/tests/it/odt_sections.rs` | Styles, lists (#87), tables, pictures, page breaks (#86), headers and footers of each master page (every, left and first page) read on open, not yet written (#863), keep with next (#2813), an embedded formula object (`draw:object`) is read from its own `content.xml`'s MathML, same as HTML (#3099; export still drops equations, as before), page size, orientation and margins (#810), sections: one per master-page change on a paragraph or table (next page, with its own page size, orientation, margins, columns and page-numbering restart) and per columned `text:section` (continuous, with its columns and separator line) (#1379); export writes them back: a master page and page layout per new-page section (size, orientation, margins, columns, page-numbering restart on the first paragraph or table), a `text:section` with its columns per continuous section whose columns differ from the page's (one column included), a list cut by a section break goes on numbering. Limits: a continuous section's page-numbering restart isn't written (ODF applies it only with a master page); a continuous section with the page's columns merges into the section before it on reopening; even and odd page starts are written as plain new pages; a tracked deletion of a section break is lost; an empty last section is dropped; tracked insertions and deletions with author and date, inserted and deleted paragraph marks and text inserted then deleted included (ODF change tracking; format changes read as none; pictures and tables inside a tracked deletion are dropped). No footnotes, comments, fields or RTL | +| `.odt` OpenDocument Text | 2% | R/W | 🟡 | 🟡 | ~50% | 8–12 | `crates/formats/src/tests.rs`, `crates/formats/src/odt.rs` unit tests, `crates/engine/tests/it/odt_sections.rs` | Styles, lists (#87), tables, pictures, page breaks (#86), paragraph spacing, indents and line spacing (proportional, fixed, at least; leading read as a proportional approximation) both ways, with the default and Standard styles' spacing as the document defaults (ODT's are none, not Word's 8 pt after and 1.15 lines), headers and footers of each master page (every, left and first page) read on open, not yet written (#863), keep with next (#2813), an embedded formula object (`draw:object`) is read from its own `content.xml`'s MathML, same as HTML (#3099; export still drops equations, as before), page size, orientation and margins (#810), sections: one per master-page change on a paragraph or table (next page, with its own page size, orientation, margins, columns and page-numbering restart) and per columned `text:section` (continuous, with its columns and separator line) (#1379); export writes them back: a master page and page layout per new-page section (size, orientation, margins, columns, page-numbering restart on the first paragraph or table), a `text:section` with its columns per continuous section whose columns differ from the page's (one column included), a list cut by a section break goes on numbering. Limits: a continuous section's page-numbering restart isn't written (ODF applies it only with a master page); a continuous section with the page's columns merges into the section before it on reopening; even and odd page starts are written as plain new pages; a tracked deletion of a section break is lost; an empty last section is dropped; tracked insertions and deletions with author and date, inserted and deleted paragraph marks and text inserted then deleted included (ODF change tracking; format changes read as none; pictures and tables inside a tracked deletion are dropped). No footnotes, comments, fields or RTL | | `.txt` Plain text | 2% | R/W | ✅ | ✅ | ~95% | 1–2 | unit tests | Encoding detection. Files with other extensions (`.m3u`/`.m3u8` playlists, logs, config files, no extension) open as text when they are text, Word and RTF files by their signature; binary files are refused (#569). A Word or RTF extension that contradicts the content also opens by signature, as in Word: RTF saved as `.doc` (hh.ru résumés, accounting exports), a `.docx` renamed `.doc`, a Word 97-2003 file renamed `.docx`; a Word XML Document (Word 2003 XML or Flat OPC) or a web page (``, ``) under a `.doc`, `.docx` or `.rtf` name opens as what it is (#2853, `tests::word_xml_and_web_pages_saved_as_doc_open_by_content`). Word's encoding choice dialog and line-ending options missing; such files save only through Save As | | `.htm/.html` Web Page and Web Page, Filtered | 1% | R/W | 🟡 | 🟡 | ~60% | 6–10 | `crates/formats/src/tests.rs` | Tables (#102), relative pictures embedded (#113), missing `` tolerated (#154); the empty comments `` and `` end at once instead of swallowing the rest of the page (#5040). Header cells (``) bold unless their CSS says otherwise; `text-align` on a cell, or inherited from its row, row group or table, aligns the cell's paragraphs (#1381). HTML gives no page size, so it opens on the region's paper (A4 or Letter), as Markdown and plain text do; `@page` isn't read (#1381). No Word "Web Page" (with `_files` folder) round-trip. The same reader takes HTML from the system clipboard: Paste keeps another program's formatting (a Word document, a web page; Windows' "HTML Format" header understood), Merge Formatting keeps its emphasis (#687). A pasted `
` (LibreOffice's paragraph indented on both sides) becomes a Normal paragraph indented by the page's `blockquote` margins, else 0.5 in each side; opening an .html file keeps it as the Quote style (#1270). Equations are written as MathML (with the linear format as a UnicodeMath annotation) and read back from MathML: our annotation, else the presentation elements, else a TeX annotation (#816). A copy of one equation alone also goes in the clipboard's MathML formats (Windows "MathML" and "MathML Presentation", macOS `public.mathml.presentation` and `public.mathml`; W3C MathML 3 §6.2.4), which Microsoft documents Word reading first when it pastes math (#1297; the paste into Word is not yet verified); Ruby (furigana, phonetic guides) is written as `basereading` in the base text's formatting and read back from ``/``/`` (`` fallback parentheses dropped; malformed markup keeps its text), so docx/doc → HTML → doc keeps the annotation; HTML states no alignment, size or raise, so the Phonetic Guide's defaults apply (#1681) CRLF and lone CR input newlines normalize before tokenization, preserving preformatted lines and leaving decoded character references separate (#4810). | | `.mht/.mhtml` Single File Web Page | <1% | R/W | ❌ | ❌ | 0% | 3–5 | — | | @@ -279,6 +279,7 @@ Custom document properties retain their bookmark link targets through DOCX saves | Date | Change | Summary | |---|---|---| +| 2026-10-31 | trivial | ODT: paragraph spacing, indents and line spacing (`fo:margin-*`, `fo:text-indent`, `fo:line-height`, `style:line-height-at-least`; `style:line-spacing` leading as a proportional approximation) read from automatic and common styles and written back; the default and Standard paragraph styles give the document defaults, so an ODT paragraph without spacing has none instead of Word's 8 pt after and 1.15 lines. `formats::odt::tests_more::spacing` | | 2026-10-31 | trivial | DOCX: a paragraph naming a style the file doesn't define is formatted as Normal, as in Word (#5077) | | 2026-10-31 | trivial | DOCX: a hyperlinked table of contents writes each page number as a `PAGEREF _Toc… \h` field to the heading's bookmark, as Word does, so updating the fields in Word moves the numbers with the headings; they were plain text (checked in Word for Mac 16.93: with the headings moved a page down, the three fields update from 2, 3, 4 to 3, 4, 5). `engine::tests::toc_page_numbers_are_pageref_fields` | | 2026-10-31 | trivial | HTML import ends the empty comments `` and `` at once, as the HTML spec does, instead of swallowing the rest of the page (#5040) | From 3402e9b64472ea33f4268a21f9a7ecefd9eb5500 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 23:13:26 +0000 Subject: [PATCH 033/308] Gap 19: ODT headers and footers written; text fields read and written MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Export writes each section's headers and footers into its master page (style:header, -left for even pages, -first for a different first page; an empty element where Word shows nothing), with their automatic styles in styles.xml under names of their own. Word's page-edge header and footer distances become ODF's page margin plus header/footer band height, and import converts back, so headers sit where LibreOffice draws them. Text fields (ODF 1.3 §7: page number and count, date, time, document information, bookmark references) open as WordCraft fields with their shown text as the result, and are written back as ODF fields. Fixed dates and fields without a Word equivalent stay text; Word fields ODF lacks are written as their result. The flow model carries fields only when asked (FlowOpts), so the other formats are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GBVQLyDnrG8Tn1yDQcUabC --- ROADMAP.md | 3 +- crates/formats/src/html/export.rs | 9 ++ crates/formats/src/latex/export.rs | 4 + crates/formats/src/markdown/export.rs | 1 + crates/formats/src/model.rs | 6 +- crates/formats/src/model/from_doc.rs | 33 +++- crates/formats/src/model/to_doc.rs | 10 ++ crates/formats/src/model/types.rs | 24 ++- crates/formats/src/odt.rs | 1 + crates/formats/src/odt/body.rs | 71 ++++++++- crates/formats/src/odt/fields.rs | 80 ++++++++++ crates/formats/src/odt/import.rs | 79 ++++++++-- crates/formats/src/odt/limits.rs | 3 + crates/formats/src/odt/package.rs | 99 +++++++++++- crates/formats/src/odt/tests_more.rs | 2 + crates/formats/src/odt/tests_more/fields.rs | 153 ++++++++++++++++++ crates/formats/src/odt/tests_more/headers.rs | 154 +++++++++++++++++++ crates/formats/src/odt/writer.rs | 58 +++++-- crates/formats/src/rtf/export.rs | 1 + docs/file-format-parity.md | 5 +- docs/gaps.md | 4 +- 21 files changed, 745 insertions(+), 55 deletions(-) create mode 100644 crates/formats/src/odt/fields.rs create mode 100644 crates/formats/src/odt/tests_more/fields.rs create mode 100644 crates/formats/src/odt/tests_more/headers.rs diff --git a/ROADMAP.md b/ROADMAP.md index 15114df637..7718cf19bc 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-29 · **Change:** trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -140,6 +140,7 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm ## Progress log +- 2026-10-31: ODT: headers and footers are saved (they were dropped on every save), each page's header sits where LibreOffice puts it on open, and page numbers, page counts, dates and other text fields open as fields that update and are saved as ODF fields (gap 19). Readiness unchanged. - 2026-10-31: ODT: paragraph spacing, indents and line spacing are read and written, and an ODT's own default spacing (none, single lines) replaces Word's 8 pt after and 1.15 lines, so LibreOffice documents keep their vertical rhythm on open and save (gap 19). Readiness unchanged. - 2026-10-31: DOCX: a paragraph naming a style the file doesn't define is formatted as Normal, as in Word, not with the document defaults alone, so a Pandoc footer naming an undefined `Footer` style is as tall as Word's and no longer pushes the last paragraph to the next page (#5077). Readiness unchanged. - 2026-10-31: A table of contents writes its page numbers as `PAGEREF` fields to the headings' bookmarks, as Word does, so updating the fields in Word moves the numbers with the headings; they were plain text (#5097). diff --git a/crates/formats/src/html/export.rs b/crates/formats/src/html/export.rs index 0862ca04d0..84aae0a8fd 100644 --- a/crates/formats/src/html/export.rs +++ b/crates/formats/src/html/export.rs @@ -137,6 +137,15 @@ pub fn inlines_html(inlines: &[Inline]) -> String { )); } Inline::Anchor(a) => o.push_str(&format!("", esc(a))), + Inline::Field { result, fmt, .. } => { + let (open, close) = fmt_tags(fmt); + let at_start = o.is_empty(); + o.push_str(&open); + o.push_str(&text_html(result, at_start)); + for c in close.iter().rev() { + o.push_str(c); + } + } // No picture to show: the browser shows the alt text in its place. Inline::Figure(alt) => o.push_str(&format!("\"{}\"", esc(alt))), // MathML, which browsers, Word and LibreOffice show as an equation (#816). diff --git a/crates/formats/src/latex/export.rs b/crates/formats/src/latex/export.rs index 00e9f4577f..77aa3a054c 100644 --- a/crates/formats/src/latex/export.rs +++ b/crates/formats/src/latex/export.rs @@ -241,6 +241,10 @@ impl Writer { } Inline::Anchor(name) => o.push_str(&format!("\\hypertarget{{{}}}{{}}", escape_url(name))), Inline::Figure(_) => {} + Inline::Field { result, fmt, .. } => { + let s = self.span(result, fmt); + push_apart(&mut o, &s); + } // LaTeX keeps no ruby (no package): its base text, in the run's formatting. Inline::Ruby { base, fmt, .. } => { let s = self.span(base, fmt); diff --git a/crates/formats/src/markdown/export.rs b/crates/formats/src/markdown/export.rs index c1dbe52678..7dc8856081 100644 --- a/crates/formats/src/markdown/export.rs +++ b/crates/formats/src/markdown/export.rs @@ -134,6 +134,7 @@ fn inlines_md_impl(inlines: &[Inline], in_table: bool) -> String { } Inline::Figure(alt) => pieces.push((String::new(), Fmt::default(), Some(format!("![{}]()", escape(alt))))), Inline::Equation { .. } => {} + Inline::Field { result, fmt, .. } => pieces.push((result.clone(), fmt.clone(), None)), // Read as its base text by `ruby_as_text` above. Inline::Ruby { .. } => {} } diff --git a/crates/formats/src/model.rs b/crates/formats/src/model.rs index 75e7d0d5e2..39e00cccc9 100644 --- a/crates/formats/src/model.rs +++ b/crates/formats/src/model.rs @@ -20,12 +20,12 @@ mod to_doc; mod types; pub use color::parse_color; -pub use from_doc::{ListCounter, flow_paras, from_doc, from_doc_tracked, is_mono, para_kind}; +pub use from_doc::{ListCounter, flow_paras, from_doc, from_doc_tracked, from_doc_with, is_mono, para_kind, part_flow}; pub use media::{base64_decode, base64_encode, data_uri, decode_bounded, image_px, make_img, mime_of, percent_decode, sized_img, sniff_image}; pub use to_doc::{char_props, part_blocks, to_doc, to_doc_in}; pub use types::{ - CODE_STYLE, Cell, FBlock, FTable, Flow, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, MAX_FONT_NAME, MAX_LINK, MONO_FONT, Meta, Para, - RUBY_BASE_SIZE, RevMark, Spacing, clean_text, equations_as_text, kept_font, kept_link, ruby_as_text, ruby_object, + CODE_STYLE, Cell, FBlock, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, MAX_FONT_NAME, MAX_LINK, MONO_FONT, + Meta, Para, RUBY_BASE_SIZE, RevMark, Spacing, clean_text, equations_as_text, kept_font, kept_link, ruby_as_text, ruby_object, }; pub(crate) use types::{Dropped, begin_import, dropped}; diff --git a/crates/formats/src/model/from_doc.rs b/crates/formats/src/model/from_doc.rs index 1ea0053093..b4abdac04b 100644 --- a/crates/formats/src/model/from_doc.rs +++ b/crates/formats/src/model/from_doc.rs @@ -1,4 +1,4 @@ -use super::types::{Cell, FBlock, FTable, Flow, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, Meta, Para, RevMark, Spacing}; +use super::types::{Cell, FBlock, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, Meta, Para, RevMark, Spacing}; use super::{ Arc, Block, Blocks, CharProps, Document, InlineObject, NumFormat, OBJ, PAGE_BREAK, ParaProps, Paragraph, StyleKind, Table, TextColor, Underline, VMerge, VertAlign, @@ -131,12 +131,13 @@ fn direct_spacing(p: &wordcraft_doc::props::ParaProps) -> Spacing { /// A document paragraph as flow paragraphs: a page break character inside the text ends the /// paragraph, and the text after it starts a new one that carries `page_break`. pub fn flow_paras(doc: &Document, p: &Paragraph) -> Vec { - flow_paras_in(doc, p, false) + flow_paras_in(doc, p, FlowOpts::default()) } /// [`flow_paras`]; `tracked` keeps tracked changes: deleted text stays, and text carries its /// insertion and deletion marks. -fn flow_paras_in(doc: &Document, p: &Paragraph, tracked: bool) -> Vec { +fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { + let tracked = opts.tracked; let (kind, list) = para_kind(doc, p); let mut done: Vec = Vec::new(); let borders = if kind == Kind::Rule { None } else { p.props.borders.filter(|b| b.any_visible()) }; @@ -250,6 +251,12 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, tracked: bool) -> Vec { // attributes (alignment, size, raise) stay in the document model. out.inlines.push(Inline::Ruby { base: base.clone(), ruby: ruby.clone(), fmt: f.clone() }); } + Some(InlineObject::Field { instr, result, .. }) if opts.fields => { + if !buf.is_empty() { + out.push_text(&std::mem::take(&mut buf), &f); + } + out.inlines.push(Inline::Field { instr: instr.clone(), result: result.clone(), fmt: f.clone() }); + } Some(InlineObject::BookmarkStart { name }) if name != "_GoBack" => { if !buf.is_empty() { out.push_text(&std::mem::take(&mut buf), &f); @@ -281,7 +288,7 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, tracked: bool) -> Vec { done } -fn flow_blocks(doc: &Document, bl: &Blocks, depth: usize, tracked: bool) -> Vec { +fn flow_blocks(doc: &Document, bl: &Blocks, depth: usize, tracked: FlowOpts) -> Vec { let mut out = Vec::new(); for b in bl { match &**b { @@ -299,7 +306,7 @@ fn flow_blocks(doc: &Document, bl: &Blocks, depth: usize, tracked: bool) -> Vec< out } -fn flow_table(doc: &Document, t: &Table, depth: usize, tracked: bool) -> FTable { +fn flow_table(doc: &Document, t: &Table, depth: usize, tracked: FlowOpts) -> FTable { let mut rows = Vec::new(); for (ri, r) in t.rows.iter().enumerate() { let header = r.props.header || (ri == 0 && t.props.look.header_row && t.rows.len() > 1); @@ -338,16 +345,26 @@ fn flow_table(doc: &Document, t: &Table, depth: usize, tracked: bool) -> FTable /// The document body as a flow. pub fn from_doc(doc: &Document) -> Flow { - flow_of(doc, false) + flow_of(doc, FlowOpts::default()) } /// [`from_doc`] keeping tracked changes: deleted text stays, and text carries its insertion and /// deletion marks ([`Fmt::ins`], [`Fmt::del`]), for formats that can write revisions (RTF). pub fn from_doc_tracked(doc: &Document) -> Flow { - flow_of(doc, true) + flow_of(doc, FlowOpts { tracked: true, ..Default::default() }) } -fn flow_of(doc: &Document, tracked: bool) -> Flow { +/// [`from_doc`] keeping what `opts` asks for. +pub fn from_doc_with(doc: &Document, opts: FlowOpts) -> Flow { + flow_of(doc, opts) +} + +/// `blocks` of `doc` (a header, footer or note) as flow blocks, keeping what `opts` asks for. +pub fn part_flow(doc: &Document, blocks: &Blocks, opts: FlowOpts) -> Vec { + flow_blocks(doc, blocks, 0, opts) +} + +fn flow_of(doc: &Document, tracked: FlowOpts) -> Flow { let c = &doc.core; Flow { blocks: flow_blocks(doc, &doc.body, 0, tracked), diff --git a/crates/formats/src/model/to_doc.rs b/crates/formats/src/model/to_doc.rs index 2f4fa13613..c9fec5b902 100644 --- a/crates/formats/src/model/to_doc.rs +++ b/crates/formats/src/model/to_doc.rs @@ -218,6 +218,16 @@ impl<'a> Builder<'a> { append(&mut out, "", &CharProps::default(), Some(InlineObject::BookmarkEnd { name: name.clone() })); } Inline::Figure(_) => {} + Inline::Field { instr, result, fmt } => { + let cp = self.budget(char_props(fmt)); + let (instr, result) = (clean_text(instr), clean_text(result)); + if instr.trim().is_empty() { + append(&mut out, &result, &cp, None); + } else { + let obj = InlineObject::Field { instr, result, locked: false, dirty: false, code: false, form: String::new() }; + append(&mut out, "", &cp, Some(obj)); + } + } Inline::Equation { linear, display } => { let linear = clean_text(linear); if !linear.is_empty() { diff --git a/crates/formats/src/model/types.rs b/crates/formats/src/model/types.rs index 1b52b2e68f..056fe0b942 100644 --- a/crates/formats/src/model/types.rs +++ b/crates/formats/src/model/types.rs @@ -164,6 +164,24 @@ pub enum Inline { ruby: String, fmt: Fmt, }, + /// A field (page number, date, document property, cross-reference): its Word field code + /// (`PAGE`, `REF name \h`…) and the result it last showed, in its run's formatting. Only + /// flows asked for fields ([`FlowOpts::fields`]) hold them; formats without fields write the + /// result as text. + Field { + instr: String, + result: String, + fmt: Fmt, + }, +} + +/// What [`super::from_doc_with`] keeps besides text and formatting. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct FlowOpts { + /// Tracked changes: deleted text stays, and text carries its insertion and deletion marks. + pub tracked: bool, + /// Fields as [`Inline::Field`] (otherwise their result is text). + pub fields: bool, } /// Base text size (points) a phonetic guide falls back to when the source states none: the @@ -269,7 +287,9 @@ impl Para { let mut s = String::new(); for i in &self.inlines { match i { - Inline::Text(t, _) | Inline::Equation { linear: t, .. } | Inline::Ruby { base: t, .. } => s.push_str(t), + Inline::Text(t, _) | Inline::Equation { linear: t, .. } | Inline::Ruby { base: t, .. } | Inline::Field { result: t, .. } => { + s.push_str(t) + } Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) => {} } } @@ -285,7 +305,7 @@ impl Para { Some(font) if wordcraft_doc::symbol_fonts::is_symbol_font(font) => s.push_str(&wordcraft_doc::symbol_fonts::map_text(font, t)), _ => s.push_str(t), }, - Inline::Equation { linear: t, .. } | Inline::Ruby { base: t, .. } => s.push_str(t), + Inline::Equation { linear: t, .. } | Inline::Ruby { base: t, .. } | Inline::Field { result: t, .. } => s.push_str(t), Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) => {} } } diff --git a/crates/formats/src/odt.rs b/crates/formats/src/odt.rs index f70b226efd..0c511bbec7 100644 --- a/crates/formats/src/odt.rs +++ b/crates/formats/src/odt.rs @@ -30,6 +30,7 @@ use wordcraft_doc::{Align, Document, RevisionKind, Rgb, SectionProps}; use crate::model::{self, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, ListInfo, Meta, Para, RevMark, make_img, mime_of}; mod body; +mod fields; mod import; mod limits; mod package; diff --git a/crates/formats/src/odt/body.rs b/crates/formats/src/odt/body.rs index b8326fd7bf..4d68945e06 100644 --- a/crates/formats/src/odt/body.rs +++ b/crates/formats/src/odt/body.rs @@ -1,5 +1,5 @@ use super::import::Pages; -use super::limits::{MAX_INFO_CHARS, MAX_OPEN_INSERTIONS, MAX_REGIONS, MAX_SECTIONS, MAX_XML_DEPTH}; +use super::limits::{MAX_FIELD_RESULT, MAX_INFO_CHARS, MAX_OPEN_INSERTIONS, MAX_REGIONS, MAX_SECTIONS, MAX_XML_DEPTH}; use super::styles::Styles; use super::xml::{Attrs, Ev, events, get, length, local}; use super::{Cell, FBlock, FTable, Fmt, HashMap, Inline, Kind, ListInfo, Para, RevMark, RevisionKind, SectionProps, SectionStart, make_img, model}; @@ -157,6 +157,21 @@ pub(super) struct Body<'a> { del: Option, /// Insertions open in the body: (region id, revision). active_ins: Vec<(String, RevMark)>, + /// The text field being read. + field: Option, +} + +/// A text field being read ([`super::fields`]). +struct OpenField { + /// Its Word field code. + instr: String, + /// Its shown text so far. + result: String, + /// Its element's name and depth. + name: String, + depth: usize, + /// The formatting it is in. + fmt: Fmt, } impl<'a> Body<'a> { @@ -195,6 +210,7 @@ impl<'a> Body<'a> { info_field: None, del: None, active_ins: Vec::new(), + field: None, } } @@ -440,6 +456,22 @@ impl Body<'_> { } return; } + if let Some(OpenField { result, .. }) = &mut self.field { + // A field's shown text, its XML whitespace collapsed; bounded like any one text run. + for c in t.chars() { + if result.len() >= MAX_FIELD_RESULT { + break; + } + if matches!(c, ' ' | '\t' | '\n' | '\r') { + if !result.ends_with(' ') { + result.push(' '); + } + } else { + result.push(c); + } + } + return; + } let Some(p) = &self.para else { return; }; @@ -805,6 +837,17 @@ impl Body<'_> { } fn start_inline(&mut self, q: &str, a: &Attrs) -> bool { + // Inside a field everything is its shown text. + if self.field.is_some() { + return true; + } + if q.starts_with("text:") + && self.frame.is_none() + && let Some(instr) = super::fields::instr_of(local(q), a) + { + self.field = Some(OpenField { instr, result: String::new(), name: q.to_string(), depth: self.depth, fmt: self.cur_fmt() }); + return true; + } match local(q) { "span" => { let base = self.cur_fmt(); @@ -842,9 +885,35 @@ impl Body<'_> { true } + /// Put the field read so far into the paragraph. + fn end_field(&mut self) { + let Some(OpenField { instr, result, mut fmt, .. }) = self.field.take() else { return }; + fmt.ins = self.open_insertion(); + if self.para.is_none() { + self.start_para(None, None); + } + if let Some(p) = &mut self.para { + p.inlines.push(Inline::Field { instr, result: result.trim().to_string(), fmt }); + } + self.xml_space_end = false; + } + fn end(&mut self, q: &str) { self.depth = self.depth.saturating_sub(1); + // Everything inside a field was its text: only the field's own end does anything. An + // outer element ending first (malformed) ends the field, then itself. + if let Some(f) = &self.field { + if self.depth >= f.depth { + return; + } + let own = f.name == q; + self.end_field(); + if own { + return; + } + } if q == "office:text" { + self.end_field(); if self.del.is_some() { // Unclosed deleted content (hostile): drop it. self.close_deletion(); diff --git a/crates/formats/src/odt/fields.rs b/crates/formats/src/odt/fields.rs new file mode 100644 index 0000000000..3c0b972840 --- /dev/null +++ b/crates/formats/src/odt/fields.rs @@ -0,0 +1,80 @@ +//! Text fields (ODF 1.3 §7.3–7.7) and the Word field codes WordCraft keeps them as. +//! +//! Read: a field element becomes a field with that code and the element's text as its result. +//! A fixed date or time (`text:fixed="true"`) is plain text, as is a field with no Word +//! equivalent (sequences, chapters, variables…), so nothing it shows is lost. Written: a field +//! whose code has an ODF element is that element around its result; any other field (or one +//! with a code ODF can't hold) is its result as text. + +use super::writer::x; +use super::xml::{Attrs, get}; + +/// Longest bookmark name a reference field keeps; a longer one leaves the field as text. +const MAX_REF_NAME: usize = 1024; + +/// ODF element (local name) ↔ Word field code, for the fields that take no argument. +const SIMPLE: &[(&str, &str)] = &[ + ("page-count", "NUMPAGES"), + ("date", "DATE"), + ("time", "TIME"), + ("title", "TITLE"), + ("subject", "SUBJECT"), + ("initial-creator", "AUTHOR"), + ("creator", "LASTSAVEDBY"), + ("author-name", "USERNAME"), + ("description", "COMMENTS"), + ("keywords", "KEYWORDS"), + ("file-name", "FILENAME"), + ("word-count", "NUMWORDS"), + ("character-count", "NUMCHARS"), + ("creation-date", "CREATEDATE"), + ("modification-date", "SAVEDATE"), + ("print-date", "PRINTDATE"), +]; + +/// The Word field code for the `text:` field element `local` with attributes `a`, or `None` +/// when it is no field WordCraft keeps (then its text is read as text). +pub(super) fn instr_of(local: &str, a: &Attrs) -> Option { + let fixed = get(a, "text:fixed").is_some_and(|v| v.trim() == "true"); + match local { + // Only the current page's number: "previous" and "next" have no Word equivalent. + "page-number" => (get(a, "text:select-page").is_none_or(|v| v.trim() == "current")).then(|| "PAGE".to_string()), + "bookmark-ref" => { + let name = + get(a, "text:ref-name").map(str::trim).filter(|n| !n.is_empty() && n.len() <= MAX_REF_NAME && !n.contains(char::is_whitespace))?; + Some(match get(a, "text:reference-format").map(str::trim) { + Some("page") => format!("PAGEREF {name} \\h"), + Some("number" | "number-all-superior" | "number-no-superior") => format!("REF {name} \\r \\h"), + _ => format!("REF {name} \\h"), + }) + } + _ if fixed => None, + _ => SIMPLE.iter().find(|(e, _)| *e == local).map(|(_, code)| code.to_string()), + } +} + +/// The ODF field element for Word field code `instr` around `result` (already escaped), or +/// `None` when ODF has none for it. +pub(super) fn element(instr: &str, result: &str) -> Option { + let mut words = instr.split_whitespace(); + let code = words.next()?.to_ascii_uppercase(); + match code.as_str() { + "PAGE" => Some(format!("{result}")), + "REF" | "PAGEREF" => { + let name = words.next().filter(|n| !n.starts_with('\\') && n.len() <= MAX_REF_NAME)?; + let rest: Vec<&str> = words.collect(); + let format = if code == "PAGEREF" { + "page" + } else if rest.iter().any(|s| s.eq_ignore_ascii_case("\\r") || s.eq_ignore_ascii_case("\\n") || s.eq_ignore_ascii_case("\\w")) { + "number" + } else { + "text" + }; + Some(format!("{result}", x(name))) + } + _ => { + let (el, _) = SIMPLE.iter().find(|(_, c)| *c == code)?; + Some(format!("{result}")) + } + } +} diff --git a/crates/formats/src/odt/import.rs b/crates/formats/src/odt/import.rs index 424c3da66e..a4aa795b61 100644 --- a/crates/formats/src/odt/import.rs +++ b/crates/formats/src/odt/import.rs @@ -199,49 +199,102 @@ pub(super) struct Pages { pub(super) first_name: Option, } +/// A page layout's header or footer area (`style:header-style`/`style:footer-style`, ODF 1.3 +/// §16.6–16.7): its minimum height and its spacing from the text, in points. +#[derive(Clone, Copy, Default)] +struct Band { + height: f32, + spacing: f32, +} + +/// A page layout read from `styles.xml`: its properties, columns, and header and footer areas. +#[derive(Default)] +struct Layout { + attrs: Attrs, + columns: Option, + header: Band, + footer: Band, +} + fn read_pages(xml: &[u8]) -> Pages { - let mut layouts: HashMap)> = HashMap::new(); + let mut layouts: HashMap = HashMap::new(); let mut cur: Option = None; - let mut masters: Vec<(String, String)> = Vec::new(); + // Inside a layout's footer style (else its header style). + let mut in_footer = false; + // Master pages: name, layout, whether a header and a footer show. + let mut masters: Vec<(String, String, bool, bool)> = Vec::new(); + let mut in_master = false; events(xml, |ev| match ev { Ev::Start(q, a) => match local(&q) { "page-layout" if q.starts_with("style:") => cur = get(&a, "style:name").map(String::from), + "header-style" => in_footer = false, + "footer-style" => in_footer = true, + "header-footer-properties" => { + if let Some(l) = cur.as_ref().and_then(|n| layouts.get_mut(n)) { + let len = |k: &str| get(&a, k).and_then(length).filter(|v| *v >= 0.0).map(|v| v.min(1584.0)); + let band = if in_footer { &mut l.footer } else { &mut l.header }; + band.height = len("fo:min-height").or_else(|| len("svg:height")).unwrap_or(0.0); + band.spacing = len(if in_footer { "fo:margin-top" } else { "fo:margin-bottom" }).unwrap_or(0.0); + } + } "page-layout-properties" => { if let Some(n) = &cur && layouts.len() < MAX_SECTIONS { - layouts.insert(n.clone(), (a, None)); + layouts.insert(n.clone(), Layout { attrs: a, ..Default::default() }); } } "columns" if q.starts_with("style:") => { if let Some(l) = cur.as_ref().and_then(|n| layouts.get_mut(n)) { - l.1 = Some(columns_of(&a)); + l.columns = Some(columns_of(&a)); } } "column-sep" => { - if let Some(c) = cur.as_ref().and_then(|n| layouts.get_mut(n)).and_then(|l| l.1.as_mut()) { + if let Some(c) = cur.as_ref().and_then(|n| layouts.get_mut(n)).and_then(|l| l.columns.as_mut()) { c.separator = column_sep(&a); } } "master-page" if masters.len() < MAX_SECTIONS => { + in_master = false; if let (Some(n), Some(l)) = (get(&a, "style:name"), get(&a, "style:page-layout-name")) { - masters.push((n.to_string(), l.to_string())); + masters.push((n.to_string(), l.to_string(), false, false)); + in_master = true; + } + } + "header" | "footer" if in_master && q.starts_with("style:") && get(&a, "style:display") != Some("false") => { + if let Some(m) = masters.last_mut() { + if local(&q) == "header" { + m.2 = true; + } else { + m.3 = true; + } } } _ => {} }, - Ev::End(q) => { - if local(&q) == "page-layout" { - cur = None; - } - } + Ev::End(q) => match local(&q) { + "page-layout" => cur = None, + "master-page" => in_master = false, + _ => {} + }, Ev::Text(_) => {} }); let mut pages = Pages::default(); - for (name, layout) in masters { - let Some((a, cols)) = layouts.get(&layout) else { continue }; + for (name, layout, header, footer) in masters { + let Some(Layout { attrs: a, columns: cols, header: hb, footer: fb }) = layouts.get(&layout) else { continue }; let mut sec = SectionProps::default(); apply_layout(a, &mut sec); + // ODF starts a header at the page margin and the text after the header's height and + // spacing; Word measures the header's distance and the text's margin from the edge. + let limit = sec.page_w.max(sec.page_h); + if header { + sec.header = sec.margin_top; + sec.margin_top = (sec.margin_top + hb.height + hb.spacing).min(limit); + } + if footer { + sec.footer = sec.margin_bottom; + sec.margin_bottom = (sec.margin_bottom + fb.height + fb.spacing).min(limit); + } // A one-column layout keeps the default columns (and their gap). if let Some(c) = cols.as_ref().filter(|c| c.count > 1) { sec.columns = c.clone(); diff --git a/crates/formats/src/odt/limits.rs b/crates/formats/src/odt/limits.rs index 48d765588c..8f3607814f 100644 --- a/crates/formats/src/odt/limits.rs +++ b/crates/formats/src/odt/limits.rs @@ -7,6 +7,9 @@ pub(super) const MAX_XML_DEPTH: usize = 512; /// Most sections an import makes (page-style changes and column sections); later ones are ignored. pub(super) const MAX_SECTIONS: usize = 10_000; +/// Longest shown text a field keeps, in bytes. +pub(super) const MAX_FIELD_RESULT: usize = 64 << 10; + /// Most `text:changed-region`s read (hostile input). pub(super) const MAX_REGIONS: usize = 10_000; diff --git a/crates/formats/src/odt/package.rs b/crates/formats/src/odt/package.rs index 9e0bdbb2c6..1d57fe6518 100644 --- a/crates/formats/src/odt/package.rs +++ b/crates/formats/src/odt/package.rs @@ -9,8 +9,62 @@ fn columns_xml(c: &Columns) -> String { /// One master page per entry of `pages` (name, setup), each with its own page layout `pm1`, `pm2`… /// (§16.5, §16.9); the first is the default one, "Standard". -/// `defaults` is the spacing of the document's Normal paragraphs, written as the default style's. -fn styles_xml(pages: &[(String, wordcraft_doc::SectionProps)], body_font: &str, defaults: &model::Spacing) -> String { +/// A master page's headers and footers (ODF 1.3 §16.10), written. +#[derive(Default)] +struct PageParts { + /// The `style:header…`/`style:footer…` elements. + xml: String, + header: bool, + footer: bool, +} + +/// The headers and footers section `sec` shows, written by `w` (its styles go in `styles.xml`): +/// the default one for every page, the even one as left pages' when the document has different +/// odd and even pages, the first one with a different first page. A slot Word leaves empty is an +/// empty element, so the page shows nothing there rather than the default one. +fn page_parts(doc: &Document, sec: &SectionProps, w: &mut Writer) -> PageParts { + let mut out = PageParts::default(); + let even = doc.settings.even_odd_headers; + for (set, name) in [(&sec.headers, "header"), (&sec.footers, "footer")] { + let wanted = set.default.is_some() || (even && set.even.is_some()) || (sec.title_page && set.first.is_some()); + if !wanted { + continue; + } + for (id, el, on) in + [(set.default, name.to_string(), true), (set.even, format!("{name}-left"), even), (set.first, format!("{name}-first"), sec.title_page)] + { + if !on { + continue; + } + let mut body = String::new(); + if let Some(part) = id.and_then(|i| doc.parts.get(&i)) { + let blocks = model::part_flow(doc, &part.blocks, model::FlowOpts { tracked: false, fields: true }); + w.blocks(&blocks, &mut body, 0); + } + if body.is_empty() { + body.push_str(""); + } + out.xml.push_str(&format!("{body}")); + } + if name == "header" { + out.header = true; + } else { + out.footer = true; + } + } + out +} + +/// `defaults` is the spacing of the document's Normal paragraphs, written as the default style's; +/// `parts` the headers and footers of each page in `pages` and `part_styles` their automatic +/// styles. +fn styles_xml( + pages: &[(String, wordcraft_doc::SectionProps)], + parts: &[PageParts], + part_styles: &str, + body_font: &str, + defaults: &model::Spacing, +) -> String { let mut s = format!("\n"); s.push_str(&format!( "", @@ -31,16 +85,41 @@ fn styles_xml(pages: &[(String, wordcraft_doc::SectionProps)], body_font: &str, s.push_str(""); s.push_str(&format!("", model::MONO_FONT)); s.push_str(""); + s.push_str(part_styles); + let none = PageParts::default(); for (i, (_, sec)) in pages.iter().enumerate() { + let pp = parts.get(i).unwrap_or(&none); // A page of several columns carries them in its layout. let cols = if sec.columns.count > 1 { columns_xml(&sec.columns) } else { String::new() }; + // Word measures the header from the page edge and the text from the page edge; ODF + // starts the header at the page margin and the text after the header's height. So with + // a header the page margin is the header's distance, and the header is tall enough to + // reach the text (it grows with its content, as Word's pushes the text down). + let (top, header_h) = if pp.header { (sec.header, (sec.margin_top - sec.header).max(0.0)) } else { (sec.margin_top, 0.0) }; + let (bottom, footer_h) = if pp.footer { (sec.footer, (sec.margin_bottom - sec.footer).max(0.0)) } else { (sec.margin_bottom, 0.0) }; + let header = if pp.header { + format!( + "", + pt(header_h) + ) + } else { + String::new() + }; + let footer = if pp.footer { + format!( + "", + pt(footer_h) + ) + } else { + String::new() + }; s.push_str(&format!( - "{cols}", + "{cols}{header}{footer}", i + 1, pt(sec.page_w), pt(sec.page_h), - pt(sec.margin_top), - pt(sec.margin_bottom), + pt(top), + pt(bottom), pt(sec.margin_left), pt(sec.margin_right), if sec.page_w > sec.page_h { "landscape" } else { "portrait" } @@ -48,7 +127,8 @@ fn styles_xml(pages: &[(String, wordcraft_doc::SectionProps)], body_font: &str, } s.push_str(""); for (i, (name, _)) in pages.iter().enumerate() { - s.push_str(&format!("", x(name), i + 1)); + let xml = parts.get(i).map(|p| p.xml.as_str()).unwrap_or(""); + s.push_str(&format!("{xml}", x(name), i + 1)); } s.push_str("\n"); s @@ -101,7 +181,7 @@ fn flow_sections(blocks: &[FBlock], last: &SectionProps) -> Vec<(SectionProps, s /// Write an `.odt` package. pub fn export(doc: &Document) -> Result, String> { - let flow = model::from_doc_tracked(doc); + let flow = model::from_doc_with(doc, model::FlowOpts { tracked: true, fields: true }); let mut w = Writer::default(); let mut body = String::new(); let sections = flow_sections(&flow.blocks, &doc.last_section); @@ -166,7 +246,10 @@ pub fn export(doc: &Document) -> Result, String> { right: Some(normal.indent_right), first: Some(normal.indent_first), }; - let styles = styles_xml(&pages, &body_font, &defaults); + let mut hw = Writer::with_prefix("M"); + let parts: Vec = pages.iter().map(|(_, sec)| page_parts(doc, sec, &mut hw)).collect(); + let styles = styles_xml(&pages, &parts, &hw.style_defs(), &body_font, &defaults); + w.pictures.append(&mut hw.pictures); let meta = meta_xml(&flow.meta); let mut manifest = String::from( "\n\n\n\n\n\n", diff --git a/crates/formats/src/odt/tests_more.rs b/crates/formats/src/odt/tests_more.rs index 1906c58e61..9becd96901 100644 --- a/crates/formats/src/odt/tests_more.rs +++ b/crates/formats/src/odt/tests_more.rs @@ -1,3 +1,5 @@ //! Tests for the ODT features added after the first reader and writer: one module per feature. +mod fields; +mod headers; mod spacing; diff --git a/crates/formats/src/odt/tests_more/fields.rs b/crates/formats/src/odt/tests_more/fields.rs new file mode 100644 index 0000000000..09e5537130 --- /dev/null +++ b/crates/formats/src/odt/tests_more/fields.rs @@ -0,0 +1,153 @@ +//! Text fields (ODF 1.3 §7): page number and count, dates and times, document information and +//! bookmark references open as WordCraft fields and are written back as ODF fields. + +use crate::odt::tests::{NS_T, package}; +use crate::odt::{export, import}; +use wordcraft_doc::para::InlineObject; +use wordcraft_doc::{Document, Paragraph, para_block}; + +/// Every field in the body: (code, cached result). +pub(super) fn fields(d: &Document) -> Vec<(String, String)> { + let mut out = Vec::new(); + for b in d.body.iter() { + if let Some(p) = b.as_para() { + for o in &p.objects { + if let InlineObject::Field { instr, result, .. } = o { + out.push((instr.clone(), result.clone())); + } + } + } + } + out +} + +fn body(xml: &str) -> Vec { + package( + &format!(r#"{xml}"#), + None, + ) +} + +#[test] +fn fields_open_as_fields_with_their_shown_text() { + let d = import(&body(concat!( + r#"Page 3 of 9."#, + r#"31/10/2026 12:00"#, + r#"My title|Subj|Ada|f.odt"#, + r#"See 2 and Intro"#, + ))) + .unwrap(); + let got = fields(&d); + let want = [ + ("PAGE", "3"), + ("NUMPAGES", "9"), + ("DATE", "31/10/2026"), + ("TIME", "12:00"), + ("TITLE", "My title"), + ("SUBJECT", "Subj"), + ("AUTHOR", "Ada"), + ("FILENAME", "f.odt"), + ("PAGEREF intro \\h", "2"), + ("REF intro \\h", "Intro"), + ]; + assert_eq!(got.iter().map(|(a, b)| (a.as_str(), b.as_str())).collect::>(), want); + // The text around the fields stays where it was. + let p0 = d.body[0].as_para().unwrap(); + assert_eq!(p0.text.replace(wordcraft_doc::para::OBJ, "#"), "Page # of #."); +} + +#[test] +fn a_fixed_date_and_unknown_fields_are_plain_text() { + let d = import(&body(r#"1 May, 4, 5"#)).unwrap(); + assert!(fields(&d).is_empty()); + assert_eq!(d.body[0].as_para().unwrap().text, "1 May, 4, 5"); +} + +fn field_para(parts: &[(&str, Option<&str>)]) -> std::sync::Arc { + // Text pieces, and fields given as (code, Some(result)). + let mut p = Paragraph::new(); + for (s, field) in parts { + match field { + Some(result) => { + p.text.push(wordcraft_doc::para::OBJ); + p.objects.push(InlineObject::Field { + instr: s.to_string(), + result: result.to_string(), + locked: false, + dirty: false, + code: false, + form: String::new(), + }); + } + None => p.text.push_str(s), + } + } + p.runs = vec![wordcraft_doc::para::Run { len: p.text.len(), props: Default::default() }]; + para_block(p) +} + +#[test] +fn fields_round_trip() { + let mut d = Document::new(); + d.body = vec![field_para(&[ + ("Page ", None), + ("PAGE", Some("1")), + (" of ", None), + ("NUMPAGES", Some("2")), + (" ", None), + ("DATE \\@ \"d MMMM yyyy\"", Some("31 October 2026")), + (" ", None), + ("TITLE", Some("T")), + (" ", None), + ("PAGEREF intro \\h", Some("4")), + ])]; + let xml = export(&d).unwrap(); + let back = import(&xml).unwrap(); + let got: Vec = fields(&back).into_iter().map(|(c, _)| c).collect(); + assert_eq!(got, ["PAGE", "NUMPAGES", "DATE", "TITLE", "PAGEREF intro \\h"]); + assert_eq!(back.body[0].as_para().unwrap().text.matches(wordcraft_doc::para::OBJ).count(), 5); +} + +#[test] +fn a_field_odf_has_no_element_for_keeps_its_result_as_text() { + let mut d = Document::new(); + d.body = vec![field_para(&[("x ", None), ("SECTIONPAGES", Some("7")), (" y", None)])]; + let back = import(&export(&d).unwrap()).unwrap(); + assert!(fields(&back).is_empty()); + assert_eq!(back.body[0].as_para().unwrap().text, "x 7 y"); +} + +#[test] +fn hostile_fields_do_not_panic() { + // Unclosed, nested and empty fields, and a long reference name. + let long = "n".repeat(100_000); + for xml in [ + "".to_string(), + "1".to_string(), + "".to_string(), + format!(r#"1"#), + ] { + let d = import(&body(&xml)).unwrap(); + let _ = export(&d).unwrap(); + } +} + +#[test] +fn an_element_inside_a_field_leaves_the_formatting_after_it_alone() { + // A span inside a field was skipped at its start but still ended, popping the enclosing + // span's formatting: the text after the field lost its bold. + let content = format!( + r#"x1y"# + ); + let d = import(&package(&content, None)).unwrap(); + let p = d.body[0].as_para().unwrap(); + assert_eq!(fields(&d), [("PAGE".to_string(), "1".to_string())]); + assert!(p.runs.iter().all(|r| r.props.bold == Some(true)), "{:?}", p.runs); +} + +#[test] +fn a_paragraph_ending_inside_a_field_still_ends() { + let d = import(&body("a1b")).unwrap(); + let texts: Vec = d.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.replace(wordcraft_doc::para::OBJ, "#")).collect(); + assert_eq!(texts, ["a#", "b"]); +} diff --git a/crates/formats/src/odt/tests_more/headers.rs b/crates/formats/src/odt/tests_more/headers.rs new file mode 100644 index 0000000000..6071b07fb7 --- /dev/null +++ b/crates/formats/src/odt/tests_more/headers.rs @@ -0,0 +1,154 @@ +//! Headers and footers written to the master pages (ODF 1.3 §16.10), read back by the reader +//! that already opens them. + +use crate::odt::{export, import}; +use wordcraft_doc::para::InlineObject; +use wordcraft_doc::props::CharProps; +use wordcraft_doc::{Document, Paragraph, Part, PartKind, para_block}; + +fn text_para(s: &str, props: CharProps) -> std::sync::Arc { + let mut p = Paragraph::new(); + p.text = s.into(); + p.runs = vec![wordcraft_doc::para::Run { len: s.len(), props }]; + para_block(p) +} + +fn page_field_para(before: &str) -> std::sync::Arc { + let mut p = Paragraph::new(); + p.text = format!("{before}{}", wordcraft_doc::para::OBJ); + p.objects.push(InlineObject::Field { instr: "PAGE".into(), result: "1".into(), locked: false, dirty: false, code: false, form: String::new() }); + p.runs = vec![wordcraft_doc::para::Run { len: p.text.len(), props: Default::default() }]; + para_block(p) +} + +fn part(kind: PartKind, blocks: Vec>) -> Part { + Part { kind, blocks, body: Default::default() } +} + +fn part_text(d: &Document, id: Option) -> String { + id.and_then(|i| d.parts.get(&i)) + .map(|p| p.blocks.iter().filter_map(|b| b.as_para()).map(|p| p.text.clone()).collect::>().join("|")) + .unwrap_or_default() +} + +#[test] +fn headers_and_footers_are_written_and_read_back() { + let mut d = Document::new(); + d.body = vec![text_para("Body", Default::default())]; + d.parts.insert(1, part(PartKind::Header, vec![text_para("Bold header", CharProps { bold: Some(true), ..Default::default() })])); + d.parts.insert(2, part(PartKind::Footer, vec![page_field_para("Page ")])); + d.parts.insert(3, part(PartKind::Header, vec![text_para("First page header", Default::default())])); + d.last_section.headers.default = Some(1); + d.last_section.footers.default = Some(2); + d.last_section.headers.first = Some(3); + d.last_section.title_page = true; + let back = import(&export(&d).unwrap()).unwrap(); + let s = &back.last_section; + assert_eq!(part_text(&back, s.headers.default), "Bold header"); + assert_eq!(part_text(&back, s.headers.first), "First page header"); + assert!(s.title_page); + assert_eq!(part_text(&back, s.footers.default).replace(wordcraft_doc::para::OBJ, "#"), "Page #"); + // The header's text keeps its formatting; the footer its page field. + let h = back.parts.get(&s.headers.default.unwrap()).unwrap().blocks[0].as_para().unwrap(); + assert_eq!(h.runs[0].props.bold, Some(true)); + let f = back.parts.get(&s.footers.default.unwrap()).unwrap().blocks[0].as_para().unwrap(); + assert!(matches!(&f.objects[..], [InlineObject::Field { instr, .. }] if instr == "PAGE")); +} + +#[test] +fn even_page_headers_are_left_page_headers() { + let mut d = Document::new(); + d.body = vec![text_para("Body", Default::default())]; + d.parts.insert(1, part(PartKind::Header, vec![text_para("Odd", Default::default())])); + d.parts.insert(2, part(PartKind::Header, vec![text_para("Even", Default::default())])); + d.last_section.headers.default = Some(1); + d.last_section.headers.even = Some(2); + d.settings.even_odd_headers = true; + let back = import(&export(&d).unwrap()).unwrap(); + assert!(back.settings.even_odd_headers); + assert_eq!(part_text(&back, back.last_section.headers.default), "Odd"); + assert_eq!(part_text(&back, back.last_section.headers.even), "Even"); +} + +#[test] +fn each_section_keeps_its_own_header() { + let mut d = Document::new(); + let mut first = Paragraph::new(); + first.text = "One".into(); + first.runs = vec![wordcraft_doc::para::Run { len: 3, props: Default::default() }]; + let mut sec = d.last_section.clone(); + sec.headers.default = Some(1); + first.section = Some(Box::new(sec)); + d.body = vec![para_block(first), text_para("Two", Default::default())]; + d.parts.insert(1, part(PartKind::Header, vec![text_para("H1", Default::default())])); + d.parts.insert(2, part(PartKind::Header, vec![text_para("H2", Default::default())])); + d.last_section.headers.default = Some(2); + let back = import(&export(&d).unwrap()).unwrap(); + let firsts: Vec<_> = back.body.iter().filter_map(|b| b.as_para()).filter_map(|p| p.section.as_ref()).collect(); + assert_eq!(firsts.len(), 1); + assert_eq!(part_text(&back, firsts[0].headers.default), "H1"); + assert_eq!(part_text(&back, back.last_section.headers.default), "H2"); +} + +#[test] +fn header_styles_do_not_clash_with_body_styles() { + // The header's automatic styles live in styles.xml, the body's in content.xml: their names + // must differ so neither reader takes one for the other. + let mut d = Document::new(); + d.body = vec![text_para("Italic body", CharProps { italic: Some(true), ..Default::default() })]; + d.parts.insert(1, part(PartKind::Header, vec![text_para("Bold header", CharProps { bold: Some(true), ..Default::default() })])); + d.last_section.headers.default = Some(1); + let back = import(&export(&d).unwrap()).unwrap(); + let body = back.body[0].as_para().unwrap(); + assert_eq!((body.runs[0].props.italic, body.runs[0].props.bold), (Some(true), None)); + let h = back.parts.get(&back.last_section.headers.default.unwrap()).unwrap().blocks[0].as_para().unwrap(); + assert_eq!((h.runs[0].props.bold, h.runs[0].props.italic), (Some(true), None)); +} + +fn styles_with_header(header: &str) -> String { + format!( + r#"{header}"#, + crate::odt::tests::NS_T + ) +} + +fn cm(v: f32) -> f32 { + v * 72.0 / 2.54 +} + +#[test] +fn a_header_starts_at_the_page_margin_and_the_text_after_it() { + // ODF puts the header at the page margin and the text below the header's height and spacing; + // Word measures both from the page edge. + let content = format!( + r#"x"#, + crate::odt::tests::NS_T + ); + let with = crate::odt::tests::package( + &content, + Some(&styles_with_header("HF")), + ); + let s = import(&with).unwrap().last_section; + let near = |a: f32, b: f32| (a - b).abs() < 0.05; + assert!(near(s.header, cm(2.0)) && near(s.margin_top, cm(2.75)), "{} {}", s.header, s.margin_top); + assert!(near(s.footer, cm(1.5)) && near(s.margin_bottom, cm(3.0)), "{} {}", s.footer, s.margin_bottom); + // No header (or a hidden one): the page margins are the text's. + let without = + crate::odt::tests::package(&content, Some(&styles_with_header(r#"H"#))); + let s = import(&without).unwrap().last_section; + assert!(near(s.margin_top, cm(2.0)) && near(s.margin_bottom, cm(1.5)), "{} {}", s.margin_top, s.margin_bottom); +} + +#[test] +fn header_and_footer_distances_round_trip() { + let mut d = Document::new(); + d.body = vec![text_para("Body", Default::default())]; + d.parts.insert(1, part(PartKind::Header, vec![text_para("H", Default::default())])); + d.parts.insert(2, part(PartKind::Footer, vec![text_para("F", Default::default())])); + d.last_section.headers.default = Some(1); + d.last_section.footers.default = Some(2); + (d.last_section.header, d.last_section.margin_top, d.last_section.footer, d.last_section.margin_bottom) = (30.0, 80.0, 20.0, 70.0); + let s = import(&export(&d).unwrap()).unwrap().last_section; + let near = |a: f32, b: f32| (a - b).abs() < 0.05; + assert!(near(s.header, 30.0) && near(s.margin_top, 80.0) && near(s.footer, 20.0) && near(s.margin_bottom, 70.0), "{s:?}"); +} diff --git a/crates/formats/src/odt/writer.rs b/crates/formats/src/odt/writer.rs index 73a29c8084..aad25abd68 100644 --- a/crates/formats/src/odt/writer.rs +++ b/crates/formats/src/odt/writer.rs @@ -45,6 +45,9 @@ type SecStart = (String, Option); #[derive(Default)] pub(super) struct Writer { + /// Prefix of every name it makes (styles, lists, tables, pictures): headers and footers are + /// written by a second writer whose styles go in `styles.xml`, apart from the body's. + pub(super) prefix: &'static str, /// Automatic text styles: properties XML → name. text_styles: Vec<(String, String)>, /// Automatic paragraph styles: (parent, properties XML, master page) → name. @@ -156,6 +159,11 @@ fn parent_style(k: Kind) -> &'static str { } impl Writer { + /// A writer whose names all start with `prefix`. + pub(super) fn with_prefix(prefix: &'static str) -> Self { + Writer { prefix, ..Default::default() } + } + fn text_style(&mut self, f: &Fmt) -> Option { let props = text_props(f); if props.is_empty() { @@ -164,7 +172,7 @@ impl Writer { if let Some((_, n)) = self.text_styles.iter().find(|(p, _)| *p == props) { return Some(n.clone()); } - let n = format!("T{}", self.text_styles.len() + 1); + let n = format!("{}T{}", self.prefix, self.text_styles.len() + 1); self.text_styles.push((props, n.clone())); Some(n) } @@ -208,14 +216,14 @@ impl Writer { if let Some((_, n)) = self.para_styles.iter().find(|(k, _)| *k == key) { return n.clone(); } - let n = format!("P{}", self.para_styles.len() + 1); + let n = format!("{}P{}", self.prefix, self.para_styles.len() + 1); self.para_styles.push((key, n.clone())); n } fn picture(&mut self, img: &model::Img) -> String { let ext = if img.ext == "jpeg" { "jpg".to_string() } else { img.ext.clone() }; - let name = format!("Pictures/image{}.{}", self.pictures.len() + 1, ext); + let name = format!("Pictures/{}image{}.{}", self.prefix, self.pictures.len() + 1, ext); self.pictures.push((name.clone(), img.data.clone(), mime_of(&img.ext).to_string())); name } @@ -402,7 +410,8 @@ impl Writer { let href = self.picture(img); let n = self.pictures.len(); out.push_str(&format!( - "", + "", + self.prefix, pt(img.w), pt(img.h) )); @@ -413,6 +422,20 @@ impl Writer { prev_space = false; } Inline::Anchor(a) => out.push_str(&format!("", x(a))), + Inline::Field { instr, result, fmt } => match super::fields::element(instr, &x(result)) { + Some(el) => { + match self.text_style(&Fmt { link: None, ..fmt.clone() }) { + Some(n) => out.push_str(&format!("{el}")), + None => out.push_str(&el), + } + prev_space = false; + } + // No ODF field for it: what it shows, as text. + None => { + self.plain_inlines(&[Inline::Text(result.clone(), fmt.clone())], out); + prev_space = result.ends_with(' '); + } + }, // Read as its base text by `ruby_as_text` above. Inline::Ruby { .. } => {} Inline::Figure(_) | Inline::Equation { .. } => {} @@ -440,7 +463,7 @@ impl Writer { } }; let tracked = paras.len() > 1 - || p.inlines.iter().any(|i| matches!(i, Inline::Text(_, f) | Inline::Ruby { fmt: f, .. } if f.ins.is_some() || f.del.is_some())); + || p.inlines.iter().any(|i| matches!(i, Inline::Text(_, f) | Inline::Ruby { fmt: f, .. } | Inline::Field { fmt: f, .. } if f.ins.is_some() || f.del.is_some())); if tracked { let mut items = Vec::new(); for (k, q) in paras.iter().enumerate() { @@ -503,7 +526,7 @@ impl Writer { i += 1; } self.list_styles.push(kinds); - let lname = format!("L{}", self.list_styles.len()); + let lname = format!("{}L{}", self.prefix, self.list_styles.len()); // Open lists: per level, whether a list-item is open. let mut stack: Vec = Vec::new(); for (qi, q) in blocks.get(start..i).unwrap_or(&[]).iter().enumerate() { @@ -523,7 +546,7 @@ impl Writer { *top = true; } if stack.is_empty() { - let id = format!("lst{}", self.list_styles.len()); + let id = format!("{}lst{}", self.prefix, self.list_styles.len()); // A list cut by a section boundary goes on numbering: continue-list names the // preceding list by its xml:id, whatever its style name (ODF 1.3 §19.743). let prev = self.last_list.clone(); @@ -577,7 +600,7 @@ impl Writer { self.cell_styles.len() - 1 } }; - format!("Cell{}", k + 1) + format!("{}Cell{}", self.prefix, k + 1) } fn table(&mut self, t: &FTable, out: &mut String, depth: usize) { @@ -590,7 +613,7 @@ impl Writer { }; self.tables.push(widths); self.table_starts.push(self.master.take()); - let tn = self.tables.len(); + let tn = format!("{}{}", self.prefix, self.tables.len()); out.push_str(&format!("")); for c in 0..cols { out.push_str(&format!("", c + 1)); @@ -642,7 +665,12 @@ impl Writer { } pub(super) fn automatic_styles(&self) -> String { - let mut s = String::from(""); + format!("{}", self.style_defs()) + } + + /// The automatic styles made so far, without their `office:automatic-styles` element. + pub(super) fn style_defs(&self) -> String { + let mut s = String::new(); for (props, n) in &self.text_styles { s.push_str(&format!("")); } @@ -653,7 +681,7 @@ impl Writer { )); } for (i, kinds) in self.list_styles.iter().enumerate() { - s.push_str(&format!("", i + 1)); + s.push_str(&format!("", self.prefix, i + 1)); for (l, ordered) in kinds.iter().enumerate() { let lv = l + 1; let props = format!( @@ -674,7 +702,7 @@ impl Writer { s.push_str(""); } for (i, widths) in self.tables.iter().enumerate() { - let tn = i + 1; + let tn = format!("{}{}", self.prefix, i + 1); let total: f32 = widths.iter().sum(); let (master, number) = match self.table_starts.get(i).and_then(Option::as_ref) { Some((m, n)) => { @@ -703,14 +731,14 @@ impl Writer { None => "", }; s.push_str(&format!( - "", + "", + self.prefix, i + 1 )); } for st in &self.section_styles { s.push_str(st); } - s.push_str(""); s } } @@ -726,7 +754,7 @@ impl Item<'_> { /// Its tracked insertion and deletion. fn marks(&self) -> (Option<&RevMark>, Option<&RevMark>) { match self { - Item::Inl(Inline::Text(_, f) | Inline::Ruby { fmt: f, .. }) => (f.ins.as_ref(), f.del.as_ref()), + Item::Inl(Inline::Text(_, f) | Inline::Ruby { fmt: f, .. } | Inline::Field { fmt: f, .. }) => (f.ins.as_ref(), f.del.as_ref()), Item::Inl(_) => (None, None), Item::Mark(p) => (p.mark_ins.as_deref(), p.mark_del.as_deref()), } diff --git a/crates/formats/src/rtf/export.rs b/crates/formats/src/rtf/export.rs index 0b98c81eb7..2209857a55 100644 --- a/crates/formats/src/rtf/export.rs +++ b/crates/formats/src/rtf/export.rs @@ -399,6 +399,7 @@ impl Writer { } // RTF keeps no ruby yet: its base text, in the run's formatting. Inline::Ruby { base, fmt, .. } => self.run(base, fmt, out), + Inline::Field { result, fmt, .. } => self.run(result, fmt, out), Inline::Figure(_) | Inline::Equation { .. } => {} } } diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 8f612b627a..90c927a5f3 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -1,6 +1,6 @@ # File-format parity with Microsoft Word -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-31 · **Change:** trivial (ODT paragraph spacing, indents and line spacing read and written); previously trivial (a paragraph naming an undefined style is formatted as Normal, as in Word, #5077); previously trivial (a hyperlinked TOC writes its page numbers as `PAGEREF _Toc… \h` fields, as Word does, #5097); previously trivial (HTML import ends the empty comments and at once, #5040); previously trivial (chart and SmartArt text the font lacks is drawn in a fallback face, so Chinese labels no longer show boxes, #4720); previously trivial (bibliography sources keep a corporate author (`b:Corporate`): read, written, cited and listed whole, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (note conversion preserves content with an ordered reference/run walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (built-in heading styles have their outline level without `w:outlineLvl`, #4696); previously trivial (DOCX comment ranges inside footnotes and endnotes are kept on read, #4548); previously trivial (RTF import and export keep Windows and Enhanced Metafile pictures (`\wmetafile`, `\emfblip`), #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (DOCX parts stored under another casing keep their embedded fonts through a save, and a private save of a kept glossary escapes the attribute values it rewrites; previously trivial: the `.doc` reader logs what its copy budget leaves out, once a kind per document; importers bound hostile expansion: font names, links, ODS repeats, LaTeX wrappers, Markdown delimiters, #3692/#3713; previously trivial: repeated DOCX style IDs: save/reopen regression; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: Arabic list labels: `arabicAlpha` and `arabicAbjad` draw the 28 Arabic letters (alphabetical and abjad order), doubling past the last letter; Hebrew, Hindi, Thai and Vietnamese still show decimal (`doc::section::tests::arabic_letters_count_in_both_orders`); previously trivial: localized TOC style ids read as the TOC styles, and a TOC in columns keeps its layout through Update Table, #3889; previously trivial: cu20we: outline caps; previously trivial: a relationship the markup names without the prefix is read as it wrote: an unqualified `id` on an element that carries a relationship names that relationship, and the writer keeps the attribute with the id the new package holds the part under; an ordinary `id` — a VML shape's, a drawing's — names no part; previously trivial: the reader takes the relationship ids the markup refers to from the relationship attributes of the main document it has already parsed, so a document with many relationships opens in the time its part takes, #3411; previously trivial: a table cell span from a `.json` file stays within 63 columns through Insert Column and Merge Cells, #3637; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: `majorBidi`/`minorBidi` theme fonts follow the settings' theme font language, as measured in Word; previously trivial: Word signature lines are kept on save, still signable; previously trivial: Times New Roman keeps Liberation Serif under the roman-font rule; previously trivial: shape gradients and transparency, #1500; previously trivial: a folder that keeps no Unix modes, such as a NAS share mounted by GVFS, still takes a save, #3003; previously trivial: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: table style band sizes written, a style without them unbanded as in Word, #3415; previously trivial: HTML export writes ruby (furigana, phonetic guides) and HTML import reads it back, #1681; previously trivial: RTF import decodes the East Asian multibyte code pages, #1653; previously trivial: PDF export: lam-alef drawn as two glyphs at one x extracts in reading order, #2310; previously trivial: native .json stores pictures and kept parts as base64, #586 follow-up; previously trivial: ODT headers and footers read from each master page, #863; previously trivial: RTF import compares heading and code formatting with the file's stylesheet; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: Word XML and web pages saved as `.doc` open by what they hold, #2853; previously trivial: a VML shape in a header cell is always placed on the page, user-checked in Word for Mac, #1775; previously trivial: `w:sym` characters keep their symbol font when the text's font changes and are written back as `w:sym`, #2036; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: two lines in one and horizontal in vertical (`w:eastAsianLayout`) kept on save; previously trivial: PDF outline targets follow leading page breaks; previously trivial: positional tabs `w:ptab` read and kept on save, #2367; previously trivial: DOCX: mixed right-to-left runs split and marked `w:rtl` with their language, #1543; previously trivial: `w:doNotTrackMoves` round-trips and turns move tracking off; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: DOCX glossary documents (building blocks) read, kept and written; previously trivial: cached field results keep their visible run formatting; previously trivial: cached REF results retain hyperlink destinations; previously trivial: CLI conversions can explicitly refresh fields; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: a pasted HTML `
` is an indented paragraph, not the Quote style, #1270); previously trivial: every table style part round-trips, #714; previously trivial: VML freeforms read and kept on save; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: `flowChartOr` draws its circle and cross spokes, as in Word; previously trivial: RTF: Mac OS Roman and DOS text, #1653; previously trivial: VML text boxes keep their internal margins, fit to their text and vertical alignment; hidden drawings not shown; previously trivial: PDF export draws horizontal and vertical lines inside groups, #1474; previously trivial: symbol-font text exported as Unicode, #776; previously trivial: text-box margins, Resize shape to fit text and freeforms in groups, #743; previously trivial: formatted paste from the system clipboard's HTML and RTF, #687; previously trivial: independent complex-script formatting inheritance and round-trip regression tests; previously trivial: import/export fixes for TXT, RTF, ODT, HTML and LaTeX, #433–#440; previously major: first version; every format Word reads or writes, measured from the readers and writers on origin/main) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4, plus Word for Windows' extra formats +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-31 · **Change:** trivial (ODT headers, footers and text fields written; fields read); previously trivial (ODT paragraph spacing, indents and line spacing read and written); previously trivial (a paragraph naming an undefined style is formatted as Normal, as in Word, #5077); previously trivial (a hyperlinked TOC writes its page numbers as `PAGEREF _Toc… \h` fields, as Word does, #5097); previously trivial (HTML import ends the empty comments and at once, #5040); previously trivial (chart and SmartArt text the font lacks is drawn in a fallback face, so Chinese labels no longer show boxes, #4720); previously trivial (bibliography sources keep a corporate author (`b:Corporate`): read, written, cited and listed whole, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (note conversion preserves content with an ordered reference/run walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (built-in heading styles have their outline level without `w:outlineLvl`, #4696); previously trivial (DOCX comment ranges inside footnotes and endnotes are kept on read, #4548); previously trivial (RTF import and export keep Windows and Enhanced Metafile pictures (`\wmetafile`, `\emfblip`), #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (DOCX parts stored under another casing keep their embedded fonts through a save, and a private save of a kept glossary escapes the attribute values it rewrites; previously trivial: the `.doc` reader logs what its copy budget leaves out, once a kind per document; importers bound hostile expansion: font names, links, ODS repeats, LaTeX wrappers, Markdown delimiters, #3692/#3713; previously trivial: repeated DOCX style IDs: save/reopen regression; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: Arabic list labels: `arabicAlpha` and `arabicAbjad` draw the 28 Arabic letters (alphabetical and abjad order), doubling past the last letter; Hebrew, Hindi, Thai and Vietnamese still show decimal (`doc::section::tests::arabic_letters_count_in_both_orders`); previously trivial: localized TOC style ids read as the TOC styles, and a TOC in columns keeps its layout through Update Table, #3889; previously trivial: cu20we: outline caps; previously trivial: a relationship the markup names without the prefix is read as it wrote: an unqualified `id` on an element that carries a relationship names that relationship, and the writer keeps the attribute with the id the new package holds the part under; an ordinary `id` — a VML shape's, a drawing's — names no part; previously trivial: the reader takes the relationship ids the markup refers to from the relationship attributes of the main document it has already parsed, so a document with many relationships opens in the time its part takes, #3411; previously trivial: a table cell span from a `.json` file stays within 63 columns through Insert Column and Merge Cells, #3637; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: `majorBidi`/`minorBidi` theme fonts follow the settings' theme font language, as measured in Word; previously trivial: Word signature lines are kept on save, still signable; previously trivial: Times New Roman keeps Liberation Serif under the roman-font rule; previously trivial: shape gradients and transparency, #1500; previously trivial: a folder that keeps no Unix modes, such as a NAS share mounted by GVFS, still takes a save, #3003; previously trivial: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: table style band sizes written, a style without them unbanded as in Word, #3415; previously trivial: HTML export writes ruby (furigana, phonetic guides) and HTML import reads it back, #1681; previously trivial: RTF import decodes the East Asian multibyte code pages, #1653; previously trivial: PDF export: lam-alef drawn as two glyphs at one x extracts in reading order, #2310; previously trivial: native .json stores pictures and kept parts as base64, #586 follow-up; previously trivial: ODT headers and footers read from each master page, #863; previously trivial: RTF import compares heading and code formatting with the file's stylesheet; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: Word XML and web pages saved as `.doc` open by what they hold, #2853; previously trivial: a VML shape in a header cell is always placed on the page, user-checked in Word for Mac, #1775; previously trivial: `w:sym` characters keep their symbol font when the text's font changes and are written back as `w:sym`, #2036; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: two lines in one and horizontal in vertical (`w:eastAsianLayout`) kept on save; previously trivial: PDF outline targets follow leading page breaks; previously trivial: positional tabs `w:ptab` read and kept on save, #2367; previously trivial: DOCX: mixed right-to-left runs split and marked `w:rtl` with their language, #1543; previously trivial: `w:doNotTrackMoves` round-trips and turns move tracking off; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: DOCX glossary documents (building blocks) read, kept and written; previously trivial: cached field results keep their visible run formatting; previously trivial: cached REF results retain hyperlink destinations; previously trivial: CLI conversions can explicitly refresh fields; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: a pasted HTML `
` is an indented paragraph, not the Quote style, #1270); previously trivial: every table style part round-trips, #714; previously trivial: VML freeforms read and kept on save; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: `flowChartOr` draws its circle and cross spokes, as in Word; previously trivial: RTF: Mac OS Roman and DOS text, #1653; previously trivial: VML text boxes keep their internal margins, fit to their text and vertical alignment; hidden drawings not shown; previously trivial: PDF export draws horizontal and vertical lines inside groups, #1474; previously trivial: symbol-font text exported as Unicode, #776; previously trivial: text-box margins, Resize shape to fit text and freeforms in groups, #743; previously trivial: formatted paste from the system clipboard's HTML and RTF, #687; previously trivial: independent complex-script formatting inheritance and round-trip regression tests; previously trivial: import/export fixes for TXT, RTF, ODT, HTML and LaTeX, #433–#440; previously major: first version; every format Word reads or writes, measured from the readers and writers on origin/main) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4, plus Word for Windows' extra formats Every format Word opens or saves, what WordCraft does with it, and how it's tested. Fidelity is the share of a typical real-world file's content and formatting that survives (read: shows correctly; @@ -57,7 +57,7 @@ Weights are the share of a Word user's file traffic (estimate). Read/write: ✅ | PDF export | 8% | W | — | ✅ | ~80% | 6–10 | `crates/pdf/src/tests.rs` | krilla: embedded subsets, selectable text incl. field results (#133); Hebrew and Arabic copy and search as whole words (right-to-left glyphs written in visual order, #1383, also with fonts that draw lam-alef as two glyphs at one x, #2310); All Caps and Small Caps text extracts as the capitals shown, as from Word's PDFs, and never changes the case of other text (#1473), links, outline, tagging. Outline destinations follow leading page breaks to the heading text. Fonts it can't subset become outlines (#132). Print Markup (#589): comments in balloons beside each page (scaled to its paper) and tracked changes; HTML/Markdown/text copies end with a comment list. Horizontal and vertical lines inside groups are drawn (#1474). No PDF/A option, no "best for printing/online" choice | | PDF open (PDF Reflow, Word for Windows) | 1% | R | ❌ | — | 0% | 30–50 | — | Converting a PDF into an editable document. A sibling app (PdfCraft) may supply the parser | | `.rtf` Rich Text Format | 3% | R/W | 🟡 | 🟡 | ~50% | 10–15 | `crates/formats/src/tests.rs` | ~1,500 lines: text, character/paragraph formatting, styles, lists, tables, pictures (`\shppict`, #154; Windows and Enhanced Metafiles, `\wmetafile` and `\emfblip`, read and written, #4259), fields, bookmarks. 8-bit text in the document's code page (`\ansicpg`) or the current font's (`\fcharset`, `\cpg`): Cyrillic, Central European, Greek, Turkish, Hebrew, Arabic, Baltic, Vietnamese, Thai (was Windows-1252 only), Mac OS Roman (`\mac`, `\fcharset77`) and DOS code pages (`\pc` 437, `\pca` 850, `\cpg` 437/737/850/852/857/866, `\fcharset254`) (#1653). East Asian multibyte code pages decoded as runs: 932 Shift-JIS (`\fcharset128`, Japanese), 936 GBK (134, Simplified Chinese), 949 UHC/windows-949 (129, Korean) and 950 Big5 (136, Traditional Chinese) through `\ansicpg`, `\cpg` or the font's charset, escaped or raw, a lead byte and its trail byte together; invalid sequences read as U+FFFD, and a run that starts `EF BB BF` is text, not a BOM (#1653). Pasted from the system clipboard where the platform offers it (Windows, Wayland) when there is no HTML (#687). Tracked insertions and deletions read and written (`\revtbl`, `\revised`/`\deleted`, authors, `\revdttm` dates), so Accept/Reject All work after opening or a round trip; tracked paragraph marks (`\par` inside `\revised`/`\deleted` groups, including a `\sect` inside a deletion) and character and paragraph formatting revisions (`\crauth`/`\crdate` with `\oldcprops`, `\prauth`/`\prdate` with `\oldpprops`) read and written; section and table formatting revisions (`\oldsprops`, `\oldtprops`) not yet. Sections on import (#1378): each `\sect` ends a section with its own page size and orientation, margins, header/footer distance, columns, break type, page-number restart, start and format, title page and headers/footers (those it leaves out link to the previous); export writes every section too (break type, page size and orientation, margins, header/footer distance, columns, page numbering, title page; not headers/footers). Thin: comments, notes, RTL | -| `.odt` OpenDocument Text | 2% | R/W | 🟡 | 🟡 | ~50% | 8–12 | `crates/formats/src/tests.rs`, `crates/formats/src/odt.rs` unit tests, `crates/engine/tests/it/odt_sections.rs` | Styles, lists (#87), tables, pictures, page breaks (#86), paragraph spacing, indents and line spacing (proportional, fixed, at least; leading read as a proportional approximation) both ways, with the default and Standard styles' spacing as the document defaults (ODT's are none, not Word's 8 pt after and 1.15 lines), headers and footers of each master page (every, left and first page) read on open, not yet written (#863), keep with next (#2813), an embedded formula object (`draw:object`) is read from its own `content.xml`'s MathML, same as HTML (#3099; export still drops equations, as before), page size, orientation and margins (#810), sections: one per master-page change on a paragraph or table (next page, with its own page size, orientation, margins, columns and page-numbering restart) and per columned `text:section` (continuous, with its columns and separator line) (#1379); export writes them back: a master page and page layout per new-page section (size, orientation, margins, columns, page-numbering restart on the first paragraph or table), a `text:section` with its columns per continuous section whose columns differ from the page's (one column included), a list cut by a section break goes on numbering. Limits: a continuous section's page-numbering restart isn't written (ODF applies it only with a master page); a continuous section with the page's columns merges into the section before it on reopening; even and odd page starts are written as plain new pages; a tracked deletion of a section break is lost; an empty last section is dropped; tracked insertions and deletions with author and date, inserted and deleted paragraph marks and text inserted then deleted included (ODF change tracking; format changes read as none; pictures and tables inside a tracked deletion are dropped). No footnotes, comments, fields or RTL | +| `.odt` OpenDocument Text | 2% | R/W | 🟡 | 🟡 | ~50% | 8–12 | `crates/formats/src/tests.rs`, `crates/formats/src/odt.rs` unit tests, `crates/engine/tests/it/odt_sections.rs` | Styles, lists (#87), tables, pictures, page breaks (#86), paragraph spacing, indents and line spacing (proportional, fixed, at least; leading read as a proportional approximation) both ways, with the default and Standard styles' spacing as the document defaults (ODT's are none, not Word's 8 pt after and 1.15 lines), headers and footers of each master page (every, left and first page) read on open and written on save, with the header and footer distances converted between Word's page-edge measures and ODF's header bands (#863), text fields (page number and count, date, time, title, subject, author, file name, word and character counts, bookmark references as `REF`/`PAGEREF`) read as fields and written back as ODF fields; fixed dates and fields without a Word equivalent (sequences, chapters, variables) read as their text, and Word fields ODF lacks written as their result, date formats not kept, keep with next (#2813), an embedded formula object (`draw:object`) is read from its own `content.xml`'s MathML, same as HTML (#3099; export still drops equations, as before), page size, orientation and margins (#810), sections: one per master-page change on a paragraph or table (next page, with its own page size, orientation, margins, columns and page-numbering restart) and per columned `text:section` (continuous, with its columns and separator line) (#1379); export writes them back: a master page and page layout per new-page section (size, orientation, margins, columns, page-numbering restart on the first paragraph or table), a `text:section` with its columns per continuous section whose columns differ from the page's (one column included), a list cut by a section break goes on numbering. Limits: a continuous section's page-numbering restart isn't written (ODF applies it only with a master page); a continuous section with the page's columns merges into the section before it on reopening; even and odd page starts are written as plain new pages; a tracked deletion of a section break is lost; an empty last section is dropped; tracked insertions and deletions with author and date, inserted and deleted paragraph marks and text inserted then deleted included (ODF change tracking; format changes read as none; pictures and tables inside a tracked deletion are dropped). No footnotes, comments or RTL | | `.txt` Plain text | 2% | R/W | ✅ | ✅ | ~95% | 1–2 | unit tests | Encoding detection. Files with other extensions (`.m3u`/`.m3u8` playlists, logs, config files, no extension) open as text when they are text, Word and RTF files by their signature; binary files are refused (#569). A Word or RTF extension that contradicts the content also opens by signature, as in Word: RTF saved as `.doc` (hh.ru résumés, accounting exports), a `.docx` renamed `.doc`, a Word 97-2003 file renamed `.docx`; a Word XML Document (Word 2003 XML or Flat OPC) or a web page (``, ``) under a `.doc`, `.docx` or `.rtf` name opens as what it is (#2853, `tests::word_xml_and_web_pages_saved_as_doc_open_by_content`). Word's encoding choice dialog and line-ending options missing; such files save only through Save As | | `.htm/.html` Web Page and Web Page, Filtered | 1% | R/W | 🟡 | 🟡 | ~60% | 6–10 | `crates/formats/src/tests.rs` | Tables (#102), relative pictures embedded (#113), missing `` tolerated (#154); the empty comments `` and `` end at once instead of swallowing the rest of the page (#5040). Header cells (``) bold unless their CSS says otherwise; `text-align` on a cell, or inherited from its row, row group or table, aligns the cell's paragraphs (#1381). HTML gives no page size, so it opens on the region's paper (A4 or Letter), as Markdown and plain text do; `@page` isn't read (#1381). No Word "Web Page" (with `_files` folder) round-trip. The same reader takes HTML from the system clipboard: Paste keeps another program's formatting (a Word document, a web page; Windows' "HTML Format" header understood), Merge Formatting keeps its emphasis (#687). A pasted `
` (LibreOffice's paragraph indented on both sides) becomes a Normal paragraph indented by the page's `blockquote` margins, else 0.5 in each side; opening an .html file keeps it as the Quote style (#1270). Equations are written as MathML (with the linear format as a UnicodeMath annotation) and read back from MathML: our annotation, else the presentation elements, else a TeX annotation (#816). A copy of one equation alone also goes in the clipboard's MathML formats (Windows "MathML" and "MathML Presentation", macOS `public.mathml.presentation` and `public.mathml`; W3C MathML 3 §6.2.4), which Microsoft documents Word reading first when it pastes math (#1297; the paste into Word is not yet verified); Ruby (furigana, phonetic guides) is written as `basereading` in the base text's formatting and read back from ``/``/`` (`` fallback parentheses dropped; malformed markup keeps its text), so docx/doc → HTML → doc keeps the annotation; HTML states no alignment, size or raise, so the Phonetic Guide's defaults apply (#1681) CRLF and lone CR input newlines normalize before tokenization, preserving preformatted lines and leaving decoded character references separate (#4810). | | `.mht/.mhtml` Single File Web Page | <1% | R/W | ❌ | ❌ | 0% | 3–5 | — | | @@ -279,6 +279,7 @@ Custom document properties retain their bookmark link targets through DOCX saves | Date | Change | Summary | |---|---|---| +| 2026-10-31 | trivial | ODT: headers and footers are written to each section's master page (every, left and first page), with Word's header and footer distances converted to ODF's header and footer bands and back on open; text fields (`text:page-number`, `text:page-count`, `text:date`, `text:time`, document information, `text:bookmark-ref`) open as WordCraft fields and are written back as ODF fields. `formats::odt::tests_more::{headers, fields}` | | 2026-10-31 | trivial | ODT: paragraph spacing, indents and line spacing (`fo:margin-*`, `fo:text-indent`, `fo:line-height`, `style:line-height-at-least`; `style:line-spacing` leading as a proportional approximation) read from automatic and common styles and written back; the default and Standard paragraph styles give the document defaults, so an ODT paragraph without spacing has none instead of Word's 8 pt after and 1.15 lines. `formats::odt::tests_more::spacing` | | 2026-10-31 | trivial | DOCX: a paragraph naming a style the file doesn't define is formatted as Normal, as in Word (#5077) | | 2026-10-31 | trivial | DOCX: a hyperlinked table of contents writes each page number as a `PAGEREF _Toc… \h` field to the heading's bookmark, as Word does, so updating the fields in Word moves the numbers with the headings; they were plain text (checked in Word for Mac 16.93: with the headings moved a page down, the three fields update from 2, 3, 4 to 3, 4, 5). `engine::tests::toc_page_numbers_are_pageref_fields` | diff --git a/docs/gaps.md b/docs/gaps.md index 32a4369fba..c545cc8da9 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where WordCraft falls short of Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-30 · **Change:** trivial (only the PDF sent to the printer leaves out link annotations; Save these pages as PDF keeps them, #4734; no ranked gap changed); previously trivial (documents with links print: the print job leaves out link annotations, #4734; no ranked gap changed; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion reference-run membership uses one ordered walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (gap 19: RTF reads and writes WMF and EMF pictures, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (gap 19 narrowed: ODT headers and footers written, text fields and paragraph spacing read and written); previously trivial (only the PDF sent to the printer leaves out link annotations; Save these pages as PDF keeps them, #4734; no ranked gap changed); previously trivial (documents with links print: the print job leaves out link annotations, #4734; no ranked gap changed; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion reference-run membership uses one ordered walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (gap 19: RTF reads and writes WMF and EMF pictures, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Highlight update:** 2026-10-25 · Highlight shortcut toggles the last chosen colour off when the whole selection has it; explicit colour commands remain setters (#4085). Readiness estimates unchanged. > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-29 · **Change:** trivial (aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (#15: bibliography sources keep a corporate author, #4848); previously trivial (Table Properties applies to the selected rows, columns and cells, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (#7: Custom Document Properties dialog, #2982; previously trivial: egui 0.37 migration: the interface catalogs stay shared and pinned by `craft-i18n.rev` at `9205108cfdced5177706264038848b68b2422d2b` (27 languages, English fallback); input-method handling has new regressions but managed CI passed, and native IME acceptance plus native-speaker catalog review remain open; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: small follow-ups from the field, table and AutoFormat batch, #4232; previously trivial: highlight target includes selected paragraph marks, #4085; previously trivial: full-target highlight check beyond 10,000 runs, #4085; previously trivial: Exact-spacing table contact uses the specified line height rather than paragraph-mark metrics (#3977); previously trivial: cu20we: restrict pre-spacing float origins to paragraph anchors; document cap helpers and regressions; previously trivial: a command that changes nothing is not an edit: no undo step, no unsaved changes, no relayout, and the redo stack survives it; previously minor: #8 narrowed: Spanish proofing (Spain, Mexico, Latin America, Uruguay) from the installed Hunspell dictionaries, #3360, #3553; previously trivial: gap 10: group connector rerouting preserves transformed free and unresolved endpoints under nonuniform scaling; obstacle routing still missing; readiness estimates unchanged; previously trivial: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: the ranked gap list held two rows each for #19, #20 and #21; each gap appears once now; a document with many relationships opens in the time its part takes, #3411; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 @@ -81,7 +81,7 @@ Text context menus now include the existing formatting mini toolbar (#2959), inc | 15 | **References depth**: no EndNote/Mendeley desktop integration (bibliography sources are saved in DOCX, #790; Zotero landed, #189; Word's own `Sources.xml` master list imports into Manage Sources, #2640) | | #790 stores sources in `customXml` and reads them back; `crates/zotero`; `references.sources` `import` reads a Word bibliography file (`b:Sources`) | Students and researchers | 3–6 | [target-app-parity.md](target-app-parity.md) | | 16 | **Right-to-left completeness**: kashida, RTL in HTML/ODT/RTF; six still-open older RTL bug reports to re-verify after #207 (#211, #199, #66, #63, #48, #19; #215 is closed). RTL tables and sections landed (#362); PDF export's Hebrew and Arabic text copies and searches as whole words (#1383) | | [layout-parity.md](layout-parity.md) | Arabic, Persian, Hebrew writers | 7–11 | [layout-parity.md](layout-parity.md) | | 17 | **East Asian typography**: hint-reliant DFKai-SB contours repaired (local Windows regression; no readiness estimate change); Asian Typography options (Phonetic Guide landed, #288; Enclose Characters landed, #297; document grid landed, #391; vertical text landed, #491) | | `para.asianTypography` is live (Paragraph dialog Asian Typography tab, #323), automatic Asian/Latin and Asian/number spacing is laid out (#2358), and no East Asian item is left in the catalog's missing list; the remaining depth is not measured | Chinese, Japanese and Korean documents | 11–19 | [layout-parity.md](layout-parity.md) | -| 19 | **RTF and ODT depth**: notes, comments, revisions (RTF and ODT insertions and deletions done; RTF paragraph-mark and character/paragraph formatting revisions done; RTF section/table and ODT formatting revisions left), sections (RTF and ODT import and export done, #1378, #1379), RTL. ODT sections landed on import (#1379): page-style changes and columned `text:section`s; ODT table column widths are read (they were dropped on every open). ODT export writes sections back (a master page per new-page section, a columned `text:section` per continuous one); RTF export writes every section (`\sect`, `\sectd`); RTF reads and writes WMF and EMF pictures (#4259) | | ~2,100 (RTF) and ~1,600 (ODT) lines in `crates/formats`; RTF code pages (#896) and page setup (#902), ODT page setup (#829) landed; open PRs: RTF notes and comments (#1056), ODT notes and comments (#1057), ODT headers and footers (#873) | Users exchanging with LibreOffice or older tools | 18–27 | [file-format-parity.md](file-format-parity.md) | +| 19 | **RTF and ODT depth**: notes, comments, revisions (RTF and ODT insertions and deletions done; RTF paragraph-mark and character/paragraph formatting revisions done; RTF section/table and ODT formatting revisions left), sections (RTF and ODT import and export done, #1378, #1379), RTL. ODT sections landed on import (#1379): page-style changes and columned `text:section`s; ODT table column widths are read (they were dropped on every open). ODT export writes sections back (a master page per new-page section, a columned `text:section` per continuous one) and their headers and footers; ODT paragraph spacing and text fields read and written; RTF export writes every section (`\sect`, `\sectd`); RTF reads and writes WMF and EMF pictures (#4259) | | ~2,100 (RTF) and ~1,600 (ODT) lines in `crates/formats`; RTF code pages (#896) and page setup (#902), ODT page setup (#829) landed; open PRs: RTF notes and comments (#1056), ODT notes and comments (#1057), ODT headers and footers (#873) | Users exchanging with LibreOffice or older tools | 18–27 | [file-format-parity.md](file-format-parity.md) | | 18 | **Screen-reader access** to the document canvas: exposed, not yet tried with real screen readers | | The canvas describes paragraphs, headings, lists, tables, links, pictures and the caret to AccessKit (#488), tested headlessly; the ribbon's font name and size boxes, and the dropdown arrows beside them, say what they are (#3172); Windows Magnifier follows the caret, onto a new line too (#1119); no NVDA, VoiceOver or Orca run yet | Blind and low-vision users; public-sector procurement | 3–6 | [ui-parity.md](ui-parity.md) | | 20 | **Performance on large real-world documents** unmeasured (500+ pages, many pictures) | | DOCX comment saves avoid repeated paragraph-ID and durable-ID reservation searches; large threaded-comment fixtures retain metadata through save/reopen. Only the 188-page sample benchmark; scrolling a 1,096-page one-table document went from ~600 ms to ~2–5 ms a frame (#811), but opening it still lays out for seconds; opening a document with pictures no longer copies them (92 MB with 20 pictures: about 90 MB less); opening a document with many relationships now costs the size of its part and not that size times the number of relationships (1024 relationships and a 64 MiB part: 224 s → 0.2 s, #3411); a word with no break opportunity in it no longer lays out quadratically (1 MiB: 29.4 s → 0.35 s, #3412); saving a document with many content controls now costs the time the controls take (32,000 controls: 3.6 s → 0.1 s, #13) | Thesis and book authors | 10–20 | [layout-parity.md](layout-parity.md) | | 21 | **Mail merge data sources**: no Outlook/Contacts, Access or database sources, no email merge; CSV, typed lists (#247) and Excel/OpenDocument spreadsheets (#334, legacy .xls #778) only | | Select Recipients reads .csv/.tsv/.txt in any common encoding (UTF-8/16, GBK, Big5, Shift_JIS…), .xlsx/.xlsm, .xls and .ods; Edit Recipient List filters and sorts (#778), finds records and can show only the merged ones (#1576) | Office administrators | 4–8 | [target-app-parity.md](target-app-parity.md) | From 4959ac5f63384e4c5d447aa845ff3e79adf10724 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 23:17:19 +0000 Subject: [PATCH 034/308] Gap 19: an ODT header's spacing is inside its height, as LibreOffice draws it A LibreOffice header 2 cm high with 1 cm spacing puts the text 2 cm below the page margin: `fo:min-height` takes in the `fo:margin-bottom` spacing. Import added the spacing on top, so the text started a spacing too low (measured: 113.9 pt in LibreOffice, 142.3 pt after a WordCraft save). The spacing now counts only past the height. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GBVQLyDnrG8Tn1yDQcUabC --- crates/formats/src/odt/import.rs | 10 ++++++---- crates/formats/src/odt/tests_more/headers.rs | 9 +++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/formats/src/odt/import.rs b/crates/formats/src/odt/import.rs index a4aa795b61..574e26eac9 100644 --- a/crates/formats/src/odt/import.rs +++ b/crates/formats/src/odt/import.rs @@ -284,16 +284,18 @@ fn read_pages(xml: &[u8]) -> Pages { let Some(Layout { attrs: a, columns: cols, header: hb, footer: fb }) = layouts.get(&layout) else { continue }; let mut sec = SectionProps::default(); apply_layout(a, &mut sec); - // ODF starts a header at the page margin and the text after the header's height and - // spacing; Word measures the header's distance and the text's margin from the edge. + // ODF starts a header at the page margin and the text after the header's band; Word + // measures the header's distance and the text's margin from the edge. The band's height + // takes in its spacing (as LibreOffice draws it: a 2 cm header with 1 cm spacing puts + // the text 2 cm below the margin), so the spacing counts only past the height. let limit = sec.page_w.max(sec.page_h); if header { sec.header = sec.margin_top; - sec.margin_top = (sec.margin_top + hb.height + hb.spacing).min(limit); + sec.margin_top = (sec.margin_top + hb.height.max(hb.spacing)).min(limit); } if footer { sec.footer = sec.margin_bottom; - sec.margin_bottom = (sec.margin_bottom + fb.height + fb.spacing).min(limit); + sec.margin_bottom = (sec.margin_bottom + fb.height.max(fb.spacing)).min(limit); } // A one-column layout keeps the default columns (and their gap). if let Some(c) = cols.as_ref().filter(|c| c.count > 1) { diff --git a/crates/formats/src/odt/tests_more/headers.rs b/crates/formats/src/odt/tests_more/headers.rs index 6071b07fb7..da87cae2a6 100644 --- a/crates/formats/src/odt/tests_more/headers.rs +++ b/crates/formats/src/odt/tests_more/headers.rs @@ -118,8 +118,9 @@ fn cm(v: f32) -> f32 { #[test] fn a_header_starts_at_the_page_margin_and_the_text_after_it() { - // ODF puts the header at the page margin and the text below the header's height and spacing; - // Word measures both from the page edge. + // ODF puts the header at the page margin and the text below the header's band, whose height + // takes in its spacing (LibreOffice: a 2 cm header with 1 cm spacing puts the text 2 cm + // below the margin); Word measures both from the page edge. let content = format!( r#"x"#, crate::odt::tests::NS_T @@ -130,8 +131,8 @@ fn a_header_starts_at_the_page_margin_and_the_text_after_it() { ); let s = import(&with).unwrap().last_section; let near = |a: f32, b: f32| (a - b).abs() < 0.05; - assert!(near(s.header, cm(2.0)) && near(s.margin_top, cm(2.75)), "{} {}", s.header, s.margin_top); - assert!(near(s.footer, cm(1.5)) && near(s.margin_bottom, cm(3.0)), "{} {}", s.footer, s.margin_bottom); + assert!(near(s.header, cm(2.0)) && near(s.margin_top, cm(2.5)), "{} {}", s.header, s.margin_top); + assert!(near(s.footer, cm(1.5)) && near(s.margin_bottom, cm(2.5)), "{} {}", s.footer, s.margin_bottom); // No header (or a hidden one): the page margins are the text's. let without = crate::odt::tests::package(&content, Some(&styles_with_header(r#"H"#))); From 1dbcf073a4c3c622e97ed2043e1b8d78b3f58375 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 23:31:02 +0000 Subject: [PATCH 035/308] Gap 19: ODT footnotes and endnotes read and written MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `text:note` (ODF 1.3 §6.3) opens as a note story built as Word's: its paragraphs in Footnote Text or Endnote Text, the first starting with the note's own mark, the reference in the text in the reference style, and a `text:label` custom mark kept. The paragraph a note sits in (with its spans, list and table cell) is put aside while the note's content is read, and goes on after it. Notes inside notes, in deleted text or in frames are left out. Before, notes were dropped on open. Export writes each note with a citation numbered as LibreOffice shows it (footnotes 1, 2, 3; endnotes i, ii, iii) or its custom mark, its paragraphs in Footnote or Endnote styles defined in styles.xml. An insertion open around a note ends before it and goes on after it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GBVQLyDnrG8Tn1yDQcUabC --- ROADMAP.md | 3 +- crates/formats/src/html/export.rs | 1 + crates/formats/src/latex/export.rs | 2 +- crates/formats/src/markdown/export.rs | 1 + crates/formats/src/model/from_doc.rs | 13 ++ crates/formats/src/model/to_doc.rs | 43 +++++++ crates/formats/src/model/types.rs | 14 ++- crates/formats/src/odt/body.rs | 134 +++++++++++++++++++-- crates/formats/src/odt/package.rs | 10 +- crates/formats/src/odt/tests.rs | 6 + crates/formats/src/odt/tests_more.rs | 1 + crates/formats/src/odt/tests_more/notes.rs | 122 +++++++++++++++++++ crates/formats/src/odt/writer.rs | 50 +++++++- crates/formats/src/rtf/export.rs | 2 +- docs/file-format-parity.md | 5 +- docs/gaps.md | 4 +- 16 files changed, 387 insertions(+), 24 deletions(-) create mode 100644 crates/formats/src/odt/tests_more/notes.rs diff --git a/ROADMAP.md b/ROADMAP.md index 7718cf19bc..c49b6524af 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT footnotes and endnotes); previously trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-29 · **Change:** trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -140,6 +140,7 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm ## Progress log +- 2026-10-31: ODT: footnotes and endnotes open and save (they were dropped on open), as Word-style note stories with their custom marks (gap 19). Readiness unchanged. - 2026-10-31: ODT: headers and footers are saved (they were dropped on every save), each page's header sits where LibreOffice puts it on open, and page numbers, page counts, dates and other text fields open as fields that update and are saved as ODF fields (gap 19). Readiness unchanged. - 2026-10-31: ODT: paragraph spacing, indents and line spacing are read and written, and an ODT's own default spacing (none, single lines) replaces Word's 8 pt after and 1.15 lines, so LibreOffice documents keep their vertical rhythm on open and save (gap 19). Readiness unchanged. - 2026-10-31: DOCX: a paragraph naming a style the file doesn't define is formatted as Normal, as in Word, not with the document defaults alone, so a Pandoc footer naming an undefined `Footer` style is as tall as Word's and no longer pushes the last paragraph to the next page (#5077). Readiness unchanged. diff --git a/crates/formats/src/html/export.rs b/crates/formats/src/html/export.rs index 84aae0a8fd..167042506b 100644 --- a/crates/formats/src/html/export.rs +++ b/crates/formats/src/html/export.rs @@ -137,6 +137,7 @@ pub fn inlines_html(inlines: &[Inline]) -> String { )); } Inline::Anchor(a) => o.push_str(&format!("", esc(a))), + Inline::Note { .. } => {} Inline::Field { result, fmt, .. } => { let (open, close) = fmt_tags(fmt); let at_start = o.is_empty(); diff --git a/crates/formats/src/latex/export.rs b/crates/formats/src/latex/export.rs index 77aa3a054c..80654f3321 100644 --- a/crates/formats/src/latex/export.rs +++ b/crates/formats/src/latex/export.rs @@ -240,7 +240,7 @@ impl Writer { o.push_str(&format!("\\textit{{[{}]}}", escape_text(&alt))); } Inline::Anchor(name) => o.push_str(&format!("\\hypertarget{{{}}}{{}}", escape_url(name))), - Inline::Figure(_) => {} + Inline::Figure(_) | Inline::Note { .. } => {} Inline::Field { result, fmt, .. } => { let s = self.span(result, fmt); push_apart(&mut o, &s); diff --git a/crates/formats/src/markdown/export.rs b/crates/formats/src/markdown/export.rs index 7dc8856081..70b6e56837 100644 --- a/crates/formats/src/markdown/export.rs +++ b/crates/formats/src/markdown/export.rs @@ -135,6 +135,7 @@ fn inlines_md_impl(inlines: &[Inline], in_table: bool) -> String { Inline::Figure(alt) => pieces.push((String::new(), Fmt::default(), Some(format!("![{}]()", escape(alt))))), Inline::Equation { .. } => {} Inline::Field { result, fmt, .. } => pieces.push((result.clone(), fmt.clone(), None)), + Inline::Note { .. } => {} // Read as its base text by `ruby_as_text` above. Inline::Ruby { .. } => {} } diff --git a/crates/formats/src/model/from_doc.rs b/crates/formats/src/model/from_doc.rs index b4abdac04b..344e32af33 100644 --- a/crates/formats/src/model/from_doc.rs +++ b/crates/formats/src/model/from_doc.rs @@ -251,6 +251,19 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { // attributes (alignment, size, raise) stay in the document model. out.inlines.push(Inline::Ruby { base: base.clone(), ruby: ruby.clone(), fmt: f.clone() }); } + Some(InlineObject::NoteRef { kind, id, custom }) if opts.notes => { + if !buf.is_empty() { + out.push_text(&std::mem::take(&mut buf), &f); + } + // The note's content without its own mark (a note holds no notes). + let story = doc.parts.get(id).map(|part| &part.blocks); + let mut blocks = story.map(|b| flow_blocks(doc, b, 0, FlowOpts { notes: false, ..opts })).unwrap_or_default(); + if let Some(FBlock::Para(first)) = blocks.first_mut() { + first.trim_with(&[' ', '\t'], &[]); + } + let endnote = *kind == wordcraft_doc::para::NoteKind::Endnote; + out.inlines.push(Inline::Note { endnote, custom: custom.clone(), blocks }); + } Some(InlineObject::Field { instr, result, .. }) if opts.fields => { if !buf.is_empty() { out.push_text(&std::mem::take(&mut buf), &f); diff --git a/crates/formats/src/model/to_doc.rs b/crates/formats/src/model/to_doc.rs index c9fec5b902..257b1588aa 100644 --- a/crates/formats/src/model/to_doc.rs +++ b/crates/formats/src/model/to_doc.rs @@ -162,6 +162,43 @@ impl<'a> Builder<'a> { cp } + /// A note story holding `blocks`, as Word's: its paragraphs in the note text style, the first + /// starting with the note's mark and a space. The reference to put in the text, or `None` + /// when the document has no room for another story. + fn note(&mut self, endnote: bool, custom: &str, blocks: &[FBlock]) -> Option { + use wordcraft_doc::para::NoteKind; + let (kind, part, text, mark) = if endnote { + (NoteKind::Endnote, wordcraft_doc::PartKind::Endnote, "EndnoteText", "EndnoteReference") + } else { + (NoteKind::Footnote, wordcraft_doc::PartKind::Footnote, "FootnoteText", "FootnoteReference") + }; + let custom: String = clean_text(custom).chars().filter(|c| !c.is_control()).take(10).collect(); + let id = self.doc.try_add_part(part, Vec::new()).ok()?; + let mut story = self.blocks(blocks, 1); + if story.is_empty() { + story.push(para_block(Paragraph::new())); + } + for (i, b) in story.iter_mut().enumerate() { + if let Block::Para(p) = Arc::make_mut(b) { + if p.props.style.is_none() { + p.props.style = Some(text.into()); + } + if i == 0 { + let starts_blank = p.text.starts_with([' ', '\t']); + let reference = InlineObject::NoteRef { kind, id, custom: custom.clone() }; + if p.insert_object(0, reference, &CharProps { style: Some(mark.into()), ..Default::default() }).is_ok() && !starts_blank { + let at = p.text.len().min(wordcraft_doc::para::OBJ.len_utf8()); + let _ = p.insert_text(at, " ", &CharProps::default()); + } + } + } + } + if let Some(part) = self.doc.parts.get_mut(&id) { + part.blocks = story; + } + Some(InlineObject::NoteRef { kind, id, custom }) + } + fn para(&mut self, p: &Para, list_num: Option) -> Paragraph { let mut out = Paragraph::new(); out.props = para_props(p, list_num); @@ -218,6 +255,12 @@ impl<'a> Builder<'a> { append(&mut out, "", &CharProps::default(), Some(InlineObject::BookmarkEnd { name: name.clone() })); } Inline::Figure(_) => {} + Inline::Note { endnote, custom, blocks } => { + if let Some(obj) = self.note(*endnote, custom, blocks) { + let style = if *endnote { "EndnoteReference" } else { "FootnoteReference" }; + append(&mut out, "", &CharProps { style: Some(style.into()), ..Default::default() }, Some(obj)); + } + } Inline::Field { instr, result, fmt } => { let cp = self.budget(char_props(fmt)); let (instr, result) = (clean_text(instr), clean_text(result)); diff --git a/crates/formats/src/model/types.rs b/crates/formats/src/model/types.rs index 056fe0b942..9ab2428f64 100644 --- a/crates/formats/src/model/types.rs +++ b/crates/formats/src/model/types.rs @@ -173,6 +173,14 @@ pub enum Inline { result: String, fmt: Fmt, }, + /// A footnote or endnote: its custom mark (empty: numbered) and its content, without the + /// note's own mark. Only flows asked for notes ([`FlowOpts::notes`]) hold them; formats + /// without notes leave them out. + Note { + endnote: bool, + custom: String, + blocks: Vec, + }, } /// What [`super::from_doc_with`] keeps besides text and formatting. @@ -182,6 +190,8 @@ pub struct FlowOpts { pub tracked: bool, /// Fields as [`Inline::Field`] (otherwise their result is text). pub fields: bool, + /// Footnotes and endnotes as [`Inline::Note`] (otherwise left out). + pub notes: bool, } /// Base text size (points) a phonetic guide falls back to when the source states none: the @@ -290,7 +300,7 @@ impl Para { Inline::Text(t, _) | Inline::Equation { linear: t, .. } | Inline::Ruby { base: t, .. } | Inline::Field { result: t, .. } => { s.push_str(t) } - Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) => {} + Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) | Inline::Note { .. } => {} } } s @@ -306,7 +316,7 @@ impl Para { _ => s.push_str(t), }, Inline::Equation { linear: t, .. } | Inline::Ruby { base: t, .. } | Inline::Field { result: t, .. } => s.push_str(t), - Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) => {} + Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) | Inline::Note { .. } => {} } } s diff --git a/crates/formats/src/odt/body.rs b/crates/formats/src/odt/body.rs index 4d68945e06..9e34a1d33c 100644 --- a/crates/formats/src/odt/body.rs +++ b/crates/formats/src/odt/body.rs @@ -159,6 +159,29 @@ pub(super) struct Body<'a> { active_ins: Vec<(String, RevMark)>, /// The text field being read. field: Option, + /// The note being read. + note: Option, +} + +/// A footnote or endnote being read (ODF 1.3 §6.3): what it is, and the reader state of the +/// paragraph it sits in, put aside while its content is read into a container of its own. +struct OpenNote { + /// The `text:note` element's depth. + depth: usize, + endnote: bool, + /// The citation's `text:label` (a custom mark). + custom: String, + para: Option, + fmt: Vec, + xml_space_end: bool, + para_cont: bool, + lists: Vec<(Option, bool)>, + item_used: Vec, + /// Stack heights when the note began: what the note opens and leaves open goes with it. + containers: usize, + tables: usize, + cells: usize, + lists_saved: usize, } /// A text field being read ([`super::fields`]). @@ -211,6 +234,7 @@ impl<'a> Body<'a> { del: None, active_ins: Vec::new(), field: None, + note: None, } } @@ -523,7 +547,7 @@ impl Body<'_> { if self.tracked_context(q, a) { return; } - if self.start_inline(q, a) { + if self.start_inline(q, a) || self.start_note_part(q, a) { return; } match local(q) { @@ -885,6 +909,98 @@ impl Body<'_> { true } + /// A note or its citation starting: true when handled. A note inside a note (allowed by the + /// schema, never written by Word or LibreOffice), or one in deleted text or a frame, is left + /// out with its content. + fn start_note_part(&mut self, q: &str, a: &Attrs) -> bool { + if !q.starts_with("text:") { + return false; + } + match local(q) { + "note" if self.note.is_none() && self.del.is_none() && !self.in_tc && self.frame.is_none() => self.start_note(a), + // The citation's text is LibreOffice's number: only a custom mark is kept. + "note-citation" => { + if let Some(n) = &mut self.note { + n.custom = get(a, "text:label").map(|l| l.chars().filter(|c| !c.is_control()).take(10).collect()).unwrap_or_default(); + } + self.skip = 1; + } + _ => return false, + } + true + } + + /// Begin reading a note: put the paragraph it is in aside and read its content into a + /// container of its own, outside any list. + fn start_note(&mut self, a: &Attrs) { + let note = OpenNote { + depth: self.depth, + endnote: get(a, "text:note-class").is_some_and(|c| c.trim() == "endnote"), + custom: String::new(), + para: self.para.take(), + fmt: std::mem::take(&mut self.fmt), + xml_space_end: std::mem::take(&mut self.xml_space_end), + para_cont: std::mem::take(&mut self.para_cont), + lists: std::mem::take(&mut self.lists), + item_used: std::mem::take(&mut self.item_used), + containers: self.containers.len(), + tables: self.tables.len(), + cells: self.cells.len(), + lists_saved: self.lists_saved.len(), + }; + self.containers.push(Vec::new()); + self.note = Some(note); + } + + /// End the note being read: its content becomes an [`Inline::Note`] in the paragraph put + /// aside, which goes on. Tables and cells the note left open (malformed) are dropped. + fn end_note(&mut self) { + self.end_para(); + let Some(n) = self.note.take() else { return }; + self.tables.truncate(n.tables); + self.cells.truncate(n.cells); + self.lists_saved.truncate(n.lists_saved); + self.containers.truncate(n.containers + 1); + let blocks = if self.containers.len() > n.containers { self.containers.pop().unwrap_or_default() } else { Vec::new() }; + self.para = n.para; + self.fmt = n.fmt; + self.xml_space_end = n.xml_space_end; + self.para_cont = n.para_cont; + self.lists = n.lists; + self.item_used = n.item_used; + if self.para.is_none() { + self.start_para(None, None); + } + if let Some(p) = &mut self.para { + p.inlines.push(Inline::Note { endnote: n.endnote, custom: n.custom, blocks }); + } + } + + /// An element ending at the current depth, for an open note or field: true when that was all + /// there was to it. + fn end_note_or_field(&mut self, q: &str) -> bool { + if self.note.as_ref().is_some_and(|n| self.depth < n.depth) { + // The note's own end, or (malformed) an outer element ending first: the note ends, + // then that element. + self.end_field(); + self.end_note(); + if local(q) == "note" { + return true; + } + } + // Everything inside a field was its text: only the field's own end does anything. An + // outer element ending first (malformed) ends the field, then itself. + if let Some(f) = &self.field { + if self.depth >= f.depth { + return true; + } + let own = f.name == q; + self.end_field(); + return own; + } + false + } + /// Put the field read so far into the paragraph. fn end_field(&mut self) { let Some(OpenField { instr, result, mut fmt, .. }) = self.field.take() else { return }; @@ -900,20 +1016,14 @@ impl Body<'_> { fn end(&mut self, q: &str) { self.depth = self.depth.saturating_sub(1); - // Everything inside a field was its text: only the field's own end does anything. An - // outer element ending first (malformed) ends the field, then itself. - if let Some(f) = &self.field { - if self.depth >= f.depth { - return; - } - let own = f.name == q; - self.end_field(); - if own { - return; - } + if self.end_note_or_field(q) { + return; } if q == "office:text" { self.end_field(); + if self.note.is_some() { + self.end_note(); + } if self.del.is_some() { // Unclosed deleted content (hostile): drop it. self.close_deletion(); diff --git a/crates/formats/src/odt/package.rs b/crates/formats/src/odt/package.rs index 1d57fe6518..e06848a4e5 100644 --- a/crates/formats/src/odt/package.rs +++ b/crates/formats/src/odt/package.rs @@ -38,7 +38,7 @@ fn page_parts(doc: &Document, sec: &SectionProps, w: &mut Writer) -> PageParts { } let mut body = String::new(); if let Some(part) = id.and_then(|i| doc.parts.get(&i)) { - let blocks = model::part_flow(doc, &part.blocks, model::FlowOpts { tracked: false, fields: true }); + let blocks = model::part_flow(doc, &part.blocks, model::FlowOpts { fields: true, ..Default::default() }); w.blocks(&blocks, &mut body, 0); } if body.is_empty() { @@ -84,6 +84,12 @@ fn styles_xml( s.push_str(""); s.push_str(""); s.push_str(&format!("", model::MONO_FONT)); + // The note text styles, as WordCraft's Footnote Text and Endnote Text: 10 pt, no spacing. + for (name, class) in [("Footnote", "extra"), ("Endnote", "extra")] { + s.push_str(&format!( + "" + )); + } s.push_str(""); s.push_str(part_styles); let none = PageParts::default(); @@ -181,7 +187,7 @@ fn flow_sections(blocks: &[FBlock], last: &SectionProps) -> Vec<(SectionProps, s /// Write an `.odt` package. pub fn export(doc: &Document) -> Result, String> { - let flow = model::from_doc_with(doc, model::FlowOpts { tracked: true, fields: true }); + let flow = model::from_doc_with(doc, model::FlowOpts { tracked: true, fields: true, notes: true }); let mut w = Writer::default(); let mut body = String::new(); let sections = flow_sections(&flow.blocks, &doc.last_section); diff --git a/crates/formats/src/odt/tests.rs b/crates/formats/src/odt/tests.rs index 729334beab..601a1fd846 100644 --- a/crates/formats/src/odt/tests.rs +++ b/crates/formats/src/odt/tests.rs @@ -1135,3 +1135,9 @@ fn meta_creation_date_timezones_and_fallbacks() { assert_eq!(m2.created, "2024-03-05T13:45:00Z"); assert_eq!(m2.author, "Solo Creator"); } + +/// The `content.xml` of a package. +pub(super) fn content_of(bytes: &[u8]) -> String { + let mut z = archive(bytes); + String::from_utf8(read_entry(&mut z, "content.xml").unwrap()).unwrap() +} diff --git a/crates/formats/src/odt/tests_more.rs b/crates/formats/src/odt/tests_more.rs index 9becd96901..1272873822 100644 --- a/crates/formats/src/odt/tests_more.rs +++ b/crates/formats/src/odt/tests_more.rs @@ -2,4 +2,5 @@ mod fields; mod headers; +mod notes; mod spacing; diff --git a/crates/formats/src/odt/tests_more/notes.rs b/crates/formats/src/odt/tests_more/notes.rs new file mode 100644 index 0000000000..60302584f1 --- /dev/null +++ b/crates/formats/src/odt/tests_more/notes.rs @@ -0,0 +1,122 @@ +//! Footnotes and endnotes (ODF 1.3 §6.3 `text:note`): read into note stories, written back. + +use crate::odt::tests::{NS_T, package}; +use crate::odt::{export, import}; +use wordcraft_doc::para::{InlineObject, NoteKind, OBJ}; +use wordcraft_doc::{Document, PartKind}; + +fn body(xml: &str) -> Vec { + package( + &format!(r#"{xml}"#), + None, + ) +} + +/// Every note referenced from the body, in order: (kind, custom mark, the note's text). +fn notes(d: &Document) -> Vec<(NoteKind, String, String)> { + let mut out = Vec::new(); + for b in d.body.iter() { + let Some(p) = b.as_para() else { continue }; + for o in &p.objects { + if let InlineObject::NoteRef { kind, id, custom } = o { + let part = d.parts.get(id).expect("the note's story"); + assert_eq!(part.kind, if *kind == NoteKind::Footnote { PartKind::Footnote } else { PartKind::Endnote }); + let text = part.blocks.iter().filter_map(|b| b.as_para()).map(|p| p.text.replace(OBJ, "")).collect::>().join("|"); + out.push((*kind, custom.clone(), text.trim().to_string())); + } + } + } + out +} + +const NOTES: &str = concat!( + r#"Text1The footnote body."#, + r#" andiEnd one.End two."#, + r#" a mark*Starred.."#, + "After." +); + +#[test] +fn notes_open_as_note_stories() { + let d = import(&body(NOTES)).unwrap(); + assert_eq!( + notes(&d), + [ + (NoteKind::Footnote, String::new(), "The footnote body.".to_string()), + (NoteKind::Endnote, String::new(), "End one.|End two.".to_string()), + (NoteKind::Footnote, "*".to_string(), "Starred.".to_string()), + ] + ); + // The citation numbers are not text; the paragraph goes on after each note. + let p = d.body[0].as_para().unwrap(); + assert_eq!(p.text.replace(OBJ, "#"), "Text# and# a mark#."); + assert_eq!(d.body[1].as_para().unwrap().text, "After."); + // As Word's: the reference is in the reference style, the note's text in the note style and + // starts with its own mark. + let first = d.parts.values().find(|p| p.kind == PartKind::Footnote).unwrap(); + let np = first.blocks[0].as_para().unwrap(); + assert_eq!(np.props.style.as_deref(), Some("FootnoteText")); + assert!(matches!(np.objects.first(), Some(InlineObject::NoteRef { .. }))); + assert_eq!(p.runs.iter().filter(|r| r.props.style.as_deref() == Some("FootnoteReference")).count(), 2); +} + +#[test] +fn notes_round_trip() { + let d = import(&body(NOTES)).unwrap(); + let back = import(&export(&d).unwrap()).unwrap(); + assert_eq!(notes(&back), notes(&d)); + assert_eq!(back.body[0].as_para().unwrap().text, d.body[0].as_para().unwrap().text); + // Saving again changes nothing more. + assert_eq!(notes(&import(&export(&back).unwrap()).unwrap()), notes(&d)); +} + +#[test] +fn a_note_keeps_formatting_and_the_text_around_it() { + let content = format!( + r#"bold1in note still bold"# + ); + let d = import(&package(&content, None)).unwrap(); + let p = d.body[0].as_para().unwrap(); + assert!(p.props.numbering.is_some(), "the list survives the note"); + let tail = p.runs.last().unwrap(); + assert_eq!(tail.props.bold, Some(true)); + assert_eq!(p.text.replace(OBJ, "#"), "bold# still bold"); + let note = d.parts.values().find(|p| p.kind == PartKind::Footnote).unwrap().blocks[0].as_para().unwrap().clone(); + assert!(note.runs.iter().any(|r| r.props.bold == Some(true))); + assert!(note.props.numbering.is_none(), "the body's list doesn't reach into the note"); +} + +#[test] +fn hostile_notes_do_not_panic() { + let deep = "".repeat(200); + for xml in [ + "a".to_string(), + "ab".to_string(), + "xy".to_string(), + format!("{deep}"), + "outside a paragraph".to_string(), + "x".to_string(), + ] { + let d = import(&body(&xml)).unwrap(); + let _ = export(&d).unwrap(); + } +} + +#[test] +fn a_note_inside_a_table_cell_is_kept() { + let xml = r#"cellicell note"#; + let d = import(&body(xml)).unwrap(); + let back = import(&export(&d).unwrap()).unwrap(); + let ends: Vec<_> = back.parts.values().filter(|p| p.kind == PartKind::Endnote).collect(); + assert_eq!(ends.len(), 1); + assert_eq!(ends[0].blocks[0].as_para().unwrap().text.replace(OBJ, "").trim(), "cell note"); +} + +#[test] +fn citations_number_footnotes_and_endnotes_apart() { + let d = import(&body(NOTES)).unwrap(); + let xml = crate::odt::tests::content_of(&export(&d).unwrap()); + let citations: Vec<&str> = + xml.split("')).map(|(_, rest)| rest.split('<').next().unwrap_or("")).collect(); + assert_eq!(citations, ["1", "i", "*"]); +} diff --git a/crates/formats/src/odt/writer.rs b/crates/formats/src/odt/writer.rs index aad25abd68..89bb20a3a0 100644 --- a/crates/formats/src/odt/writer.rs +++ b/crates/formats/src/odt/writer.rs @@ -45,6 +45,12 @@ type SecStart = (String, Option); #[derive(Default)] pub(super) struct Writer { + /// Notes written so far (their ids), and footnotes and endnotes among them (their numbers). + notes: usize, + footnotes: u32, + endnotes: u32, + /// Paragraph style that plain paragraphs get: the note style inside a note. + note_parent: Option<&'static str>, /// Prefix of every name it makes (styles, lists, tables, pictures): headers and footers are /// written by a second writer whose styles go in `styles.xml`, apart from the body's. pub(super) prefix: &'static str, @@ -184,7 +190,10 @@ impl Writer { /// The paragraph style of `p`; `start` makes it begin a page of that master page (ODF 1.3 /// §19.471 `style:master-page-name`), restarting page numbering where asked (§20.322). fn para_style_at(&mut self, p: &Para, start: Option<&SecStart>) -> String { - let parent = parent_style(p.kind).to_string(); + let parent = match (p.kind, self.note_parent) { + (Kind::Normal, Some(n)) => n.to_string(), + _ => parent_style(p.kind).to_string(), + }; let mut props = String::new(); match p.align { Some(Align::Center) => props.push_str(" fo:text-align=\"center\""), @@ -422,6 +431,10 @@ impl Writer { prev_space = false; } Inline::Anchor(a) => out.push_str(&format!("", x(a))), + Inline::Note { endnote, custom, blocks } => { + self.note(*endnote, custom, blocks, out); + prev_space = false; + } Inline::Field { instr, result, fmt } => match super::fields::element(instr, &x(result)) { Some(el) => { match self.text_style(&Fmt { link: None, ..fmt.clone() }) { @@ -443,6 +456,41 @@ impl Writer { } } + /// A footnote or endnote (ODF 1.3 §6.3): its citation (the number LibreOffice shows until it + /// renumbers, or the custom mark) and its content. The paragraph being written is put aside + /// meanwhile, and an insertion open around the note ends before it and goes on after it. + fn note(&mut self, endnote: bool, custom: &str, blocks: &[FBlock], out: &mut String) { + let reopen = self.open_ins.as_ref().map(|(m, _)| m.clone()); + self.close_ins(out); + self.notes += 1; + let id = format!("{}ftn{}", self.prefix, self.notes); + let (class, parent) = if endnote { ("endnote", "Endnote") } else { ("footnote", "Footnote") }; + let saved = (std::mem::take(&mut self.cur_style), std::mem::take(&mut self.chain), self.part, self.note_parent.replace(parent)); + let mut body = String::new(); + self.blocks(blocks, &mut body, 1); + self.close_ins(&mut body); + (self.cur_style, self.chain, self.part, self.note_parent) = saved; + if body.is_empty() { + body.push_str(&format!("")); + } + // The number LibreOffice shows by default (footnotes 1, 2, 3; endnotes i, ii, iii) until it + // renumbers them itself. + let number = if endnote { + self.endnotes = self.endnotes.saturating_add(1); + wordcraft_doc::section::roman(self.endnotes) + } else { + self.footnotes = self.footnotes.saturating_add(1); + self.footnotes.to_string() + }; + let (label, citation) = if custom.is_empty() { (String::new(), number) } else { (format!(" text:label=\"{}\"", x(custom)), x(custom)) }; + out.push_str(&format!( + "{citation}{body}" + )); + if let Some(m) = reopen { + self.open_ins(&m, out); + } + } + /// Paragraph `p`, or with `chain` the paragraphs joined at deleted paragraph marks (`p` /// is the first). fn para_in(&mut self, p: &Para, chain: Option<&Vec>, out: &mut String) { diff --git a/crates/formats/src/rtf/export.rs b/crates/formats/src/rtf/export.rs index 2209857a55..9256ffc653 100644 --- a/crates/formats/src/rtf/export.rs +++ b/crates/formats/src/rtf/export.rs @@ -400,7 +400,7 @@ impl Writer { // RTF keeps no ruby yet: its base text, in the run's formatting. Inline::Ruby { base, fmt, .. } => self.run(base, fmt, out), Inline::Field { result, fmt, .. } => self.run(result, fmt, out), - Inline::Figure(_) | Inline::Equation { .. } => {} + Inline::Figure(_) | Inline::Equation { .. } | Inline::Note { .. } => {} } } } diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 90c927a5f3..68abb860ec 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -1,6 +1,6 @@ # File-format parity with Microsoft Word -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-31 · **Change:** trivial (ODT headers, footers and text fields written; fields read); previously trivial (ODT paragraph spacing, indents and line spacing read and written); previously trivial (a paragraph naming an undefined style is formatted as Normal, as in Word, #5077); previously trivial (a hyperlinked TOC writes its page numbers as `PAGEREF _Toc… \h` fields, as Word does, #5097); previously trivial (HTML import ends the empty comments and at once, #5040); previously trivial (chart and SmartArt text the font lacks is drawn in a fallback face, so Chinese labels no longer show boxes, #4720); previously trivial (bibliography sources keep a corporate author (`b:Corporate`): read, written, cited and listed whole, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (note conversion preserves content with an ordered reference/run walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (built-in heading styles have their outline level without `w:outlineLvl`, #4696); previously trivial (DOCX comment ranges inside footnotes and endnotes are kept on read, #4548); previously trivial (RTF import and export keep Windows and Enhanced Metafile pictures (`\wmetafile`, `\emfblip`), #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (DOCX parts stored under another casing keep their embedded fonts through a save, and a private save of a kept glossary escapes the attribute values it rewrites; previously trivial: the `.doc` reader logs what its copy budget leaves out, once a kind per document; importers bound hostile expansion: font names, links, ODS repeats, LaTeX wrappers, Markdown delimiters, #3692/#3713; previously trivial: repeated DOCX style IDs: save/reopen regression; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: Arabic list labels: `arabicAlpha` and `arabicAbjad` draw the 28 Arabic letters (alphabetical and abjad order), doubling past the last letter; Hebrew, Hindi, Thai and Vietnamese still show decimal (`doc::section::tests::arabic_letters_count_in_both_orders`); previously trivial: localized TOC style ids read as the TOC styles, and a TOC in columns keeps its layout through Update Table, #3889; previously trivial: cu20we: outline caps; previously trivial: a relationship the markup names without the prefix is read as it wrote: an unqualified `id` on an element that carries a relationship names that relationship, and the writer keeps the attribute with the id the new package holds the part under; an ordinary `id` — a VML shape's, a drawing's — names no part; previously trivial: the reader takes the relationship ids the markup refers to from the relationship attributes of the main document it has already parsed, so a document with many relationships opens in the time its part takes, #3411; previously trivial: a table cell span from a `.json` file stays within 63 columns through Insert Column and Merge Cells, #3637; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: `majorBidi`/`minorBidi` theme fonts follow the settings' theme font language, as measured in Word; previously trivial: Word signature lines are kept on save, still signable; previously trivial: Times New Roman keeps Liberation Serif under the roman-font rule; previously trivial: shape gradients and transparency, #1500; previously trivial: a folder that keeps no Unix modes, such as a NAS share mounted by GVFS, still takes a save, #3003; previously trivial: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: table style band sizes written, a style without them unbanded as in Word, #3415; previously trivial: HTML export writes ruby (furigana, phonetic guides) and HTML import reads it back, #1681; previously trivial: RTF import decodes the East Asian multibyte code pages, #1653; previously trivial: PDF export: lam-alef drawn as two glyphs at one x extracts in reading order, #2310; previously trivial: native .json stores pictures and kept parts as base64, #586 follow-up; previously trivial: ODT headers and footers read from each master page, #863; previously trivial: RTF import compares heading and code formatting with the file's stylesheet; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: Word XML and web pages saved as `.doc` open by what they hold, #2853; previously trivial: a VML shape in a header cell is always placed on the page, user-checked in Word for Mac, #1775; previously trivial: `w:sym` characters keep their symbol font when the text's font changes and are written back as `w:sym`, #2036; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: two lines in one and horizontal in vertical (`w:eastAsianLayout`) kept on save; previously trivial: PDF outline targets follow leading page breaks; previously trivial: positional tabs `w:ptab` read and kept on save, #2367; previously trivial: DOCX: mixed right-to-left runs split and marked `w:rtl` with their language, #1543; previously trivial: `w:doNotTrackMoves` round-trips and turns move tracking off; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: DOCX glossary documents (building blocks) read, kept and written; previously trivial: cached field results keep their visible run formatting; previously trivial: cached REF results retain hyperlink destinations; previously trivial: CLI conversions can explicitly refresh fields; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: a pasted HTML `
` is an indented paragraph, not the Quote style, #1270); previously trivial: every table style part round-trips, #714; previously trivial: VML freeforms read and kept on save; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: `flowChartOr` draws its circle and cross spokes, as in Word; previously trivial: RTF: Mac OS Roman and DOS text, #1653; previously trivial: VML text boxes keep their internal margins, fit to their text and vertical alignment; hidden drawings not shown; previously trivial: PDF export draws horizontal and vertical lines inside groups, #1474; previously trivial: symbol-font text exported as Unicode, #776; previously trivial: text-box margins, Resize shape to fit text and freeforms in groups, #743; previously trivial: formatted paste from the system clipboard's HTML and RTF, #687; previously trivial: independent complex-script formatting inheritance and round-trip regression tests; previously trivial: import/export fixes for TXT, RTF, ODT, HTML and LaTeX, #433–#440; previously major: first version; every format Word reads or writes, measured from the readers and writers on origin/main) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4, plus Word for Windows' extra formats +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-31 · **Change:** trivial (ODT footnotes and endnotes read and written); previously trivial (ODT headers, footers and text fields written; fields read); previously trivial (ODT paragraph spacing, indents and line spacing read and written); previously trivial (a paragraph naming an undefined style is formatted as Normal, as in Word, #5077); previously trivial (a hyperlinked TOC writes its page numbers as `PAGEREF _Toc… \h` fields, as Word does, #5097); previously trivial (HTML import ends the empty comments and at once, #5040); previously trivial (chart and SmartArt text the font lacks is drawn in a fallback face, so Chinese labels no longer show boxes, #4720); previously trivial (bibliography sources keep a corporate author (`b:Corporate`): read, written, cited and listed whole, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (note conversion preserves content with an ordered reference/run walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (built-in heading styles have their outline level without `w:outlineLvl`, #4696); previously trivial (DOCX comment ranges inside footnotes and endnotes are kept on read, #4548); previously trivial (RTF import and export keep Windows and Enhanced Metafile pictures (`\wmetafile`, `\emfblip`), #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (DOCX parts stored under another casing keep their embedded fonts through a save, and a private save of a kept glossary escapes the attribute values it rewrites; previously trivial: the `.doc` reader logs what its copy budget leaves out, once a kind per document; importers bound hostile expansion: font names, links, ODS repeats, LaTeX wrappers, Markdown delimiters, #3692/#3713; previously trivial: repeated DOCX style IDs: save/reopen regression; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: Arabic list labels: `arabicAlpha` and `arabicAbjad` draw the 28 Arabic letters (alphabetical and abjad order), doubling past the last letter; Hebrew, Hindi, Thai and Vietnamese still show decimal (`doc::section::tests::arabic_letters_count_in_both_orders`); previously trivial: localized TOC style ids read as the TOC styles, and a TOC in columns keeps its layout through Update Table, #3889; previously trivial: cu20we: outline caps; previously trivial: a relationship the markup names without the prefix is read as it wrote: an unqualified `id` on an element that carries a relationship names that relationship, and the writer keeps the attribute with the id the new package holds the part under; an ordinary `id` — a VML shape's, a drawing's — names no part; previously trivial: the reader takes the relationship ids the markup refers to from the relationship attributes of the main document it has already parsed, so a document with many relationships opens in the time its part takes, #3411; previously trivial: a table cell span from a `.json` file stays within 63 columns through Insert Column and Merge Cells, #3637; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: `majorBidi`/`minorBidi` theme fonts follow the settings' theme font language, as measured in Word; previously trivial: Word signature lines are kept on save, still signable; previously trivial: Times New Roman keeps Liberation Serif under the roman-font rule; previously trivial: shape gradients and transparency, #1500; previously trivial: a folder that keeps no Unix modes, such as a NAS share mounted by GVFS, still takes a save, #3003; previously trivial: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: table style band sizes written, a style without them unbanded as in Word, #3415; previously trivial: HTML export writes ruby (furigana, phonetic guides) and HTML import reads it back, #1681; previously trivial: RTF import decodes the East Asian multibyte code pages, #1653; previously trivial: PDF export: lam-alef drawn as two glyphs at one x extracts in reading order, #2310; previously trivial: native .json stores pictures and kept parts as base64, #586 follow-up; previously trivial: ODT headers and footers read from each master page, #863; previously trivial: RTF import compares heading and code formatting with the file's stylesheet; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: Word XML and web pages saved as `.doc` open by what they hold, #2853; previously trivial: a VML shape in a header cell is always placed on the page, user-checked in Word for Mac, #1775; previously trivial: `w:sym` characters keep their symbol font when the text's font changes and are written back as `w:sym`, #2036; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: two lines in one and horizontal in vertical (`w:eastAsianLayout`) kept on save; previously trivial: PDF outline targets follow leading page breaks; previously trivial: positional tabs `w:ptab` read and kept on save, #2367; previously trivial: DOCX: mixed right-to-left runs split and marked `w:rtl` with their language, #1543; previously trivial: `w:doNotTrackMoves` round-trips and turns move tracking off; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: DOCX glossary documents (building blocks) read, kept and written; previously trivial: cached field results keep their visible run formatting; previously trivial: cached REF results retain hyperlink destinations; previously trivial: CLI conversions can explicitly refresh fields; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: a pasted HTML `
` is an indented paragraph, not the Quote style, #1270); previously trivial: every table style part round-trips, #714; previously trivial: VML freeforms read and kept on save; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: `flowChartOr` draws its circle and cross spokes, as in Word; previously trivial: RTF: Mac OS Roman and DOS text, #1653; previously trivial: VML text boxes keep their internal margins, fit to their text and vertical alignment; hidden drawings not shown; previously trivial: PDF export draws horizontal and vertical lines inside groups, #1474; previously trivial: symbol-font text exported as Unicode, #776; previously trivial: text-box margins, Resize shape to fit text and freeforms in groups, #743; previously trivial: formatted paste from the system clipboard's HTML and RTF, #687; previously trivial: independent complex-script formatting inheritance and round-trip regression tests; previously trivial: import/export fixes for TXT, RTF, ODT, HTML and LaTeX, #433–#440; previously major: first version; every format Word reads or writes, measured from the readers and writers on origin/main) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4, plus Word for Windows' extra formats Every format Word opens or saves, what WordCraft does with it, and how it's tested. Fidelity is the share of a typical real-world file's content and formatting that survives (read: shows correctly; @@ -57,7 +57,7 @@ Weights are the share of a Word user's file traffic (estimate). Read/write: ✅ | PDF export | 8% | W | — | ✅ | ~80% | 6–10 | `crates/pdf/src/tests.rs` | krilla: embedded subsets, selectable text incl. field results (#133); Hebrew and Arabic copy and search as whole words (right-to-left glyphs written in visual order, #1383, also with fonts that draw lam-alef as two glyphs at one x, #2310); All Caps and Small Caps text extracts as the capitals shown, as from Word's PDFs, and never changes the case of other text (#1473), links, outline, tagging. Outline destinations follow leading page breaks to the heading text. Fonts it can't subset become outlines (#132). Print Markup (#589): comments in balloons beside each page (scaled to its paper) and tracked changes; HTML/Markdown/text copies end with a comment list. Horizontal and vertical lines inside groups are drawn (#1474). No PDF/A option, no "best for printing/online" choice | | PDF open (PDF Reflow, Word for Windows) | 1% | R | ❌ | — | 0% | 30–50 | — | Converting a PDF into an editable document. A sibling app (PdfCraft) may supply the parser | | `.rtf` Rich Text Format | 3% | R/W | 🟡 | 🟡 | ~50% | 10–15 | `crates/formats/src/tests.rs` | ~1,500 lines: text, character/paragraph formatting, styles, lists, tables, pictures (`\shppict`, #154; Windows and Enhanced Metafiles, `\wmetafile` and `\emfblip`, read and written, #4259), fields, bookmarks. 8-bit text in the document's code page (`\ansicpg`) or the current font's (`\fcharset`, `\cpg`): Cyrillic, Central European, Greek, Turkish, Hebrew, Arabic, Baltic, Vietnamese, Thai (was Windows-1252 only), Mac OS Roman (`\mac`, `\fcharset77`) and DOS code pages (`\pc` 437, `\pca` 850, `\cpg` 437/737/850/852/857/866, `\fcharset254`) (#1653). East Asian multibyte code pages decoded as runs: 932 Shift-JIS (`\fcharset128`, Japanese), 936 GBK (134, Simplified Chinese), 949 UHC/windows-949 (129, Korean) and 950 Big5 (136, Traditional Chinese) through `\ansicpg`, `\cpg` or the font's charset, escaped or raw, a lead byte and its trail byte together; invalid sequences read as U+FFFD, and a run that starts `EF BB BF` is text, not a BOM (#1653). Pasted from the system clipboard where the platform offers it (Windows, Wayland) when there is no HTML (#687). Tracked insertions and deletions read and written (`\revtbl`, `\revised`/`\deleted`, authors, `\revdttm` dates), so Accept/Reject All work after opening or a round trip; tracked paragraph marks (`\par` inside `\revised`/`\deleted` groups, including a `\sect` inside a deletion) and character and paragraph formatting revisions (`\crauth`/`\crdate` with `\oldcprops`, `\prauth`/`\prdate` with `\oldpprops`) read and written; section and table formatting revisions (`\oldsprops`, `\oldtprops`) not yet. Sections on import (#1378): each `\sect` ends a section with its own page size and orientation, margins, header/footer distance, columns, break type, page-number restart, start and format, title page and headers/footers (those it leaves out link to the previous); export writes every section too (break type, page size and orientation, margins, header/footer distance, columns, page numbering, title page; not headers/footers). Thin: comments, notes, RTL | -| `.odt` OpenDocument Text | 2% | R/W | 🟡 | 🟡 | ~50% | 8–12 | `crates/formats/src/tests.rs`, `crates/formats/src/odt.rs` unit tests, `crates/engine/tests/it/odt_sections.rs` | Styles, lists (#87), tables, pictures, page breaks (#86), paragraph spacing, indents and line spacing (proportional, fixed, at least; leading read as a proportional approximation) both ways, with the default and Standard styles' spacing as the document defaults (ODT's are none, not Word's 8 pt after and 1.15 lines), headers and footers of each master page (every, left and first page) read on open and written on save, with the header and footer distances converted between Word's page-edge measures and ODF's header bands (#863), text fields (page number and count, date, time, title, subject, author, file name, word and character counts, bookmark references as `REF`/`PAGEREF`) read as fields and written back as ODF fields; fixed dates and fields without a Word equivalent (sequences, chapters, variables) read as their text, and Word fields ODF lacks written as their result, date formats not kept, keep with next (#2813), an embedded formula object (`draw:object`) is read from its own `content.xml`'s MathML, same as HTML (#3099; export still drops equations, as before), page size, orientation and margins (#810), sections: one per master-page change on a paragraph or table (next page, with its own page size, orientation, margins, columns and page-numbering restart) and per columned `text:section` (continuous, with its columns and separator line) (#1379); export writes them back: a master page and page layout per new-page section (size, orientation, margins, columns, page-numbering restart on the first paragraph or table), a `text:section` with its columns per continuous section whose columns differ from the page's (one column included), a list cut by a section break goes on numbering. Limits: a continuous section's page-numbering restart isn't written (ODF applies it only with a master page); a continuous section with the page's columns merges into the section before it on reopening; even and odd page starts are written as plain new pages; a tracked deletion of a section break is lost; an empty last section is dropped; tracked insertions and deletions with author and date, inserted and deleted paragraph marks and text inserted then deleted included (ODF change tracking; format changes read as none; pictures and tables inside a tracked deletion are dropped). No footnotes, comments or RTL | +| `.odt` OpenDocument Text | 2% | R/W | 🟡 | 🟡 | ~50% | 8–12 | `crates/formats/src/tests.rs`, `crates/formats/src/odt.rs` unit tests, `crates/engine/tests/it/odt_sections.rs` | Styles, lists (#87), tables, pictures, page breaks (#86), paragraph spacing, indents and line spacing (proportional, fixed, at least; leading read as a proportional approximation) both ways, with the default and Standard styles' spacing as the document defaults (ODT's are none, not Word's 8 pt after and 1.15 lines), headers and footers of each master page (every, left and first page) read on open and written on save, with the header and footer distances converted between Word's page-edge measures and ODF's header bands (#863), text fields (page number and count, date, time, title, subject, author, file name, word and character counts, bookmark references as `REF`/`PAGEREF`) read as fields and written back as ODF fields; fixed dates and fields without a Word equivalent (sequences, chapters, variables) read as their text, and Word fields ODF lacks written as their result, date formats not kept, keep with next (#2813), an embedded formula object (`draw:object`) is read from its own `content.xml`'s MathML, same as HTML (#3099; export still drops equations, as before), page size, orientation and margins (#810), sections: one per master-page change on a paragraph or table (next page, with its own page size, orientation, margins, columns and page-numbering restart) and per columned `text:section` (continuous, with its columns and separator line) (#1379); export writes them back: a master page and page layout per new-page section (size, orientation, margins, columns, page-numbering restart on the first paragraph or table), a `text:section` with its columns per continuous section whose columns differ from the page's (one column included), a list cut by a section break goes on numbering. Limits: a continuous section's page-numbering restart isn't written (ODF applies it only with a master page); a continuous section with the page's columns merges into the section before it on reopening; even and odd page starts are written as plain new pages; a tracked deletion of a section break is lost; an empty last section is dropped; tracked insertions and deletions with author and date, inserted and deleted paragraph marks and text inserted then deleted included (ODF change tracking; format changes read as none; pictures and tables inside a tracked deletion are dropped). footnotes and endnotes (`text:note`) read into note stories as Word's (Footnote Text style, the note's own mark first, a custom mark kept) and written back with their citations; notes inside notes and in deleted text left out. No comments or RTL | | `.txt` Plain text | 2% | R/W | ✅ | ✅ | ~95% | 1–2 | unit tests | Encoding detection. Files with other extensions (`.m3u`/`.m3u8` playlists, logs, config files, no extension) open as text when they are text, Word and RTF files by their signature; binary files are refused (#569). A Word or RTF extension that contradicts the content also opens by signature, as in Word: RTF saved as `.doc` (hh.ru résumés, accounting exports), a `.docx` renamed `.doc`, a Word 97-2003 file renamed `.docx`; a Word XML Document (Word 2003 XML or Flat OPC) or a web page (``, ``) under a `.doc`, `.docx` or `.rtf` name opens as what it is (#2853, `tests::word_xml_and_web_pages_saved_as_doc_open_by_content`). Word's encoding choice dialog and line-ending options missing; such files save only through Save As | | `.htm/.html` Web Page and Web Page, Filtered | 1% | R/W | 🟡 | 🟡 | ~60% | 6–10 | `crates/formats/src/tests.rs` | Tables (#102), relative pictures embedded (#113), missing `` tolerated (#154); the empty comments `` and `` end at once instead of swallowing the rest of the page (#5040). Header cells (``) bold unless their CSS says otherwise; `text-align` on a cell, or inherited from its row, row group or table, aligns the cell's paragraphs (#1381). HTML gives no page size, so it opens on the region's paper (A4 or Letter), as Markdown and plain text do; `@page` isn't read (#1381). No Word "Web Page" (with `_files` folder) round-trip. The same reader takes HTML from the system clipboard: Paste keeps another program's formatting (a Word document, a web page; Windows' "HTML Format" header understood), Merge Formatting keeps its emphasis (#687). A pasted `
` (LibreOffice's paragraph indented on both sides) becomes a Normal paragraph indented by the page's `blockquote` margins, else 0.5 in each side; opening an .html file keeps it as the Quote style (#1270). Equations are written as MathML (with the linear format as a UnicodeMath annotation) and read back from MathML: our annotation, else the presentation elements, else a TeX annotation (#816). A copy of one equation alone also goes in the clipboard's MathML formats (Windows "MathML" and "MathML Presentation", macOS `public.mathml.presentation` and `public.mathml`; W3C MathML 3 §6.2.4), which Microsoft documents Word reading first when it pastes math (#1297; the paste into Word is not yet verified); Ruby (furigana, phonetic guides) is written as `basereading` in the base text's formatting and read back from ``/``/`` (`` fallback parentheses dropped; malformed markup keeps its text), so docx/doc → HTML → doc keeps the annotation; HTML states no alignment, size or raise, so the Phonetic Guide's defaults apply (#1681) CRLF and lone CR input newlines normalize before tokenization, preserving preformatted lines and leaving decoded character references separate (#4810). | | `.mht/.mhtml` Single File Web Page | <1% | R/W | ❌ | ❌ | 0% | 3–5 | — | | @@ -279,6 +279,7 @@ Custom document properties retain their bookmark link targets through DOCX saves | Date | Change | Summary | |---|---|---| +| 2026-10-31 | trivial | ODT: footnotes and endnotes (`text:note`, ODF 1.3 §6.3) open as note stories built as Word's (Footnote Text or Endnote Text paragraphs starting with the note's mark, the reference in the reference style, a `text:label` custom mark kept) and are written back with `text:note-citation`s numbered as LibreOffice shows them (footnotes 1, 2, 3; endnotes i, ii, iii); before, notes were dropped on open. `formats::odt::tests_more::notes` | | 2026-10-31 | trivial | ODT: headers and footers are written to each section's master page (every, left and first page), with Word's header and footer distances converted to ODF's header and footer bands and back on open; text fields (`text:page-number`, `text:page-count`, `text:date`, `text:time`, document information, `text:bookmark-ref`) open as WordCraft fields and are written back as ODF fields. `formats::odt::tests_more::{headers, fields}` | | 2026-10-31 | trivial | ODT: paragraph spacing, indents and line spacing (`fo:margin-*`, `fo:text-indent`, `fo:line-height`, `style:line-height-at-least`; `style:line-spacing` leading as a proportional approximation) read from automatic and common styles and written back; the default and Standard paragraph styles give the document defaults, so an ODT paragraph without spacing has none instead of Word's 8 pt after and 1.15 lines. `formats::odt::tests_more::spacing` | | 2026-10-31 | trivial | DOCX: a paragraph naming a style the file doesn't define is formatted as Normal, as in Word (#5077) | diff --git a/docs/gaps.md b/docs/gaps.md index c545cc8da9..6cce18241d 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where WordCraft falls short of Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (gap 19 narrowed: ODT headers and footers written, text fields and paragraph spacing read and written); previously trivial (only the PDF sent to the printer leaves out link annotations; Save these pages as PDF keeps them, #4734; no ranked gap changed); previously trivial (documents with links print: the print job leaves out link annotations, #4734; no ranked gap changed; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion reference-run membership uses one ordered walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (gap 19: RTF reads and writes WMF and EMF pictures, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (gap 19 narrowed: ODT footnotes and endnotes read and written); previously trivial (gap 19 narrowed: ODT headers and footers written, text fields and paragraph spacing read and written); previously trivial (only the PDF sent to the printer leaves out link annotations; Save these pages as PDF keeps them, #4734; no ranked gap changed); previously trivial (documents with links print: the print job leaves out link annotations, #4734; no ranked gap changed; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion reference-run membership uses one ordered walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (gap 19: RTF reads and writes WMF and EMF pictures, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Highlight update:** 2026-10-25 · Highlight shortcut toggles the last chosen colour off when the whole selection has it; explicit colour commands remain setters (#4085). Readiness estimates unchanged. > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-29 · **Change:** trivial (aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (#15: bibliography sources keep a corporate author, #4848); previously trivial (Table Properties applies to the selected rows, columns and cells, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (#7: Custom Document Properties dialog, #2982; previously trivial: egui 0.37 migration: the interface catalogs stay shared and pinned by `craft-i18n.rev` at `9205108cfdced5177706264038848b68b2422d2b` (27 languages, English fallback); input-method handling has new regressions but managed CI passed, and native IME acceptance plus native-speaker catalog review remain open; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: small follow-ups from the field, table and AutoFormat batch, #4232; previously trivial: highlight target includes selected paragraph marks, #4085; previously trivial: full-target highlight check beyond 10,000 runs, #4085; previously trivial: Exact-spacing table contact uses the specified line height rather than paragraph-mark metrics (#3977); previously trivial: cu20we: restrict pre-spacing float origins to paragraph anchors; document cap helpers and regressions; previously trivial: a command that changes nothing is not an edit: no undo step, no unsaved changes, no relayout, and the redo stack survives it; previously minor: #8 narrowed: Spanish proofing (Spain, Mexico, Latin America, Uruguay) from the installed Hunspell dictionaries, #3360, #3553; previously trivial: gap 10: group connector rerouting preserves transformed free and unresolved endpoints under nonuniform scaling; obstacle routing still missing; readiness estimates unchanged; previously trivial: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: the ranked gap list held two rows each for #19, #20 and #21; each gap appears once now; a document with many relationships opens in the time its part takes, #3411; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 @@ -81,7 +81,7 @@ Text context menus now include the existing formatting mini toolbar (#2959), inc | 15 | **References depth**: no EndNote/Mendeley desktop integration (bibliography sources are saved in DOCX, #790; Zotero landed, #189; Word's own `Sources.xml` master list imports into Manage Sources, #2640) | | #790 stores sources in `customXml` and reads them back; `crates/zotero`; `references.sources` `import` reads a Word bibliography file (`b:Sources`) | Students and researchers | 3–6 | [target-app-parity.md](target-app-parity.md) | | 16 | **Right-to-left completeness**: kashida, RTL in HTML/ODT/RTF; six still-open older RTL bug reports to re-verify after #207 (#211, #199, #66, #63, #48, #19; #215 is closed). RTL tables and sections landed (#362); PDF export's Hebrew and Arabic text copies and searches as whole words (#1383) | | [layout-parity.md](layout-parity.md) | Arabic, Persian, Hebrew writers | 7–11 | [layout-parity.md](layout-parity.md) | | 17 | **East Asian typography**: hint-reliant DFKai-SB contours repaired (local Windows regression; no readiness estimate change); Asian Typography options (Phonetic Guide landed, #288; Enclose Characters landed, #297; document grid landed, #391; vertical text landed, #491) | | `para.asianTypography` is live (Paragraph dialog Asian Typography tab, #323), automatic Asian/Latin and Asian/number spacing is laid out (#2358), and no East Asian item is left in the catalog's missing list; the remaining depth is not measured | Chinese, Japanese and Korean documents | 11–19 | [layout-parity.md](layout-parity.md) | -| 19 | **RTF and ODT depth**: notes, comments, revisions (RTF and ODT insertions and deletions done; RTF paragraph-mark and character/paragraph formatting revisions done; RTF section/table and ODT formatting revisions left), sections (RTF and ODT import and export done, #1378, #1379), RTL. ODT sections landed on import (#1379): page-style changes and columned `text:section`s; ODT table column widths are read (they were dropped on every open). ODT export writes sections back (a master page per new-page section, a columned `text:section` per continuous one) and their headers and footers; ODT paragraph spacing and text fields read and written; RTF export writes every section (`\sect`, `\sectd`); RTF reads and writes WMF and EMF pictures (#4259) | | ~2,100 (RTF) and ~1,600 (ODT) lines in `crates/formats`; RTF code pages (#896) and page setup (#902), ODT page setup (#829) landed; open PRs: RTF notes and comments (#1056), ODT notes and comments (#1057), ODT headers and footers (#873) | Users exchanging with LibreOffice or older tools | 18–27 | [file-format-parity.md](file-format-parity.md) | +| 19 | **RTF and ODT depth**: notes, comments, revisions (RTF and ODT insertions and deletions done; RTF paragraph-mark and character/paragraph formatting revisions done; RTF section/table and ODT formatting revisions left), sections (RTF and ODT import and export done, #1378, #1379), RTL. ODT sections landed on import (#1379): page-style changes and columned `text:section`s; ODT table column widths are read (they were dropped on every open). ODT export writes sections back (a master page per new-page section, a columned `text:section` per continuous one) and their headers and footers; ODT paragraph spacing, text fields, footnotes and endnotes read and written; RTF export writes every section (`\sect`, `\sectd`); RTF reads and writes WMF and EMF pictures (#4259) | | ~2,100 (RTF) and ~1,600 (ODT) lines in `crates/formats`; RTF code pages (#896) and page setup (#902), ODT page setup (#829) landed; open PRs: RTF notes and comments (#1056), ODT notes and comments (#1057), ODT headers and footers (#873) | Users exchanging with LibreOffice or older tools | 18–27 | [file-format-parity.md](file-format-parity.md) | | 18 | **Screen-reader access** to the document canvas: exposed, not yet tried with real screen readers | | The canvas describes paragraphs, headings, lists, tables, links, pictures and the caret to AccessKit (#488), tested headlessly; the ribbon's font name and size boxes, and the dropdown arrows beside them, say what they are (#3172); Windows Magnifier follows the caret, onto a new line too (#1119); no NVDA, VoiceOver or Orca run yet | Blind and low-vision users; public-sector procurement | 3–6 | [ui-parity.md](ui-parity.md) | | 20 | **Performance on large real-world documents** unmeasured (500+ pages, many pictures) | | DOCX comment saves avoid repeated paragraph-ID and durable-ID reservation searches; large threaded-comment fixtures retain metadata through save/reopen. Only the 188-page sample benchmark; scrolling a 1,096-page one-table document went from ~600 ms to ~2–5 ms a frame (#811), but opening it still lays out for seconds; opening a document with pictures no longer copies them (92 MB with 20 pictures: about 90 MB less); opening a document with many relationships now costs the size of its part and not that size times the number of relationships (1024 relationships and a 64 MiB part: 224 s → 0.2 s, #3411); a word with no break opportunity in it no longer lays out quadratically (1 MiB: 29.4 s → 0.35 s, #3412); saving a document with many content controls now costs the time the controls take (32,000 controls: 3.6 s → 0.1 s, #13) | Thesis and book authors | 10–20 | [layout-parity.md](layout-parity.md) | | 21 | **Mail merge data sources**: no Outlook/Contacts, Access or database sources, no email merge; CSV, typed lists (#247) and Excel/OpenDocument spreadsheets (#334, legacy .xls #778) only | | Select Recipients reads .csv/.tsv/.txt in any common encoding (UTF-8/16, GBK, Big5, Shift_JIS…), .xlsx/.xlsm, .xls and .ods; Edit Recipient List filters and sorts (#778), finds records and can show only the merged ones (#1576) | Office administrators | 4–8 | [target-app-parity.md](target-app-parity.md) | From 084d26a3c85fc18602f7d3b3de4d43dd8c0d17b9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 23:48:09 +0000 Subject: [PATCH 036/308] Gap 19: ODT comments read and written MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `office:annotation` (ODF 1.3 §14.1) opens as a document comment with its author, date, initials and text; `office:name` and `office:annotation-end` (§14.2) give its range, across paragraphs too. LibreOffice's resolved state (`loext:resolved`) and reply threads (`loext:parent-name`) are kept: a reply is a comment of its parent with no range of its own. An end with no comment is dropped, and a comment whose end never comes covers nothing. LibreOffice's all-zero date is no date. Before, comments were dropped on open. Export writes each comment named, with its end where its range ends, and its replies after it naming it; LibreOffice keeps the threads when it saves the file again. Truncated XML no longer loses the paragraph a note or comment sits in. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GBVQLyDnrG8Tn1yDQcUabC --- ROADMAP.md | 3 +- crates/formats/src/html/export.rs | 2 +- crates/formats/src/latex/export.rs | 2 +- crates/formats/src/markdown/export.rs | 2 +- crates/formats/src/model.rs | 5 +- crates/formats/src/model/from_doc.rs | 75 +++++-- crates/formats/src/model/to_doc.rs | 46 +++- crates/formats/src/model/types.rs | 50 ++++- crates/formats/src/odt.rs | 2 +- crates/formats/src/odt/body.rs | 198 ++++++++++++++++-- crates/formats/src/odt/limits.rs | 3 + crates/formats/src/odt/package.rs | 2 +- crates/formats/src/odt/tests_more.rs | 1 + crates/formats/src/odt/tests_more/comments.rs | 170 +++++++++++++++ crates/formats/src/odt/tests_more/notes.rs | 9 + crates/formats/src/odt/writer.rs | 46 ++++ crates/formats/src/rtf/export.rs | 2 +- docs/file-format-parity.md | 5 +- docs/gaps.md | 4 +- 19 files changed, 574 insertions(+), 53 deletions(-) create mode 100644 crates/formats/src/odt/tests_more/comments.rs diff --git a/ROADMAP.md b/ROADMAP.md index c49b6524af..38ebb2080a 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT footnotes and endnotes); previously trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT comments); previously trivial (progress log: ODT footnotes and endnotes); previously trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-29 · **Change:** trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -140,6 +140,7 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm ## Progress log +- 2026-10-31: ODT: comments open and save with their ranges, replies and resolved state, as LibreOffice keeps them (gap 19; they were dropped on open). Readiness unchanged. - 2026-10-31: ODT: footnotes and endnotes open and save (they were dropped on open), as Word-style note stories with their custom marks (gap 19). Readiness unchanged. - 2026-10-31: ODT: headers and footers are saved (they were dropped on every save), each page's header sits where LibreOffice puts it on open, and page numbers, page counts, dates and other text fields open as fields that update and are saved as ODF fields (gap 19). Readiness unchanged. - 2026-10-31: ODT: paragraph spacing, indents and line spacing are read and written, and an ODT's own default spacing (none, single lines) replaces Word's 8 pt after and 1.15 lines, so LibreOffice documents keep their vertical rhythm on open and save (gap 19). Readiness unchanged. diff --git a/crates/formats/src/html/export.rs b/crates/formats/src/html/export.rs index 167042506b..a913b5aaa3 100644 --- a/crates/formats/src/html/export.rs +++ b/crates/formats/src/html/export.rs @@ -137,7 +137,7 @@ pub fn inlines_html(inlines: &[Inline]) -> String { )); } Inline::Anchor(a) => o.push_str(&format!("", esc(a))), - Inline::Note { .. } => {} + Inline::Note { .. } | Inline::Comment { .. } | Inline::CommentEnd(_) => {} Inline::Field { result, fmt, .. } => { let (open, close) = fmt_tags(fmt); let at_start = o.is_empty(); diff --git a/crates/formats/src/latex/export.rs b/crates/formats/src/latex/export.rs index 80654f3321..20dd9ab038 100644 --- a/crates/formats/src/latex/export.rs +++ b/crates/formats/src/latex/export.rs @@ -240,7 +240,7 @@ impl Writer { o.push_str(&format!("\\textit{{[{}]}}", escape_text(&alt))); } Inline::Anchor(name) => o.push_str(&format!("\\hypertarget{{{}}}{{}}", escape_url(name))), - Inline::Figure(_) | Inline::Note { .. } => {} + Inline::Figure(_) | Inline::Note { .. } | Inline::Comment { .. } | Inline::CommentEnd(_) => {} Inline::Field { result, fmt, .. } => { let s = self.span(result, fmt); push_apart(&mut o, &s); diff --git a/crates/formats/src/markdown/export.rs b/crates/formats/src/markdown/export.rs index 70b6e56837..21951598b3 100644 --- a/crates/formats/src/markdown/export.rs +++ b/crates/formats/src/markdown/export.rs @@ -135,7 +135,7 @@ fn inlines_md_impl(inlines: &[Inline], in_table: bool) -> String { Inline::Figure(alt) => pieces.push((String::new(), Fmt::default(), Some(format!("![{}]()", escape(alt))))), Inline::Equation { .. } => {} Inline::Field { result, fmt, .. } => pieces.push((result.clone(), fmt.clone(), None)), - Inline::Note { .. } => {} + Inline::Note { .. } | Inline::Comment { .. } | Inline::CommentEnd(_) => {} // Read as its base text by `ruby_as_text` above. Inline::Ruby { .. } => {} } diff --git a/crates/formats/src/model.rs b/crates/formats/src/model.rs index 39e00cccc9..6af8b9a4a1 100644 --- a/crates/formats/src/model.rs +++ b/crates/formats/src/model.rs @@ -24,8 +24,9 @@ pub use from_doc::{ListCounter, flow_paras, from_doc, from_doc_tracked, from_doc pub use media::{base64_decode, base64_encode, data_uri, decode_bounded, image_px, make_img, mime_of, percent_decode, sized_img, sniff_image}; pub use to_doc::{char_props, part_blocks, to_doc, to_doc_in}; pub use types::{ - CODE_STYLE, Cell, FBlock, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, MAX_FONT_NAME, MAX_LINK, MONO_FONT, - Meta, Para, RUBY_BASE_SIZE, RevMark, Spacing, clean_text, equations_as_text, kept_font, kept_link, ruby_as_text, ruby_object, + CODE_STYLE, Cell, FBlock, FComment, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, MAX_FONT_NAME, MAX_LINK, + MONO_FONT, Meta, Para, RUBY_BASE_SIZE, RevMark, Spacing, clean_text, equations_as_text, flatten_tables, kept_font, kept_link, ruby_as_text, + ruby_object, }; pub(crate) use types::{Dropped, begin_import, dropped}; diff --git a/crates/formats/src/model/from_doc.rs b/crates/formats/src/model/from_doc.rs index 344e32af33..ef90a5bd4e 100644 --- a/crates/formats/src/model/from_doc.rs +++ b/crates/formats/src/model/from_doc.rs @@ -1,4 +1,6 @@ -use super::types::{Cell, FBlock, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, Meta, Para, RevMark, Spacing}; +use super::types::{ + Cell, FBlock, FComment, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, Meta, Para, RevMark, Spacing, +}; use super::{ Arc, Block, Blocks, CharProps, Document, InlineObject, NumFormat, OBJ, PAGE_BREAK, ParaProps, Paragraph, StyleKind, Table, TextColor, Underline, VMerge, VertAlign, @@ -123,6 +125,51 @@ fn media_ext(key: &str, data: &[u8]) -> String { } } +/// The flow inlines for a note reference, comment mark or field `o` of `doc`, when `opts` asks +/// for them; `None` for other objects. `f` is the formatting of the run `o` is in. +fn rich_inlines(doc: &Document, o: &InlineObject, f: &Fmt, opts: FlowOpts) -> Option> { + Some(match o { + InlineObject::NoteRef { kind, id, custom } if opts.notes => { + // The note's content without its own mark (a note holds no notes). + let story = doc.parts.get(id).map(|part| &part.blocks); + let mut blocks = story.map(|b| flow_blocks(doc, b, 0, FlowOpts { notes: false, ..opts })).unwrap_or_default(); + if let Some(FBlock::Para(first)) = blocks.first_mut() { + first.trim_with(&[' ', '\t'], &[]); + } + let endnote = *kind == wordcraft_doc::para::NoteKind::Endnote; + vec![Inline::Note { endnote, custom: custom.clone(), blocks }] + } + // The comment, then its replies, which have no place of their own. + InlineObject::CommentStart { id } if opts.comments => { + let Some(c) = doc.comments.get(id) else { return Some(Vec::new()) }; + let mut out = vec![Inline::Comment { id: *id, comment: Box::new(flow_comment(doc, c, None, opts)) }]; + for (rid, r) in doc.comments.iter().filter(|(_, r)| r.parent == Some(*id)) { + out.push(Inline::Comment { id: *rid, comment: Box::new(flow_comment(doc, r, Some(*id), opts)) }); + } + out + } + InlineObject::CommentEnd { id } if opts.comments => { + if doc.comments.contains_key(id) { + vec![Inline::CommentEnd(*id)] + } else { + Vec::new() + } + } + InlineObject::Field { instr, result, .. } if opts.fields => { + vec![Inline::Field { instr: instr.clone(), result: result.clone(), fmt: f.clone() }] + } + _ => return None, + }) +} + +/// Comment `c` (a reply to `parent`) of `doc` as a flow comment: its text holds no comments or +/// notes of its own. +fn flow_comment(doc: &Document, c: &wordcraft_doc::Comment, parent: Option, opts: FlowOpts) -> FComment { + let story = doc.parts.get(&c.part).map(|p| &p.blocks); + let blocks = story.map(|b| flow_blocks(doc, b, 0, FlowOpts { notes: false, comments: false, ..opts })).unwrap_or_default(); + FComment { author: c.author.clone(), initials: c.initials.clone(), date: c.date.clone(), resolved: c.resolved, parent, blocks } +} + /// The spacing and indents a paragraph sets itself (its style's are not included). fn direct_spacing(p: &wordcraft_doc::props::ParaProps) -> Spacing { Spacing { before: p.space_before, after: p.space_after, line: p.line_spacing, left: p.indent_left, right: p.indent_right, first: p.indent_first } @@ -206,6 +253,13 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { if hidden || obj.and_then(InlineObject::frame).is_some_and(|(_, _, f)| f.hidden) { continue; } + if let Some(items) = obj.and_then(|o| rich_inlines(doc, o, &f, opts)) { + if !buf.is_empty() { + out.push_text(&std::mem::take(&mut buf), &f); + } + out.inlines.extend(items); + continue; + } match obj { Some(o @ InlineObject::Image { media, w, h, alt, .. }) => { if !buf.is_empty() { @@ -251,25 +305,6 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { // attributes (alignment, size, raise) stay in the document model. out.inlines.push(Inline::Ruby { base: base.clone(), ruby: ruby.clone(), fmt: f.clone() }); } - Some(InlineObject::NoteRef { kind, id, custom }) if opts.notes => { - if !buf.is_empty() { - out.push_text(&std::mem::take(&mut buf), &f); - } - // The note's content without its own mark (a note holds no notes). - let story = doc.parts.get(id).map(|part| &part.blocks); - let mut blocks = story.map(|b| flow_blocks(doc, b, 0, FlowOpts { notes: false, ..opts })).unwrap_or_default(); - if let Some(FBlock::Para(first)) = blocks.first_mut() { - first.trim_with(&[' ', '\t'], &[]); - } - let endnote = *kind == wordcraft_doc::para::NoteKind::Endnote; - out.inlines.push(Inline::Note { endnote, custom: custom.clone(), blocks }); - } - Some(InlineObject::Field { instr, result, .. }) if opts.fields => { - if !buf.is_empty() { - out.push_text(&std::mem::take(&mut buf), &f); - } - out.inlines.push(Inline::Field { instr: instr.clone(), result: result.clone(), fmt: f.clone() }); - } Some(InlineObject::BookmarkStart { name }) if name != "_GoBack" => { if !buf.is_empty() { out.push_text(&std::mem::take(&mut buf), &f); diff --git a/crates/formats/src/model/to_doc.rs b/crates/formats/src/model/to_doc.rs index 257b1588aa..7ccf634376 100644 --- a/crates/formats/src/model/to_doc.rs +++ b/crates/formats/src/model/to_doc.rs @@ -112,6 +112,8 @@ struct Builder<'a> { revs: std::collections::HashMap<(u8, RevMark), u32>, /// Link and font bytes given to runs so far ([`MAX_RUN_STRINGS`]). run_strings: usize, + /// The document's comment for each flow comment id. + comments: std::collections::HashMap, } fn rev_key(kind: RevisionKind) -> u8 { @@ -131,7 +133,7 @@ impl<'a> Builder<'a> { revs.entry((rev_key(r.kind), mark)).or_insert(i as u32); } } - Builder { doc, revs, run_strings: 0 } + Builder { doc, revs, run_strings: 0, comments: Default::default() } } /// The document revision for a tracked change, added the first time it is seen. @@ -162,6 +164,35 @@ impl<'a> Builder<'a> { cp } + /// Comment `c` (flow id `id`) added to the document with its text, as a reply when its parent + /// was added before it; its document id, or `None` when the document has no room for it or + /// the flow already used `id`. + fn comment(&mut self, id: u32, c: &super::types::FComment) -> Option { + if self.comments.contains_key(&id) { + return None; + } + let blocks = self.blocks(&c.blocks, 1); + let part = self.doc.try_add_part(wordcraft_doc::PartKind::Comment, blocks).ok()?; + let ids = &self.doc.comments; + let Some(doc_id) = wordcraft_doc::free_id(ids.keys().next_back().copied(), ids.keys().copied(), 0) else { + self.doc.parts.remove(&part); + return None; + }; + let parent = c.parent.and_then(|p| self.comments.get(&p).copied()); + let comment = wordcraft_doc::Comment { + author: clean_text(&c.author), + initials: clean_text(&c.initials), + date: clean_text(&c.date), + parent, + resolved: c.resolved, + part, + ..Default::default() + }; + self.doc.comments.insert(doc_id, comment); + self.comments.insert(id, doc_id); + Some(doc_id) + } + /// A note story holding `blocks`, as Word's: its paragraphs in the note text style, the first /// starting with the note's mark and a space. The reference to put in the text, or `None` /// when the document has no room for another story. @@ -255,6 +286,19 @@ impl<'a> Builder<'a> { append(&mut out, "", &CharProps::default(), Some(InlineObject::BookmarkEnd { name: name.clone() })); } Inline::Figure(_) => {} + Inline::Comment { id, comment } => { + // A reply is the comment's, not the text's: it has no range. + if let Some(doc_id) = self.comment(*id, comment) + && comment.parent.is_none() + { + append(&mut out, "", &CharProps::default(), Some(InlineObject::CommentStart { id: doc_id })); + } + } + Inline::CommentEnd(id) => { + if let Some(doc_id) = self.comments.get(id) { + append(&mut out, "", &CharProps::default(), Some(InlineObject::CommentEnd { id: *doc_id })); + } + } Inline::Note { endnote, custom, blocks } => { if let Some(obj) = self.note(*endnote, custom, blocks) { let style = if *endnote { "EndnoteReference" } else { "FootnoteReference" }; diff --git a/crates/formats/src/model/types.rs b/crates/formats/src/model/types.rs index 9ab2428f64..1cc99dc722 100644 --- a/crates/formats/src/model/types.rs +++ b/crates/formats/src/model/types.rs @@ -181,6 +181,50 @@ pub enum Inline { custom: String, blocks: Vec, }, + /// Where comment `id` (the flow's own number for it) starts, and the comment. A reply + /// ([`FComment::parent`]) has no range: it only follows its comment. Only flows asked for + /// comments ([`FlowOpts::comments`]) hold them. + Comment { + id: u32, + comment: Box, + }, + /// Where comment `id`'s range ends. + CommentEnd(u32), +} + +/// `blocks` with each table replaced by the blocks of its cells, row by row (nested tables too), +/// for places that hold paragraphs only. +pub fn flatten_tables(blocks: &[FBlock]) -> Vec { + let mut out = Vec::new(); + let mut stack: Vec> = vec![blocks.iter()]; + while let Some(top) = stack.last_mut() { + match top.next() { + Some(FBlock::Para(p)) => out.push(FBlock::Para(p.clone())), + Some(FBlock::Table(t)) => { + // Cells in reverse so they come off the stack in order. + for c in t.rows.iter().rev().flat_map(|r| r.iter().rev()) { + stack.push(c.blocks.iter()); + } + } + None => { + stack.pop(); + } + } + } + out +} + +/// A comment in a flow ([`Inline::Comment`]). +#[derive(Clone, Debug, Default, PartialEq)] +pub struct FComment { + pub author: String, + pub initials: String, + /// ISO 8601; empty = unknown. + pub date: String, + pub resolved: bool, + /// The comment this one answers (its flow id). + pub parent: Option, + pub blocks: Vec, } /// What [`super::from_doc_with`] keeps besides text and formatting. @@ -192,6 +236,8 @@ pub struct FlowOpts { pub fields: bool, /// Footnotes and endnotes as [`Inline::Note`] (otherwise left out). pub notes: bool, + /// Comments as [`Inline::Comment`] and [`Inline::CommentEnd`] (otherwise left out). + pub comments: bool, } /// Base text size (points) a phonetic guide falls back to when the source states none: the @@ -300,7 +346,7 @@ impl Para { Inline::Text(t, _) | Inline::Equation { linear: t, .. } | Inline::Ruby { base: t, .. } | Inline::Field { result: t, .. } => { s.push_str(t) } - Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) | Inline::Note { .. } => {} + Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) | Inline::Note { .. } | Inline::Comment { .. } | Inline::CommentEnd(_) => {} } } s @@ -316,7 +362,7 @@ impl Para { _ => s.push_str(t), }, Inline::Equation { linear: t, .. } | Inline::Ruby { base: t, .. } | Inline::Field { result: t, .. } => s.push_str(t), - Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) | Inline::Note { .. } => {} + Inline::Image(_) | Inline::Anchor(_) | Inline::Figure(_) | Inline::Note { .. } | Inline::Comment { .. } | Inline::CommentEnd(_) => {} } } s diff --git a/crates/formats/src/odt.rs b/crates/formats/src/odt.rs index 0c511bbec7..0348f6a98a 100644 --- a/crates/formats/src/odt.rs +++ b/crates/formats/src/odt.rs @@ -27,7 +27,7 @@ use wordcraft_doc::props::VAlign; use wordcraft_doc::section::{Columns, SectionStart}; use wordcraft_doc::{Align, Document, RevisionKind, Rgb, SectionProps}; -use crate::model::{self, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, ListInfo, Meta, Para, RevMark, make_img, mime_of}; +use crate::model::{self, Cell, FBlock, FComment, FTable, Flow, Fmt, Inline, Kind, ListInfo, Meta, Para, RevMark, make_img, mime_of}; mod body; mod fields; diff --git a/crates/formats/src/odt/body.rs b/crates/formats/src/odt/body.rs index 9e34a1d33c..f86277b4bd 100644 --- a/crates/formats/src/odt/body.rs +++ b/crates/formats/src/odt/body.rs @@ -1,8 +1,10 @@ use super::import::Pages; -use super::limits::{MAX_FIELD_RESULT, MAX_INFO_CHARS, MAX_OPEN_INSERTIONS, MAX_REGIONS, MAX_SECTIONS, MAX_XML_DEPTH}; +use super::limits::{MAX_COMMENTS, MAX_FIELD_RESULT, MAX_INFO_CHARS, MAX_OPEN_INSERTIONS, MAX_REGIONS, MAX_SECTIONS, MAX_XML_DEPTH}; use super::styles::Styles; use super::xml::{Attrs, Ev, events, get, length, local}; -use super::{Cell, FBlock, FTable, Fmt, HashMap, Inline, Kind, ListInfo, Para, RevMark, RevisionKind, SectionProps, SectionStart, make_img, model}; +use super::{ + Cell, FBlock, FComment, FTable, Fmt, HashMap, Inline, Kind, ListInfo, Para, RevMark, RevisionKind, SectionProps, SectionStart, make_img, model, +}; /// An embedded ODF formula sub-document's `content.xml`, read for its `math:math` element /// (ODF 1.3 §10.4.3.2 wraps it in `office:document-content`/`office:body`/`office:math`, but @@ -60,6 +62,52 @@ fn read_formula(xml: &[u8]) -> Option<(String, bool)> { crate::mathml::read(&math?) } +/// Make every comment range in `blocks` close: an end before its comment (or of no comment) is +/// dropped, and a comment whose end never comes covers nothing. Replies have no range. +pub(super) fn balance_comments(blocks: &mut [FBlock]) { + fn each_para(blocks: &mut [FBlock], depth: usize, f: &mut dyn FnMut(&mut Para)) { + for b in blocks { + match b { + FBlock::Para(p) => f(p), + FBlock::Table(t) if depth < model::MAX_DEPTH => { + for c in t.rows.iter_mut().flatten() { + each_para(&mut c.blocks, depth + 1, f); + } + } + FBlock::Table(_) => {} + } + } + } + let mut open = std::collections::HashSet::new(); + each_para(blocks, 0, &mut |p| { + p.inlines.retain(|i| match i { + Inline::Comment { id, comment } if comment.parent.is_none() => { + open.insert(*id); + true + } + Inline::CommentEnd(id) => open.remove(id), + _ => true, + }) + }); + if open.is_empty() { + return; + } + each_para(blocks, 0, &mut |p| { + let mut k = 0; + while k < p.inlines.len() { + if let Some(Inline::Comment { id, comment }) = p.inlines.get(k) + && comment.parent.is_none() + && open.remove(id) + { + let id = *id; + p.inlines.insert(k + 1, Inline::CommentEnd(id)); + k += 1; + } + k += 1; + } + }); +} + struct TableB { rows: Vec>, row: Vec, @@ -159,18 +207,42 @@ pub(super) struct Body<'a> { active_ins: Vec<(String, RevMark)>, /// The text field being read. field: Option, - /// The note being read. + /// The note or comment being read. note: Option, + /// Comments by `office:name`: their flow ids; and the next flow id. + comment_names: HashMap, + next_comment: u32, } -/// A footnote or endnote being read (ODF 1.3 §6.3): what it is, and the reader state of the -/// paragraph it sits in, put aside while its content is read into a container of its own. +/// A comment being read (ODF 1.3 §14.1): what `office:annotation` and its `dc:creator`, +/// `dc:date` and `meta:creator-initials` say. +#[derive(Default)] +struct CommentInfo { + /// Its flow id. + id: u32, + /// Its `office:name`, which its `office:annotation-end` names. + named: bool, + /// The flow id of the comment it answers (`loext:parent-name`). + parent: Option, + resolved: bool, + author: String, + date: String, + initials: String, + /// The child whose text is being read: 0 author, 1 date, 2 initials. + field: Option, +} + +/// A footnote, endnote or comment being read (ODF 1.3 §6.3, §14.1): what it is, and the +/// reader state of the paragraph it sits in, put aside while its content is read into a +/// container of its own. struct OpenNote { - /// The `text:note` element's depth. + /// The `text:note` or `office:annotation` element's depth. depth: usize, endnote: bool, /// The citation's `text:label` (a custom mark). custom: String, + /// Set for a comment. + comment: Option, para: Option, fmt: Vec, xml_space_end: bool, @@ -235,6 +307,8 @@ impl<'a> Body<'a> { active_ins: Vec::new(), field: None, note: None, + comment_names: HashMap::new(), + next_comment: 0, } } @@ -260,6 +334,11 @@ impl<'a> Body<'a> { /// The blocks read, with anything still open closed. pub(super) fn take_blocks(&mut self) -> Vec { + // What truncated XML left open: a field, a note or comment (its paragraph goes on). + self.end_field(); + if self.note.is_some() { + self.end_note(); + } self.end_para(); while self.containers.len() > 1 { let c = self.containers.pop().unwrap_or_default(); @@ -271,6 +350,7 @@ impl<'a> Body<'a> { blocks.push(FBlock::Table(t.finish())); } } + balance_comments(&mut blocks); blocks } } @@ -480,6 +560,18 @@ impl Body<'_> { } return; } + if let Some(c) = self.note.as_mut().and_then(|n| n.comment.as_mut()) + && let Some(k) = c.field + { + let s = match k { + 0 => &mut c.author, + 1 => &mut c.date, + _ => &mut c.initials, + }; + let room = MAX_INFO_CHARS.saturating_sub(s.chars().count()); + s.extend(t.chars().take(room)); + return; + } if let Some(OpenField { result, .. }) = &mut self.field { // A field's shown text, its XML whitespace collapsed; bounded like any one text run. for c in t.chars() { @@ -913,13 +1005,28 @@ impl Body<'_> { /// schema, never written by Word or LibreOffice), or one in deleted text or a frame, is left /// out with its content. fn start_note_part(&mut self, q: &str, a: &Attrs) -> bool { - if !q.starts_with("text:") { - return false; + let free = self.note.is_none() && self.del.is_none() && !self.in_tc && self.frame.is_none(); + if let Some(c) = self.note.as_mut().and_then(|n| n.comment.as_mut()) { + c.field = match q { + "dc:creator" => Some(0), + "dc:date" => Some(1), + "meta:creator-initials" => Some(2), + _ => None, + }; + if c.field.is_some() { + return true; + } } - match local(q) { - "note" if self.note.is_none() && self.del.is_none() && !self.in_tc && self.frame.is_none() => self.start_note(a), + match q { + "office:annotation" if free && self.comment_names.len() < MAX_COMMENTS => self.start_comment(a), + "office:annotation-end" => { + if let Some(id) = get(a, "office:name").and_then(|n| self.comment_names.get(n)).copied() { + self.push_inline(Inline::CommentEnd(id)); + } + } + "text:note" if free => self.start_note(a), // The citation's text is LibreOffice's number: only a custom mark is kept. - "note-citation" => { + "text:note-citation" => { if let Some(n) = &mut self.note { n.custom = get(a, "text:label").map(|l| l.chars().filter(|c| !c.is_control()).take(10).collect()).unwrap_or_default(); } @@ -930,6 +1037,34 @@ impl Body<'_> { true } + /// Begin reading a comment, as a note: its text in a container of its own. + fn start_comment(&mut self, a: &Attrs) { + let id = self.next_comment; + self.next_comment = self.next_comment.saturating_add(1); + let name = get(a, "office:name").filter(|n| !n.is_empty() && n.len() <= MAX_INFO_CHARS); + if let Some(n) = name { + // The first comment of a name keeps it. + self.comment_names.entry(n.to_string()).or_insert(id); + } + let parent = get(a, "loext:parent-name").and_then(|n| self.comment_names.get(n)).copied(); + let resolved = get(a, "loext:resolved").is_some_and(|r| r.trim() == "true"); + let info = CommentInfo { id, named: name.is_some(), parent, resolved, ..Default::default() }; + self.start_note(a); + if let Some(n) = &mut self.note { + n.comment = Some(info); + } + } + + /// `inline` at the end of the paragraph being read (a new one if none is). + fn push_inline(&mut self, inline: Inline) { + if self.para.is_none() { + self.start_para(None, None); + } + if let Some(p) = &mut self.para { + p.inlines.push(inline); + } + } + /// Begin reading a note: put the paragraph it is in aside and read its content into a /// container of its own, outside any list. fn start_note(&mut self, a: &Attrs) { @@ -937,6 +1072,7 @@ impl Body<'_> { depth: self.depth, endnote: get(a, "text:note-class").is_some_and(|c| c.trim() == "endnote"), custom: String::new(), + comment: None, para: self.para.take(), fmt: std::mem::take(&mut self.fmt), xml_space_end: std::mem::take(&mut self.xml_space_end), @@ -968,23 +1104,51 @@ impl Body<'_> { self.para_cont = n.para_cont; self.lists = n.lists; self.item_used = n.item_used; - if self.para.is_none() { - self.start_para(None, None); - } - if let Some(p) = &mut self.para { - p.inlines.push(Inline::Note { endnote: n.endnote, custom: n.custom, blocks }); + match n.comment { + Some(c) => { + // LibreOffice writes an empty date as all zeroes; a date without a zone is + // read as UTC, as the document's own dates are. + let date = c.date.trim(); + let date = if date.is_empty() || date.starts_with("0000") { + String::new() + } else if date.ends_with(['Z', 'z']) || date.rsplit_once('T').is_some_and(|(_, t)| t.contains(['+', '-'])) { + date.to_string() + } else { + format!("{date}Z") + }; + let comment = FComment { + author: c.author.trim().to_string(), + initials: c.initials.trim().to_string(), + date, + resolved: c.resolved, + parent: c.parent, + blocks, + }; + self.push_inline(Inline::Comment { id: c.id, comment: Box::new(comment) }); + // A comment without a name has no end to wait for: it covers nothing. + if !c.named && c.parent.is_none() { + self.push_inline(Inline::CommentEnd(c.id)); + } + } + None => self.push_inline(Inline::Note { endnote: n.endnote, custom: n.custom, blocks }), } } /// An element ending at the current depth, for an open note or field: true when that was all /// there was to it. fn end_note_or_field(&mut self, q: &str) -> bool { + if let Some(c) = self.note.as_mut().and_then(|n| n.comment.as_mut()) + && c.field.is_some() + { + c.field = None; + return true; + } if self.note.as_ref().is_some_and(|n| self.depth < n.depth) { // The note's own end, or (malformed) an outer element ending first: the note ends, // then that element. self.end_field(); self.end_note(); - if local(q) == "note" { + if q == "text:note" || q == "office:annotation" { return true; } } diff --git a/crates/formats/src/odt/limits.rs b/crates/formats/src/odt/limits.rs index 8f3607814f..b67109ed57 100644 --- a/crates/formats/src/odt/limits.rs +++ b/crates/formats/src/odt/limits.rs @@ -10,6 +10,9 @@ pub(super) const MAX_SECTIONS: usize = 10_000; /// Longest shown text a field keeps, in bytes. pub(super) const MAX_FIELD_RESULT: usize = 64 << 10; +/// Most comments read; later ones are left out. +pub(super) const MAX_COMMENTS: usize = 100_000; + /// Most `text:changed-region`s read (hostile input). pub(super) const MAX_REGIONS: usize = 10_000; diff --git a/crates/formats/src/odt/package.rs b/crates/formats/src/odt/package.rs index e06848a4e5..34ad812b30 100644 --- a/crates/formats/src/odt/package.rs +++ b/crates/formats/src/odt/package.rs @@ -187,7 +187,7 @@ fn flow_sections(blocks: &[FBlock], last: &SectionProps) -> Vec<(SectionProps, s /// Write an `.odt` package. pub fn export(doc: &Document) -> Result, String> { - let flow = model::from_doc_with(doc, model::FlowOpts { tracked: true, fields: true, notes: true }); + let flow = model::from_doc_with(doc, model::FlowOpts { tracked: true, fields: true, notes: true, comments: true }); let mut w = Writer::default(); let mut body = String::new(); let sections = flow_sections(&flow.blocks, &doc.last_section); diff --git a/crates/formats/src/odt/tests_more.rs b/crates/formats/src/odt/tests_more.rs index 1272873822..b1b167e022 100644 --- a/crates/formats/src/odt/tests_more.rs +++ b/crates/formats/src/odt/tests_more.rs @@ -1,5 +1,6 @@ //! Tests for the ODT features added after the first reader and writer: one module per feature. +mod comments; mod fields; mod headers; mod notes; diff --git a/crates/formats/src/odt/tests_more/comments.rs b/crates/formats/src/odt/tests_more/comments.rs new file mode 100644 index 0000000000..0890f99d23 --- /dev/null +++ b/crates/formats/src/odt/tests_more/comments.rs @@ -0,0 +1,170 @@ +//! Comments (ODF 1.3 §14.1 `office:annotation`, §14.2 `office:annotation-end`): read into the +//! document's comments with their ranges, replies and resolved state, and written back. + +use crate::odt::tests::{NS_T, package}; +use crate::odt::{export, import}; +use wordcraft_doc::para::{InlineObject, OBJ}; +use wordcraft_doc::{Document, StoryRef}; + +const NS_C: &str = r#"xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0""#; + +fn body(xml: &str) -> Vec { + package( + &format!(r#"{xml}"#), + None, + ) +} + +/// Each comment: (author, initials, date, resolved, parent's text, its text, the text it covers). +type Summary = (String, String, String, bool, Option, String, String); + +fn text_of(d: &Document, part: u32) -> String { + d.parts + .get(&part) + .map(|p| p.blocks.iter().filter_map(|b| b.as_para()).map(|p| p.text.replace(OBJ, "")).collect::>().join("|")) + .unwrap_or_default() +} + +/// The body text between each comment's start and end. +fn covered(d: &Document, id: u32) -> String { + let mut out = String::new(); + let mut inside = false; + for b in d.body.iter() { + let Some(p) = b.as_para() else { continue }; + let offs = p.object_offsets(); + let mut last = 0; + for (k, o) in p.objects.iter().enumerate() { + let at = offs.get(k).copied().unwrap_or(0); + if inside { + out.push_str(&p.text[last..at].replace(OBJ, "")); + } + match o { + InlineObject::CommentStart { id: i } if *i == id => inside = true, + InlineObject::CommentEnd { id: i } if *i == id => inside = false, + _ => {} + } + last = at + OBJ.len_utf8(); + } + if inside { + out.push_str(&p.text[last.min(p.text.len())..].replace(OBJ, "")); + out.push('|'); + } + } + out +} + +fn comments(d: &Document) -> Vec { + d.comments + .iter() + .map(|(id, c)| { + let parent = c.parent.and_then(|p| d.comments.get(&p)).map(|p| text_of(d, p.part)); + (c.author.clone(), c.initials.clone(), c.date.clone(), c.resolved, parent, text_of(d, c.part), covered(d, *id)) + }) + .collect() +} + +const COMMENTS: &str = concat!( + r#"Before Ada Lovelace2026-10-31T10:00:00ALFirst comment.Second line.commented words and"#, + r#"Grace0000-00-00T00:00:00A point. after."#, +); + +#[test] +fn comments_open_with_their_ranges() { + let d = import(&body(COMMENTS)).unwrap(); + let got = comments(&d); + assert_eq!(got.len(), 2, "{got:?}"); + assert_eq!( + got[0], + ( + "Ada Lovelace".into(), + "AL".into(), + "2026-10-31T10:00:00Z".into(), + false, + None, + "First comment.|Second line.".into(), + "commented words".into() + ) + ); + // A comment without a range covers nothing; LibreOffice's empty date is no date. + assert_eq!(got[1], ("Grace".into(), String::new(), String::new(), true, None, "A point.".into(), String::new())); + // The comment's text isn't body text. + assert_eq!(d.plain_text(StoryRef::Body).trim(), "Before commented words and after."); +} + +#[test] +fn a_comment_across_paragraphs_keeps_its_range() { + let d = import(&body(r#"a Aconetwo b"#)).unwrap(); + let (id, _) = d.comments.iter().next().unwrap(); + assert_eq!(covered(&d, *id), "one|two"); +} + +#[test] +fn replies_follow_their_parent() { + let xml = concat!( + r#"AdaQuestion?text"#, + r#"AlanAnswer."#, + ); + let d = import(&body(xml)).unwrap(); + let got = comments(&d); + assert_eq!(got.len(), 2); + assert_eq!(got[1].4.as_deref(), Some("Question?")); + assert_eq!(got[1].5, "Answer."); + // A reply has no place in the text of its own. + let reply_id = *d.comments.iter().find(|(_, c)| c.parent.is_some()).unwrap().0; + let anchored = d + .body + .iter() + .filter_map(|b| b.as_para()) + .flat_map(|p| p.objects.iter()) + .any(|o| matches!(o, InlineObject::CommentStart { id } if *id == reply_id)); + assert!(!anchored); +} + +#[test] +fn comments_round_trip() { + let xml = concat!( + r#"Before Ada Lovelace2026-10-31T10:00:00ALFirst comment.commentedwords"#, + r#"AlanReply. and"#, + r#"GraceA point. after."#, + ); + let d = import(&body(xml)).unwrap(); + let back = import(&export(&d).unwrap()).unwrap(); + assert_eq!(comments(&back), comments(&d)); + assert_eq!(back.plain_text(StoryRef::Body), d.plain_text(StoryRef::Body)); +} + +#[test] +fn unmatched_and_hostile_comments_do_not_panic() { + for xml in [ + // An end without a start, a start whose end never comes, a nested annotation. + r#"ab"#.to_string(), + r#"acb"#.to_string(), + r#"xy"# + .to_string(), + r#""#.to_string(), + format!(r#"{}long"#, "n".repeat(100_000)), + r#"orphan"#.to_string(), + ] { + let d = import(&body(&xml)).unwrap(); + let again = import(&export(&d).unwrap()).unwrap(); + // Every comment kept is whole: its story exists and its range closes. + for (id, c) in &again.comments { + assert!(again.parts.contains_key(&c.part), "{xml}"); + let starts = again + .body + .iter() + .filter_map(|b| b.as_para()) + .flat_map(|p| p.objects.iter()) + .filter(|o| matches!(o, InlineObject::CommentStart { id: i } if i == id)) + .count(); + let ends = again + .body + .iter() + .filter_map(|b| b.as_para()) + .flat_map(|p| p.objects.iter()) + .filter(|o| matches!(o, InlineObject::CommentEnd { id: i } if i == id)) + .count(); + assert_eq!(starts, ends, "{xml}"); + } + } +} diff --git a/crates/formats/src/odt/tests_more/notes.rs b/crates/formats/src/odt/tests_more/notes.rs index 60302584f1..5435684e89 100644 --- a/crates/formats/src/odt/tests_more/notes.rs +++ b/crates/formats/src/odt/tests_more/notes.rs @@ -120,3 +120,12 @@ fn citations_number_footnotes_and_endnotes_apart() { xml.split("')).map(|(_, rest)| rest.split('<').next().unwrap_or("")).collect(); assert_eq!(citations, ["1", "i", "*"]); } + +#[test] +fn a_note_cut_off_by_the_end_of_the_file_keeps_its_paragraph() { + // Truncated XML: no end for the note, its paragraph or the body. + let content = format!(r#"beforein note"#); + let d = import(&package(&content, None)).unwrap(); + assert_eq!(d.body[0].as_para().unwrap().text.replace(OBJ, "#"), "before#"); + assert_eq!(notes(&d), [(NoteKind::Footnote, String::new(), "in note".to_string())]); +} diff --git a/crates/formats/src/odt/writer.rs b/crates/formats/src/odt/writer.rs index 89bb20a3a0..710042868b 100644 --- a/crates/formats/src/odt/writer.rs +++ b/crates/formats/src/odt/writer.rs @@ -32,6 +32,7 @@ pub(super) const NS: &str = concat!( "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" ", "xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\" ", "xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\" ", + "xmlns:loext=\"urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0\" ", "office:version=\"1.3\"" ); @@ -149,6 +150,11 @@ pub(super) fn spacing_props(sp: &model::Spacing) -> String { p } +/// The `office:name` of comment `id` written by a writer with names prefixed `prefix`. +fn annotation_name(prefix: &str, id: u32) -> String { + format!("__Annotation__{prefix}{id}") +} + fn parent_style(k: Kind) -> &'static str { match k { Kind::Heading(1) => "Heading_20_1", @@ -435,6 +441,11 @@ impl Writer { self.note(*endnote, custom, blocks, out); prev_space = false; } + Inline::Comment { id, comment } => { + self.comment(*id, comment, out); + prev_space = false; + } + Inline::CommentEnd(id) => out.push_str(&format!("", annotation_name(self.prefix, *id))), Inline::Field { instr, result, fmt } => match super::fields::element(instr, &x(result)) { Some(el) => { match self.text_style(&Fmt { link: None, ..fmt.clone() }) { @@ -456,6 +467,41 @@ impl Writer { } } + /// A comment (ODF 1.3 §14.1): named, so its `office:annotation-end` closes its range, or for + /// a reply naming the comment it answers (LibreOffice's `loext:parent-name`); its author, + /// date, initials, resolved state (`loext:resolved`) and text. + fn comment(&mut self, id: u32, c: &model::FComment, out: &mut String) { + let reopen = self.open_ins.as_ref().map(|(m, _)| m.clone()); + self.close_ins(out); + let mut attrs = match c.parent { + Some(p) => format!(" loext:parent-name=\"{}\"", annotation_name(self.prefix, p)), + None => format!(" office:name=\"{}\"", annotation_name(self.prefix, id)), + }; + if c.resolved { + attrs.push_str(" loext:resolved=\"true\""); + } + let mut head = String::new(); + if !c.author.is_empty() { + head.push_str(&format!("{}", x(&c.author))); + } + if !c.date.is_empty() { + head.push_str(&format!("{}", x(&c.date))); + } + if !c.initials.is_empty() { + head.push_str(&format!("{}", x(&c.initials))); + } + let saved = (std::mem::take(&mut self.cur_style), std::mem::take(&mut self.chain), self.part, self.note_parent.take()); + let mut body = String::new(); + // An annotation holds paragraphs and lists only: a table's text goes in as paragraphs. + self.blocks(&model::flatten_tables(&c.blocks), &mut body, 1); + self.close_ins(&mut body); + (self.cur_style, self.chain, self.part, self.note_parent) = saved; + out.push_str(&format!("{head}{body}")); + if let Some(m) = reopen { + self.open_ins(&m, out); + } + } + /// A footnote or endnote (ODF 1.3 §6.3): its citation (the number LibreOffice shows until it /// renumbers, or the custom mark) and its content. The paragraph being written is put aside /// meanwhile, and an insertion open around the note ends before it and goes on after it. diff --git a/crates/formats/src/rtf/export.rs b/crates/formats/src/rtf/export.rs index 9256ffc653..966ff1f217 100644 --- a/crates/formats/src/rtf/export.rs +++ b/crates/formats/src/rtf/export.rs @@ -400,7 +400,7 @@ impl Writer { // RTF keeps no ruby yet: its base text, in the run's formatting. Inline::Ruby { base, fmt, .. } => self.run(base, fmt, out), Inline::Field { result, fmt, .. } => self.run(result, fmt, out), - Inline::Figure(_) | Inline::Equation { .. } | Inline::Note { .. } => {} + Inline::Figure(_) | Inline::Equation { .. } | Inline::Note { .. } | Inline::Comment { .. } | Inline::CommentEnd(_) => {} } } } diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 68abb860ec..2965141706 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -1,6 +1,6 @@ # File-format parity with Microsoft Word -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-31 · **Change:** trivial (ODT footnotes and endnotes read and written); previously trivial (ODT headers, footers and text fields written; fields read); previously trivial (ODT paragraph spacing, indents and line spacing read and written); previously trivial (a paragraph naming an undefined style is formatted as Normal, as in Word, #5077); previously trivial (a hyperlinked TOC writes its page numbers as `PAGEREF _Toc… \h` fields, as Word does, #5097); previously trivial (HTML import ends the empty comments and at once, #5040); previously trivial (chart and SmartArt text the font lacks is drawn in a fallback face, so Chinese labels no longer show boxes, #4720); previously trivial (bibliography sources keep a corporate author (`b:Corporate`): read, written, cited and listed whole, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (note conversion preserves content with an ordered reference/run walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (built-in heading styles have their outline level without `w:outlineLvl`, #4696); previously trivial (DOCX comment ranges inside footnotes and endnotes are kept on read, #4548); previously trivial (RTF import and export keep Windows and Enhanced Metafile pictures (`\wmetafile`, `\emfblip`), #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (DOCX parts stored under another casing keep their embedded fonts through a save, and a private save of a kept glossary escapes the attribute values it rewrites; previously trivial: the `.doc` reader logs what its copy budget leaves out, once a kind per document; importers bound hostile expansion: font names, links, ODS repeats, LaTeX wrappers, Markdown delimiters, #3692/#3713; previously trivial: repeated DOCX style IDs: save/reopen regression; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: Arabic list labels: `arabicAlpha` and `arabicAbjad` draw the 28 Arabic letters (alphabetical and abjad order), doubling past the last letter; Hebrew, Hindi, Thai and Vietnamese still show decimal (`doc::section::tests::arabic_letters_count_in_both_orders`); previously trivial: localized TOC style ids read as the TOC styles, and a TOC in columns keeps its layout through Update Table, #3889; previously trivial: cu20we: outline caps; previously trivial: a relationship the markup names without the prefix is read as it wrote: an unqualified `id` on an element that carries a relationship names that relationship, and the writer keeps the attribute with the id the new package holds the part under; an ordinary `id` — a VML shape's, a drawing's — names no part; previously trivial: the reader takes the relationship ids the markup refers to from the relationship attributes of the main document it has already parsed, so a document with many relationships opens in the time its part takes, #3411; previously trivial: a table cell span from a `.json` file stays within 63 columns through Insert Column and Merge Cells, #3637; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: `majorBidi`/`minorBidi` theme fonts follow the settings' theme font language, as measured in Word; previously trivial: Word signature lines are kept on save, still signable; previously trivial: Times New Roman keeps Liberation Serif under the roman-font rule; previously trivial: shape gradients and transparency, #1500; previously trivial: a folder that keeps no Unix modes, such as a NAS share mounted by GVFS, still takes a save, #3003; previously trivial: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: table style band sizes written, a style without them unbanded as in Word, #3415; previously trivial: HTML export writes ruby (furigana, phonetic guides) and HTML import reads it back, #1681; previously trivial: RTF import decodes the East Asian multibyte code pages, #1653; previously trivial: PDF export: lam-alef drawn as two glyphs at one x extracts in reading order, #2310; previously trivial: native .json stores pictures and kept parts as base64, #586 follow-up; previously trivial: ODT headers and footers read from each master page, #863; previously trivial: RTF import compares heading and code formatting with the file's stylesheet; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: Word XML and web pages saved as `.doc` open by what they hold, #2853; previously trivial: a VML shape in a header cell is always placed on the page, user-checked in Word for Mac, #1775; previously trivial: `w:sym` characters keep their symbol font when the text's font changes and are written back as `w:sym`, #2036; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: two lines in one and horizontal in vertical (`w:eastAsianLayout`) kept on save; previously trivial: PDF outline targets follow leading page breaks; previously trivial: positional tabs `w:ptab` read and kept on save, #2367; previously trivial: DOCX: mixed right-to-left runs split and marked `w:rtl` with their language, #1543; previously trivial: `w:doNotTrackMoves` round-trips and turns move tracking off; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: DOCX glossary documents (building blocks) read, kept and written; previously trivial: cached field results keep their visible run formatting; previously trivial: cached REF results retain hyperlink destinations; previously trivial: CLI conversions can explicitly refresh fields; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: a pasted HTML `
` is an indented paragraph, not the Quote style, #1270); previously trivial: every table style part round-trips, #714; previously trivial: VML freeforms read and kept on save; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: `flowChartOr` draws its circle and cross spokes, as in Word; previously trivial: RTF: Mac OS Roman and DOS text, #1653; previously trivial: VML text boxes keep their internal margins, fit to their text and vertical alignment; hidden drawings not shown; previously trivial: PDF export draws horizontal and vertical lines inside groups, #1474; previously trivial: symbol-font text exported as Unicode, #776; previously trivial: text-box margins, Resize shape to fit text and freeforms in groups, #743; previously trivial: formatted paste from the system clipboard's HTML and RTF, #687; previously trivial: independent complex-script formatting inheritance and round-trip regression tests; previously trivial: import/export fixes for TXT, RTF, ODT, HTML and LaTeX, #433–#440; previously major: first version; every format Word reads or writes, measured from the readers and writers on origin/main) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4, plus Word for Windows' extra formats +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-31 · **Change:** trivial (ODT comments read and written); previously trivial (ODT footnotes and endnotes read and written); previously trivial (ODT headers, footers and text fields written; fields read); previously trivial (ODT paragraph spacing, indents and line spacing read and written); previously trivial (a paragraph naming an undefined style is formatted as Normal, as in Word, #5077); previously trivial (a hyperlinked TOC writes its page numbers as `PAGEREF _Toc… \h` fields, as Word does, #5097); previously trivial (HTML import ends the empty comments and at once, #5040); previously trivial (chart and SmartArt text the font lacks is drawn in a fallback face, so Chinese labels no longer show boxes, #4720); previously trivial (bibliography sources keep a corporate author (`b:Corporate`): read, written, cited and listed whole, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (note conversion preserves content with an ordered reference/run walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (built-in heading styles have their outline level without `w:outlineLvl`, #4696); previously trivial (DOCX comment ranges inside footnotes and endnotes are kept on read, #4548); previously trivial (RTF import and export keep Windows and Enhanced Metafile pictures (`\wmetafile`, `\emfblip`), #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (DOCX parts stored under another casing keep their embedded fonts through a save, and a private save of a kept glossary escapes the attribute values it rewrites; previously trivial: the `.doc` reader logs what its copy budget leaves out, once a kind per document; importers bound hostile expansion: font names, links, ODS repeats, LaTeX wrappers, Markdown delimiters, #3692/#3713; previously trivial: repeated DOCX style IDs: save/reopen regression; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: Arabic list labels: `arabicAlpha` and `arabicAbjad` draw the 28 Arabic letters (alphabetical and abjad order), doubling past the last letter; Hebrew, Hindi, Thai and Vietnamese still show decimal (`doc::section::tests::arabic_letters_count_in_both_orders`); previously trivial: localized TOC style ids read as the TOC styles, and a TOC in columns keeps its layout through Update Table, #3889; previously trivial: cu20we: outline caps; previously trivial: a relationship the markup names without the prefix is read as it wrote: an unqualified `id` on an element that carries a relationship names that relationship, and the writer keeps the attribute with the id the new package holds the part under; an ordinary `id` — a VML shape's, a drawing's — names no part; previously trivial: the reader takes the relationship ids the markup refers to from the relationship attributes of the main document it has already parsed, so a document with many relationships opens in the time its part takes, #3411; previously trivial: a table cell span from a `.json` file stays within 63 columns through Insert Column and Merge Cells, #3637; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: `majorBidi`/`minorBidi` theme fonts follow the settings' theme font language, as measured in Word; previously trivial: Word signature lines are kept on save, still signable; previously trivial: Times New Roman keeps Liberation Serif under the roman-font rule; previously trivial: shape gradients and transparency, #1500; previously trivial: a folder that keeps no Unix modes, such as a NAS share mounted by GVFS, still takes a save, #3003; previously trivial: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: table style band sizes written, a style without them unbanded as in Word, #3415; previously trivial: HTML export writes ruby (furigana, phonetic guides) and HTML import reads it back, #1681; previously trivial: RTF import decodes the East Asian multibyte code pages, #1653; previously trivial: PDF export: lam-alef drawn as two glyphs at one x extracts in reading order, #2310; previously trivial: native .json stores pictures and kept parts as base64, #586 follow-up; previously trivial: ODT headers and footers read from each master page, #863; previously trivial: RTF import compares heading and code formatting with the file's stylesheet; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: Word XML and web pages saved as `.doc` open by what they hold, #2853; previously trivial: a VML shape in a header cell is always placed on the page, user-checked in Word for Mac, #1775; previously trivial: `w:sym` characters keep their symbol font when the text's font changes and are written back as `w:sym`, #2036; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: two lines in one and horizontal in vertical (`w:eastAsianLayout`) kept on save; previously trivial: PDF outline targets follow leading page breaks; previously trivial: positional tabs `w:ptab` read and kept on save, #2367; previously trivial: DOCX: mixed right-to-left runs split and marked `w:rtl` with their language, #1543; previously trivial: `w:doNotTrackMoves` round-trips and turns move tracking off; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: DOCX glossary documents (building blocks) read, kept and written; previously trivial: cached field results keep their visible run formatting; previously trivial: cached REF results retain hyperlink destinations; previously trivial: CLI conversions can explicitly refresh fields; previously trivial: a copied equation also goes in the clipboard's MathML formats, #1297; previously trivial: a pasted HTML `
` is an indented paragraph, not the Quote style, #1270); previously trivial: every table style part round-trips, #714; previously trivial: VML freeforms read and kept on save; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: `flowChartOr` draws its circle and cross spokes, as in Word; previously trivial: RTF: Mac OS Roman and DOS text, #1653; previously trivial: VML text boxes keep their internal margins, fit to their text and vertical alignment; hidden drawings not shown; previously trivial: PDF export draws horizontal and vertical lines inside groups, #1474; previously trivial: symbol-font text exported as Unicode, #776; previously trivial: text-box margins, Resize shape to fit text and freeforms in groups, #743; previously trivial: formatted paste from the system clipboard's HTML and RTF, #687; previously trivial: independent complex-script formatting inheritance and round-trip regression tests; previously trivial: import/export fixes for TXT, RTF, ODT, HTML and LaTeX, #433–#440; previously major: first version; every format Word reads or writes, measured from the readers and writers on origin/main) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4, plus Word for Windows' extra formats Every format Word opens or saves, what WordCraft does with it, and how it's tested. Fidelity is the share of a typical real-world file's content and formatting that survives (read: shows correctly; @@ -57,7 +57,7 @@ Weights are the share of a Word user's file traffic (estimate). Read/write: ✅ | PDF export | 8% | W | — | ✅ | ~80% | 6–10 | `crates/pdf/src/tests.rs` | krilla: embedded subsets, selectable text incl. field results (#133); Hebrew and Arabic copy and search as whole words (right-to-left glyphs written in visual order, #1383, also with fonts that draw lam-alef as two glyphs at one x, #2310); All Caps and Small Caps text extracts as the capitals shown, as from Word's PDFs, and never changes the case of other text (#1473), links, outline, tagging. Outline destinations follow leading page breaks to the heading text. Fonts it can't subset become outlines (#132). Print Markup (#589): comments in balloons beside each page (scaled to its paper) and tracked changes; HTML/Markdown/text copies end with a comment list. Horizontal and vertical lines inside groups are drawn (#1474). No PDF/A option, no "best for printing/online" choice | | PDF open (PDF Reflow, Word for Windows) | 1% | R | ❌ | — | 0% | 30–50 | — | Converting a PDF into an editable document. A sibling app (PdfCraft) may supply the parser | | `.rtf` Rich Text Format | 3% | R/W | 🟡 | 🟡 | ~50% | 10–15 | `crates/formats/src/tests.rs` | ~1,500 lines: text, character/paragraph formatting, styles, lists, tables, pictures (`\shppict`, #154; Windows and Enhanced Metafiles, `\wmetafile` and `\emfblip`, read and written, #4259), fields, bookmarks. 8-bit text in the document's code page (`\ansicpg`) or the current font's (`\fcharset`, `\cpg`): Cyrillic, Central European, Greek, Turkish, Hebrew, Arabic, Baltic, Vietnamese, Thai (was Windows-1252 only), Mac OS Roman (`\mac`, `\fcharset77`) and DOS code pages (`\pc` 437, `\pca` 850, `\cpg` 437/737/850/852/857/866, `\fcharset254`) (#1653). East Asian multibyte code pages decoded as runs: 932 Shift-JIS (`\fcharset128`, Japanese), 936 GBK (134, Simplified Chinese), 949 UHC/windows-949 (129, Korean) and 950 Big5 (136, Traditional Chinese) through `\ansicpg`, `\cpg` or the font's charset, escaped or raw, a lead byte and its trail byte together; invalid sequences read as U+FFFD, and a run that starts `EF BB BF` is text, not a BOM (#1653). Pasted from the system clipboard where the platform offers it (Windows, Wayland) when there is no HTML (#687). Tracked insertions and deletions read and written (`\revtbl`, `\revised`/`\deleted`, authors, `\revdttm` dates), so Accept/Reject All work after opening or a round trip; tracked paragraph marks (`\par` inside `\revised`/`\deleted` groups, including a `\sect` inside a deletion) and character and paragraph formatting revisions (`\crauth`/`\crdate` with `\oldcprops`, `\prauth`/`\prdate` with `\oldpprops`) read and written; section and table formatting revisions (`\oldsprops`, `\oldtprops`) not yet. Sections on import (#1378): each `\sect` ends a section with its own page size and orientation, margins, header/footer distance, columns, break type, page-number restart, start and format, title page and headers/footers (those it leaves out link to the previous); export writes every section too (break type, page size and orientation, margins, header/footer distance, columns, page numbering, title page; not headers/footers). Thin: comments, notes, RTL | -| `.odt` OpenDocument Text | 2% | R/W | 🟡 | 🟡 | ~50% | 8–12 | `crates/formats/src/tests.rs`, `crates/formats/src/odt.rs` unit tests, `crates/engine/tests/it/odt_sections.rs` | Styles, lists (#87), tables, pictures, page breaks (#86), paragraph spacing, indents and line spacing (proportional, fixed, at least; leading read as a proportional approximation) both ways, with the default and Standard styles' spacing as the document defaults (ODT's are none, not Word's 8 pt after and 1.15 lines), headers and footers of each master page (every, left and first page) read on open and written on save, with the header and footer distances converted between Word's page-edge measures and ODF's header bands (#863), text fields (page number and count, date, time, title, subject, author, file name, word and character counts, bookmark references as `REF`/`PAGEREF`) read as fields and written back as ODF fields; fixed dates and fields without a Word equivalent (sequences, chapters, variables) read as their text, and Word fields ODF lacks written as their result, date formats not kept, keep with next (#2813), an embedded formula object (`draw:object`) is read from its own `content.xml`'s MathML, same as HTML (#3099; export still drops equations, as before), page size, orientation and margins (#810), sections: one per master-page change on a paragraph or table (next page, with its own page size, orientation, margins, columns and page-numbering restart) and per columned `text:section` (continuous, with its columns and separator line) (#1379); export writes them back: a master page and page layout per new-page section (size, orientation, margins, columns, page-numbering restart on the first paragraph or table), a `text:section` with its columns per continuous section whose columns differ from the page's (one column included), a list cut by a section break goes on numbering. Limits: a continuous section's page-numbering restart isn't written (ODF applies it only with a master page); a continuous section with the page's columns merges into the section before it on reopening; even and odd page starts are written as plain new pages; a tracked deletion of a section break is lost; an empty last section is dropped; tracked insertions and deletions with author and date, inserted and deleted paragraph marks and text inserted then deleted included (ODF change tracking; format changes read as none; pictures and tables inside a tracked deletion are dropped). footnotes and endnotes (`text:note`) read into note stories as Word's (Footnote Text style, the note's own mark first, a custom mark kept) and written back with their citations; notes inside notes and in deleted text left out. No comments or RTL | +| `.odt` OpenDocument Text | 2% | R/W | 🟡 | 🟡 | ~50% | 8–12 | `crates/formats/src/tests.rs`, `crates/formats/src/odt.rs` unit tests, `crates/engine/tests/it/odt_sections.rs` | Styles, lists (#87), tables, pictures, page breaks (#86), paragraph spacing, indents and line spacing (proportional, fixed, at least; leading read as a proportional approximation) both ways, with the default and Standard styles' spacing as the document defaults (ODT's are none, not Word's 8 pt after and 1.15 lines), headers and footers of each master page (every, left and first page) read on open and written on save, with the header and footer distances converted between Word's page-edge measures and ODF's header bands (#863), text fields (page number and count, date, time, title, subject, author, file name, word and character counts, bookmark references as `REF`/`PAGEREF`) read as fields and written back as ODF fields; fixed dates and fields without a Word equivalent (sequences, chapters, variables) read as their text, and Word fields ODF lacks written as their result, date formats not kept, keep with next (#2813), an embedded formula object (`draw:object`) is read from its own `content.xml`'s MathML, same as HTML (#3099; export still drops equations, as before), page size, orientation and margins (#810), sections: one per master-page change on a paragraph or table (next page, with its own page size, orientation, margins, columns and page-numbering restart) and per columned `text:section` (continuous, with its columns and separator line) (#1379); export writes them back: a master page and page layout per new-page section (size, orientation, margins, columns, page-numbering restart on the first paragraph or table), a `text:section` with its columns per continuous section whose columns differ from the page's (one column included), a list cut by a section break goes on numbering. Limits: a continuous section's page-numbering restart isn't written (ODF applies it only with a master page); a continuous section with the page's columns merges into the section before it on reopening; even and odd page starts are written as plain new pages; a tracked deletion of a section break is lost; an empty last section is dropped; tracked insertions and deletions with author and date, inserted and deleted paragraph marks and text inserted then deleted included (ODF change tracking; format changes read as none; pictures and tables inside a tracked deletion are dropped). footnotes and endnotes (`text:note`) read into note stories as Word's (Footnote Text style, the note's own mark first, a custom mark kept) and written back with their citations; notes inside notes and in deleted text left out; comments (`office:annotation` with `office:annotation-end` ranges, author, date, initials, LibreOffice's resolved state and reply threads, `loext:resolved`/`loext:parent-name`) read and written, a comment whose end never comes covering nothing; comments in notes and in deleted text left out. No RTL | | `.txt` Plain text | 2% | R/W | ✅ | ✅ | ~95% | 1–2 | unit tests | Encoding detection. Files with other extensions (`.m3u`/`.m3u8` playlists, logs, config files, no extension) open as text when they are text, Word and RTF files by their signature; binary files are refused (#569). A Word or RTF extension that contradicts the content also opens by signature, as in Word: RTF saved as `.doc` (hh.ru résumés, accounting exports), a `.docx` renamed `.doc`, a Word 97-2003 file renamed `.docx`; a Word XML Document (Word 2003 XML or Flat OPC) or a web page (``, ``) under a `.doc`, `.docx` or `.rtf` name opens as what it is (#2853, `tests::word_xml_and_web_pages_saved_as_doc_open_by_content`). Word's encoding choice dialog and line-ending options missing; such files save only through Save As | | `.htm/.html` Web Page and Web Page, Filtered | 1% | R/W | 🟡 | 🟡 | ~60% | 6–10 | `crates/formats/src/tests.rs` | Tables (#102), relative pictures embedded (#113), missing `` tolerated (#154); the empty comments `` and `` end at once instead of swallowing the rest of the page (#5040). Header cells (``) bold unless their CSS says otherwise; `text-align` on a cell, or inherited from its row, row group or table, aligns the cell's paragraphs (#1381). HTML gives no page size, so it opens on the region's paper (A4 or Letter), as Markdown and plain text do; `@page` isn't read (#1381). No Word "Web Page" (with `_files` folder) round-trip. The same reader takes HTML from the system clipboard: Paste keeps another program's formatting (a Word document, a web page; Windows' "HTML Format" header understood), Merge Formatting keeps its emphasis (#687). A pasted `
` (LibreOffice's paragraph indented on both sides) becomes a Normal paragraph indented by the page's `blockquote` margins, else 0.5 in each side; opening an .html file keeps it as the Quote style (#1270). Equations are written as MathML (with the linear format as a UnicodeMath annotation) and read back from MathML: our annotation, else the presentation elements, else a TeX annotation (#816). A copy of one equation alone also goes in the clipboard's MathML formats (Windows "MathML" and "MathML Presentation", macOS `public.mathml.presentation` and `public.mathml`; W3C MathML 3 §6.2.4), which Microsoft documents Word reading first when it pastes math (#1297; the paste into Word is not yet verified); Ruby (furigana, phonetic guides) is written as `basereading` in the base text's formatting and read back from ``/``/`` (`` fallback parentheses dropped; malformed markup keeps its text), so docx/doc → HTML → doc keeps the annotation; HTML states no alignment, size or raise, so the Phonetic Guide's defaults apply (#1681) CRLF and lone CR input newlines normalize before tokenization, preserving preformatted lines and leaving decoded character references separate (#4810). | | `.mht/.mhtml` Single File Web Page | <1% | R/W | ❌ | ❌ | 0% | 3–5 | — | | @@ -279,6 +279,7 @@ Custom document properties retain their bookmark link targets through DOCX saves | Date | Change | Summary | |---|---|---| +| 2026-10-31 | trivial | ODT: comments (`office:annotation`, ODF 1.3 §14.1–14.2) open with their ranges (across paragraphs too), author, date, initials, resolved state and replies (`loext:resolved`, `loext:parent-name`, kept by LibreOffice when it saves our files), and are written back; an end with no comment is dropped and a comment whose end never comes covers nothing; before, comments were dropped on open. `formats::odt::tests_more::comments` | | 2026-10-31 | trivial | ODT: footnotes and endnotes (`text:note`, ODF 1.3 §6.3) open as note stories built as Word's (Footnote Text or Endnote Text paragraphs starting with the note's mark, the reference in the reference style, a `text:label` custom mark kept) and are written back with `text:note-citation`s numbered as LibreOffice shows them (footnotes 1, 2, 3; endnotes i, ii, iii); before, notes were dropped on open. `formats::odt::tests_more::notes` | | 2026-10-31 | trivial | ODT: headers and footers are written to each section's master page (every, left and first page), with Word's header and footer distances converted to ODF's header and footer bands and back on open; text fields (`text:page-number`, `text:page-count`, `text:date`, `text:time`, document information, `text:bookmark-ref`) open as WordCraft fields and are written back as ODF fields. `formats::odt::tests_more::{headers, fields}` | | 2026-10-31 | trivial | ODT: paragraph spacing, indents and line spacing (`fo:margin-*`, `fo:text-indent`, `fo:line-height`, `style:line-height-at-least`; `style:line-spacing` leading as a proportional approximation) read from automatic and common styles and written back; the default and Standard paragraph styles give the document defaults, so an ODT paragraph without spacing has none instead of Word's 8 pt after and 1.15 lines. `formats::odt::tests_more::spacing` | diff --git a/docs/gaps.md b/docs/gaps.md index 6cce18241d..5dc3f7b9e9 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where WordCraft falls short of Word -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (gap 19 narrowed: ODT footnotes and endnotes read and written); previously trivial (gap 19 narrowed: ODT headers and footers written, text fields and paragraph spacing read and written); previously trivial (only the PDF sent to the printer leaves out link annotations; Save these pages as PDF keeps them, #4734; no ranked gap changed); previously trivial (documents with links print: the print job leaves out link annotations, #4734; no ranked gap changed; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion reference-run membership uses one ordered walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (gap 19: RTF reads and writes WMF and EMF pictures, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (gap 19 narrowed: ODT comments read and written); previously trivial (gap 19 narrowed: ODT footnotes and endnotes read and written); previously trivial (gap 19 narrowed: ODT headers and footers written, text fields and paragraph spacing read and written); previously trivial (only the PDF sent to the printer leaves out link annotations; Save these pages as PDF keeps them, #4734; no ranked gap changed); previously trivial (documents with links print: the print job leaves out link annotations, #4734; no ranked gap changed; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion reference-run membership uses one ordered walk, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (gap 19: RTF reads and writes WMF and EMF pictures, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (View windows: several open documents and Switch Windows in the engine; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Highlight update:** 2026-10-25 · Highlight shortcut toggles the last chosen colour off when the whole selection has it; explicit colour commands remain setters (#4085). Readiness estimates unchanged. > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-29 · **Change:** trivial (aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (#15: bibliography sources keep a corporate author, #4848); previously trivial (Table Properties applies to the selected rows, columns and cells, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (#7: Custom Document Properties dialog, #2982; previously trivial: egui 0.37 migration: the interface catalogs stay shared and pinned by `craft-i18n.rev` at `9205108cfdced5177706264038848b68b2422d2b` (27 languages, English fallback); input-method handling has new regressions but managed CI passed, and native IME acceptance plus native-speaker catalog review remain open; previously trivial: Markdown code spans keep line-broken text literal, #3738; previously trivial: small follow-ups from the field, table and AutoFormat batch, #4232; previously trivial: highlight target includes selected paragraph marks, #4085; previously trivial: full-target highlight check beyond 10,000 runs, #4085; previously trivial: Exact-spacing table contact uses the specified line height rather than paragraph-mark metrics (#3977); previously trivial: cu20we: restrict pre-spacing float origins to paragraph anchors; document cap helpers and regressions; previously trivial: a command that changes nothing is not an edit: no undo step, no unsaved changes, no relayout, and the redo stack survives it; previously minor: #8 narrowed: Spanish proofing (Spain, Mexico, Latin America, Uruguay) from the installed Hunspell dictionaries, #3360, #3553; previously trivial: gap 10: group connector rerouting preserves transformed free and unresolved endpoints under nonuniform scaling; obstacle routing still missing; readiness estimates unchanged; previously trivial: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: the ranked gap list held two rows each for #19, #20 and #21; each gap appears once now; a document with many relationships opens in the time its part takes, #3411; previously trivial: the file commands that change the document are checked on a protected document, the protection of a protected document needs an explicit stop, the session author can't change through `file.setAuthor` under protection (the CLI's `--author` uses the same protection check), and the post-check compares the settings and the metadata; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: text mini toolbar available with the right-click menu, #2959; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: tracked table/row/cell/section property changes record in WordCraft, narrowing gap 5; previously trivial: a document with many content controls saves in the time the controls take, #13; previously trivial: Format Page Numbers dialog carries Word's number formats and Continue from previous section / Start at, #1113; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: fixed font inputs in four layout controls; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: #8: English dialects — en-GB, en-CA, en-AU… flag each other's spellings, #1281; previously trivial: #1: shape gradients and transparency, #1500; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: line breaking: a slash opening a word travels whole to the next line, #2963; previously trivial: recipient list search and merged-only view, #1576; previously trivial: #2 impact restated: only ink rendering and OLE editing remain; previously trivial: #2 narrowed: native ink nested in groups and drawing canvases kept on save; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: automatic Asian/Latin and Asian/number spacing laid out, #2358; previously trivial: hint-reliant CJK outlines; previously trivial: numbered headings, #1793; previously trivial: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: #8 narrowed: Indonesian proofing from the installed Hunspell dictionary, #498; previously trivial: glossary/building blocks closed; previously trivial: Split Cells and Convert Text to Table follow the Word check, #2592; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: #29: a copied equation also goes in the MathML clipboard formats, #1297; previously trivial: #24: the Spike, Ctrl+F3 and Ctrl+Shift+F3; previously trivial: VML freeforms read and kept on save; previously trivial: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: floating-table overlap ignores empty line-break lines; previously trivial: copies keep their formatting on the clipboard as HTML, #1523; previously trivial: sample-text formulas on Enter, #1067; previously trivial: #29: copied equations as MathML, OMML open as #1297, #816; previously trivial: the Font list no longer keeps every previewed font in memory, #848; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw Table and Eraser landed, #303; previously minor: beta distance restated: ~15 points, ~150–230 h; previously: first version; every known shortfall from the 2026-10-10 re-measure, ranked) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 @@ -81,7 +81,7 @@ Text context menus now include the existing formatting mini toolbar (#2959), inc | 15 | **References depth**: no EndNote/Mendeley desktop integration (bibliography sources are saved in DOCX, #790; Zotero landed, #189; Word's own `Sources.xml` master list imports into Manage Sources, #2640) | | #790 stores sources in `customXml` and reads them back; `crates/zotero`; `references.sources` `import` reads a Word bibliography file (`b:Sources`) | Students and researchers | 3–6 | [target-app-parity.md](target-app-parity.md) | | 16 | **Right-to-left completeness**: kashida, RTL in HTML/ODT/RTF; six still-open older RTL bug reports to re-verify after #207 (#211, #199, #66, #63, #48, #19; #215 is closed). RTL tables and sections landed (#362); PDF export's Hebrew and Arabic text copies and searches as whole words (#1383) | | [layout-parity.md](layout-parity.md) | Arabic, Persian, Hebrew writers | 7–11 | [layout-parity.md](layout-parity.md) | | 17 | **East Asian typography**: hint-reliant DFKai-SB contours repaired (local Windows regression; no readiness estimate change); Asian Typography options (Phonetic Guide landed, #288; Enclose Characters landed, #297; document grid landed, #391; vertical text landed, #491) | | `para.asianTypography` is live (Paragraph dialog Asian Typography tab, #323), automatic Asian/Latin and Asian/number spacing is laid out (#2358), and no East Asian item is left in the catalog's missing list; the remaining depth is not measured | Chinese, Japanese and Korean documents | 11–19 | [layout-parity.md](layout-parity.md) | -| 19 | **RTF and ODT depth**: notes, comments, revisions (RTF and ODT insertions and deletions done; RTF paragraph-mark and character/paragraph formatting revisions done; RTF section/table and ODT formatting revisions left), sections (RTF and ODT import and export done, #1378, #1379), RTL. ODT sections landed on import (#1379): page-style changes and columned `text:section`s; ODT table column widths are read (they were dropped on every open). ODT export writes sections back (a master page per new-page section, a columned `text:section` per continuous one) and their headers and footers; ODT paragraph spacing, text fields, footnotes and endnotes read and written; RTF export writes every section (`\sect`, `\sectd`); RTF reads and writes WMF and EMF pictures (#4259) | | ~2,100 (RTF) and ~1,600 (ODT) lines in `crates/formats`; RTF code pages (#896) and page setup (#902), ODT page setup (#829) landed; open PRs: RTF notes and comments (#1056), ODT notes and comments (#1057), ODT headers and footers (#873) | Users exchanging with LibreOffice or older tools | 18–27 | [file-format-parity.md](file-format-parity.md) | +| 19 | **RTF and ODT depth**: notes, comments, revisions (RTF and ODT insertions and deletions done; RTF paragraph-mark and character/paragraph formatting revisions done; RTF section/table and ODT formatting revisions left), sections (RTF and ODT import and export done, #1378, #1379), RTL. ODT sections landed on import (#1379): page-style changes and columned `text:section`s; ODT table column widths are read (they were dropped on every open). ODT export writes sections back (a master page per new-page section, a columned `text:section` per continuous one) and their headers and footers; ODT paragraph spacing, text fields, footnotes, endnotes and comments read and written; RTF export writes every section (`\sect`, `\sectd`); RTF reads and writes WMF and EMF pictures (#4259) | | ~2,100 (RTF) and ~1,600 (ODT) lines in `crates/formats`; RTF code pages (#896) and page setup (#902), ODT page setup (#829) landed; open PRs: RTF notes and comments (#1056), ODT notes and comments (#1057), ODT headers and footers (#873) | Users exchanging with LibreOffice or older tools | 18–27 | [file-format-parity.md](file-format-parity.md) | | 18 | **Screen-reader access** to the document canvas: exposed, not yet tried with real screen readers | | The canvas describes paragraphs, headings, lists, tables, links, pictures and the caret to AccessKit (#488), tested headlessly; the ribbon's font name and size boxes, and the dropdown arrows beside them, say what they are (#3172); Windows Magnifier follows the caret, onto a new line too (#1119); no NVDA, VoiceOver or Orca run yet | Blind and low-vision users; public-sector procurement | 3–6 | [ui-parity.md](ui-parity.md) | | 20 | **Performance on large real-world documents** unmeasured (500+ pages, many pictures) | | DOCX comment saves avoid repeated paragraph-ID and durable-ID reservation searches; large threaded-comment fixtures retain metadata through save/reopen. Only the 188-page sample benchmark; scrolling a 1,096-page one-table document went from ~600 ms to ~2–5 ms a frame (#811), but opening it still lays out for seconds; opening a document with pictures no longer copies them (92 MB with 20 pictures: about 90 MB less); opening a document with many relationships now costs the size of its part and not that size times the number of relationships (1024 relationships and a 64 MiB part: 224 s → 0.2 s, #3411); a word with no break opportunity in it no longer lays out quadratically (1 MiB: 29.4 s → 0.35 s, #3412); saving a document with many content controls now costs the time the controls take (32,000 controls: 3.6 s → 0.1 s, #13) | Thesis and book authors | 10–20 | [layout-parity.md](layout-parity.md) | | 21 | **Mail merge data sources**: no Outlook/Contacts, Access or database sources, no email merge; CSV, typed lists (#247) and Excel/OpenDocument spreadsheets (#334, legacy .xls #778) only | | Select Recipients reads .csv/.tsv/.txt in any common encoding (UTF-8/16, GBK, Big5, Shift_JIS…), .xlsx/.xlsm, .xls and .ods; Edit Recipient List filters and sorts (#778), finds records and can show only the merged ones (#1576) | Office administrators | 4–8 | [target-app-parity.md](target-app-parity.md) | From bc50c6f7f933e7a56305af94e34852407aaa2dfc Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 23:53:03 +0000 Subject: [PATCH 037/308] Gap 19: ODT export keeps replies to replies Export wrote only the direct replies of a comment, so a reply to a reply was lost on save. Every reply in a comment's thread is written now, each answering the comment that starts the thread, as LibreOffice's threads are flat. A reply chain that loops (hostile) is written once. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GBVQLyDnrG8Tn1yDQcUabC --- crates/formats/src/model/from_doc.rs | 15 ++++++++++++--- crates/formats/src/odt/tests_more/comments.rs | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/formats/src/model/from_doc.rs b/crates/formats/src/model/from_doc.rs index ef90a5bd4e..d5da5a3801 100644 --- a/crates/formats/src/model/from_doc.rs +++ b/crates/formats/src/model/from_doc.rs @@ -139,12 +139,21 @@ fn rich_inlines(doc: &Document, o: &InlineObject, f: &Fmt, opts: FlowOpts) -> Op let endnote = *kind == wordcraft_doc::para::NoteKind::Endnote; vec![Inline::Note { endnote, custom: custom.clone(), blocks }] } - // The comment, then its replies, which have no place of their own. + // The comment, then the replies in its thread (replies to replies too, each answering the + // comment that starts the thread), which have no place of their own. InlineObject::CommentStart { id } if opts.comments => { let Some(c) = doc.comments.get(id) else { return Some(Vec::new()) }; let mut out = vec![Inline::Comment { id: *id, comment: Box::new(flow_comment(doc, c, None, opts)) }]; - for (rid, r) in doc.comments.iter().filter(|(_, r)| r.parent == Some(*id)) { - out.push(Inline::Comment { id: *rid, comment: Box::new(flow_comment(doc, r, Some(*id), opts)) }); + let mut thread = std::collections::BTreeSet::from([*id]); + let mut k = 0; + while let Some(Inline::Comment { id: at, .. }) = out.get(k) { + let at = *at; + k += 1; + for (rid, r) in doc.comments.iter().filter(|(_, r)| r.parent == Some(at)) { + if thread.insert(*rid) { + out.push(Inline::Comment { id: *rid, comment: Box::new(flow_comment(doc, r, Some(*id), opts)) }); + } + } } out } diff --git a/crates/formats/src/odt/tests_more/comments.rs b/crates/formats/src/odt/tests_more/comments.rs index 0890f99d23..0e3a43a7d8 100644 --- a/crates/formats/src/odt/tests_more/comments.rs +++ b/crates/formats/src/odt/tests_more/comments.rs @@ -168,3 +168,20 @@ fn unmatched_and_hostile_comments_do_not_panic() { } } } + +#[test] +fn a_reply_to_a_reply_is_kept() { + let xml = concat!( + r#"ARootx"#, + r#"BReply"#, + r#"CReply to reply"#, + ); + let d = import(&body(xml)).unwrap(); + assert_eq!(d.comments.len(), 3); + // LibreOffice's threads are flat: every reply answers the comment that starts them. + let back = import(&export(&d).unwrap()).unwrap(); + let got = comments(&back); + assert_eq!(got.len(), 3, "{got:?}"); + assert_eq!(got[2].4.as_deref(), Some("Root")); + assert_eq!(got[2].5, "Reply to reply"); +} From e4bac37236235baff1f74c672a1ace33ae075878 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 1 Nov 2026 00:08:10 +0000 Subject: [PATCH 038/308] Gap 19: ODT lists keep their number formats, bullets and identity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each level of a list style (ODF 1.3 §16.30-16.33) is read with its number format (1, i, I, a, A, none), prefix and suffix, levels shown, start value and bullet character, and written back. Each `text:list` is a list of its own unless it continues the one before it (`text:continue-numbering`) or the one it names (`text:continue-list`); an item's `text:start-value` restarts from there. A list inside a note or table cell leaves the list around it alone. Before, consecutive lists merged into one: three LibreOffice lists numbered 1., i. and A. opened as one decimal list 1-7. The flow model's ListInfo carries a list number and a compact label (prefix and suffix up to 12 bytes, so "Chapter %1" fits) only for flows that ask for them (FlowOpts::lists); the other formats are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GBVQLyDnrG8Tn1yDQcUabC --- ROADMAP.md | 3 +- crates/formats/src/html/builder.rs | 2 +- crates/formats/src/latex/parser.rs | 2 +- crates/formats/src/latex/tests.rs | 4 +- crates/formats/src/markdown/blocks.rs | 2 +- crates/formats/src/markdown/tests.rs | 14 +- crates/formats/src/model.rs | 10 +- crates/formats/src/model/from_doc.rs | 22 +++- crates/formats/src/model/to_doc.rs | 48 ++++++- crates/formats/src/model/types.rs | 100 ++++++++++++++ crates/formats/src/odt/body.rs | 110 ++++++++++++---- crates/formats/src/odt/package.rs | 4 +- crates/formats/src/odt/styles.rs | 51 +++++++- crates/formats/src/odt/tests_more.rs | 1 + crates/formats/src/odt/tests_more/lists.rs | 143 +++++++++++++++++++++ crates/formats/src/odt/writer.rs | 76 +++++++++-- crates/formats/src/rtf/styles.rs | 2 +- crates/formats/src/rtf/tests.rs | 8 +- crates/formats/src/tests.rs | 5 +- docs/file-format-parity.md | 5 +- docs/gaps.md | 4 +- 21 files changed, 539 insertions(+), 77 deletions(-) create mode 100644 crates/formats/src/odt/tests_more/lists.rs diff --git a/ROADMAP.md b/ROADMAP.md index 38ebb2080a..776aacb432 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -4,7 +4,7 @@ **Stage: alpha** · next: beta, ~15 points (60% → 75% ready for real work) and ~150–230 h away -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT comments); previously trivial (progress log: ODT footnotes and endnotes); previously trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-31 · **Change:** trivial (progress log: ODT lists); previously trivial (progress log: ODT comments); previously trivial (progress log: ODT footnotes and endnotes); previously trivial (progress log: ODT headers, footers and fields); previously trivial (progress log: ODT paragraph spacing); previously trivial (progress log: a paragraph naming an undefined style is formatted as Normal, #5077); previously trivial (progress log: TOC page numbers as PAGEREF fields, #5097); previously trivial (progress log: Backstage keytips, #4960); previously trivial (Progress log: only the printer's PDF leaves out links; Save these pages as PDF keeps them, #4734); previously trivial (progress log: Chinese and Japanese text counts a word per character, #4547; previously trivial: aligned-copy review fixes retain the document axis through scrolling and include secondary selections (#4764)); previously trivial (connector rerouting indexes laid-out object positions once per batch (#3631); previously trivial (progress log: corporate authors in bibliographies, #4848); previously trivial (custom-property bookmark-link preservation, #4767); previously trivial (progress log: Table Properties follows the selection, #4857); previously trivial (Relative font-size commands preserve mixed sizes in the selection (#4903); existing button steps and one-point shortcuts are unchanged; readiness estimates unchanged); previously trivial (Arabic Backspace removes one combining mark and keeps its base letter (#4757); previously trivial (note conversion walks reference offsets and formatting runs together, #4750); previously trivial (DOCX comment-ID save lookups); previously trivial (progress log: RTF pictures stored as Windows or Enhanced Metafiles are imported, #4259); previously trivial (#4658 bounds nested Markdown reference-label work); previously trivial (HTML import keeps preformatted lines with CR or CRLF input newlines, #4810); previously trivial (progress log: Insert › Shapes icon grid, #4349); previously trivial (progress log: AutoCorrect Options with no document open, #4585); previously trivial (progress log: window fallback geometry and equation symbol tooltips); previously trivial (progress log: window titles omit NUL characters, #3634); previously trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (progress log: Custom document properties dialog, #2982; previously trivial: progress log: View › New Window; previously trivial: progress log: interface translations come from storytold/craft-i18n through the `craft-i18n` crate; previously trivial: progress log: disabled-control reasons translated in every interface language, #2678; previously trivial: progress log: mail merge record navigation and record ranges for printing, #3028; previously trivial: selection bar for right-to-left paragraphs and table rows; previously trivial: group connector rerouting preserves rotated and flipped free or unresolved endpoints under nonuniform group scaling; readiness estimates unchanged; previously trivial: progress log: a word with no break opportunity in it no longer makes the layout quadratic, #3412; previously trivial: progress log: a relationship the markup names without the prefix is read as it wrote, and an ordinary `id` names no part; previously trivial: progress log: a document with many relationships opens in the time its part takes, and an id the text only mentions is not a reference, #3411; previously trivial: progress log: read only and comments-only protection grey out edits; previously trivial: progress log: an empty paragraph with a hidden mark shows nothing, not even its list number, #3982; previously trivial: progress log: "NaN" in the Font dialog's Size box no longer closes the app, #3663; previously trivial: progress log: a table cell span from a `.json` file no longer overflows, #3637; previously trivial: progress log: a click in the footer while editing the header moves there; previously trivial: explicit documents keep the editor when recovery discovery finishes; readable recovery controls; previously trivial: Slovenian interface catalog, 2,614 entries; previously trivial: Insert Table of Figures dialog for any caption label, #3013; previously trivial: progress log: Home › Underline drop-down, #2738; previously trivial: personal templates in File › New and Save as Word Template, #2852; previously trivial: progress log: the scrollbar page tip stays on one line, #3341; previously trivial: progress log: forms protection greys out the control buttons; previously trivial: progress log: a document with many content controls saves in the time the controls take, #13; previously trivial: progress log: Word signature lines kept on save; previously trivial: a Thai run with Latin finds one installed font covering it all on Linux too, #1549; previously trivial: progress log: closing the header returns to the body selection, #3331; previously trivial: progress log: saving to a NAS share mounted by GVFS, #3003; previously trivial: progress log: a picture dragged near the bottom of a page lands there, #3169; previously trivial: centred list labels after leading page breaks, #2964; previously trivial: progress log: inline pictures drag to an insertion cursor and stay inline, #2233, #2234; previously trivial: progress log: a text file saved over itself closes without asking, #2376; previously trivial: progress log: a text frame in the paragraph defaults frames nothing, #2360; previously trivial: fixed font inputs in four layout controls; previously trivial: typing no longer starts render threads on every repaint, #3027; previously trivial: a right, centre or decimal tab whose text doesn't fit, or a left tab past the right indent, goes to the next line with its text, #1840; previously trivial: progress log: banded rows shade as in Word, #3415; previously trivial: progress log: the table handles show while the caret is in the table, #2428; previously trivial: progress log: a click below a floating table reaches the text after it, #2038; previously trivial: progress log: Delete on selected table cells leaves one empty paragraph in each, #2802; previously trivial: progress log: lam-alef in exported PDFs extracts whole with fonts that draw it as two glyphs, #2310; previously trivial: Bullets and Numbering toggle off style-inherited lists at their effective level, including basedOn chains and numbered ListParagraph styles; other paragraphs and list definitions stay unchanged (related to #3459); previously trivial: Russian sorting keeps ё with е and recognises decomposed ё/й; previously trivial: progress log: a text box that keeps the keyboard no longer resets the input method every frame, #2986; previously trivial: progress log: recovery copies and .json files store pictures as base64, #586 follow-up; previously trivial: progress log: legacy menu keys, #1697; previously trivial: progress log: shape gradients and transparency, #1500; previously trivial: progress log: Focus fills the screen, and Full Screen View shows the pages alone, #1678; previously trivial: progress log: text above a float's anchor wraps around it, #2048; previously trivial: progress log: File › Recover follows the app theme, #3243; previously trivial: progress log: formatting toolbar with the text context menu, #2959; previously trivial: progress log: caret inside ligatures, #1974; previously trivial: text typed into a content control takes the control's formatting; localized placeholder styles recognised; previously trivial: forms protection keeps its content controls: no inserting, Properties or Remove Content Control; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: progress log: a header-cell picture watermark sits behind the text, #1775; previously trivial: symbols keep their font when the text font changes, #2036; previously trivial: opening a DOCX no longer copies its pictures; previously trivial: Add Font imports every selected font file, #1789); previously trivial (RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: Read Aloud speaks each sentence in its own language, #1843; previously trivial: hint-reliant CJK outlines; previously trivial: Ctrl+wheel zoom no longer scrolls the document, #1743; previously trivial: progress log: Turkish case mapping in AutoCorrect, #1923; previously trivial: a mouse drag on a table leaves the view where it is, #1738; previously trivial: progress log: Hebrew field results and smart quotes, #1670; previously trivial: Backspace removes a selected table, rows or columns, #1844; previously trivial: progress log: mixed Hebrew/Latin text keeps its order in Word, #1543; previously trivial: progress log: web typing in non-Latin text no longer repeats, #1480; previously trivial: progress log: font family caches bounded and math font lookups follow font changes, #1354; previously trivial: `-->` AutoCorrect replacement, #2223; previously trivial: numbered headings, #1793; previously trivial: Navigation Find exposes Results, #2096; previously trivial: progress log: Columns on selected text, #2024; previously trivial: progress log: legacy outline, shadow, emboss and imprint text effects drawn, #2534; previously trivial: progress log: Indonesian proofing, #498; previously trivial: progress log: DOCX glossary documents (building blocks); previously trivial: progress log: Read Mode keeps the keyboard and shows whole, unobscured columns, #1772); previously trivial: progress log: font list entries of tall-ascent fonts no longer cut off, #2570); previously trivial: progress log: Split Cells and Convert Text to Table dialogs follow the Word check, #2592; previously trivial: a new source's Tag is made like Word's, three letters of the author and two digits of the year with a number added when taken, and an edit keeps the Tag, #2094; previously trivial: Web Layout fills the window width, #1876; previously trivial: progress log: Different First Page headers reachable from page 1, #2826; previously trivial: Language dialog keeps inherited proofing language and no-proof choices (#2717); previously trivial: progress log: web preferences kept between visits, #2132; previously trivial: progress log: undrawable private-use list bullets show the level's default bullet, #1683; previously trivial: progress log: `w:eastAsianLayout` kept on save; previously trivial: table style editor formats every part, #714); previously trivial: VML freeforms read and kept on save; previously trivial: progress log: consistent preferred cell widths; previously trivial: multi-paragraph field instructions no longer add blank lines; previously trivial: page-break-only list paragraphs hide their labels; previously trivial: progress log: grouped `flowChartOr` shapes draw their circle and crossing spokes; previously trivial: progress log: copies keep their formatting in other programs, #1523; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-29 · **Change:** trivial (Recipient labels continue across product-sized sheets instead of stopping after the first sheet (#4880)); previously trivial (English dialects in proofing, #1281; previously trivial: triple borders draw three lines, #1740; previously trivial: progress log: double-clicking a later page's footer no longer scrolls up, #2103; previously trivial: long navigation headings wrap onto further lines within the pane, #3015; previously trivial: clearing a no-match font query keeps the recent heading visible, #2068; previously trivial: a word after a slash wraps whole, like Word, #2963; previously trivial: RTF export writes sections (gap 19); previously trivial: ODT export writes sections (gap 19); previously trivial: progress log: pasted blockquotes are indented paragraphs, #1270; previously trivial: progress log: print-area strings translated in every catalog; previously trivial: progress log: comments readable in the dark theme, #2045; previously trivial: progress log: ODT sections, #1379; previously trivial: progress log: table deletes, Change Case and Unlink Fields respect Track Changes and locked content controls, #1329; previously trivial: progress log: rounded rectangle corners, #1476; previously trivial: progress log: Save As type list, #1399; previously trivial: progress log: privacy options on save, #1062; previously trivial: progress log: equation pictures in theme colours on Windows, #1002; previously trivial: symbol fonts, #776; previously trivial: the save prompt answers from the keyboard, #861; previously trivial: table column and row markers on the rulers, #700; previously trivial: table borders and the resize handle drag on the page, #217; previously trivial: formatting revisions landed, #41; previously trivial: Draw tab ink, #307, and Draw Table and Eraser, #303, landed: catalog 402/434; previously minor: readiness table with hours per audience; full number back to the additive weighted sum, 60%; mainstream and essentials numbers added; alpha gate checked; previously major: full re-measure against Word for Mac 16.113.4; progress docs restructured) · **Target:** Microsoft Word (Microsoft 365) for Mac 16.113.4 > **Printing update:** 2026-10-12 · direct desktop printing through craft-print; details below. @@ -140,6 +140,7 @@ Ranked, to beta (detail and remaining milestones in [docs/roadmap.md](docs/roadm ## Progress log +- 2026-10-31: ODT: lists keep their numbering (i., A., (1), bullets) and stay separate lists as in LibreOffice; consecutive lists used to merge into one decimal list (gap 19). Readiness unchanged. - 2026-10-31: ODT: comments open and save with their ranges, replies and resolved state, as LibreOffice keeps them (gap 19; they were dropped on open). Readiness unchanged. - 2026-10-31: ODT: footnotes and endnotes open and save (they were dropped on open), as Word-style note stories with their custom marks (gap 19). Readiness unchanged. - 2026-10-31: ODT: headers and footers are saved (they were dropped on every save), each page's header sits where LibreOffice puts it on open, and page numbers, page counts, dates and other text fields open as fields that update and are saved as ODF fields (gap 19). Readiness unchanged. diff --git a/crates/formats/src/html/builder.rs b/crates/formats/src/html/builder.rs index 5de71edd67..90242f010d 100644 --- a/crates/formats/src/html/builder.rs +++ b/crates/formats/src/html/builder.rs @@ -272,7 +272,7 @@ impl<'r> Builder<'r> { } } if let (Some((o, _)), Some(li)) = (list_level, li_at) { - p.list = Some(ListInfo { ordered: o, level: lists.saturating_sub(1).min(8) as u8, start: 1 }); + p.list = Some(ListInfo { ordered: o, level: lists.saturating_sub(1).min(8) as u8, start: 1, ..Default::default() }); self.para_cont = self.stack.get(li).is_some_and(|e| e.used); if let Some(e) = self.stack.get_mut(li) { e.used = true; diff --git a/crates/formats/src/latex/parser.rs b/crates/formats/src/latex/parser.rs index 2be4d3b8ab..ff78f06397 100644 --- a/crates/formats/src/latex/parser.rs +++ b/crates/formats/src/latex/parser.rs @@ -729,7 +729,7 @@ impl<'a> Parser<'a> { let level = self.lists.len().saturating_sub(1).min(8) as u8; self.para = Para::default(); if let Some(&ordered) = self.lists.last() { - self.para.list = Some(ListInfo { ordered, level, start: 1 }); + self.para.list = Some(ListInfo { ordered, level, start: 1, ..Default::default() }); } let label = self.opt_arg(); if let Some(l) = label { diff --git a/crates/formats/src/latex/tests.rs b/crates/formats/src/latex/tests.rs index 60638d6f84..cc258a83a1 100644 --- a/crates/formats/src/latex/tests.rs +++ b/crates/formats/src/latex/tests.rs @@ -151,8 +151,8 @@ fn assert_article_blocks(ps: &[Para]) { assert_eq!(find("On Sample Documents").kind, Kind::Title); assert_eq!(find("Introduction").kind, Kind::Heading(1)); assert_eq!(find("Lists").kind, Kind::Heading(2)); - assert_eq!(find("First").list, Some(ListInfo { ordered: false, level: 0, start: 1 })); - assert_eq!(find("Nested one").list, Some(ListInfo { ordered: true, level: 1, start: 1 })); + assert_eq!(find("First").list, Some(ListInfo { ordered: false, level: 0, start: 1, ..Default::default() })); + assert_eq!(find("Nested one").list, Some(ListInfo { ordered: true, level: 1, start: 1, ..Default::default() })); assert!(ps.iter().any(|p| p.text().trim() == "b) Nested two" && p.list.is_some()), "{:?}", texts(ARTICLE)); assert_eq!(find("Quoted text.").kind, Kind::Quote); assert_eq!(find("Centered.").align, Some(Align::Center)); diff --git a/crates/formats/src/markdown/blocks.rs b/crates/formats/src/markdown/blocks.rs index 3a1bb2a269..cf6216b640 100644 --- a/crates/formats/src/markdown/blocks.rs +++ b/crates/formats/src/markdown/blocks.rs @@ -620,7 +620,7 @@ fn parse_list(lines: &[String], mut i: usize, depth: usize, level: u8, out: &mut while item.len() > 1 && item.last().is_some_and(String::is_empty) { item.pop(); } - let li = ListInfo { ordered: first.ordered, level: level.min(8), start: first.start }; + let li = ListInfo { ordered: first.ordered, level: level.min(8), start: first.start, ..Default::default() }; let inner = if depth >= MAX_NEST { let mut p = Para::default(); p.push_text(&item.join(" "), &Fmt::default()); diff --git a/crates/formats/src/markdown/tests.rs b/crates/formats/src/markdown/tests.rs index 32767eeee4..97bd3c6443 100644 --- a/crates/formats/src/markdown/tests.rs +++ b/crates/formats/src/markdown/tests.rs @@ -418,8 +418,8 @@ fn code_span_line_endings_stay_literal_on_export() { ]; let shapes = [ (Kind::Normal, None), - (Kind::Normal, Some(ListInfo { ordered: false, level: 0, start: 1 })), - (Kind::Code, Some(ListInfo { ordered: true, level: 0, start: 1 })), + (Kind::Normal, Some(ListInfo { ordered: false, level: 0, start: 1, ..Default::default() })), + (Kind::Code, Some(ListInfo { ordered: true, level: 0, start: 1, ..Default::default() })), ]; for (source, expected) in cases { for (kind, list) in shapes { @@ -576,10 +576,10 @@ fn lines_after_line_break_stay_in_paragraph() { fn lines_after_line_break_stay_in_list_item_and_quote() { let cases = ["a\n- x", "a\n# h", "a\n1. x", "a\n--", "a\n---", "a\n==", "a\n> q"]; let shapes: [(Kind, Option); 5] = [ - (Kind::Normal, Some(ListInfo { ordered: false, level: 0, start: 1 })), - (Kind::Normal, Some(ListInfo { ordered: true, level: 0, start: 1 })), - (Kind::Normal, Some(ListInfo { ordered: false, level: 1, start: 1 })), - (Kind::Normal, Some(ListInfo { ordered: true, level: 1, start: 1 })), + (Kind::Normal, Some(ListInfo { ordered: false, level: 0, start: 1, ..Default::default() })), + (Kind::Normal, Some(ListInfo { ordered: true, level: 0, start: 1, ..Default::default() })), + (Kind::Normal, Some(ListInfo { ordered: false, level: 1, start: 1, ..Default::default() })), + (Kind::Normal, Some(ListInfo { ordered: true, level: 1, start: 1, ..Default::default() })), (Kind::Quote, None), ]; for s in cases { @@ -587,7 +587,7 @@ fn lines_after_line_break_stay_in_list_item_and_quote() { let mut blocks = Vec::new(); if let Some(li) = list.filter(|l| l.level > 0) { let mut parent = Para::new(Kind::Normal); - parent.list = Some(ListInfo { ordered: li.ordered, level: 0, start: 1 }); + parent.list = Some(ListInfo { ordered: li.ordered, level: 0, start: 1, ..Default::default() }); parent.push_text("top", &Fmt::default()); blocks.push(FBlock::Para(parent)); } diff --git a/crates/formats/src/model.rs b/crates/formats/src/model.rs index 6af8b9a4a1..c55fb2e5fe 100644 --- a/crates/formats/src/model.rs +++ b/crates/formats/src/model.rs @@ -24,9 +24,9 @@ pub use from_doc::{ListCounter, flow_paras, from_doc, from_doc_tracked, from_doc pub use media::{base64_decode, base64_encode, data_uri, decode_bounded, image_px, make_img, mime_of, percent_decode, sized_img, sniff_image}; pub use to_doc::{char_props, part_blocks, to_doc, to_doc_in}; pub use types::{ - CODE_STYLE, Cell, FBlock, FComment, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, ListInfo, MAX_DEPTH, MAX_FONT_NAME, MAX_LINK, - MONO_FONT, Meta, Para, RUBY_BASE_SIZE, RevMark, Spacing, clean_text, equations_as_text, flatten_tables, kept_font, kept_link, ruby_as_text, - ruby_object, + Affix, CODE_STYLE, Cell, FBlock, FComment, FTable, Flow, FlowOpts, Fmt, FmtChange, Img, Inline, Kind, Label, ListInfo, MAX_DEPTH, MAX_FONT_NAME, + MAX_LINK, MONO_FONT, Meta, Para, RUBY_BASE_SIZE, RevMark, Spacing, clean_text, equations_as_text, flatten_tables, kept_font, kept_link, + ruby_as_text, ruby_object, }; pub(crate) use types::{Dropped, begin_import, dropped}; @@ -90,12 +90,12 @@ mod tests { #[test] fn list_labels() { let mut c = ListCounter::default(); - let o = |level| ListInfo { ordered: true, level, start: 1 }; + let o = |level| ListInfo { ordered: true, level, start: 1, ..Default::default() }; assert_eq!(c.label(o(0)), "1."); assert_eq!(c.label(o(1)), "a."); assert_eq!(c.label(o(1)), "b."); assert_eq!(c.label(o(0)), "2."); - assert_eq!(c.label(ListInfo { ordered: false, level: 0, start: 1 }), "•"); + assert_eq!(c.label(ListInfo { ordered: false, level: 0, start: 1, ..Default::default() }), "•"); } #[test] diff --git a/crates/formats/src/model/from_doc.rs b/crates/formats/src/model/from_doc.rs index d5da5a3801..50c7259fc7 100644 --- a/crates/formats/src/model/from_doc.rs +++ b/crates/formats/src/model/from_doc.rs @@ -109,7 +109,13 @@ fn props_kind(doc: &Document, props: &ParaProps, blank: bool) -> (Kind, Option Vec { /// insertion and deletion marks. fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { let tracked = opts.tracked; - let (kind, list) = para_kind(doc, p); + let (kind, mut list) = para_kind(doc, p); + if !opts.lists + && let Some(li) = &mut list + { + (li.list, li.label) = (0, None); + } let mut done: Vec = Vec::new(); let borders = if kind == Kind::Rule { None } else { p.props.borders.filter(|b| b.any_visible()) }; let mut out = Para { @@ -334,7 +345,12 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { // The formatting change belongs to the paragraph the mark ends. out.fmt_change = p.props.fmt_change.as_deref().map(|c| { let blank = out.inlines.is_empty(); - let (kind, list) = props_kind(doc, &c.old, blank); + let (kind, mut list) = props_kind(doc, &c.old, blank); + if !opts.lists + && let Some(li) = &mut list + { + (li.list, li.label) = (0, None); + } let old = Para { kind, list, align: c.old.align, page_break: c.old.page_break_before.unwrap_or(false), ..Default::default() }; Box::new(FmtChange { mark: rev_mark(doc, c.rev), old }) }); diff --git a/crates/formats/src/model/to_doc.rs b/crates/formats/src/model/to_doc.rs index 7ccf634376..80e5d28ee2 100644 --- a/crates/formats/src/model/to_doc.rs +++ b/crates/formats/src/model/to_doc.rs @@ -114,6 +114,8 @@ struct Builder<'a> { run_strings: usize, /// The document's comment for each flow comment id. comments: std::collections::HashMap, + /// The document's list instance for each flow list number ([`ListInfo::list`]). + lists: std::collections::HashMap, } fn rev_key(kind: RevisionKind) -> u8 { @@ -133,7 +135,7 @@ impl<'a> Builder<'a> { revs.entry((rev_key(r.kind), mark)).or_insert(i as u32); } } - Builder { doc, revs, run_strings: 0, comments: Default::default() } + Builder { doc, revs, run_strings: 0, comments: Default::default(), lists: Default::default() } } /// The document revision for a tracked change, added the first time it is seen. @@ -358,12 +360,18 @@ impl<'a> Builder<'a> { } } let mut starts: [Option; 9] = [None; 9]; + let mut labels: [Option; 9] = [None; 9]; for li in group { if let Some(s) = starts.get_mut(li.level.min(8) as usize) && s.is_none() { *s = Some(li.start); } + if let Some(l) = labels.get_mut(li.level.min(8) as usize) + && l.is_none() + { + *l = li.label; + } } // Levels nobody used follow the first level's kind. let first = group.first().map(|l| l.ordered).unwrap_or(false); @@ -375,6 +383,21 @@ impl<'a> Builder<'a> { if ordered && let Some(s) = starts.get(i).copied().flatten() { level.start = s; } + // The source's own label: its format and text, or its bullet. + match labels.get(i).copied().flatten() { + Some(l) if ordered && l.format != wordcraft_doc::section::NumFormat::Bullet => { + level.format = l.format; + level.text = l.text(i as u8); + } + Some(l) if !ordered => { + if let Some(c) = l.bullet().filter(|c| !c.is_control() && !level.text.starts_with(*c)) { + let custom = levels_for(ListKind::BulletChar(c)); + level.text = c.to_string(); + level.chr.font = custom.first().and_then(|l| l.chr.font.clone()); + } + } + _ => {} + } level }) .collect(); @@ -392,14 +415,20 @@ impl<'a> Builder<'a> { while let Some(b) = blocks.get(i) { match b { FBlock::Para(p) if p.list.is_some() => { - // A run of consecutive list paragraphs is one list, until a top-level item of - // the other kind (bulleted or numbered) starts a new one. + // A run of consecutive list paragraphs is one list, until an item of another + // list or (when the source doesn't tell lists apart) a top-level item of the + // other kind (bulleted or numbered) starts a new one. A list the source + // numbers goes on where it shows up again. let mut j = i; let mut group: Vec = Vec::new(); let mut top: Option = None; + let id = p.list.map_or(0, |l| l.list); while let Some(FBlock::Para(q)) = blocks.get(j) { let Some(li) = q.list else { break }; - if li.level == 0 { + if li.list != id { + break; + } + if li.level == 0 && id == 0 { if top.is_some_and(|o| o != li.ordered) { break; } @@ -408,7 +437,16 @@ impl<'a> Builder<'a> { group.push(li); j += 1; } - let num = self.new_list(&group); + let num = match self.lists.get(&id) { + Some(num) if id != 0 => *num, + _ => { + let num = self.new_list(&group); + if id != 0 { + self.lists.insert(id, num); + } + num + } + }; for q in blocks.get(i..j).unwrap_or(&[]) { if let FBlock::Para(q) = q { let para = self.para(q, Some(num)); diff --git a/crates/formats/src/model/types.rs b/crates/formats/src/model/types.rs index 1cc99dc722..460699db62 100644 --- a/crates/formats/src/model/types.rs +++ b/crates/formats/src/model/types.rs @@ -238,6 +238,9 @@ pub struct FlowOpts { pub notes: bool, /// Comments as [`Inline::Comment`] and [`Inline::CommentEnd`] (otherwise left out). pub comments: bool, + /// Lists with their identity and labels ([`ListInfo::list`], [`ListInfo::label`]; otherwise + /// consecutive items form one list in the default look of its kind). + pub lists: bool, } /// Base text size (points) a phonetic guide falls back to when the source states none: the @@ -281,6 +284,103 @@ pub struct ListInfo { pub level: u8, /// Number of the first item of an ordered list (1 unless the source says otherwise). pub start: u32, + /// The list the paragraph belongs to, when the source tells its lists apart (ODT): items + /// of one list share its number, wherever they are, and a different number starts another + /// list. 0: not stated (consecutive items form one list). + pub list: u32, + /// How the paragraph's level shows its number or bullet, when the source says. + pub label: Option