use std::{
collections::{BTreeMap, BTreeSet},
sync::Arc,
};
use lopdf::{
Dictionary as LopdfDictionary, Document as LopdfDocument, Object, ObjectId, Stream as LopdfStream,
};
use super::pattern::{pattern_space_names, read_scope_patterns, PatternCtx};
use super::{
convert_operations, decode_content, decode_limited, dictitem_from_lopdf_resolved, font_resources,
get_dict_or_resolve_ref, parse_rect, parse_xobjects_internal, parsefont, read_extgstates,
read_shadings, warn_if_budget_spent, Decoded, FontTextDecodeKind, InlineBudget,
ParsedOrBuiltinFont, PdfWarnMsg, PendingXObject, MAX_STREAM_BYTES,
};
use crate::{
form_walk::MAX_FORM_DEPTH,
matrix::CurTransMat,
ops::PdfFontHandle,
xobject::{FormType, FormXObject, GroupXObject, GroupXObjectType},
DictItem, ExtendedGraphicsState, ExtendedGraphicsStateId, ExtendedGraphicsStateMap, FontId, Op,
Pattern, PatternId, PatternMap, PdfFontMap, PdfLayerMap, PdfResources, Shading, ShadingId,
ShadingMap, XObjectId, XObjectMap,
};
const MAX_FORMS: usize = 100_000;
const MAX_FORM_CONTENT_BYTES: usize = 256 * 1024 * 1024;
const FORM_KEYS: &[&[u8]] = &[
b"Type",
b"Subtype",
b"FormType",
b"BBox",
b"Matrix",
b"Resources",
b"Group",
b"Ref",
b"Metadata",
b"PieceInfo",
b"LastModified",
b"StructParent",
b"StructParents",
b"OPI",
b"OC",
b"Name",
b"Length",
b"Filter",
b"DecodeParms",
b"Measure",
b"PtData",
b"AF",
];
const EXTRA_KEYS: &[&str] = &[
"Ref",
"Metadata",
"PieceInfo",
"LastModified",
"OPI",
"OC",
"Measure",
"PtData",
"AF",
];
const RESOURCE_KEYS: &[&[u8]] = &[
b"Font",
b"XObject",
b"ExtGState",
b"Shading",
b"ColorSpace",
b"Pattern",
b"Properties",
b"ProcSet",
];
const RAW_CATEGORIES: &[&str] = &["ColorSpace", "Properties", "ProcSet"];
#[derive(Clone)]
pub(crate) struct PendingForm {
pub(crate) form: FormXObject,
pub(crate) xobjects: BTreeMap<XObjectId, PendingXObject>,
}
pub(crate) struct XObjectCtx<'a> {
pub(super) budget: InlineBudget,
font_decode: &'a BTreeMap<String, FontTextDecodeKind>,
cache: BTreeMap<ObjectId, Result<Arc<PendingForm>, String>>,
stack: Vec<ObjectId>,
forms_left: usize,
bytes_left: usize,
reported_budget: bool,
patterns: PatternCtx,
}
impl<'a> XObjectCtx<'a> {
pub(crate) fn new(font_decode: &'a BTreeMap<String, FontTextDecodeKind>) -> Self {
XObjectCtx {
budget: InlineBudget::new(),
font_decode,
cache: BTreeMap::new(),
stack: Vec::new(),
forms_left: MAX_FORMS,
bytes_left: MAX_FORM_CONTENT_BYTES,
reported_budget: false,
patterns: PatternCtx::new(),
}
}
}
pub(super) fn parse_form(
doc: &LopdfDocument,
value: &Object,
stream: &LopdfStream,
name: &XObjectId,
page: usize,
ctx: &mut XObjectCtx<'_>,
warnings: &mut Vec<PdfWarnMsg>,
) -> Option<Arc<PendingForm>> {
let id = match value {
Object::Reference(id) => Some(*id),
_ => None,
};
if let Some(id) = id {
if let Some(done) = ctx.cache.get(&id) {
return done.clone().ok();
}
if ctx.stack.contains(&id) {
warnings.push(PdfWarnMsg::info(
page,
0,
format!(
"form XObject {} has itself in its resources; that one is kept as it is",
name.0
),
));
return None;
}
}
if ctx.stack.len() >= MAX_FORM_DEPTH {
warnings.push(PdfWarnMsg::info(
page,
0,
format!(
"form XObject {} is nested in more than {MAX_FORM_DEPTH} forms and is kept as it is",
name.0
),
));
return None;
}
if ctx.forms_left == 0 || ctx.bytes_left == 0 {
if !ctx.reported_budget {
ctx.reported_budget = true;
warnings.push(PdfWarnMsg::warning(
page,
0,
format!(
"the document has more than {MAX_FORMS} forms or {MAX_FORM_CONTENT_BYTES} \
bytes of form content; the forms from {} on are kept as they are",
name.0
),
));
}
return None;
}
ctx.forms_left = ctx.forms_left.saturating_sub(1);
ctx.stack.extend(id);
let result = read_form(doc, stream, page, ctx, warnings).map(Arc::new);
if id.is_some() {
ctx.stack.pop();
}
if let Some(id) = id {
ctx.cache.insert(id, result.clone());
}
match result {
Ok(form) => {
warn_if_budget_spent(&mut ctx.budget, name, page, warnings);
Some(form)
}
Err(reason) => {
warnings.push(PdfWarnMsg::info(
page,
0,
format!("form XObject {} is kept as it is: {reason}", name.0),
));
None
}
}
}
fn read_form(
doc: &LopdfDocument,
stream: &LopdfStream,
page: usize,
ctx: &mut XObjectCtx<'_>,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PendingForm, String> {
let dict = &stream.dict;
if let Some(key) = dict
.iter()
.map(|(key, _)| key)
.find(|key| !FORM_KEYS.contains(&key.as_slice()))
{
return Err(format!(
"its dictionary has the entry /{}, which is not modelled",
String::from_utf8_lossy(key)
));
}
let bbox = dict
.get(b"BBox")
.ok()
.and_then(|bbox| parse_rect(deref(doc, bbox)).ok())
.ok_or_else(|| "it has no usable /BBox".to_string())?;
let matrix = match dict.get(b"Matrix") {
Err(_) => None,
Ok(matrix) => Some(
parse_matrix(deref(doc, matrix))
.ok_or_else(|| "its /Matrix is not six numbers".to_string())?,
),
};
let limit = ctx.bytes_left.min(MAX_STREAM_BYTES);
let content = match decode_limited(stream, limit) {
Decoded::Data(data) => data,
Decoded::Undecodable => return Err("its content stream cannot be decoded".to_string()),
Decoded::TooLarge => return Err("its content stream is too large".to_string()),
};
ctx.bytes_left = ctx.bytes_left.saturating_sub(content.len());
let operations = decode_content(&content)?;
let mut scope = match dict.get(b"Resources") {
Err(_) => None,
Ok(resources) => {
let Some(resources) = get_dict_or_resolve_ref(
"form /Resources",
doc,
resources,
warnings,
Some(page),
) else {
return Err("its /Resources is not a dictionary".to_string());
};
Some(read_scope(doc, resources, page, ctx, warnings)?)
}
};
let font_decode = match &scope {
Some(scope) => scope.font_decode(),
None => ctx.font_decode.clone(),
};
let no_pattern_spaces = BTreeSet::new();
let pattern_spaces = scope
.as_ref()
.map_or(&no_pattern_spaces, |scope| &scope.pattern_spaces);
let ops = convert_operations(
page,
&operations,
&BTreeMap::new(),
&font_decode,
pattern_spaces,
warnings,
)?;
let fonts = match scope.as_mut() {
Some(scope) => font_resources(std::mem::take(&mut scope.fonts).into_iter()),
None => BTreeMap::new(),
};
check_content(
&ops,
scope.as_ref().map(|scope| (&fonts, &scope.patterns)),
)?;
let mut extra = BTreeMap::new();
let mut group = None;
if let Ok(object) = dict.get(b"Group") {
let item = inline(doc, object, &mut ctx.budget);
if is_plain_transparency_group(&item) {
group = Some(GroupXObject {
group_type: GroupXObjectType::TransparencyGroup,
});
} else {
extra.insert("Group".to_string(), item);
}
}
for key in EXTRA_KEYS {
if let Ok(object) = dict.get(key.as_bytes()) {
extra.insert((*key).to_string(), inline(doc, object, &mut ctx.budget));
}
}
let integer = |key: &[u8]| {
dict.get(key)
.ok()
.and_then(|object| deref(doc, object).as_i64().ok())
};
let form_name = match dict.get(b"Name").ok().map(|object| deref(doc, object)) {
Some(Object::String(bytes, _)) | Some(Object::Name(bytes)) => {
Some(String::from_utf8_lossy(bytes).into_owned())
}
_ => None,
};
let (resources, xobjects) = match scope {
None => (None, BTreeMap::new()),
Some(scope) => {
if !scope.raw.is_empty() {
extra.insert("Resources".to_string(), DictItem::Dict { map: scope.raw });
}
let resources = PdfResources {
fonts: PdfFontMap { map: fonts },
xobjects: XObjectMap::default(),
extgstates: ExtendedGraphicsStateMap {
map: scope.extgstates,
},
shadings: ShadingMap { map: scope.shadings },
patterns: PatternMap { map: scope.patterns },
layers: PdfLayerMap::default(),
};
(Some(resources), scope.xobjects)
}
};
Ok(PendingForm {
form: FormXObject {
form_type: FormType::Type1,
size: None,
bbox,
matrix,
ops,
resources,
group,
ref_dict: None,
metadata: None,
piece_info: None,
last_modified: None,
struct_parent: integer(b"StructParent"),
struct_parents: integer(b"StructParents"),
opi: None,
oc: None,
name: form_name,
extra,
},
xobjects,
})
}
struct FormScope {
fonts: BTreeMap<FontId, ParsedOrBuiltinFont>,
xobjects: BTreeMap<XObjectId, PendingXObject>,
extgstates: BTreeMap<ExtendedGraphicsStateId, ExtendedGraphicsState>,
shadings: BTreeMap<ShadingId, Shading>,
patterns: BTreeMap<PatternId, Pattern>,
pattern_spaces: BTreeSet<String>,
raw: BTreeMap<String, DictItem>,
}
impl FormScope {
fn font_decode(&self) -> BTreeMap<String, FontTextDecodeKind> {
self.fonts
.iter()
.map(|(id, font)| (id.0.clone(), font.decode_kind()))
.collect()
}
}
fn read_scope(
doc: &LopdfDocument,
resources: &LopdfDictionary,
page: usize,
ctx: &mut XObjectCtx<'_>,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<FormScope, String> {
if let Some(key) = resources
.iter()
.map(|(key, _)| key)
.find(|key| !RESOURCE_KEYS.contains(&key.as_slice()))
{
return Err(format!(
"its /Resources has the entry /{}, which is not modelled",
String::from_utf8_lossy(key)
));
}
let fonts =
parsefont::parse_fonts_enhanced(doc, resources, warnings, Some(page), &BTreeMap::new());
let mut xobjects = BTreeMap::new();
parse_xobjects_internal(doc, resources, warnings, Some(page), &mut xobjects, ctx);
let extgstates = read_extgstates(doc, resources, page, warnings).unwrap_or_default();
let mut shadings = BTreeMap::new();
read_shadings(doc, resources, page, &mut shadings, warnings);
let mut raw = BTreeMap::new();
for category in RAW_CATEGORIES {
if let Ok(object) = resources.get(category.as_bytes()) {
raw.insert((*category).to_string(), inline(doc, object, &mut ctx.budget));
}
}
keep_lossy_extgstates(doc, resources, &extgstates, &mut ctx.budget, &mut raw);
keep_unmodelled_shadings(doc, resources, &shadings, &mut ctx.budget, &mut raw);
let patterns = read_scope_patterns(doc, resources, page, &mut ctx.patterns, warnings)?;
let pattern_spaces = pattern_space_names(doc, resources);
Ok(FormScope {
fonts,
xobjects,
extgstates,
shadings,
patterns,
pattern_spaces,
raw,
})
}
type ScopeNames<'a> = (
&'a BTreeMap<FontId, crate::font::PdfFont>,
&'a BTreeMap<PatternId, Pattern>,
);
fn check_content(ops: &[Op], scope: Option<ScopeNames<'_>>) -> Result<(), String> {
if ops.iter().any(|op| matches!(op, Op::Unknown { .. })) {
return Err("its content has an operator that no Op models".to_string());
}
for op in ops {
match op {
Op::SetFillPattern { id, .. } | Op::SetOutlinePattern { id, .. } => {
match scope {
Some((_, patterns)) if patterns.contains_key(id) => {}
_ => return Err(format!("its content sets the pattern {}, which it has not", id.0)),
}
}
Op::SetFont {
font: PdfFontHandle::External(id),
..
} => {
if let Some((fonts, _)) = scope {
if !fonts.contains_key(id) {
return Err(format!("it selects the font {}, which has no font program", id.0));
}
}
}
_ => {}
}
}
Ok(())
}
fn deref<'a>(doc: &'a LopdfDocument, object: &'a Object) -> &'a Object {
match object {
Object::Reference(id) => doc.get_object(*id).unwrap_or(object),
other => other,
}
}
fn parse_matrix(object: &Object) -> Option<CurTransMat> {
let Object::Array(entries) = object else {
return None;
};
let mut matrix = [0.0f32; 6];
if entries.len() != matrix.len() {
return None;
}
for (slot, entry) in matrix.iter_mut().zip(entries) {
*slot = match entry {
Object::Integer(i) => *i as f32,
Object::Real(r) => *r,
_ => return None,
};
}
Some(CurTransMat::Raw(matrix))
}
fn inline(doc: &LopdfDocument, object: &Object, budget: &mut InlineBudget) -> DictItem {
dictitem_from_lopdf_resolved(doc, object, 0, &mut Vec::new(), budget)
}
fn is_plain_transparency_group(group: &DictItem) -> bool {
let DictItem::Dict { map } = group else {
return false;
};
map.keys().all(|key| key == "Type" || key == "S")
&& matches!(map.get("S"), Some(DictItem::Name(name)) if name.as_slice() == b"Transparency")
}
fn category_dict<'a>(
doc: &'a LopdfDocument,
resources: &'a LopdfDictionary,
key: &[u8],
) -> Option<&'a LopdfDictionary> {
let object = resources.get(key).ok()?;
get_dict_or_resolve_ref("form resources", doc, object, &mut Vec::new(), None)
}
fn keep_lossy_extgstates(
doc: &LopdfDocument,
resources: &LopdfDictionary,
typed: &BTreeMap<ExtendedGraphicsStateId, ExtendedGraphicsState>,
budget: &mut InlineBudget,
raw: &mut BTreeMap<String, DictItem>,
) {
let Some(category) = category_dict(doc, resources, b"ExtGState") else {
return;
};
let mut kept = BTreeMap::new();
for (name, state) in typed {
let Ok(entry) = category.get(name.0.as_bytes()) else {
continue;
};
let Some(source) =
get_dict_or_resolve_ref("form graphics state", doc, entry, &mut Vec::new(), None)
else {
continue;
};
let written = crate::graphics::extgstate_to_dict(state);
if source
.iter()
.any(|(key, _)| key.as_slice() != b"Type" && !written.has(key))
{
kept.insert(name.0.clone(), inline(doc, entry, budget));
}
}
if !kept.is_empty() {
raw.insert("ExtGState".to_string(), DictItem::Dict { map: kept });
}
}
fn keep_unmodelled_shadings(
doc: &LopdfDocument,
resources: &LopdfDictionary,
typed: &BTreeMap<ShadingId, Shading>,
budget: &mut InlineBudget,
raw: &mut BTreeMap<String, DictItem>,
) {
let Some(category) = category_dict(doc, resources, b"Shading") else {
return;
};
let mut kept = BTreeMap::new();
for (key, entry) in category.iter() {
let name = String::from_utf8_lossy(key).into_owned();
let lossy = match typed.get(&ShadingId(name.clone())) {
None => true,
Some(shading) => {
let written = shading.to_dict();
get_dict_or_resolve_ref("form shading", doc, entry, &mut Vec::new(), None)
.is_none_or(|source| source.iter().any(|(key, _)| !written.has(key)))
}
};
if lossy {
kept.insert(name, inline(doc, entry, budget));
}
}
if !kept.is_empty() {
raw.insert("Shading".to_string(), DictItem::Dict { map: kept });
}
}