docx_reader/documents/elements/
paragraph_property.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use serde::Serialize;

use super::*;
use crate::types::{AlignmentType, SpecialIndentType};
use crate::ParagraphBorderPosition;

#[derive(Serialize, Debug, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ParagraphProperty {
	pub run_property: RunProperty,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub style: Option<ParagraphStyle>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub numbering_property: Option<NumberingProperty>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub alignment: Option<Justification>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub indent: Option<Indent>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub line_spacing: Option<LineSpacing>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub keep_next: Option<bool>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub keep_lines: Option<bool>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub page_break_before: Option<bool>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub widow_control: Option<bool>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub outline_lvl: Option<OutlineLvl>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub section_property: Option<SectionProperty>,
	pub tabs: Vec<Tab>,
	// read only
	#[serde(skip_serializing_if = "Option::is_none")]
	pub(crate) div_id: Option<String>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub paragraph_property_change: Option<ParagraphPropertyChange>,
	#[serde(skip_serializing_if = "Option::is_none")]
	pub borders: Option<ParagraphBorders>,
}

// 17.3.1.26
// pPr (Paragraph Properties)
// This element specifies a set of paragraph properties which shall be applied to the contents of the parent
// paragraph after all style/numbering/table properties have been applied to the text. These properties are defined
// as direct formatting, since they are directly applied to the paragraph and supersede any formatting from styles.
impl ParagraphProperty {
	pub fn new() -> ParagraphProperty {
		Default::default()
	}

	pub fn align(mut self, alignment_type: AlignmentType) -> Self {
		self.alignment = Some(Justification::new(alignment_type.to_string()));
		self
	}

	pub fn style(mut self, style_id: &str) -> Self {
		self.style = Some(ParagraphStyle::new(Some(style_id)));
		self
	}

	pub fn indent(
		mut self,
		left: Option<i32>,
		special_indent: Option<SpecialIndentType>,
		end: Option<i32>,
		start_chars: Option<i32>,
	) -> Self {
		self.indent = Some(Indent::new(left, special_indent, end, start_chars));
		self
	}

	pub fn numbering(mut self, id: NumberingId, level: IndentLevel) -> Self {
		self.numbering_property = Some(NumberingProperty::new().add_num(id, level));
		self
	}

	pub fn numbering_property(mut self, np: NumberingProperty) -> Self {
		self.numbering_property = Some(np);
		self
	}

	pub fn line_spacing(mut self, spacing: LineSpacing) -> Self {
		self.line_spacing = Some(spacing);
		self
	}

	pub fn character_spacing(mut self, spacing: i32) -> Self {
		self.run_property.character_spacing = Some(CharacterSpacing::new(spacing));
		self
	}

	pub fn keep_next(mut self, v: bool) -> Self {
		self.keep_next = Some(v);
		self
	}

	pub fn keep_lines(mut self, v: bool) -> Self {
		self.keep_lines = Some(v);
		self
	}

	pub fn outline_lvl(mut self, v: usize) -> Self {
		if v >= 10 {
			// clamped
			self.outline_lvl = Some(OutlineLvl::new(9));
			return self;
		}
		self.outline_lvl = Some(OutlineLvl::new(v));
		self
	}

	pub fn page_break_before(mut self, v: bool) -> Self {
		self.page_break_before = Some(v);
		self
	}

	pub fn widow_control(mut self, v: bool) -> Self {
		self.widow_control = Some(v);
		self
	}

	pub fn add_tab(mut self, t: Tab) -> Self {
		self.tabs.push(t);
		self
	}

	pub fn section_property(mut self, s: SectionProperty) -> Self {
		self.section_property = Some(s);
		self
	}

	pub fn paragraph_property_change(mut self, p: ParagraphPropertyChange) -> Self {
		self.paragraph_property_change = Some(p);
		self
	}

	pub(crate) fn hanging_chars(mut self, chars: i32) -> Self {
		if let Some(indent) = self.indent {
			self.indent = Some(indent.hanging_chars(chars));
		}
		self
	}

	pub(crate) fn first_line_chars(mut self, chars: i32) -> Self {
		if let Some(indent) = self.indent {
			self.indent = Some(indent.first_line_chars(chars));
		}
		self
	}

	pub fn set_borders(mut self, borders: ParagraphBorders) -> Self {
		self.borders = Some(borders);
		self
	}

	pub fn set_border(mut self, border: ParagraphBorder) -> Self {
		self.borders = Some(self.borders.unwrap_or_default().set(border));
		self
	}

	pub fn clear_border(mut self, position: ParagraphBorderPosition) -> Self {
		self.borders = Some(self.borders.unwrap_or_default().clear(position));
		self
	}

	pub fn clear_all_borders(mut self) -> Self {
		self.borders = Some(self.borders.unwrap_or_default().clear_all());
		self
	}
}