Skip to main content

umya_spreadsheet/structs/drawing/spreadsheet/
shape.rs

1// xdr:sp
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::{
14    super::super::Anchor,
15    NonVisualShapeProperties,
16    ShapeProperties,
17    ShapeStyle,
18    TextBody,
19};
20use crate::{
21    reader::driver::{
22        get_attribute,
23        set_string_from_xml,
24        xml_read_loop,
25    },
26    structs::{
27        StringValue,
28        raw::RawRelationships,
29    },
30    writer::driver::{
31        write_end_tag,
32        write_start_tag,
33    },
34};
35
36#[derive(Clone, Default, Debug)]
37pub struct Shape {
38    anchor:                      Anchor,
39    non_visual_shape_properties: NonVisualShapeProperties,
40    shape_properties:            ShapeProperties,
41    shape_style:                 Option<Box<ShapeStyle>>,
42    text_body:                   Option<Box<TextBody>>,
43    r#macro:                     StringValue,
44}
45
46impl Shape {
47    #[inline]
48    #[must_use]
49    pub fn anchor(&self) -> &Anchor {
50        &self.anchor
51    }
52
53    #[inline]
54    #[must_use]
55    #[deprecated(since = "3.0.0", note = "Use anchor()")]
56    pub fn get_anchor(&self) -> &Anchor {
57        self.anchor()
58    }
59
60    #[inline]
61    pub fn anchor_mut(&mut self) -> &mut Anchor {
62        &mut self.anchor
63    }
64
65    #[inline]
66    #[deprecated(since = "3.0.0", note = "Use anchor_mut()")]
67    pub fn get_anchor_mut(&mut self) -> &mut Anchor {
68        self.anchor_mut()
69    }
70
71    #[inline]
72    pub fn set_anchor(&mut self, value: Anchor) {
73        self.anchor = value;
74    }
75
76    #[inline]
77    #[must_use]
78    pub fn non_visual_shape_properties(&self) -> &NonVisualShapeProperties {
79        &self.non_visual_shape_properties
80    }
81
82    #[inline]
83    #[must_use]
84    #[deprecated(since = "3.0.0", note = "Use non_visual_shape_properties()")]
85    pub fn get_non_visual_shape_properties(&self) -> &NonVisualShapeProperties {
86        self.non_visual_shape_properties()
87    }
88
89    pub fn non_visual_shape_properties_mut(&mut self) -> &mut NonVisualShapeProperties {
90        &mut self.non_visual_shape_properties
91    }
92
93    #[deprecated(since = "3.0.0", note = "Use non_visual_shape_properties_mut()")]
94    pub fn get_non_visual_shape_properties_mut(&mut self) -> &mut NonVisualShapeProperties {
95        self.non_visual_shape_properties_mut()
96    }
97
98    #[inline]
99    pub fn set_non_visual_shape_properties(&mut self, value: NonVisualShapeProperties) {
100        self.non_visual_shape_properties = value;
101    }
102
103    #[inline]
104    #[must_use]
105    pub fn shape_properties(&self) -> &ShapeProperties {
106        &self.shape_properties
107    }
108
109    #[inline]
110    #[must_use]
111    #[deprecated(since = "3.0.0", note = "Use shape_properties()")]
112    pub fn get_shape_properties(&self) -> &ShapeProperties {
113        self.shape_properties()
114    }
115
116    #[inline]
117    pub fn shape_properties_mut(&mut self) -> &mut ShapeProperties {
118        &mut self.shape_properties
119    }
120
121    #[inline]
122    #[deprecated(since = "3.0.0", note = "Use shape_properties_mut()")]
123    pub fn get_shape_properties_mut(&mut self) -> &mut ShapeProperties {
124        self.shape_properties_mut()
125    }
126
127    #[inline]
128    pub fn set_shape_properties(&mut self, value: ShapeProperties) {
129        self.shape_properties = value;
130    }
131
132    #[must_use]
133    pub fn shape_style(&self) -> Option<&ShapeStyle> {
134        self.shape_style.as_deref()
135    }
136
137    #[must_use]
138    #[deprecated(since = "3.0.0", note = "Use shape_style()")]
139    pub fn get_shape_style(&self) -> Option<&ShapeStyle> {
140        self.shape_style()
141    }
142
143    #[inline]
144    pub fn shape_style_mut(&mut self) -> Option<&mut ShapeStyle> {
145        self.shape_style.as_deref_mut()
146    }
147
148    #[inline]
149    #[deprecated(since = "3.0.0", note = "Use shape_style_mut()")]
150    pub fn get_shape_style_mut(&mut self) -> Option<&mut ShapeStyle> {
151        self.shape_style_mut()
152    }
153
154    #[inline]
155    pub fn set_shape_style(&mut self, value: ShapeStyle) {
156        self.shape_style = Some(Box::new(value));
157    }
158
159    #[inline]
160    #[must_use]
161    pub fn text_body(&self) -> Option<&TextBody> {
162        self.text_body.as_deref()
163    }
164
165    #[inline]
166    #[must_use]
167    #[deprecated(since = "3.0.0", note = "Use text_body()")]
168    pub fn get_text_body(&self) -> Option<&TextBody> {
169        self.text_body()
170    }
171
172    #[inline]
173    pub fn text_body_mut(&mut self) -> Option<&mut TextBody> {
174        self.text_body.as_deref_mut()
175    }
176
177    #[inline]
178    #[deprecated(since = "3.0.0", note = "Use text_body_mut()")]
179    pub fn get_text_body_mut(&mut self) -> Option<&mut TextBody> {
180        self.text_body_mut()
181    }
182
183    #[inline]
184    pub fn set_text_body(&mut self, value: TextBody) {
185        self.text_body = Some(Box::new(value));
186    }
187
188    #[inline]
189    #[must_use]
190    pub fn get_macro(&self) -> &str {
191        self.r#macro.value_str()
192    }
193
194    #[inline]
195    pub fn set_macro<S: Into<String>>(&mut self, value: S) -> &mut Self {
196        self.r#macro.set_value(value);
197        self
198    }
199
200    pub(crate) fn set_attributes<R: std::io::BufRead>(
201        &mut self,
202        reader: &mut Reader<R>,
203        e: &BytesStart,
204        drawing_relationships: Option<&RawRelationships>,
205    ) {
206        set_string_from_xml!(self, e, r#macro, "macro");
207
208        xml_read_loop!(
209            reader,
210                Event::Start(ref e) => {
211                    match e.name().into_inner() {
212                        b"xdr:nvSpPr" => {
213                            self.non_visual_shape_properties.set_attributes(reader, e);
214                        }
215                        b"xdr:spPr" => {
216                            self.shape_properties.set_attributes(reader, e, drawing_relationships);
217                        }
218                        b"xdr:style" => {
219                            let mut obj = ShapeStyle::default();
220                            obj.set_attributes(reader, e);
221                            self.set_shape_style(obj);
222                        }
223                        b"xdr:txBody" => {
224                            let mut obj = TextBody::default();
225                            obj.set_attributes(reader, e);
226                            self.set_text_body(obj);
227                        }
228                        _ => (),
229                    }
230                },
231                Event::End(ref e) => {
232                    if e.name().into_inner() == b"xdr:sp" {
233                        return;
234                    }
235                },
236                Event::Eof => panic!("Error: Could not find {} end element", "xdr:sp")
237        );
238    }
239
240    pub(crate) fn write_to(
241        &self,
242        writer: &mut Writer<Cursor<Vec<u8>>>,
243        rel_list: &mut Vec<(String, String)>,
244        ole_id: usize,
245    ) {
246        // xdr:sp
247        write_start_tag(
248            writer,
249            "xdr:sp",
250            vec![
251                ("macro", self.r#macro.value_str()).into(),
252                ("textlink", "").into(),
253            ],
254            false,
255        );
256
257        // xdr:nvSpPr
258        self.non_visual_shape_properties.write_to(writer, ole_id);
259
260        // xdr:spPr
261        self.shape_properties.write_to(writer, rel_list);
262
263        // xdr:style
264        if let Some(v) = &self.shape_style {
265            v.write_to(writer);
266        }
267
268        // xdr:txBody
269        if let Some(v) = &self.text_body {
270            v.write_to(writer);
271        }
272
273        write_end_tag(writer, "xdr:sp");
274    }
275}