kakemono 0.24.0

Convert PDF, Word, Excel, PowerPoint and EPUB documents and web pages to HTML, Markdown, EPUB, LaTeX and JSON in Rust
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! The text of a shape (`p:txBody`) as paragraphs of `word::ir`: each
//! paragraph with a bullet or a number becomes an item of a list at its
//! level, the others plain paragraphs; runs keep their style and links;
//! fields show their text (a slide number field, the slide's number);
//! formulas (`a14:m`) keep their structure.

use super::inherit::level_props;
use crate::model::Span;
use crate::model::style::{BOLD, ITALIC, SUB, SUP};
use crate::word::docx::Rel;
use crate::word::ir::{Inline, ListMark, Para};
use crate::word::omml;
use crate::word::xml::{DRAWING_NS, MATH_NS, REL_NS, attr, child, children};
use roxmltree::Node;
use std::collections::HashMap;

/// Namespace of the Office 2010 drawing extensions (`a14:`), which hold a
/// paragraph's formulas.
pub(crate) const A14_NS: &str = "http://schemas.microsoft.com/office/drawing/2010/main";
/// PowerPoint's size of text that names none, in points.
pub(crate) const DEFAULT_SIZE_PT: f32 = 18.0;
/// A percentage in DrawingML's units (thousandths of a percent): 100 000
/// is 100 %.
pub(crate) const PERCENT_UNITS: f32 = 100_000.0;
/// The deepest level a paragraph can have (`lvl` 0 to 8).
const LEVELS: usize = 9;

/// What the text of a slide needs besides its XML.
pub(crate) struct TextContext<'r> {
    /// The slide's number in the deck, for slide number fields.
    pub(crate) number: usize,
    /// The slide's relationships, for links.
    pub(crate) rels: &'r HashMap<String, Rel>,
}

/// What marks a paragraph, from the first of its level properties that
/// says.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Bullet {
    /// None (`a:buNone`, or nothing said).
    None,
    /// A character or a picture (`a:buChar`, `a:buBlip`).
    Symbol,
    /// A number (`a:buAutoNum`): its scheme and the number of the first
    /// paragraph.
    Number(String, usize),
}

/// The paragraphs of the text body `body` (`p:txBody`, `a:txBody`), whose
/// levels take their properties from `styles` (most specific first, see
/// `inherit::Inheritance::list_styles`). Empty paragraphs are left out.
pub(crate) fn paragraphs(body: Node<'_, '_>, styles: &[Node<'_, '_>], context: &TextContext<'_>) -> Vec<Para> {
    let mut numbers = Numbers::default();
    let mut out = Vec::new();
    for para in children(body, DRAWING_NS, "p") {
        let props = child(para, DRAWING_NS, "pPr");
        let level = props
            .and_then(|props| props.attribute("lvl"))
            .and_then(|lvl| lvl.parse::<usize>().ok())
            .unwrap_or(0)
            .min(LEVELS - 1);
        let chain = level_props(props, styles, level as u8);
        let inlines = runs(para, &chain, context);
        if inlines_text(&inlines).trim().is_empty()
            && !inlines.iter().any(|inline| matches!(inline, Inline::Math { .. }))
        {
            continue;
        }
        let list = match bullet(&chain) {
            Bullet::None => {
                numbers.plain(level);
                None
            }
            Bullet::Symbol => {
                numbers.plain(level);
                Some(ListMark {
                    level: level as u8,
                    marker: None,
                })
            }
            Bullet::Number(scheme, start) => Some(ListMark {
                level: level as u8,
                marker: Some(numbers.next(level, &scheme, start)),
            }),
        };
        out.push(Para {
            style: if list.is_some() {
                "List Paragraph".to_string()
            } else {
                String::new()
            },
            list,
            inlines,
            ..Default::default()
        });
    }
    out
}

/// The size of the largest text of each paragraph with text of the text
/// body `body`, in points, as PowerPoint shows it: each run's size (`a:rPr
/// sz`), else its level's (`a:defRPr sz` through `styles`), else
/// PowerPoint's default, scaled by the body's autofit (`a:normAutofit
/// fontScale`).
pub(crate) fn paragraph_sizes_pt(body: Node<'_, '_>, styles: &[Node<'_, '_>]) -> Vec<f32> {
    let scale = child(body, DRAWING_NS, "bodyPr")
        .and_then(|props| child(props, DRAWING_NS, "normAutofit"))
        .and_then(|fit| fit.attribute("fontScale"))
        .and_then(|value| value.parse::<f32>().ok())
        .filter(|scale| *scale > 0.0)
        .map_or(1.0, |scale| (scale / PERCENT_UNITS).min(1.0));
    let mut out = Vec::new();
    for para in children(body, DRAWING_NS, "p") {
        let mut largest: Option<f32> = None;
        let props = child(para, DRAWING_NS, "pPr");
        let level = props
            .and_then(|props| props.attribute("lvl"))
            .and_then(|lvl| lvl.parse::<u8>().ok())
            .unwrap_or(0);
        let chain = level_props(props, styles, level);
        let level_size = chain
            .iter()
            .filter_map(|props| child(*props, DRAWING_NS, "defRPr"))
            .find_map(|props| hundredths(props.attribute("sz")));
        for run in children(para, DRAWING_NS, "r") {
            let has_text = child(run, DRAWING_NS, "t")
                .and_then(|node| node.text())
                .is_some_and(|text| !text.trim().is_empty());
            if !has_text {
                continue;
            }
            let size = child(run, DRAWING_NS, "rPr")
                .and_then(|props| hundredths(props.attribute("sz")))
                .or(level_size)
                .unwrap_or(DEFAULT_SIZE_PT)
                * scale;
            largest = Some(largest.map_or(size, |largest: f32| largest.max(size)));
        }
        out.extend(largest);
    }
    out
}

/// A size in hundredths of a point (`sz="2800"`), in points.
fn hundredths(value: Option<&str>) -> Option<f32> {
    value
        .and_then(|value| value.parse::<f32>().ok())
        .filter(|size| size.is_finite() && *size > 0.0)
        .map(|size| size / 100.0)
}

/// The plain text of `inlines`.
pub(crate) fn inlines_text(inlines: &[Inline]) -> String {
    inlines
        .iter()
        .map(|inline| match inline {
            Inline::Text(span) => span.text.as_str(),
            _ => "",
        })
        .collect()
}

/// The numbers of a text body's numbered paragraphs, level by level: a
/// number continues the one before at its level when the scheme is the
/// same, across deeper paragraphs; a paragraph without a number at a level
/// ends the numbering there, and any paragraph ends the deeper ones'.
#[derive(Default)]
pub(crate) struct Numbers {
    /// The scheme and last number of each level.
    levels: [Option<(String, usize)>; LEVELS],
}

impl Numbers {
    /// The marker of a numbered paragraph at `level`.
    pub(crate) fn next(&mut self, level: usize, scheme: &str, start: usize) -> String {
        self.end_deeper(level);
        let number = match &self.levels[level] {
            Some((last_scheme, last)) if last_scheme == scheme => last + 1,
            _ => start,
        };
        self.levels[level] = Some((scheme.to_string(), number));
        marker(scheme, number)
    }

    /// A paragraph without a number at `level`.
    pub(crate) fn plain(&mut self, level: usize) {
        self.end_deeper(level);
        self.levels[level] = None;
    }

    /// Ends the numbering of the levels deeper than `level`.
    fn end_deeper(&mut self, level: usize) {
        for deeper in &mut self.levels[level + 1..] {
            *deeper = None;
        }
    }
}

/// The bullet the first of `chain` that says gives.
pub(crate) fn bullet(chain: &[Node<'_, '_>]) -> Bullet {
    for props in chain {
        for item in props.children().filter(Node::is_element) {
            match item.tag_name().name() {
                "buNone" => return Bullet::None,
                "buChar" | "buBlip" => return Bullet::Symbol,
                "buAutoNum" => {
                    let scheme = item.attribute("type").unwrap_or("arabicPeriod").to_string();
                    let start = item
                        .attribute("startAt")
                        .and_then(|value| value.parse().ok())
                        .filter(|start| *start > 0)
                        .unwrap_or(1);
                    return Bullet::Number(scheme, start);
                }
                _ => {}
            }
        }
    }
    Bullet::None
}

/// The text of the number `number` in the scheme `scheme` (`arabicPeriod`
/// "3.", `alphaLcParenR` "c)", `romanUcParenBoth` "(III)"); a scheme of
/// another script is written with Arabic numerals.
fn marker(scheme: &str, number: usize) -> String {
    let digits = if scheme.starts_with("alphaLc") {
        letters(number)
    } else if scheme.starts_with("alphaUc") {
        letters(number).to_uppercase()
    } else if scheme.starts_with("romanLc") {
        crate::word::roman_lower(number)
    } else if scheme.starts_with("romanUc") {
        crate::word::roman_lower(number).to_uppercase()
    } else {
        number.to_string()
    };
    if scheme.ends_with("ParenBoth") {
        format!("({digits})")
    } else if scheme.ends_with("ParenR") {
        format!("{digits})")
    } else if scheme.ends_with("Plain") {
        digits
    } else {
        format!("{digits}.")
    }
}

/// The letters of `number` as a list counts them: a … z, then aa … zz,
/// aaa …, as PowerPoint and Word do.
fn letters(number: usize) -> String {
    let number = number.max(1);
    let letter = char::from(b'a' + ((number - 1) % 26) as u8);
    std::iter::repeat_n(letter, (number - 1) / 26 + 1).collect()
}

/// The runs, line breaks and fields of the paragraph `para`, as spans.
fn runs(para: Node<'_, '_>, chain: &[Node<'_, '_>], context: &TextContext<'_>) -> Vec<Inline> {
    let defaults = chain
        .iter()
        .filter_map(|props| child(*props, DRAWING_NS, "defRPr"))
        .collect::<Vec<_>>();
    let mut out: Vec<Inline> = Vec::new();
    for item in para.children().filter(Node::is_element) {
        // A formula (`a14:m`, read in the choice of an `mc:AlternateContent`).
        if item.tag_name().namespace() == Some(A14_NS) && item.tag_name().name() == "m" {
            out.extend(
                item.children()
                    .filter(|node| node.tag_name().namespace() == Some(MATH_NS))
                    .map(|node| Inline::Math {
                        node: omml::formula(node),
                        display: node.tag_name().name() == "oMathPara",
                    }),
            );
            continue;
        }
        let text = match item.tag_name().name() {
            // A tab separates words, as in a Word document.
            "r" => child(item, DRAWING_NS, "t")
                .and_then(|node| node.text())
                .unwrap_or_default()
                .replace('\t', " "),
            "fld" => field_text(item, context),
            "br" => "\n".to_string(),
            _ => continue,
        };
        if text.is_empty() {
            continue;
        }
        let props = child(item, DRAWING_NS, "rPr");
        let span = Span {
            href: props.and_then(|props| link(props, context)),
            ..Span::styled(text, run_style(props, &defaults))
        };
        // Neighbouring runs of one style and link make one span.
        match out.last_mut() {
            Some(Inline::Text(last)) if last.style == span.style && last.href == span.href => {
                last.text.push_str(&span.text);
            }
            _ => out.push(Inline::Text(span)),
        }
    }
    out
}

/// The text a field shows: the slide's number for a slide number field,
/// else the text PowerPoint stored (a date as it was last shown).
fn field_text(field: Node<'_, '_>, context: &TextContext<'_>) -> String {
    if field.attribute("type") == Some("slidenum") {
        return context.number.to_string();
    }
    child(field, DRAWING_NS, "t")
        .and_then(|node| node.text())
        .unwrap_or_default()
        .to_string()
}

/// The style bits of a run with the properties `props`, bold and italic
/// inherited from the level's `defaults` when the run does not say.
fn run_style(props: Option<Node<'_, '_>>, defaults: &[Node<'_, '_>]) -> u8 {
    let flag = |name: &str| {
        props
            .into_iter()
            .chain(defaults.iter().copied())
            .find_map(|node| node.attribute(name))
            .is_some_and(|value| matches!(value, "1" | "true"))
    };
    let mut style = 0;
    if flag("b") {
        style |= BOLD;
    }
    if flag("i") {
        style |= ITALIC;
    }
    let baseline = props
        .and_then(|props| props.attribute("baseline"))
        .and_then(|value| value.parse::<i32>().ok())
        .unwrap_or(0);
    if baseline > 0 {
        style |= SUP;
    } else if baseline < 0 {
        style |= SUB;
    }
    style
}

/// The target of a run's link (`a:hlinkClick r:id`): an address outside
/// the deck. A link to another slide or an action has none.
fn link(props: Node<'_, '_>, context: &TextContext<'_>) -> Option<String> {
    let click = child(props, DRAWING_NS, "hlinkClick")?;
    let rel = context.rels.get(attr(click, REL_NS, "id")?)?;
    rel.external.then(|| rel.target.clone())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::word::docx::parse;

    const NS: &str = "xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" \
                      xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"";

    /// The paragraphs of a text body of `paras`, with the list style `style`.
    fn read(paras: &str, style: &str) -> Vec<Para> {
        let xml = format!("<a:txBody {NS}><a:bodyPr/><a:lstStyle>{style}</a:lstStyle>{paras}</a:txBody>");
        let doc = parse(&xml).unwrap();
        let body = doc.root_element();
        let styles: Vec<Node<'_, '_>> = child(body, DRAWING_NS, "lstStyle").into_iter().collect();
        let mut rels = HashMap::new();
        rels.insert(
            "rId1".to_string(),
            Rel {
                target: "https://example.org/".to_string(),
                external: true,
            },
        );
        paragraphs(body, &styles, &TextContext { number: 7, rels: &rels })
    }

    fn markers(paras: &[Para]) -> Vec<Option<(u8, Option<String>)>> {
        paras
            .iter()
            .map(|para| para.list.as_ref().map(|list| (list.level, list.marker.clone())))
            .collect()
    }

    fn numbered(text: &str, kind: &str, lvl: u8, start: Option<u8>) -> String {
        let start = start.map(|start| format!(" startAt=\"{start}\"")).unwrap_or_default();
        format!(
            "<a:p><a:pPr lvl=\"{lvl}\"><a:buAutoNum type=\"{kind}\"{start}/></a:pPr><a:r><a:t>{text}</a:t></a:r></a:p>"
        )
    }

    #[test]
    fn bullet_levels_become_list_levels() {
        let style = "<a:lvl1pPr><a:buChar char=\"•\"/></a:lvl1pPr><a:lvl2pPr><a:buChar char=\"–\"/></a:lvl2pPr>";
        let paras = read(
            "<a:p><a:r><a:t>One</a:t></a:r></a:p><a:p><a:pPr lvl=\"1\"/><a:r><a:t>Two</a:t></a:r></a:p>\
             <a:p><a:pPr lvl=\"2\"/><a:r><a:t>Three</a:t></a:r></a:p>\
             <a:p><a:pPr><a:buNone/></a:pPr><a:r><a:t>Plain</a:t></a:r></a:p><a:p/>",
            style,
        );
        assert_eq!(
            markers(&paras),
            [Some((0, None)), Some((1, None)), None, None],
            "level 3 has no bullet in this style; the empty paragraph is left out"
        );
    }

    #[test]
    fn auto_numbers_keep_their_marker() {
        let paras = read(
            &[
                numbered("a", "arabicPeriod", 0, Some(3)),
                numbered("b", "alphaLcParenR", 1, None),
                numbered("c", "alphaLcParenR", 1, None),
                numbered("d", "arabicPeriod", 0, Some(3)),
                numbered("e", "alphaLcParenR", 1, None),
                numbered("f", "romanUcParenBoth", 0, None),
            ]
            .concat(),
            "",
        );
        let found: Vec<String> = paras
            .iter()
            .map(|para| para.list.as_ref().unwrap().marker.clone().unwrap())
            .collect();
        assert_eq!(found, ["3.", "a)", "b)", "4.", "a)", "(I)"]);
    }

    #[test]
    fn markers_of_large_numbers() {
        assert_eq!(marker("alphaUcPeriod", 28), "BB.");
        assert_eq!(marker("romanLcPlain", 14), "xiv");
        assert_eq!(marker("circleNumDbPlain", 5), "5");
    }

    #[test]
    fn runs_keep_style_links_and_fields() {
        let paras = read(
            "<a:p><a:r><a:rPr b=\"1\"/><a:t>Bo\tld</a:t></a:r><a:r><a:rPr baseline=\"30000\"/><a:t>2</a:t></a:r>\
             <a:br/><a:r><a:rPr><a:hlinkClick r:id=\"rId1\"/></a:rPr><a:t>link</a:t></a:r>\
             <a:fld type=\"slidenum\"><a:t>‹#›</a:t></a:fld></a:p>",
            "<a:lvl1pPr><a:defRPr i=\"1\"/></a:lvl1pPr>",
        );
        let spans: Vec<(String, u8, Option<String>)> = paras[0]
            .inlines
            .iter()
            .map(|inline| match inline {
                Inline::Text(span) => (span.text.clone(), span.style, span.href.clone()),
                _ => unreachable!(),
            })
            .collect();
        assert_eq!(
            spans,
            [
                ("Bo ld".to_string(), BOLD | ITALIC, None),
                ("2".to_string(), ITALIC | SUP, None),
                ("\n".to_string(), ITALIC, None),
                ("link".to_string(), ITALIC, Some("https://example.org/".to_string())),
                ("7".to_string(), ITALIC, None),
            ]
        );
    }
}