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