text-document-io 1.12.0

Import/export for text-document: plain text, Markdown, HTML, LaTeX, DOCX
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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#![cfg(feature = "pdf")]
//! Feature tests for the PDF exporter (embedded Typst).
//!
//! Documents are built with the (well-tested) djot importer, then exported via the file-less
//! builder [`document_io_controller::build_pdf_document`], and the resulting bytes are asserted
//! directly (PDF magic bytes, non-trivial size, page count) — mirroring
//! `docx_export_tests.rs`/`epub_export_tests.rs`'s harness. A real end-to-end, on-disk export
//! (through the `LongOperation` path) is exercised once, at the bottom, the same way
//! `rich_document_packs_to_a_valid_{docx,epub}_file*` do for their formats.

extern crate text_document_io as document_io;

use common::long_operation::{LongOperationManager, OperationStatus};
use common::parser_tools::PdfExportOptions;
use document_io::{ExportPdfDto, document_io_controller};
use std::sync::Arc;
use test_harness::{EventHub, setup};

/// A small, real TTF embedded as the sole test font — DejaVu Serif has broad Latin/Cyrillic/
/// Greek coverage but no Arabic/Hebrew shaping; the RTL fixture below deliberately reuses it
/// anyway (tofu glyphs are an acceptable outcome, a hard compile error is not).
const TEST_FONT: &[u8] = include_bytes!("assets/DejaVuSerif.ttf");

fn pdf_options() -> PdfExportOptions {
    PdfExportOptions {
        font_family: "DejaVu Serif".to_string(),
        font_bytes: vec![TEST_FONT.to_vec()],
        ..Default::default()
    }
}

/// Touches headings, bold/italic, an ordered and an unordered list, and a table — the "plain
/// prose" golden fixture required by the M7 test plan.
const RICH_DJOT: &str = "\
# Chapter One

Some *bold* and _italic_ prose about a #[stormy] night.

## A subsection

- bullet one
- bullet two

1. first
2. second

| A | B |
|---|---|
| 1 | 2 |
";

// --- harness -----------------------------------------------------------------

fn wait(mgr: &LongOperationManager, op_id: &str) {
    while let Some(OperationStatus::Running) = mgr.get_operation_status(op_id) {
        std::thread::sleep(std::time::Duration::from_millis(2));
    }
}

fn import_djot(db: &test_harness::DbContext, ev: &Arc<EventHub>, djot: &str) {
    let mut mgr = LongOperationManager::new();
    let op = document_io_controller::import_djot(
        db,
        ev,
        &mut mgr,
        &document_io::ImportDjotDto {
            djot_text: djot.to_string(),
            options: Default::default(),
        },
    )
    .expect("import_djot");
    wait(&mgr, &op);
    assert_eq!(
        mgr.get_operation_status(&op),
        Some(OperationStatus::Completed),
        "import of {djot:?} did not complete"
    );
}

/// Import `djot` into a fresh document and return the compiled PDF bytes, using `options`.
fn pdf_from_djot(djot: &str, options: PdfExportOptions) -> Vec<u8> {
    let (db, ev, _) = setup().expect("setup");
    import_djot(&db, &ev, djot);
    document_io_controller::build_pdf_document(
        &db,
        &ExportPdfDto {
            output_path: String::new(),
            options,
        },
    )
    .expect("build_pdf_document")
}

/// Count `/Type/Page` page-object dictionaries in raw PDF bytes — a word-boundary match so
/// `/Type/Pages` (the tree root) is never miscounted as a page. This is an **independent**
/// byte-level cross-check: the use case itself reports its page count from the laid-out
/// `PagedDocument` (`pages.len()`), not from a byte scan, so these two paths agreeing is what
/// this test set actually verifies.
fn count_pdf_pages(bytes: &[u8]) -> usize {
    let re = regex::bytes::Regex::new(r"/Type\s*/Page\b").unwrap();
    re.find_iter(bytes).count()
}

// --- (a) plain-prose fixture -------------------------------------------------

#[test]
fn plain_prose_fixture_exports_a_valid_pdf() {
    let bytes = pdf_from_djot(RICH_DJOT, pdf_options());
    assert!(
        bytes.starts_with(b"%PDF-"),
        "output must start with the PDF magic bytes"
    );
    assert!(
        bytes.len() > 500,
        "a document with headings/lists/a table must not compile to a trivially small PDF, got {} bytes",
        bytes.len()
    );
    assert!(
        count_pdf_pages(&bytes) >= 1,
        "must report at least one page"
    );
}

#[test]
fn plain_paragraph_exports_a_valid_pdf() {
    let bytes = pdf_from_djot(
        "Just a plain paragraph, no formatting at all.",
        pdf_options(),
    );
    assert!(bytes.starts_with(b"%PDF-"));
    assert!(count_pdf_pages(&bytes) >= 1);
}

#[test]
fn heading_levels_all_compile() {
    for level in 1..=6 {
        let hashes = "#".repeat(level);
        let djot = format!("{hashes} Title level {level}\n\nSome body text.\n");
        let bytes = pdf_from_djot(&djot, pdf_options());
        assert!(bytes.starts_with(b"%PDF-"), "level {level} must compile");
    }
}

#[test]
fn code_block_compiles_and_does_not_interpret_its_own_content_as_markup() {
    // The code's own literal `*`/`#`/backslash characters must survive untouched, and must not
    // be interpreted as Typst markup (which would be a security-relevant escaping bug, not just
    // a cosmetic one, since `#raw(..)` is only safe if the content is string-escaped, not
    // markup-escaped).
    let djot = "```rust\nlet s = \"a * b # c\\\\d\";\n```\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(bytes.starts_with(b"%PDF-"));
}

// --- (b) RTL fixture ----------------------------------------------------------

#[test]
fn rtl_hebrew_fixture_compiles() {
    let djot =
        "{direction=rtl}\n\u{05e9}\u{05dc}\u{05d5}\u{05dd} \u{05e2}\u{05d5}\u{05dc}\u{05dd}\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(
        bytes.starts_with(b"%PDF-"),
        "an RTL Hebrew block must still compile even with a non-Hebrew-shaping font"
    );
}

#[test]
fn rtl_arabic_fixture_compiles() {
    let djot = "{direction=rtl}\n\u{0645}\u{0631}\u{062d}\u{0628}\u{0627} \u{0628}\u{0627}\u{0644}\u{0639}\u{0627}\u{0644}\u{0645}\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(
        bytes.starts_with(b"%PDF-"),
        "an RTL Arabic block must still compile even with a non-Arabic-shaping font"
    );
}

#[test]
fn mixed_ltr_and_rtl_blocks_compile_in_one_document() {
    let djot = "English prose first.\n\n{direction=rtl}\n\u{05e9}\u{05dc}\u{05d5}\u{05dd}\n\nMore English prose after.\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(bytes.starts_with(b"%PDF-"));
}

#[test]
fn rtl_heading_compiles() {
    // An RTL heading must emit `= #text(dir: rtl)[..]` — the `=` marker at the block start with
    // only the *inline* content direction-wrapped. The earlier `#text(dir: rtl)[= ..]` form
    // buried the marker inside a text element; guard that the marker-first form is valid Typst
    // and still compiles.
    let djot = "{direction=rtl}\n# \u{05e9}\u{05dc}\u{05d5}\u{05dd} \u{05e2}\u{05d5}\u{05dc}\u{05dd}\n\nBody paragraph.\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(
        bytes.starts_with(b"%PDF-"),
        "an RTL heading must compile with the marker kept at block start"
    );
}

// --- (c) font failure fixture --------------------------------------------------

#[test]
fn garbage_font_bytes_produce_a_clear_error_not_a_silent_success() {
    let (db, ev, _) = setup().expect("setup");
    import_djot(&db, &ev, "Hello, world.");

    let options = PdfExportOptions {
        font_bytes: vec![vec![0u8; 16]], // not a font at all
        ..Default::default()
    };
    let err = document_io_controller::build_pdf_document(
        &db,
        &ExportPdfDto {
            output_path: String::new(),
            options,
        },
    )
    .expect_err("corrupt font bytes must be rejected, not silently produce an empty-font PDF");
    assert!(
        err.to_string().contains("could not be parsed as a font"),
        "got: {err}"
    );
}

#[test]
fn no_fonts_at_all_produce_a_clear_error() {
    let (db, ev, _) = setup().expect("setup");
    import_djot(&db, &ev, "Hello, world.");

    let err = document_io_controller::build_pdf_document(
        &db,
        &ExportPdfDto {
            output_path: String::new(),
            options: PdfExportOptions::default(), // font_bytes: vec![] by default
        },
    )
    .expect_err("an export with zero fonts must fail loudly");
    assert!(err.to_string().contains("no fonts supplied"), "got: {err}");
}

// --- escaping round-trips through the real import→export pipeline -------------

#[test]
fn special_characters_round_trip_through_import_and_export() {
    // Backslash-escaped in the djot source so the importer stores these as LITERAL characters in
    // the block's plain text (not djot's own formatting) — exercising `escape_typst` against
    // every character it must neutralize, driven through the real document model rather than
    // called directly (which isn't reachable from an external test crate; `escape_typst` also has
    // dedicated unit tests inside `typst_markup.rs` itself).
    let djot = "\\#hashtag \\*star\\* \\_underscore\\_ \\[bracket\\] \\$dollar \\`tick\\` \\~tilde\\~ \\<lt\\> \\@at and a literal - hyphen / slash = equals + plus.\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(
        bytes.starts_with(b"%PDF-"),
        "prose containing every escaped-special character must still compile"
    );
}

#[test]
fn leading_numbered_looking_prose_does_not_become_a_typst_list() {
    let djot = "12\\. Go left at the fork.\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(bytes.starts_with(b"%PDF-"));
}

// --- end-to-end write-to-disk via the LongOperation path -----------------------

/// A page break really produces a second page, and the *only* thing producing it is the
/// flag: the same prose without it fits on one.
#[test]
fn a_page_break_actually_opens_a_new_page() {
    let one = count_pdf_pages(&pdf_from_djot("Alpha.\n\nBeta.", pdf_options()));
    let two = count_pdf_pages(&pdf_from_djot(
        "Alpha.\n\n{page_break_before=true}\nBeta.",
        pdf_options(),
    ));
    assert_eq!(one, 1, "the control document must be a single page");
    assert_eq!(two, 2, "the break must open a second page");
}

/// `weak: true`, so a break on the very first block does not open on a blank one.
#[test]
fn a_break_on_the_first_block_does_not_produce_a_leading_blank_page() {
    let pages = count_pdf_pages(&pdf_from_djot(
        "{page_break_before=true}\n# Chapter One\n\nProse.",
        pdf_options(),
    ));
    assert_eq!(pages, 1, "expected no blank leading page");
}

/// The break is its own Typst chunk, so the `= ` that follows it still reads as a heading
/// rather than being buried mid-line.
#[test]
fn a_heading_after_a_page_break_is_still_a_heading() {
    let bytes = pdf_from_djot(
        "Body.\n\n{page_break_before=true}\n# Chapter Two\n\nMore.",
        pdf_options(),
    );
    assert_eq!(count_pdf_pages(&bytes), 2);
    assert!(bytes.starts_with(b"%PDF-"));
}

/// Typst refuses a `#pagebreak` inside any container — "pagebreaks are not allowed inside
/// of containers" — and it fails the whole export, not just the quote. A break that opens
/// a quotation therefore has to be lifted out of it, which is what it meant anyway.
#[test]
fn a_page_break_opening_a_quotation_is_lifted_out_of_it() {
    let bytes = pdf_from_djot(
        "Body.\n\n> {page_break_before=true}\n> Quoted matter.\n\nAfter.",
        pdf_options(),
    );
    assert!(bytes.starts_with(b"%PDF-"));
    assert_eq!(count_pdf_pages(&bytes), 2, "the break must still break");
}

/// Same, for the epigraph shape — which goes through Typst's `quote(attribution:)` slot
/// and so builds its container a different way.
#[test]
fn a_page_break_opening_an_epigraph_is_lifted_out_of_it() {
    let bytes = pdf_from_djot(
        "Body.\n\n> {semantic_role=epigraph page_break_before=true}\n> All happy families.\n>\n         > {alignment=right}\n> Tolstoy\n\nAfter.",
        pdf_options(),
    );
    assert!(bytes.starts_with(b"%PDF-"));
    assert_eq!(count_pdf_pages(&bytes), 2);
}

#[test]
fn rich_document_writes_a_real_pdf_file_to_disk() {
    let (db, ev, _) = setup().expect("setup");
    import_djot(&db, &ev, RICH_DJOT);

    let dir = std::env::temp_dir();
    let path = dir.join(format!("pdf_export_rich_{}.pdf", std::process::id()));
    let path_str = path.to_string_lossy().to_string();

    let mut mgr = LongOperationManager::new();
    let op = document_io_controller::export_pdf(
        &db,
        &ev,
        &mut mgr,
        &ExportPdfDto {
            output_path: path_str.clone(),
            options: PdfExportOptions {
                title: Some("Rich Book".to_string()),
                author: Some("Test Author".to_string()),
                ..pdf_options()
            },
        },
    )
    .expect("export_pdf");
    wait(&mgr, &op);
    assert_eq!(
        mgr.get_operation_status(&op),
        Some(OperationStatus::Completed),
        "export should complete"
    );

    let result_json = mgr.get_operation_result(&op).expect("result present");
    let result: document_io::ExportPdfResultDto =
        serde_json::from_str(&result_json).expect("result deserializes");
    assert_eq!(result.file_path, path_str);
    assert!(result.page_count >= 1);

    let bytes = std::fs::read(&path).expect("output file exists");
    assert!(!bytes.is_empty());
    assert!(
        bytes.starts_with(b"%PDF-"),
        "the written file is a real PDF"
    );
    assert_eq!(count_pdf_pages(&bytes) as i64, result.page_count);

    let _ = std::fs::remove_file(&path);
}

// --- per-block spacing overrides (what a scene break needs) ----------------

#[test]
fn per_block_spacing_overrides_compile() {
    // The Typst wraps for `top_margin` / `text_indent` are hand-written markup,
    // so the real risk is emitting something Typst cannot parse. Compiling is
    // the assertion: malformed markup fails here rather than at a user's export.
    let djot = "Ordinary indented paragraph.\n\n\
                {top_margin=24 text_indent=0}\nThe paragraph after a scene break.";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(bytes.starts_with(b"%PDF-"));
    assert!(count_pdf_pages(&bytes) >= 1);
}

#[test]
fn per_block_spacing_compiles_alongside_a_document_wide_indent() {
    // The block override has to coexist with `#set par(first-line-indent: ..)`
    // from the preamble — that combination is what a real manuscript export hits.
    let mut options = pdf_options();
    options.first_line_indent_mm = Some(5.0);
    options.paragraph_spacing_pt = Some(6.0);
    let djot = "First paragraph.\n\n{text_indent=0}\nFlush after the break.";
    let bytes = pdf_from_djot(djot, options);
    assert!(bytes.starts_with(b"%PDF-"));
}

#[test]
fn per_block_spacing_compiles_together_with_rtl_and_alignment() {
    // An RTL scene whose first paragraph follows a blank-line break stacks
    // direction + alignment + both spacing wraps on one block.
    let djot = "{direction=rtl alignment=center top_margin=24 text_indent=0}\nنص عربي بعد الفاصل.";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(bytes.starts_with(b"%PDF-"));
}

// --- inline images -----------------------------------------------------------

/// A real 4×3 PNG. Typst validates the bytes it is handed, so a placeholder
/// would fail the compile rather than test the wiring.
fn png_bytes() -> Vec<u8> {
    let mut buf = Vec::new();
    {
        let mut enc = png::Encoder::new(&mut buf, 4, 3);
        enc.set_color(png::ColorType::Rgba);
        enc.set_depth(png::BitDepth::Eight);
        let mut w = enc.write_header().unwrap();
        w.write_image_data(&[0u8, 128, 255, 255].repeat(12))
            .unwrap();
    }
    buf
}

fn options_with_image() -> PdfExportOptions {
    PdfExportOptions {
        images: common::parser_tools::ExportImages::from_iter([(
            "pic.png",
            common::parser_tools::ExportImage::new(png_bytes(), "image/png"),
        )]),
        ..pdf_options()
    }
}

/// The whole point of the Typst wiring: the compiler resolves `#image(..)` to a
/// virtual file the caller registered. If the markup's path and the registered
/// file id disagree by so much as a leading slash, Typst fails the compile — so
/// a PDF coming out at all is the assertion.
#[test]
fn an_inline_image_compiles_into_the_pdf() {
    let pdf = pdf_from_djot(
        "Before ![a blue square](pic.png){width=64 height=48} after.\n",
        options_with_image(),
    );
    assert_eq!(&pdf[..5], b"%PDF-", "not a PDF");
    assert!(
        pdf.len() > 1000,
        "suspiciously small PDF: {} bytes",
        pdf.len()
    );
    assert_eq!(count_pdf_pages(&pdf), 1);
}

/// An image the caller supplied no bytes for must not abort the export — a
/// missing picture is not worth failing a manuscript over.
#[test]
fn an_image_without_bytes_falls_back_to_its_description() {
    let pdf = pdf_from_djot(
        "Before ![a blue square](missing.png) after.\n",
        pdf_options(),
    );
    assert_eq!(&pdf[..5], b"%PDF-");
    assert_eq!(count_pdf_pages(&pdf), 1);
}

/// Two images must each resolve to their own registered file.
#[test]
fn several_images_each_resolve() {
    let mut images = common::parser_tools::ExportImages::new();
    for name in ["a.png", "b.png"] {
        images.insert(
            name,
            common::parser_tools::ExportImage::new(png_bytes(), "image/png"),
        );
    }
    let pdf = pdf_from_djot(
        "![one](a.png) and ![two](b.png)\n",
        PdfExportOptions {
            images,
            ..pdf_options()
        },
    );
    assert_eq!(&pdf[..5], b"%PDF-");
    assert_eq!(count_pdf_pages(&pdf), 1);
}

// --- footnotes ---------------------------------------------------------------

/// Typst takes a note's body **at the reference** — like LaTeX, and unlike every
/// other backend — then numbers it and places it at the foot of the page itself.
///
/// Compiling is the assertion. Typst rejects a malformed `#footnote[…]` outright
/// rather than rendering it wrongly, so a PDF coming out the other side is proof
/// the markup was well-formed with the body inside it. This test lives here, with
/// the font harness, rather than beside the other footnote round-trip tests: the
/// PDF path is the one exporter that cannot run without an embedded font, and a
/// `to_pdf` test with none fails on "no fonts supplied" long before it reaches
/// anything about notes.
#[test]
fn a_footnote_compiles_into_the_pdf() {
    let djot = "The lighthouse stood alone.[^a]\n\n[^a]: Decommissioned in 1961.\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(
        bytes.starts_with(b"%PDF-"),
        "a document carrying a footnote must still compile"
    );
    assert!(count_pdf_pages(&bytes) >= 1);
}

/// A reference whose definition was deleted must not take the export down with
/// it. The body is simply absent; Typst is handed a note with nothing in it
/// rather than a dangling command.
#[test]
fn a_footnote_with_no_body_still_compiles() {
    let bytes = pdf_from_djot("Orphaned here.[^gone]\n", pdf_options());
    assert!(bytes.starts_with(b"%PDF-"));
}

/// Citing the same label twice must not emit a second `#footnote[…]` for it —
/// see `TypstNotes::mark_emitted`'s doc: Typst would count that as a second,
/// independently-numbered note, duplicating the body. The repeat citation
/// instead uses Typst's own reference form, `#footnote(<label>)`, pointing at
/// a `<label>` the first citation defines.
///
/// Compiling is the assertion, same reasoning as `a_footnote_compiles_into_
/// the_pdf`: Typst rejects a malformed `#footnote(<…>)` — an unlabeled
/// target, a label used before it exists — outright, so a PDF coming out the
/// other side proves both the defining call's `<label>` and the reference
/// call's `(<label>)` were well-formed and consistent.
#[test]
fn a_repeat_footnote_citation_reuses_one_note_and_compiles() {
    let djot = "First[^n1] and second[^n1] citation.\n\n[^n1]: The note body.\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(
        bytes.starts_with(b"%PDF-"),
        "a repeat citation of one label must still compile"
    );
    assert!(count_pdf_pages(&bytes) >= 1);
}

/// Two DIFFERENT labels, each cited twice, exercises the per-label `emitted`
/// bookkeeping independently — a shared, mis-scoped flag would either dedupe
/// across labels (dropping a real second note) or fail to dedupe at all.
#[test]
fn independently_repeated_footnotes_all_compile() {
    let djot = "One[^a] two[^a] three[^b] four[^b].\n\n[^a]: Body A.\n\n[^b]: Body B.\n";
    let bytes = pdf_from_djot(djot, pdf_options());
    assert!(bytes.starts_with(b"%PDF-"));
    assert!(count_pdf_pages(&bytes) >= 1);
}