Run level shading (#727)

* Initial Run-level Shading support: add Shading to RunChild

* Initial Run-level Shading support: add Shading to RunProperty
main
git-noise 2024-06-29 06:23:20 -04:00 committed by GitHub
parent 2c8638dcb0
commit e04dc4a757
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 0 deletions

View File

@ -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"}}"#
);
}
}

View File

@ -48,6 +48,8 @@ pub struct RunProperty {
pub ins: Option<Insert>,
#[serde(skip_serializing_if = "Option::is_none")]
pub strike: Option<Strike>,
#[serde(skip_serializing_if = "Option::is_none")]
pub shading: Option<Shading>,
}
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#"<w:rPr><w:spacing w:val="20" /></w:rPr>"#
);
}
#[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#"<w:rPr><w:shd w:val="clear" w:color="auto" w:fill="FFFFFF" /></w:rPr>"#
);
}
}