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 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-11-14 11:23:04 +01:00
co-authored by Claude Sonnet 5.5
parent f0f4758c01
commit a528fa7430
8 changed files with 50 additions and 0 deletions
+3
View File
@@ -493,6 +493,8 @@ fn flow_paras_in(doc: &Document, p: &Paragraph, opts: FlowOpts) -> Vec<Para> {
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<Para> {
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(),
+3
View File
@@ -159,6 +159,9 @@ fn para_props(p: &Para, list_num: Option<u32>) -> 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;
+2
View File
@@ -676,6 +676,8 @@ pub struct Para {
pub kind: Kind,
pub list: Option<ListInfo>,
pub align: Option<Align>,
/// 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,
+4
View File
@@ -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");
}
+3
View File
@@ -148,6 +148,8 @@ pub(super) struct St {
pub(super) hidden: bool,
// Paragraph properties.
pub(super) align: Option<Align>,
/// `\rtlpar`: the paragraph reads right to left.
pub(super) rtldir: bool,
pub(super) style: i32,
pub(super) intbl: bool,
pub(super) ls: Option<i32>,
@@ -185,6 +187,7 @@ impl Default for St {
uc: 1,
hidden: false,
align: None,
rtldir: false,
style: 0,
intbl: false,
ls: None,
+1
View File
@@ -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);
+30
View File
@@ -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::<Vec<_>>()
};
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.
+4
View File
@@ -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,