From a528fa7430314b3a0905a5cdc86be39631bf86f8 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 11:23:04 +0100 Subject: [PATCH] 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,