use crate::page_tree_rebuild::RebuildResult;
use crate::ref_chain::resolve_ref_chain;
use crate::{Dictionary, Object, ObjectRef, Pdf, Result};
use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Seek};
pub fn drop_thread_bead_dangling_p<R: Read + Seek>(
pdf: &mut Pdf<R>,
result: &RebuildResult,
) -> Result<()> {
let surviving: BTreeMap<ObjectRef, ObjectRef> = result
.ref_map
.iter()
.filter_map(|(&old, new_refs)| new_refs.first().map(|&new| (old, new)))
.collect();
let mut queue: Vec<ObjectRef> = Vec::new();
seed_from_threads(pdf, &mut queue)?;
seed_from_surviving_pages(pdf, result, &mut queue)?;
let mut visited: BTreeSet<ObjectRef> = BTreeSet::new();
while let Some(start_ref) = queue.pop() {
let (concrete, terminal) = resolve_ref_chain(pdf, &Object::Reference(start_ref))?;
let bead_ref = terminal.unwrap_or(start_ref);
if !visited.insert(bead_ref) {
continue;
}
let Some(mut bead) = concrete.into_dict() else {
continue;
};
for key in ["N", "V"] {
if let Some(Object::Reference(r)) = bead.get(key) {
queue.push(*r);
}
}
if remap_or_drop_bead_p(pdf, &mut bead, &surviving)? {
pdf.set_object(bead_ref, Object::Dictionary(bead));
}
}
Ok(())
}
fn seed_from_threads<R: Read + Seek>(pdf: &mut Pdf<R>, queue: &mut Vec<ObjectRef>) -> Result<()> {
let Some(catalog_ref) = pdf.root_ref() else {
return Ok(()); };
let threads_val = {
let catalog_obj = pdf.resolve_borrowed(catalog_ref)?;
let Some(catalog) = catalog_obj.as_dict() else {
return Ok(());
};
catalog.get("Threads").cloned()
};
let Some(threads_val) = threads_val else {
return Ok(()); };
let (threads_concrete, _) = resolve_ref_chain(pdf, &threads_val)?;
let Object::Array(threads) = threads_concrete else {
return Ok(());
};
for thread in &threads {
if let Some(first_bead) = thread_first_bead(pdf, thread)? {
queue.push(first_bead);
}
}
Ok(())
}
fn thread_first_bead<R: Read + Seek>(
pdf: &mut Pdf<R>,
thread: &Object,
) -> Result<Option<ObjectRef>> {
let (concrete, _) = resolve_ref_chain(pdf, thread)?;
let Some(dict) = concrete.as_dict() else {
return Ok(None);
};
let Some(f_val) = dict.get("F") else {
return Ok(None);
};
let (_, terminal) = resolve_ref_chain(pdf, f_val)?;
Ok(terminal)
}
fn seed_from_surviving_pages<R: Read + Seek>(
pdf: &mut Pdf<R>,
result: &RebuildResult,
queue: &mut Vec<ObjectRef>,
) -> Result<()> {
let mut seen_pages: BTreeSet<ObjectRef> = BTreeSet::new();
for &page_ref in &result.new_kids {
if !seen_pages.insert(page_ref) {
continue;
}
let b_val = {
let page_obj = pdf.resolve_borrowed(page_ref)?;
page_obj.as_dict().and_then(|p| p.get("B")).cloned()
};
let Some(b_val) = b_val else {
continue;
};
let (b_concrete, _) = resolve_ref_chain(pdf, &b_val)?;
if let Object::Array(beads) = b_concrete {
for bead in &beads {
if let Some(r) = bead.as_ref_id() {
queue.push(r);
}
}
}
}
Ok(())
}
fn remap_or_drop_bead_p<R: Read + Seek>(
pdf: &mut Pdf<R>,
bead: &mut Dictionary,
surviving: &BTreeMap<ObjectRef, ObjectRef>,
) -> Result<bool> {
let Some(p_val) = bead.get("P").cloned() else {
return Ok(false);
};
let (p_concrete, p_terminal) = resolve_ref_chain(pdf, &p_val)?;
let Some(page_ref) = p_terminal else {
return Ok(false); };
if !is_page_dict(&p_concrete) && !matches!(p_concrete, Object::Null) {
return Ok(false); }
match surviving.get(&page_ref) {
Some(&new) if new != page_ref => {
bead.insert("P", Object::Reference(new));
Ok(true)
}
Some(_) => Ok(false), None => {
bead.remove("P");
Ok(true)
}
}
}
fn is_page_dict(obj: &Object) -> bool {
obj.as_dict()
.and_then(|d| d.get("Type"))
.is_some_and(|t| matches!(t, Object::Name(n) if n == b"Page"))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Pdf;
use std::collections::BTreeMap;
use std::io::Cursor;
fn build_pdf(objs: &BTreeMap<u32, String>) -> Vec<u8> {
build_pdf_inner(objs, true)
}
fn build_pdf_inner(objs: &BTreeMap<u32, String>, with_root: bool) -> Vec<u8> {
let mut raw: Vec<u8> = b"%PDF-1.5\n".to_vec();
let mut offs: BTreeMap<u32, usize> = BTreeMap::new();
for (n, body) in objs {
offs.insert(*n, raw.len());
raw.extend_from_slice(format!("{n} 0 obj\n{body}\nendobj\n").as_bytes());
}
let max_num = *objs.keys().max().unwrap();
let xref_pos = raw.len();
raw.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", max_num + 1).as_bytes());
for i in 1..=max_num {
if let Some(&off) = offs.get(&i) {
raw.extend_from_slice(format!("{off:010} 00000 n \n").as_bytes());
} else {
raw.extend_from_slice(b"0000000000 65535 f \n");
}
}
let root = if with_root { " /Root 1 0 R" } else { "" };
raw.extend_from_slice(
format!(
"trailer\n<< /Size {}{root} >>\nstartxref\n{xref_pos}\n%%EOF\n",
max_num + 1
)
.as_bytes(),
);
raw
}
fn base_objs() -> BTreeMap<u32, String> {
let mut objs: BTreeMap<u32, String> = BTreeMap::new();
objs.insert(
1,
"<< /Type /Catalog /Pages 2 0 R /Threads [10 0 R] >>".into(),
);
objs.insert(
2,
"<< /Type /Pages /Kids [3 0 R 4 0 R 5 0 R] /Count 3 >>".into(),
);
objs.insert(
3,
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /B [11 0 R] >>".into(),
);
objs.insert(
4,
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /B [12 0 R] >>".into(),
);
objs.insert(
5,
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /B [13 0 R] >>".into(),
);
objs.insert(10, "<< /Type /Thread /F 11 0 R >>".into());
objs.insert(
11,
"<< /Type /Bead /T 10 0 R /N 12 0 R /V 13 0 R /P 3 0 R /R [0 0 100 100] >>".into(),
);
objs.insert(
12,
"<< /Type /Bead /T 10 0 R /N 13 0 R /V 11 0 R /P 4 0 R /R [0 0 100 100] >>".into(),
);
objs.insert(
13,
"<< /Type /Bead /T 10 0 R /N 11 0 R /V 12 0 R /P 5 0 R /R [0 0 100 100] >>".into(),
);
objs
}
fn base_objs_no_b() -> BTreeMap<u32, String> {
let mut objs = base_objs();
for n in 3..=5 {
objs.insert(
n,
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>".into(),
);
}
objs
}
fn open(objs: &BTreeMap<u32, String>) -> Pdf<Cursor<Vec<u8>>> {
Pdf::open(Cursor::new(build_pdf(objs))).expect("open fixture")
}
fn keep_3_and_5() -> RebuildResult {
let mut ref_map: BTreeMap<ObjectRef, Vec<ObjectRef>> = BTreeMap::new();
ref_map.insert(ObjectRef::new(3, 0), vec![ObjectRef::new(3, 0)]);
ref_map.insert(ObjectRef::new(5, 0), vec![ObjectRef::new(5, 0)]);
RebuildResult {
new_kids: vec![ObjectRef::new(3, 0), ObjectRef::new(5, 0)],
ref_map,
..Default::default()
}
}
fn bead_dict(pdf: &mut Pdf<Cursor<Vec<u8>>>, num: u32) -> Dictionary {
pdf.resolve(ObjectRef::new(num, 0))
.expect("resolve bead")
.into_dict()
.expect("bead object is a dictionary")
}
#[test]
fn removed_bead_p_dropped_and_bead_kept() {
let mut pdf = open(&base_objs());
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("bead /P drop");
let bead12 = bead_dict(&mut pdf, 12);
let p12 = bead12.get("P");
assert!(
p12.is_none(),
"bead 12 /P (removed page) must be dropped, got {p12:?}"
);
let n12 = bead12.get("N");
assert!(
matches!(n12, Some(Object::Reference(r)) if r.number == 13),
"bead 12 /N must be kept, got {n12:?}"
);
let v12 = bead12.get("V");
assert!(
matches!(v12, Some(Object::Reference(r)) if r.number == 11),
"bead 12 /V must be kept, got {v12:?}"
);
assert!(bead12.get("T").is_some(), "bead 12 /T must be kept");
assert!(bead12.get("R").is_some(), "bead 12 /R must be kept");
let bead11 = bead_dict(&mut pdf, 11);
let p11 = bead11.get("P");
assert!(
matches!(p11, Some(Object::Reference(r)) if r.number == 3),
"bead 11 /P (surviving page 3) must be kept, got {p11:?}"
);
let bead13 = bead_dict(&mut pdf, 13);
let p13 = bead13.get("P");
assert!(
matches!(p13, Some(Object::Reference(r)) if r.number == 5),
"bead 13 /P (surviving page 5) must be kept, got {p13:?}"
);
}
#[test]
fn surviving_bead_p_remapped_to_new_ref() {
let mut pdf = open(&base_objs());
let mut ref_map: BTreeMap<ObjectRef, Vec<ObjectRef>> = BTreeMap::new();
ref_map.insert(ObjectRef::new(3, 0), vec![ObjectRef::new(7, 0)]);
ref_map.insert(ObjectRef::new(5, 0), vec![ObjectRef::new(5, 0)]);
let result = RebuildResult {
new_kids: vec![ObjectRef::new(7, 0), ObjectRef::new(5, 0)],
ref_map,
..Default::default()
};
drop_thread_bead_dangling_p(&mut pdf, &result).expect("bead /P remap");
let bead11 = bead_dict(&mut pdf, 11);
let p11 = bead11.get("P");
assert!(
matches!(p11, Some(Object::Reference(r)) if r.number == 7),
"bead 11 /P (surviving page under new ref) must be remapped, got {p11:?}"
);
let bead13 = bead_dict(&mut pdf, 13);
let p13 = bead13.get("P");
assert!(
matches!(p13, Some(Object::Reference(r)) if r.number == 5),
"bead 13 /P (surviving page, identity) must be kept, got {p13:?}"
);
}
#[test]
fn duplicate_selection_keeps_bead_p_at_first_occurrence() {
let mut pdf = open(&base_objs());
let mut ref_map: BTreeMap<ObjectRef, Vec<ObjectRef>> = BTreeMap::new();
ref_map.insert(
ObjectRef::new(3, 0),
vec![ObjectRef::new(3, 0), ObjectRef::new(7, 0)],
);
ref_map.insert(ObjectRef::new(5, 0), vec![ObjectRef::new(5, 0)]);
let result = RebuildResult {
new_kids: vec![
ObjectRef::new(3, 0),
ObjectRef::new(7, 0),
ObjectRef::new(5, 0),
],
ref_map,
..Default::default()
};
drop_thread_bead_dangling_p(&mut pdf, &result).expect("duplicate-page bead /P");
let p11 = bead_dict(&mut pdf, 11).get("P").cloned();
assert!(
matches!(p11, Some(Object::Reference(r)) if r.number == 3),
"a duplicated page's bead /P must stay at the first occurrence (page 3), got {p11:?}"
);
assert!(
bead_dict(&mut pdf, 12).get("P").is_none(),
"the removed-page bead /P must still be dropped under a duplicate selection"
);
}
fn objs_with_beadless_page() -> BTreeMap<u32, String> {
let mut objs = base_objs();
objs.insert(
2,
"<< /Type /Pages /Kids [3 0 R 4 0 R 5 0 R 6 0 R] /Count 4 >>".into(),
);
objs.insert(
6,
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>".into(),
);
objs
}
#[test]
fn all_beads_dangling_keeps_thread_and_ring_and_terminates() {
let mut pdf = open(&objs_with_beadless_page());
let mut ref_map: BTreeMap<ObjectRef, Vec<ObjectRef>> = BTreeMap::new();
ref_map.insert(ObjectRef::new(6, 0), vec![ObjectRef::new(6, 0)]);
let result = RebuildResult {
new_kids: vec![ObjectRef::new(6, 0)],
ref_map,
..Default::default()
};
drop_thread_bead_dangling_p(&mut pdf, &result).expect("all-dangling drop");
for num in [11, 12, 13] {
let bead = bead_dict(&mut pdf, num);
let p = bead.get("P");
assert!(
p.is_none(),
"bead {num} /P must be dropped (all dangling), got {p:?}"
);
assert!(bead.get("N").is_some(), "bead {num} /N must be kept");
assert!(bead.get("V").is_some(), "bead {num} /V must be kept");
}
let thread = bead_dict(&mut pdf, 10);
let f = thread.get("F");
assert!(
matches!(f, Some(Object::Reference(r)) if r.number == 11),
"thread /F must be kept, got {f:?}"
);
}
#[test]
fn seeds_from_surviving_page_b_without_threads() {
let mut objs = base_objs();
objs.insert(1, "<< /Type /Catalog /Pages 2 0 R >>".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("/B seeding");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
p12.is_none(),
"bead reached only via a surviving page's /B must have its dangling /P dropped, got {p12:?}"
);
let p11 = bead_dict(&mut pdf, 11).get("P").cloned();
assert!(
matches!(p11, Some(Object::Reference(r)) if r.number == 3),
"surviving bead /P reached via /B must be kept, got {p11:?}"
);
}
#[test]
fn direct_thread_dict_in_threads_is_processed() {
let mut objs = base_objs_no_b();
objs.insert(
1,
"<< /Type /Catalog /Pages 2 0 R /Threads [ << /Type /Thread /F 11 0 R >> ] >>".into(),
);
objs.remove(&10); let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("direct thread dict");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
p12.is_none(),
"a direct (inline) thread dictionary's dangling bead /P must be dropped, got {p12:?}"
);
}
#[test]
fn bead_p_reference_chain_is_resolved() {
let mut objs = base_objs_no_b();
objs.insert(
11,
"<< /Type /Bead /T 10 0 R /N 12 0 R /V 13 0 R /P 21 0 R /R [0 0 100 100] >>".into(),
);
objs.insert(
12,
"<< /Type /Bead /T 10 0 R /N 13 0 R /V 11 0 R /P 20 0 R /R [0 0 100 100] >>".into(),
);
objs.insert(20, "4 0 R".into()); objs.insert(21, "3 0 R".into()); let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("/P chain");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
p12.is_none(),
"a chained /P to a removed page must be dropped, got {p12:?}"
);
let p11 = bead_dict(&mut pdf, 11).get("P").cloned();
assert!(
matches!(p11, Some(Object::Reference(r)) if r.number == 21),
"a chained /P to a surviving page must be kept verbatim, got {p11:?}"
);
}
#[test]
fn ring_neighbour_reference_chain_is_followed() {
let mut objs = base_objs_no_b();
objs.insert(
11,
"<< /Type /Bead /T 10 0 R /N 22 0 R /V 13 0 R /P 3 0 R /R [0 0 100 100] >>".into(),
);
objs.insert(22, "12 0 R".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("/N chain");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
p12.is_none(),
"a bead reached only through a chained /N must have its dangling /P dropped, got {p12:?}"
);
}
#[test]
fn threads_multi_hop_reference_chain_is_resolved() {
let mut objs = base_objs_no_b();
objs.insert(
1,
"<< /Type /Catalog /Pages 2 0 R /Threads 14 0 R >>".into(),
);
objs.insert(14, "15 0 R".into());
objs.insert(15, "[10 0 R]".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("multi-hop /Threads");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
p12.is_none(),
"a thread reached through a multi-hop /Threads chain must be walked, got {p12:?}"
);
}
#[test]
fn non_dict_thread_entry_and_thread_without_f_skipped() {
let mut objs = base_objs_no_b();
objs.insert(
1,
"<< /Type /Catalog /Pages 2 0 R /Threads [42 10 0 R 15 0 R] >>".into(),
);
objs.insert(15, "<< /Type /Thread >>".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("mixed /Threads");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
p12.is_none(),
"the real thread's dangling bead /P must still be dropped, got {p12:?}"
);
}
#[test]
fn thread_f_non_reference_is_skipped() {
let mut objs = base_objs_no_b();
objs.insert(10, "<< /Type /Thread /F 0 >>".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("non-ref /F");
assert!(
bead_dict(&mut pdf, 12).get("P").is_some(),
"a thread whose /F is not a reference yields no first bead"
);
}
#[test]
fn non_array_threads_with_no_b_is_a_noop() {
let mut objs = base_objs_no_b();
objs.insert(1, "<< /Type /Catalog /Pages 2 0 R /Threads 42 >>".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("noop");
assert!(
bead_dict(&mut pdf, 12).get("P").is_some(),
"a non-array /Threads with no /B must leave beads untouched"
);
}
#[test]
fn no_threads_no_b_is_a_noop() {
let mut objs = base_objs_no_b();
objs.insert(1, "<< /Type /Catalog /Pages 2 0 R >>".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("noop");
assert!(
bead_dict(&mut pdf, 12).get("P").is_some(),
"with neither /Threads nor /B, beads are untouched"
);
}
#[test]
fn surviving_page_b_non_array_is_skipped() {
let mut objs = base_objs();
objs.insert(1, "<< /Type /Catalog /Pages 2 0 R >>".into());
objs.insert(
3,
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /B 99 >>".into(),
);
objs.insert(
5,
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /B 99 >>".into(),
);
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("noop");
assert!(
bead_dict(&mut pdf, 12).get("P").is_some(),
"a non-array /B must be ignored"
);
}
#[test]
fn malformed_non_ref_p_left_unchanged() {
let mut objs = base_objs();
objs.insert(
12,
"<< /Type /Bead /T 10 0 R /N 13 0 R /V 11 0 R /P 999 /R [0 0 100 100] >>".into(),
);
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("malformed /P");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
matches!(p12, Some(Object::Integer(999))),
"a non-reference /P must be left unchanged, got {p12:?}"
);
}
#[test]
fn p_resolving_to_non_page_left_unchanged() {
let mut objs = base_objs();
objs.insert(
12,
"<< /Type /Bead /T 10 0 R /N 13 0 R /V 11 0 R /P 30 0 R /R [0 0 100 100] >>".into(),
);
objs.insert(30, "<< /Type /Whatever >>".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("non-page /P");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
matches!(p12, Some(Object::Reference(r)) if r.number == 30),
"a /P resolving to a non-page object must be left unchanged, got {p12:?}"
);
}
#[test]
fn bead_p_to_nulled_removed_page_still_dropped() {
let mut objs = base_objs();
objs.insert(4, "null".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("nulled-page /P drop");
let p12 = bead_dict(&mut pdf, 12).get("P").cloned();
assert!(
p12.is_none(),
"a bead /P to a removed page nulled by the dest null-out pass must still be dropped, got {p12:?}"
);
let p11 = bead_dict(&mut pdf, 11).get("P").cloned();
assert!(
matches!(p11, Some(Object::Reference(r)) if r.number == 3),
"surviving-page bead /P must be kept, got {p11:?}"
);
}
#[test]
fn bead_without_p_is_left_unchanged() {
let mut objs = base_objs();
objs.insert(
12,
"<< /Type /Bead /T 10 0 R /N 13 0 R /V 11 0 R /R [0 0 100 100] >>".into(),
);
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("bead without /P");
let bead12 = bead_dict(&mut pdf, 12);
assert!(bead12.get("P").is_none(), "bead 12 still has no /P");
assert!(
bead_dict(&mut pdf, 11).get("P").is_some(),
"the rest of the ring is still walked"
);
}
#[test]
fn non_dict_bead_in_ring_skipped() {
let mut objs = base_objs_no_b();
objs.insert(
11,
"<< /Type /Bead /T 10 0 R /N 16 0 R /V 13 0 R /P 3 0 R /R [0 0 100 100] >>".into(),
);
objs.insert(16, "42".into());
objs.insert(10, "<< /Type /Thread /F 12 0 R >>".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("non-dict neighbour");
assert!(
bead_dict(&mut pdf, 12).get("P").is_none(),
"bead 12 dangling /P must be dropped despite a non-dict ring neighbour"
);
}
#[test]
fn non_dict_catalog_still_seeds_from_b() {
let mut objs = base_objs();
objs.insert(1, "42".into());
let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("non-dict catalog");
assert!(
bead_dict(&mut pdf, 12).get("P").is_none(),
"a non-dictionary catalog still permits /B-seeded bead /P drop"
);
}
#[test]
fn no_root_still_seeds_from_b() {
let mut objs = base_objs();
objs.remove(&1); let raw = build_pdf_inner(&objs, false);
let mut pdf = Pdf::open(Cursor::new(raw)).expect("open no-root fixture");
assert!(pdf.root_ref().is_none(), "fixture must have no /Root");
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("no-root /B seeding");
assert!(
bead_dict(&mut pdf, 12).get("P").is_none(),
"a missing catalog still permits /B-seeded bead /P drop"
);
}
#[test]
fn non_dict_surviving_page_is_skipped() {
let mut objs = base_objs();
objs.insert(1, "<< /Type /Catalog /Pages 2 0 R >>".into());
objs.insert(3, "42".into()); let mut pdf = open(&objs);
drop_thread_bead_dangling_p(&mut pdf, &keep_3_and_5()).expect("non-dict surviving page");
assert!(
bead_dict(&mut pdf, 12).get("P").is_none(),
"a non-dict surviving page is skipped but the others are still walked"
);
}
#[test]
fn duplicate_surviving_page_ref_in_new_kids_is_deduped() {
let mut objs = base_objs();
objs.insert(1, "<< /Type /Catalog /Pages 2 0 R >>".into());
let mut pdf = open(&objs);
let mut ref_map: BTreeMap<ObjectRef, Vec<ObjectRef>> = BTreeMap::new();
ref_map.insert(ObjectRef::new(3, 0), vec![ObjectRef::new(3, 0)]);
ref_map.insert(ObjectRef::new(5, 0), vec![ObjectRef::new(5, 0)]);
let result = RebuildResult {
new_kids: vec![
ObjectRef::new(3, 0),
ObjectRef::new(3, 0),
ObjectRef::new(5, 0),
],
ref_map,
..Default::default()
};
drop_thread_bead_dangling_p(&mut pdf, &result).expect("dedup");
assert!(
bead_dict(&mut pdf, 12).get("P").is_none(),
"a duplicated page ref in new_kids must not break /B seeding"
);
}
}