umya_spreadsheet/structs/
page_setup.rs

1use crate::reader::driver::*;
2use crate::structs::raw::RawRelationships;
3use crate::structs::EnumValue;
4use crate::structs::OrientationValues;
5use crate::structs::UInt32Value;
6use crate::writer::driver::*;
7use quick_xml::events::BytesStart;
8use quick_xml::Reader;
9use quick_xml::Writer;
10use std::io::Cursor;
11use thin_vec::ThinVec;
12
13#[derive(Clone, Default, Debug)]
14pub struct PageSetup {
15    paper_size: UInt32Value,
16    orientation: EnumValue<OrientationValues>,
17    scale: UInt32Value,
18    fit_to_height: UInt32Value,
19    fit_to_width: UInt32Value,
20    horizontal_dpi: UInt32Value,
21    vertical_dpi: UInt32Value,
22    object_data: Option<ThinVec<u8>>,
23}
24
25impl PageSetup {
26    #[inline]
27    pub fn get_paper_size(&self) -> &u32 {
28        self.paper_size.get_value()
29    }
30
31    #[inline]
32    pub fn set_paper_size(&mut self, value: u32) -> &mut Self {
33        self.paper_size.set_value(value);
34        self
35    }
36
37    #[inline]
38    pub fn get_orientation(&self) -> &OrientationValues {
39        self.orientation.get_value()
40    }
41
42    #[inline]
43    pub fn set_orientation(&mut self, value: OrientationValues) -> &mut Self {
44        self.orientation.set_value(value);
45        self
46    }
47
48    #[inline]
49    pub fn get_scale(&self) -> &u32 {
50        self.scale.get_value()
51    }
52
53    #[inline]
54    pub fn set_scale(&mut self, value: u32) -> &mut Self {
55        self.scale.set_value(value);
56        self
57    }
58
59    #[inline]
60    pub fn get_fit_to_height(&self) -> &u32 {
61        self.fit_to_height.get_value()
62    }
63
64    #[inline]
65    pub fn set_fit_to_height(&mut self, value: u32) -> &mut Self {
66        self.fit_to_height.set_value(value);
67        self
68    }
69
70    #[inline]
71    pub fn get_fit_to_width(&self) -> &u32 {
72        self.fit_to_width.get_value()
73    }
74
75    #[inline]
76    pub fn set_fit_to_width(&mut self, value: u32) -> &mut Self {
77        self.fit_to_width.set_value(value);
78        self
79    }
80
81    #[inline]
82    pub fn get_horizontal_dpi(&self) -> &u32 {
83        self.horizontal_dpi.get_value()
84    }
85
86    #[inline]
87    pub fn set_horizontal_dpi(&mut self, value: u32) -> &mut Self {
88        self.horizontal_dpi.set_value(value);
89        self
90    }
91
92    #[inline]
93    pub fn get_vertical_dpi(&self) -> &u32 {
94        self.vertical_dpi.get_value()
95    }
96
97    #[inline]
98    pub fn set_vertical_dpi(&mut self, value: u32) -> &mut Self {
99        self.vertical_dpi.set_value(value);
100        self
101    }
102
103    #[inline]
104    pub fn get_object_data(&self) -> Option<&[u8]> {
105        self.object_data.as_deref()
106    }
107
108    #[inline]
109    pub fn get_object_data_mut(&mut self) -> Option<&mut ThinVec<u8>> {
110        self.object_data.as_mut()
111    }
112
113    #[inline]
114    pub fn set_object_data(&mut self, value: impl Into<ThinVec<u8>>) -> &mut Self {
115        self.object_data = Some(value.into());
116        self
117    }
118
119    #[inline]
120    pub(crate) fn has_param(&self) -> bool {
121        self.paper_size.has_value()
122            || self.orientation.has_value()
123            || self.scale.has_value()
124            || self.fit_to_height.has_value()
125            || self.fit_to_width.has_value()
126            || self.horizontal_dpi.has_value()
127            || self.vertical_dpi.has_value()
128            || self.object_data.is_some()
129    }
130
131    pub(crate) fn set_attributes<R: std::io::BufRead>(
132        &mut self,
133        _reader: &mut Reader<R>,
134        e: &BytesStart,
135        relationships: Option<&RawRelationships>,
136    ) {
137        set_string_from_xml!(self, e, paper_size, "paperSize");
138        set_string_from_xml!(self, e, orientation, "orientation");
139        set_string_from_xml!(self, e, scale, "scale");
140        set_string_from_xml!(self, e, fit_to_height, "fitToHeight");
141        set_string_from_xml!(self, e, fit_to_width, "fitToWidth");
142        set_string_from_xml!(self, e, horizontal_dpi, "horizontalDpi");
143        set_string_from_xml!(self, e, vertical_dpi, "verticalDpi");
144
145        if let Some(r_id) = get_attribute(e, b"r:id") {
146            let attached_file = relationships
147                .unwrap()
148                .get_relationship_by_rid(&r_id)
149                .get_raw_file();
150            self.set_object_data(attached_file.get_file_data().clone());
151        }
152    }
153
154    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>, r_id: &mut usize) {
155        if self.has_param() {
156            // pageSetup
157            let r_id_str = format!("rId{}", r_id);
158            let mut attributes: Vec<(&str, &str)> = Vec::new();
159            let paper_size = self.paper_size.get_value_string();
160            if self.paper_size.has_value() {
161                attributes.push(("paperSize", &paper_size));
162            }
163            let scale = self.scale.get_value_string();
164            if self.scale.has_value() {
165                attributes.push(("scale", &scale));
166            }
167            let orientation = self.orientation.get_value_string();
168            if self.orientation.has_value() {
169                attributes.push(("orientation", orientation));
170            }
171            let fit_to_height = self.fit_to_height.get_value_string();
172            if self.fit_to_height.has_value() {
173                attributes.push(("fitToHeight", &fit_to_height));
174            }
175            let fit_to_width = self.fit_to_width.get_value_string();
176            if self.fit_to_width.has_value() {
177                attributes.push(("fitToWidth", &fit_to_width));
178            }
179            let horizontal_dpi = self.horizontal_dpi.get_value_string();
180            if self.horizontal_dpi.has_value() {
181                attributes.push(("horizontalDpi", &horizontal_dpi));
182            }
183            let vertical_dpi = self.vertical_dpi.get_value_string();
184            if self.vertical_dpi.has_value() {
185                attributes.push(("verticalDpi", &vertical_dpi));
186            }
187            if self.object_data.is_some() {
188                attributes.push(("r:id", &r_id_str));
189                *r_id += 1;
190            }
191            write_start_tag(writer, "pageSetup", attributes, true);
192        }
193    }
194}