use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use std::io::Read;
use crate::excel::XmlReader;
use crate::raw::drawing::text::font::{XlsxMajorFont, XlsxMinorFont};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxFontScheme {
pub major_font: Option<XlsxMajorFont>,
pub minor_font: Option<XlsxMinorFont>,
pub name: Option<String>,
}
impl XlsxFontScheme {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut buf = Vec::new();
let mut scheme = Self {
major_font: None,
minor_font: None,
name: None,
};
let attributes = e.attributes();
for a in attributes {
match a {
Ok(a) => {
let string_value = String::from_utf8(a.value.to_vec())?;
match a.key.local_name().as_ref() {
b"name" => {
scheme.name = Some(string_value);
break;
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"extLst" => {
let _ = reader.read_to_end_into(e.to_end().to_owned().name(), &mut Vec::new());
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"majorFont" => {
scheme.major_font = Some(XlsxMajorFont::load_major(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"minorFont" => {
scheme.minor_font = Some(XlsxMinorFont::load_minor(reader)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"fontScheme" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(scheme)
}
}