flpdf 0.2.1

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
//! Resource pruning after page-subset extraction.
//!
//! After [`crate::page_tree_rebuild::rebuild_page_tree`] has restructured the
//! document so that only the selected pages remain reachable from `/Root`,
//! two kinds of "garbage" may linger in the object table:
//!
//! 1. **Stale `/Resources` name entries** – fonts or XObjects that are listed
//!    in a page's `/Resources` sub-dictionary but not actually referenced by
//!    any content stream of a retained page.
//!
//! 2. **Orphan objects at the xref level** – whole indirect objects that are
//!    no longer reachable from `/Root` at all (e.g. dropped pages, their
//!    content streams, the intermediate `/Pages` nodes that `rebuild_page_tree`
//!    intentionally leaves as orphans).
//!
//! [`prune_after_subset`] addresses both in one call, gated by
//! [`RemoveUnreferencedResources`]:
//!
//! | Mode | Name-level prune | xref-level GC |
//! |------|------------------|---------------|
//! | [`RemoveUnreferencedResources::No`]   | No  | No  |
//! | [`RemoveUnreferencedResources::Auto`] | Yes | Yes |
//! | [`RemoveUnreferencedResources::Yes`]  | Yes | Yes |
//!
//! # qpdf 11.9.0 observed behaviour (truth source `/usr/bin/qpdf`)
//!
//! Input: a 2-page PDF where page 1 uses font `/F1` only and page 2 uses font
//! `/F2` only (each page carries its own `/Resources` dict).
//!
//! ```text
//! qpdf two_page.pdf --pages two_page.pdf 1 -- subset.pdf
//! ```
//!
//! Before extraction (10 objects):
//!   obj 1 = Catalog, 2 = Pages root, 3 = page1 dict, 4 = page1 content,
//!   5 = page1 /Font (F1 entry), 6 = font F1,
//!   7 = page2 dict, 8 = page2 content, 9 = page2 /Font (F2 entry), 10 = font F2
//!
//! After extraction (6 objects, qpdf default = auto):
//!   - obj 7, 8, 9, 10 are completely absent from xref (xref-level GC).
//!   - F2 font is gone; F1 remains.
//!   - The page 1 objects are renumbered but all present.
//!
//! This confirms that `Auto` (the qpdf default) performs both name-level
//! pruning **and** xref-level GC of unreachable objects.  `No` preserves both.

use crate::object::MAX_INLINE_DEPTH;
use crate::resources::{remove_unreferenced_resources, RemoveUnreferencedResources};
use crate::{Object, ObjectRef, Pdf, Result};
use std::collections::BTreeSet;
use std::io::{Read, Seek};

// ── Public entry point ────────────────────────────────────────────────────────

/// Prune unreferenced resources from a PDF whose page tree has already been
/// rebuilt by [`crate::page_tree_rebuild::rebuild_page_tree`].
///
/// Two passes are performed when `mode` is not [`RemoveUnreferencedResources::No`]:
///
/// 1. **Name-level prune** (`remove_unreferenced_resources`): removes entries
///    from each page's `/Resources` sub-dictionaries (`/Font`, `/XObject`, …)
///    that are not referenced by any content stream of the retained pages.
///    After `rebuild_page_tree` materialises inherited attributes onto each
///    leaf, every leaf's `/Resources` is an inline dict (unshared), so
///    [`RemoveUnreferencedResources::Auto`] prunes just as aggressively as
///    `Yes` for those leaves.
///
/// 2. **xref-level GC** (`collect_reachable` + `delete_object`): walks every
///    `Object::Reference` reachable from `/Root` (transitively), then calls
///    [`Pdf::delete_object`] for every live object that was **not** reached.
///    This removes orphaned intermediate `/Pages` nodes left by
///    `rebuild_page_tree`, dropped-page content streams, and similar debris.
///
/// Calling this function on a PDF that has **not** been rebuilt (i.e. all
/// pages are still reachable) is safe: no objects will be deleted by the GC
/// pass, and the name-level prune is equivalent to calling
/// `remove_unreferenced_resources` directly.
///
/// # Errors
///
/// Propagates errors from [`remove_unreferenced_resources`]. The GC
/// reachability pass deliberately *swallows* [`Pdf::resolve`] errors
/// (an unresolvable object is conservatively treated as reachable and
/// kept), so a resolve failure there does not abort the prune.
pub fn prune_after_subset<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    mode: RemoveUnreferencedResources,
) -> Result<()> {
    if mode == RemoveUnreferencedResources::No {
        return Ok(());
    }

    // ── Pass 1: name-level prune ──────────────────────────────────────────────
    // Delegate entirely to the existing per-page name pruning logic.
    // After rebuild_page_tree, each leaf carries its /Resources as a
    // PageInline dict (never inherited or shared at the indirect level),
    // so Auto behaves identically to Yes for those leaves.
    remove_unreferenced_resources(pdf, mode)?;

    // ── Pass 2: xref-level GC ─────────────────────────────────────────────────
    sweep_unreachable_objects(pdf)?;

    Ok(())
}

/// Mark-and-sweep every indirect object that is **not** reachable from the
/// document `/Root` or the PDF trailer, mirroring qpdf's complete-rewrite
/// model (the writer only emits reachable objects; everything else is
/// implicitly dropped — `truth source /usr/bin/qpdf`).
///
/// The trailer is seeded in addition to `/Root` because it can reference
/// objects that are not reachable through `/Root`, most notably `/Info`
/// (document information dictionary) and `/Encrypt` (encryption dictionary).
///
/// Returns the number of objects deleted (useful for diagnostics/logging;
/// callers may ignore it). Returns `Ok(0)` when the document has no `/Root`
/// (nothing can be proven unreachable, so the GC stays maximally
/// conservative).
///
/// Used by [`prune_after_subset`] (after page-subset rebuild) and by
/// [`crate::embedded_files::remove_attachment`] (after detaching an
/// attachment): in both cases the mutation makes some objects unreachable and
/// this single, well-tested sweep is the one place that physically removes
/// them — no per-feature ad-hoc reachability heuristics.
pub(crate) fn sweep_unreachable_objects<R: Read + Seek>(pdf: &mut Pdf<R>) -> Result<usize> {
    let root_ref = match pdf.root_ref() {
        Some(r) => r,
        None => return Ok(0), // no /Root → nothing can be proven unreachable
    };

    // Snapshot live refs before the walk so we can compute the unreachable
    // set after marking.
    let all_live = pdf.live_object_refs();

    // Mark: traverse from /Root AND from the trailer (protects /Info,
    // /Encrypt and any other trailer-only references from the sweep).
    let trailer_refs = {
        let trailer_clone = Object::Dictionary(pdf.trailer().clone());
        let mut refs: Vec<ObjectRef> = Vec::new();
        walk_refs(&trailer_clone, 0, &mut refs)?;
        refs
    };
    let reachable = collect_reachable(pdf, root_ref, trailer_refs)?;

    // Sweep: delete every live object that was not reached.
    let mut deleted = 0usize;
    for obj_ref in all_live {
        if !reachable.contains(&obj_ref) {
            pdf.delete_object(obj_ref);
            deleted += 1;
        }
    }
    Ok(deleted)
}

// ── Reachability walker ───────────────────────────────────────────────────────

/// Transitively collect every `ObjectRef` reachable from `start` (and any
/// additional seeds in `extra_seeds`) by following all `Object::Reference`
/// values encountered while resolving objects.
///
/// `extra_seeds` is used to protect objects referenced by the PDF trailer
/// (e.g. `/Info`, `/Encrypt`) that are NOT reachable through `/Root`.
///
/// Cycles are handled by the `visited` set: an object already in the set is
/// not resolved again.  Object-number 0 (the free-list head) is never
/// traversed.
///
/// Errors from [`Pdf::resolve`] on individual objects are silently ignored so
/// that a malformed or partially-corrupt PDF does not abort the entire GC pass.
/// The conservative effect is that the problematic object stays reachable (the
/// walk cannot mark it unreachable) and is therefore not deleted.
fn collect_reachable<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    start: ObjectRef,
    extra_seeds: Vec<ObjectRef>,
) -> Result<BTreeSet<ObjectRef>> {
    let mut visited: BTreeSet<ObjectRef> = BTreeSet::new();
    let mut queue: Vec<ObjectRef> = vec![start];
    queue.extend(extra_seeds);

    while let Some(current) = queue.pop() {
        if current.number == 0 {
            continue;
        }
        if !visited.insert(current) {
            continue;
        }

        // If `current` lives inside an /ObjStm, that container object must
        // survive the sweep too: walk_refs only follows Object::Reference and
        // never sees the metadata-level compressed-parent link, so without
        // this, delete_object would drop the /ObjStm and make every compressed
        // member unrecoverable in the output.
        if let Some((objstm_ref, _)) = pdf.compressed_parent(current) {
            queue.push(objstm_ref);
        }

        // Resolve the object; skip on error (conservative — keeps the object).
        let obj = match pdf.resolve_borrowed(current) {
            Ok(o) => o,
            Err(_) => continue,
        };

        // Walk all ObjectRefs contained in the resolved object.
        walk_refs(obj, 0, &mut queue)?;
    }

    Ok(visited)
}

/// Recursively push every `Object::Reference` found inside `obj` onto `queue`.
///
/// This is a pure structural walk — it does not resolve any references; the
/// caller drives resolution in the BFS/DFS loop.
fn walk_refs(obj: &Object, depth: usize, queue: &mut Vec<ObjectRef>) -> Result<()> {
    if depth > MAX_INLINE_DEPTH {
        return Err(crate::Error::Unsupported(format!(
            "subset prune: inline object nesting exceeds maximum of {MAX_INLINE_DEPTH}"
        )));
    }
    match obj {
        Object::Reference(r) => {
            queue.push(*r);
        }
        Object::Array(arr) => {
            for item in arr {
                walk_refs(item, depth + 1, queue)?;
            }
        }
        Object::Dictionary(dict) => {
            for (_, val) in dict.iter() {
                walk_refs(val, depth + 1, queue)?;
            }
        }
        Object::Stream(stream) => {
            // Walk the stream dictionary; the stream data itself contains no
            // nested PDF object references at the indirect-object level.
            for (_, val) in stream.dict.iter() {
                walk_refs(val, depth + 1, queue)?;
            }
        }
        // Scalar values (null, boolean, integer, real, name, string) carry
        // no references.
        _ => {}
    }
    Ok(())
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::check::check_reader;
    use crate::object::MAX_INLINE_DEPTH;
    use crate::page_tree_rebuild::rebuild_page_tree;
    use crate::pages::page_refs;
    use crate::writer::write_pdf;
    use crate::{Object, ObjectRef, Pdf};
    use std::collections::BTreeMap;
    use std::io::Cursor;

    // ── Inline-depth guard ───────────────────────────────────────────────────

    fn nested_arrays(depth: usize) -> Object {
        let mut o = Object::Null;
        for _ in 0..depth {
            o = Object::Array(vec![o]);
        }
        o
    }

    #[test]
    fn walk_refs_errors_on_excessive_nesting() {
        let mut queue = Vec::new();
        let err = walk_refs(&nested_arrays(MAX_INLINE_DEPTH + 5), 0, &mut queue);
        assert!(matches!(err, Err(crate::Error::Unsupported(_))));
    }

    #[test]
    fn walk_refs_accepts_nesting_up_to_the_limit() {
        let mut queue = Vec::new();
        // Bury one Reference so it is visited at exactly inline depth
        // MAX_INLINE_DEPTH (the deepest accepted level under the strict `>`
        // guard); it must be collected, not errored.
        let mut o = Object::Array(vec![Object::Reference(ObjectRef::new(9, 0))]);
        for _ in 0..(MAX_INLINE_DEPTH - 1) {
            o = Object::Array(vec![o]);
        }
        walk_refs(&o, 0, &mut queue).unwrap();
        assert_eq!(queue, vec![ObjectRef::new(9, 0)]);
    }

    // ── Fixture builders ─────────────────────────────────────────────────────

    /// Build a 2-page PDF where each page has its own dedicated /Resources with
    /// a single font (F1 on page 1, F2 on page 2).  The two pages do NOT share
    /// any /Resources object.
    ///
    /// Object layout:
    ///   1  Catalog  (/Pages 2)
    ///   2  Pages root  (/Kids [3 7])
    ///   3  Page 1 dict  (/Contents 4, /Resources << /Font 5 0 R >>)
    ///   4  Content stream for page 1 (uses /F1)
    ///   5  Font dict for page 1  (<< /F1 6 0 R >>)
    ///   6  Font F1 object
    ///   7  Page 2 dict  (/Contents 8, /Resources << /Font 9 0 R >>)
    ///   8  Content stream for page 2 (uses /F2)
    ///   9  Font dict for page 2  (<< /F2 10 0 R >>)
    ///   10 Font F2 object
    fn build_two_page_distinct_fonts() -> Vec<u8> {
        let c1 = b"BT /F1 12 Tf 10 10 Td (Page1) Tj ET";
        let c2 = b"BT /F2 12 Tf 10 10 Td (Page2) Tj ET";

        let mut out: Vec<u8> = b"%PDF-1.4\n".to_vec();
        let mut offs: BTreeMap<u32, u64> = BTreeMap::new();

        let objs: Vec<(u32, String)> = vec![
            (1, "<< /Type /Catalog /Pages 2 0 R >>".into()),
            (2, "<< /Type /Pages /Kids [3 0 R 7 0 R] /Count 2 >>".into()),
            (
                3,
                "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] \
                 /Contents 4 0 R /Resources << /Font 5 0 R >> >>"
                    .into(),
            ),
            // 4 = content stream, written below
            (5, "<< /F1 6 0 R >>".into()),
            (
                6,
                "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".into(),
            ),
            (
                7,
                "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] \
                 /Contents 8 0 R /Resources << /Font 9 0 R >> >>"
                    .into(),
            ),
            // 8 = content stream, written below
            (9, "<< /F2 10 0 R >>".into()),
            (
                10,
                "<< /Type /Font /Subtype /Type1 /BaseFont /Courier >>".into(),
            ),
        ];

        // Write non-stream objects.
        let stream_placeholder = [(4u32, c1.as_ref()), (8u32, c2.as_ref())];

        // Write in order 1,2,3 then insert 4, then 5,6,7,8,9,10 etc.
        for (n, s) in &objs {
            if *n < 4 {
                offs.insert(*n, out.len() as u64);
                out.extend_from_slice(format!("{n} 0 obj\n{s}\nendobj\n").as_bytes());
            }
        }
        // stream 4
        offs.insert(4, out.len() as u64);
        out.extend_from_slice(format!("4 0 obj\n<< /Length {} >>\nstream\n", c1.len()).as_bytes());
        out.extend_from_slice(c1);
        out.extend_from_slice(b"\nendstream\nendobj\n");

        for (n, s) in &objs {
            if *n >= 5 && *n < 8 {
                offs.insert(*n, out.len() as u64);
                out.extend_from_slice(format!("{n} 0 obj\n{s}\nendobj\n").as_bytes());
            }
        }
        // stream 8
        offs.insert(8, out.len() as u64);
        out.extend_from_slice(format!("8 0 obj\n<< /Length {} >>\nstream\n", c2.len()).as_bytes());
        out.extend_from_slice(c2);
        out.extend_from_slice(b"\nendstream\nendobj\n");

        for (n, s) in &objs {
            if *n >= 9 {
                offs.insert(*n, out.len() as u64);
                out.extend_from_slice(format!("{n} 0 obj\n{s}\nendobj\n").as_bytes());
            }
        }

        let _ = stream_placeholder; // silence unused warning

        let xref_start = out.len() as u64;
        let total = 11u32;
        out.extend_from_slice(format!("xref\n0 {total}\n0000000000 65535 f \n").as_bytes());
        for i in 1..total {
            out.extend_from_slice(format!("{:010} 00000 n \n", offs[&i]).as_bytes());
        }
        out.extend_from_slice(
            format!("trailer\n<< /Size {total} /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );
        out
    }

    /// Build a PDF with an intermediate /Pages node carrying shared /Resources.
    ///
    /// Object layout:
    ///   1  Catalog
    ///   2  Pages root  (/Kids [3])
    ///   3  Intermediate Pages  (/Kids [4, 5], /Resources 6 0 R with F1+F2)
    ///   4  Page 1 dict  (/Contents 7)
    ///   5  Page 2 dict  (/Contents 8)
    ///   6  Resources dict with F1, F2
    ///   7  Content stream page 1 (uses F1 only)
    ///   8  Content stream page 2 (uses F2 only)
    fn build_shared_resources_pdf() -> Vec<u8> {
        let c1 = b"BT /F1 12 Tf 10 10 Td (P1) Tj ET";
        let c2 = b"BT /F2 12 Tf 10 10 Td (P2) Tj ET";

        let mut out: Vec<u8> = b"%PDF-1.4\n".to_vec();
        let mut offs: BTreeMap<u32, u64> = BTreeMap::new();

        let dicts: Vec<(u32, String)> = vec![
            (1, "<< /Type /Catalog /Pages 2 0 R >>".into()),
            (2, "<< /Type /Pages /Kids [3 0 R] /Count 2 >>".into()),
            (
                3,
                "<< /Type /Pages /Parent 2 0 R /Kids [4 0 R 5 0 R] /Count 2 \
                 /Resources 6 0 R >>"
                    .into(),
            ),
            (
                4,
                "<< /Type /Page /Parent 3 0 R /MediaBox [0 0 612 792] /Contents 7 0 R >>".into(),
            ),
            (
                5,
                "<< /Type /Page /Parent 3 0 R /MediaBox [0 0 612 792] /Contents 8 0 R >>".into(),
            ),
            (
                6,
                "<< /Font << \
                 /F1 << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >> \
                 /F2 << /Type /Font /Subtype /Type1 /BaseFont /Courier >> \
                 >> >>"
                    .into(),
            ),
        ];

        for (n, s) in &dicts {
            offs.insert(*n, out.len() as u64);
            out.extend_from_slice(format!("{n} 0 obj\n{s}\nendobj\n").as_bytes());
        }

        offs.insert(7, out.len() as u64);
        out.extend_from_slice(format!("7 0 obj\n<< /Length {} >>\nstream\n", c1.len()).as_bytes());
        out.extend_from_slice(c1);
        out.extend_from_slice(b"\nendstream\nendobj\n");

        offs.insert(8, out.len() as u64);
        out.extend_from_slice(format!("8 0 obj\n<< /Length {} >>\nstream\n", c2.len()).as_bytes());
        out.extend_from_slice(c2);
        out.extend_from_slice(b"\nendstream\nendobj\n");

        let xref_start = out.len() as u64;
        let total = 9u32;
        out.extend_from_slice(format!("xref\n0 {total}\n0000000000 65535 f \n").as_bytes());
        for i in 1..total {
            out.extend_from_slice(format!("{:010} 00000 n \n", offs[&i]).as_bytes());
        }
        out.extend_from_slice(
            format!("trailer\n<< /Size {total} /Root 1 0 R >>\nstartxref\n{xref_start}\n%%EOF\n")
                .as_bytes(),
        );
        out
    }

    fn open(bytes: Vec<u8>) -> Pdf<Cursor<Vec<u8>>> {
        Pdf::open(Cursor::new(bytes)).expect("PDF should parse")
    }

    // ── Helper ────────────────────────────────────────────────────────────────

    /// True if the given ObjectRef resolves to a non-null live object.
    fn is_live(pdf: &mut Pdf<Cursor<Vec<u8>>>, r: ObjectRef) -> bool {
        pdf.live_object_refs().contains(&r)
    }

    // ── Tests: distinct fonts per page ───────────────────────────────────────

    /// After extracting page 1 (which uses F1), page2 objects should be
    /// garbage-collected; F1 must remain; F2 must be gone.
    #[test]
    fn auto_drops_page2_objects_and_f2_font() {
        let bytes = build_two_page_distinct_fonts();
        let mut pdf = open(bytes);

        // Rebuild to keep only page 1 (obj 3).
        rebuild_page_tree(&mut pdf, &[ObjectRef::new(3, 0)]).unwrap();

        prune_after_subset(&mut pdf, RemoveUnreferencedResources::Auto).unwrap();

        // xref-level: page2 objects (7=page2, 8=content, 9=fontdict, 10=font) should be deleted.
        assert!(
            !is_live(&mut pdf, ObjectRef::new(7, 0)),
            "page2 dict should be deleted"
        );
        assert!(
            !is_live(&mut pdf, ObjectRef::new(8, 0)),
            "page2 content should be deleted"
        );
        assert!(
            !is_live(&mut pdf, ObjectRef::new(9, 0)),
            "page2 /Font dict should be deleted"
        );
        assert!(
            !is_live(&mut pdf, ObjectRef::new(10, 0)),
            "font F2 should be deleted"
        );

        // Name-level: page1 /Font entry for F1 must survive.
        // (obj 5 = font dict { /F1 6 0 R }; obj 6 = font F1)
        assert!(
            is_live(&mut pdf, ObjectRef::new(5, 0)),
            "font dict for page1 should survive"
        );
        assert!(
            is_live(&mut pdf, ObjectRef::new(6, 0)),
            "font F1 should survive"
        );

        // Output must still be valid.
        let mut out: Vec<u8> = Vec::new();
        write_pdf(&mut pdf, &mut out).unwrap();
        let report = check_reader(Cursor::new(out)).unwrap();
        assert!(
            report.valid,
            "pruned PDF must be valid: {:?}",
            report.diagnostics
        );
    }

    /// Yes mode: same expectation as Auto for this case (each page has its own
    /// resources, so Auto and Yes behave identically).
    #[test]
    fn yes_drops_page2_objects_same_as_auto() {
        let bytes = build_two_page_distinct_fonts();
        let mut pdf = open(bytes);

        rebuild_page_tree(&mut pdf, &[ObjectRef::new(3, 0)]).unwrap();
        prune_after_subset(&mut pdf, RemoveUnreferencedResources::Yes).unwrap();

        assert!(!is_live(&mut pdf, ObjectRef::new(7, 0)));
        assert!(!is_live(&mut pdf, ObjectRef::new(10, 0)));
        assert!(is_live(&mut pdf, ObjectRef::new(6, 0)));
    }

    /// No mode: nothing deleted — all original objects survive.
    #[test]
    fn no_mode_preserves_all_objects() {
        let bytes = build_two_page_distinct_fonts();
        let mut pdf = open(bytes);

        // Even after rebuild, No mode must not delete anything.
        rebuild_page_tree(&mut pdf, &[ObjectRef::new(3, 0)]).unwrap();
        prune_after_subset(&mut pdf, RemoveUnreferencedResources::No).unwrap();

        // All original objects 1-10 should still be live.
        for n in 1u32..=10 {
            assert!(
                is_live(&mut pdf, ObjectRef::new(n, 0)),
                "obj {n} should be live in No mode"
            );
        }
    }

    /// Shared resource: when both pages are retained, the shared intermediate
    /// /Pages node and its /Resources must NOT be garbage-collected.
    #[test]
    fn shared_resources_survive_when_both_pages_retained() {
        let bytes = build_shared_resources_pdf();
        let mut pdf = open(bytes);

        // Keep both pages (4 and 5).
        rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0), ObjectRef::new(5, 0)]).unwrap();
        prune_after_subset(&mut pdf, RemoveUnreferencedResources::Auto).unwrap();

        // After rebuild, the intermediate /Pages node (3) becomes orphan
        // because rebuild_page_tree makes leaves point directly to the root.
        // The /Resources dict (6) was materialized onto the leaves.
        // Object 3 (intermediate node) is now orphaned and should be GC'd.
        // Objects 4, 5 (pages), 6 (resources), 7, 8 (streams) should survive.
        assert!(
            !is_live(&mut pdf, ObjectRef::new(3, 0)),
            "intermediate /Pages node (obj 3) must be GC'd after rebuild+prune"
        );
        assert!(
            is_live(&mut pdf, ObjectRef::new(4, 0)),
            "page1 must survive"
        );
        assert!(
            is_live(&mut pdf, ObjectRef::new(5, 0)),
            "page2 must survive"
        );
        assert!(
            is_live(&mut pdf, ObjectRef::new(7, 0)),
            "content stream 1 must survive"
        );
        assert!(
            is_live(&mut pdf, ObjectRef::new(8, 0)),
            "content stream 2 must survive"
        );

        // Output should be valid.
        let mut out: Vec<u8> = Vec::new();
        write_pdf(&mut pdf, &mut out).unwrap();
        let report = check_reader(Cursor::new(out)).unwrap();
        assert!(
            report.valid,
            "PDF with both pages should be valid after prune: {:?}",
            report.diagnostics
        );
    }

    /// Extract page 1 from shared-resources PDF.
    /// After rebuild, page1 leaf has materialized /Resources (inline) with F1+F2.
    /// After prune (Auto), F2 entry must be removed from the inline /Resources,
    /// and page2 objects must be GC'd.
    #[test]
    fn auto_extracts_page1_from_shared_resources_prunes_f2() {
        let bytes = build_shared_resources_pdf();
        let mut pdf = open(bytes);

        // Keep only page 1.
        rebuild_page_tree(&mut pdf, &[ObjectRef::new(4, 0)]).unwrap();
        prune_after_subset(&mut pdf, RemoveUnreferencedResources::Auto).unwrap();

        // xref-level: intermediate /Pages node (3), page2 (5) and its content
        // stream (8) should be gone.
        assert!(
            !is_live(&mut pdf, ObjectRef::new(3, 0)),
            "intermediate /Pages node (obj 3) must be GC'd"
        );
        assert!(
            !is_live(&mut pdf, ObjectRef::new(5, 0)),
            "page2 must be GC'd"
        );
        assert!(
            !is_live(&mut pdf, ObjectRef::new(8, 0)),
            "page2 content must be GC'd"
        );

        // Page 1 must survive.
        assert!(
            is_live(&mut pdf, ObjectRef::new(4, 0)),
            "page1 must survive"
        );
        assert!(
            is_live(&mut pdf, ObjectRef::new(7, 0)),
            "page1 content must survive"
        );

        // Name-level: page1's materialized inline /Resources should have F1 but not F2.
        let page1 = match pdf.resolve_borrowed(ObjectRef::new(4, 0)).unwrap() {
            Object::Dictionary(d) => d,
            other => panic!("page1 not a dict: {other:?}"),
        };
        let res_dict = match page1.get("Resources") {
            Some(Object::Dictionary(d)) => d.clone(),
            other => panic!("page1 /Resources not an inline dict: {other:?}"),
        };
        let font_dict = match res_dict.get("Font") {
            Some(Object::Dictionary(d)) => d.clone(),
            other => panic!("page1 /Font not a dict: {other:?}"),
        };
        let font_keys: Vec<String> = font_dict
            .iter()
            .map(|(k, _)| String::from_utf8(k.to_vec()).unwrap())
            .collect();
        assert!(
            font_keys.contains(&"F1".to_string()),
            "F1 must remain: {font_keys:?}"
        );
        assert!(
            !font_keys.contains(&"F2".to_string()),
            "F2 must be pruned: {font_keys:?}"
        );

        // Valid output.
        let mut out: Vec<u8> = Vec::new();
        write_pdf(&mut pdf, &mut out).unwrap();
        let report = check_reader(Cursor::new(out)).unwrap();
        assert!(
            report.valid,
            "pruned PDF must be valid: {:?}",
            report.diagnostics
        );
    }

    /// Build a 2-page PDF where the trailer has an /Info reference.
    /// After extracting page 1, the /Info object must NOT be GC'd.
    ///
    /// Object layout:
    ///   1  Catalog  (/Pages 2)
    ///   2  Pages root  (/Kids [3 4])
    ///   3  Page 1 dict
    ///   4  Page 2 dict
    ///   5  /Info dict  (referenced from trailer, NOT from /Root)
    fn build_pdf_with_info() -> Vec<u8> {
        let mut out: Vec<u8> = b"%PDF-1.4\n".to_vec();
        let mut offs: BTreeMap<u32, u64> = BTreeMap::new();

        let objs: Vec<(u32, &str)> = vec![
            (1, "<< /Type /Catalog /Pages 2 0 R >>"),
            (2, "<< /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >>"),
            (3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>"),
            (4, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>"),
            (5, "<< /Title (Test Document) /Author (Test Author) >>"),
        ];

        for (n, s) in &objs {
            offs.insert(*n, out.len() as u64);
            out.extend_from_slice(format!("{n} 0 obj\n{s}\nendobj\n").as_bytes());
        }

        let xref_start = out.len() as u64;
        let total = 6u32;
        out.extend_from_slice(format!("xref\n0 {total}\n0000000000 65535 f \n").as_bytes());
        for i in 1..total {
            out.extend_from_slice(format!("{:010} 00000 n \n", offs[&i]).as_bytes());
        }
        // Trailer references /Info 5 0 R directly — not through /Root.
        out.extend_from_slice(
            format!(
                "trailer\n<< /Size {total} /Root 1 0 R /Info 5 0 R >>\nstartxref\n{xref_start}\n%%EOF\n"
            )
            .as_bytes(),
        );
        out
    }

    /// Regression: /Info object referenced from the trailer (not from /Root)
    /// must NOT be deleted by the xref-level GC pass.
    #[test]
    fn trailer_info_object_survives_gc() {
        let bytes = build_pdf_with_info();
        let mut pdf = open(bytes);

        // Keep only page 1 (obj 3); page 2 (obj 4) becomes unreachable.
        rebuild_page_tree(&mut pdf, &[ObjectRef::new(3, 0)]).unwrap();
        prune_after_subset(&mut pdf, RemoveUnreferencedResources::Auto).unwrap();

        // /Info (obj 5) is referenced from the trailer — it must survive.
        assert!(
            is_live(&mut pdf, ObjectRef::new(5, 0)),
            "/Info object (trailer ref) must NOT be GC'd"
        );

        // Page 2 (obj 4) is not reachable from anywhere and must be GC'd.
        assert!(
            !is_live(&mut pdf, ObjectRef::new(4, 0)),
            "page 2 should be GC'd"
        );

        // Output must still be valid.
        let mut out: Vec<u8> = Vec::new();
        write_pdf(&mut pdf, &mut out).unwrap();
        let report = check_reader(Cursor::new(out)).unwrap();
        assert!(
            report.valid,
            "pruned PDF with /Info must be valid: {:?}",
            report.diagnostics
        );
    }

    /// Round-trip: prune + serialize + reopen and check page refs.
    #[test]
    fn round_trip_valid_after_prune() {
        let bytes = build_two_page_distinct_fonts();
        let mut pdf = open(bytes);

        rebuild_page_tree(&mut pdf, &[ObjectRef::new(3, 0)]).unwrap();
        prune_after_subset(&mut pdf, RemoveUnreferencedResources::Auto).unwrap();

        let mut out: Vec<u8> = Vec::new();
        write_pdf(&mut pdf, &mut out).unwrap();

        let mut pdf2 = Pdf::open(Cursor::new(out)).unwrap();
        let refs = page_refs(&mut pdf2).unwrap();
        assert_eq!(refs.len(), 1, "output should have exactly 1 page");
    }
}