Skip to main content

umya_spreadsheet/structs/drawing/
blip_fill.rs

1// a:blipFill
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::{
14    super::super::BooleanValue,
15    Blip,
16    SourceRectangle,
17    Stretch,
18};
19use crate::{
20    reader::driver::{
21        get_attribute,
22        set_string_from_xml,
23        xml_read_loop,
24    },
25    structs::raw::RawRelationships,
26    writer::driver::{
27        write_end_tag,
28        write_start_tag,
29    },
30};
31
32#[derive(Clone, Default, Debug)]
33pub struct BlipFill {
34    rotate_with_shape: BooleanValue,
35    blip:              Blip,
36    source_rectangle:  Option<Box<SourceRectangle>>,
37    stretch:           Stretch,
38}
39
40impl BlipFill {
41    #[inline]
42    #[must_use]
43    pub fn rotate_with_shape(&self) -> bool {
44        self.rotate_with_shape.value()
45    }
46
47    #[inline]
48    #[must_use]
49    #[deprecated(since = "3.0.0", note = "Use rotate_with_shape()")]
50    pub fn get_rotate_with_shape(&self) -> bool {
51        self.rotate_with_shape()
52    }
53
54    #[inline]
55    pub fn set_rotate_with_shape(&mut self, value: bool) -> &mut Self {
56        self.rotate_with_shape.set_value(value);
57        self
58    }
59
60    #[inline]
61    #[must_use]
62    pub fn source_rectangle(&self) -> Option<&SourceRectangle> {
63        self.source_rectangle.as_deref()
64    }
65
66    #[inline]
67    #[must_use]
68    #[deprecated(since = "3.0.0", note = "Use source_rectangle()")]
69    pub fn get_source_rectangle(&self) -> Option<&SourceRectangle> {
70        self.source_rectangle()
71    }
72
73    #[inline]
74    pub fn source_rectangle_mut(&mut self) -> Option<&mut SourceRectangle> {
75        self.source_rectangle.as_deref_mut()
76    }
77
78    #[inline]
79    #[deprecated(since = "3.0.0", note = "Use source_rectangle_mut()")]
80    pub fn get_source_rectangle_mut(&mut self) -> Option<&mut SourceRectangle> {
81        self.source_rectangle_mut()
82    }
83
84    #[inline]
85    pub fn set_source_rectangle(&mut self, value: SourceRectangle) -> &mut Self {
86        self.source_rectangle = Some(Box::new(value));
87        self
88    }
89
90    #[inline]
91    #[must_use]
92    pub fn blip(&self) -> &Blip {
93        &self.blip
94    }
95
96    #[inline]
97    #[must_use]
98    #[deprecated(since = "3.0.0", note = "Use blip()")]
99    pub fn get_blip(&self) -> &Blip {
100        self.blip()
101    }
102
103    #[inline]
104    pub fn blip_mut(&mut self) -> &mut Blip {
105        &mut self.blip
106    }
107
108    #[inline]
109    #[deprecated(since = "3.0.0", note = "Use blip_mut()")]
110    pub fn get_blip_mut(&mut self) -> &mut Blip {
111        self.blip_mut()
112    }
113
114    #[inline]
115    pub fn set_blip(&mut self, value: Blip) -> &mut Self {
116        self.blip = value;
117        self
118    }
119
120    #[inline]
121    #[must_use]
122    pub fn stretch(&self) -> &Stretch {
123        &self.stretch
124    }
125
126    #[inline]
127    #[must_use]
128    #[deprecated(since = "3.0.0", note = "Use stretch()")]
129    pub fn get_stretch(&self) -> &Stretch {
130        self.stretch()
131    }
132
133    #[inline]
134    pub fn stretch_mut(&mut self) -> &mut Stretch {
135        &mut self.stretch
136    }
137
138    #[inline]
139    #[deprecated(since = "3.0.0", note = "Use stretch_mut()")]
140    pub fn get_stretch_mut(&mut self) -> &mut Stretch {
141        self.stretch_mut()
142    }
143
144    #[inline]
145    pub fn set_stretch(&mut self, value: Stretch) -> &mut Self {
146        self.stretch = value;
147        self
148    }
149
150    pub(crate) fn set_attributes<R: std::io::BufRead>(
151        &mut self,
152        reader: &mut Reader<R>,
153        e: &BytesStart,
154        drawing_relationships: Option<&RawRelationships>,
155    ) {
156        set_string_from_xml!(self, e, rotate_with_shape, "rotWithShape");
157
158        xml_read_loop!(
159            reader,
160            Event::Start(ref e) => {
161                match e.name().into_inner() {
162                    b"a:blip" => {
163                        self.blip
164                            .set_attributes(reader, e, drawing_relationships.unwrap(), false);
165                        }
166                    b"a:stretch" => {
167                        self.stretch.set_attributes(reader, e);
168                    }
169                    _ => (),
170                }
171            },
172            Event::Empty(ref e) => {
173                match e.name().into_inner() {
174                    b"a:blip" => {
175                        self.blip
176                            .set_attributes(reader, e, drawing_relationships.unwrap(), true);
177                        }
178                    b"a:srcRect" => {
179                        let mut source_rectangle = SourceRectangle::default();
180                        source_rectangle.set_attributes(reader, e);
181                        self.set_source_rectangle(source_rectangle);
182                    }
183                    _ => (),
184                }
185            },
186            Event::End(ref e) => {
187                if e.name().into_inner() == b"a:blipFill" {
188                    return;
189                }
190            },
191            Event::Eof => panic!("Error: Could not find {} end element", "a:blipFill")
192        );
193    }
194
195    pub(crate) fn write_to(
196        &self,
197        writer: &mut Writer<Cursor<Vec<u8>>>,
198        rel_list: &mut Vec<(String, String)>,
199    ) {
200        // a:blipFill
201        let mut attributes: crate::structs::AttrCollection = Vec::new();
202        if self.rotate_with_shape.has_value() {
203            attributes.push(("rotWithShape", self.rotate_with_shape.value_string()).into());
204        }
205        write_start_tag(writer, "a:blipFill", attributes, false);
206
207        // a:blip
208        self.blip.write_to(writer, rel_list);
209
210        // a:srcRect
211        if let Some(v) = &self.source_rectangle {
212            v.write_to(writer);
213        }
214
215        // a:stretch
216        self.stretch.write_to(writer);
217
218        write_end_tag(writer, "a:blipFill");
219    }
220}