Skip to content

Commit

Permalink
Bidi Support
Browse files Browse the repository at this point in the history
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
  • Loading branch information
OssNass committed Feb 7, 2025
1 parent 5741806 commit a39cfd7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion docx-core/src/documents/elements/paragraph_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct ParagraphProperty {
pub keep_next: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keep_lines: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
pub bidi:Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub page_break_before: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" })
})?
Expand Down Expand Up @@ -258,6 +264,13 @@ mod tests {
assert_eq!(str::from_utf8(&b).unwrap(), r#"<w:pPr><w:rPr /></w:pPr>"#);
}

#[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#"<w:pPr><w:rPr /><w:bidi /></w:pPr>"#);
}
#[test]
fn test_alignment() {
let c = ParagraphProperty::new();
Expand Down
2 changes: 1 addition & 1 deletion docx-core/src/xml_builder/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ impl<W: Write> XMLBuilder<W> {
closed!(keep_lines, "w:keepLines");
closed!(page_break_before, "w:pageBreakBefore");
closed!(widow_control, "w:widowControl", "w:val");

closed!(bidi,"w:bidi");
/*
<w:lvlOverride w:ilvl="0">
<w:startOverride w:val="1"/>
Expand Down

0 comments on commit a39cfd7

Please sign in to comment.