mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
a528fa7430
commit
376fa26e4c
@@ -149,7 +149,7 @@ pub(super) struct St {
|
||||
// Paragraph properties.
|
||||
pub(super) align: Option<Align>,
|
||||
/// `\rtlpar`: the paragraph reads right to left.
|
||||
pub(super) rtldir: bool,
|
||||
pub(super) rtldir: Option<bool>,
|
||||
pub(super) style: i32,
|
||||
pub(super) intbl: bool,
|
||||
pub(super) ls: Option<i32>,
|
||||
@@ -187,7 +187,7 @@ impl Default for St {
|
||||
uc: 1,
|
||||
hidden: false,
|
||||
align: None,
|
||||
rtldir: false,
|
||||
rtldir: None,
|
||||
style: 0,
|
||||
intbl: false,
|
||||
ls: None,
|
||||
|
||||
@@ -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<f32>,
|
||||
/// A `\fonttbl` index, named when it's used (the table may follow).
|
||||
pub(super) font: Option<i32>,
|
||||
/// `\rtlpar` (`Some(true)`) or `\ltrpar`: the paragraph direction the style sets.
|
||||
pub(super) rtl: Option<bool>,
|
||||
}
|
||||
|
||||
/// Clear from `f` the formatting a paragraph of kind `kind` gets from its style, whose
|
||||
|
||||
@@ -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<bool> = 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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user