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