Skip to main content

umya_spreadsheet/structs/drawing/
transform2d.rs

1// a:xfrm
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use crate::{
14    StringValue,
15    reader::driver::{
16        get_attribute,
17        xml_read_loop,
18    },
19    structs::drawing::{
20        Point2DType,
21        PositiveSize2DType,
22    },
23    writer::driver::{
24        write_end_tag,
25        write_start_tag,
26    },
27};
28
29#[derive(Clone, Default, Debug)]
30pub struct Transform2D {
31    offset:        Point2DType,
32    extents:       PositiveSize2DType,
33    child_offset:  Option<Box<Point2DType>>,
34    child_extents: Option<Box<PositiveSize2DType>>,
35    rot:           StringValue,
36    flip_v:        StringValue,
37    flip_h:        StringValue,
38}
39
40impl Transform2D {
41    #[inline]
42    #[must_use]
43    pub fn offset(&self) -> &Point2DType {
44        &self.offset
45    }
46
47    #[inline]
48    #[must_use]
49    #[deprecated(since = "3.0.0", note = "Use offset()")]
50    pub fn get_offset(&self) -> &Point2DType {
51        self.offset()
52    }
53
54    #[inline]
55    pub fn offset_mut(&mut self) -> &mut Point2DType {
56        &mut self.offset
57    }
58
59    #[inline]
60    #[deprecated(since = "3.0.0", note = "Use offset_mut()")]
61    pub fn get_offset_mut(&mut self) -> &mut Point2DType {
62        self.offset_mut()
63    }
64
65    #[inline]
66    pub fn set_offset(&mut self, value: Point2DType) {
67        self.offset = value;
68    }
69
70    #[inline]
71    #[must_use]
72    pub fn extents(&self) -> &PositiveSize2DType {
73        &self.extents
74    }
75
76    #[inline]
77    #[must_use]
78    #[deprecated(since = "3.0.0", note = "Use extents()")]
79    pub fn get_extents(&self) -> &PositiveSize2DType {
80        self.extents()
81    }
82
83    #[inline]
84    pub fn extents_mut(&mut self) -> &mut PositiveSize2DType {
85        &mut self.extents
86    }
87
88    #[inline]
89    #[deprecated(since = "3.0.0", note = "Use extents_mut()")]
90    pub fn get_extents_mut(&mut self) -> &mut PositiveSize2DType {
91        self.extents_mut()
92    }
93
94    #[inline]
95    pub fn set_extents(&mut self, value: PositiveSize2DType) {
96        self.extents = value;
97    }
98
99    #[inline]
100    #[must_use]
101    pub fn child_offset(&self) -> Option<&Point2DType> {
102        self.child_offset.as_deref()
103    }
104
105    #[inline]
106    #[must_use]
107    #[deprecated(since = "3.0.0", note = "Use child_offset()")]
108    pub fn get_child_offset(&self) -> Option<&Point2DType> {
109        self.child_offset()
110    }
111
112    #[inline]
113    pub fn child_offset_mut(&mut self) -> Option<&mut Point2DType> {
114        self.child_offset.as_deref_mut()
115    }
116
117    #[inline]
118    #[deprecated(since = "3.0.0", note = "Use child_offset_mut()")]
119    pub fn get_child_offset_mut(&mut self) -> Option<&mut Point2DType> {
120        self.child_offset_mut()
121    }
122
123    #[inline]
124    pub fn set_child_offset(&mut self, value: Point2DType) {
125        self.child_offset = Some(Box::new(value));
126    }
127
128    #[inline]
129    #[must_use]
130    pub fn child_extents(&self) -> Option<&PositiveSize2DType> {
131        self.child_extents.as_deref()
132    }
133
134    #[inline]
135    #[must_use]
136    #[deprecated(since = "3.0.0", note = "Use child_extents()")]
137    pub fn get_child_extents(&self) -> Option<&PositiveSize2DType> {
138        self.child_extents()
139    }
140
141    #[inline]
142    pub fn child_extents_mut(&mut self) -> Option<&mut PositiveSize2DType> {
143        self.child_extents.as_deref_mut()
144    }
145
146    #[inline]
147    #[deprecated(since = "3.0.0", note = "Use child_extents_mut()")]
148    pub fn get_child_extents_mut(&mut self) -> Option<&mut PositiveSize2DType> {
149        self.child_extents_mut()
150    }
151
152    #[inline]
153    pub fn set_child_extents(&mut self, value: PositiveSize2DType) {
154        self.child_extents = Some(Box::new(value));
155    }
156
157    #[inline]
158    #[must_use]
159    pub fn rot(&self) -> Option<&str> {
160        self.rot.value()
161    }
162
163    #[inline]
164    #[must_use]
165    #[deprecated(since = "3.0.0", note = "Use rot()")]
166    pub fn get_rot(&self) -> Option<&str> {
167        self.rot()
168    }
169
170    #[inline]
171    pub fn set_rot<S: Into<String>>(&mut self, value: S) {
172        self.rot.set_value(value);
173    }
174
175    #[inline]
176    #[must_use]
177    pub fn flip_v(&self) -> Option<&str> {
178        self.flip_v.value()
179    }
180
181    #[inline]
182    #[must_use]
183    #[deprecated(since = "3.0.0", note = "Use flip_v()")]
184    pub fn get_flip_v(&self) -> Option<&str> {
185        self.flip_v()
186    }
187
188    #[inline]
189    pub fn set_flip_v<S: Into<String>>(&mut self, value: S) {
190        self.flip_v.set_value(value);
191    }
192
193    #[inline]
194    #[must_use]
195    pub fn flip_h(&self) -> Option<&str> {
196        self.flip_h.value()
197    }
198
199    #[inline]
200    #[must_use]
201    #[deprecated(since = "3.0.0", note = "Use flip_h()")]
202    pub fn get_flip_h(&self) -> Option<&str> {
203        self.flip_h()
204    }
205
206    #[inline]
207    pub fn set_flip_h<S: Into<String>>(&mut self, value: S) {
208        self.flip_h.set_value(value);
209    }
210
211    pub(crate) fn set_attributes<R: std::io::BufRead>(
212        &mut self,
213        reader: &mut Reader<R>,
214        e: &BytesStart,
215    ) {
216        if let Some(v) = get_attribute(e, b"rot") {
217            self.set_rot(v);
218        }
219
220        if let Some(v) = get_attribute(e, b"flipH") {
221            self.set_flip_h(v);
222        }
223
224        if let Some(v) = get_attribute(e, b"flipV") {
225            self.set_flip_v(v);
226        }
227
228        xml_read_loop!(
229            reader,
230            Event::Empty(ref e) => {
231                match e.name().into_inner() {
232                    b"a:off" => {
233                        self.offset.set_attributes(reader, e, true);
234                    }
235                    b"a:ext" => {
236                        self.extents.set_attributes(reader, e, true);
237                    }
238                    b"a:chOff" => {
239                        let mut obj = Point2DType::default();
240                        obj.set_attributes(reader, e, true);
241                        self.set_child_offset(obj);
242                    }
243                    b"a:chExt" => {
244                        let mut obj = PositiveSize2DType::default();
245                        obj.set_attributes(reader, e, true);
246                        self.set_child_extents(obj);
247                    }
248                    _ => (),
249                }
250            },
251            Event::Start(ref e) => {
252                match e.name().into_inner() {
253                    b"a:off" => {
254                        self.offset.set_attributes(reader, e, false);
255                    }
256                    b"a:ext" => {
257                        self.extents.set_attributes(reader, e, false);
258                    }
259                    b"a:chOff" => {
260                        let mut obj = Point2DType::default();
261                        obj.set_attributes(reader, e, false);
262                        self.set_child_offset(obj);
263                    }
264                    b"a:chExt" => {
265                        let mut obj = PositiveSize2DType::default();
266                        obj.set_attributes(reader, e, false);
267                        self.set_child_extents(obj);
268                    }
269                    _ => (),
270                }
271            },
272            Event::End(ref e) => {
273                if e.name().into_inner() == b"a:xfrm" {
274                    return;
275                }
276            },
277            Event::Eof => panic!("Error: Could not find {} end element", "a:xfrm")
278        );
279    }
280
281    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
282        // a:xfrm
283        let mut attributes: crate::structs::AttrCollection = Vec::new();
284        if let Some(v) = self.rot.value() {
285            attributes.push(("rot", v).into());
286        }
287        if let Some(v) = self.flip_h.value() {
288            attributes.push(("flipH", v).into());
289        }
290        if let Some(v) = self.flip_v.value() {
291            attributes.push(("flipV", v).into());
292        }
293        write_start_tag(writer, "a:xfrm", attributes, false);
294
295        // a:off
296        self.offset.write_to_off(writer);
297
298        // a:ext
299        self.extents.write_to_ext(writer);
300
301        // a:chOff
302        if let Some(v) = &self.child_offset {
303            v.write_to_ch_off(writer);
304        }
305
306        // a:chExt
307        if let Some(v) = &self.child_extents {
308            v.write_to_ch_ext(writer);
309        }
310
311        write_end_tag(writer, "a:xfrm");
312    }
313}