1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// xdr:blipFill
use super::super::super::BooleanValue;
use super::super::Blip;
use super::super::SourceRectangle;
use super::super::Stretch;
use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;
use quick_xml::Writer;
use reader::driver::*;
use std::io::Cursor;
use structs::raw::RawRelationships;
use writer::driver::*;

#[derive(Clone, Default, Debug)]
pub struct BlipFill {
    rotate_with_shape: BooleanValue,
    blip: Blip,
    source_rectangle: Option<SourceRectangle>,
    stretch: Stretch,
}
impl BlipFill {
    pub fn get_rotate_with_shape(&self) -> &bool {
        self.rotate_with_shape.get_value()
    }

    pub fn set_rotate_with_shape(&mut self, value: bool) -> &mut BlipFill {
        self.rotate_with_shape.set_value(value);
        self
    }

    pub fn get_source_rectangle(&self) -> &Option<SourceRectangle> {
        &self.source_rectangle
    }

    pub fn get_source_rectangle_mut(&mut self) -> &mut Option<SourceRectangle> {
        &mut self.source_rectangle
    }

    pub fn set_source_rectangle(&mut self, value: SourceRectangle) -> &mut BlipFill {
        self.source_rectangle = Some(value);
        self
    }

    pub fn get_blip(&self) -> &Blip {
        &self.blip
    }

    pub fn get_blip_mut(&mut self) -> &mut Blip {
        &mut self.blip
    }

    pub fn set_blip(&mut self, value: Blip) -> &mut BlipFill {
        self.blip = value;
        self
    }

    pub fn get_stretch(&self) -> &Stretch {
        &self.stretch
    }

    pub fn get_stretch_mut(&mut self) -> &mut Stretch {
        &mut self.stretch
    }

    pub fn set_stretch(&mut self, value: Stretch) -> &mut BlipFill {
        self.stretch = value;
        self
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        e: &BytesStart,
        drawing_relationships: Option<&RawRelationships>,
    ) {
        let mut buf = Vec::new();

        match get_attribute(e, b"rotWithShape") {
            Some(v) => {
                self.rotate_with_shape.set_value_string(v);
            }
            None => {}
        }

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Start(ref e)) => match e.name().into_inner() {
                    b"a:blip" => {
                        self.blip
                            .set_attributes(reader, e, drawing_relationships.unwrap());
                    }
                    b"a:stretch" => {
                        self.stretch.set_attributes(reader, e);
                    }
                    _ => (),
                },
                Ok(Event::Empty(ref e)) => match e.name().into_inner() {
                    b"a:blip" => {
                        self.blip
                            .set_attributes(reader, e, drawing_relationships.unwrap());
                    }
                    b"a:srcRect" => {
                        let mut source_rectangle = SourceRectangle::default();
                        source_rectangle.set_attributes(reader, e);
                        self.set_source_rectangle(source_rectangle);
                    }
                    _ => (),
                },
                Ok(Event::End(ref e)) => match e.name().into_inner() {
                    b"xdr:blipFill" => return,
                    _ => (),
                },
                Ok(Event::Eof) => panic!("Error not find {} end element", "xdr:blipFill"),
                Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
                _ => (),
            }
            buf.clear();
        }
    }

    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>, r_id: &i32) {
        // xdr:blipFill
        let mut attributes: Vec<(&str, &str)> = Vec::new();
        if &self.rotate_with_shape.has_value() == &true {
            attributes.push(("rotWithShape", self.rotate_with_shape.get_value_string()))
        }
        write_start_tag(writer, "xdr:blipFill", attributes, false);

        // a:blip
        let _ = &self.blip.write_to(writer, r_id);

        // a:srcRect
        match &self.source_rectangle {
            Some(v) => v.write_to(writer),
            None => {}
        }

        // a:stretch
        let _ = &self.stretch.write_to(writer);

        write_end_tag(writer, "xdr:blipFill");
    }
}