umya_spreadsheet/structs/drawing/
paragraph.rs

1// a:p
2use super::ParagraphProperties;
3use super::Run;
4use super::RunProperties;
5use crate::reader::driver::*;
6use crate::writer::driver::*;
7use quick_xml::events::{BytesStart, Event};
8use quick_xml::Reader;
9use quick_xml::Writer;
10use std::io::Cursor;
11use thin_vec::ThinVec;
12
13#[derive(Clone, Default, Debug)]
14pub struct Paragraph {
15    paragraph_properties: ParagraphProperties,
16    run: ThinVec<Run>,
17    end_para_run_properties: Option<Box<RunProperties>>,
18}
19
20impl Paragraph {
21    #[inline]
22    pub fn get_paragraph_properties(&self) -> &ParagraphProperties {
23        &self.paragraph_properties
24    }
25
26    #[inline]
27    pub fn get_paragraph_properties_mut(&mut self) -> &mut ParagraphProperties {
28        &mut self.paragraph_properties
29    }
30
31    #[inline]
32    pub fn set_paragraph_properties(&mut self, value: ParagraphProperties) -> &mut Paragraph {
33        self.paragraph_properties = value;
34        self
35    }
36
37    #[inline]
38    pub fn get_run(&self) -> &[Run] {
39        &self.run
40    }
41
42    #[inline]
43    pub fn add_run(&mut self, value: Run) {
44        self.run.push(value);
45    }
46
47    #[inline]
48    pub fn get_end_para_run_properties(&self) -> Option<&RunProperties> {
49        self.end_para_run_properties.as_deref()
50    }
51
52    #[inline]
53    pub fn get_end_para_run_properties_mut(&mut self) -> Option<&mut RunProperties> {
54        self.end_para_run_properties.as_deref_mut()
55    }
56
57    #[inline]
58    pub fn set_end_para_run_properties(&mut self, value: RunProperties) -> &mut Paragraph {
59        self.end_para_run_properties = Some(Box::new(value));
60        self
61    }
62
63    #[inline]
64    pub fn remove_end_para_run_properties(&mut self) {
65        self.end_para_run_properties = None;
66    }
67
68    pub(crate) fn set_attributes<R: std::io::BufRead>(
69        &mut self,
70        reader: &mut Reader<R>,
71        _e: &BytesStart,
72    ) {
73        xml_read_loop!(
74            reader,
75            Event::Start(ref e) => {
76                match e.name().into_inner() {
77                    b"a:pPr" => {
78                        self.paragraph_properties.set_attributes(reader, e, false);
79                    }
80                    b"a:r" => {
81                        let mut run = Run::default();
82                        run.set_attributes(reader, e);
83                        self.add_run(run);
84                    }
85                    b"a:endParaRPr" => {
86                        let mut run_properties = RunProperties::default();
87                        run_properties.set_attributes(reader, e, false);
88                        self.set_end_para_run_properties(run_properties);
89                    }
90                    _ => (),
91                }
92            },
93            Event::Empty(ref e) => {
94                match e.name().into_inner() {
95                    b"a:pPr" => {
96                        self.paragraph_properties.set_attributes(reader, e, true);
97                    }
98                    b"a:endParaRPr" => {
99                        let mut run_properties = RunProperties::default();
100                        run_properties.set_attributes(reader, e, true);
101                        self.set_end_para_run_properties(run_properties);
102                    }
103                    _ => (),
104                }
105            },
106            Event::End(ref e) => {
107                if e.name().into_inner() == b"a:p" {
108                    return;
109                }
110            },
111            Event::Eof => panic!("Error: Could not find {} end element", "a:p")
112        );
113    }
114
115    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
116        // a:p
117        write_start_tag(writer, "a:p", vec![], false);
118
119        // a:pPr
120        self.paragraph_properties.write_to(writer);
121
122        // a:r
123        for run in &self.run {
124            run.write_to(writer);
125        }
126
127        // a:endParaRPr
128        if let Some(v) = &self.end_para_run_properties {
129            v.write_to_end_para_rpr(writer);
130        }
131
132        write_end_tag(writer, "a:p");
133    }
134}