Skip to main content

umya_spreadsheet/structs/drawing/
paragraph_properties.rs

1// a:pPr
2use std::io::Cursor;
3
4use quick_xml::{
5    Reader,
6    Writer,
7    events::{
8        BytesStart,
9        Event,
10    },
11};
12
13use super::{
14    super::EnumValue,
15    LineSpacing,
16    RunProperties,
17    TextAlignmentTypeValues,
18};
19use crate::{
20    StringValue,
21    reader::driver::{
22        get_attribute,
23        set_string_from_xml,
24        xml_read_loop,
25    },
26    writer::driver::{
27        write_end_tag,
28        write_start_tag,
29    },
30};
31
32#[derive(Clone, Default, Debug)]
33pub struct ParagraphProperties {
34    right_to_left:          StringValue,
35    alignment:              EnumValue<TextAlignmentTypeValues>,
36    default_run_properties: Option<Box<RunProperties>>,
37    line_spacing:           Option<LineSpacing>,
38}
39
40impl ParagraphProperties {
41    #[inline]
42    #[must_use]
43    pub fn right_to_left(&self) -> Option<&str> {
44        self.right_to_left.value()
45    }
46
47    #[inline]
48    #[must_use]
49    #[deprecated(since = "3.0.0", note = "Use right_to_left()")]
50    pub fn get_right_to_left(&self) -> Option<&str> {
51        self.right_to_left()
52    }
53
54    #[inline]
55    pub fn set_right_to_left<S: Into<String>>(&mut self, value: S) -> &mut Self {
56        self.right_to_left.set_value(value);
57        self
58    }
59
60    #[inline]
61    #[must_use]
62    pub fn alignment(&self) -> &TextAlignmentTypeValues {
63        self.alignment.value()
64    }
65
66    #[inline]
67    #[must_use]
68    #[deprecated(since = "3.0.0", note = "Use alignment()")]
69    pub fn get_alignment(&self) -> &TextAlignmentTypeValues {
70        self.alignment()
71    }
72
73    #[inline]
74    pub fn set_alignment(&mut self, value: TextAlignmentTypeValues) -> &mut Self {
75        self.alignment.set_value(value);
76        self
77    }
78
79    #[inline]
80    #[must_use]
81    pub fn default_run_properties(&self) -> Option<&RunProperties> {
82        self.default_run_properties.as_deref()
83    }
84
85    #[inline]
86    #[must_use]
87    #[deprecated(since = "3.0.0", note = "Use default_run_properties()")]
88    pub fn get_default_run_properties(&self) -> Option<&RunProperties> {
89        self.default_run_properties()
90    }
91
92    #[inline]
93    pub fn default_run_properties_mut(&mut self) -> Option<&mut RunProperties> {
94        self.default_run_properties.as_deref_mut()
95    }
96
97    #[inline]
98    #[deprecated(since = "3.0.0", note = "Use default_run_properties_mut()")]
99    pub fn get_default_run_properties_mut(&mut self) -> Option<&mut RunProperties> {
100        self.default_run_properties_mut()
101    }
102
103    #[inline]
104    pub fn set_default_run_properties(&mut self, value: RunProperties) -> &mut Self {
105        self.default_run_properties = Some(Box::new(value));
106        self
107    }
108
109    #[inline]
110    #[must_use]
111    pub fn line_spacing(&self) -> Option<&LineSpacing> {
112        self.line_spacing.as_ref()
113    }
114
115    #[inline]
116    #[must_use]
117    #[deprecated(since = "3.0.0", note = "Use line_spacing()")]
118    pub fn get_line_spacing(&self) -> Option<&LineSpacing> {
119        self.line_spacing()
120    }
121
122    #[inline]
123    pub fn line_spacing_mut(&mut self) -> Option<&mut LineSpacing> {
124        self.line_spacing.as_mut()
125    }
126
127    #[inline]
128    #[deprecated(since = "3.0.0", note = "Use line_spacing_mut()")]
129    pub fn get_line_spacing_mut(&mut self) -> Option<&mut LineSpacing> {
130        self.line_spacing_mut()
131    }
132
133    #[inline]
134    pub fn set_line_spacing(&mut self, value: LineSpacing) -> &mut Self {
135        self.line_spacing = Some(value);
136        self
137    }
138
139    pub(crate) fn set_attributes<R: std::io::BufRead>(
140        &mut self,
141        reader: &mut Reader<R>,
142        e: &BytesStart,
143        empty_flag: bool,
144    ) {
145        if let Some(v) = get_attribute(e, b"rtl") {
146            self.set_right_to_left(v);
147        }
148        set_string_from_xml!(self, e, alignment, "algn");
149
150        if empty_flag {
151            return;
152        }
153
154        xml_read_loop!(
155            reader,
156            ref n @ (Event::Empty(ref e) | Event::Start(ref e)) => {
157                let is_empty = matches!(n, Event::Empty(_));
158                match e.name().into_inner() {
159                b"a:defRPr" => {
160                    let mut obj = RunProperties::default();
161                    obj.set_attributes(reader, e, is_empty);
162                    self.set_default_run_properties(obj);
163                }
164                b"a:lnSpc" => {
165                    let mut obj = LineSpacing::default();
166                    obj.set_attributes(reader, e);
167                    self.set_line_spacing(obj);
168                }
169                _ => (),
170                }
171            },
172            Event::End(ref e) => {
173                if  e.name().into_inner() == b"a:pPr" {
174                    return
175                }
176            },
177            Event::Eof => panic!("Error: Could not find {} end element", "a:pPr")
178        );
179    }
180
181    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
182        // a:pPr
183        let mut attributes: crate::structs::AttrCollection = Vec::new();
184        if let Some(v) = self.right_to_left.value() {
185            attributes.push(("rtl", v).into());
186        }
187        if self.alignment.has_value() {
188            attributes.push(("algn", self.alignment.value_string()).into());
189        }
190
191        let empty_flag = self.default_run_properties.is_none() && self.line_spacing.is_none();
192        write_start_tag(writer, "a:pPr", attributes, empty_flag);
193
194        if !empty_flag {
195            // a:defRPr
196            if let Some(v) = &self.default_run_properties {
197                v.write_to_def_rpr(writer);
198            }
199
200            // a:lnSpc
201            if let Some(v) = &self.line_spacing {
202                v.write_to(writer);
203            }
204
205            write_end_tag(writer, "a:pPr");
206        }
207    }
208}