use std::io::Cursor;
use quick_xml::{
Reader,
Writer,
events::{
BytesStart,
Event,
},
};
use super::super::{
BodyProperties,
ListStyle,
Paragraph,
};
use crate::{
reader::driver::xml_read_loop,
writer::driver::{
write_end_tag,
write_start_tag,
},
};
#[derive(Clone, Default, Debug)]
pub struct TextProperties {
body_properties: BodyProperties,
list_style: ListStyle,
paragraph: Vec<Paragraph>,
}
impl TextProperties {
#[must_use]
pub fn body_properties(&self) -> &BodyProperties {
&self.body_properties
}
#[must_use]
#[deprecated(since = "3.0.0", note = "Use body_properties()")]
pub fn get_body_properties(&self) -> &BodyProperties {
self.body_properties()
}
pub fn body_properties_mut(&mut self) -> &mut BodyProperties {
&mut self.body_properties
}
#[deprecated(since = "3.0.0", note = "Use body_properties_mut()")]
pub fn get_body_properties_mut(&mut self) -> &mut BodyProperties {
self.body_properties_mut()
}
pub fn set_body_properties(&mut self, value: BodyProperties) -> &mut TextProperties {
self.body_properties = value;
self
}
#[must_use]
pub fn list_style(&self) -> &ListStyle {
&self.list_style
}
#[must_use]
#[deprecated(since = "3.0.0", note = "Use list_style()")]
pub fn get_list_style(&self) -> &ListStyle {
self.list_style()
}
pub fn list_style_mut(&mut self) -> &mut ListStyle {
&mut self.list_style
}
#[deprecated(since = "3.0.0", note = "Use list_style_mut()")]
pub fn get_list_style_mut(&mut self) -> &mut ListStyle {
self.list_style_mut()
}
pub fn set_list_style(&mut self, value: ListStyle) -> &mut TextProperties {
self.list_style = value;
self
}
#[must_use]
pub fn paragraph(&self) -> &[Paragraph] {
&self.paragraph
}
#[must_use]
#[deprecated(since = "3.0.0", note = "Use paragraph()")]
pub fn get_paragraph(&self) -> &[Paragraph] {
self.paragraph()
}
pub fn paragraph_mut(&mut self) -> &mut Vec<Paragraph> {
&mut self.paragraph
}
#[deprecated(since = "3.0.0", note = "Use paragraph_mut()")]
pub fn get_paragraph_mut(&mut self) -> &mut Vec<Paragraph> {
self.paragraph_mut()
}
pub fn add_paragraph(&mut self, value: Paragraph) -> &mut TextProperties {
self.paragraph.push(value);
self
}
pub(crate) fn set_attributes<R: std::io::BufRead>(
&mut self,
reader: &mut Reader<R>,
_e: &BytesStart,
) {
xml_read_loop!(
reader,
Event::Start(ref e) => {
match e.name().0 {
b"a:p" => {
let mut paragraph = Paragraph::default();
paragraph.set_attributes(reader, e);
self.add_paragraph(paragraph);
}
b"a:bodyPr" => {
let mut body_properties = BodyProperties::default();
body_properties.set_attributes(reader, e, false);
self.set_body_properties(body_properties);
}
_ => (),
}
},
Event::Empty(ref e) => {
if e.name().0 == b"a:bodyPr" {
let mut body_properties = BodyProperties::default();
body_properties.set_attributes(reader, e, true);
self.set_body_properties(body_properties);
}
},
Event::End(ref e) => {
if e.name().0 == b"c:txPr" {
return;
}
},
Event::Eof => panic!("Error: Could not find {} end element", "c:txPr")
);
}
pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
write_start_tag(writer, "c:txPr", vec![], false);
self.body_properties.write_to(writer);
write_start_tag(writer, "a:lstStyle", vec![], true);
for content in &self.paragraph {
content.write_to(writer);
}
write_end_tag(writer, "c:txPr");
}
}