From a528fa7430314b3a0905a5cdc86be39631bf86f8 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 11:23:04 +0100 Subject: [PATCH 1/6] rtf: keep right-to-left paragraph direction Read \rtlpar/\rtldir and \ltrpar/\ltrdir into the paragraph's bidi flag and write \rtlpar for right-to-left paragraphs. The flow paragraph carries the direction between the document and the RTF reader/writer. Co-Authored-By: Claude Sonnet 5.5 --- crates/flow/src/from_doc.rs | 3 +++ crates/flow/src/to_doc.rs | 3 +++ crates/flow/src/types.rs | 2 ++ crates/rtf/src/doc/export.rs | 4 ++++ crates/rtf/src/doc/state.rs | 3 +++ crates/rtf/src/doc/styles.rs | 1 + crates/rtf/src/doc/tests.rs | 30 ++++++++++++++++++++++++++++++ crates/rtf/src/doc/word.rs | 4 ++++ 8 files changed, 50 insertions(+) diff --git a/crates/flow/src/from_doc.rs b/crates/flow/src/from_doc.rs index 341ded449..ea7f6fc92 100644 --- a/crates/flow/src/from_doc.rs +++ b/crates/flow/src/from_doc.rs @@ -493,6 +493,8 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { kind, list, align: p.props.align, + // Direction can come from the paragraph's style (`w:bidi` in the style's pPr). + rtl: doc.styles.resolve_para(&p.props).bidi, page_break: p.props.page_break_before.unwrap_or(false), keep_next: p.props.keep_next.unwrap_or(false), inlines: Vec::new(), @@ -536,6 +538,7 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { kind, list: None, align: p.props.align, + rtl: out.rtl, page_break: true, keep_next: p.props.keep_next.unwrap_or(false), inlines: Vec::new(), diff --git a/crates/flow/src/to_doc.rs b/crates/flow/src/to_doc.rs index c10be85c2..ce29a4448 100644 --- a/crates/flow/src/to_doc.rs +++ b/crates/flow/src/to_doc.rs @@ -159,6 +159,9 @@ fn para_props(p: &Para, list_num: Option) -> ParaProps { out.numbering = Some(NumRef { num, level: li.level.min(8) }); } out.align = p.align; + if p.rtl { + out.bidi = Some(true); + } let sp = p.spacing; out.space_before = sp.before; out.space_after = sp.after; diff --git a/crates/flow/src/types.rs b/crates/flow/src/types.rs index e897778f9..f1c281c62 100644 --- a/crates/flow/src/types.rs +++ b/crates/flow/src/types.rs @@ -676,6 +676,8 @@ pub struct Para { pub kind: Kind, pub list: Option, pub align: Option, + /// The paragraph reads right to left (OOXML `w:bidi`, RTF `\rtlpar`). + pub rtl: bool, pub page_break: bool, /// Keep the paragraph on the same page as the next one (ODT `fo:keep-with-next`). pub keep_next: bool, diff --git a/crates/rtf/src/doc/export.rs b/crates/rtf/src/doc/export.rs index 69fab3d76..8f0f1734b 100644 --- a/crates/rtf/src/doc/export.rs +++ b/crates/rtf/src/doc/export.rs @@ -664,6 +664,10 @@ impl Writer { Some(Align::Distribute) => out.push_str("\\qd"), _ => out.push_str("\\ql"), } + // The paragraph's text direction, as Word writes it (`\rtlpar`; `\ltrpar` is the default). + if p.rtl { + out.push_str("\\rtlpar"); + } if p.page_break { out.push_str("\\pagebb"); } diff --git a/crates/rtf/src/doc/state.rs b/crates/rtf/src/doc/state.rs index 486b94a32..1f5d8d2e6 100644 --- a/crates/rtf/src/doc/state.rs +++ b/crates/rtf/src/doc/state.rs @@ -148,6 +148,8 @@ pub(super) struct St { pub(super) hidden: bool, // Paragraph properties. pub(super) align: Option, + /// `\rtlpar`: the paragraph reads right to left. + pub(super) rtldir: bool, pub(super) style: i32, pub(super) intbl: bool, pub(super) ls: Option, @@ -185,6 +187,7 @@ impl Default for St { uc: 1, hidden: false, align: None, + rtldir: false, style: 0, intbl: false, ls: None, diff --git a/crates/rtf/src/doc/styles.rs b/crates/rtf/src/doc/styles.rs index 014a105b4..dd42f3b87 100644 --- a/crates/rtf/src/doc/styles.rs +++ b/crates/rtf/src/doc/styles.rs @@ -87,6 +87,7 @@ impl Reader { pub(super) fn para_props(&self, st: &St) -> Para { let mut p = Para::new(self.para_kind(st)); p.align = st.align; + p.rtl = st.rtldir; p.page_break = st.pagebb; p.spacing = spacing(st); p.borders = st.borders.any_visible().then_some(st.borders); diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 7c1075a86..9274104ab 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -38,6 +38,36 @@ fn basic_reader() { assert!(matches!(&l.inlines[0], Inline::Text(_, f) if f.link.as_deref() == Some("http://a.b"))); } +#[test] +fn rtl_paragraphs_read_and_write_their_direction() { + // Word writes an RTL paragraph as `\rtlpar` (`\ltrpar` for LTR); reading it keeps the + // direction as w:bidi on the document paragraph, and export writes it back. + let rtf = br#"{\rtf1\ansi\pard\rtlpar RTL\par\pard\ltrpar LTR\par\pard plain\par}"#; + let flow = parse(rtf).unwrap(); + let paras: Vec<&Para> = flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect(); + assert!(paras[0].rtl, "rtlpar sets the direction"); + assert!(!paras[1].rtl && !paras[2].rtl, "ltrpar and no word: not RTL"); + // \rtldir and \ltrdir, which some writers used for paragraphs, are read too. + let old = parse(br#"{\rtf1\ansi\pard\rtldir RTL\par\pard\ltrdir LTR\par}"#).unwrap(); + let old: Vec<&Para> = old.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect(); + assert!(old[0].rtl && !old[1].rtl, "rtldir and ltrdir are read"); + // \pard resets the direction. + let reset = parse(br#"{\rtf1\ansi\pard\rtlpar A\par\pard B\par}"#).unwrap(); + let reset: Vec<&Para> = reset.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p) } else { None }).collect(); + assert!(reset[0].rtl && !reset[1].rtl, "pard resets the direction"); + // Import keeps it as bidi on the document paragraph. + let bidi = |d: &Document| { + d.body.iter().filter_map(|b| if let Block::Para(p) = &**b { Some(p.props.bidi.unwrap_or(false)) } else { None }).collect::>() + }; + let doc = import(rtf).unwrap(); + assert_eq!(bidi(&doc), [true, false, false]); + // Export writes \rtlpar once, and not the \rtldir. + let out = export(&doc); + assert_eq!(out.matches("\\rtlpar").count(), 1, "{out}"); + assert!(!out.contains("rtldir"), "{out}"); + assert_eq!(bidi(&import(out.as_bytes()).unwrap()), [true, false, false]); +} + #[test] fn stylesheet_names_take_unicode_escapes() { // \u116?=t \u105?=i \u116?=t \u108?=l \u101?=e — how Word writes a non-Latin style name. diff --git a/crates/rtf/src/doc/word.rs b/crates/rtf/src/doc/word.rs index 28360c616..bf163db0c 100644 --- a/crates/rtf/src/doc/word.rs +++ b/crates/rtf/src/doc/word.rs @@ -171,6 +171,10 @@ impl Reader { "qr" => self.st.align = Some(Align::Right), "qj" => self.st.align = Some(Align::Justify), "qd" => self.st.align = Some(Align::Distribute), + // `\rtldir`/`\ltrdir` are not paragraph words (they set a table's or section's + // direction in RTF); some writers used them for paragraphs, so they are read too. + "rtlpar" | "rtldir" => self.st.rtldir = true, + "ltrpar" | "ltrdir" => self.st.rtldir = false, "s" => self.st.style = pv, "outlinelevel" => self.st.outline = Some(pv.clamp(0, 9) as u8), "pagebb" => self.st.pagebb = on, From 376fa26e4c4349e95363abf3d21bc76e8ee4d86e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 22:01:59 +0100 Subject: [PATCH 2/6] rtf: read paragraph direction from stylesheet entries A style's \rtlpar/\ltrpar (and \rtldir/\ltrdir) is stored with the style and found through \sbasedon. A paragraph in the style gets it unless it sets its own direction; \pard drops the direct direction and the style's stays. Co-Authored-By: Claude Sonnet 5.5 --- crates/rtf/src/doc/state.rs | 4 ++-- crates/rtf/src/doc/styles.rs | 6 +++++- crates/rtf/src/doc/tests.rs | 12 ++++++++++++ crates/rtf/src/doc/word.rs | 6 ++++-- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/rtf/src/doc/state.rs b/crates/rtf/src/doc/state.rs index 1f5d8d2e6..c27758d05 100644 --- a/crates/rtf/src/doc/state.rs +++ b/crates/rtf/src/doc/state.rs @@ -149,7 +149,7 @@ pub(super) struct St { // Paragraph properties. pub(super) align: Option, /// `\rtlpar`: the paragraph reads right to left. - pub(super) rtldir: bool, + pub(super) rtldir: Option, pub(super) style: i32, pub(super) intbl: bool, pub(super) ls: Option, @@ -187,7 +187,7 @@ impl Default for St { uc: 1, hidden: false, align: None, - rtldir: false, + rtldir: None, style: 0, intbl: false, ls: None, diff --git a/crates/rtf/src/doc/styles.rs b/crates/rtf/src/doc/styles.rs index dd42f3b87..e8eb9904c 100644 --- a/crates/rtf/src/doc/styles.rs +++ b/crates/rtf/src/doc/styles.rs @@ -30,6 +30,7 @@ impl Reader { out.italic = out.italic.or(d.italic); out.size = out.size.or(d.size); out.font = out.font.or(d.font); + out.rtl = out.rtl.or(d.rtl); cur = d.based_on; } out @@ -87,7 +88,8 @@ impl Reader { pub(super) fn para_props(&self, st: &St) -> Para { let mut p = Para::new(self.para_kind(st)); p.align = st.align; - p.rtl = st.rtldir; + // Direct `\rtlpar`/`\ltrpar` wins over the style's direction. + p.rtl = st.rtldir.or_else(|| self.file_style(st.style).rtl).unwrap_or(false); p.page_break = st.pagebb; p.spacing = spacing(st); p.borders = st.borders.any_visible().then_some(st.borders); @@ -136,6 +138,8 @@ pub(super) struct StyleDef { pub(super) size: Option, /// A `\fonttbl` index, named when it's used (the table may follow). pub(super) font: Option, + /// `\rtlpar` (`Some(true)`) or `\ltrpar`: the paragraph direction the style sets. + pub(super) rtl: Option, } /// Clear from `f` the formatting a paragraph of kind `kind` gets from its style, whose diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 9274104ab..30fc8127c 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -68,6 +68,18 @@ fn rtl_paragraphs_read_and_write_their_direction() { assert_eq!(bidi(&import(out.as_bytes()).unwrap()), [true, false, false]); } +#[test] +fn rtl_paragraph_styles_carry_their_direction() { + // Word often writes the direction in the style: `{\s1\rtlpar RTL;}` then `\pard\s1`. + let rtf = br#"{\rtf1\ansi{\stylesheet{\s0 Normal;}{\s1\rtlpar RTL;}{\s2\sbasedon1 Child;}{\s3\sbasedon1\ltrpar Back;}} +\pard\s1 A\par\pard\s2 B\par\pard\s3 C\par\pard\s1\ltrpar D\par\pard\s0\rtlpar E\par\pard F\par}"#; + let flow = parse(rtf).unwrap(); + let rtl: Vec = flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some(p.rtl) } else { None }).collect(); + // A: style. B: inherited from the base style. C: the style overrides its base. D: direct + // \ltrpar beats the style. E: direct \rtlpar on Normal. F: \pard drops direct formatting. + assert_eq!(rtl, [true, true, false, false, true, false]); +} + #[test] fn stylesheet_names_take_unicode_escapes() { // \u116?=t \u105?=i \u116?=t \u108?=l \u101?=e — how Word writes a non-Latin style name. diff --git a/crates/rtf/src/doc/word.rs b/crates/rtf/src/doc/word.rs index bf163db0c..0a346ef93 100644 --- a/crates/rtf/src/doc/word.rs +++ b/crates/rtf/src/doc/word.rs @@ -173,8 +173,8 @@ impl Reader { "qd" => self.st.align = Some(Align::Distribute), // `\rtldir`/`\ltrdir` are not paragraph words (they set a table's or section's // direction in RTF); some writers used them for paragraphs, so they are read too. - "rtlpar" | "rtldir" => self.st.rtldir = true, - "ltrpar" | "ltrdir" => self.st.rtldir = false, + "rtlpar" | "rtldir" => self.st.rtldir = Some(true), + "ltrpar" | "ltrdir" => self.st.rtldir = Some(false), "s" => self.st.style = pv, "outlinelevel" => self.st.outline = Some(pv.clamp(0, 9) as u8), "pagebb" => self.st.pagebb = on, @@ -421,6 +421,8 @@ impl Reader { "i" => def.italic = Some(on), "fs" => def.size = (value > 0).then(|| (value as f32 / 2.0).clamp(1.0, 1638.0)), "f" => def.font = Some(value), + "rtlpar" | "rtldir" => def.rtl = Some(true), + "ltrpar" | "ltrdir" => def.rtl = Some(false), _ => {} } true From d869560911f5e14914314ebaddd97b8acfa87805 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 22:01:59 +0100 Subject: [PATCH 3/6] rtf: keep direction words in old paragraph formatting \rtlpar, \ltrpar, \rtldir and \ltrdir are paragraph formatting words, so \oldpprops holds them and a tracked direction change keeps the old direction. Co-Authored-By: Claude Sonnet 5.5 --- crates/rtf/src/doc/styles.rs | 4 ++++ crates/rtf/src/doc/tests.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/crates/rtf/src/doc/styles.rs b/crates/rtf/src/doc/styles.rs index e8eb9904c..6047bf3c4 100644 --- a/crates/rtf/src/doc/styles.rs +++ b/crates/rtf/src/doc/styles.rs @@ -208,6 +208,10 @@ pub(super) fn is_format_word(w: &str) -> bool { | "s" | "outlinelevel" | "pagebb" + | "rtlpar" + | "ltrpar" + | "rtldir" + | "ltrdir" | "intbl" | "ls" | "ilvl" diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 30fc8127c..8ba6294ae 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -80,6 +80,20 @@ fn rtl_paragraph_styles_carry_their_direction() { assert_eq!(rtl, [true, true, false, false, true, false]); } +#[test] +fn oldpprops_keep_the_old_paragraph_direction() { + // A tracked change of direction: now LTR, before RTL (`\rtlpar` in the old properties). + let rtf = br"{\rtf1\ansi{\*\revtbl {Unknown;}{Alice;}} +\pard\ltrpar\prauth1\prdate-2014675576{\*\oldpprops \pard \rtlpar}A\par}"; + let flow = parse(rtf).unwrap(); + let Some(FBlock::Para(p)) = flow.blocks.first() else { panic!("no paragraph") }; + assert!(!p.rtl); + assert!(p.fmt_change.as_deref().expect("paragraph change").old.rtl, "old direction is RTL"); + let doc = import(rtf).unwrap(); + let c = doc_paras(&doc)[0].props.fmt_change.as_deref().expect("change"); + assert_eq!(c.old.bidi, Some(true)); +} + #[test] fn stylesheet_names_take_unicode_escapes() { // \u116?=t \u105?=i \u116?=t \u108?=l \u101?=e — how Word writes a non-Latin style name. From 537d61a60f84a92675b2391a6e912067c810d0b9 Mon Sep 17 00:00:00 2001 From: vangel76 <271991477+vangel76@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:05:40 +0100 Subject: [PATCH 4/6] rtf: read \ql and \qr after \rtlpar as start and end The model's paragraph alignment is logical (start/end) in right-to-left paragraphs, but the reader mapped \ql/\qr straight to left/right. For a \rtlpar paragraph that mirrored the alignment compared to Word. Word reads a side word that follows \rtlpar as start/end, and one that comes before it as plain left/right (checked black-box by saving RTF variants to DOCX). So \rtlpar\qr is start (no explicit alignment), \rtlpar\ql is end, and \qr\rtlpar is end. The writer already puts the alignment before \rtlpar, so its output keeps its meaning and is unchanged. Co-Authored-By: Claude Sonnet 5.5 --- crates/rtf/src/doc/tests.rs | 43 +++++++++++++++++++++++++++++++++++++ crates/rtf/src/doc/word.rs | 16 ++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 8ba6294ae..8313ad5b8 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -1886,3 +1886,46 @@ fn compound_borders_round_trip() { let back = import(export(&d).as_bytes()).unwrap(); assert_eq!(tops(&back), want); } + +fn para_aligns(flow: &Flow) -> Vec<(bool, Option)> { + flow.blocks.iter().filter_map(|b| if let FBlock::Para(p) = b { Some((p.rtl, p.align)) } else { None }).collect() +} + +#[test] +fn alignment_words_after_rtlpar_mean_start_and_end() { + // Word reads `\ql`/`\qr` that follow `\rtlpar` as start/end (the model's alignment is + // logical), so `\rtlpar\qr` sits at the right edge and `\rtlpar\ql` at the left edge. + // Words written before `\rtlpar` keep their plain meaning (checked in Word by saving to DOCX: + // `\qr\rtlpar` gives jc=right, i.e. end, in the bidi paragraph; `\ql\rtlpar` gives no jc). + let rtf = br#"{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}\pard LTR one.\par \pard\rtlpar\qr A.\par \pard\rtlpar B.\par \pard\ltrpar Back.\par \pard\rtlpar\ql C.\par \pard\rtlpar\qc D.\par \pard\ql\rtlpar E.\par \pard\qr\rtlpar F.\par \pard\rtlpar\ltrpar\qr G.\par}"#; + let got = para_aligns(&parse(rtf).unwrap()); + assert_eq!( + got, + [ + (false, None), + (true, Some(Align::Left)), + (true, None), + (false, None), + (true, Some(Align::Right)), + (true, Some(Align::Center)), + (true, Some(Align::Left)), + (true, Some(Align::Right)), + (false, Some(Align::Right)), + ] + ); +} + +#[test] +fn alignment_survives_an_export_import_round_trip() { + let rtf = br#"{\rtf1\ansi\pard\ql L.\par \pard\qr R.\par \pard\qc C.\par \pard\rtlpar\qr start.\par \pard\rtlpar\ql end.\par \pard\rtlpar\qc mid.\par \pard\rtlpar plain.\par}"#; + let first = para_aligns(&parse(rtf).unwrap()); + let doc = import(rtf).unwrap(); + let out = export(&doc); + let second = para_aligns(&parse(out.as_bytes()).unwrap()); + let norm = |v: Vec<(bool, Option)>| v.into_iter().map(|(r, a)| (r, a.filter(|a| *a != Align::Left))).collect::>(); + assert_eq!(norm(first), norm(second.clone()), "{out}"); + // `\rtlpar\qr` is start (no explicit alignment in the model) and `\rtlpar\ql` is end. + assert_eq!(norm(second)[3..5], [(true, None), (true, Some(Align::Right))], "{out}"); + // The export writes the alignment before the direction, which Word reads as logical start/end. + assert!(out.contains("\\qr\\rtlpar"), "{out}"); +} diff --git a/crates/rtf/src/doc/word.rs b/crates/rtf/src/doc/word.rs index 0a346ef93..2eb86692f 100644 --- a/crates/rtf/src/doc/word.rs +++ b/crates/rtf/src/doc/word.rs @@ -3,6 +3,18 @@ use super::styles::{StyleDef, is_char_format_word, is_format_word}; use super::{Align, Fmt, Reader, VAlign, model}; impl Reader { + /// The logical alignment for a side word (`Left` for `\ql`, `Right` for `\qr`). The model's + /// alignment is logical (start/end), and Word reads a side word that comes after `\rtlpar` as + /// start/end: the sides swap in a paragraph already marked right to left. Before `\rtlpar` + /// the side keeps its plain meaning, which is why the writer puts the alignment first. + fn side_align(&self, side: Align) -> Align { + match (self.st.rtldir, side) { + (Some(true), Align::Left) => Align::Right, + (Some(true), Align::Right) => Align::Left, + _ => side, + } + } + pub(super) fn word(&mut self, w: &str, p: Option, starred: bool, first_in_group: bool) { // Every control word ends a multibyte run: the code page it is read in may change here // (`\f`, `\fcharset`, `\cpg`, `\ansicpg`, `\pard`), and `\par` must see the text before it. @@ -166,9 +178,9 @@ impl Reader { "cf" => self.st.fmt.color = self.colors.get(pv.max(0) as usize).copied().flatten(), "highlight" | "chcbpat" | "cb" => self.st.fmt.background = self.colors.get(pv.max(0) as usize).copied().flatten(), "v" => self.st.hidden = on, - "ql" => self.st.align = Some(Align::Left), + "ql" => self.st.align = Some(self.side_align(Align::Left)), "qc" => self.st.align = Some(Align::Center), - "qr" => self.st.align = Some(Align::Right), + "qr" => self.st.align = Some(self.side_align(Align::Right)), "qj" => self.st.align = Some(Align::Justify), "qd" => self.st.align = Some(Align::Distribute), // `\rtldir`/`\ltrdir` are not paragraph words (they set a table's or section's From 6767c7136297160cfca1460943b87f46a236cd8d Mon Sep 17 00:00:00 2001 From: vangel76 <271991477+vangel76@users.noreply.github.com> Date: Sun, 15 Nov 2026 02:26:31 +0100 Subject: [PATCH 5/6] Re-run CI: the Ubuntu gate timed out Co-Authored-By: Claude Sonnet 5.5 From 51a35cecb7b3d0416f0195032bbcc7f960731688 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 13:27:34 +0100 Subject: [PATCH 6/6] flow: keep the old paragraph direction in the tracked formatting-change snapshot Co-Authored-By: Claude Sonnet 5.5 --- crates/flow/src/from_doc.rs | 5 ++++- crates/rtf/src/doc/tests.rs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/flow/src/from_doc.rs b/crates/flow/src/from_doc.rs index ea7f6fc92..717283f57 100644 --- a/crates/flow/src/from_doc.rs +++ b/crates/flow/src/from_doc.rs @@ -661,7 +661,10 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec { { (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() }; + // Direction resolved like the current paragraph's: the old properties' own `w:bidi`, else + // the style's. + let rtl = doc.styles.resolve_para(&c.old).bidi; + let old = Para { kind, list, align: c.old.align, rtl, 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/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 8313ad5b8..bf98d190a 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -94,6 +94,30 @@ fn oldpprops_keep_the_old_paragraph_direction() { assert_eq!(c.old.bidi, Some(true)); } +#[test] +fn tracked_rtl_to_ltr_change_from_a_document_keeps_the_old_direction() { + use craft_wordmodel::props::{CharProps, ParaProps, PropChange}; + use craft_wordmodel::{Revision, RevisionKind}; + // Document origin (not RTF): now LTR, before RTL. + let mut p = craft_wordmodel::Paragraph::with_text("A", CharProps::default()); + p.props.bidi = Some(false); + p.props.fmt_change = PropChange::boxed(0, ParaProps { bidi: Some(true), ..Default::default() }); + let doc = doc_with(vec![p], vec![Revision::new(RevisionKind::Format, "Bob", "2025-01-02T08:05:00Z")]); + let flow = model::from_doc_tracked(&doc); + let Some(FBlock::Para(fp)) = flow.blocks.first() else { panic!("no paragraph") }; + assert!(!fp.rtl); + assert!(fp.fmt_change.as_deref().expect("change").old.rtl, "snapshot keeps RTL"); + let rtf = export(&doc); + assert!(rtf.contains("{\\*\\oldpprops"), "{rtf}"); + let back = import(rtf.as_bytes()).unwrap(); + let p = doc_paras(&back)[0]; + assert_ne!(p.props.bidi, Some(true), "current direction is LTR"); + let change = p.props.fmt_change.as_deref().expect("change read back"); + assert_eq!(change.old.bidi, Some(true)); + // Reject restores RTL. + assert_eq!(change.old.formatting().bidi, Some(true)); +} + #[test] fn stylesheet_names_take_unicode_escapes() { // \u116?=t \u105?=i \u116?=t \u108?=l \u101?=e — how Word writes a non-Latin style name.