From e04dc4a75729c4a76b4725f36fc53357a01177d0 Mon Sep 17 00:00:00 2001 From: git-noise <63017366+git-noise@users.noreply.github.com> Date: Sat, 29 Jun 2024 06:23:20 -0400 Subject: [PATCH] Run level shading (#727) * Initial Run-level Shading support: add Shading to RunChild * Initial Run-level Shading support: add Shading to RunProperty --- docx-core/src/documents/elements/run.rs | 22 ++++++++++++++++++ .../src/documents/elements/run_property.rs | 23 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index f87c664..c800f7c 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -40,6 +40,7 @@ pub enum RunChild { // For reader InstrTextString(String), FootnoteReference(FootnoteReference), + Shading(Shading), } impl Serialize for RunChild { @@ -131,6 +132,12 @@ impl Serialize for RunChild { t.serialize_field("data", f)?; t.end() } + RunChild::Shading(ref f) => { + let mut t = serializer.serialize_struct("Shading", 2)?; + t.serialize_field("type", "shading")?; + t.serialize_field("data", f)?; + t.end() + } } } } @@ -297,6 +304,11 @@ impl Run { .push(RunChild::FootnoteReference(footnote.into())); self } + + pub fn shading(mut self, shading: Shading) -> Run { + self.run_property = self.run_property.shading(shading); + self + } } impl BuildXML for Run { @@ -321,6 +333,7 @@ impl BuildXML for Run { RunChild::DeleteInstrText(c) => b = b.add_child(c), RunChild::InstrTextString(_) => unreachable!(), RunChild::FootnoteReference(c) => b = b.add_child(c), + RunChild::Shading(s) => b = b.add_child(s), } } b.close().build() @@ -400,4 +413,13 @@ mod tests { r#"{"type":"footnoteReference","data":{"id":1}}"# ); } + + #[test] + fn test_run_shading() { + let c = RunChild::Shading(Shading::new()); + assert_eq!( + serde_json::to_string(&c).unwrap(), + r#"{"type":"shading","data":{"shdType":"clear","color":"auto","fill":"FFFFFF"}}"# + ); + } } diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index cccc043..c21d3dc 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -48,6 +48,8 @@ pub struct RunProperty { pub ins: Option, #[serde(skip_serializing_if = "Option::is_none")] pub strike: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub shading: Option, } impl RunProperty { @@ -154,6 +156,11 @@ impl RunProperty { self.ins = Some(i); self } + + pub fn shading(mut self, s: Shading) -> Self { + self.shading = Some(s); + self + } } impl BuildXML for RunProperty { @@ -180,6 +187,7 @@ impl BuildXML for RunProperty { .add_optional_child(&self.vert_align) .add_optional_child(&self.character_spacing) .add_optional_child(&self.style) + .add_optional_child(&self.shading) .close() .build() } @@ -271,4 +279,19 @@ mod tests { r#""# ); } + + #[test] + fn test_character_shading() { + let c = RunProperty::new().shading( + Shading::new() + .shd_type(ShdType::Clear) + .fill("FFFFFF") + .color("auto"), + ); + let b = c.build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } }