diff --git a/docx-core/examples/size.rs b/docx-core/examples/size.rs new file mode 100644 index 0000000..6a36a0b --- /dev/null +++ b/docx-core/examples/size.rs @@ -0,0 +1,15 @@ +use docx_core::*; + +pub fn main() { + let path = std::path::Path::new("./output/size.docx"); + let file = std::fs::File::create(&path).unwrap(); + Docx::new() + .add_paragraph(Paragraph::new().add_run(Run::new("Hello")).size(60)) + .add_paragraph( + Paragraph::new() + .add_run(Run::new(" Wor").size(50)) + .add_run(Run::new("ld")), + ) + .build() + .pack(file); +} diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index 6e153e7..70fab11 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -13,6 +13,7 @@ mod run_property; mod run_property_default; mod style; mod sz; +mod sz_cs; mod text; pub use based_on::*; @@ -30,4 +31,5 @@ pub use run_property::*; pub use run_property_default::*; pub use style::*; pub use sz::*; +pub use sz_cs::*; pub use text::*; diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 895c4cc..771cba1 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -1,4 +1,4 @@ -use super::{ParagraphProperty, ParagraphStyle, Run}; +use super::{ParagraphProperty, Run}; use crate::documents::BuildXML; use crate::types::*; use crate::xml_builder::*; @@ -7,16 +7,13 @@ use crate::xml_builder::*; pub struct Paragraph { runs: Vec, property: ParagraphProperty, - style: ParagraphStyle, } impl Default for Paragraph { fn default() -> Self { - let s: Option<&str> = None; Self { runs: Vec::new(), property: ParagraphProperty::new(), - style: ParagraphStyle::new(s), } } } @@ -35,6 +32,16 @@ impl Paragraph { self.property = self.property.align(alignment_type); self } + + pub fn size(mut self, size: usize) -> Paragraph { + self.runs = self.runs.into_iter().map(|r| r.size(size)).collect(); + self + } + + pub fn style(mut self, style_id: &str) -> Paragraph { + self.property = self.property.style(style_id); + self + } } impl BuildXML for Paragraph { @@ -42,7 +49,6 @@ impl BuildXML for Paragraph { XMLBuilder::new() .open_paragraph() .add_child(&self.property) - .add_child(&self.style) .add_children(&self.runs) .close() .build() @@ -65,4 +71,13 @@ mod tests { r#"Hello"# ); } + + #[test] + fn test_paragraph_size() { + let b = Paragraph::new().add_run(Run::new("Hello")).size(60).build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#"Hello"# + ); + } } diff --git a/docx-core/src/documents/elements/paragraph_property.rs b/docx-core/src/documents/elements/paragraph_property.rs index cc716c7..e7210b1 100644 --- a/docx-core/src/documents/elements/paragraph_property.rs +++ b/docx-core/src/documents/elements/paragraph_property.rs @@ -1,4 +1,4 @@ -use super::{Justification, RunProperty}; +use super::{Justification, ParagraphStyle, RunProperty, Sz, SzCs}; use crate::documents::BuildXML; use crate::types::AlignmentType; use crate::xml_builder::*; @@ -7,13 +7,16 @@ use crate::xml_builder::*; pub struct ParagraphProperty { alignment: Option, run_property: RunProperty, + style: ParagraphStyle, } impl Default for ParagraphProperty { fn default() -> Self { + let s: Option<&str> = None; ParagraphProperty { alignment: None, run_property: RunProperty::new(), + style: ParagraphStyle::new(s), } } } @@ -32,15 +35,20 @@ impl ParagraphProperty { self.alignment = Some(Justification::new(alignment_type.to_string())); self } + + pub fn style(mut self, style_id: &str) -> ParagraphProperty { + self.style = ParagraphStyle::new(Some(style_id)); + self + } } impl BuildXML for ParagraphProperty { fn build(&self) -> Vec { - let b = XMLBuilder::new(); - let p = b + XMLBuilder::new() .open_paragraph_property() - .add_optional_child(&self.alignment); - p.close().build() + .add_optional_child(&self.alignment) + .close() + .build() } } @@ -53,9 +61,19 @@ mod tests { use std::str; #[test] - fn test_build() { + fn test_default() { let c = ParagraphProperty::new(); let b = c.build(); assert_eq!(str::from_utf8(&b).unwrap(), r#""#); } + + #[test] + fn test_alignment() { + let c = ParagraphProperty::new(); + let b = c.align(AlignmentType::Right).build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } } diff --git a/docx-core/src/documents/elements/run.rs b/docx-core/src/documents/elements/run.rs index 4ff014a..6c9757f 100644 --- a/docx-core/src/documents/elements/run.rs +++ b/docx-core/src/documents/elements/run.rs @@ -15,6 +15,11 @@ impl Run { ..Default::default() } } + + pub fn size(mut self, size: usize) -> Run { + self.run_property = self.run_property.size(size); + self + } } impl Default for Run { diff --git a/docx-core/src/documents/elements/run_property.rs b/docx-core/src/documents/elements/run_property.rs index 6fce08d..e73c90a 100644 --- a/docx-core/src/documents/elements/run_property.rs +++ b/docx-core/src/documents/elements/run_property.rs @@ -1,10 +1,11 @@ -use super::{Color, Sz}; +use super::{Color, Sz, SzCs}; use crate::documents::BuildXML; use crate::xml_builder::*; #[derive(Debug)] pub struct RunProperty { sz: Option, + sz_cs: Option, color: Option, } @@ -13,12 +14,13 @@ impl RunProperty { Default::default() } - pub fn add_sz(mut self, sz: usize) -> RunProperty { - self.sz = Some(Sz::new(sz)); + pub fn size(mut self, size: usize) -> RunProperty { + self.sz = Some(Sz::new(size)); + self.sz_cs = Some(SzCs::new(size)); self } - pub fn add_color(mut self, color: &str) -> RunProperty { + pub fn color(mut self, color: &str) -> RunProperty { self.color = Some(Color::new(color)); self } @@ -27,8 +29,9 @@ impl RunProperty { impl Default for RunProperty { fn default() -> Self { Self { - sz: None, color: None, + sz: None, + sz_cs: None, } } } @@ -38,6 +41,7 @@ impl BuildXML for RunProperty { let b = XMLBuilder::new(); b.open_run_property() .add_optional_child(&self.sz) + .add_optional_child(&self.sz_cs) .add_optional_child(&self.color) .close() .build() @@ -54,11 +58,11 @@ mod tests { #[test] fn test_build() { - let c = RunProperty::new().add_sz(10).add_color("FFFFFF"); + let c = RunProperty::new().size(10).color("FFFFFF"); let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/elements/sz_cs.rs b/docx-core/src/documents/elements/sz_cs.rs new file mode 100644 index 0000000..15fd6fb --- /dev/null +++ b/docx-core/src/documents/elements/sz_cs.rs @@ -0,0 +1,35 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug)] +pub struct SzCs { + val: usize, +} + +impl SzCs { + pub fn new(val: usize) -> SzCs { + SzCs { val } + } +} + +impl BuildXML for SzCs { + fn build(&self) -> Vec { + XMLBuilder::new().sz_cs(self.val).build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_sz_cs() { + let c = SzCs::new(20); + let b = c.build(); + assert_eq!(str::from_utf8(&b).unwrap(), r#""#); + } +} diff --git a/docx-core/src/xml_builder/elements.rs b/docx-core/src/xml_builder/elements.rs index 90c7586..14e3493 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -8,7 +8,6 @@ impl XMLBuilder { // i.e. only_str_val_el!(based_on, "w:basedOn"); // i.e. - // i.e. pub(crate) fn text(mut self, text: &str, preserve_space: bool) -> Self { let space = if preserve_space { "preserve" @@ -38,12 +37,9 @@ impl XMLBuilder { // i.e. only_str_val_el!(paragraph_style, "w:pStyle"); // i.e. - pub(crate) fn sz(mut self, val: usize) -> Self { - self.writer - .write(XmlEvent::start_element("w:sz").attr("w:val", &format!("{}", val))) - .expect("should write to buf"); - self.close() - } + only_usize_val_el!(sz, "w:sz"); + // i.e. + only_usize_val_el!(sz_cs, "w:szCs"); // Build w:style element // i.e. pub(crate) fn open_style(mut self, style_type: StyleType, id: &str) -> Self { diff --git a/docx-core/src/xml_builder/macros.rs b/docx-core/src/xml_builder/macros.rs index 61b91a3..ad3981d 100644 --- a/docx-core/src/xml_builder/macros.rs +++ b/docx-core/src/xml_builder/macros.rs @@ -142,15 +142,13 @@ macro_rules! only_str_val_el { }; } -/* macro_rules! only_usize_val_el { ($name: ident, $el_name: expr) => { pub(crate) fn $name(mut self, val: usize) -> Self { self.writer - .write(XmlEvent::start_element($el_name).attr("w:val", val)) + .write(XmlEvent::start_element($el_name).attr("w:val", &format!("{}", val))) .expect("should write to buf"); self.close() } }; } -*/