From a39cfd79a123073e19e44b653c2fc87cafbb3eda Mon Sep 17 00:00:00 2001 From: Ossama Nasser Date: Fri, 7 Feb 2025 05:39:54 +0300 Subject: [PATCH] Bidi Support Added a simple bidi support by new paragraph property called bidi Works as a boolean flag when available MS Word treats the paragraph as RTL, otherwise it's LTR --- .../src/documents/elements/paragraph_property.rs | 15 ++++++++++++++- docx-core/src/xml_builder/elements.rs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index c07d28cc3..0d5391998 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -25,6 +25,8 @@ pub struct ParagraphProperty { pub keep_next: Option, #[serde(skip_serializing_if = "Option::is_none")] pub keep_lines: Option, + #[serde(skip_serializing_if="Option::is_none")] + pub bidi:Option, #[serde(skip_serializing_if = "Option::is_none")] pub page_break_before: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -131,7 +133,10 @@ impl ParagraphProperty { self.page_break_before = Some(v); self } - + pub fn bidi(mut self,v:bool)->Self{ + self.bidi=Some(v); + self + } pub fn widow_control(mut self, v: bool) -> Self { self.widow_control = Some(v); self @@ -230,6 +235,7 @@ impl BuildXML for ParagraphProperty { .apply_if(self.keep_next, |b| b.keep_next())? .apply_if(self.keep_lines, |b| b.keep_lines())? .apply_if(self.page_break_before, |b| b.page_break_before())? + .apply_if(self.bidi,|b| b.bidi())? .apply_opt(self.widow_control, |flag, b| { b.widow_control(if flag { "1" } else { "0" }) })? @@ -258,6 +264,13 @@ mod tests { assert_eq!(str::from_utf8(&b).unwrap(), r#""#); } + #[test] + fn test_bidi(){ + let c=ParagraphProperty::new().bidi(true); + let b=c.build(); + println!("-----Test bidi: {}", str::from_utf8(&b).unwrap()); + assert_eq!(str::from_utf8(&b).unwrap(),r#""#); + } #[test] fn test_alignment() { let c = ParagraphProperty::new(); diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 24b34f0e5..ecd6649bf 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -470,7 +470,7 @@ impl XMLBuilder { closed!(keep_lines, "w:keepLines"); closed!(page_break_before, "w:pageBreakBefore"); closed!(widow_control, "w:widowControl", "w:val"); - + closed!(bidi,"w:bidi"); /*