1use 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#[derive(Debug, Clone)]
18pub struct Annotation {
19    name: String,
21    display: bool,
23    creator: Option<String>,
25    date: Option<NaiveDateTime>,
26    text: Vec<TextTag>,
27    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    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    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    pub fn attrmap(&self) -> &AttrMap2 {
69        &self.attr
70    }
71
72    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
74        &mut self.attr
75    }
76
77    pub fn name(&self) -> &str {
79        &self.name
80    }
81
82    pub fn set_name<S: Into<String>>(&mut self, name: S) {
84        self.name = name.into();
85    }
86
87    pub fn display(&self) -> bool {
89        self.display
90    }
91
92    pub fn set_display(&mut self, display: bool) {
94        self.display = display;
95    }
96
97    pub fn creator(&self) -> Option<&String> {
99        self.creator.as_ref()
100    }
101
102    pub fn set_creator<S: Into<String>>(&mut self, creator: Option<S>) {
104        self.creator = creator.map(|v| v.into())
105    }
106
107    pub fn date(&self) -> Option<&NaiveDateTime> {
109        self.date.as_ref()
110    }
111
112    pub fn set_date(&mut self, date: Option<NaiveDateTime>) {
114        self.date = date;
115    }
116
117    pub fn text(&self) -> &Vec<TextTag> {
119        &self.text
120    }
121
122    pub fn push_text(&mut self, text: TextTag) {
124        self.text.push(text);
125    }
126
127    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    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#[derive(Debug, Clone, Default, GetSize)]
228pub struct DrawFrame {
229    title: Option<String>,
231    desc: Option<String>,
234    attr: AttrMap2,
236    content: Vec<DrawFrameContent>,
238}
239
240#[derive(Debug, Clone, GetSize)]
242pub enum DrawFrameContent {
243    Image(DrawImage),
245}
246
247impl DrawFrame {
248    pub fn new() -> Self {
250        Default::default()
251    }
252
253    pub fn attrmap(&self) -> &AttrMap2 {
255        &self.attr
256    }
257
258    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
260        &mut self.attr
261    }
262
263    pub fn desc(&self) -> Option<&String> {
265        self.desc.as_ref()
266    }
267
268    pub fn set_desc<S: Into<String>>(&mut self, desc: S) {
270        self.desc = Some(desc.into())
271    }
272
273    pub fn clear_desc(&mut self) {
275        self.desc = None;
276    }
277
278    pub fn title(&self) -> Option<&String> {
280        self.title.as_ref()
281    }
282
283    pub fn set_title<S: Into<String>>(&mut self, title: S) {
285        self.title = Some(title.into());
286    }
287
288    pub fn clear_title(&mut self) {
290        self.title = None;
291    }
292
293    pub fn set_content(&mut self, content: Vec<DrawFrameContent>) {
295        self.content = content;
296    }
297
298    pub fn push_content(&mut self, content: DrawFrameContent) {
300        self.content.push(content);
301    }
302
303    pub fn clear_content(&mut self) {
305        self.content.clear();
306    }
307
308    pub fn content_ref(&self) -> &Vec<DrawFrameContent> {
310        &self.content
311    }
312
313    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#[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    pub fn new() -> Self {
360        Default::default()
361    }
362
363    pub fn attrmap(&self) -> &AttrMap2 {
365        &self.attr
366    }
367
368    pub fn attrmap_mut(&mut self) -> &mut AttrMap2 {
370        &mut self.attr
371    }
372
373    pub fn get_binary_base64(&self) -> Option<&String> {
375        self.binary_data.as_ref()
376    }
377
378    pub fn set_binary_base64(&mut self, binary: String) {
380        self.binary_data = Some(binary);
381    }
382
383    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    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    pub fn clear_binary(&mut self) {
411        self.binary_data = None;
412    }
413
414    pub fn get_text(&self) -> &Vec<TextTag> {
416        &self.text
417    }
418
419    pub fn push_text(&mut self, text: TextTag) {
421        self.text.push(text);
422    }
423
424    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    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}