spreadsheet_ods/style/
pagestyle.rs

1use crate::attrmap2::AttrMap2;
2use crate::color::Rgb;
3use crate::style::units::{
4    Border, LengthPercent, Margin, MasterPageUsage, Percent, PrintCentering, PrintContent,
5    PrintOrder, PrintOrientation, StyleNumFormat, WritingMode,
6};
7use crate::style::AnyStyleRef;
8use crate::style::{
9    border_line_width_string, border_string, color_string, shadow_string, ParseStyleAttr,
10};
11use crate::{Length, OdsResult};
12use get_size2::GetSize;
13use std::borrow::Borrow;
14
15style_ref2!(PageStyleRef);
16
17/// The <style:page-layout> element represents the styles that specify the formatting properties
18/// of a page.
19///
20/// For an example see MasterPage.
21///
22#[derive(Debug, Clone, GetSize)]
23pub struct PageStyle {
24    name: String,
25    // Everywhere else this is a AttrMap2, but here is just this lonely.
26    // We still need access to the string to read and write.
27    pub(crate) master_page_usage: Option<String>,
28
29    style: AttrMap2,
30    header: HeaderFooterStyle,
31    footer: HeaderFooterStyle,
32}
33
34impl PageStyle {
35    /// New pagestyle.
36    pub fn new_empty() -> Self {
37        Self {
38            name: Default::default(),
39            master_page_usage: None,
40            style: Default::default(),
41            header: Default::default(),
42            footer: Default::default(),
43        }
44    }
45
46    /// New pagestyle.
47    pub fn new<S: AsRef<str>>(name: S) -> Self {
48        Self {
49            name: name.as_ref().to_string(),
50            master_page_usage: None,
51            style: Default::default(),
52            header: Default::default(),
53            footer: Default::default(),
54        }
55    }
56
57    /// Style reference.
58    pub fn style_ref(&self) -> PageStyleRef {
59        PageStyleRef::from(self.name())
60    }
61
62    /// Style name
63    pub fn name(&self) -> &str {
64        &self.name
65    }
66
67    /// Style name
68    pub fn set_name<S: Into<String>>(&mut self, name: S) {
69        self.name = name.into();
70    }
71
72    /// The style:page-usage attribute specifies the type of pages that a page master should
73    /// generate.
74    /// The defined values for the style:page-usage attribute are:
75    /// * all: if there are no <style:header-left> or <style:footer-left> elements, the
76    ///   header and footer content is the same for left and right pages.
77    /// * left: <style:header-left> or <style:footer-left> elements are ignored.
78    /// * mirrored: if there are no <style:header-left> or <style:footer-left> elements,
79    ///   the header and footer content is the same for left and right pages.
80    /// * right: <style:header-left> or <style:footer-left> elements are ignored.
81    ///
82    /// The default value for this attribute is all.
83    pub fn set_page_usage(&mut self, usage: MasterPageUsage) {
84        self.master_page_usage = Some(usage.to_string());
85    }
86
87    /// Remove page-usage flag.
88    pub fn clear_page_usage(&mut self) {
89        self.master_page_usage = None;
90    }
91
92    /// The style:page-usage attribute specifies the type of pages that a page master should
93    /// generate.
94    pub fn page_usage(&self) -> OdsResult<Option<MasterPageUsage>> {
95        MasterPageUsage::parse_attr(self.master_page_usage.as_deref())
96    }
97
98    /// Attributes for header.
99    pub fn headerstyle(&self) -> &HeaderFooterStyle {
100        &self.header
101    }
102
103    /// Attributes for header.
104    pub fn headerstyle_mut(&mut self) -> &mut HeaderFooterStyle {
105        &mut self.header
106    }
107
108    /// Attributes for footer.
109    pub fn footerstyle(&self) -> &HeaderFooterStyle {
110        &self.footer
111    }
112
113    /// Attributes for footer.
114    pub fn footerstyle_mut(&mut self) -> &mut HeaderFooterStyle {
115        &mut self.footer
116    }
117
118    /// Access to all style attributes.
119    pub fn style(&self) -> &AttrMap2 {
120        &self.style
121    }
122
123    /// Access to all style attributes.
124    pub fn style_mut(&mut self) -> &mut AttrMap2 {
125        &mut self.style
126    }
127
128    fo_page_height!(style);
129    fo_page_width!(style);
130    style_first_page_number!(style);
131    style_footnote_max_height!(style);
132    style_num_format!(style);
133    style_num_letter_sync!(style);
134    style_num_prefix!(style);
135    style_num_suffix!(style);
136    style_paper_tray_name!(style);
137    style_print!(style);
138    style_print_orientation!(style);
139    style_print_page_order!(style);
140    style_scale_to!(style);
141    style_scale_to_pages!(style);
142    style_table_centering!(style);
143    style_writing_mode!(style);
144    fo_background_color!(style);
145    fo_border!(style);
146    fo_border_line_width!(style);
147    fo_margin!(style);
148    fo_padding!(style);
149    style_dynamic_spacing!(style);
150    style_shadow!(style);
151}
152
153/// Style attributes for header/footer.
154#[derive(Clone, Debug, Default, GetSize)]
155pub struct HeaderFooterStyle {
156    style: AttrMap2,
157}
158
159impl HeaderFooterStyle {
160    /// General attributes.
161    pub fn style(&self) -> &AttrMap2 {
162        &self.style
163    }
164
165    /// General attributes.
166    pub fn style_mut(&mut self) -> &mut AttrMap2 {
167        &mut self.style
168    }
169
170    fo_background_color!(style);
171    fo_border!(style);
172    fo_margin!(style);
173    fo_min_height!(style);
174    fo_padding!(style);
175    fo_border_line_width!(style);
176    style_dynamic_spacing!(style);
177    style_shadow!(style);
178    svg_height!(style);
179}