umya_spreadsheet/structs/drawing/
paragraph_properties.rs

1// a:pPr
2use super::super::EnumValue;
3use super::LineSpacing;
4use super::RunProperties;
5use super::TextAlignmentTypeValues;
6use crate::reader::driver::*;
7use crate::writer::driver::*;
8use crate::StringValue;
9use quick_xml::events::{BytesStart, Event};
10use quick_xml::Reader;
11use quick_xml::Writer;
12use std::io::Cursor;
13
14#[derive(Clone, Default, Debug)]
15pub struct ParagraphProperties {
16    right_to_left: StringValue,
17    alignment: EnumValue<TextAlignmentTypeValues>,
18    default_run_properties: Option<Box<RunProperties>>,
19    line_spacing: Option<LineSpacing>,
20}
21
22impl ParagraphProperties {
23    #[inline]
24    pub fn get_right_to_left(&self) -> Option<&str> {
25        self.right_to_left.get_value()
26    }
27
28    #[inline]
29    pub fn set_right_to_left<S: Into<String>>(&mut self, value: S) -> &mut ParagraphProperties {
30        self.right_to_left.set_value(value);
31        self
32    }
33
34    #[inline]
35    pub fn get_alignment(&self) -> &TextAlignmentTypeValues {
36        self.alignment.get_value()
37    }
38
39    #[inline]
40    pub fn set_alignment(&mut self, value: TextAlignmentTypeValues) -> &mut ParagraphProperties {
41        self.alignment.set_value(value);
42        self
43    }
44
45    #[inline]
46    pub fn get_default_run_properties(&self) -> Option<&RunProperties> {
47        self.default_run_properties.as_deref()
48    }
49
50    #[inline]
51    pub fn get_default_run_properties_mut(&mut self) -> Option<&mut RunProperties> {
52        self.default_run_properties.as_deref_mut()
53    }
54
55    #[inline]
56    pub fn set_default_run_properties(&mut self, value: RunProperties) -> &mut ParagraphProperties {
57        self.default_run_properties = Some(Box::new(value));
58        self
59    }
60
61    #[inline]
62    pub fn get_line_spacing(&self) -> Option<&LineSpacing> {
63        self.line_spacing.as_ref()
64    }
65
66    #[inline]
67    pub fn get_line_spacing_mut(&mut self) -> Option<&mut LineSpacing> {
68        self.line_spacing.as_mut()
69    }
70
71    #[inline]
72    pub fn set_line_spacing(&mut self, value: LineSpacing) -> &mut Self {
73        self.line_spacing = Some(value);
74        self
75    }
76
77    pub(crate) fn set_attributes<R: std::io::BufRead>(
78        &mut self,
79        reader: &mut Reader<R>,
80        e: &BytesStart,
81        empty_flag: bool,
82    ) {
83        if let Some(v) = get_attribute(e, b"rtl") {
84            self.set_right_to_left(v);
85        }
86        set_string_from_xml!(self, e, alignment, "algn");
87
88        if empty_flag {
89            return;
90        }
91
92        xml_read_loop!(
93            reader,
94            Event::Start(ref e) => {
95                match e.name().into_inner() {
96                b"a:defRPr" => {
97                    let mut obj = RunProperties::default();
98                    obj.set_attributes(reader, e, false);
99                    self.set_default_run_properties(obj);
100                }
101                b"a:lnSpc" => {
102                    let mut obj = LineSpacing::default();
103                    obj.set_attributes(reader, e);
104                    self.set_line_spacing(obj);
105                }
106                _ => (),
107                }
108            },
109            Event::Empty(ref e) => {
110                if e.name().into_inner() == b"a:defRPr" {
111                    let mut obj = RunProperties::default();
112                    obj.set_attributes(reader, e, true);
113                    self.set_default_run_properties(obj);
114                }
115            },
116            Event::End(ref e) => {
117                if  e.name().into_inner() == b"a:pPr" {
118                    return
119                }
120            },
121            Event::Eof => panic!("Error: Could not find {} end element", "a:pPr")
122        );
123    }
124
125    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
126        // a:pPr
127        let mut attributes: Vec<(&str, &str)> = Vec::new();
128        if let Some(v) = self.right_to_left.get_value() {
129            attributes.push(("rtl", v));
130        }
131        if self.alignment.has_value() {
132            attributes.push(("algn", self.alignment.get_value_string()));
133        }
134
135        let empty_flag = self.default_run_properties.is_none() && self.line_spacing.is_none();
136        write_start_tag(writer, "a:pPr", attributes, empty_flag);
137
138        if !empty_flag {
139            // a:defRPr
140            if let Some(v) = &self.default_run_properties {
141                v.write_to_def_rpr(writer)
142            }
143
144            // a:lnSpc
145            if let Some(v) = &self.line_spacing {
146                v.write_to(writer)
147            }
148
149            write_end_tag(writer, "a:pPr");
150        }
151    }
152}