dxpdf 0.2.7

A fast DOCX-to-PDF converter powered by Skia
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Integration tests that parse real DOCX files from the test-files directory
//! and validate the resulting Document structure.

use dxpdf::model::*;

const TEST_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/test-files");

fn load(name: &str) -> Document {
    let path = format!("{TEST_DIR}/{name}");
    let data = std::fs::read(&path).unwrap_or_else(|e| panic!("failed to read {path}: {e}"));
    dxpdf::docx::parse(&data).unwrap_or_else(|e| panic!("failed to parse {path}: {e}"))
}

// ── Helpers ──────────────────────────────────────────────────────────────────

fn count_paragraphs(blocks: &[Block]) -> usize {
    blocks
        .iter()
        .map(|b| match b {
            Block::Paragraph(_) => 1,
            Block::Table(t) => t
                .rows
                .iter()
                .flat_map(|r| &r.cells)
                .map(|c| count_paragraphs(&c.content))
                .sum(),
            Block::SectionBreak(_) => 0,
        })
        .sum()
}

fn count_tables(blocks: &[Block]) -> usize {
    blocks
        .iter()
        .map(|b| match b {
            Block::Table(t) => {
                1 + t
                    .rows
                    .iter()
                    .flat_map(|r| &r.cells)
                    .map(|c| count_tables(&c.content))
                    .sum::<usize>()
            }
            _ => 0,
        })
        .sum()
}

fn count_images(blocks: &[Block]) -> usize {
    blocks
        .iter()
        .map(|b| match b {
            Block::Paragraph(p) => count_images_inline(&p.content),
            Block::Table(t) => t
                .rows
                .iter()
                .flat_map(|r| &r.cells)
                .map(|c| count_images(&c.content))
                .sum(),
            Block::SectionBreak(_) => 0,
        })
        .sum()
}

fn count_images_inline(inlines: &[Inline]) -> usize {
    inlines
        .iter()
        .map(|i| match i {
            Inline::Image(_) => 1,
            Inline::Hyperlink(h) => count_images_inline(&h.content),
            _ => 0,
        })
        .sum()
}

fn count_hyperlinks(blocks: &[Block]) -> usize {
    blocks
        .iter()
        .map(|b| match b {
            Block::Paragraph(p) => p
                .content
                .iter()
                .filter(|i| matches!(i, Inline::Hyperlink(_)))
                .count(),
            Block::Table(t) => t
                .rows
                .iter()
                .flat_map(|r| &r.cells)
                .map(|c| count_hyperlinks(&c.content))
                .sum(),
            Block::SectionBreak(_) => 0,
        })
        .sum()
}

fn collect_text(blocks: &[Block]) -> String {
    let mut out = String::new();
    for block in blocks {
        match block {
            Block::Paragraph(p) => {
                for inline in &p.content {
                    if let Inline::TextRun(run) = inline {
                        for elem in &run.content {
                            if let RunElement::Text(t) = elem {
                                out.push_str(t);
                            }
                        }
                    }
                }
                out.push('\n');
            }
            Block::Table(t) => {
                for row in &t.rows {
                    for cell in &row.cells {
                        out.push_str(&collect_text(&cell.content));
                    }
                }
            }
            Block::SectionBreak(_) => {}
        }
    }
    out
}

// ── sample1: richest file (tables, images, hyperlinks, numbering, footnotes) ─

#[test]
fn sample1_parses() {
    let doc = load("sample-docx-files-sample1.docx");
    assert!(!doc.body.is_empty());
}

#[test]
fn sample1_tables() {
    let doc = load("sample-docx-files-sample1.docx");
    let n = count_tables(&doc.body);
    assert!(n >= 6, "expected at least 6 tables, got {n}");
}

#[test]
fn sample1_images() {
    let doc = load("sample-docx-files-sample1.docx");
    assert!(
        doc.media.len() >= 3,
        "expected at least 3 media entries, got {}",
        doc.media.len()
    );
    let n = count_images(&doc.body);
    assert!(n >= 3, "expected at least 3 images in body, got {n}");
}

#[test]
fn sample1_hyperlinks() {
    let doc = load("sample-docx-files-sample1.docx");
    let n = count_hyperlinks(&doc.body);
    assert!(n >= 10, "expected at least 10 hyperlinks, got {n}");
}

#[test]
fn sample1_footnotes_endnotes() {
    let doc = load("sample-docx-files-sample1.docx");
    let _ = doc.footnotes.len() + doc.endnotes.len();
}

#[test]
fn sample1_numbering() {
    let doc = load("sample-docx-files-sample1.docx");
    let has_numbering = doc
        .body
        .iter()
        .any(|b| matches!(b, Block::Paragraph(p) if p.properties.numbering.is_some()));
    assert!(has_numbering, "expected at least one numbered paragraph");
}

#[test]
fn sample1_theme() {
    let doc = load("sample-docx-files-sample1.docx");
    let theme = doc.theme.as_ref().expect("expected theme");
    assert!(
        !theme.minor_font.latin.is_empty(),
        "theme should have minor latin font"
    );
}

#[test]
fn sample1_has_direct_formatting() {
    let doc = load("sample-docx-files-sample1.docx");
    let mut sizes = std::collections::HashSet::new();
    for block in &doc.body {
        if let Block::Paragraph(p) = block {
            for inline in &p.content {
                if let Inline::TextRun(run) = inline {
                    if let Some(sz) = run.properties.font_size {
                        sizes.insert(sz.raw());
                    }
                }
            }
        }
    }
    assert!(
        !sizes.is_empty(),
        "expected some runs to have explicit font sizes"
    );
}

// ── sample2: minimal file (5 paragraphs, 1 image) ───────────────────────────

#[test]
fn sample2_parses() {
    let doc = load("sample-docx-files-sample2.docx");
    assert!(!doc.body.is_empty());
}

#[test]
fn sample2_small_body() {
    let doc = load("sample-docx-files-sample2.docx");
    let n = count_paragraphs(&doc.body);
    assert!(n >= 3, "expected at least 3 paragraphs, got {n}");
    assert_eq!(count_tables(&doc.body), 0, "expected no tables");
}

#[test]
fn sample2_single_image() {
    let doc = load("sample-docx-files-sample2.docx");
    assert_eq!(doc.media.len(), 1, "expected 1 media entry");
    let n = count_images(&doc.body);
    assert_eq!(n, 1, "expected 1 image in body");
}

// ── sample3: tables + hyperlinks + numbering ─────────────────────────────────

#[test]
fn sample3_parses() {
    let doc = load("sample-docx-files-sample3.docx");
    assert!(!doc.body.is_empty());
}

#[test]
fn sample3_tables() {
    let doc = load("sample-docx-files-sample3.docx");
    let n = count_tables(&doc.body);
    assert!(n >= 2, "expected at least 2 tables, got {n}");
}

#[test]
fn sample3_images() {
    let doc = load("sample-docx-files-sample3.docx");
    assert_eq!(doc.media.len(), 2, "expected 2 media entries");
}

#[test]
fn sample3_hyperlinks() {
    let doc = load("sample-docx-files-sample3.docx");
    let n = count_hyperlinks(&doc.body);
    assert!(n >= 3, "expected at least 3 hyperlinks, got {n}");
}

#[test]
fn sample3_numbering() {
    let doc = load("sample-docx-files-sample3.docx");
    let has_numbering = doc
        .body
        .iter()
        .any(|b| matches!(b, Block::Paragraph(p) if p.properties.numbering.is_some()));
    assert!(has_numbering, "expected numbered paragraphs");
}

// ── sample-4: text-only with header ──────────────────────────────────────────

#[test]
fn sample_4_parses() {
    let doc = load("sample-docx-files-sample-4.docx");
    assert!(!doc.body.is_empty());
}

#[test]
fn sample_4_has_header() {
    let doc = load("sample-docx-files-sample-4.docx");
    assert!(
        !doc.headers.is_empty(),
        "expected at least one header, got none"
    );
}

#[test]
fn sample_4_no_images_or_tables() {
    let doc = load("sample-docx-files-sample-4.docx");
    assert_eq!(count_tables(&doc.body), 0);
    assert_eq!(count_images(&doc.body), 0);
    assert!(doc.media.is_empty());
}

#[test]
fn sample_4_paragraph_count() {
    let doc = load("sample-docx-files-sample-4.docx");
    let n = count_paragraphs(&doc.body);
    assert!(n >= 50, "expected at least 50 paragraphs, got {n}");
}

// ── sample-5: large text-only document ───────────────────────────────────────

#[test]
fn sample_5_parses() {
    let doc = load("sample-docx-files-sample-5.docx");
    assert!(!doc.body.is_empty());
}

#[test]
fn sample_5_many_paragraphs() {
    let doc = load("sample-docx-files-sample-5.docx");
    let n = count_paragraphs(&doc.body);
    assert!(n >= 500, "expected at least 500 paragraphs, got {n}");
}

#[test]
fn sample_5_text_only() {
    let doc = load("sample-docx-files-sample-5.docx");
    assert_eq!(count_tables(&doc.body), 0);
    assert_eq!(count_images(&doc.body), 0);
    assert!(doc.media.is_empty());
    assert!(doc.headers.is_empty());
    assert!(doc.footers.is_empty());
}

// ── sample-6: very large text-only document ──────────────────────────────────

#[test]
fn sample_6_parses() {
    let doc = load("sample-docx-files-sample-6.docx");
    assert!(!doc.body.is_empty());
}

#[test]
fn sample_6_many_paragraphs() {
    let doc = load("sample-docx-files-sample-6.docx");
    let n = count_paragraphs(&doc.body);
    assert!(n >= 2000, "expected at least 2000 paragraphs, got {n}");
}

// ── sample4: image-heavy (47 images) ─────────────────────────────────────────

#[test]
fn sample4_parses() {
    let doc = load("sample-docx-files-sample4.docx");
    assert!(!doc.body.is_empty());
}

#[test]
fn sample4_many_images() {
    let doc = load("sample-docx-files-sample4.docx");
    assert!(
        doc.media.len() >= 40,
        "expected at least 40 media entries, got {}",
        doc.media.len()
    );
}

#[test]
fn sample4_image_extents() {
    let doc = load("sample-docx-files-sample4.docx");
    let n = count_images(&doc.body);
    assert!(n >= 30, "expected at least 30 images in body, got {n}");

    // All images should have non-zero extents
    fn check(blocks: &[Block]) {
        for block in blocks {
            match block {
                Block::Paragraph(p) => {
                    for inline in &p.content {
                        if let Inline::Image(img) = inline {
                            assert!(
                                img.extent.width.raw() > 0 && img.extent.height.raw() > 0,
                                "image should have non-zero extent"
                            );
                        }
                    }
                }
                Block::Table(t) => {
                    for row in &t.rows {
                        for cell in &row.cells {
                            check(&cell.content);
                        }
                    }
                }
                Block::SectionBreak(_) => {}
            }
        }
    }
    check(&doc.body);
}

// ── Cross-cutting tests ──────────────────────────────────────────────────────

const ALL_FILES: &[&str] = &[
    "sample-docx-files-sample1.docx",
    "sample-docx-files-sample2.docx",
    "sample-docx-files-sample3.docx",
    "sample-docx-files-sample4.docx",
    "sample-docx-files-sample-4.docx",
    "sample-docx-files-sample-5.docx",
    "sample-docx-files-sample-6.docx",
];

#[test]
fn all_files_parse_without_error() {
    for name in ALL_FILES {
        let _ = load(name);
    }
}

#[test]
fn all_files_have_body_text() {
    for name in ALL_FILES {
        let doc = load(name);
        let text = collect_text(&doc.body);
        assert!(
            !text.trim().is_empty(),
            "{name}: document should contain text"
        );
    }
}

#[test]
fn all_files_have_page_size() {
    for name in ALL_FILES {
        let doc = load(name);
        assert!(
            doc.final_section.page_size.is_some(),
            "{name}: final section should have page size"
        );
    }
}

#[test]
fn all_files_have_margins() {
    for name in ALL_FILES {
        let doc = load(name);
        assert!(
            doc.final_section.page_margins.is_some(),
            "{name}: final section should have page margins"
        );
    }
}

#[test]
fn all_files_have_default_tab_stop() {
    for name in ALL_FILES {
        let doc = load(name);
        let tab = doc.settings.default_tab_stop.raw();
        assert!(tab > 0, "{name}: expected non-zero default tab stop");
    }
}

#[test]
fn table_rows_have_cells() {
    for name in ALL_FILES {
        let doc = load(name);
        fn check(blocks: &[Block], name: &str) {
            for block in blocks {
                if let Block::Table(t) = block {
                    for (i, row) in t.rows.iter().enumerate() {
                        assert!(!row.cells.is_empty(), "{name}: table row {i} has no cells");
                        for cell in &row.cells {
                            check(&cell.content, name);
                        }
                    }
                }
            }
        }
        check(&doc.body, name);
    }
}

#[test]
fn table_cells_have_content() {
    for name in ALL_FILES {
        let doc = load(name);
        fn count_empty(blocks: &[Block]) -> (usize, usize) {
            let mut total = 0usize;
            let mut empty = 0usize;
            for block in blocks {
                if let Block::Table(t) = block {
                    for row in &t.rows {
                        for cell in &row.cells {
                            total += 1;
                            if cell.content.is_empty() {
                                empty += 1;
                            }
                            let (t2, e2) = count_empty(&cell.content);
                            total += t2;
                            empty += e2;
                        }
                    }
                }
            }
            (total, empty)
        }
        let (total, empty) = count_empty(&doc.body);
        if total > 0 {
            assert!(
                empty <= total / 2,
                "{name}: too many empty cells ({empty}/{total})"
            );
        }
    }
}

#[test]
fn media_bytes_are_nonempty() {
    for name in ALL_FILES {
        let doc = load(name);
        for (rel_id, (data, _fmt)) in &doc.media {
            assert!(!data.is_empty(), "{name}: media {rel_id:?} has empty bytes");
        }
    }
}