umya_spreadsheet/structs/drawing/charts/
rich_text.rs

1// c:rich
2use super::super::BodyProperties;
3use super::super::ListStyle;
4use super::super::Paragraph;
5use crate::drawing::Run;
6use crate::writer::driver::*;
7use crate::xml_read_loop;
8use quick_xml::events::{BytesStart, Event};
9use quick_xml::Reader;
10use quick_xml::Writer;
11use std::io::Cursor;
12use thin_vec::ThinVec;
13
14#[derive(Clone, Default, Debug)]
15pub struct RichText {
16    body_properties: BodyProperties,
17    list_style: ListStyle,
18    paragraph: ThinVec<Paragraph>,
19}
20
21impl RichText {
22    #[inline]
23    pub fn set_text<S: Into<String>>(&mut self, value: S) -> &mut Self {
24        self.paragraph.clear();
25        let mut paragraph = Paragraph::default();
26        let mut run = Run::default();
27        run.set_text(value);
28        paragraph.add_run(run);
29        self.add_paragraph(paragraph);
30        self
31    }
32
33    pub fn get_body_properties(&self) -> &BodyProperties {
34        &self.body_properties
35    }
36
37    pub fn get_body_properties_mut(&mut self) -> &mut BodyProperties {
38        &mut self.body_properties
39    }
40
41    pub fn set_body_properties(&mut self, value: BodyProperties) {
42        self.body_properties = value;
43    }
44
45    pub fn get_list_style(&self) -> &ListStyle {
46        &self.list_style
47    }
48
49    pub fn get_list_style_mut(&mut self) -> &mut ListStyle {
50        &mut self.list_style
51    }
52
53    pub fn set_list_style(&mut self, value: ListStyle) {
54        self.list_style = value;
55    }
56
57    pub fn get_paragraph(&self) -> &[Paragraph] {
58        &self.paragraph
59    }
60
61    pub fn get_paragraph_mut(&mut self) -> &mut ThinVec<Paragraph> {
62        &mut self.paragraph
63    }
64
65    pub fn add_paragraph(&mut self, value: Paragraph) {
66        self.paragraph.push(value);
67    }
68
69    pub(crate) fn set_attributes<R: std::io::BufRead>(
70        &mut self,
71        reader: &mut Reader<R>,
72        _e: &BytesStart,
73    ) {
74        xml_read_loop!(
75            reader,
76            Event::Start(ref e) => match e.name().0 {
77                b"a:p" => {
78                    let mut paragraph = Paragraph::default();
79                    paragraph.set_attributes(reader, e);
80                    self.add_paragraph(paragraph);
81                }
82                b"a:bodyPr" => {
83                    let mut body_properties = BodyProperties::default();
84                    body_properties.set_attributes(reader, e, false);
85                    self.set_body_properties(body_properties);
86                }
87                _ => (),
88            },
89            Event::Empty(ref e) => {
90                if e.name().0 == b"a:bodyPr" {
91                    let mut body_properties = BodyProperties::default();
92                    body_properties.set_attributes(reader, e, true);
93                    self.set_body_properties(body_properties);
94                }
95            },
96            Event::End(ref e) => {
97                if e.name().0 == b"c:rich" {
98                    return;
99                }
100            },
101            Event::Eof => panic!("Error: Could not find {} end element", "c:rich"),
102        );
103    }
104
105    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
106        // c:rich
107        write_start_tag(writer, "c:rich", vec![], false);
108
109        // a:bodyPr
110        self.body_properties.write_to(writer);
111
112        // a:lstStyle
113        write_start_tag(writer, "a:lstStyle", vec![], true);
114
115        for content in &self.paragraph {
116            content.write_to(writer);
117        }
118
119        write_end_tag(writer, "c:rich");
120    }
121}