Skip to main content

umya_spreadsheet/structs/drawing/spreadsheet/
text_body.rs

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