diff --git a/docx-core/src/documents/document.rs b/docx-core/src/documents/document.rs index a79d368..bc5bacb 100644 --- a/docx-core/src/documents/document.rs +++ b/docx-core/src/documents/document.rs @@ -1,10 +1,11 @@ -use super::{Paragraph, Table}; +use super::{Paragraph, SectionProperty, Table}; use crate::documents::BuildXML; use crate::xml_builder::*; #[derive(Debug)] pub struct Document { pub(crate) children: Vec, + pub section_property: SectionProperty, } #[derive(Debug, Clone)] @@ -33,6 +34,7 @@ impl Default for Document { fn default() -> Self { Self { children: Vec::new(), + section_property: SectionProperty::new(), } } } @@ -49,7 +51,7 @@ impl BuildXML for Document { DocumentChild::Table(t) => b = b.add_child(t), } } - b.close().close().build() + b.add_child(&self.section_property).close().close().build() } } @@ -71,7 +73,9 @@ mod tests { str::from_utf8(&b).unwrap(), r#" - Hello + Hello + + "# ); } diff --git a/docx-core/src/documents/elements/mod.rs b/docx-core/src/documents/elements/mod.rs index d1b534e..b747e47 100644 --- a/docx-core/src/documents/elements/mod.rs +++ b/docx-core/src/documents/elements/mod.rs @@ -30,6 +30,8 @@ mod number_format; mod numbering; mod numbering_id; mod numbering_property; +mod page_margin; +mod page_size; mod paragraph; mod paragraph_property; mod paragraph_style; @@ -37,6 +39,7 @@ mod q_format; mod run; mod run_property; mod run_property_default; +mod section_property; mod start; mod style; mod sz; @@ -92,6 +95,8 @@ pub use number_format::*; pub use numbering::*; pub use numbering_id::*; pub use numbering_property::*; +pub use page_margin::*; +pub use page_size::*; pub use paragraph::*; pub use paragraph_property::*; pub use paragraph_style::*; @@ -99,6 +104,7 @@ pub use q_format::*; pub use run::*; pub use run_property::*; pub use run_property_default::*; +pub use section_property::*; pub use start::*; pub use style::*; pub use sz::*; diff --git a/docx-core/src/documents/elements/page_margin.rs b/docx-core/src/documents/elements/page_margin.rs new file mode 100644 index 0000000..8bb2ecb --- /dev/null +++ b/docx-core/src/documents/elements/page_margin.rs @@ -0,0 +1,93 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct PageMargin { + top: usize, + left: usize, + bottom: usize, + right: usize, + header: usize, + footer: usize, + gutter: usize, +} + +// These values were based on microsoft office word2019 windows edition. +// +impl Default for PageMargin { + fn default() -> PageMargin { + PageMargin { + top: 1985, + left: 1701, + bottom: 1701, + right: 1701, + header: 851, + footer: 992, + gutter: 0, + } + } +} + +impl PageMargin { + pub fn new() -> PageMargin { + Default::default() + } + + pub fn top(self, v: usize) -> PageMargin { + PageMargin { top: v, ..self } + } + + pub fn left(self, v: usize) -> PageMargin { + PageMargin { left: v, ..self } + } + + pub fn bottom(self, v: usize) -> PageMargin { + PageMargin { bottom: v, ..self } + } + + pub fn right(self, v: usize) -> PageMargin { + PageMargin { right: v, ..self } + } + + pub fn header(self, v: usize) -> PageMargin { + PageMargin { header: v, ..self } + } + + pub fn gutter(self, v: usize) -> PageMargin { + PageMargin { gutter: v, ..self } + } +} + +impl BuildXML for PageMargin { + fn build(&self) -> Vec { + XMLBuilder::new() + .page_margin( + &format!("{}", self.top), + &format!("{}", self.right), + &format!("{}", self.bottom), + &format!("{}", self.left), + &format!("{}", self.header), + &format!("{}", self.footer), + &format!("{}", self.gutter), + ) + .build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_page_margin_default() { + let b = PageMargin::new().build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/documents/elements/page_size.rs b/docx-core/src/documents/elements/page_size.rs new file mode 100644 index 0000000..f3ccefb --- /dev/null +++ b/docx-core/src/documents/elements/page_size.rs @@ -0,0 +1,52 @@ +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct PageSize { + w: usize, + h: usize, +} + +// These values were based on microsoft office word2019 windows edition. +// +impl Default for PageSize { + fn default() -> PageSize { + PageSize { w: 11906, h: 16838 } + } +} + +impl PageSize { + pub fn new() -> PageSize { + Default::default() + } + + pub fn size(self, w: usize, h: usize) -> PageSize { + PageSize { w, h } + } +} + +impl BuildXML for PageSize { + fn build(&self) -> Vec { + XMLBuilder::new() + .page_size(&format!("{}", self.w), &format!("{}", self.h)) + .build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_page_size_default() { + let b = PageSize::new().build(); + assert_eq!( + str::from_utf8(&b).unwrap(), + r#""# + ); + } +} diff --git a/docx-core/src/documents/elements/section_property.rs b/docx-core/src/documents/elements/section_property.rs new file mode 100644 index 0000000..aee38b2 --- /dev/null +++ b/docx-core/src/documents/elements/section_property.rs @@ -0,0 +1,62 @@ +use super::*; +use crate::documents::BuildXML; +use crate::xml_builder::*; + +#[derive(Debug, Clone)] +pub struct SectionProperty { + page_size: PageSize, + page_margin: PageMargin, + columns: usize, + document_grid: usize, +} + +impl SectionProperty { + pub fn new() -> SectionProperty { + Default::default() + } +} + +impl Default for SectionProperty { + fn default() -> Self { + Self { + page_size: PageSize::new(), + page_margin: PageMargin::new(), + columns: 425, + document_grid: 360, + } + } +} + +impl BuildXML for SectionProperty { + fn build(&self) -> Vec { + let b = XMLBuilder::new(); + b.open_section_property() + .add_child(&self.page_size) + .add_child(&self.page_margin) + .columns(&format!("{}", &self.columns)) + .document_grid("lines", &format!("{}", &self.document_grid)) + .close() + .build() + } +} + +#[cfg(test)] +mod tests { + + use super::*; + #[cfg(test)] + use pretty_assertions::assert_eq; + use std::str; + + #[test] + fn test_section_property_default() { + let c = SectionProperty::new(); + 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 632809b..2bdbf4f 100644 --- a/docx-core/src/xml_builder/elements.rs +++ b/docx-core/src/xml_builder/elements.rs @@ -152,20 +152,22 @@ impl XMLBuilder { only_str_val_el!(family, "w:family"); only_str_val_el!(charset, "w:charset"); - only_usize_val_el!(section_property, "w:sectPr"); + opened_el!(open_section_property, "w:sectPr"); only_str_val_el!(type_tag, "w:type"); closed_el!(page_size, "w:pgSz", "w:w", "w:h"); closed_el!( page_margin, "w:pgMar", - "w:left", - "w:right", - "w:header", "w:top", - "w:footer", + "w:right", "w:bottom", + "w:left", + "w:header", + "w:footer", "w:gutter" ); + closed_el!(columns, "w:cols", "w:space"); + closed_el!(document_grid, "w:docGrid", "w:type", "w:linePitch"); opened_el!(open_insert, "w:ins", "w:id", "w:author", "w:date"); opened_el!(open_delete, "w:del", "w:id", "w:author", "w:date"); diff --git a/docx-wasm/pkg/index_bg.wasm b/docx-wasm/pkg/index_bg.wasm index 4bfe8d5..0f5201c 100644 Binary files a/docx-wasm/pkg/index_bg.wasm and b/docx-wasm/pkg/index_bg.wasm differ