spreadsheet_ods/
draw.rs

1//!
2//! Draw.
3//!
4
5use crate::attrmap2::AttrMap2;
6use crate::style::units::RelativeScale;
7use crate::style::{GraphicStyleRef, ParagraphStyleRef};
8use crate::text::{TextP, TextTag};
9use crate::xlink::{XLinkActuate, XLinkShow, XLinkType};
10use crate::{CellRef, Length, OdsError};
11use base64::Engine;
12use chrono::NaiveDateTime;
13use get_size::GetSize;
14use get_size_derive::GetSize;
15
16/// The <office:annotation> element specifies an OpenDocument annotation. The annotation's
17/// text is contained in <text:p> and <text:list> elements.
18#[derive(Debug, Clone)]
19pub struct Annotation {
20    //
21    name: String,
22    //
23    display: bool,
24    //
25    creator: Option<String>,
26    date: Option<NaiveDateTime>,
27    text: Vec<TextTag>,
28    //
29    attr: AttrMap2,
30}
31
32impl GetSize for Annotation {
33    fn get_heap_size(&self) -> usize {
34        self.name.get_heap_size()
35            + self.creator.get_heap_size()
36            + self.text.get_heap_size()
37            + self.attr.get_heap_size()
38    }
39}
40
41impl Annotation {
42    /// New annotation.
43    pub fn new_empty() -> Self {
44        Self {
45            name: Default::default(),
46            display: false,
47            creator: None,
48            date: None,
49            text: Default::default(),
50            attr: Default::default(),
51        }
52    }
53
54    /// New annotation.
55    pub fn new<S: Into<String>>(annotation: S) -> Self {
56        let mut r = Self {
57            name: Default::default(),
58            display: true,
59            creator: None,
60            date: None,
61            text: Default::default(),
62            attr: Default::default(),
63        };
64        r.push_text(TextP::new().text(annotation).into_xmltag());
65        r
66    }
67
68    /// Allows access to all attributes of the style itself.
69    pub fn attrmap(&self) -> &AttrMap2 {
70        &self.attr
71    }
72
73    /// Allows access to all attributes of the style itself.
74    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
75        &mut self.attr
76    }
77
78    /// Name
79    pub fn name(&self) -> &str {
80        &self.name
81    }
82
83    /// Name
84    pub fn set_name<S: Into<String>>(&mut self, name: S) {
85        self.name = name.into();
86    }
87
88    /// Display
89    pub fn display(&self) -> bool {
90        self.display
91    }
92
93    /// Name
94    pub fn set_display(&mut self, display: bool) {
95        self.display = display;
96    }
97
98    /// Creator
99    pub fn creator(&self) -> Option<&String> {
100        self.creator.as_ref()
101    }
102
103    /// Creator
104    pub fn set_creator<S: Into<String>>(&mut self, creator: Option<S>) {
105        self.creator = creator.map(|v| v.into())
106    }
107
108    /// Date of the annotation.
109    pub fn date(&self) -> Option<&NaiveDateTime> {
110        self.date.as_ref()
111    }
112
113    /// Date of the annotation.
114    pub fn set_date(&mut self, date: Option<NaiveDateTime>) {
115        self.date = date;
116    }
117
118    /// Text.
119    pub fn text(&self) -> &Vec<TextTag> {
120        &self.text
121    }
122
123    /// Text.
124    pub fn push_text(&mut self, text: TextTag) {
125        self.text.push(text);
126    }
127
128    /// Text.
129    pub fn push_text_str<S: Into<String>>(&mut self, text: S) {
130        self.text.push(TextP::new().text(text).into_xmltag());
131    }
132
133    /// Text.
134    pub fn set_text(&mut self, text: Vec<TextTag>) {
135        self.text = text;
136    }
137
138    draw_caption_point_x!(attr);
139    draw_caption_point_y!(attr);
140    draw_class_names!(attr);
141    draw_corner_radius!(attr);
142    draw_id!(attr);
143    draw_layer!(attr);
144    draw_style_name!(attr);
145    draw_text_style_name!(attr);
146    draw_transform!(attr);
147    draw_z_index!(attr);
148    svg_height!(attr);
149    svg_width!(attr);
150    svg_x!(attr);
151    svg_y!(attr);
152    table_end_cell_address!(attr);
153    table_end_x!(attr);
154    table_end_y!(attr);
155    table_table_background!(attr);
156    xml_id!(attr);
157}
158
159// /// The <draw:rect> element represents a rectangular drawing shape.
160// #[derive(Debug, Clone)]
161// pub struct DrawRect {
162//     ///
163//     name: String,
164//     ///
165//     attr: AttrMap2,
166// }
167//
168// impl DrawRect {
169//     pub fn new_empty() -> Self {
170//         Self {
171//             name: "".to_string(),
172//             attr: Default::default(),
173//         }
174//     }
175//
176//     pub fn new<S: Into<String>>(name: S) -> Self {
177//         Self {
178//             name: name.into(),
179//             attr: Default::default(),
180//         }
181//     }
182//
183//     /// Allows access to all attributes of the style itself.
184//     pub fn attrmap(&self) -> &AttrMap2 {
185//         &self.attr
186//     }
187//
188//     /// Allows access to all attributes of the style itself.
189//     pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
190//         &mut self.attr
191//     }
192//
193//     /// Name
194//     pub fn name(&self) -> &str {
195//         &self.name
196//     }
197//
198//     /// Name
199//     pub fn set_name<S: Into<String>>(&mut self, name: S) {
200//         self.name = name.into();
201//     }
202//
203//     draw_caption_id!(attr);
204//     draw_class_names!(attr);
205//     draw_corner_radius!(attr);
206//     draw_id!(attr);
207//     draw_layer!(attr);
208//     draw_style_name!(attr);
209//     draw_text_style_name!(attr);
210//     draw_transform!(attr);
211//     draw_z_index!(attr);
212//     svg_height!(attr);
213//     svg_width!(attr);
214//     svg_rx!(attr);
215//     svg_ry!(attr);
216//     svg_x!(attr);
217//     svg_y!(attr);
218//     table_end_cell_address!(attr);
219//     table_end_x!(attr);
220//     table_end_y!(attr);
221//     table_table_background!(attr);
222//     xml_id!(attr);
223// }
224
225/// The <draw:frame> element represents a frame and serves as the container for elements that
226/// may occur in a frame.
227/// Frame formatting properties are stored in styles belonging to the graphic family.
228#[derive(Debug, Clone, Default, GetSize)]
229pub struct DrawFrame {
230    /// The <svg:title> element specifies a name for a graphic object.
231    title: Option<String>,
232    /// The <svg:desc> element specifies a prose description of a graphic object that may be used to
233    /// support accessibility. See appendix D.
234    desc: Option<String>,
235    ///
236    attr: AttrMap2,
237    ///
238    content: Vec<DrawFrameContent>,
239}
240
241/// Draw-frame content data.
242#[derive(Debug, Clone, GetSize)]
243pub enum DrawFrameContent {
244    /// Image
245    Image(DrawImage),
246}
247
248impl DrawFrame {
249    /// New.
250    pub fn new() -> Self {
251        Default::default()
252    }
253
254    /// Allows access to all attributes of the style itself.
255    pub fn attrmap(&self) -> &AttrMap2 {
256        &self.attr
257    }
258
259    /// Allows access to all attributes of the style itself.
260    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
261        &mut self.attr
262    }
263
264    /// Desc
265    pub fn desc(&self) -> Option<&String> {
266        self.desc.as_ref()
267    }
268
269    /// Desc
270    pub fn set_desc<S: Into<String>>(&mut self, desc: S) {
271        self.desc = Some(desc.into())
272    }
273
274    /// Desc
275    pub fn clear_desc(&mut self) {
276        self.desc = None;
277    }
278
279    /// Title
280    pub fn title(&self) -> Option<&String> {
281        self.title.as_ref()
282    }
283
284    /// Name
285    pub fn set_title<S: Into<String>>(&mut self, title: S) {
286        self.title = Some(title.into());
287    }
288
289    /// Name
290    pub fn clear_title(&mut self) {
291        self.title = None;
292    }
293
294    /// Frame content.
295    pub fn set_content(&mut self, content: Vec<DrawFrameContent>) {
296        self.content = content;
297    }
298
299    /// Frame content.
300    pub fn push_content(&mut self, content: DrawFrameContent) {
301        self.content.push(content);
302    }
303
304    /// Frame content.
305    pub fn clear_content(&mut self) {
306        self.content.clear();
307    }
308
309    /// Frame content.
310    pub fn content_ref(&self) -> &Vec<DrawFrameContent> {
311        &self.content
312    }
313
314    /// Frame content.
315    pub fn content_mut(&mut self) -> &mut Vec<DrawFrameContent> {
316        &mut self.content
317    }
318
319    draw_name!(attr);
320    draw_caption_id!(attr);
321    draw_class_names!(attr);
322    draw_corner_radius!(attr);
323    draw_copy_of!(attr);
324    draw_id!(attr);
325    draw_layer!(attr);
326    draw_style_name!(attr);
327    draw_text_style_name!(attr);
328    draw_transform!(attr);
329    draw_z_index!(attr);
330    style_rel_height!(attr);
331    style_rel_width!(attr);
332    svg_height!(attr);
333    svg_width!(attr);
334    svg_x!(attr);
335    svg_y!(attr);
336    table_end_cell_address!(attr);
337    table_end_x!(attr);
338    table_end_y!(attr);
339    table_table_background!(attr);
340    xml_id!(attr);
341}
342
343/// The <draw:image> element represents an image. An image can be either:
344/// • A link to an external resource
345/// or
346/// • Embedded in the document
347/// The <draw:image> element may have text content. Text content is displayed in addition to the
348/// image data.
349/// Note: While the image data may have an arbitrary format, vector graphics should
350/// be stored in the SVG format and bitmap graphics in the PNG format.
351#[derive(Debug, Clone, Default, GetSize)]
352pub struct DrawImage {
353    attr: AttrMap2,
354    binary_data: Option<String>,
355    text: Vec<TextTag>,
356}
357
358impl DrawImage {
359    /// New.
360    pub fn new() -> Self {
361        Default::default()
362    }
363
364    /// Allows access to all attributes of the style itself.
365    pub fn attrmap(&self) -> &AttrMap2 {
366        &self.attr
367    }
368
369    /// Allows access to all attributes of the style itself.
370    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
371        &mut self.attr
372    }
373
374    /// Image binary data.
375    pub fn get_binary_base64(&self) -> Option<&String> {
376        self.binary_data.as_ref()
377    }
378
379    /// Image binary data.
380    pub fn set_binary_base64(&mut self, binary: String) {
381        self.binary_data = Some(binary);
382    }
383
384    /// Image binary data.
385    pub fn get_binary(&self) -> Result<Vec<u8>, OdsError> {
386        let ng = base64::engine::GeneralPurpose::new(
387            &base64::alphabet::STANDARD,
388            base64::engine::general_purpose::NO_PAD,
389        );
390
391        if let Some(binary_data) = &self.binary_data {
392            Ok(ng.decode(binary_data)?)
393        } else {
394            Ok(Default::default())
395        }
396    }
397
398    /// Image binary data.
399    /// Note: While the image data may have an arbitrary format, vector graphics should
400    /// be stored in the SVG format and bitmap graphics in the PNG format.
401    pub fn set_binary(&mut self, binary: &[u8]) {
402        let ng = base64::engine::GeneralPurpose::new(
403            &base64::alphabet::STANDARD,
404            base64::engine::general_purpose::NO_PAD,
405        );
406
407        self.binary_data = Some(ng.encode(binary));
408    }
409
410    /// Image binary data.
411    pub fn clear_binary(&mut self) {
412        self.binary_data = None;
413    }
414
415    /// Text
416    pub fn get_text(&self) -> &Vec<TextTag> {
417        &self.text
418    }
419
420    /// Text
421    pub fn push_text(&mut self, text: TextTag) {
422        self.text.push(text);
423    }
424
425    /// Text
426    pub fn push_text_str<S: Into<String>>(&mut self, text: S) {
427        self.text.push(TextP::new().text(text).into_xmltag());
428    }
429
430    /// Text
431    pub fn set_text(&mut self, text: Vec<TextTag>) {
432        self.text = text;
433    }
434
435    draw_filter_name!(attr);
436    draw_mime_type!(attr);
437    xlink_actuate!(attr);
438    xlink_href!(attr);
439    xlink_show!(attr);
440    xlink_type!(attr);
441    xml_id!(attr);
442}