2020-02-11 10:01:39 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2019-11-06 12:17:49 +02:00
|
|
|
use crate::documents::BuildXML;
|
2020-02-12 08:44:53 +02:00
|
|
|
use crate::types::*;
|
2019-11-06 12:17:49 +02:00
|
|
|
use crate::xml_builder::*;
|
2019-11-07 09:08:59 +02:00
|
|
|
use crate::StyleType;
|
2019-11-06 12:17:49 +02:00
|
|
|
|
2021-03-25 08:30:33 +02:00
|
|
|
use super::*;
|
2019-11-06 12:17:49 +02:00
|
|
|
|
2020-02-11 10:01:39 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2019-12-06 19:15:21 +02:00
|
|
|
pub struct Style {
|
2020-02-11 10:01:39 +02:00
|
|
|
pub style_id: String,
|
|
|
|
pub name: Name,
|
|
|
|
pub style_type: StyleType,
|
|
|
|
pub run_property: RunProperty,
|
|
|
|
pub paragraph_property: ParagraphProperty,
|
2020-10-30 13:29:06 +02:00
|
|
|
pub table_property: TableProperty,
|
2021-03-25 08:30:33 +02:00
|
|
|
pub table_cell_property: TableCellProperty,
|
2020-10-30 13:29:06 +02:00
|
|
|
pub based_on: Option<BasedOn>,
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
impl Default for Style {
|
2019-12-06 12:18:48 +02:00
|
|
|
fn default() -> Self {
|
2019-11-07 06:57:58 +02:00
|
|
|
let name = Name::new("");
|
|
|
|
let rpr = RunProperty::new();
|
|
|
|
let ppr = ParagraphProperty::new();
|
|
|
|
Style {
|
|
|
|
style_id: "".to_owned(),
|
|
|
|
style_type: StyleType::Paragraph,
|
|
|
|
name,
|
|
|
|
run_property: rpr,
|
|
|
|
paragraph_property: ppr,
|
2020-10-30 13:29:06 +02:00
|
|
|
table_property: TableProperty::new(),
|
2021-03-25 08:30:33 +02:00
|
|
|
table_cell_property: TableCellProperty::new(),
|
2020-10-30 13:29:06 +02:00
|
|
|
based_on: None,
|
2019-11-07 06:57:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
impl Style {
|
2020-02-12 08:44:53 +02:00
|
|
|
pub fn new(style_id: impl Into<String>, style_type: StyleType) -> Self {
|
2019-11-07 06:57:58 +02:00
|
|
|
let default = Default::default();
|
2019-11-06 12:17:49 +02:00
|
|
|
Style {
|
|
|
|
style_id: style_id.into(),
|
|
|
|
style_type,
|
2019-11-07 06:57:58 +02:00
|
|
|
..default
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 12:21:45 +02:00
|
|
|
|
2020-02-12 08:44:53 +02:00
|
|
|
pub fn name(mut self, name: impl Into<String>) -> Self {
|
|
|
|
self.name = Name::new(name);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:29:06 +02:00
|
|
|
pub fn based_on(mut self, base: impl Into<String>) -> Self {
|
|
|
|
self.based_on = Some(BasedOn::new(base));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-14 12:21:45 +02:00
|
|
|
pub fn size(mut self, size: usize) -> Self {
|
|
|
|
self.run_property = self.run_property.size(size);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-12 08:44:53 +02:00
|
|
|
pub fn color(mut self, color: impl Into<String>) -> Self {
|
2019-11-14 12:21:45 +02:00
|
|
|
self.run_property = self.run_property.color(color);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-02-12 08:44:53 +02:00
|
|
|
pub fn highlight(mut self, color: impl Into<String>) -> Self {
|
|
|
|
self.run_property = self.run_property.highlight(color);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-11-14 12:21:45 +02:00
|
|
|
pub fn bold(mut self) -> Self {
|
|
|
|
self.run_property = self.run_property.bold();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn italic(mut self) -> Self {
|
|
|
|
self.run_property = self.run_property.italic();
|
|
|
|
self
|
|
|
|
}
|
2020-02-12 08:44:53 +02:00
|
|
|
|
|
|
|
pub fn underline(mut self, line_type: impl Into<String>) -> Self {
|
|
|
|
self.run_property = self.run_property.underline(line_type);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn vanish(mut self) -> Self {
|
|
|
|
self.run_property = self.run_property.vanish();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn align(mut self, alignment_type: AlignmentType) -> Self {
|
|
|
|
self.paragraph_property = self.paragraph_property.align(alignment_type);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn indent(
|
|
|
|
mut self,
|
2020-03-10 04:56:12 +02:00
|
|
|
left: Option<i32>,
|
2020-02-12 08:44:53 +02:00
|
|
|
special_indent: Option<SpecialIndentType>,
|
2020-02-28 12:52:41 +02:00
|
|
|
end: Option<i32>,
|
2020-03-10 04:56:12 +02:00
|
|
|
start_chars: Option<i32>,
|
2020-02-12 08:44:53 +02:00
|
|
|
) -> Self {
|
2020-03-10 04:56:12 +02:00
|
|
|
self.paragraph_property =
|
|
|
|
self.paragraph_property
|
|
|
|
.indent(left, special_indent, end, start_chars);
|
2020-02-12 08:44:53 +02:00
|
|
|
self
|
|
|
|
}
|
2020-10-30 13:29:06 +02:00
|
|
|
|
2021-04-08 10:53:21 +03:00
|
|
|
pub(crate) fn hanging_chars(mut self, chars: i32) -> Self {
|
|
|
|
self.paragraph_property = self.paragraph_property.hanging_chars(chars);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn first_line_chars(mut self, chars: i32) -> Self {
|
|
|
|
self.paragraph_property = self.paragraph_property.first_line_chars(chars);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-30 13:29:06 +02:00
|
|
|
pub fn table_property(mut self, p: TableProperty) -> Self {
|
|
|
|
self.table_property = p;
|
|
|
|
self
|
|
|
|
}
|
2021-03-25 08:30:33 +02:00
|
|
|
|
|
|
|
pub fn table_cell_property(mut self, p: TableCellProperty) -> Self {
|
|
|
|
self.table_cell_property = p;
|
|
|
|
self
|
|
|
|
}
|
2019-11-06 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:15:21 +02:00
|
|
|
impl BuildXML for Style {
|
2019-11-06 12:17:49 +02:00
|
|
|
fn build(&self) -> Vec<u8> {
|
|
|
|
let b = XMLBuilder::new();
|
2019-11-07 06:57:58 +02:00
|
|
|
// Set "Normal" as default if you need change these values please fix it
|
2019-11-06 12:17:49 +02:00
|
|
|
b.open_style(self.style_type, &self.style_id)
|
2019-11-07 06:57:58 +02:00
|
|
|
.add_child(&self.name)
|
|
|
|
.add_child(&self.run_property)
|
|
|
|
.add_child(&self.paragraph_property)
|
|
|
|
.add_child(&BasedOn::new("Normal"))
|
|
|
|
.add_child(&Next::new("Normal"))
|
|
|
|
.add_child(&QFormat::new())
|
2020-10-30 13:29:06 +02:00
|
|
|
.add_optional_child(&self.based_on)
|
2019-11-06 12:17:49 +02:00
|
|
|
.close()
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
#[cfg(test)]
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_build() {
|
2020-02-12 08:44:53 +02:00
|
|
|
let c = Style::new("Heading", StyleType::Paragraph).name("Heading1");
|
2019-11-06 12:17:49 +02:00
|
|
|
let b = c.build();
|
|
|
|
assert_eq!(
|
|
|
|
str::from_utf8(&b).unwrap(),
|
2020-05-27 14:43:25 +03:00
|
|
|
r#"<w:style w:type="paragraph" w:styleId="Heading"><w:name w:val="Heading1" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style>"#
|
2019-11-06 12:17:49 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|