feat: Support size
parent
d92d111aa6
commit
b167bd7f8d
|
@ -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);
|
||||
}
|
|
@ -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::*;
|
||||
|
|
|
@ -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<Run>,
|
||||
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#"<w:p><w:pPr /><w:pStyle w:val="Normal" /><w:r><w:rPr /><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
|
||||
);
|
||||
}
|
||||
|
||||
#[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#"<w:p><w:pPr /><w:r><w:rPr><w:sz w:val="60" /><w:szCs w:val="60" /></w:rPr><w:t xml:space="preserve">Hello</w:t></w:r></w:p>"#
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Justification>,
|
||||
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<u8> {
|
||||
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#"<w:pPr />"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_alignment() {
|
||||
let c = ParagraphProperty::new();
|
||||
let b = c.align(AlignmentType::Right).build();
|
||||
assert_eq!(
|
||||
str::from_utf8(&b).unwrap(),
|
||||
r#"<w:pPr><w:jc w:val="right" /></w:pPr>"#
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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>,
|
||||
sz_cs: Option<SzCs>,
|
||||
color: Option<Color>,
|
||||
}
|
||||
|
||||
|
@ -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#"<w:rPr><w:sz w:val="10" /><w:color w:val="FFFFFF" /></w:rPr>"#
|
||||
r#"<w:rPr><w:sz w:val="10" /><w:szCs w:val="10" /><w:color w:val="FFFFFF" /></w:rPr>"#
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<u8> {
|
||||
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#"<w:szCs w:val="20" />"#);
|
||||
}
|
||||
}
|
|
@ -8,7 +8,6 @@ impl XMLBuilder {
|
|||
// i.e. <w:basedOn ... >
|
||||
only_str_val_el!(based_on, "w:basedOn");
|
||||
// i.e. <w:t ... >
|
||||
// i.e. <w:sz ... >
|
||||
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. <w:pStyle ... >
|
||||
only_str_val_el!(paragraph_style, "w:pStyle");
|
||||
// i.e. <w:sz ... >
|
||||
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. <w:szCs ... >
|
||||
only_usize_val_el!(sz_cs, "w:szCs");
|
||||
// Build w:style element
|
||||
// i.e. <w:style ... >
|
||||
pub(crate) fn open_style(mut self, style_type: StyleType, id: &str) -> Self {
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
};
|
||||
}
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue