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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//! スライド 1 枚
//!
//! スライドは図形の並びである。文字は図形の中に入っていて、図形の外に文字は置けない。
//! 図形には差し込み口 (`<p:ph>`) の種類が付いていることがあり、題名や本文はそれで見分ける。
//!
//! # 段落は docx と別の型にしてある
//!
//! docx の段落は書式の名前を持ち、pptx の段落は箇条書きの深さを持つ。持ち物が違うので分けてある。
//! 中の [`Run`] は同じものを使う。
use crate::error::Error;
use crate::paragraph::{Align, Run};
use crate::xml::{Event, Reader};
/// 1 インチあたりの長さ。pptx の位置と大きさはこの単位で入っている
pub const INCH: i64 = 914400;
/// スライドの段落
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Paragraph {
/// 箇条書きの深さ。0 がいちばん浅い
pub level: u32,
/// 行のそろえ方
pub align: Align,
/// 中身
pub run: Vec<Run>,
}
impl Paragraph {
/// 中の続きをつないだ文字
pub fn text(&self) -> String {
let mut result = String::new();
for one in &self.run {
result.push_str(&one.text);
}
result
}
}
/// スライドに置かれたもの
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Shape {
/// 図形の名前
pub name: String,
/// 差し込み口の種類 (title、body、subTitle など)。ないときは空
pub place: String,
/// 左上の位置。[`INCH`] 単位。分からないときは 0
pub x: i64,
pub y: i64,
/// 大きさ。[`INCH`] 単位。分からないときは 0
pub width: i64,
pub height: i64,
/// 中の段落
pub paragraph: Vec<Paragraph>,
}
impl Shape {
/// 中の文字を、段落ごとに改行でつないで返す
pub fn body(&self) -> String {
let mut result = String::new();
for one in &self.paragraph {
if !result.is_empty() {
result.push('\n');
}
result.push_str(&one.text());
}
result
}
}
/// スライド 1 枚
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Slide {
pub shape: Vec<Shape>,
}
impl Slide {
/// 中の文字を、図形ごとに改行でつないで返す
pub fn text(&self) -> String {
let mut result = String::new();
for one in &self.shape {
let body = one.body();
if body.is_empty() {
continue;
}
if !result.is_empty() {
result.push('\n');
}
result.push_str(&body);
}
result
}
}
/// スライドのパートを読む
///
/// 組にまとめられた図形 (`<p:grpSp>`) の中身も、同じ並びに入れて返す。
pub fn read(source: &mut Reader) -> Result<Slide, Error> {
let mut result = Slide::default();
while let Some(event) = source.next()? {
if event != Event::Open {
continue;
}
match source.name() {
"sp" => result.shape.push(read_shape(source, "sp")?),
"pic" => result.shape.push(read_shape(source, "pic")?),
"graphicFrame" => result.shape.push(read_shape(source, "graphicFrame")?),
_ => {}
}
}
Ok(result)
}
/// 図形 1 つを、閉じる印まで読む
fn read_shape(source: &mut Reader, close: &str) -> Result<Shape, Error> {
let mut result = Shape::default();
let mut named = false;
let mut placed = false;
let mut sized = false;
while let Some(event) = source.next()? {
if event == Event::Close {
if source.name() == close {
return Ok(result);
}
continue;
}
if event == Event::Text {
continue;
}
match source.name() {
// 図形の名前は最初の cNvPr に入っている。中の図の名前ではない
"cNvPr" if !named => {
result.name = source.attribute("name").unwrap_or("").to_string();
named = true;
}
"ph" if !placed => {
result.place = source.attribute("type").unwrap_or("body").to_string();
placed = true;
}
"off" if !sized => {
result.x = number(source.attribute("x"));
result.y = number(source.attribute("y"));
}
"ext" if !sized => {
result.width = number(source.attribute("cx"));
result.height = number(source.attribute("cy"));
sized = true;
}
"p" if event == Event::Open => result.paragraph.push(read_paragraph(source)?),
_ => {}
}
}
Ok(result)
}
/// `<a:p>` を読んだ直後から `</a:p>` まで読む
fn read_paragraph(source: &mut Reader) -> Result<Paragraph, Error> {
let mut result = Paragraph::default();
let mut decorating = false;
let mut reading = false;
let mut coloring = false;
let mut run: Option<Run> = None;
while let Some(event) = source.next()? {
if event == Event::Text {
if reading {
if let Some(one) = run.as_mut() {
one.text.push_str(source.text());
}
}
continue;
}
if event == Event::Close {
match source.name() {
"p" => {
if let Some(one) = run.take() {
result.run.push(one);
}
return Ok(result);
}
"rPr" | "defRPr" => decorating = false,
"solidFill" => coloring = false,
"t" => reading = false,
"r" | "fld" => {
if let Some(one) = run.take() {
result.run.push(one);
}
}
_ => {}
}
continue;
}
let empty = event == Event::Empty;
match source.name() {
"pPr" => {
result.level = number(source.attribute("lvl")) as u32;
result.align = align(source.attribute("algn"));
}
"r" | "fld" => run = Some(Run::default()),
"rPr" => {
if let Some(one) = run.as_mut() {
decorate(one, source);
}
decorating = !empty && run.is_some();
}
"t" if run.is_some() => reading = !empty,
"solidFill" if decorating => coloring = true,
"srgbClr" if coloring => {
if let Some(one) = run.as_mut() {
one.color = source.attribute("val").unwrap_or("").to_string();
}
}
"latin" if decorating => {
if let Some(one) = run.as_mut() {
one.font = source.attribute("typeface").unwrap_or("").to_string();
}
}
"br" => {
match result.run.last_mut() {
Some(one) => one.text.push('\n'),
None => result.run.push(Run::new("\n")),
}
}
_ => {}
}
}
Ok(result)
}
/// `<a:rPr>` の属性を読む
fn decorate(run: &mut Run, source: &Reader) {
run.bold = flag(source.attribute("b"));
run.italic = flag(source.attribute("i"));
run.strike = matches!(source.attribute("strike"), Some("sngStrike") | Some("dblStrike"));
run.underline = !matches!(source.attribute("u"), None | Some("none"));
// 大きさは 100 分の 1 ポイントで入っている
run.size = number(source.attribute("sz")) as f64 / 100.0;
}
/// `b="1"` のような印を読む
fn flag(value: Option<&str>) -> bool {
matches!(value, Some("1") | Some("true"))
}
/// 数として読む。読めなければ 0
fn number(value: Option<&str>) -> i64 {
match value {
Some(text) => text.trim().parse().unwrap_or(0),
None => 0,
}
}
/// `algn="ctr"` を読む
fn align(value: Option<&str>) -> Align {
match value {
Some("l") => Align::Left,
Some("ctr") => Align::Center,
Some("r") => Align::Right,
Some("just") | Some("dist") => Align::Justify,
_ => Align::None,
}
}