typlite/writer/docx/
styles.rs1use docx_rs::*;
4
5#[derive(Clone, Debug)]
7pub struct DocxStyles {
8 initialized: bool,
9}
10
11impl DocxStyles {
12 pub fn new() -> Self {
14 Self { initialized: false }
15 }
16
17 fn create_heading_style(name: &str, display_name: &str, size: usize) -> Style {
19 Style::new(name, StyleType::Paragraph)
20 .name(display_name)
21 .size(size)
22 .bold()
23 }
24
25 pub fn initialize_styles(&self, docx: Docx) -> Docx {
27 if self.initialized {
28 return docx;
29 }
30
31 let heading1 = Self::create_heading_style("Heading1", "Heading 1", 32);
32 let heading2 = Self::create_heading_style("Heading2", "Heading 2", 28);
33 let heading3 = Self::create_heading_style("Heading3", "Heading 3", 26);
34 let heading4 = Self::create_heading_style("Heading4", "Heading 4", 24);
35 let heading5 = Self::create_heading_style("Heading5", "Heading 5", 22);
36 let heading6 = Self::create_heading_style("Heading6", "Heading 6", 20);
37
38 let courier_fonts = RunFonts::new()
39 .ascii("Courier New")
40 .hi_ansi("Courier New")
41 .east_asia("Courier New")
42 .cs("Courier New");
43
44 let code_block = Style::new("CodeBlock", StyleType::Paragraph)
45 .name("Code Block")
46 .fonts(courier_fonts.clone())
47 .size(18);
48
49 let code_inline = Style::new("CodeInline", StyleType::Character)
50 .name("Code Inline")
51 .fonts(courier_fonts)
52 .size(18);
53
54 let math_block = Style::new("MathBlock", StyleType::Paragraph)
55 .name("Math Block")
56 .align(AlignmentType::Center);
57
58 let emphasis = Style::new("Emphasis", StyleType::Character)
59 .name("Emphasis")
60 .italic();
61
62 let strong = Style::new("Strong", StyleType::Character)
63 .name("Strong")
64 .bold();
65
66 let highlight = Style::new("Highlight", StyleType::Character)
67 .name("Highlight")
68 .highlight("yellow");
69
70 let hyperlink = Style::new("Hyperlink", StyleType::Character)
71 .name("Hyperlink")
72 .color("0000FF")
73 .underline("single");
74
75 let blockquote = Style::new("Blockquote", StyleType::Paragraph)
76 .name("Block Quote")
77 .indent(Some(720), None, None, None)
78 .italic();
79
80 let caption = Style::new("Caption", StyleType::Paragraph)
81 .name("Caption")
82 .italic()
83 .size(16)
84 .align(AlignmentType::Center);
85
86 let table = Style::new("Table", StyleType::Table)
87 .name("Table")
88 .table_align(TableAlignmentType::Center);
89
90 docx.add_style(heading1)
91 .add_style(heading2)
92 .add_style(heading3)
93 .add_style(heading4)
94 .add_style(heading5)
95 .add_style(heading6)
96 .add_style(code_block)
97 .add_style(code_inline)
98 .add_style(math_block)
99 .add_style(emphasis)
100 .add_style(strong)
101 .add_style(highlight)
102 .add_style(hyperlink)
103 .add_style(blockquote)
104 .add_style(caption)
105 .add_style(table)
106 }
107}