From 160fac91b734b14db725619f262af59f9d4545d0 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Fri, 19 May 2023 18:32:33 +0900 Subject: [PATCH] feat: Support sym (#625) --- docx-core/src/documents/elements/mod.rs | 2 ++ docx-core/src/documents/elements/run.rs | 13 +++++++++ docx-core/src/documents/elements/sym.rs | 39 +++++++++++++++++++++++++ docx-core/src/reader/run.rs | 8 +++++ docx-core/src/reader/xml_element.rs | 2 ++ docx-core/src/xml_builder/elements.rs | 1 + 6 files changed, 65 insertions(+) create mode 100644 docx-core/src/documents/elements/sym.rs diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index bc0a6af..5f89397 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -82,6 +82,7 @@ mod strike; mod structured_data_tag; mod structured_data_tag_property; mod style; +mod sym; mod sz; mod sz_cs; mod tab; @@ -201,6 +202,7 @@ pub use strike::*; pub use structured_data_tag::*; pub use structured_data_tag_property::*; pub use style::*; +pub use sym::*; pub use sz::*; pub use sz_cs::*; pub use tab::*; diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index db15a48..9827c1b 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -26,6 +26,7 @@ impl Default for Run { #[derive(Debug, Clone, PartialEq)] pub enum RunChild { Text(Text), + Sym(Sym), DeleteText(DeleteText), Tab(Tab), Break(Break), @@ -52,6 +53,12 @@ impl Serialize for RunChild { t.serialize_field("data", s)?; t.end() } + RunChild::Sym(ref s) => { + let mut t = serializer.serialize_struct("Sym", 2)?; + t.serialize_field("type", "sym")?; + t.serialize_field("data", s)?; + t.end() + } RunChild::DeleteText(ref s) => { let mut t = serializer.serialize_struct("DeleteText", 2)?; t.serialize_field("type", "deleteText")?; @@ -202,6 +209,11 @@ impl Run { self } + pub fn add_sym(mut self, sym: Sym) -> Run { + self.children.push(RunChild::Sym(sym)); + self + } + pub fn style(mut self, style_id: &str) -> Self { self.run_property = self.run_property.style(style_id); self @@ -280,6 +292,7 @@ impl BuildXML for Run { for c in &self.children { match c { RunChild::Text(t) => b = b.add_child(t), + RunChild::Sym(t) => b = b.add_child(t), RunChild::DeleteText(t) => b = b.add_child(t), RunChild::Tab(t) => b = b.add_child(t), RunChild::Break(t) => b = b.add_child(t), diff --git a/docx-core/src/documents/elements/sym.rs b/docx-core/src/documents/elements/sym.rs new file mode 100644 index 0000000..b6de208 --- /dev/null +++ b/docx-core/src/documents/elements/sym.rs @@ -0,0 +1,39 @@ +use serde::ser::{Serialize, SerializeStruct, Serializer}; +use serde::Deserialize; + +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone, Deserialize, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct Sym { + pub font: String, + pub char: String, +} + +impl Sym { + pub fn new(font: impl Into, char: impl Into) -> Self { + Self { + font: font.into(), + char: char.into(), + } + } +} +impl BuildXML for Sym { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.sym(&self.font, &self.char).build() + } +} + +impl Serialize for Sym { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut t = serializer.serialize_struct("Sym", 1)?; + t.serialize_field("font", &self.font)?; + t.serialize_field("char", &self.char)?; + t.end() + } +} diff --git a/docx-core/src/reader/run.rs b/docx-core/src/reader/run.rs index ec4a18b..e4f5213 100644 --- a/docx-core/src/reader/run.rs +++ b/docx-core/src/reader/run.rs @@ -71,6 +71,14 @@ impl ElementReader for Run { XMLElement::Tab => { run = run.add_tab(); } + XMLElement::Sym => { + if let Some(font) = read(&attributes, "font") { + if let Some(char) = read(&attributes, "char") { + let sym = Sym::new(font, char); + run = run.add_sym(sym); + } + } + } XMLElement::RunProperty => { let p = RunProperty::read(r, &attributes)?; run = run.set_property(p); diff --git a/docx-core/src/reader/xml_element.rs b/docx-core/src/reader/xml_element.rs index 3c97e19..4471944 100644 --- a/docx-core/src/reader/xml_element.rs +++ b/docx-core/src/reader/xml_element.rs @@ -35,6 +35,7 @@ pub enum XMLElement { BoldCs, Break, Tab, + Sym, ParagraphStyle, ParagraphPropertyChange, RunPropertyChange, @@ -370,6 +371,7 @@ impl FromStr for XMLElement { "marRight" => Ok(XMLElement::MarginRight), "marTop" => Ok(XMLElement::MarginTop), "marBottom" => Ok(XMLElement::MarginBottom), + "sym" => Ok(XMLElement::Sym), "webSettings" => Ok(XMLElement::WebSettings), "keepNext" => Ok(XMLElement::KeepNext), "keepLines" => Ok(XMLElement::KeepLines), diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 3ae410b..8fe0239 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -342,6 +342,7 @@ impl XMLBuilder { closed!(tab_with_pos, "w:tab", "w:val", "w:pos"); closed!(br, "w:br", "w:type"); + closed!(sym, "w:sym", "w:font", "w:char"); closed!(zoom, "w:zoom", "w:percent"); closed_with_usize!(default_tab_stop, "w:defaultTabStop");