Skip to main content

docx_rs/reader/
a_graphic.rs

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