docx_rs/reader/
pic.rs

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