Skip to main content

docx_rs/reader/
font_group.rs

1#![allow(clippy::single_match)]
2
3use std::io::Read;
4use std::str::FromStr;
5
6use super::*;
7
8fn read_typeface(attributes: &[OwnedAttribute]) -> Option<String> {
9    for a in attributes {
10        let local_name = &a.name.local_name;
11        if let "typeface" = local_name.as_str() {
12            return Some(a.value.to_string());
13        }
14    }
15    None
16}
17
18fn read_script_and_typeface(attributes: &[OwnedAttribute]) -> Option<(String, String)> {
19    let mut script = None;
20    let mut typeface = None;
21    for a in attributes {
22        let local_name = &a.name.local_name;
23        if let "script" = local_name.as_str() {
24            script = Some(a.value.to_string());
25        }
26        if let "typeface" = local_name.as_str() {
27            typeface = Some(a.value.to_string());
28        }
29    }
30    if let (Some(script), Some(typeface)) = (script, typeface) {
31        return Some((script, typeface));
32    }
33    None
34}
35
36impl ElementReader for FontGroup {
37    fn read<R: Read>(
38        r: &mut EventReader<R>,
39        _attrs: &[OwnedAttribute],
40    ) -> Result<Self, ReaderError> {
41        let mut f = FontGroup::default();
42        loop {
43            let e = r.next();
44            match e {
45                Ok(XmlEvent::StartElement {
46                    attributes, name, ..
47                }) => {
48                    let e = AXMLElement::from_str(&name.local_name).unwrap();
49                    match e {
50                        AXMLElement::Latin => {
51                            if let Some(t) = read_typeface(&attributes) {
52                                f.latin = t;
53                            }
54                        }
55                        AXMLElement::Ea => {
56                            if let Some(t) = read_typeface(&attributes) {
57                                f.ea = t;
58                            }
59                        }
60                        AXMLElement::Cs => {
61                            if let Some(t) = read_typeface(&attributes) {
62                                f.cs = t;
63                            }
64                        }
65                        AXMLElement::Font => {
66                            if let Some((script, typeface)) = read_script_and_typeface(&attributes)
67                            {
68                                f.fonts.push(FontSchemeFont { script, typeface })
69                            }
70                        }
71                        _ => {}
72                    }
73                }
74                Ok(XmlEvent::EndElement { name, .. }) => {
75                    let e = AXMLElement::from_str(&name.local_name).unwrap();
76                    match e {
77                        AXMLElement::MajorFont | AXMLElement::MinorFont => {
78                            return Ok(f);
79                        }
80                        _ => {}
81                    }
82                }
83                Err(_) => return Err(ReaderError::XMLReadError),
84                _ => {}
85            }
86        }
87    }
88}