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