Skip to main content

umya_spreadsheet/structs/drawing/charts/
text_properties.rs

1// c:txPr
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::super::{
14    BodyProperties,
15    ListStyle,
16    Paragraph,
17};
18use crate::{
19    reader::driver::xml_read_loop,
20    writer::driver::{
21        write_end_tag,
22        write_start_tag,
23    },
24};
25
26#[derive(Clone, Default, Debug)]
27pub struct TextProperties {
28    body_properties: BodyProperties,
29    list_style:      ListStyle,
30    paragraph:       Vec<Paragraph>,
31}
32
33impl TextProperties {
34    #[must_use]
35    pub fn body_properties(&self) -> &BodyProperties {
36        &self.body_properties
37    }
38
39    #[must_use]
40    #[deprecated(since = "3.0.0", note = "Use body_properties()")]
41    pub fn get_body_properties(&self) -> &BodyProperties {
42        self.body_properties()
43    }
44
45    pub fn body_properties_mut(&mut self) -> &mut BodyProperties {
46        &mut self.body_properties
47    }
48
49    #[deprecated(since = "3.0.0", note = "Use body_properties_mut()")]
50    pub fn get_body_properties_mut(&mut self) -> &mut BodyProperties {
51        self.body_properties_mut()
52    }
53
54    pub fn set_body_properties(&mut self, value: BodyProperties) -> &mut TextProperties {
55        self.body_properties = value;
56        self
57    }
58
59    #[must_use]
60    pub fn list_style(&self) -> &ListStyle {
61        &self.list_style
62    }
63
64    #[must_use]
65    #[deprecated(since = "3.0.0", note = "Use list_style()")]
66    pub fn get_list_style(&self) -> &ListStyle {
67        self.list_style()
68    }
69
70    pub fn list_style_mut(&mut self) -> &mut ListStyle {
71        &mut self.list_style
72    }
73
74    #[deprecated(since = "3.0.0", note = "Use list_style_mut()")]
75    pub fn get_list_style_mut(&mut self) -> &mut ListStyle {
76        self.list_style_mut()
77    }
78
79    pub fn set_list_style(&mut self, value: ListStyle) -> &mut TextProperties {
80        self.list_style = value;
81        self
82    }
83
84    #[must_use]
85    pub fn paragraph(&self) -> &[Paragraph] {
86        &self.paragraph
87    }
88
89    #[must_use]
90    #[deprecated(since = "3.0.0", note = "Use paragraph()")]
91    pub fn get_paragraph(&self) -> &[Paragraph] {
92        self.paragraph()
93    }
94
95    pub fn paragraph_mut(&mut self) -> &mut Vec<Paragraph> {
96        &mut self.paragraph
97    }
98
99    #[deprecated(since = "3.0.0", note = "Use paragraph_mut()")]
100    pub fn get_paragraph_mut(&mut self) -> &mut Vec<Paragraph> {
101        self.paragraph_mut()
102    }
103
104    pub fn add_paragraph(&mut self, value: Paragraph) -> &mut TextProperties {
105        self.paragraph.push(value);
106        self
107    }
108
109    pub(crate) fn set_attributes<R: std::io::BufRead>(
110        &mut self,
111        reader: &mut Reader<R>,
112        _e: &BytesStart,
113    ) {
114        xml_read_loop!(
115            reader,
116            Event::Start(ref e) => {
117                match e.name().0 {
118                    b"a:p" => {
119                        let mut paragraph = Paragraph::default();
120                        paragraph.set_attributes(reader, e);
121                        self.add_paragraph(paragraph);
122                    }
123                    b"a:bodyPr" => {
124                        let mut body_properties = BodyProperties::default();
125                        body_properties.set_attributes(reader, e, false);
126                        self.set_body_properties(body_properties);
127                    }
128                    _ => (),
129                }
130            },
131            Event::Empty(ref e) => {
132                if e.name().0 == b"a:bodyPr" {
133                    let mut body_properties = BodyProperties::default();
134                    body_properties.set_attributes(reader, e, true);
135                    self.set_body_properties(body_properties);
136                }
137            },
138            Event::End(ref e) => {
139                if e.name().0 == b"c:txPr" {
140                    return;
141                }
142            },
143            Event::Eof => panic!("Error: Could not find {} end element", "c:txPr")
144        );
145    }
146
147    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
148        // c:txPr
149        write_start_tag(writer, "c:txPr", vec![], false);
150
151        // a:bodyPr
152        self.body_properties.write_to(writer);
153
154        // a:lstStyle
155        write_start_tag(writer, "a:lstStyle", vec![], true);
156
157        // a:p
158        for content in &self.paragraph {
159            content.write_to(writer);
160        }
161
162        write_end_tag(writer, "c:txPr");
163    }
164}