docx_rs/documents/elements/
style.rs1use serde::Serialize;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::escape::escape;
6use crate::types::*;
7use crate::xml_builder::*;
8use crate::StyleType;
9
10use super::*;
11
12#[derive(Debug, Clone, PartialEq, Serialize)]
13#[serde(rename_all = "camelCase")]
14pub struct Style {
15 pub style_id: String,
16 pub name: Name,
17 pub style_type: StyleType,
18 pub run_property: RunProperty,
19 pub paragraph_property: ParagraphProperty,
20 pub table_property: TableProperty,
21 pub table_cell_property: TableCellProperty,
22 pub based_on: Option<BasedOn>,
23 pub next: Option<Next>,
24 #[serde(skip_serializing_if = "Option::is_none")]
25 pub link: Option<Link>,
26 #[serde(skip_serializing_if = "is_true")]
27 pub q_format: bool,
28 #[serde(skip_serializing_if = "Option::is_none")]
29 pub ui_priority: Option<usize>,
30 #[serde(skip_serializing_if = "is_false")]
31 pub semi_hidden: bool,
32 #[serde(skip_serializing_if = "is_false")]
33 pub unhide_when_used: bool,
34}
35
36const fn is_true(v: &bool) -> bool {
37 *v
38}
39
40const fn is_false(v: &bool) -> bool {
41 !*v
42}
43
44impl Default for Style {
45 fn default() -> Self {
46 let name = Name::new("");
47 let rpr = RunProperty::new();
48 let ppr = ParagraphProperty::new();
49 Style {
50 style_id: "".to_owned(),
51 style_type: StyleType::Paragraph,
52 name,
53 run_property: rpr,
54 paragraph_property: ppr,
55 table_property: TableProperty::new(),
56 table_cell_property: TableCellProperty::new(),
57 based_on: None,
58 next: None,
59 link: None,
60 q_format: true,
61 ui_priority: None,
62 semi_hidden: false,
63 unhide_when_used: false,
64 }
65 }
66}
67
68impl Style {
69 pub fn new(style_id: impl Into<String>, style_type: StyleType) -> Self {
70 let default = Default::default();
71 Style {
72 style_id: escape(&style_id.into()),
73 style_type,
74 ..default
75 }
76 }
77
78 pub fn name(mut self, name: impl Into<String>) -> Self {
79 self.name = Name::new(name);
80 self
81 }
82
83 pub fn based_on(mut self, base: impl Into<String>) -> Self {
84 self.based_on = Some(BasedOn::new(base));
85 self
86 }
87
88 pub fn next(mut self, next: impl Into<String>) -> Self {
89 self.next = Some(Next::new(next));
90 self
91 }
92
93 pub fn link(mut self, link: impl Into<String>) -> Self {
94 self.link = Some(Link::new(link));
95 self
96 }
97
98 pub fn q_format(mut self, q_format: bool) -> Self {
99 self.q_format = q_format;
100 self
101 }
102
103 pub fn ui_priority(mut self, ui_priority: usize) -> Self {
104 self.ui_priority = Some(ui_priority);
105 self
106 }
107
108 pub fn semi_hidden(mut self) -> Self {
109 self.semi_hidden = true;
110 self
111 }
112
113 pub fn unhide_when_used(mut self) -> Self {
114 self.unhide_when_used = true;
115 self
116 }
117
118 pub fn size(mut self, size: usize) -> Self {
119 self.run_property = self.run_property.size(size);
120 self
121 }
122
123 pub fn color(mut self, color: impl Into<String>) -> Self {
124 self.run_property = self.run_property.color(color);
125 self
126 }
127
128 pub fn highlight(mut self, color: impl Into<String>) -> Self {
129 self.run_property = self.run_property.highlight(color);
130 self
131 }
132
133 pub fn bold(mut self) -> Self {
134 self.run_property = self.run_property.bold();
135 self
136 }
137
138 pub fn italic(mut self) -> Self {
139 self.run_property = self.run_property.italic();
140 self
141 }
142
143 pub fn underline(mut self, line_type: impl Into<String>) -> Self {
144 self.run_property = self.run_property.underline(line_type);
145 self
146 }
147
148 pub fn vanish(mut self) -> Self {
149 self.run_property = self.run_property.vanish();
150 self
151 }
152
153 pub fn text_border(mut self, b: TextBorder) -> Self {
154 self.run_property = self.run_property.text_border(b);
155 self
156 }
157
158 pub fn fonts(mut self, f: RunFonts) -> Self {
159 self.run_property = self.run_property.fonts(f);
160 self
161 }
162
163 pub fn align(mut self, alignment_type: AlignmentType) -> Self {
164 self.paragraph_property = self.paragraph_property.align(alignment_type);
165 self
166 }
167
168 pub fn text_alignment(mut self, alignment_type: TextAlignmentType) -> Self {
169 self.paragraph_property = self.paragraph_property.text_alignment(alignment_type);
170 self
171 }
172
173 pub fn snap_to_grid(mut self, v: bool) -> Self {
174 self.paragraph_property = self.paragraph_property.snap_to_grid(v);
175 self
176 }
177
178 pub fn line_spacing(mut self, spacing: LineSpacing) -> Self {
179 self.paragraph_property = self.paragraph_property.line_spacing(spacing);
180 self
181 }
182
183 pub fn indent(
184 mut self,
185 left: Option<i32>,
186 special_indent: Option<SpecialIndentType>,
187 end: Option<i32>,
188 start_chars: Option<i32>,
189 ) -> Self {
190 self.paragraph_property =
191 self.paragraph_property
192 .indent(left, special_indent, end, start_chars);
193 self
194 }
195
196 pub fn hanging_chars(mut self, chars: i32) -> Self {
197 self.paragraph_property = self.paragraph_property.hanging_chars(chars);
198 self
199 }
200
201 pub fn first_line_chars(mut self, chars: i32) -> Self {
202 self.paragraph_property = self.paragraph_property.first_line_chars(chars);
203 self
204 }
205
206 pub fn outline_lvl(mut self, l: usize) -> Self {
207 self.paragraph_property = self.paragraph_property.outline_lvl(l);
208 self
209 }
210
211 pub fn table_property(mut self, p: TableProperty) -> Self {
212 self.table_property = p;
213 self
214 }
215
216 pub fn table_indent(mut self, v: i32) -> Self {
217 self.table_property = self.table_property.indent(v);
218 self
219 }
220
221 pub fn table_align(mut self, v: TableAlignmentType) -> Self {
222 self.table_property = self.table_property.align(v);
223 self
224 }
225
226 pub fn style(mut self, s: impl Into<String>) -> Self {
227 self.table_property = self.table_property.style(s);
228 self
229 }
230
231 pub fn layout(mut self, t: TableLayoutType) -> Self {
232 self.table_property = self.table_property.layout(t);
233 self
234 }
235
236 pub fn width(mut self, w: usize, t: WidthType) -> Self {
237 self.table_property = self.table_property.width(w, t);
238 self
239 }
240
241 pub fn margins(mut self, margins: TableCellMargins) -> Self {
242 self.table_property = self.table_property.set_margins(margins);
243 self
244 }
245
246 pub fn set_borders(mut self, borders: TableBorders) -> Self {
247 self.table_property = self.table_property.set_borders(borders);
248 self
249 }
250
251 pub fn set_border(mut self, border: TableBorder) -> Self {
252 self.table_property = self.table_property.set_border(border);
253 self
254 }
255
256 pub fn clear_border(mut self, position: TableBorderPosition) -> Self {
257 self.table_property = self.table_property.clear_border(position);
258 self
259 }
260
261 pub fn clear_all_border(mut self) -> Self {
262 self.table_property = self.table_property.clear_all_border();
263 self
264 }
265
266 pub fn table_cell_property(mut self, p: TableCellProperty) -> Self {
267 self.table_cell_property = p;
268 self
269 }
270
271 pub fn wrap(mut self, wrap: impl Into<String>) -> Self {
273 self.paragraph_property.frame_property = Some(FrameProperty {
274 wrap: Some(wrap.into()),
275 ..self.paragraph_property.frame_property.unwrap_or_default()
276 });
277 self
278 }
279
280 pub fn v_anchor(mut self, anchor: impl Into<String>) -> Self {
281 self.paragraph_property.frame_property = Some(FrameProperty {
282 v_anchor: Some(anchor.into()),
283 ..self.paragraph_property.frame_property.unwrap_or_default()
284 });
285 self
286 }
287
288 pub fn h_anchor(mut self, anchor: impl Into<String>) -> Self {
289 self.paragraph_property.frame_property = Some(FrameProperty {
290 h_anchor: Some(anchor.into()),
291 ..self.paragraph_property.frame_property.unwrap_or_default()
292 });
293 self
294 }
295
296 pub fn h_rule(mut self, r: impl Into<String>) -> Self {
297 self.paragraph_property.frame_property = Some(FrameProperty {
298 h_rule: Some(r.into()),
299 ..self.paragraph_property.frame_property.unwrap_or_default()
300 });
301 self
302 }
303
304 pub fn x_align(mut self, align: impl Into<String>) -> Self {
305 self.paragraph_property.frame_property = Some(FrameProperty {
306 x_align: Some(align.into()),
307 ..self.paragraph_property.frame_property.unwrap_or_default()
308 });
309 self
310 }
311
312 pub fn y_align(mut self, align: impl Into<String>) -> Self {
313 self.paragraph_property.frame_property = Some(FrameProperty {
314 y_align: Some(align.into()),
315 ..self.paragraph_property.frame_property.unwrap_or_default()
316 });
317 self
318 }
319
320 pub fn h_space(mut self, x: i32) -> Self {
321 self.paragraph_property.frame_property = Some(FrameProperty {
322 h_space: Some(x),
323 ..self.paragraph_property.frame_property.unwrap_or_default()
324 });
325 self
326 }
327
328 pub fn v_space(mut self, x: i32) -> Self {
329 self.paragraph_property.frame_property = Some(FrameProperty {
330 v_space: Some(x),
331 ..self.paragraph_property.frame_property.unwrap_or_default()
332 });
333 self
334 }
335
336 pub fn frame_x(mut self, x: i32) -> Self {
337 self.paragraph_property.frame_property = Some(FrameProperty {
338 x: Some(x),
339 ..self.paragraph_property.frame_property.unwrap_or_default()
340 });
341 self
342 }
343
344 pub fn frame_y(mut self, y: i32) -> Self {
345 self.paragraph_property.frame_property = Some(FrameProperty {
346 y: Some(y),
347 ..self.paragraph_property.frame_property.unwrap_or_default()
348 });
349 self
350 }
351
352 pub fn frame_width(mut self, n: u32) -> Self {
353 self.paragraph_property.frame_property = Some(FrameProperty {
354 w: Some(n),
355 ..self.paragraph_property.frame_property.unwrap_or_default()
356 });
357 self
358 }
359
360 pub fn frame_height(mut self, n: u32) -> Self {
361 self.paragraph_property.frame_property = Some(FrameProperty {
362 h: Some(n),
363 ..self.paragraph_property.frame_property.unwrap_or_default()
364 });
365 self
366 }
367}
368
369impl BuildXML for Style {
370 fn build_to<W: Write>(
371 &self,
372 stream: crate::xml::writer::EventWriter<W>,
373 ) -> crate::xml::writer::Result<crate::xml::writer::EventWriter<W>> {
374 XMLBuilder::from(stream)
376 .open_style(self.style_type, &self.style_id)?
377 .add_child(&self.name)?
378 .add_child(&self.run_property)?
379 .add_child(&self.paragraph_property)?
380 .apply_if(self.style_type == StyleType::Table, |b| {
381 b.add_child(&self.table_cell_property)?
382 .add_child(&self.table_property)
383 })?
384 .add_optional_child(&self.next)?
385 .add_optional_child(&self.link)?
386 .apply_if(self.q_format, |b| b.add_child(&QFormat::new()))?
387 .apply_if(self.ui_priority.is_some(), |b| {
388 b.ui_priority(self.ui_priority.unwrap_or_default())
389 })?
390 .apply_if(self.semi_hidden, |b| b.semi_hidden())?
391 .apply_if(self.unhide_when_used, |b| b.unhide_when_used())?
392 .add_optional_child(&self.based_on)?
393 .close()?
394 .into_inner()
395 }
396}
397
398#[cfg(test)]
399mod tests {
400
401 use super::*;
402 #[cfg(test)]
403 use pretty_assertions::assert_eq;
404 use std::str;
405
406 #[test]
407 fn test_build() {
408 let c = Style::new("Heading", StyleType::Paragraph).name("Heading1");
409 let b = c.build();
410 assert_eq!(
411 str::from_utf8(&b).unwrap(),
412 r#"<w:style w:type="paragraph" w:styleId="Heading"><w:name w:val="Heading1" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:qFormat /></w:style>"#
413 );
414 }
415
416 #[test]
417 fn test_build_with_visibility_flags() {
418 let c = Style::new("MyStyle", StyleType::Paragraph)
419 .name("My Style")
420 .q_format(false)
421 .ui_priority(99)
422 .semi_hidden()
423 .unhide_when_used();
424 let b = c.build();
425 assert_eq!(
426 str::from_utf8(&b).unwrap(),
427 r#"<w:style w:type="paragraph" w:styleId="MyStyle"><w:name w:val="My Style" /><w:rPr /><w:pPr><w:rPr /></w:pPr><w:uiPriority w:val="99" /><w:semiHidden /><w:unhideWhenUsed /></w:style>"#
428 );
429 }
430}