Skip to main content

umya_spreadsheet/structs/drawing/spreadsheet/
transform.rs

1// xdr:xfrm
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::super::{
14    Extents,
15    Offset,
16};
17use crate::{
18    BooleanValue,
19    Int32Value,
20    reader::driver::{
21        get_attribute,
22        set_string_from_xml,
23        xml_read_loop,
24    },
25    writer::driver::{
26        write_end_tag,
27        write_start_tag,
28    },
29};
30
31#[derive(Clone, Default, Debug)]
32pub struct Transform {
33    offset:          Offset,
34    extents:         Extents,
35    rotation:        Int32Value,
36    vertical_flip:   BooleanValue,
37    horizontal_flip: BooleanValue,
38}
39
40impl Transform {
41    #[inline]
42    #[must_use]
43    pub fn offset(&self) -> &Offset {
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) -> &Offset {
51        self.offset()
52    }
53
54    #[inline]
55    pub fn offset_mut(&mut self) -> &mut Offset {
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 Offset {
62        self.offset_mut()
63    }
64
65    #[inline]
66    pub fn set_offset(&mut self, value: Offset) -> &mut Transform {
67        self.offset = value;
68        self
69    }
70
71    #[inline]
72    #[must_use]
73    pub fn extents(&self) -> &Extents {
74        &self.extents
75    }
76
77    #[inline]
78    #[must_use]
79    #[deprecated(since = "3.0.0", note = "Use extents()")]
80    pub fn get_extents(&self) -> &Extents {
81        self.extents()
82    }
83
84    #[inline]
85    pub fn extents_mut(&mut self) -> &mut Extents {
86        &mut self.extents
87    }
88
89    #[inline]
90    #[deprecated(since = "3.0.0", note = "Use extents_mut()")]
91    pub fn get_extents_mut(&mut self) -> &mut Extents {
92        self.extents_mut()
93    }
94
95    #[inline]
96    pub fn set_extents(&mut self, value: Extents) -> &mut Transform {
97        self.extents = value;
98        self
99    }
100
101    #[inline]
102    #[must_use]
103    pub fn rotation(&self) -> i32 {
104        self.rotation.value()
105    }
106
107    #[inline]
108    #[must_use]
109    #[deprecated(since = "3.0.0", note = "Use rotation()")]
110    pub fn get_rotation(&self) -> i32 {
111        self.rotation()
112    }
113
114    #[inline]
115    pub fn set_rotation(&mut self, value: i32) {
116        self.rotation.set_value(value);
117    }
118
119    #[inline]
120    #[must_use]
121    pub fn vertical_flip(&self) -> bool {
122        self.vertical_flip.value()
123    }
124
125    #[inline]
126    #[must_use]
127    #[deprecated(since = "3.0.0", note = "Use vertical_flip()")]
128    pub fn get_vertical_flip(&self) -> bool {
129        self.vertical_flip()
130    }
131
132    #[inline]
133    pub fn set_vertical_flip(&mut self, value: bool) {
134        self.vertical_flip.set_value(value);
135    }
136
137    #[inline]
138    #[must_use]
139    pub fn horizontal_flip(&self) -> bool {
140        self.horizontal_flip.value()
141    }
142
143    #[inline]
144    #[must_use]
145    #[deprecated(since = "3.0.0", note = "Use horizontal_flip()")]
146    pub fn get_horizontal_flip(&self) -> bool {
147        self.horizontal_flip()
148    }
149
150    #[inline]
151    pub fn set_horizontal_flip(&mut self, value: bool) {
152        self.horizontal_flip.set_value(value);
153    }
154
155    pub(crate) fn set_attributes<R: std::io::BufRead>(
156        &mut self,
157        reader: &mut Reader<R>,
158        e: &BytesStart,
159    ) {
160        set_string_from_xml!(self, e, rotation, "rot");
161        set_string_from_xml!(self, e, horizontal_flip, "flipH");
162        set_string_from_xml!(self, e, vertical_flip, "flipV");
163
164        xml_read_loop!(
165            reader,
166            Event::Empty(ref e) => {
167                match e.name().into_inner() {
168                    b"a:off" => {
169                        self.offset.set_attributes(reader, e);
170                    }
171                    b"a:ext" => {
172                        self.extents.set_attributes(reader, e);
173                    }
174                    _ => (),
175                }
176            },
177            Event::End(ref e) => {
178                if  e.name().into_inner() == b"xdr:xfrm" {
179                    return;
180                }
181            },
182            Event::Eof => panic!("Error: Could not find {} end element", "xdr:xfrm")
183        );
184    }
185
186    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
187        // xdr:xfrm
188        let mut attributes: crate::structs::AttrCollection = Vec::new();
189        let rot = self.rotation.value_string();
190        if self.rotation.has_value() {
191            attributes.push(("rot", &rot).into());
192        }
193        if self.horizontal_flip.has_value() {
194            attributes.push(("flipH", self.horizontal_flip.value_string()).into());
195        }
196        if self.vertical_flip.has_value() {
197            attributes.push(("flipV", self.vertical_flip.value_string()).into());
198        }
199        write_start_tag(writer, "xdr:xfrm", attributes, false);
200
201        // a:off
202        self.offset.write_to(writer);
203
204        // a:ext
205        self.extents.write_to(writer);
206
207        write_end_tag(writer, "xdr:xfrm");
208    }
209}