umya_spreadsheet/structs/vml/
image_data.rs

1use crate::reader::driver::*;
2use crate::structs::raw::RawRelationships;
3use crate::structs::MediaObject;
4use crate::structs::StringValue;
5use crate::writer::driver::*;
6use quick_xml::events::BytesStart;
7use quick_xml::Reader;
8use quick_xml::Writer;
9use std::io::Cursor;
10
11#[derive(Clone, Default, Debug)]
12pub struct ImageData {
13    image: Option<MediaObject>,
14    title: StringValue,
15}
16
17impl ImageData {
18    #[inline]
19    pub fn get_image(&self) -> Option<&MediaObject> {
20        self.image.as_ref()
21    }
22
23    #[inline]
24    pub fn get_image_mut(&mut self) -> Option<&mut MediaObject> {
25        self.image.as_mut()
26    }
27
28    #[inline]
29    pub fn set_image(&mut self, value: MediaObject) -> &mut Self {
30        self.image = Some(value);
31        self
32    }
33
34    pub fn get_title(&self) -> &str {
35        self.title.get_value_str()
36    }
37
38    pub fn set_title<S: Into<String>>(&mut self, value: S) -> &mut Self {
39        self.title.set_value(value);
40        self
41    }
42
43    pub(crate) fn set_attributes<R: std::io::BufRead>(
44        &mut self,
45        _reader: &mut Reader<R>,
46        e: &BytesStart,
47        drawing_relationships: Option<&RawRelationships>,
48    ) {
49        if let Some(relid) = get_attribute(e, b"o:relid") {
50            if let Some(rel) = drawing_relationships {
51                let relationship = rel.get_relationship_by_rid(&relid);
52                let mut obj = MediaObject::default();
53                obj.set_image_name(relationship.get_raw_file().get_file_name());
54                obj.set_image_data(relationship.get_raw_file().get_file_data());
55                self.set_image(obj);
56            }
57        }
58
59        set_string_from_xml!(self, e, title, "o:title");
60    }
61
62    pub(crate) fn write_to(
63        &self,
64        writer: &mut Writer<Cursor<Vec<u8>>>,
65        rel_list: &mut Vec<(String, String)>,
66    ) {
67        // v:imagedata
68        let mut attributes: Vec<(&str, &str)> = Vec::new();
69        let mut r_id_str = String::from("");
70        if let Some(image) = &self.image {
71            r_id_str = format!("rId{}", image.get_rid(rel_list));
72            attributes.push(("o:relid", &r_id_str));
73        }
74        if self.title.has_value() {
75            attributes.push(("o:title", self.title.get_value_str()));
76        }
77
78        write_start_tag(writer, "v:imagedata", attributes, true);
79    }
80}