docx_rs/reader/
a_graphic.rs

1use std::io::Read;
2use std::str::FromStr;
3
4use xml::attribute::OwnedAttribute;
5use xml::reader::{EventReader, XmlEvent};
6
7use super::*;
8
9impl ElementReader for AGraphic {
10    fn read<R: Read>(
11        r: &mut EventReader<R>,
12        _attrs: &[OwnedAttribute],
13    ) -> Result<Self, ReaderError> {
14        let mut graphic = AGraphic::new();
15        loop {
16            let e = r.next();
17            match e {
18                Ok(XmlEvent::StartElement {
19                    name, attributes, ..
20                }) => {
21                    let e = AXMLElement::from_str(&name.local_name)
22                        .expect("should convert to XMLElement");
23                    if let AXMLElement::GraphicData = e {
24                        let data = AGraphicData::read(r, &attributes)?;
25                        graphic = graphic.add_graphic_data(data);
26                    }
27                }
28                Ok(XmlEvent::EndElement { name, .. }) => {
29                    let e = AXMLElement::from_str(&name.local_name).unwrap();
30                    if e == AXMLElement::Graphic {
31                        return Ok(graphic);
32                    }
33                }
34                Err(_) => return Err(ReaderError::XMLReadError),
35                _ => {}
36            }
37        }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43
44    use super::*;
45    #[cfg(test)]
46    use pretty_assertions::assert_eq;
47
48    #[test]
49    fn test_read_graphic_with_textbox() {
50        let c = r#"<w:document xmlns:o="urn:schemas-microsoft-com:office:office"
51        xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
52        xmlns:v="urn:schemas-microsoft-com:vml"
53        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
54        xmlns:w10="urn:schemas-microsoft-com:office:word"
55        xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
56        xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
57        xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
58        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
59        xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
60        xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14">
61        <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
62        <a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
63            <wps:wsp>
64                <wps:spPr>
65                    <a:xfrm>
66                        <a:off x="0" y="0"/>
67                        <a:ext cx="914400" cy="343080"/>
68                    </a:xfrm>
69                    <a:prstGeom prst="rect">
70                        <a:avLst></a:avLst>
71                    </a:prstGeom>
72                    <a:solidFill>
73                        <a:srgbClr val="ffffff"/>
74                    </a:solidFill>
75                    <a:ln w="720">
76                        <a:solidFill>
77                            <a:srgbClr val="000000"/>
78                        </a:solidFill>
79                        <a:round/>
80                    </a:ln>
81                </wps:spPr>
82                <wps:style>
83                    <a:lnRef idx="0"/>
84                    <a:fillRef idx="0"/>
85                    <a:effectRef idx="0"/>
86                    <a:fontRef idx="minor"/>
87                </wps:style>
88                <wps:txbx>
89                    <w:txbxContent>
90                        <w:p>
91                            <w:pPr>
92                                <w:rPr></w:rPr>
93                            </w:pPr>
94                            <w:r>
95                                <w:rPr></w:rPr>
96                                <w:t>pattern1</w:t>
97                            </w:r>
98                        </w:p>
99                    </w:txbxContent>
100                </wps:txbx>
101                <wps:bodyPr>
102                </wps:bodyPr>
103            </wps:wsp>
104        </a:graphicData>
105    </a:graphic></w:body>"#;
106        let mut parser = EventReader::new(c.as_bytes());
107        let g = AGraphic::read(&mut parser, &[]).unwrap();
108        assert_eq!(
109            g,
110            AGraphic::new().add_graphic_data(
111                AGraphicData::new(GraphicDataType::WpShape).add_shape(
112                    WpsShape::new().add_text_box(WpsTextBox::new().add_content(
113                        TextBoxContent::new().add_paragraph(
114                            Paragraph::new().add_run(Run::new().add_text("pattern1"))
115                        )
116                    ))
117                )
118            )
119        );
120    }
121}