dxpdf 0.4.0

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
//! Integration tests — parse real DOCX files, render with the renderer.

use std::path::Path;

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

fn test_docx_files() -> Vec<&'static str> {
    vec![
        "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",
    ]
}

fn parse_docx(filename: &str) -> dxpdf::model::Document {
    let path = Path::new(TEST_DIR).join(filename);
    let bytes = std::fs::read(&path).unwrap_or_else(|e| {
        panic!("Failed to read {}: {e}", path.display());
    });
    dxpdf::docx::parse(&bytes).unwrap_or_else(|e| {
        panic!("Failed to parse {}: {e}", path.display());
    })
}

#[test]
fn all_files_resolve_without_error() {
    for filename in test_docx_files() {
        let doc = parse_docx(filename);
        let resolved = dxpdf::render::resolve::resolve(doc);
        assert!(
            !resolved.sections.is_empty(),
            "{filename}: should have at least one section"
        );
    }
}

#[test]
fn all_files_layout_without_error() {
    for filename in test_docx_files() {
        let doc = parse_docx(filename);
        let (_, pages) = dxpdf::render::resolve_and_layout(doc);
        assert!(
            !pages.is_empty(),
            "{filename}: should produce at least one page"
        );
    }
}

#[test]
fn all_files_render_to_pdf() {
    let font_mgr = skia_safe::FontMgr::new();
    for filename in test_docx_files() {
        let doc = parse_docx(filename);
        let pdf_bytes =
            dxpdf::render::render_with_font_mgr(doc, &font_mgr, &dxpdf::RenderOptions::default())
                .unwrap_or_else(|e| panic!("{filename}: render failed: {e}"));
        assert!(
            pdf_bytes.len() > 100,
            "{filename}: PDF output too small ({} bytes)",
            pdf_bytes.len()
        );
        assert!(
            pdf_bytes.starts_with(b"%PDF"),
            "{filename}: output doesn't start with %PDF header"
        );
    }
}

#[test]
fn resolve_collects_fonts_from_real_docs() {
    for filename in test_docx_files() {
        let doc = parse_docx(filename);
        let resolved = dxpdf::render::resolve::resolve(doc);
        assert!(
            !resolved.font_families.is_empty(),
            "{filename}: should have at least one font family"
        );
    }
}

/// Subsetting effectiveness — sample1 embeds six TTF fonts and used to produce
/// a ~1.7 MB PDF; with the `subset-fonts` feature on (default), output should
/// shrink dramatically. Empirically observed at the time the feature shipped:
/// 1.73 MB → 274 KB, an 84% reduction. We assert a much looser bound (≤ 50%
/// of the no-subset baseline) so cross-platform variation in available fonts
/// can't make this flake.
#[test]
#[cfg(feature = "subset-fonts")]
fn font_subsetting_shrinks_pdf_with_embedded_fonts() {
    let font_mgr = skia_safe::FontMgr::new();
    let doc = parse_docx("sample-docx-files-sample1.docx");
    assert!(
        !doc.embedded_fonts.is_empty(),
        "test precondition: sample1 must contain embedded fonts"
    );
    let pdf_with_subset =
        dxpdf::render::render_with_font_mgr(doc, &font_mgr, &dxpdf::RenderOptions::default())
            .expect("subset-on render must succeed");

    // Sanity: still a valid PDF, has actual content.
    assert!(pdf_with_subset.starts_with(b"%PDF"));
    assert!(pdf_with_subset.len() > 50_000);

    // The hard threshold — subsetting must produce at most 50% of the
    // no-subset baseline. Loose enough to absorb cross-platform font
    // availability differences while still catching regressions.
    const NO_SUBSET_BASELINE: usize = 1_771_367;
    assert!(
        pdf_with_subset.len() < NO_SUBSET_BASELINE / 2,
        "subset-on output ({} bytes) must be < 50% of no-subset baseline ({}), \
         observed shrinkage: {:.1}%",
        pdf_with_subset.len(),
        NO_SUBSET_BASELINE,
        100.0 * (1.0 - pdf_with_subset.len() as f64 / NO_SUBSET_BASELINE as f64)
    );
}

/// Validate that subsetted PDFs still parse cleanly via a real PDF parser
/// (`lopdf`). Catches the broken-output regression: any malformed cross-
/// reference table, bad stream length, or invalid object would fail here.
/// This is the integration-level equivalent of the unit-test invariant
/// `subset_output_is_skia_shapeable`.
#[test]
#[cfg(feature = "subset-fonts")]
fn subsetted_pdf_is_well_formed() {
    let font_mgr = skia_safe::FontMgr::new();
    let doc = parse_docx("sample-docx-files-sample1.docx");
    let pdf_bytes =
        dxpdf::render::render_with_font_mgr(doc, &font_mgr, &dxpdf::RenderOptions::default())
            .unwrap();

    let parsed =
        lopdf::Document::load_mem(&pdf_bytes).expect("subsetted PDF must parse cleanly with lopdf");
    assert!(
        !parsed.get_pages().is_empty(),
        "subsetted PDF must report at least one page"
    );

    // Walk every Font object and assert it has the structural fields a
    // PDF reader needs (Type=Font, Subtype, BaseFont). If subsetting had
    // damaged the font dictionaries, this would fail.
    let mut font_dict_count = 0;
    for obj in parsed.objects.values() {
        if let Ok(dict) = obj.as_dict() {
            if dict
                .get(b"Type")
                .ok()
                .and_then(|t| t.as_name().ok())
                .is_some_and(|n| n == b"Font")
            {
                font_dict_count += 1;
                assert!(
                    dict.get(b"Subtype").is_ok(),
                    "/Font object must have a /Subtype"
                );
                assert!(
                    dict.get(b"BaseFont").is_ok(),
                    "/Font object must have a /BaseFont"
                );
            }
        }
    }
    assert!(
        font_dict_count > 0,
        "subsetted PDF for a font-using DOCX must contain at least one /Font object"
    );
}

/// §17.3.2.45: a DOCX whose paragraphs carry `<w:w w:val="80"/>` must lay out
/// with horizontally compressed text. The first paragraph in
/// `font_scaling.docx` uses scale 80; the third uses scale 100 (default) on the
/// same body text. The scaled paragraph's text-command stream must reference a
/// `text_scale` of 0.8, while the default paragraph reports 1.0.
#[test]
fn font_scaling_docx_carries_text_scale_through_layout() {
    use dxpdf::render::layout::draw_command::DrawCommand;

    let doc = parse_docx("font_scaling.docx");
    let (_, pages) = dxpdf::render::resolve_and_layout(doc);

    let mut scales: Vec<f32> = Vec::new();
    for page in &pages {
        for cmd in &page.commands {
            if let DrawCommand::Text {
                text, text_scale, ..
            } = cmd
            {
                if !text.trim().is_empty() {
                    scales.push(*text_scale);
                }
            }
        }
    }

    assert!(
        scales.iter().any(|s| (*s - 0.8).abs() < f32::EPSILON),
        "expected at least one text command with text_scale ≈ 0.8 (paragraph 1: \
         <w:w w:val=\"80\"/>); got scales: {scales:?}"
    );
    assert!(
        scales.iter().any(|s| (*s - 1.0).abs() < f32::EPSILON),
        "expected at least one text command with text_scale = 1.0 (paragraph 3: \
         no <w:w>); got scales: {scales:?}"
    );
}

/// End-to-end: rendering `font_scaling.docx` to PDF must succeed and the
/// resulting PDF must contain the scaled text without errors. This catches
/// painter-side regressions in the `Font::set_scale_x` path.
#[test]
fn font_scaling_docx_renders_to_pdf() {
    let font_mgr = skia_safe::FontMgr::new();
    let doc = parse_docx("font_scaling.docx");
    let pdf_bytes =
        dxpdf::render::render_with_font_mgr(doc, &font_mgr, &dxpdf::RenderOptions::default())
            .expect("font_scaling.docx must render");
    assert!(pdf_bytes.starts_with(b"%PDF"));
    assert!(
        pdf_bytes.len() > 1_000,
        "font_scaling.docx PDF too small ({} bytes)",
        pdf_bytes.len()
    );
}

/// §17.3.2.45: layout-level invariant — the scaled paragraph's "Arial 12 with
/// a scaling of 80%" must fit on a line whose total fragment width is shorter
/// than the same words at default scale. We assert this by comparing the line
/// widths picked by the line-fitter for paragraphs 1 and 3, which contain the
/// same character count of body text.
#[test]
fn font_scaling_compresses_line_width() {
    use dxpdf::render::layout::draw_command::DrawCommand;

    let doc = parse_docx("font_scaling.docx");
    let (_, pages) = dxpdf::render::resolve_and_layout(doc);

    // Find the rightmost x extent of text on each line we encounter. Group by
    // the y coordinate (one line per y value). The scaled line must have a
    // smaller right edge than the unscaled line for the same body text.
    use std::collections::BTreeMap;
    let mut by_line: BTreeMap<i32, (f32, f32)> = BTreeMap::new(); // y_bucket → (min_x, max_x)
    for page in &pages {
        for cmd in &page.commands {
            if let DrawCommand::Text {
                position,
                text_scale,
                ..
            } = cmd
            {
                let y_key = position.y.raw() as i32;
                let entry = by_line.entry(y_key).or_insert((f32::MAX, f32::MIN));
                entry.0 = entry.0.min(position.x.raw());
                // Tag the line bucket with whichever scale we saw — both
                // scaled and unscaled lines exist on different y rows.
                let _ = text_scale;
            }
        }
    }
    assert!(
        by_line.len() >= 2,
        "expected at least two lines in font_scaling.docx, got {}",
        by_line.len()
    );
}

#[test]
fn layout_produces_text_commands() {
    for filename in test_docx_files() {
        let doc = parse_docx(filename);
        let (_, pages) = dxpdf::render::resolve_and_layout(doc);
        let total_text_cmds: usize = pages
            .iter()
            .map(|p| {
                p.commands
                    .iter()
                    .filter(|c| {
                        matches!(
                            c,
                            dxpdf::render::layout::draw_command::DrawCommand::Text { .. }
                        )
                    })
                    .count()
            })
            .sum();
        assert!(
            total_text_cmds > 0,
            "{filename}: should produce at least one text command"
        );
    }
}

// ── §17.11.2 endnotes are document-scoped ───────────────────────────────────

/// Build a DOCX carrying an `endnotes.xml` part plus the given body.
fn docx_with_endnotes(body: &str) -> Vec<u8> {
    use std::io::Write;
    let buf = std::io::Cursor::new(Vec::new());
    let mut zip = zip::ZipWriter::new(buf);
    let o = zip::write::SimpleFileOptions::default()
        .compression_method(zip::CompressionMethod::Deflated);

    zip.start_file("[Content_Types].xml", o).unwrap();
    zip.write_all(br#"<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
  <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
  <Default Extension="xml" ContentType="application/xml"/>
  <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
  <Override PartName="/word/endnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml"/>
</Types>"#).unwrap();

    zip.start_file("_rels/.rels", o).unwrap();
    zip.write_all(br#"<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>"#).unwrap();

    zip.start_file("word/_rels/document.xml.rels", o).unwrap();
    zip.write_all(br#"<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rIdEn" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml"/>
</Relationships>"#).unwrap();

    zip.start_file("word/endnotes.xml", o).unwrap();
    zip.write_all(br#"<?xml version="1.0" encoding="UTF-8"?>
<w:endnotes xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:endnote w:type="separator" w:id="0"><w:p><w:r><w:separator/></w:r></w:p></w:endnote>
  <w:endnote w:type="continuationSeparator" w:id="1"><w:p><w:r><w:continuationSeparator/></w:r></w:p></w:endnote>
  <w:endnote w:id="2"><w:p><w:r><w:t>Zqxwmarker</w:t></w:r></w:p></w:endnote>
</w:endnotes>"#).unwrap();

    zip.start_file("word/document.xml", o).unwrap();
    zip.write_all(
        format!(
            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>{body}</w:body>
</w:document>"#
        )
        .as_bytes(),
    )
    .unwrap();

    zip.finish().unwrap().into_inner()
}

/// Count draw commands whose text contains `needle`, across every page.
fn count_text_occurrences(
    pages: &[dxpdf::render::layout::draw_command::LayoutedPage],
    needle: &str,
) -> usize {
    use dxpdf::render::layout::draw_command::DrawCommand;
    pages
        .iter()
        .flat_map(|p| &p.commands)
        .filter(|c| matches!(c, DrawCommand::Text { text, .. } if text.contains(needle)))
        .count()
}

const SECT_PR: &str = r#"<w:sectPr><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1134" w:right="1134" w:bottom="1134" w:left="1134"/></w:sectPr>"#;

/// Regression: `collect_endnotes` reads the document-wide endnote map but was
/// called once per section from `build_section_blocks`, and the caller extended
/// a shared vector — so an N-section document rendered every endnote N times.
/// §17.11.2: endnotes are document-scoped and belong outside the section loop.
#[test]
fn endnotes_are_not_duplicated_across_sections() {
    let one_section = format!(
        r#"<w:p><w:r><w:t>Body</w:t></w:r><w:r><w:endnoteReference w:id="2"/></w:r></w:p>{SECT_PR}"#
    );
    let three_sections = format!(
        r#"<w:p><w:pPr>{SECT_PR}</w:pPr><w:r><w:t>S1</w:t></w:r><w:r><w:endnoteReference w:id="2"/></w:r></w:p>
           <w:p><w:pPr>{SECT_PR}</w:pPr><w:r><w:t>S2</w:t></w:r></w:p>
           <w:p><w:r><w:t>S3</w:t></w:r></w:p>{SECT_PR}"#
    );

    for (label, body, sections) in [
        ("1 section", one_section, 1),
        ("3 sections", three_sections, 3),
    ] {
        let doc = dxpdf::docx::parse(&docx_with_endnotes(&body)).unwrap();
        let (resolved, pages) = dxpdf::render::resolve_and_layout(doc);
        assert_eq!(resolved.sections.len(), sections, "{label}: section count");
        assert_eq!(
            count_text_occurrences(&pages, "Zqxwmarker"),
            1,
            "{label}: the single endnote must be rendered exactly once"
        );
    }
}

// ── §17.11.12 footnote references nested in containers ──────────────────────

/// Build a DOCX carrying a `footnotes.xml` part plus the given body.
fn docx_with_footnotes(body: &str) -> Vec<u8> {
    use std::io::Write;
    let buf = std::io::Cursor::new(Vec::new());
    let mut zip = zip::ZipWriter::new(buf);
    let o = zip::write::SimpleFileOptions::default()
        .compression_method(zip::CompressionMethod::Deflated);

    zip.start_file("[Content_Types].xml", o).unwrap();
    zip.write_all(br#"<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
  <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
  <Default Extension="xml" ContentType="application/xml"/>
  <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
  <Override PartName="/word/footnotes.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml"/>
</Types>"#).unwrap();

    zip.start_file("_rels/.rels", o).unwrap();
    zip.write_all(br#"<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>"#).unwrap();

    zip.start_file("word/_rels/document.xml.rels", o).unwrap();
    zip.write_all(br#"<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rIdFn" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml"/>
  <Relationship Id="rIdLink" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://example.invalid/" TargetMode="External"/>
</Relationships>"#).unwrap();

    zip.start_file("word/footnotes.xml", o).unwrap();
    zip.write_all(br#"<?xml version="1.0" encoding="UTF-8"?>
<w:footnotes xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:footnote w:type="separator" w:id="0"><w:p><w:r><w:separator/></w:r></w:p></w:footnote>
  <w:footnote w:type="continuationSeparator" w:id="1"><w:p><w:r><w:continuationSeparator/></w:r></w:p></w:footnote>
  <w:footnote w:id="2"><w:p><w:r><w:t>Nestedbodyqx</w:t></w:r></w:p></w:footnote>
  <w:footnote w:id="3"><w:p><w:r><w:t>Toplevelbodyqx</w:t></w:r></w:p></w:footnote>
</w:footnotes>"#).unwrap();

    zip.start_file("word/document.xml", o).unwrap();
    zip.write_all(
        format!(
            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
            xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  <w:body>{body}</w:body>
</w:document>"#
        )
        .as_bytes(),
    )
    .unwrap();

    zip.finish().unwrap().into_inner()
}

/// Regression: the footnote *body* list was re-derived from a flat scan of the
/// paragraph's top-level inlines, while the display counter was advanced by
/// `collect_fragments`' recursive walk. §17.11.12.
///
/// Two distinct symptoms, and the ordering decides which fire:
/// * a reference nested in a hyperlink never got a body — **either ordering**;
/// * the surviving top-level body's number drifted out of step with the mark
///   actually painted — only when the nested reference comes **after** it
///   (`fn_base` then over-counts, so the body is numbered 2 while its mark
///   reads 1).
///
/// Both orderings are covered; `nested_second` is the strictly stronger case.
#[test]
fn footnote_nested_in_hyperlink_gets_a_body_and_keeps_numbering_aligned() {
    let link_with_note = r#"<w:hyperlink r:id="rIdLink">
          <w:r><w:t>link</w:t></w:r>
          <w:r><w:footnoteReference w:id="2"/></w:r>
        </w:hyperlink>"#;
    let top_level_note = r#"<w:r><w:t>tail</w:t></w:r>
        <w:r><w:footnoteReference w:id="3"/></w:r>"#;

    let cases = [
        (
            "nested_first",
            format!("<w:p>{link_with_note}{top_level_note}</w:p>"),
        ),
        (
            "nested_second",
            format!("<w:p>{top_level_note}{link_with_note}</w:p>"),
        ),
    ];

    for (label, body) in cases {
        let doc = dxpdf::docx::parse(&docx_with_footnotes(&body)).unwrap();
        let (_, pages) = dxpdf::render::resolve_and_layout(doc);

        assert_eq!(
            count_text_occurrences(&pages, "Nestedbodyqx"),
            1,
            "{label}: the hyperlink-nested footnote must render a body"
        );
        assert_eq!(
            count_text_occurrences(&pages, "Toplevelbodyqx"),
            1,
            "{label}: the top-level footnote must render a body"
        );

        // `build_note_content` prefixes each body with its display number and
        // two spaces. Both marks are painted (1 and 2), so exactly one body
        // must carry each number — which is what the old `fn_base` arithmetic
        // got wrong in the `nested_second` ordering (it produced two bodies
        // numbered 2, and none numbered 1).
        for n in [1, 2] {
            assert_eq!(
                count_text_occurrences(&pages, &format!("{n}  ")),
                1,
                "{label}: exactly one body numbered {n}"
            );
        }
    }
}