xberg-native-pdf 1.3.6

PDF parsing, extraction and rendering -- the native PDF engine behind xberg.
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
use super::super::*;
use super::common::*;
use super::pdf_fixtures::*;

#[test]
fn test_document_open_nonexistent_file() {
    let result = PdfDocument::open("/nonexistent/path/to/file.pdf");
    assert!(result.is_err());
    assert!(matches!(result.unwrap_err(), Error::Io(_)));
}

/// Recursion into a Form XObject invoked by another Form XObject, with
/// no `cm` at either level, must still resolve each `Do`'s operand
/// correctly and extract the innermost text.
#[test]
fn test_nested_xobject_text_extracted_with_orphaned_do_operands() {
    let pdf_bytes = build_nested_xobject_do_with_orphaned_operands_pdf();
    let doc = PdfDocument::from_bytes(pdf_bytes).expect("parse repro pdf");

    let text = doc.extract_text(0).unwrap();
    assert!(text.contains("nested text"), "nested XObject text missing: {text:?}");
}

/// Regression test: a content stream with a degenerate CTM (`a == 0`)
/// and an oversized `Tm` translation literal. The lexer now clamps an
/// overflowing real literal to a finite value (see
/// `lexer::tests::test_parse_oversized_real_clamps_to_finite`), so this
/// specific literal no longer reaches `TjBuffer::new` as `Infinity` and
/// can no longer turn into NaN via `ctm.a * Infinity`. This test is
/// kept as a regression guard for that class of bug — before the lexer
/// clamp, `f64::from_str` *saturated* the all-digit literal to
/// `f64::INFINITY` rather than erroring, and combined with the zero
/// CTM component this became a NaN `bbox.y` in `TjBuffer::new`,
/// panicking `snap_superscript_baselines`'s index sort — the first
/// span sort that runs on every page, before `postprocess_spans`'s
/// off-page filter (which otherwise silently drops any NaN-bbox span
/// and requires a missing `/MediaBox` to bypass, see
/// `build_minimal_pdf_with_font`) — with the exact signature
/// `smallsort.rs: user-provided comparison function does not
/// correctly implement a total order`.
///
/// 320 glyphs with distinct (sub-point-jittered) Y coordinates —
/// matching real OCR/scanned-text bbox noise, so no two glyphs tie in
/// the sort key — emitted in a riffle-shuffled, far-from-sorted
/// order: a near-sorted input lets Rust's pattern-defeating sort skip
/// the internal total-order consistency check entirely, which is why
/// a naive grid/row-major layout would not have reproduced the
/// original panic even with the same NaN present.
#[test]
fn test_nan_bbox_from_oversized_tm_literal_does_not_panic() {
    let n = 320usize;
    let ys: Vec<f32> = (0..n).map(|i| 750.0 - (i as f32) * 1.37).collect();
    let mid = ys.len() / 2;
    let (a, b) = ys.split_at(mid);
    let mut shuffled: Vec<f32> = Vec::with_capacity(ys.len());
    let (mut ai, mut bi) = (a.iter(), b.iter());
    loop {
        match (ai.next(), bi.next()) {
            (Some(x), Some(y)) => {
                shuffled.push(*x);
                shuffled.push(*y);
            }
            (Some(x), None) => shuffled.push(*x),
            (None, Some(y)) => shuffled.push(*y),
            (None, None) => break,
        }
    }

    let mut content = Vec::new();
    content.extend_from_slice(b"BT\n/F1 10 Tf\n");
    for (i, y) in shuffled.iter().enumerate() {
        if i == 1 {
            // The malicious glyph, isolated under its own degenerate
            // CTM (a = 0) so only this glyph's position collapses
            // via `0.0 * Infinity`; ordinary glyphs are unaffected. ~keep
            let huge = "9".repeat(400) + ".0";
            content.extend_from_slice(b"ET\nq\n0 0 0 1 0 0 cm\nBT\n/F1 10 Tf\n");
            content.extend_from_slice(format!("1 0 0 1 {huge} 400 Tm\n(BOOM) Tj\n").as_bytes());
            content.extend_from_slice(b"ET\nQ\nBT\n/F1 10 Tf\n");
        }
        content.extend_from_slice(format!("1 0 0 1 20 {y} Tm\n(X) Tj\n").as_bytes());
    }
    content.extend_from_slice(b"ET\n");

    let pdf_bytes = build_minimal_pdf_with_font(&content);
    let doc = PdfDocument::from_bytes(pdf_bytes).expect("parse repro pdf");
    let result1 = doc.extract_text(0);
    assert!(
        result1.is_ok(),
        "extract_text panicked or errored on NaN bbox coordinate"
    );
    let result2 = doc.extract_spans(0);
    assert!(
        result2.is_ok(),
        "extract_spans panicked or errored on NaN bbox coordinate"
    );
}

#[test]
fn test_from_bytes_empty() {
    let result = PdfDocument::from_bytes(vec![]);
    assert!(result.is_err());
}

#[test]
fn test_page_count_single_page() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert_eq!(doc.page_count().unwrap(), 1);
}

#[test]
fn test_page_count_multiple_pages() {
    let pdf = build_multi_page_pdf(5);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert_eq!(doc.page_count().unwrap(), 5);
}

#[test]
fn test_authenticate_unencrypted_pdf() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let result = doc.authenticate(b"anypassword").unwrap();
    assert!(result);
}

#[test]
fn test_extract_text_blank_page() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let text = doc.extract_text(0).unwrap();
    assert!(text.is_empty());
}

#[test]
fn test_extract_text_no_font_resources() {
    let content = b"BT /F1 12 Tf (Hello) Tj ET";
    let pdf = build_minimal_pdf(content);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    // Should not crash, may return empty or partial text ~keep
    let _text = doc.extract_text(0).unwrap();
}

#[test]
fn test_extract_all_text_multiple_pages() {
    let pdf = build_multi_page_pdf(3);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let text = doc.extract_all_text().unwrap();
    let page_count = text.matches('\x0c').count();
    assert_eq!(page_count, 2);
}

#[test]
fn test_extract_all_text_single_page() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let text = doc.extract_all_text().unwrap();
    assert!(!text.contains('\x0c'));
}

#[test]
fn test_extract_spans_blank_page() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let spans = doc.extract_spans(0).unwrap();
    assert!(spans.is_empty());
}

#[test]
fn test_extract_spans_no_text_operators() {
    let content = b"100 200 300 400 re S";
    let pdf = build_minimal_pdf(content);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let spans = doc.extract_spans(0).unwrap();
    assert!(spans.is_empty());
}

#[test]
fn test_decode_stream_with_encryption_null_object() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let result = doc
        .decode_stream_with_encryption(&Object::Null, ObjectRef::new(1, 0))
        .unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_page_cannot_have_text_no_resources() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();

    let page_dict = std::collections::HashMap::new();
    assert!(doc.page_cannot_have_text(&page_dict));
}

#[test]
fn test_page_cannot_have_text_with_font_resources() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();

    let mut font_dict = std::collections::HashMap::new();
    font_dict.insert("F1".to_string(), Object::Reference(ObjectRef::new(10, 0)));

    let mut resources_dict = std::collections::HashMap::new();
    resources_dict.insert("Font".to_string(), Object::Dictionary(font_dict));

    let mut page_dict = std::collections::HashMap::new();
    page_dict.insert("Resources".to_string(), Object::Dictionary(resources_dict));

    assert!(!doc.page_cannot_have_text(&page_dict));
}

#[test]
fn test_page_cannot_have_text_empty_font_dict() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();

    let font_dict = std::collections::HashMap::new();

    let mut resources_dict = std::collections::HashMap::new();
    resources_dict.insert("Font".to_string(), Object::Dictionary(font_dict));

    let mut page_dict = std::collections::HashMap::new();
    page_dict.insert("Resources".to_string(), Object::Dictionary(resources_dict));

    assert!(doc.page_cannot_have_text(&page_dict));
}

#[test]
fn test_extract_paths_blank_page() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let paths = doc.extract_paths(0).unwrap();
    assert!(paths.is_empty());
}

#[test]
fn test_extract_paths_rectangle() {
    let content = b"100 200 300 400 re S";
    let pdf = build_minimal_pdf(content);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let paths = doc.extract_paths(0).unwrap();
    assert!(!paths.is_empty());
}

#[test]
fn test_get_page_caching() {
    let pdf = build_multi_page_pdf(3);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let _page1 = doc.get_page(0).unwrap();
    let _page2 = doc.get_page(0).unwrap();
}

#[test]
fn test_get_page_out_of_bounds() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let result = doc.get_page(99);
    assert!(result.is_err());
}

#[test]
#[allow(deprecated)]
fn test_page_count_u32_returns_correct_value() {
    let pdf = build_multi_page_pdf(3);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert_eq!(doc.page_count_u32(), 3);
}

#[test]
fn test_open_with_config() {
    let pdf = build_minimal_pdf(b"");
    let dir = tempfile::tempdir().expect("create temp dir");
    let tmp_path = dir.path().join("native_pdf_test_open_with_config.pdf");
    std::fs::write(&tmp_path, &pdf).unwrap();
    let config = 42u32;
    let result = PdfDocument::open_with_config(&tmp_path, config);
    let _ = std::fs::remove_file(&tmp_path);
    assert!(result.is_ok());
}

#[test]
fn test_extract_paths_in_rect_empty_page() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let region = crate::geometry::Rect {
        x: 0.0,
        y: 0.0,
        width: 612.0,
        height: 792.0,
    };
    let paths = doc.extract_paths_in_rect(0, region).unwrap();
    assert!(paths.is_empty());
}

#[test]
fn test_get_page_ref_out_of_bounds() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let result = doc.get_page_ref(99);
    assert!(result.is_err());
}

/// A ToUnicode CMap that maps char 0x01 → U+FB01 (fi) must produce the
/// ligature character in extract_text output — NOT the expanded "fi".
///
/// Before the fix, `extract_text` unconditionally calls
/// `get_ligature_components(fi)` → "fi", discarding the font's own
/// ToUnicode intent. After the fix the ligature char is preserved.
#[test]
fn test_ligature_fi_preserved_in_extract_text() {
    let pdf = build_ligature_fi_pdf();
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let text = doc.extract_text(0).unwrap();
    assert!(
        text.contains('\u{FB01}'),
        "U+FB01 (fi) must be preserved in extracted text; got: {text:?}"
    );
    assert!(
        !text.contains("fi") || text.contains('\u{FB01}'),
        "must not expand fi → fi; got: {text:?}"
    );
}

#[test]
fn test_annotation_freetext() {
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /FreeText /Contents (Hello from annotation) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let text = doc.extract_text(0).unwrap();
    assert!(text.contains("Hello from annotation"));
}

#[test]
fn test_annotation_text_type() {
    // Text (sticky-note) /Contents is reviewer popup comment text, not visible page
    // content — it must NOT appear in extract_text output (ISO 32000-1 §12.5.6.2). ~keep
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /Text /Contents (Sticky note) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_text(0).unwrap().contains("Sticky note"));
}

#[test]
fn test_annotation_stamp() {
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /Stamp /Contents (APPROVED) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_text(0).unwrap().contains("APPROVED"));
}

#[test]
fn test_annotation_link() {
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /Link /Contents (Click here) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_text(0).unwrap().contains("Click here"));
}

#[test]
fn test_annotation_highlight() {
    // Highlight annotation /Contents is a user comment on the highlighted
    // text — it is NOT page content and must NOT appear in extract_text output. ~keep
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /Highlight /Contents (Highlighted) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_text(0).unwrap().contains("Highlighted"));
}

#[test]
fn test_annotation_hidden_flag() {
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /FreeText /F 2 /Contents (Hidden) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_text(0).unwrap().contains("Hidden"));
}

#[test]
fn test_annotation_invisible_flag() {
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /FreeText /F 1 /Contents (Invisible) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_text(0).unwrap().contains("Invisible"));
}

#[test]
fn test_annotation_noview_flag() {
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /Text /F 32 /Contents (NoView) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_text(0).unwrap().contains("NoView"));
}

#[test]
fn test_annotation_unknown_subtype() {
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /CustomType /Contents (Custom) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_text(0).unwrap().contains("Custom"));
}

#[test]
fn test_annotation_multiple() {
    // FreeText /Contents is visible page text; Text (sticky-note) /Contents is popup
    // comment — only FreeText should appear in extract_text output. ~keep
    let a1 = b"4 0 obj\n<< /Type /Annot /Subtype /FreeText /Contents (First) >>\nendobj\n".to_vec();
    let a2 = b"5 0 obj\n<< /Type /Annot /Subtype /Text /Contents (Second) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, a1), (5, a2)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let text = doc.extract_text(0).unwrap();
    assert!(text.contains("First"));
    assert!(!text.contains("Second"));
}

#[test]
fn test_annotation_no_subtype() {
    let annot = b"4 0 obj\n<< /Type /Annot /Contents (No subtype) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_text(0).unwrap().contains("No subtype"));
}

#[test]
fn test_annotation_widget_with_value() {
    let annot =
        b"4 0 obj\n<< /Type /Annot /Subtype /Widget /FT /Tx /V (Field value) /Rect [72 700 272 720] >>\nendobj\n"
            .to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_text(0).unwrap().contains("Field value"));
}

#[test]
fn test_extract_text_graphics_only() {
    let pdf = build_minimal_pdf(b"q 1 0 0 1 0 0 cm 100 200 300 400 re S Q");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_text(0).unwrap().is_empty());
}

#[test]
fn test_extract_text_page_out_of_bounds() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_text(100).is_err());
}

#[test]
fn test_extract_spans_out_of_bounds() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_spans(999).is_err());
}

#[test]
fn test_extract_paths_line() {
    let pdf = build_minimal_pdf(b"0 0 m 100 100 l S");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_paths(0).unwrap().is_empty());
}

#[test]
fn test_extract_paths_out_of_bounds() {
    let pdf = build_minimal_pdf(b"");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_paths(999).is_err());
}

#[test]
fn test_extract_paths_curve() {
    let pdf = build_minimal_pdf(b"0 0 m 25 50 75 50 100 0 c S");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_paths(0).unwrap().is_empty());
}

#[test]
fn test_extract_paths_filled_rect() {
    let pdf = build_minimal_pdf(b"50 50 200 100 re f");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(!doc.extract_paths(0).unwrap().is_empty());
}

#[test]
fn test_extract_paths_in_rect_with_content() {
    let pdf = build_minimal_pdf(b"100 200 300 400 re S");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let region = crate::geometry::Rect {
        x: 0.0,
        y: 0.0,
        width: 612.0,
        height: 792.0,
    };
    assert!(!doc.extract_paths_in_rect(0, region).unwrap().is_empty());
}

#[test]
fn test_populate_page_cache_sequential() {
    let pdf = build_multi_page_pdf(5);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    for i in 0..5 {
        assert!(doc.get_page(i).unwrap().as_dict().is_some());
    }
}

#[test]
fn test_get_page_ref_multi_page() {
    let pdf = build_multi_page_pdf(3);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let r0 = doc.get_page_ref(0).unwrap();
    let r1 = doc.get_page_ref(1).unwrap();
    let r2 = doc.get_page_ref(2).unwrap();
    assert_ne!(r0.id, r1.id);
    assert_ne!(r1.id, r2.id);
}

#[test]
fn test_decode_stream_with_encryption_non_null() {
    let pdf = build_minimal_pdf(b"BT (Hello) Tj ET");
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    let stream_obj = doc.load_object(ObjectRef::new(4, 0)).unwrap();
    assert!(
        doc.decode_stream_with_encryption(&stream_obj, ObjectRef::new(4, 0))
            .is_ok()
    );
}

#[test]
fn test_extract_text_annotations_only() {
    let annot = b"4 0 obj\n<< /Type /Annot /Subtype /FreeText /Contents (Only annotation) >>\nendobj\n".to_vec();
    let pdf = build_pdf_with_annotations(vec![(4, annot)]);
    let doc = PdfDocument::from_bytes(pdf).unwrap();
    assert!(doc.extract_text(0).unwrap().contains("Only annotation"));
}

/// A password-protected PDF is detected as encrypted, and text extraction
/// degrades to empty output (warn + empty) rather than erroring — matching
/// pdftotext/PyMuPDF. (`page_count` still surfaces `Error::EncryptedPdf`;
/// see `tests/test_extraction_robustness.rs`.)
#[test]
fn test_encrypted_pdf_extracts_empty_without_password() {
    let pdf_path = "tests/fixtures/encrypted_needs_password.pdf";
    let doc = PdfDocument::open(pdf_path).expect("open should succeed even without password");
    assert!(doc.is_encrypted(), "PDF should be detected as encrypted");

    let text = doc
        .extract_text(0)
        .expect("extract_text degrades to empty, not an error");
    assert!(
        text.is_empty(),
        "undecryptable extraction should be empty, got: {:?}",
        text,
    );
}

/// After authenticating with the correct password, extraction should succeed.
#[test]
fn test_encrypted_pdf_works_after_authentication() {
    let pdf_path = "tests/fixtures/encrypted_needs_password.pdf";
    let doc = PdfDocument::open(pdf_path).expect("open should succeed");
    assert!(doc.is_encrypted());

    let result = doc.authenticate(b"secret").expect("authenticate should not error");
    assert!(result, "Authentication with correct password should succeed");

    let page_count = doc.page_count().expect("page_count should work after auth");
    assert!(page_count > 0, "Should have at least 1 page after auth");

    // extract_text should not error (content may be minimal since it's a test PDF) ~keep
    let _text = doc.extract_text(0).expect("extract_text should work after auth");
}

/// PDFs that are encrypted but authenticated with empty password (the common
/// case for permission-only encryption) must continue to work without error.
#[test]
fn test_encrypted_pdf_with_empty_password_still_works() {
    let pdf_path = "tests/fixtures/encrypted_cid_truetype.pdf";
    let doc = PdfDocument::open(pdf_path).expect("open should succeed");
    // This PDF auto-authenticates with empty password during open() ~keep
    assert!(doc.is_encrypted(), "Should be detected as encrypted");

    let page_count = doc.page_count().expect("page_count should work");
    assert!(page_count > 0);

    let text = doc.extract_text(0).expect("extract_text should work");
    assert!(!text.trim().is_empty(), "Should extract non-empty text");
}

#[test]
fn test_encrypted_pdf_with_compressed_object_streams() {
    // Encrypted PDFs with /Type /ObjStm streams must NOT have those streams
    // decrypted, per ISO 32000-1 Section 7.6.2. Object streams and XRef
    // streams are never individually encrypted; only the overall stream
    // data is compressed. Attempting to decrypt them causes AES errors
    // because the data length is not a multiple of the block size. ~keep
    let pdf_path = "tests/fixtures/encrypted_objstm.pdf";
    let doc = PdfDocument::open(pdf_path).expect("open should succeed for encrypted+objstm PDF");
    assert!(doc.is_encrypted(), "Should be detected as encrypted");

    let page_count = doc.page_count().expect("page_count should work with encrypted objstm");
    assert!(page_count > 0, "Should have at least one page");
}

#[test]
fn test_extract_paths_layer_none_for_plain_stroke() {
    // End-to-end through the real page pipeline: a stroked line on a page
    // with no optional content yields a path whose `layer` is None. Guards
    // the page-level marked-content refactor against perturbing plain
    // extraction (and mirrors the Python shape test's synthetic PDF). ~keep
    let doc = PdfDocument::from_bytes(build_minimal_pdf(b"100 100 m 200 200 l S")).unwrap();
    let paths = doc.extract_paths(0).unwrap();
    assert_eq!(paths.len(), 1);
    assert_eq!(paths[0].layer, None);
}

#[test]
fn test_get_page_rotation_status_absent_is_absent() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(None, false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Absent);
}

#[test]
fn test_get_page_rotation_status_zero_is_valid_zero() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("0"), false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Valid(0));
}

#[test]
fn test_get_page_rotation_status_90_is_valid_90() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("90"), false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Valid(90));
}

#[test]
fn test_get_page_rotation_status_180_is_valid_180() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("180"), false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Valid(180));
}

#[test]
fn test_get_page_rotation_status_270_is_valid_270() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("270"), false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Valid(270));
}

#[test]
fn test_get_page_rotation_status_negative_90_normalizes_to_270() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("-90"), false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Valid(270));
}

#[test]
fn test_get_page_rotation_status_real_value_is_read() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("90.0"), false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Valid(90));
}

#[test]
fn test_get_page_rotation_status_non_integral_real_is_malformed() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("90.5"), false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Malformed);
}

#[test]
fn test_get_page_rotation_status_non_numeric_is_malformed() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("(bogus)"), false)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Malformed);
}

#[test]
fn test_get_page_rotation_status_inherited_from_pages_node() {
    let doc = PdfDocument::from_bytes(build_pdf_with_rotate_token(Some("90"), true)).unwrap();
    assert_eq!(doc.get_page_rotation_status(0).unwrap(), PageRotation::Valid(90));
}