docx_rs/documents/
settings.rs

1use super::*;
2use std::io::Write;
3
4use crate::documents::BuildXML;
5use crate::types::CharacterSpacingValues;
6use crate::xml_builder::*;
7
8use serde::Serialize;
9
10#[derive(Debug, Clone, PartialEq, Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Settings {
13    default_tab_stop: DefaultTabStop,
14    zoom: Zoom,
15    doc_id: Option<DocId>,
16    doc_vars: Vec<DocVar>,
17    even_and_odd_headers: bool,
18    adjust_line_height_in_table: bool,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    character_spacing_control: Option<CharacterSpacingValues>,
21}
22
23impl Settings {
24    pub fn new() -> Settings {
25        Default::default()
26    }
27
28    pub fn doc_id(mut self, id: impl Into<String>) -> Self {
29        self.doc_id = Some(DocId::new(id.into()));
30        self
31    }
32
33    pub fn default_tab_stop(mut self, tab_stop: usize) -> Self {
34        self.default_tab_stop = DefaultTabStop::new(tab_stop);
35        self
36    }
37
38    pub fn add_doc_var(mut self, name: impl Into<String>, val: impl Into<String>) -> Self {
39        self.doc_vars.push(DocVar::new(name, val));
40        self
41    }
42
43    pub fn even_and_odd_headers(mut self) -> Self {
44        self.even_and_odd_headers = true;
45        self
46    }
47
48    pub fn adjust_line_height_in_table(mut self) -> Self {
49        self.adjust_line_height_in_table = true;
50        self
51    }
52
53    pub fn character_spacing_control(mut self, val: CharacterSpacingValues) -> Self {
54        self.character_spacing_control = Some(val);
55        self
56    }
57}
58
59impl Default for Settings {
60    fn default() -> Self {
61        Self {
62            default_tab_stop: DefaultTabStop::new(840),
63            zoom: Zoom::new(100),
64            doc_id: None,
65            doc_vars: vec![],
66            even_and_odd_headers: false,
67            adjust_line_height_in_table: false,
68            character_spacing_control: None,
69        }
70    }
71}
72
73impl BuildXML for Settings {
74    fn build_to<W: Write>(
75        &self,
76        stream: xml::writer::EventWriter<W>,
77    ) -> xml::writer::Result<xml::writer::EventWriter<W>> {
78        XMLBuilder::from(stream)
79            .declaration(Some(true))?
80            .open_settings()?
81            .add_child(&self.default_tab_stop)?
82            .add_child(&self.zoom)?
83            .open_compat()?
84            .space_for_ul()?
85            .balance_single_byte_double_byte_width()?
86            .do_not_leave_backslash_alone()?
87            .ul_trail_space()?
88            .do_not_expand_shift_return()?
89            .apply_opt(self.character_spacing_control, |v, b| {
90                b.character_spacing_control(&v.to_string())
91            })?
92            .apply_if(self.adjust_line_height_in_table, |b| {
93                b.adjust_line_height_table()
94            })?
95            .use_fe_layout()?
96            .compat_setting(
97                "compatibilityMode",
98                "http://schemas.microsoft.com/office/word",
99                "15",
100            )?
101            .compat_setting(
102                "overrideTableStyleFontSizeAndJustification",
103                "http://schemas.microsoft.com/office/word",
104                "1",
105            )?
106            .compat_setting(
107                "enableOpenTypeFeatures",
108                "http://schemas.microsoft.com/office/word",
109                "1",
110            )?
111            .compat_setting(
112                "doNotFlipMirrorIndents",
113                "http://schemas.microsoft.com/office/word",
114                "1",
115            )?
116            .compat_setting(
117                "differentiateMultirowTableHeaders",
118                "http://schemas.microsoft.com/office/word",
119                "1",
120            )?
121            .compat_setting(
122                "useWord2013TrackBottomHyphenation",
123                "http://schemas.microsoft.com/office/word",
124                "0",
125            )?
126            .close()?
127            .add_optional_child(&self.doc_id)?
128            .apply_if(!self.doc_vars.is_empty(), |b| {
129                b.open_doc_vars()?.add_children(&self.doc_vars)?.close()
130            })?
131            .apply_if(self.even_and_odd_headers, |b| b.even_and_odd_headers())?
132            .close()?
133            .into_inner()
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use super::*;
140    use pretty_assertions::assert_eq;
141    use std::str;
142
143    #[test]
144    fn test_settings() {
145        let c = Settings::new();
146        let b = c.build();
147        assert_eq!(
148            str::from_utf8(&b).unwrap(),
149            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"><w:defaultTabStop w:val="840" /><w:zoom w:percent="100" /><w:compat><w:spaceForUL /><w:balanceSingleByteDoubleByteWidth /><w:doNotLeaveBackslashAlone /><w:ulTrailSpace /><w:doNotExpandShiftReturn /><w:useFELayout /><w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="15" /><w:compatSetting w:name="overrideTableStyleFontSizeAndJustification" w:uri="http://schemas.microsoft.com/office/word" w:val="1" /><w:compatSetting w:name="enableOpenTypeFeatures" w:uri="http://schemas.microsoft.com/office/word" w:val="1" /><w:compatSetting w:name="doNotFlipMirrorIndents" w:uri="http://schemas.microsoft.com/office/word" w:val="1" /><w:compatSetting w:name="differentiateMultirowTableHeaders" w:uri="http://schemas.microsoft.com/office/word" w:val="1" /><w:compatSetting w:name="useWord2013TrackBottomHyphenation" w:uri="http://schemas.microsoft.com/office/word" w:val="0" /></w:compat></w:settings>"#
150        );
151    }
152}