1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! Paragraph type for text paragraphs within a text frame.
use crate::dml::color::ColorFormat;
use crate::enums::text::{PpParagraphAlignment, TextDirection};
use crate::text::bullet::BulletFormat;
use crate::text::font::Font;
use crate::text::run::Run;
use crate::xml_util::{xml_escape, xml_escape_char};
use crate::WriteXml;
/// A paragraph within a text frame, corresponding to the `<a:p>` element.
///
/// Contains runs of text and paragraph-level formatting such as alignment
/// and indent level.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Paragraph {
pub(crate) runs: Vec<Run>,
/// Horizontal alignment.
pub alignment: Option<PpParagraphAlignment>,
/// Indent level (0-8).
pub level: u8,
/// Space before the paragraph, in points.
pub space_before: Option<f64>,
/// Space after the paragraph, in points.
pub space_after: Option<f64>,
/// Line spacing multiplier (e.g. 1.5 for 150% line spacing).
pub line_spacing: Option<f64>,
/// Bullet format for this paragraph.
pub bullet: Option<BulletFormat>,
/// Bullet color (`<a:buClr>`).
pub bullet_color: Option<ColorFormat>,
/// Bullet font typeface (`<a:buFont>`).
pub bullet_font: Option<String>,
/// Bullet size as a percentage of the text size (`<a:buSzPct>`, e.g. 100.0 for 100%).
pub bullet_size_pct: Option<f64>,
/// Bullet size in points (`<a:buSzPts>`, e.g. 12.0 for 12pt).
pub bullet_size_pts: Option<f64>,
/// Default run properties for this paragraph (`<a:defRPr>`).
pub font: Option<Font>,
/// Text direction (LTR or RTL) for this paragraph.
pub text_direction: Option<TextDirection>,
}
impl Paragraph {
/// Create a new empty paragraph.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Return the concatenated text of all runs in this paragraph.
///
/// Line-break runs contribute a `'\n'` character to the output.
#[must_use]
pub fn text(&self) -> String {
self.runs
.iter()
.map(|r| if r.is_line_break { "\n" } else { r.text() })
.collect()
}
/// Return a reference to the runs in this paragraph.
#[must_use]
pub fn runs(&self) -> &[Run] {
&self.runs
}
/// Return a mutable reference to the runs in this paragraph.
pub fn runs_mut(&mut self) -> &mut [Run] {
&mut self.runs
}
/// Append a new empty run and return a mutable reference to it.
///
/// # Panics
///
/// Panics if the internal runs vector is empty after push (should never happen).
pub fn add_run(&mut self) -> &mut Run {
self.runs.push(Run::new());
let idx = self.runs.len() - 1;
&mut self.runs[idx]
}
/// Append a line break element (`<a:br>`) to this paragraph.
///
/// The returned mutable reference allows setting font properties on the
/// line break (which controls the height of the blank line). The font
/// defaults to the paragraph's default font when available.
///
/// # Panics
///
/// Panics if the internal runs vector is empty after push (should never happen).
pub fn add_line_break(&mut self) -> &mut Run {
let mut run = Run::new();
run.is_line_break = true;
if let Some(ref default_font) = self.font {
*run.font_mut() = default_font.clone();
}
self.runs.push(run);
let idx = self.runs.len() - 1;
&mut self.runs[idx]
}
/// Set the horizontal alignment of this paragraph.
pub fn set_alignment(&mut self, align: PpParagraphAlignment) {
self.alignment = Some(align);
}
/// Set the text direction (LTR or RTL) of this paragraph.
pub fn set_text_direction(&mut self, direction: TextDirection) {
self.text_direction = Some(direction);
}
/// Remove all runs from this paragraph (keeps paragraph properties).
pub fn clear(&mut self) {
self.runs.clear();
}
/// Set the bullet format for this paragraph.
pub fn set_bullet(&mut self, bullet: BulletFormat) {
self.bullet = Some(bullet);
}
/// Generate the `<a:p>` XML element string.
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn to_xml_string(&self) -> String {
let mut xml = String::from("<a:p>");
// <a:pPr> - only emit if there are properties to set
let has_ppr = self.alignment.is_some()
|| self.level > 0
|| self.text_direction.is_some()
|| self.space_before.is_some()
|| self.space_after.is_some()
|| self.line_spacing.is_some()
|| self.bullet.is_some()
|| self.bullet_color.is_some()
|| self.bullet_font.is_some()
|| self.bullet_size_pct.is_some()
|| self.bullet_size_pts.is_some()
|| self.font.is_some();
if has_ppr {
xml.push_str("<a:pPr");
if let Some(align) = self.alignment {
xml.push_str(&format!(r#" algn="{}""#, align.to_xml_str()));
}
if self.level > 0 {
xml.push_str(&format!(r#" lvl="{}""#, self.level));
}
if let Some(dir) = self.text_direction {
xml.push_str(&format!(r#" rtl="{}""#, dir.to_xml_attr()));
}
let has_children = self.space_before.is_some()
|| self.space_after.is_some()
|| self.line_spacing.is_some()
|| self.bullet.is_some()
|| self.bullet_color.is_some()
|| self.bullet_font.is_some()
|| self.bullet_size_pct.is_some()
|| self.bullet_size_pts.is_some()
|| self.font.is_some();
if has_children {
xml.push('>');
if let Some(spacing) = self.line_spacing {
// Line spacing as percentage (e.g. 1.5 -> 150000)
#[allow(clippy::cast_possible_truncation)]
// intentional f64→i64 for OOXML units
let val = (spacing * 100_000.0) as i64;
xml.push_str(&format!(r#"<a:lnSpc><a:spcPct val="{val}"/></a:lnSpc>"#));
}
if let Some(before) = self.space_before {
// Space before in hundredths of a point
#[allow(clippy::cast_possible_truncation)]
// intentional f64→i64 for OOXML units
let val = (before * 100.0) as i64;
xml.push_str(&format!(r#"<a:spcBef><a:spcPts val="{val}"/></a:spcBef>"#));
}
if let Some(after) = self.space_after {
// Space after in hundredths of a point
#[allow(clippy::cast_possible_truncation)]
// intentional f64→i64 for OOXML units
let val = (after * 100.0) as i64;
xml.push_str(&format!(r#"<a:spcAft><a:spcPts val="{val}"/></a:spcAft>"#));
}
// Bullet color
if let Some(ref color) = self.bullet_color {
xml.push_str(&format!("<a:buClr>{}</a:buClr>", color.to_xml_string()));
}
// Bullet size
if let Some(pct) = self.bullet_size_pct {
#[allow(clippy::cast_possible_truncation)]
// intentional f64→i64 for OOXML units
let val = (pct * 1000.0) as i64;
xml.push_str(&format!(r#"<a:buSzPct val="{val}"/>"#));
} else if let Some(pts) = self.bullet_size_pts {
#[allow(clippy::cast_possible_truncation)]
// intentional f64→i64 for OOXML units
let val = (pts * 100.0) as i64;
xml.push_str(&format!(r#"<a:buSzPts val="{val}"/>"#));
}
// Bullet font
if let Some(ref font_name) = self.bullet_font {
xml.push_str(&format!(
r#"<a:buFont typeface="{}"/>"#,
xml_escape(font_name)
));
}
// Bullet format
if let Some(ref bullet) = self.bullet {
match bullet {
BulletFormat::Character(ch) => {
xml.push_str(&format!(
r#"<a:buChar char="{}"/>"#,
xml_escape_char(*ch)
));
}
BulletFormat::AutoNumbered(numbering_type) => {
xml.push_str(&format!(
r#"<a:buAutoNum type="{}"/>"#,
xml_escape(numbering_type)
));
}
BulletFormat::Picture(r_id) => {
xml.push_str(&format!(
r#"<a:buBlip><a:blip r:embed="{}"/></a:buBlip>"#,
xml_escape(r_id)
));
}
BulletFormat::None => {
xml.push_str("<a:buNone/>");
}
}
}
// Default run properties
if let Some(ref font) = self.font {
// Reuse Font::to_xml_string() but replace <a:rPr with <a:defRPr
let rpr = font.to_xml_string();
let def_rpr = rpr
.replace("<a:rPr ", "<a:defRPr ")
.replace("<a:rPr/>", "<a:defRPr/>")
.replace("</a:rPr>", "</a:defRPr>");
xml.push_str(&def_rpr);
}
xml.push_str("</a:pPr>");
} else {
xml.push_str("/>");
}
}
// <a:r> elements
for run in &self.runs {
xml.push_str(&run.to_xml_string());
}
xml.push_str("</a:p>");
xml
}
}
#[cfg(test)]
#[path = "paragraph_tests.rs"]
mod tests;