Skip to main content

umya_spreadsheet/structs/drawing/
paragraph.rs

1// a:p
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::{
14    ParagraphProperties,
15    Run,
16    RunProperties,
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 Paragraph {
28    paragraph_properties:    ParagraphProperties,
29    run:                     Vec<Run>,
30    end_para_run_properties: Option<Box<RunProperties>>,
31}
32
33impl Paragraph {
34    #[inline]
35    #[must_use]
36    pub fn paragraph_properties(&self) -> &ParagraphProperties {
37        &self.paragraph_properties
38    }
39
40    #[inline]
41    #[must_use]
42    #[deprecated(since = "3.0.0", note = "Use paragraph_properties()")]
43    pub fn get_paragraph_properties(&self) -> &ParagraphProperties {
44        self.paragraph_properties()
45    }
46
47    #[inline]
48    pub fn paragraph_properties_mut(&mut self) -> &mut ParagraphProperties {
49        &mut self.paragraph_properties
50    }
51
52    #[inline]
53    #[deprecated(since = "3.0.0", note = "Use paragraph_properties_mut()")]
54    pub fn get_paragraph_properties_mut(&mut self) -> &mut ParagraphProperties {
55        self.paragraph_properties_mut()
56    }
57
58    #[inline]
59    pub fn set_paragraph_properties(&mut self, value: ParagraphProperties) -> &mut Self {
60        self.paragraph_properties = value;
61        self
62    }
63
64    #[inline]
65    #[must_use]
66    pub fn run(&self) -> &[Run] {
67        &self.run
68    }
69
70    #[inline]
71    #[must_use]
72    #[deprecated(since = "3.0.0", note = "Use run()")]
73    pub fn get_run(&self) -> &[Run] {
74        self.run()
75    }
76
77    #[inline]
78    pub fn add_run(&mut self, value: Run) {
79        self.run.push(value);
80    }
81
82    #[inline]
83    #[must_use]
84    pub fn end_para_run_properties(&self) -> Option<&RunProperties> {
85        self.end_para_run_properties.as_deref()
86    }
87
88    #[inline]
89    #[must_use]
90    #[deprecated(since = "3.0.0", note = "Use end_para_run_properties()")]
91    pub fn get_end_para_run_properties(&self) -> Option<&RunProperties> {
92        self.end_para_run_properties()
93    }
94
95    #[inline]
96    pub fn end_para_run_properties_mut(&mut self) -> Option<&mut RunProperties> {
97        self.end_para_run_properties.as_deref_mut()
98    }
99
100    #[inline]
101    #[deprecated(since = "3.0.0", note = "Use end_para_run_properties_mut()")]
102    pub fn get_end_para_run_properties_mut(&mut self) -> Option<&mut RunProperties> {
103        self.end_para_run_properties_mut()
104    }
105
106    #[inline]
107    pub fn set_end_para_run_properties(&mut self, value: RunProperties) -> &mut Self {
108        self.end_para_run_properties = Some(Box::new(value));
109        self
110    }
111
112    #[inline]
113    pub fn remove_end_para_run_properties(&mut self) {
114        self.end_para_run_properties = None;
115    }
116
117    pub(crate) fn set_attributes<R: std::io::BufRead>(
118        &mut self,
119        reader: &mut Reader<R>,
120        _e: &BytesStart,
121    ) {
122        xml_read_loop!(
123            reader,
124            Event::Start(ref e) => {
125                match e.name().into_inner() {
126                    b"a:pPr" => {
127                        self.paragraph_properties.set_attributes(reader, e, false);
128                    }
129                    b"a:r" => {
130                        let mut run = Run::default();
131                        run.set_attributes(reader, e);
132                        self.add_run(run);
133                    }
134                    b"a:endParaRPr" => {
135                        let mut run_properties = RunProperties::default();
136                        run_properties.set_attributes(reader, e, false);
137                        self.set_end_para_run_properties(run_properties);
138                    }
139                    _ => (),
140                }
141            },
142            Event::Empty(ref e) => {
143                match e.name().into_inner() {
144                    b"a:pPr" => {
145                        self.paragraph_properties.set_attributes(reader, e, true);
146                    }
147                    b"a:endParaRPr" => {
148                        let mut run_properties = RunProperties::default();
149                        run_properties.set_attributes(reader, e, true);
150                        self.set_end_para_run_properties(run_properties);
151                    }
152                    _ => (),
153                }
154            },
155            Event::End(ref e) => {
156                if e.name().into_inner() == b"a:p" {
157                    return;
158                }
159            },
160            Event::Eof => panic!("Error: Could not find {} end element", "a:p")
161        );
162    }
163
164    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
165        // a:p
166        write_start_tag(writer, "a:p", vec![], false);
167
168        // a:pPr
169        self.paragraph_properties.write_to(writer);
170
171        // a:r
172        for run in &self.run {
173            run.write_to(writer);
174        }
175
176        // a:endParaRPr
177        if let Some(v) = &self.end_para_run_properties {
178            v.write_to_end_para_rpr(writer);
179        }
180
181        write_end_tag(writer, "a:p");
182    }
183}