Skip to main content

docx_rs/reader/
pic.rs

1#![allow(clippy::single_match)]
2
3use std::io::Read;
4use std::str::FromStr;
5
6use super::*;
7
8impl ElementReader for Pic {
9    fn read<R: Read>(
10        r: &mut EventReader<R>,
11        _attrs: &[OwnedAttribute],
12    ) -> Result<Self, ReaderError> {
13        let mut pic = Pic::with_empty();
14        loop {
15            let e = r.next();
16            match e {
17                Ok(XmlEvent::StartElement {
18                    name, attributes, ..
19                }) => {
20                    if let Ok(e) = AXMLElement::from_str(&name.local_name) {
21                        match e {
22                            AXMLElement::Blip => {
23                                if let Some(id) = read(&attributes, "embed") {
24                                    pic = pic.id(id)
25                                }
26                            }
27                            AXMLElement::Off => {
28                                let mut offset_x: i32 = 0;
29                                let mut offset_y: i32 = 0;
30                                if let Some(x) = read(&attributes, "x") {
31                                    if let Ok(x) = f64::from_str(&x) {
32                                        offset_x = x as i32;
33                                    }
34                                }
35                                if let Some(y) = read(&attributes, "y") {
36                                    if let Ok(y) = f64::from_str(&y) {
37                                        offset_y = y as i32;
38                                    }
39                                }
40                                pic = pic.offset_x(offset_x).offset_y(offset_y);
41                            }
42                            AXMLElement::Ext => {
43                                let mut w: u32 = 0;
44                                let mut h: u32 = 0;
45                                if let Some(x) = read(&attributes, "cx") {
46                                    if let Ok(x) = u32::from_str(&x) {
47                                        w = x;
48                                    }
49                                }
50                                if let Some(y) = read(&attributes, "cy") {
51                                    if let Ok(y) = u32::from_str(&y) {
52                                        h = y;
53                                    }
54                                }
55                                pic = pic.size(w, h);
56                            }
57                            _ => {}
58                        }
59                    }
60                }
61                Ok(XmlEvent::EndElement { name, .. }) => {
62                    let e = PicXMLElement::from_str(&name.local_name).unwrap();
63                    if e == PicXMLElement::Pic {
64                        return Ok(pic);
65                    }
66                }
67                Err(_) => return Err(ReaderError::XMLReadError),
68                _ => {}
69            }
70        }
71    }
72}