umya_spreadsheet/structs/drawing/
blip.rs

1// a:blip
2use crate::helper::const_str::*;
3use crate::reader::driver::*;
4use crate::structs::raw::RawRelationships;
5use crate::structs::MediaObject;
6use crate::writer::driver::*;
7use quick_xml::events::{BytesStart, Event};
8use quick_xml::Reader;
9use quick_xml::Writer;
10use std::io::Cursor;
11
12#[derive(Clone, Default, Debug)]
13pub struct Blip {
14    image: MediaObject,
15    cstate: Box<str>,
16}
17
18impl Blip {
19    #[inline]
20    pub fn get_image(&self) -> &MediaObject {
21        &self.image
22    }
23
24    #[inline]
25    pub fn get_image_mut(&mut self) -> &mut MediaObject {
26        &mut self.image
27    }
28
29    #[inline]
30    pub fn set_image(&mut self, value: MediaObject) -> &mut Self {
31        self.image = value;
32        self
33    }
34
35    #[inline]
36    pub fn get_cstate(&self) -> &str {
37        &self.cstate
38    }
39
40    #[inline]
41    pub fn set_cstate<S: Into<String>>(&mut self, value: S) -> &mut Self {
42        self.cstate = value.into().into_boxed_str();
43        self
44    }
45
46    pub(crate) fn set_attributes<R: std::io::BufRead>(
47        &mut self,
48        reader: &mut Reader<R>,
49        e: &BytesStart,
50        drawing_relationships: &RawRelationships,
51        empty_flag: bool,
52    ) {
53        if let Some(v) = get_attribute(e, b"cstate") {
54            self.set_cstate(v);
55        }
56
57        let picture_id = get_attribute(e, b"r:embed").unwrap();
58        let relationship = drawing_relationships.get_relationship_by_rid(&picture_id);
59        self.get_image_mut()
60            .set_image_name(relationship.get_raw_file().get_file_name());
61        self.get_image_mut()
62            .set_image_data(relationship.get_raw_file().get_file_data());
63
64        if empty_flag {
65            return;
66        }
67
68        xml_read_loop!(
69            reader,
70            Event::End(ref e) => {
71                if e.name().into_inner() == b"a:blip" {
72                    return
73                }
74            },
75            Event::Eof => panic!("Error: Could not find {} end element", "a:blip")
76        );
77    }
78
79    pub(crate) fn write_to(
80        &self,
81        writer: &mut Writer<Cursor<Vec<u8>>>,
82        rel_list: &mut Vec<(String, String)>,
83    ) {
84        // a:blip
85        let r_id = self.image.get_rid(rel_list);
86        let r_id_str = format!("rId{}", r_id);
87        let mut attributes: Vec<(&str, &str)> = Vec::new();
88        attributes.push(("xmlns:r", REL_OFC_NS));
89        attributes.push(("r:embed", &r_id_str));
90        if !&self.cstate.is_empty() {
91            attributes.push(("cstate", &self.cstate));
92        }
93        write_start_tag(writer, "a:blip", attributes, false);
94
95        // a:extLst
96        write_start_tag(writer, "a:extLst", vec![], false);
97
98        // a:ext
99        write_start_tag(
100            writer,
101            "a:ext",
102            vec![("uri", "{28A0092B-C50C-407E-A947-70E740481C1C}")],
103            false,
104        );
105
106        // a14:useLocalDpi
107        write_start_tag(
108            writer,
109            "a14:useLocalDpi",
110            vec![("xmlns:a14", DRAWING_MAIN_NS), ("val", "0")],
111            true,
112        );
113        write_end_tag(writer, "a:ext");
114        write_end_tag(writer, "a:extLst");
115        write_end_tag(writer, "a:blip");
116    }
117}