2020-02-11 10:01:39 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-06-08 07:41:13 +03:00
|
|
|
use super::*;
|
2019-11-06 12:17:49 +02:00
|
|
|
use crate::documents::BuildXML;
|
|
|
|
use crate::xml_builder::*;
|
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-11-06 12:17:49 +02:00
|
|
|
pub struct RunProperty {
|
2020-02-11 10:01:39 +02:00
|
|
|
pub sz: Option<Sz>,
|
|
|
|
pub sz_cs: Option<SzCs>,
|
|
|
|
pub color: Option<Color>,
|
|
|
|
pub highlight: Option<Highlight>,
|
|
|
|
pub underline: Option<Underline>,
|
|
|
|
pub bold: Option<Bold>,
|
|
|
|
pub bold_cs: Option<BoldCs>,
|
|
|
|
pub italic: Option<Italic>,
|
|
|
|
pub italic_cs: Option<ItalicCs>,
|
|
|
|
pub vanish: Option<Vanish>,
|
2020-10-20 12:22:12 +03:00
|
|
|
pub spacing: Option<i32>,
|
2020-06-08 07:41:13 +03:00
|
|
|
pub fonts: Option<RunFonts>,
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RunProperty {
|
|
|
|
pub fn new() -> RunProperty {
|
2019-11-07 06:57:58 +02:00
|
|
|
Default::default()
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
2019-11-11 08:36:26 +02:00
|
|
|
pub fn size(mut self, size: usize) -> RunProperty {
|
|
|
|
self.sz = Some(Sz::new(size));
|
|
|
|
self.sz_cs = Some(SzCs::new(size));
|
2019-11-06 12:17:49 +02:00
|
|
|
self
|
|
|
|
}
|
2019-11-07 06:57:58 +02:00
|
|
|
|
2020-10-20 12:22:12 +03:00
|
|
|
pub fn spacing(mut self, spacing: i32) -> RunProperty {
|
|
|
|
self.spacing = Some(spacing);
|
2020-10-14 04:16:13 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn color(mut self, color: impl Into<String>) -> RunProperty {
|
2019-11-07 06:57:58 +02:00
|
|
|
self.color = Some(Color::new(color));
|
|
|
|
self
|
|
|
|
}
|
2019-11-13 09:08:25 +02:00
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn highlight(mut self, color: impl Into<String>) -> RunProperty {
|
2019-11-13 09:08:25 +02:00
|
|
|
self.highlight = Some(Highlight::new(color));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bold(mut self) -> RunProperty {
|
|
|
|
self.bold = Some(Bold::new());
|
|
|
|
self.bold_cs = Some(BoldCs::new());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn italic(mut self) -> RunProperty {
|
|
|
|
self.italic = Some(Italic::new());
|
|
|
|
self.italic_cs = Some(ItalicCs::new());
|
|
|
|
self
|
|
|
|
}
|
2019-12-04 09:28:11 +02:00
|
|
|
|
2019-12-08 21:14:27 +02:00
|
|
|
pub fn underline(mut self, line_type: impl Into<String>) -> RunProperty {
|
2019-12-04 09:28:11 +02:00
|
|
|
self.underline = Some(Underline::new(line_type));
|
|
|
|
self
|
|
|
|
}
|
2019-12-13 17:47:47 +02:00
|
|
|
|
|
|
|
pub fn vanish(mut self) -> RunProperty {
|
|
|
|
self.vanish = Some(Vanish::new());
|
|
|
|
self
|
|
|
|
}
|
2020-06-08 07:41:13 +03:00
|
|
|
|
|
|
|
pub fn fonts(mut self, font: RunFonts) -> RunProperty {
|
|
|
|
self.fonts = Some(font);
|
|
|
|
self
|
|
|
|
}
|
2019-11-07 06:57:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RunProperty {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
color: None,
|
2019-11-11 08:36:26 +02:00
|
|
|
sz: None,
|
|
|
|
sz_cs: None,
|
2019-11-13 09:08:25 +02:00
|
|
|
highlight: None,
|
2019-12-04 09:28:11 +02:00
|
|
|
underline: None,
|
2019-11-13 09:08:25 +02:00
|
|
|
bold: None,
|
|
|
|
bold_cs: None,
|
|
|
|
italic: None,
|
|
|
|
italic_cs: None,
|
2019-12-13 17:47:47 +02:00
|
|
|
vanish: None,
|
2020-06-08 07:41:13 +03:00
|
|
|
fonts: None,
|
2020-10-14 04:16:13 +03:00
|
|
|
spacing: None,
|
2019-11-07 06:57:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BuildXML for RunProperty {
|
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
let b = XMLBuilder::new();
|
2020-10-14 04:16:13 +03:00
|
|
|
let spacing = if let Some(s) = self.spacing {
|
|
|
|
Some(Spacing::new(crate::SpacingType::Value(s)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-11-07 06:57:58 +02:00
|
|
|
b.open_run_property()
|
|
|
|
.add_optional_child(&self.sz)
|
2019-11-11 08:36:26 +02:00
|
|
|
.add_optional_child(&self.sz_cs)
|
2019-11-07 06:57:58 +02:00
|
|
|
.add_optional_child(&self.color)
|
2019-11-13 09:08:25 +02:00
|
|
|
.add_optional_child(&self.bold)
|
|
|
|
.add_optional_child(&self.bold_cs)
|
|
|
|
.add_optional_child(&self.italic)
|
|
|
|
.add_optional_child(&self.italic_cs)
|
|
|
|
.add_optional_child(&self.highlight)
|
2019-12-04 09:28:11 +02:00
|
|
|
.add_optional_child(&self.underline)
|
2019-12-13 17:47:47 +02:00
|
|
|
.add_optional_child(&self.vanish)
|
2020-06-08 07:41:13 +03:00
|
|
|
.add_optional_child(&self.fonts)
|
2020-10-14 04:16:13 +03:00
|
|
|
.add_optional_child(&spacing)
|
2019-11-07 06:57:58 +02:00
|
|
|
.close()
|
|
|
|
.build()
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
2019-11-13 09:08:25 +02:00
|
|
|
fn test_size() {
|
2019-11-11 08:36:26 +02:00
|
|
|
let c = RunProperty::new().size(10).color("FFFFFF");
|
2019-11-06 12:17:49 +02:00
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2019-11-11 08:36:26 +02:00
|
|
|
r#"<w:rPr><w:sz w:val="10" /><w:szCs w:val="10" /><w:color w:val="FFFFFF" /></w:rPr>"#
|
2019-11-06 12:17:49 +02:00
|
|
|
);
|
|
|
|
}
|
2019-11-13 09:08:25 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_highlight() {
|
|
|
|
let c = RunProperty::new().highlight("FFFFFF");
|
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:rPr><w:highlight w:val="FFFFFF" /></w:rPr>"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bold() {
|
|
|
|
let c = RunProperty::new().bold();
|
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:rPr><w:b /><w:bCs /></w:rPr>"#
|
|
|
|
);
|
|
|
|
}
|
2019-12-04 09:28:11 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_underline() {
|
|
|
|
let c = RunProperty::new().underline("single");
|
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:rPr><w:u w:val="single" /></w:rPr>"#
|
|
|
|
);
|
|
|
|
}
|
2019-12-13 17:47:47 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vanish() {
|
|
|
|
let c = RunProperty::new().vanish();
|
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:rPr><w:vanish /></w:rPr>"#
|
|
|
|
);
|
|
|
|
}
|
2020-06-08 07:41:13 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_run_fonts() {
|
|
|
|
let c = RunProperty::new().fonts(RunFonts::new().east_asia("Hiragino"));
|
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
|
|
|
r#"<w:rPr><w:rFonts w:eastAsia="Hiragino" /></w:rPr>"#
|
|
|
|
);
|
|
|
|
}
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|