use std::collections::BTreeSet;
use laminapdf_cos::{Dict, Object, Result};
use crate::document::Document;
use crate::page::Pages;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FontResource {
pub name: Option<String>,
pub subtype: Option<String>,
pub embedded: bool,
pub subset: bool,
pub encoding: Option<FontEncoding>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FontEncoding {
Named(String),
Custom,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
enum FontKey {
Object(u32),
Direct(Option<String>, Option<String>),
}
impl Pages<'_> {
pub fn fonts(&self) -> Result<Vec<FontResource>> {
let mut walk = Walk {
doc: self.document(),
max_depth: self.document().limits().max_tree_depth(),
seen_containers: BTreeSet::new(),
seen_fonts: BTreeSet::new(),
out: Vec::new(),
};
for page in self.iter() {
if let Some(resources) = page.resources() {
walk.resources(resources, 0)?;
}
}
Ok(walk.out)
}
}
struct Walk<'d> {
doc: &'d Document,
max_depth: usize,
seen_containers: BTreeSet<u32>,
seen_fonts: BTreeSet<FontKey>,
out: Vec<FontResource>,
}
impl Walk<'_> {
fn resources(&mut self, resources: &Dict, depth: usize) -> Result<()> {
if depth > self.max_depth {
return Ok(());
}
if let Some(fonts) = self.dict(resources.get("Font"))? {
for (_, value) in fonts.iter() {
self.font(value, depth)?;
}
}
if let Some(xobjects) = self.dict(resources.get("XObject"))? {
for (_, value) in xobjects.iter() {
if let Some(r) = value.as_ref()
&& !self.seen_containers.insert(r.num)
{
continue;
}
let Some(object) = self.doc.read(value)? else { continue };
let Object::Stream(form) = &*object else { continue };
if form.dict.get_name("Subtype") != Some(b"Form") {
continue;
}
if let Some(inner) = self.dict(form.dict.get("Resources"))? {
self.resources(&inner, depth + 1)?;
}
}
}
Ok(())
}
fn font(&mut self, value: &Object, depth: usize) -> Result<()> {
let Some(object) = self.doc.read(value)? else {
return Ok(());
};
let Some(font) = object.as_dict() else { return Ok(()) };
let reference = value.as_ref().map(|r| r.num);
let subtype = font.get_name("Subtype").map(lossy);
let base = font.get_name("BaseFont").map(lossy);
let key = match reference {
Some(num) => FontKey::Object(num),
None => FontKey::Direct(subtype.clone(), base.clone()),
};
if !self.seen_fonts.insert(key) {
return Ok(());
}
let subset = base.as_deref().is_some_and(has_subset_tag);
let name = base.map(|b| {
if subset {
b.get(7..).unwrap_or_default().to_owned()
} else {
b
}
});
let type3 = subtype.as_deref() == Some("Type3");
let descriptor_font = if subtype.as_deref() == Some("Type0") {
self.descendant(font)?
} else {
None
};
let embedded = type3 || self.embeds(descriptor_font.as_ref().unwrap_or(font))?;
let encoding = self.encoding(font)?;
self.out.push(FontResource {
name,
subtype,
embedded,
subset,
encoding,
});
if type3 {
if let Some(r) = value.as_ref()
&& !self.seen_containers.insert(r.num)
{
return Ok(());
}
if let Some(inner) = self.dict(font.get("Resources"))? {
self.resources(&inner, depth + 1)?;
}
}
Ok(())
}
fn descendant(&self, font: &Dict) -> Result<Option<Dict>> {
let Some(list) = font.get("DescendantFonts") else {
return Ok(None);
};
let Some(list) = self.doc.read(list)? else {
return Ok(None);
};
let Some(first) = list.as_array().and_then(<[Object]>::first) else {
return Ok(None);
};
self.dict(Some(first))
}
fn embeds(&self, font: &Dict) -> Result<bool> {
let Some(descriptor) = self.dict(font.get("FontDescriptor"))? else {
return Ok(false);
};
Ok(["FontFile", "FontFile2", "FontFile3"]
.iter()
.any(|key| descriptor.get(key).is_some()))
}
fn encoding(&self, font: &Dict) -> Result<Option<FontEncoding>> {
let Some(value) = font.get("Encoding") else {
return Ok(None);
};
Ok(match self.doc.read(value)?.as_deref() {
Some(Object::Name(n)) => Some(FontEncoding::Named(lossy(n.as_bytes()))),
Some(Object::Dict(_) | Object::Stream(_)) => Some(FontEncoding::Custom),
_ => None,
})
}
fn dict(&self, value: Option<&Object>) -> Result<Option<Dict>> {
let Some(value) = value else { return Ok(None) };
Ok(match self.doc.read(value)?.as_deref() {
Some(Object::Dict(d)) => Some(d.clone()),
Some(Object::Stream(s)) => Some(s.dict.clone()),
_ => None,
})
}
}
fn lossy(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).into_owned()
}
fn has_subset_tag(name: &str) -> bool {
let bytes = name.as_bytes();
bytes.len() > 7 && bytes.get(6) == Some(&b'+') && bytes.iter().take(6).all(u8::is_ascii_uppercase)
}
#[cfg(test)]
mod tests {
use super::has_subset_tag;
#[test]
fn a_subset_tag_is_six_capitals_and_a_plus() {
assert!(has_subset_tag("ABCDEF+NotoSans-Regular"));
assert!(!has_subset_tag("ABCDEf+NotoSans"));
assert!(!has_subset_tag("ABCDEF+"));
assert!(!has_subset_tag("Helvetica"));
}
}