use crate::content_stream::{ContentStreamParser, ContentToken};
use crate::filters::decode_stream_data;
use crate::{Dictionary, Error, Object, ObjectRef, Pdf, Result};
use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Seek};
type UsedNames = BTreeMap<Vec<u8>, BTreeSet<Vec<u8>>>;
type CatRefKey = (Vec<u8>, ObjectRef);
struct CatRefInfo {
group_count: usize,
used_union: BTreeSet<Vec<u8>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum ResGroupKey {
Indirect(ObjectRef),
AncestorInline(ObjectRef),
PageInline(ObjectRef),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum ResourcesLoc {
None,
PageInline,
AncestorInline(ObjectRef),
Indirect(ObjectRef),
}
const RESOURCE_CATEGORIES: &[&str] = &[
"Font",
"XObject",
"ColorSpace",
"Pattern",
"Shading",
"ExtGState",
"Properties",
];
fn is_builtin_color_space_cs_op(name: &[u8]) -> bool {
matches!(
name,
b"DeviceGray" | b"DeviceRGB" | b"DeviceCMYK" | b"Pattern"
)
}
fn is_builtin_inline_image_cs(name: &[u8]) -> bool {
matches!(
name,
b"DeviceGray"
| b"DeviceRGB"
| b"DeviceCMYK"
| b"Pattern"
| b"G"
| b"RGB"
| b"CMYK"
| b"I"
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RemoveUnreferencedResources {
#[default]
Auto,
Yes,
No,
}
pub fn remove_unreferenced_resources<R: Read + Seek>(
pdf: &mut Pdf<R>,
mode: RemoveUnreferencedResources,
) -> Result<()> {
if mode == RemoveUnreferencedResources::No {
return Ok(());
}
let page_refs = crate::pages::page_refs(pdf)?;
let mut page_res_loc: Vec<ResourcesLoc> = Vec::with_capacity(page_refs.len());
for &pr in &page_refs {
page_res_loc.push(resources_location(pdf, pr)?);
}
let mut ref_count: BTreeMap<ObjectRef, usize> = BTreeMap::new();
let mut ancestor_count: BTreeMap<ObjectRef, usize> = BTreeMap::new();
for loc in &page_res_loc {
match loc {
ResourcesLoc::Indirect(r) => {
*ref_count.entry(*r).or_insert(0) += 1;
}
ResourcesLoc::AncestorInline(a) => {
*ancestor_count.entry(*a).or_insert(0) += 1;
}
_ => {}
}
}
let mut ref_used: BTreeMap<ObjectRef, UsedNames> = BTreeMap::new();
let mut ancestor_inline_used: BTreeMap<ObjectRef, UsedNames> = BTreeMap::new();
let mut inline_used: Vec<Option<UsedNames>> = vec![None; page_refs.len()];
for (i, &page_ref) in page_refs.iter().enumerate() {
let is_shared = match &page_res_loc[i] {
ResourcesLoc::Indirect(r) => ref_count.get(r).copied().unwrap_or(0) > 1,
ResourcesLoc::AncestorInline(a) => ancestor_count.get(a).copied().unwrap_or(0) > 1,
_ => false, };
if mode == RemoveUnreferencedResources::Auto && is_shared {
continue;
}
let used = collect_used_names_for_page(pdf, page_ref)?;
match &page_res_loc[i] {
ResourcesLoc::Indirect(r) => {
let entry = ref_used.entry(*r).or_default();
for (cat, names) in used {
entry.entry(cat).or_default().extend(names);
}
}
ResourcesLoc::AncestorInline(a) => {
let entry = ancestor_inline_used.entry(*a).or_default();
for (cat, names) in used {
entry.entry(cat).or_default().extend(names);
}
}
ResourcesLoc::PageInline => {
inline_used[i] = Some(used);
}
ResourcesLoc::None => {}
}
}
let mut cat_ref_map: BTreeMap<CatRefKey, CatRefInfo> = BTreeMap::new();
let mut cat_ref_seen_groups: BTreeMap<CatRefKey, BTreeSet<ResGroupKey>> = BTreeMap::new();
for (i, loc) in page_res_loc.iter().enumerate() {
let res_dict_opt: Option<Dictionary> = match loc {
ResourcesLoc::Indirect(r) => match pdf.resolve(*r) {
Ok(Object::Dictionary(d)) => Some(d),
_ => None,
},
ResourcesLoc::PageInline => {
match pdf.resolve(page_refs[i]) {
Ok(Object::Dictionary(page_dict)) => {
match page_dict.get("Resources").cloned() {
Some(Object::Dictionary(d)) => Some(d),
_ => None,
}
}
_ => None,
}
}
ResourcesLoc::AncestorInline(a) => match pdf.resolve(*a) {
Ok(Object::Dictionary(anc_dict)) => match anc_dict.get("Resources").cloned() {
Some(Object::Dictionary(d)) => Some(d),
_ => None,
},
_ => None,
},
ResourcesLoc::None => None,
};
let Some(res_dict) = res_dict_opt else {
continue;
};
let group_key: ResGroupKey = match loc {
ResourcesLoc::Indirect(r) => ResGroupKey::Indirect(*r),
ResourcesLoc::AncestorInline(a) => ResGroupKey::AncestorInline(*a),
ResourcesLoc::PageInline => ResGroupKey::PageInline(page_refs[i]),
ResourcesLoc::None => unreachable!("None loc filtered above by res_dict_opt"),
};
let group_used: Option<&UsedNames> = match loc {
ResourcesLoc::Indirect(r) => ref_used.get(r),
ResourcesLoc::AncestorInline(a) => ancestor_inline_used.get(a),
ResourcesLoc::PageInline => inline_used[i].as_ref(),
ResourcesLoc::None => None,
};
for &category in RESOURCE_CATEGORIES {
let cat_key = category.as_bytes();
if let Some(cat_ref) = res_dict.get(category).and_then(Object::as_ref_id) {
let key: CatRefKey = (cat_key.to_vec(), cat_ref);
let seen = cat_ref_seen_groups.entry(key.clone()).or_default();
if seen.insert(group_key) {
let info = cat_ref_map
.entry(key.clone())
.or_insert_with(|| CatRefInfo {
group_count: 0,
used_union: BTreeSet::new(),
});
info.group_count += 1;
if let Some(used_map) = group_used {
if let Some(names) = used_map.get(cat_key) {
info.used_union.extend(names.iter().cloned());
}
}
}
}
}
}
for (res_ref, used) in &ref_used {
prune_resources_object(pdf, *res_ref, used, &cat_ref_map, mode)?;
}
for (ancestor_ref, used) in &ancestor_inline_used {
prune_ancestor_inline_resources(pdf, *ancestor_ref, used, &cat_ref_map, mode)?;
}
for (i, used_opt) in inline_used.iter().enumerate() {
let Some(used) = used_opt else { continue };
let page_ref = page_refs[i];
prune_inline_resources(pdf, page_ref, used, &cat_ref_map, mode)?;
}
Ok(())
}
fn resources_location<R: Read + Seek>(
pdf: &mut Pdf<R>,
page_ref: ObjectRef,
) -> Result<ResourcesLoc> {
let mut current = page_ref;
let mut seen: BTreeSet<ObjectRef> = BTreeSet::new();
let mut depth = 0usize;
const MAX_DEPTH: usize = 100;
loop {
if depth >= MAX_DEPTH {
return Ok(ResourcesLoc::None);
}
if !seen.insert(current) {
return Ok(ResourcesLoc::None); }
depth += 1;
let node_obj = pdf.resolve(current)?;
let Some(dict) = node_obj.into_dict() else {
return Ok(ResourcesLoc::None);
};
match dict.get("Resources").cloned() {
Some(Object::Reference(r)) => return Ok(ResourcesLoc::Indirect(r)),
Some(Object::Dictionary(_)) => {
if current == page_ref {
return Ok(ResourcesLoc::PageInline);
} else {
return Ok(ResourcesLoc::AncestorInline(current));
}
}
Some(Object::Null) | None => {
match dict.get("Parent").cloned() {
Some(Object::Reference(parent)) => {
current = parent;
}
_ => return Ok(ResourcesLoc::None),
}
}
_ => return Ok(ResourcesLoc::None),
}
}
}
fn collect_used_names_for_page<R: Read + Seek>(
pdf: &mut Pdf<R>,
page_ref: ObjectRef,
) -> Result<UsedNames> {
let content_bytes = crate::pages::page_content_bytes(pdf, page_ref)?;
let page_resources = crate::pages::resolve_inherited_resources(pdf, page_ref)?;
let mut used: UsedNames = BTreeMap::new();
let mut visited_xobjects: BTreeSet<ObjectRef> = BTreeSet::new();
collect_from_stream(
pdf,
&content_bytes,
page_resources.as_ref(),
&mut used,
&mut visited_xobjects,
0,
)?;
Ok(used)
}
const MAX_FORM_DEPTH: usize = 64;
fn collect_from_stream<R: Read + Seek>(
pdf: &mut Pdf<R>,
stream_bytes: &[u8],
resources: Option<&Dictionary>,
used: &mut UsedNames,
visited: &mut BTreeSet<ObjectRef>,
depth: usize,
) -> Result<()> {
let parser = ContentStreamParser::new(stream_bytes);
for token_result in parser {
let Ok(token) = token_result else { break };
match token {
ContentToken::Op { operands, operator } => {
process_operator(pdf, &operator, &operands, resources, used, visited, depth)?;
}
ContentToken::InlineImage { dict, .. } => {
let cs_val = dict.get("CS").or_else(|| dict.get("ColorSpace")).cloned();
if let Some(name) = cs_val.and_then(Object::into_name) {
if !is_builtin_inline_image_cs(&name) {
used.entry(b"ColorSpace".to_vec()).or_default().insert(name);
}
}
}
ContentToken::Comment(_) => {}
}
}
Ok(())
}
fn process_operator<R: Read + Seek>(
pdf: &mut Pdf<R>,
operator: &[u8],
operands: &[Object],
resources: Option<&Dictionary>,
used: &mut UsedNames,
visited: &mut BTreeSet<ObjectRef>,
depth: usize,
) -> Result<()> {
match operator {
b"Do" => {
if let Some(name) = operands.first().and_then(Object::as_name) {
used.entry(b"XObject".to_vec())
.or_default()
.insert(name.to_vec());
recurse_form_xobject(pdf, name, resources, used, visited, depth)?;
}
}
b"Tf" => {
if let Some(name) = operands.first().and_then(Object::as_name) {
used.entry(b"Font".to_vec())
.or_default()
.insert(name.to_vec());
}
}
b"gs" => {
if let Some(name) = operands.first().and_then(Object::as_name) {
used.entry(b"ExtGState".to_vec())
.or_default()
.insert(name.to_vec());
}
}
b"cs" | b"CS" => {
if let Some(name) = operands.first().and_then(Object::as_name) {
if !is_builtin_color_space_cs_op(name) {
used.entry(b"ColorSpace".to_vec())
.or_default()
.insert(name.to_vec());
}
}
}
b"scn" | b"SCN" => {
if let Some(name) = operands.last().and_then(Object::as_name) {
used.entry(b"Pattern".to_vec())
.or_default()
.insert(name.to_vec());
}
}
b"sh" => {
if let Some(name) = operands.first().and_then(Object::as_name) {
used.entry(b"Shading".to_vec())
.or_default()
.insert(name.to_vec());
}
}
b"BDC" | b"DP" => {
if let Some(name) = operands.get(1).and_then(Object::as_name) {
used.entry(b"Properties".to_vec())
.or_default()
.insert(name.to_vec());
}
}
_ => {}
}
Ok(())
}
fn recurse_form_xobject<R: Read + Seek>(
pdf: &mut Pdf<R>,
xobject_name: &[u8],
page_resources: Option<&Dictionary>,
used: &mut UsedNames,
visited: &mut BTreeSet<ObjectRef>,
depth: usize,
) -> Result<()> {
if depth >= MAX_FORM_DEPTH {
return Ok(());
}
let xobj_val: Option<Object> = match page_resources.and_then(|res| res.get("XObject").cloned())
{
Some(Object::Dictionary(xobj_dict)) => xobj_dict.get(xobject_name).cloned(),
Some(Object::Reference(cat_ref)) => match pdf.resolve(cat_ref)? {
Object::Dictionary(xobj_dict) => xobj_dict.get(xobject_name).cloned(),
_ => None,
},
_ => None,
};
let Some(xobj_val) = xobj_val else {
return Ok(());
};
let indirect_ref: Option<ObjectRef> = match &xobj_val {
Object::Reference(r) => Some(*r),
_ => None,
};
let stream: crate::object::Stream = match xobj_val {
Object::Reference(xobj_ref) => {
let obj = pdf.resolve(xobj_ref)?;
match obj {
Object::Stream(s) => s,
_ => return Ok(()),
}
}
Object::Stream(s) => {
s
}
_ => return Ok(()),
};
let is_form = stream
.dict
.get("Subtype")
.is_some_and(|v| matches!(v, Object::Name(n) if n.as_slice() == b"Form"));
if !is_form {
return Ok(());
}
let form_bytes = match decode_stream_data(&stream.dict, &stream.data) {
Ok(b) => b,
Err(_) => return Ok(()), };
let form_has_own_resources = stream.dict.get("Resources").is_some();
if let Some(r) = indirect_ref {
if !visited.insert(r) {
return Ok(()); }
}
if form_has_own_resources {
let form_resources: Option<Dictionary> = match stream.dict.get("Resources").cloned() {
Some(Object::Dictionary(d)) => Some(d),
Some(Object::Reference(r)) => match pdf.resolve(r)? {
Object::Dictionary(d) => Some(d),
_ => None, },
_ => None,
};
let mut form_used: UsedNames = BTreeMap::new();
collect_from_stream(
pdf,
&form_bytes,
form_resources.as_ref(),
&mut form_used,
visited,
depth + 1,
)?;
} else {
collect_from_stream(pdf, &form_bytes, page_resources, used, visited, depth + 1)?;
}
if let Some(r) = indirect_ref {
visited.remove(&r);
}
Ok(())
}
fn prune_resources_object<R: Read + Seek>(
pdf: &mut Pdf<R>,
res_ref: ObjectRef,
used: &UsedNames,
cat_ref_map: &BTreeMap<CatRefKey, CatRefInfo>,
mode: RemoveUnreferencedResources,
) -> Result<()> {
let obj = pdf.resolve(res_ref)?;
let Some(mut res_dict) = obj.into_dict() else {
return Ok(()); };
apply_pruning(pdf, &mut res_dict, used, cat_ref_map, mode)?;
pdf.set_object(res_ref, Object::Dictionary(res_dict));
Ok(())
}
fn prune_inline_resources<R: Read + Seek>(
pdf: &mut Pdf<R>,
page_ref: ObjectRef,
used: &UsedNames,
cat_ref_map: &BTreeMap<CatRefKey, CatRefInfo>,
mode: RemoveUnreferencedResources,
) -> Result<()> {
let page_obj = pdf.resolve(page_ref)?;
let Some(mut page_dict) = page_obj.into_dict() else {
return Err(Error::Unsupported(format!(
"page {page_ref} resolved to non-dictionary"
)));
};
let resources_val = page_dict.get("Resources").cloned();
match resources_val {
Some(Object::Dictionary(mut res_dict)) => {
apply_pruning(pdf, &mut res_dict, used, cat_ref_map, mode)?;
page_dict.insert("Resources", Object::Dictionary(res_dict));
}
_ => return Ok(()), }
pdf.set_object(page_ref, Object::Dictionary(page_dict));
Ok(())
}
fn prune_ancestor_inline_resources<R: Read + Seek>(
pdf: &mut Pdf<R>,
ancestor_ref: ObjectRef,
used: &UsedNames,
cat_ref_map: &BTreeMap<CatRefKey, CatRefInfo>,
mode: RemoveUnreferencedResources,
) -> Result<()> {
let ancestor_obj = pdf.resolve(ancestor_ref)?;
let Some(mut ancestor_dict) = ancestor_obj.into_dict() else {
return Ok(()); };
let resources_val = ancestor_dict.get("Resources").cloned();
match resources_val {
Some(Object::Dictionary(mut res_dict)) => {
apply_pruning(pdf, &mut res_dict, used, cat_ref_map, mode)?;
ancestor_dict.insert("Resources", Object::Dictionary(res_dict));
}
_ => return Ok(()), }
pdf.set_object(ancestor_ref, Object::Dictionary(ancestor_dict));
Ok(())
}
fn apply_pruning<R: Read + Seek>(
pdf: &mut Pdf<R>,
res_dict: &mut Dictionary,
used: &UsedNames,
cat_ref_map: &BTreeMap<CatRefKey, CatRefInfo>,
mode: RemoveUnreferencedResources,
) -> Result<()> {
for &category in RESOURCE_CATEGORIES {
let cat_key = category.as_bytes();
let cat_val = res_dict.get(category).cloned();
match cat_val {
Some(Object::Dictionary(mut cat_dict)) => {
let empty_set = BTreeSet::new();
let used_names = used.get(cat_key).unwrap_or(&empty_set);
let to_remove: Vec<Vec<u8>> = cat_dict
.iter()
.filter(|(k, _)| !used_names.contains(*k))
.map(|(k, _)| k.to_vec())
.collect();
for key in to_remove {
cat_dict.remove(&key);
}
if cat_dict.iter().next().is_none() {
res_dict.remove(category);
} else {
res_dict.insert(category, Object::Dictionary(cat_dict));
}
}
Some(Object::Reference(cat_ref)) => {
let map_key: CatRefKey = (cat_key.to_vec(), cat_ref);
if let Some(info) = cat_ref_map.get(&map_key) {
if mode == RemoveUnreferencedResources::Auto && info.group_count > 1 {
continue;
}
let resolved = pdf.resolve(cat_ref)?;
let Some(mut cat_dict) = resolved.into_dict() else {
continue; };
let to_remove: Vec<Vec<u8>> = cat_dict
.iter()
.filter(|(k, _)| !info.used_union.contains(*k))
.map(|(k, _)| k.to_vec())
.collect();
for key in to_remove {
cat_dict.remove(&key);
}
if cat_dict.iter().next().is_none() {
res_dict.remove(category);
} else {
pdf.set_object(cat_ref, Object::Dictionary(cat_dict));
}
} else {
let resolved = pdf.resolve(cat_ref)?;
let Some(mut cat_dict) = resolved.into_dict() else {
continue;
};
let empty_set = BTreeSet::new();
let used_names = used.get(cat_key).unwrap_or(&empty_set);
let to_remove: Vec<Vec<u8>> = cat_dict
.iter()
.filter(|(k, _)| !used_names.contains(*k))
.map(|(k, _)| k.to_vec())
.collect();
for key in to_remove {
cat_dict.remove(&key);
}
if cat_dict.iter().next().is_none() {
res_dict.remove(category);
} else {
pdf.set_object(cat_ref, Object::Dictionary(cat_dict));
}
}
}
_ => {
continue;
}
}
}
Ok(())
}