use std::collections::BTreeMap;
use std::sync::Arc;
use lopdf::{
Dictionary as LopdfDictionary, Document as LopdfDocument, Object as LopdfObject, Object,
ObjectId, Stream as LopdfStream,
};
use serde_derive::{Deserialize, Serialize};
use crate::{
cmap::ToUnicodeCMap,
conformance::PdfConformance,
date::{parse_pdf_date, OffsetDateTime},
font::ParsedFont,
xobject::{ExternalStream, ExternalXObject},
BuiltinFont, BuiltinOrExternalFontId, Color, DictItem, ExtendedGraphicsState,
ExtendedGraphicsStateId, ExtendedGraphicsStateMap, FontId, LayerInternalId, Line,
LineDashPattern, LinePoint, LinkAnnotation, Op, PageAnnotId, PageAnnotMap, PaintMode,
PdfDocument, PdfDocumentInfo, PdfFontMap, PdfLayerMap, PdfMetadata, PdfPage, PdfResources,
Point, Polygon, PolygonRing, Pt, RawImage, Rect, RenderingIntent, TextMatrix,
TextRenderingMode, WindingOrder, XObject, XObjectId, XObjectMap,
};
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PdfParseOptions {
#[serde(default)]
pub fail_on_error: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PdfWarnMsg {
pub page: usize,
pub op_id: usize,
pub severity: PdfParseErrorSeverity,
pub msg: String,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PdfParseErrorSeverity {
Error,
Info,
Warning,
}
impl PdfWarnMsg {
pub fn error(page: usize, op_id: usize, e: String) -> Self {
PdfWarnMsg {
page,
op_id,
severity: PdfParseErrorSeverity::Error,
msg: e,
}
}
pub fn warning(page: usize, op_id: usize, msg: String) -> Self {
PdfWarnMsg {
page,
op_id,
severity: PdfParseErrorSeverity::Warning,
msg,
}
}
pub fn info(page: usize, op_id: usize, msg: String) -> Self {
PdfWarnMsg {
page,
op_id,
severity: PdfParseErrorSeverity::Info,
msg,
}
}
}
struct InitialPdf {
doc: lopdf::Document,
objs_to_search_for_resources: Vec<(Object, Option<usize>)>,
page_refs: Vec<(u32, u16)>,
document_info: PdfDocumentInfo,
link_annots: Vec<(usize, LinkAnnotation)>,
}
fn parse_pdf_from_bytes_start(
bytes: &[u8],
_opts: &PdfParseOptions,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<InitialPdf, String> {
let doc = LopdfDocument::load_mem(bytes).map_err(|e| format!("Failed to load PDF: {}", e))?;
let mut objs_to_search_for_resources = Vec::new();
let root_obj = doc
.trailer
.get(b"Root")
.map_err(|_| "Missing Root in trailer".to_string())?;
objs_to_search_for_resources.push((root_obj.clone(), None));
let root_ref = match root_obj {
Object::Reference(r) => *r,
_ => return Err("Invalid Root reference".to_string()),
};
let catalog_obj = doc
.get_object(root_ref)
.map_err(|e| format!("Failed to get catalog: {}", e))?;
objs_to_search_for_resources.push((catalog_obj.clone(), None));
let catalog = catalog_obj
.as_dict()
.map_err(|e| format!("Catalog is not a dictionary: {}", e))?;
if let Ok(resources) = catalog.get(b"Resources") {
objs_to_search_for_resources.push((resources.clone(), None));
}
let pages_obj = catalog
.get(b"Pages")
.map_err(|e| format!("Missing Pages key in catalog: {}", e))?;
objs_to_search_for_resources.push((pages_obj.clone(), None));
let pages_dict = match pages_obj {
Object::Reference(r) => doc
.get_object(*r)
.map_err(|e| format!("Failed to get Pages object: {}", e))?
.as_dict()
.map_err(|e| format!("Pages object is not a dictionary: {}", e))?,
Object::Dictionary(d) => {
warnings.push(PdfWarnMsg::warning(
0,
0,
"Catalog /Pages is an inline dictionary instead of a reference".to_string(),
));
d
}
_ => return Err("Pages key is not a reference".to_string()),
};
if let Ok(resources) = pages_dict.get(b"Resources") {
objs_to_search_for_resources.push((resources.clone(), None));
}
let page_refs = collect_page_refs(pages_dict, &doc, warnings)?;
let page_objects = page_refs
.iter()
.enumerate()
.filter_map(|(i, &page_ref)| {
doc.get_object(page_ref)
.ok()
.map(|obj| (obj.clone(), Some(i)))
})
.collect::<Vec<_>>();
objs_to_search_for_resources.extend(page_objects);
let document_info = doc
.trailer
.get(b"Info")
.ok()
.and_then(|s| get_dict_or_resolve_ref("document_info", &doc, s, warnings, None))
.map(parse_document_info)
.unwrap_or_default();
let link_annots = links_and_bookmarks::extract_link_annotations(&doc);
Ok(InitialPdf {
doc,
objs_to_search_for_resources,
page_refs,
document_info,
link_annots,
})
}
pub fn parse_pdf_from_bytes(
bytes: &[u8],
opts: &PdfParseOptions,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PdfDocument, String> {
let i = parse_pdf_from_bytes_start(bytes, opts, warnings)?;
let fonts = parse_fonts_from_entire_pdf(&i.doc, &i.objs_to_search_for_resources, warnings);
let xobjects =
parse_xobjects_from_entire_pdf(&i.doc, &i.objs_to_search_for_resources, warnings);
let xobjects = process_xobjects(xobjects, warnings);
parse_pdf_from_bytes_end(i, opts, fonts, xobjects, warnings)
}
pub async fn parse_pdf_from_bytes_async(
bytes: &[u8],
opts: &PdfParseOptions,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PdfDocument, String> {
let i = parse_pdf_from_bytes_start(bytes, opts, warnings)?;
let fonts = parse_fonts_from_entire_pdf(&i.doc, &i.objs_to_search_for_resources, warnings);
let xobjects =
parse_xobjects_from_entire_pdf(&i.doc, &i.objs_to_search_for_resources, warnings);
let xobjects = process_xobjects_async(xobjects, warnings).await;
parse_pdf_from_bytes_end(i, opts, fonts, xobjects, warnings)
}
fn parse_pdf_from_bytes_end(
initial_pdf: InitialPdf,
_opts: &PdfParseOptions,
fonts: BTreeMap<BuiltinOrExternalFontId, ParsedOrBuiltinFont>,
xobjects: BTreeMap<XObjectId, XObject>,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PdfDocument, String> {
let mut pages = Vec::new();
let InitialPdf {
doc,
objs_to_search_for_resources,
page_refs,
document_info,
link_annots,
} = initial_pdf;
let font_decode: BTreeMap<String, FontTextDecodeKind> = fonts
.iter()
.filter_map(|(id, pf)| {
let name = match id {
BuiltinOrExternalFontId::External(fid) => fid.0.clone(),
BuiltinOrExternalFontId::Builtin(_) => return None,
};
let kind = match pf {
ParsedOrBuiltinFont::B(b) => FontTextDecodeKind::Builtin(*b),
ParsedOrBuiltinFont::P(p) => {
if p.is_cid {
FontTextDecodeKind::Cid {
to_unicode: p.to_unicode.clone(),
encoding_cmap: p.encoding_cmap.clone(),
cid_to_gid: p.cid_to_gid.clone(),
}
} else {
FontTextDecodeKind::Simple {
to_unicode: p.to_unicode.clone(),
}
}
}
ParsedOrBuiltinFont::CidStub(to_unicode, encoding_cmap) => {
FontTextDecodeKind::Cid {
to_unicode: to_unicode.clone(),
encoding_cmap: encoding_cmap.clone(),
cid_to_gid: None,
}
}
};
Some((name, kind))
})
.collect();
for (i, page_ref) in page_refs.into_iter().enumerate() {
let page_obj = doc
.get_object(page_ref)
.map_err(|e| format!("Failed to get page object: {}", e))?
.as_dict()
.map_err(|e| format!("Page object is not a dictionary: {}", e))?;
let pdf_page = parse_page(i, page_obj, &doc, &xobjects, &font_decode, warnings)?;
pages.push(pdf_page);
}
for (page_idx, link) in link_annots {
match pages.get_mut(page_idx) {
Some(page) => page.ops.push(Op::LinkAnnotation { link }),
None => warnings.push(PdfWarnMsg::warning(
page_idx,
0,
"link annotation references a page that was not parsed".to_string(),
)),
}
}
let bookmarks = links_and_bookmarks::extract_bookmarks(&doc);
let layers = layers::extract_layers(&doc);
let layers: Vec<(LayerInternalId, crate::ops::Layer)> = layers
.into_iter()
.enumerate()
.map(|(i, (name, layer))| {
let key = name.unwrap_or_else(|| format!("layer_{}", i));
(LayerInternalId(key), layer)
})
.collect();
let extgstates = extract_extgstates(&doc, &objs_to_search_for_resources, warnings);
let shadings = extract_shadings(&doc, &objs_to_search_for_resources, warnings);
let fonts = fonts
.into_iter()
.filter_map(|(id, pf)| {
let parsed_font = pf.as_parsed_font()?;
let pdf_font = crate::font::PdfFont::new(parsed_font);
Some((FontId(id.get_id().to_string()), pdf_font))
})
.collect();
let pdf_doc = PdfDocument {
metadata: PdfMetadata {
info: document_info,
xmp: None,
},
resources: PdfResources {
fonts: PdfFontMap { map: fonts },
xobjects: XObjectMap { map: xobjects },
extgstates: ExtendedGraphicsStateMap { map: extgstates },
shadings: crate::ShadingMap { map: shadings },
layers: PdfLayerMap {
map: layers.into_iter().collect(),
},
},
bookmarks: PageAnnotMap {
map: bookmarks
.into_iter()
.enumerate()
.map(|(i, bookmark)| (PageAnnotId(format!("bookmark_{}", i)), bookmark))
.collect(),
},
pages,
};
Ok(pdf_doc)
}
pub struct ParsedExternalFont {
pub font: ParsedFont,
pub to_unicode: Option<Arc<ToUnicodeCMap>>,
pub is_cid: bool,
pub cid_to_gid: Option<Arc<BTreeMap<u16, u16>>>,
pub encoding_cmap: Option<Arc<crate::cmap::CidCMap>>,
}
pub enum ParsedOrBuiltinFont {
P(ParsedExternalFont),
B(BuiltinFont),
CidStub(
Option<Arc<ToUnicodeCMap>>,
Option<Arc<crate::cmap::CidCMap>>,
),
}
impl ParsedOrBuiltinFont {
fn as_parsed_font(self) -> Option<ParsedFont> {
match self {
ParsedOrBuiltinFont::P(p) => Some(p.font),
ParsedOrBuiltinFont::B(_) => None,
ParsedOrBuiltinFont::CidStub(..) => None,
}
}
#[allow(dead_code)]
fn cmap(&self) -> Option<&ToUnicodeCMap> {
match self {
ParsedOrBuiltinFont::P(p) => p.to_unicode.as_deref(),
ParsedOrBuiltinFont::B(_) => None,
ParsedOrBuiltinFont::CidStub(c, _) => c.as_deref(),
}
}
}
#[derive(Debug, Clone)]
pub enum FontTextDecodeKind {
Builtin(BuiltinFont),
Simple {
to_unicode: Option<Arc<ToUnicodeCMap>>,
},
Cid {
to_unicode: Option<Arc<ToUnicodeCMap>>,
encoding_cmap: Option<Arc<crate::cmap::CidCMap>>,
cid_to_gid: Option<Arc<BTreeMap<u16, u16>>>,
},
}
fn get_dict_or_resolve_ref<'a>(
id: &str,
doc: &'a LopdfDocument,
xobj_obj: &'a LopdfObject,
warnings: &mut Vec<PdfWarnMsg>,
page: Option<usize>,
) -> Option<&'a LopdfDictionary> {
match xobj_obj {
Object::Dictionary(dict) => Some(dict),
Object::Stream(stream) => Some(&stream.dict),
Object::Reference(r) => match doc.get_dictionary(*r) {
Ok(s) => Some(s),
Err(_) => match doc.get_object(*r) {
Ok(Object::Stream(stream)) => Some(&stream.dict),
Ok(Object::Dictionary(dict)) => Some(dict),
_ => {
warnings.push(PdfWarnMsg::error(
page.unwrap_or(0),
0,
format!("{id}: Invalid dictionary reference {r:?}"),
));
None
}
},
},
_ => {
warnings.push(PdfWarnMsg::error(
page.unwrap_or(0),
0,
"Unexpected type for XObject resource".to_string(),
));
None
}
}
}
fn get_stream_or_resolve_ref<'a>(
doc: &'a LopdfDocument,
xobj_obj: &'a LopdfObject,
warnings: &mut Vec<PdfWarnMsg>,
page: Option<usize>,
) -> Option<&'a LopdfStream> {
match xobj_obj {
Object::Stream(dict) => Some(dict),
Object::Reference(r) => match doc.get_object(*r) {
Ok(Object::Stream(s)) => Some(s),
Ok(_) => {
warnings.push(PdfWarnMsg::error(
page.unwrap_or(0),
0,
format!("Invalid stream reference {r:?}"),
));
None
}
Err(e) => {
warnings.push(PdfWarnMsg::error(
page.unwrap_or(0),
0,
format!("Invalid stream reference {r:?}: {e:?}"),
));
None
}
},
_ => {
warnings.push(PdfWarnMsg::error(
page.unwrap_or(0),
0,
"Unexpected type for XObject resource".to_string(),
));
None
}
}
}
pub(crate) enum PendingXObject {
DecodeImage {
bytes: Vec<u8>,
smask: Option<SMaskData>,
fallback: ExternalXObject,
},
Ready(XObject),
}
pub(crate) struct SMaskData {
pub width: usize,
pub height: usize,
pub gray: Vec<u8>,
}
fn apply_smask_to_rawimage(mut img: RawImage, smask: SMaskData) -> RawImage {
use crate::image_types::{RawImageData, RawImageFormat};
if smask.width != img.width || smask.height != img.height {
return img;
}
let expected = img.width * img.height;
if smask.gray.len() < expected {
return img;
}
match (&img.data_format, &img.pixels) {
(RawImageFormat::RGB8, RawImageData::U8(rgb)) => {
if rgb.len() < expected * 3 {
return img;
}
let mut rgba = Vec::with_capacity(expected * 4);
for i in 0..expected {
rgba.extend_from_slice(&rgb[i * 3..i * 3 + 3]);
rgba.push(smask.gray[i]);
}
img.pixels = RawImageData::U8(rgba);
img.data_format = RawImageFormat::RGBA8;
img
}
(RawImageFormat::R8, RawImageData::U8(gray)) => {
if gray.len() < expected {
return img;
}
let mut ga = Vec::with_capacity(expected * 2);
for i in 0..expected {
ga.push(gray[i]);
ga.push(smask.gray[i]);
}
img.pixels = RawImageData::U8(ga);
img.data_format = RawImageFormat::RG8;
img
}
_ => img,
}
}
fn process_xobjects(
xobjects: BTreeMap<XObjectId, PendingXObject>,
warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<XObjectId, XObject> {
let mut map = BTreeMap::new();
for (xobject_id, pending) in xobjects {
match pending {
PendingXObject::Ready(x) => {
map.insert(xobject_id, x);
}
PendingXObject::DecodeImage {
bytes,
smask,
fallback,
} => {
warnings.push(PdfWarnMsg::info(
0,
0,
format!("process XObject {} ({} bytes)", xobject_id.0, bytes.len()),
));
match RawImage::decode_from_bytes(&bytes, warnings) {
Ok(mut o) => {
if let Some(sm) = smask {
o = apply_smask_to_rawimage(o, sm);
}
map.insert(xobject_id, XObject::Image(o));
}
Err(e) => {
warnings.push(PdfWarnMsg::warning(
0,
0,
format!(
"could not decode image XObject {} ({} bytes): {e}; preserving \
original stream",
xobject_id.0,
bytes.len()
),
));
map.insert(xobject_id, XObject::External(fallback));
}
}
}
}
}
map
}
fn extract_shadings(
doc: &LopdfDocument,
objs_to_search_for_resources: &[(LopdfObject, Option<usize>)],
warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<crate::ShadingId, crate::Shading> {
let mut map = BTreeMap::new();
for (obj, page_idx) in objs_to_search_for_resources {
let page_num = page_idx.unwrap_or(0);
let dict = match obj {
LopdfObject::Dictionary(d) => Some(d),
LopdfObject::Reference(r) => doc.get_dictionary(*r).ok(),
_ => None,
};
let Some(dict) = dict else { continue };
let resources = if let Ok(res) = dict.get(b"Resources") {
get_dict_or_resolve_ref(
&format!("extract_shadings Resources page {page_num}"),
doc,
res,
warnings,
Some(page_num),
)
} else {
Some(dict)
};
let Some(resources) = resources else { continue };
let Ok(shading_res) = resources.get(b"Shading") else {
continue;
};
let Some(shading_dict) = get_dict_or_resolve_ref(
&format!("extract_shadings Shading page {page_num}"),
doc,
shading_res,
warnings,
Some(page_num),
) else {
continue;
};
for (key, value) in shading_dict.iter() {
let id = crate::ShadingId(String::from_utf8_lossy(key).to_string());
if map.contains_key(&id) {
continue; }
let Some(sh) = get_dict_or_resolve_ref(
&format!("extract_shadings entry page {page_num}"),
doc,
value,
warnings,
Some(page_num),
) else {
continue;
};
match parse_shading_dict(doc, sh) {
Ok(parsed) => {
map.insert(id, parsed);
}
Err(e) => warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!("shading {:?} not preserved: {e}", id.0),
)),
}
}
}
map
}
fn parse_shading_dict(
doc: &LopdfDocument,
dict: &LopdfDictionary,
) -> Result<crate::Shading, String> {
fn as_f32(o: &LopdfObject) -> Option<f32> {
match o {
LopdfObject::Integer(i) => Some(*i as f32),
LopdfObject::Real(r) => Some(*r),
_ => None,
}
}
fn num_array(doc: &LopdfDocument, o: &LopdfObject, n: usize) -> Option<Vec<f32>> {
let o = match o {
LopdfObject::Reference(r) => doc.get_object(*r).ok()?,
other => other,
};
let arr = o.as_array().ok()?;
let v: Vec<f32> = arr.iter().filter_map(as_f32).collect();
(v.len() == n).then_some(v)
}
fn rgb(v: &[f32]) -> [f32; 3] {
match v.len() {
1 => [v[0], v[0], v[0]], 3 => [v[0], v[1], v[2]],
_ => [0.0, 0.0, 0.0],
}
}
fn resolve<'a>(doc: &'a LopdfDocument, o: &'a LopdfObject) -> &'a LopdfObject {
match o {
LopdfObject::Reference(r) => doc.get_object(*r).unwrap_or(o),
other => other,
}
}
fn fn_dict(o: &LopdfObject) -> Option<&LopdfDictionary> {
match o {
LopdfObject::Dictionary(d) => Some(d),
LopdfObject::Stream(s) => Some(&s.dict),
_ => None,
}
}
let shading_type = dict
.get(b"ShadingType")
.ok()
.and_then(as_f32)
.ok_or("missing ShadingType")? as i64;
let geometry = match shading_type {
2 => {
let c = dict
.get(b"Coords")
.ok()
.and_then(|o| num_array(doc, o, 4))
.ok_or("axial shading without 4-element Coords")?;
crate::ShadingGeometry::Axial {
coords: [c[0], c[1], c[2], c[3]],
}
}
3 => {
let c = dict
.get(b"Coords")
.ok()
.and_then(|o| num_array(doc, o, 6))
.ok_or("radial shading without 6-element Coords")?;
crate::ShadingGeometry::Radial {
coords: [c[0], c[1], c[2], c[3], c[4], c[5]],
}
}
other => return Err(format!("unsupported ShadingType {other}")),
};
let extend = dict
.get(b"Extend")
.ok()
.map(|o| resolve(doc, o))
.and_then(|o| o.as_array().ok())
.map(|a| {
let b = |i: usize| matches!(a.get(i), Some(LopdfObject::Boolean(true)));
(b(0), b(1))
})
.unwrap_or((false, false));
let func_obj = resolve(
doc,
dict.get(b"Function")
.map_err(|_| "shading without Function")?,
);
let func = fn_dict(func_obj).ok_or("shading Function is not a dictionary/stream")?;
let func_type = func
.get(b"FunctionType")
.ok()
.and_then(as_f32)
.ok_or("Function without FunctionType")? as i64;
fn exponential_stops(
doc: &LopdfDocument,
f: &LopdfDictionary,
t0: f32,
t1: f32,
) -> Result<Vec<crate::GradientStop>, String> {
let get = |k: &[u8]| -> Vec<f32> {
f.get(k)
.ok()
.and_then(|o| {
let o = match o {
LopdfObject::Reference(r) => doc.get_object(*r).ok()?,
other => other,
};
o.as_array().ok()
})
.map(|a| {
a.iter()
.filter_map(|x| match x {
LopdfObject::Integer(i) => Some(*i as f32),
LopdfObject::Real(r) => Some(*r),
_ => None,
})
.collect()
})
.unwrap_or_else(|| vec![0.0])
};
let c0 = get(b"C0");
let c1 = get(b"C1");
Ok(vec![
crate::GradientStop {
offset: t0,
color: rgb(&c0),
},
crate::GradientStop {
offset: t1,
color: rgb(&c1),
},
])
}
let mut stops = match func_type {
2 => exponential_stops(doc, func, 0.0, 1.0)?,
3 => {
let bounds = func
.get(b"Bounds")
.ok()
.map(|o| resolve(doc, o))
.and_then(|o| o.as_array().ok())
.map(|a| a.iter().filter_map(as_f32).collect::<Vec<_>>())
.unwrap_or_default();
let funcs = func
.get(b"Functions")
.ok()
.map(|o| resolve(doc, o))
.and_then(|o| o.as_array().ok())
.ok_or("stitching function without Functions array")?;
let mut edges = Vec::with_capacity(funcs.len() + 1);
edges.push(0.0);
edges.extend(bounds.iter().copied());
edges.push(1.0);
let mut all = Vec::new();
for (i, sub) in funcs.iter().enumerate() {
let sub = resolve(doc, sub);
let sub = fn_dict(sub).ok_or("stitching sub-function is not a dictionary")?;
let (t0, t1) = (
edges.get(i).copied().unwrap_or(0.0),
edges.get(i + 1).copied().unwrap_or(1.0),
);
all.extend(exponential_stops(doc, sub, t0, t1)?);
}
all
}
other => return Err(format!("unsupported FunctionType {other}")),
};
stops.dedup_by(|b, a| (a.offset - b.offset).abs() < 1e-6 && a.color == b.color);
if stops.is_empty() {
return Err("shading function produced no stops".to_string());
}
Ok(crate::Shading {
geometry,
stops,
extend,
})
}
fn extract_extgstates(
doc: &LopdfDocument,
objs_to_search_for_resources: &[(LopdfObject, Option<usize>)],
warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<ExtendedGraphicsStateId, ExtendedGraphicsState> {
objs_to_search_for_resources
.iter()
.filter_map(|(obj, page_idx)| {
let page_num = page_idx.unwrap_or(0);
let dict = match obj {
LopdfObject::Dictionary(d) => Some(d),
LopdfObject::Reference(r) => doc.get_dictionary(*r).ok(),
_ => None,
}?;
let resources = if let Ok(res) = dict.get(b"Resources") {
get_dict_or_resolve_ref(
&format!("extract_extgstates Resources page {page_num}"),
doc,
res,
warnings,
Some(page_num),
)
} else {
Some(dict)
}?;
let extgstate = resources.get(b"ExtGState").ok()?;
let extgstate_dict = get_dict_or_resolve_ref(
&format!("extract_extgstates ExtGState page {page_num}"),
doc,
extgstate,
warnings,
Some(page_num),
)?;
let gs_entries = extgstate_dict
.iter()
.filter_map(|(key, value)| {
let gs_dict = get_dict_or_resolve_ref(
&format!("extract_extgstates GsDict page {page_num}"),
doc,
value,
warnings,
Some(page_num),
)?;
let gs_id = ExtendedGraphicsStateId(String::from_utf8_lossy(key).to_string());
let gs = extgstate::parse_extgstate(gs_dict);
Some((gs_id, gs))
})
.collect::<BTreeMap<_, _>>();
Some(gs_entries)
})
.fold(BTreeMap::new(), |mut acc, extgstates| {
acc.extend(extgstates);
acc
})
}
async fn process_xobjects_async(
xobjects: BTreeMap<XObjectId, PendingXObject>,
warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<XObjectId, XObject> {
let mut map = BTreeMap::new();
for (xobject_id, pending) in xobjects {
match pending {
PendingXObject::Ready(x) => {
map.insert(xobject_id, x);
}
PendingXObject::DecodeImage {
bytes,
smask,
fallback,
} => match RawImage::decode_from_bytes_async(&bytes, warnings).await {
Ok(mut o) => {
if let Some(sm) = smask {
o = apply_smask_to_rawimage(o, sm);
}
map.insert(xobject_id, XObject::Image(o));
}
Err(e) => {
warnings.push(PdfWarnMsg::warning(
0,
0,
format!(
"could not decode image XObject {}: {e}; preserving original stream",
xobject_id.0
),
));
map.insert(xobject_id, XObject::External(fallback));
}
},
}
}
map
}
const MAX_RESOLVE_DEPTH: usize = 96;
fn dictitem_from_lopdf_resolved(
doc: &LopdfDocument,
obj: &LopdfObject,
depth: usize,
ref_path: &mut Vec<ObjectId>,
) -> DictItem {
if depth > MAX_RESOLVE_DEPTH {
return DictItem::Null;
}
match obj {
Object::Reference(r) => {
if ref_path.contains(r) {
return DictItem::Null;
}
match doc.get_object(*r) {
Ok(target) => {
ref_path.push(*r);
let item = dictitem_from_lopdf_resolved(doc, target, depth + 1, ref_path);
ref_path.pop();
item
}
Err(_) => DictItem::Null,
}
}
Object::Array(objects) => DictItem::Array(
objects
.iter()
.map(|o| dictitem_from_lopdf_resolved(doc, o, depth + 1, ref_path))
.collect(),
),
Object::Dictionary(dict) => DictItem::Dict {
map: dict
.iter()
.map(|(k, v)| {
(
String::from_utf8_lossy(k).to_string(),
dictitem_from_lopdf_resolved(doc, v, depth + 1, ref_path),
)
})
.collect(),
},
Object::Stream(stream) => DictItem::Stream {
stream: external_stream_preserved(doc, stream, depth + 1, ref_path),
},
other => DictItem::from_lopdf(other),
}
}
fn external_stream_preserved(
doc: &LopdfDocument,
stream: &LopdfStream,
depth: usize,
ref_path: &mut Vec<ObjectId>,
) -> ExternalStream {
let dict = stream
.dict
.iter()
.filter(|(k, _)| k.as_slice() != b"Length")
.map(|(k, v)| {
(
String::from_utf8_lossy(k).to_string(),
dictitem_from_lopdf_resolved(doc, v, depth, ref_path),
)
})
.collect();
ExternalStream {
dict,
content: stream.content.clone(),
compress: false,
}
}
fn preserve_xobject_stream(doc: &LopdfDocument, stream: &LopdfStream) -> ExternalXObject {
ExternalXObject {
stream: external_stream_preserved(doc, stream, 0, &mut Vec::new()),
width: None,
height: None,
dpi: None,
}
}
fn colorspace_components(doc: &LopdfDocument, cs: &LopdfObject, depth: usize) -> Option<usize> {
if depth > MAX_RESOLVE_DEPTH {
return None;
}
match cs {
Object::Reference(r) => colorspace_components(doc, doc.get_object(*r).ok()?, depth + 1),
Object::Name(n) => match n.as_slice() {
b"DeviceRGB" | b"CalRGB" => Some(3),
b"DeviceGray" | b"CalGray" => Some(1),
_ => None,
},
Object::Array(arr) => {
let first = arr.first()?;
let family = match first {
Object::Name(n) => n.as_slice(),
_ => return None,
};
match family {
b"ICCBased" => {
let stream = arr.get(1)?;
let stream = match stream {
Object::Reference(r) => match doc.get_object(*r).ok()? {
Object::Stream(s) => s,
_ => return None,
},
Object::Stream(s) => s,
_ => return None,
};
match stream.dict.get(b"N").ok()? {
Object::Integer(1) => Some(1),
Object::Integer(3) => Some(3),
_ => None,
}
}
b"CalRGB" => Some(3),
b"CalGray" => Some(1),
_ => None,
}
}
_ => None,
}
}
fn stream_filters(stream: &LopdfStream) -> Vec<Vec<u8>> {
match stream.dict.get(b"Filter") {
Ok(Object::Name(n)) => vec![n.clone()],
Ok(Object::Array(arr)) => arr
.iter()
.filter_map(|o| match o {
Object::Name(n) => Some(n.clone()),
_ => None,
})
.collect(),
_ => Vec::new(),
}
}
fn extract_smask(doc: &LopdfDocument, stream: &LopdfStream) -> Option<SMaskData> {
let smask_obj = stream.dict.get(b"SMask").ok()?;
let smask_stream = match smask_obj {
Object::Reference(r) => match doc.get_object(*r).ok()? {
Object::Stream(s) => s,
_ => return None,
},
Object::Stream(s) => s,
_ => return None,
};
let width = smask_stream.dict.get(b"Width").ok()?.as_i64().ok()? as usize;
let height = smask_stream.dict.get(b"Height").ok()?.as_i64().ok()? as usize;
let bpc = smask_stream
.dict
.get(b"BitsPerComponent")
.ok()
.and_then(|o| o.as_i64().ok())
.unwrap_or(8);
if bpc != 8 {
return None;
}
let gray = smask_stream
.decompressed_content()
.unwrap_or_else(|_| smask_stream.content.clone());
if gray.len() < width * height {
return None;
}
Some(SMaskData {
width,
height,
gray,
})
}
pub(crate) fn raw_bitmap_from_stream(
doc: &LopdfDocument,
stream: &LopdfStream,
) -> Option<RawImage> {
use crate::image_types::{RawImageData, RawImageFormat};
let width = stream.dict.get(b"Width").ok()?.as_i64().ok()? as usize;
let height = stream.dict.get(b"Height").ok()?.as_i64().ok()? as usize;
if width == 0 || height == 0 || width.checked_mul(height).is_none() {
return None;
}
let bpc = stream
.dict
.get(b"BitsPerComponent")
.ok()
.and_then(|o| o.as_i64().ok())
.unwrap_or(8);
let components = colorspace_components(doc, stream.dict.get(b"ColorSpace").ok()?, 0)?;
if let Ok(im) = stream.dict.get(b"ImageMask") {
if im.as_bool().unwrap_or(false) {
return None;
}
}
if let Ok(Object::Dictionary(dp)) = stream.dict.get(b"DecodeParms") {
if dp
.get(b"Predictor")
.map(|p| p.as_i64().unwrap_or(1))
.unwrap_or(1)
> 1
{
return None;
}
}
let data = stream.decompressed_content().ok()?;
let expected = width * height * components * (bpc as usize / 8).max(1);
match (bpc, components) {
(8, 3) | (8, 1) => {
if data.len() < expected {
return None;
}
let format = if components == 3 {
RawImageFormat::RGB8
} else {
RawImageFormat::R8
};
let mut pixels = data;
pixels.truncate(expected);
let mut img = RawImage {
pixels: RawImageData::U8(pixels),
width,
height,
data_format: format,
tag: Vec::new(),
};
if let Some(smask) = extract_smask(doc, stream) {
img = apply_smask_to_rawimage(img, smask);
}
Some(img)
}
(16, 3) | (16, 1) => {
if data.len() < expected {
return None;
}
let format = if components == 3 {
RawImageFormat::RGB16
} else {
RawImageFormat::R16
};
let pixels: Vec<u16> = data[..expected]
.as_chunks::<2>()
.0
.iter()
.map(|c| u16::from_be_bytes([c[0], c[1]]))
.collect();
Some(RawImage {
pixels: RawImageData::U16(pixels),
width,
height,
data_format: format,
tag: Vec::new(),
})
}
_ => None,
}
}
fn parse_xobjects_internal(
doc: &LopdfDocument,
resources: &LopdfDictionary,
warnings: &mut Vec<PdfWarnMsg>,
page: Option<usize>,
) -> BTreeMap<XObjectId, PendingXObject> {
let mut xobj_map = BTreeMap::new();
let page_num = page.unwrap_or_default();
let xobj_obj = match resources.get(b"XObject") {
Ok(s) => s,
Err(_) => return xobj_map,
};
let xobj_dict = match get_dict_or_resolve_ref(
&format!("parse_xobjects_internal 1 page {page_num}"),
doc,
xobj_obj,
warnings,
page,
) {
Some(s) => s,
None => return xobj_map,
};
for (key, value) in xobj_dict.iter() {
let xobject_id = XObjectId(String::from_utf8_lossy(key).to_string());
let stream = match get_stream_or_resolve_ref(doc, value, warnings, page) {
Some(s) => s,
None => continue,
};
let subtype = match stream.dict.get(b"Subtype") {
Ok(Object::Name(o)) => o.clone(),
_ => {
if stream.dict.get(b"Width").is_ok() && stream.dict.get(b"Height").is_ok() {
b"Image".to_vec()
} else {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"parse-xobjects: XObject {} has no /Subtype; preserving verbatim",
xobject_id.0
),
));
xobj_map.insert(
xobject_id,
PendingXObject::Ready(XObject::External(preserve_xobject_stream(
doc, stream,
))),
);
continue;
}
}
};
match subtype.as_slice() {
b"Image" => {
let filters = stream_filters(stream);
let has_container_format = filters
.iter()
.any(|f| f == b"DCTDecode" || f == b"JPXDecode");
if has_container_format {
let bytes = if filters.len() == 1 {
stream.content.clone()
} else {
stream
.decompressed_content()
.unwrap_or_else(|_| stream.content.clone())
};
xobj_map.insert(
xobject_id,
PendingXObject::DecodeImage {
bytes,
smask: extract_smask(doc, stream),
fallback: preserve_xobject_stream(doc, stream),
},
);
} else if let Some(img) = raw_bitmap_from_stream(doc, stream) {
let entry = if cfg!(feature = "images") {
PendingXObject::Ready(XObject::Image(img))
} else {
PendingXObject::Ready(XObject::External(preserve_xobject_stream(
doc, stream,
)))
};
xobj_map.insert(xobject_id, entry);
} else {
let bytes = stream
.decompressed_content()
.unwrap_or_else(|_| stream.content.clone());
xobj_map.insert(
xobject_id,
PendingXObject::DecodeImage {
bytes,
smask: extract_smask(doc, stream),
fallback: preserve_xobject_stream(doc, stream),
},
);
}
}
_other => {
xobj_map.insert(
xobject_id,
PendingXObject::Ready(XObject::External(preserve_xobject_stream(doc, stream))),
);
}
}
}
xobj_map
}
fn parse_fonts_from_entire_pdf(
doc: &LopdfDocument,
objs_to_search_for_resources: &[(LopdfObject, Option<usize>)],
warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<BuiltinOrExternalFontId, ParsedOrBuiltinFont> {
let obj = objs_to_search_for_resources
.iter()
.filter_map(|(obj, page_idx)| {
let page_num = page_idx.unwrap_or(0);
let dict = match obj {
LopdfObject::Dictionary(d) => Some(d),
LopdfObject::Reference(r) => doc.get_dictionary(*r).ok(),
_ => None,
}?;
let resources_obj = dict.get(b"Resources").ok()?;
let resources_dict = get_dict_or_resolve_ref(
&format!("parse_fonts_from_entire_pdf 1 page {page_num}"),
doc,
resources_obj,
warnings,
Some(page_num),
)?;
Some(parsefont::parse_fonts_enhanced(
doc,
resources_dict,
warnings,
Some(page_num),
))
})
.fold(BTreeMap::new(), |mut acc, fonts| {
fonts.into_iter().for_each(|(font_id, parsed_font)| {
acc.entry(BuiltinOrExternalFontId::External(font_id))
.or_insert(parsed_font);
});
acc
});
let mut map = BuiltinFont::all_ids()
.iter()
.map(|b| {
(
BuiltinOrExternalFontId::Builtin(*b),
ParsedOrBuiltinFont::B(*b),
)
})
.collect::<BTreeMap<_, _>>();
map.extend(obj);
map
}
fn parse_xobjects_from_entire_pdf(
doc: &LopdfDocument,
objs_to_search_for_resources: &[(LopdfObject, Option<usize>)],
warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<XObjectId, PendingXObject> {
objs_to_search_for_resources
.iter()
.filter_map(|(obj, page_idx)| {
let page_num = page_idx.unwrap_or(0);
let dict = match obj {
LopdfObject::Dictionary(d) => Some(d),
LopdfObject::Reference(r) => doc.get_dictionary(*r).ok(),
_ => None,
}?;
let resources_obj = dict.get(b"Resources").ok()?;
let resources_dict = get_dict_or_resolve_ref(
&format!("parse_xobjects_from_entire_pdf 1 page {page_num}"),
doc,
resources_obj,
warnings,
Some(page_num),
)?;
Some(parse_xobjects_internal(
doc,
resources_dict,
warnings,
Some(page_num),
))
})
.fold(BTreeMap::new(), |mut acc, xobjs| {
xobjs.into_iter().for_each(|(xobj_id, xobj)| {
acc.entry(xobj_id).or_insert(xobj);
});
acc
})
.into_iter()
.collect()
}
fn collect_page_refs(
dict: &LopdfDictionary,
doc: &LopdfDocument,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<Vec<ObjectId>, String> {
let mut pages = Vec::new();
let mut visited = std::collections::BTreeSet::new();
collect_page_refs_inner(dict, doc, &mut pages, &mut visited, warnings, 0)?;
Ok(pages)
}
fn collect_page_refs_inner(
dict: &LopdfDictionary,
doc: &LopdfDocument,
pages: &mut Vec<ObjectId>,
visited: &mut std::collections::BTreeSet<ObjectId>,
warnings: &mut Vec<PdfWarnMsg>,
depth: usize,
) -> Result<(), String> {
if depth > 64 {
warnings.push(PdfWarnMsg::warning(
0,
0,
"Pages tree deeper than 64 levels, ignoring deeper nodes".to_string(),
));
return Ok(());
}
let kids = dict
.get(b"Kids")
.map_err(|e| format!("Pages dictionary missing Kids key: {}", e))?;
let kids = match kids {
Object::Reference(r) => doc
.get_object(*r)
.map_err(|e| format!("Failed to resolve Pages.Kids reference: {}", e))?,
other => other,
};
let page_refs = kids
.as_array()
.map(|s| {
s.iter()
.filter_map(|k| k.as_reference().ok())
.collect::<Vec<_>>()
})
.map_err(|_| "Pages.Kids is not an array".to_string())?;
for r in page_refs {
if !visited.insert(r) {
warnings.push(PdfWarnMsg::warning(
0,
0,
format!(
"Pages tree contains object {:?} more than once, skipping",
r
),
));
continue;
}
let kid_obj = match doc.get_object(r) {
Ok(o) => o,
Err(e) => {
warnings.push(PdfWarnMsg::warning(
0,
0,
format!("Failed to get pages-tree kid object {:?}: {}", r, e),
));
continue;
}
};
if let Ok(kid_dict) = kid_obj.as_dict() {
let type_name = match kid_dict.get(b"Type") {
Ok(Object::Name(t)) => Some(t.clone()),
_ => None,
};
match type_name.as_deref() {
Some(b"Page") => pages.push(r),
Some(b"Pages") => {
collect_page_refs_inner(kid_dict, doc, pages, visited, warnings, depth + 1)?
}
Some(other) => {
warnings.push(PdfWarnMsg::warning(
0,
0,
format!(
"Unknown pages-tree kid type {:?}, skipping",
String::from_utf8_lossy(other)
),
));
}
None => {
if kid_dict.has(b"Kids") {
collect_page_refs_inner(kid_dict, doc, pages, visited, warnings, depth + 1)?
} else {
warnings.push(PdfWarnMsg::warning(
0,
0,
format!("Pages-tree kid {:?} missing /Type, treating as /Page", r),
));
pages.push(r);
}
}
}
}
}
Ok(())
}
fn get_inheritable_page_attr<'a>(
page: &'a LopdfDictionary,
doc: &'a LopdfDocument,
key: &[u8],
) -> Option<&'a Object> {
let mut current = page;
for _ in 0..64 {
if let Ok(obj) = current.get(key) {
return Some(obj);
}
let parent = current.get(b"Parent").ok()?;
let parent_ref = parent.as_reference().ok()?;
current = doc.get_dictionary(parent_ref).ok()?;
}
None
}
fn append_content_stream(
doc: &LopdfDocument,
obj: &Object,
page_num: usize,
content_data: &mut Vec<u8>,
warnings: &mut Vec<PdfWarnMsg>,
) {
let stream = match obj {
Object::Reference(r) => match doc.get_object(*r).and_then(|o| o.as_stream()) {
Ok(s) => s,
Err(e) => {
warnings.push(PdfWarnMsg::error(
page_num,
0,
format!("Failed to resolve content stream {:?}: {}", r, e),
));
return;
}
},
Object::Stream(s) => s,
_other => {
warnings.push(PdfWarnMsg::error(
page_num,
0,
"Content entry is not a stream".to_string(),
));
return;
}
};
let data = stream
.decompressed_content()
.unwrap_or_else(|_| stream.content.clone());
content_data.extend(data);
content_data.push(b'\n');
}
fn parse_page(
num: usize,
page: &LopdfDictionary,
doc: &LopdfDocument,
xobjects: &BTreeMap<XObjectId, XObject>,
font_decode: &BTreeMap<String, FontTextDecodeKind>,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PdfPage, String> {
let media_box = match get_inheritable_page_attr(page, doc, b"MediaBox") {
Some(obj) => match parse_rect(obj) {
Ok(r) => r,
Err(e) => {
warnings.push(PdfWarnMsg::warning(
num,
0,
format!("Invalid MediaBox: {e}; defaulting to A4"),
));
default_media_box()
}
},
None => {
warnings.push(PdfWarnMsg::warning(
num,
0,
"Page missing MediaBox (also not inherited); defaulting to A4".to_string(),
));
default_media_box()
}
};
let trim_box = match get_inheritable_page_attr(page, doc, b"TrimBox").map(parse_rect) {
Some(Ok(r)) => r,
_ => media_box.clone(),
};
let crop_box = match get_inheritable_page_attr(page, doc, b"CropBox").map(parse_rect) {
Some(Ok(r)) => r,
_ => media_box.clone(),
};
let mut content_data = Vec::new();
match page.get(b"Contents") {
Ok(Object::Array(arr)) => {
for obj in arr {
append_content_stream(doc, obj, num, &mut content_data, warnings);
}
}
Ok(Object::Reference(r)) => {
match doc.get_object(*r) {
Ok(Object::Array(arr)) => {
for obj in arr {
append_content_stream(doc, obj, num, &mut content_data, warnings);
}
}
Ok(other) => {
append_content_stream(doc, other, num, &mut content_data, warnings);
}
Err(e) => {
warnings.push(PdfWarnMsg::error(
num,
0,
format!("Failed to get content stream: {}", e),
));
}
}
}
Ok(other) => {
append_content_stream(doc, other, num, &mut content_data, warnings);
}
Err(_) => {
warnings.push(PdfWarnMsg::info(
num,
0,
"Page has no /Contents (empty page)".to_string(),
));
}
}
let ops = match lopdf::content::Content::decode(&content_data) {
Ok(content) => content.operations,
Err(e) => {
warnings.push(PdfWarnMsg::error(
num,
0,
format!("Failed to decode content stream: {}", e),
));
Vec::new()
}
};
let mut page_state = PageState {
font_decode: font_decode.clone(),
..PageState::default()
};
let mut printpdf_ops = Vec::new();
for (op_id, op) in ops.iter().enumerate() {
let parsed_op = parse_op(num, op_id, op, &mut page_state, xobjects, warnings)?;
printpdf_ops.extend(parsed_op);
}
Ok(PdfPage {
media_box,
trim_box,
crop_box,
ops: printpdf_ops,
})
}
fn default_media_box() -> Rect {
Rect::from_xywh(Pt(0.0), Pt(0.0), Pt(595.28), Pt(841.89))
}
#[derive(Debug, Clone, Default)]
pub struct PageState {
pub in_text_mode: bool,
pub current_font: Option<crate::BuiltinOrExternalFontId>,
pub current_font_size: Option<crate::units::Pt>,
pub current_font_resource: Option<String>,
pub font_decode: BTreeMap<String, FontTextDecodeKind>,
pub current_font_decode: Option<FontTextDecodeKind>,
pub transform_stack: Vec<[f32; 6]>,
pub text_line: [f32; 2],
pub text_leading: f32,
pub current_marked_content: Vec<String>,
pub path_builder: PathBuilder,
pub last_emitted_font_size: Option<(FontId, Pt)>,
pub rectangle_builder: RectangleBuilder,
}
#[derive(Debug, Clone, Default)]
pub struct RectangleBuilder {
rectangle: Option<Rect>,
}
impl RectangleBuilder {
pub fn new(&mut self, x: Pt, y: Pt, width: Pt, height: Pt) {
self.rectangle = Some(Rect::from_xywh(x, y, width, height));
}
pub fn set_winding_order(&mut self, winding_order: WindingOrder) {
if let Some(ref mut rectangle) = self.rectangle {
rectangle.winding_order = Some(winding_order);
}
}
pub fn get_ops(&self) -> Vec<Op> {
if self.rectangle.is_none() {
vec![]
} else {
vec![Op::DrawRectangle {
rectangle: self.rectangle.as_ref().unwrap().clone(),
}]
}
}
pub fn clear(&mut self) {
self.rectangle = None;
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum PathOperation {
MoveTo(Point),
LineTo(Point),
CurveTo {
control1: Point,
control2: Point,
endpoint: Point,
},
ClosePath,
}
#[derive(Debug, Clone, PartialEq)]
pub struct PdfSubPath {
operations: Vec<PathOperation>,
is_closed: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct PdfPath {
subpaths: Vec<PdfSubPath>,
paint_mode: PaintMode,
winding_order: WindingOrder,
}
#[derive(Debug, Clone, Default)]
pub struct PathBuilder {
current_subpath: Vec<PathOperation>,
subpaths: Vec<Vec<PathOperation>>,
current_point: Option<Point>,
start_point: Option<Point>,
}
impl PathBuilder {
pub fn move_to(&mut self, point: Point) {
if !self.current_subpath.is_empty() {
self.subpaths
.push(std::mem::take(&mut self.current_subpath));
}
self.current_subpath.push(PathOperation::MoveTo(point));
self.current_point = Some(point);
self.start_point = Some(point);
}
pub fn line_to(&mut self, point: Point) {
if self.current_point.is_none() {
self.move_to(point);
return;
}
self.current_subpath.push(PathOperation::LineTo(point));
self.current_point = Some(point);
}
pub fn curve_to(&mut self, control1: Point, control2: Point, endpoint: Point) {
if self.current_point.is_none() {
self.move_to(control1);
self.curve_to(control1, control2, endpoint);
return;
}
self.current_subpath.push(PathOperation::CurveTo {
control1,
control2,
endpoint,
});
self.current_point = Some(endpoint);
}
pub fn v_curve_to(&mut self, control2: Point, endpoint: Point) {
if let Some(current) = self.current_point {
self.curve_to(current, control2, endpoint);
} else {
self.move_to(control2);
self.curve_to(control2, control2, endpoint);
}
}
pub fn y_curve_to(&mut self, control1: Point, endpoint: Point) {
if self.current_point.is_none() {
self.move_to(control1);
}
self.curve_to(control1, endpoint, endpoint);
}
pub fn close_path(&mut self) {
if self.current_subpath.is_empty() {
return;
}
self.current_subpath.push(PathOperation::ClosePath);
if let (Some(start), Some(current)) = (self.start_point, self.current_point) {
if current != start {
self.current_point = Some(start);
}
}
}
pub fn clear(&mut self) {
self.current_subpath.clear();
self.subpaths.clear();
self.current_point = None;
self.start_point = None;
}
#[cfg(test)]
pub fn finalize_subpath(&mut self) {
if !self.current_subpath.is_empty() {
self.subpaths
.push(std::mem::take(&mut self.current_subpath));
self.current_point = None;
self.start_point = None;
}
}
#[cfg(test)]
pub fn is_empty(&self) -> bool {
self.current_subpath.is_empty() && self.subpaths.is_empty()
}
pub fn build(&self, paint_mode: PaintMode, winding_order: WindingOrder) -> PdfPath {
let mut all_subpaths = self.subpaths.clone();
if !self.current_subpath.is_empty() {
all_subpaths.push(self.current_subpath.clone());
}
let subpaths = all_subpaths
.into_iter()
.map(|ops| {
let is_closed = ops.iter().any(|op| matches!(op, PathOperation::ClosePath));
PdfSubPath {
operations: ops,
is_closed,
}
})
.collect();
PdfPath {
subpaths,
paint_mode,
winding_order,
}
}
}
pub fn path_to_op(path: &PdfPath) -> Op {
let mut all_rings = Vec::new();
for subpath in &path.subpaths {
let points = subpath_to_line_points(subpath);
if !points.is_empty() {
all_rings.push(PolygonRing { points });
}
}
if path.subpaths.len() == 1
&& path.paint_mode == PaintMode::Stroke
&& !path.subpaths[0].is_closed
{
if let Some(ring) = all_rings.first() {
return Op::DrawLine {
line: Line {
points: ring.points.clone(),
is_closed: false,
},
};
}
}
Op::DrawPolygon {
polygon: Polygon {
rings: all_rings,
mode: path.paint_mode,
winding_order: path.winding_order,
},
}
}
#[cfg(test)]
fn parse_pdf_stream(stream: &str) -> Vec<(String, Vec<f32>)> {
let mut operations = Vec::new();
let mut current_operands = Vec::new();
let tokens = stream.split_whitespace().peekable();
for token in tokens {
if let Ok(num) = token.parse::<f32>() {
current_operands.push(num);
continue;
}
if token == "m"
|| token == "l"
|| token == "c"
|| token == "v"
|| token == "y"
|| token == "h"
|| token == "f"
|| token == "S"
|| token == "s"
|| token == "f*"
|| token == "B"
|| token == "B*"
|| token == "b"
|| token == "b*"
{
operations.push((token.to_string(), current_operands.clone()));
current_operands = Vec::new();
}
}
operations
}
#[cfg(test)]
fn apply_path_operation(builder: &mut PathBuilder, op: &(String, Vec<f32>)) {
match op.0.as_str() {
"m" => {
if op.1.len() >= 2 {
builder.move_to(Point {
x: Pt(op.1[0]),
y: Pt(op.1[1]),
});
}
}
"l" => {
if op.1.len() >= 2 {
builder.line_to(Point {
x: Pt(op.1[0]),
y: Pt(op.1[1]),
});
}
}
"c" => {
if op.1.len() >= 6 {
builder.curve_to(
Point {
x: Pt(op.1[0]),
y: Pt(op.1[1]),
},
Point {
x: Pt(op.1[2]),
y: Pt(op.1[3]),
},
Point {
x: Pt(op.1[4]),
y: Pt(op.1[5]),
},
);
}
}
"v" => {
if op.1.len() >= 4 {
builder.v_curve_to(
Point {
x: Pt(op.1[0]),
y: Pt(op.1[1]),
},
Point {
x: Pt(op.1[2]),
y: Pt(op.1[3]),
},
);
}
}
"y" => {
if op.1.len() >= 4 {
builder.y_curve_to(
Point {
x: Pt(op.1[0]),
y: Pt(op.1[1]),
},
Point {
x: Pt(op.1[2]),
y: Pt(op.1[3]),
},
);
}
}
"h" => {
builder.close_path();
}
"f" | "S" | "s" | "f*" | "B" | "B*" | "b" | "b*" => {
builder.finalize_subpath();
}
_ => {}
}
}
fn subpath_to_line_points(subpath: &PdfSubPath) -> Vec<LinePoint> {
let mut points = Vec::new();
for operation in &subpath.operations {
match operation {
PathOperation::MoveTo(point) => {
points.push(LinePoint {
p: *point,
bezier: false,
});
}
PathOperation::LineTo(point) => {
points.push(LinePoint {
p: *point,
bezier: false,
});
}
PathOperation::CurveTo {
control1,
control2,
endpoint,
} => {
points.push(LinePoint {
p: *control1,
bezier: true,
});
points.push(LinePoint {
p: *control2,
bezier: true,
});
points.push(LinePoint {
p: *endpoint,
bezier: false,
});
}
PathOperation::ClosePath => {
}
}
}
points
}
#[test]
fn test_2() {
let pdf_stream = "
20.5 344.5 m
20.5 344.5 22 333.5 10.5 346.5 c
S"
.trim();
let expected_output = Op::DrawLine {
line: Line {
points: vec![
LinePoint {
p: Point {
x: Pt(20.5),
y: Pt(344.5),
},
bezier: false,
},
LinePoint {
p: Point {
x: Pt(20.5),
y: Pt(344.5),
},
bezier: true,
},
LinePoint {
p: Point {
x: Pt(22.0),
y: Pt(333.5),
},
bezier: true,
},
LinePoint {
p: Point {
x: Pt(10.5),
y: Pt(346.5),
},
bezier: false,
},
],
is_closed: false,
},
};
let mut builder = PathBuilder::default();
let ops = parse_pdf_stream(pdf_stream);
for op in ops {
apply_path_operation(&mut builder, &op);
}
let path = builder.build(PaintMode::Stroke, WindingOrder::NonZero);
let draw_op = path_to_op(&path);
assert_eq!(draw_op, expected_output);
}
#[test]
fn test_bezier_path_parsing() {
let pdf_stream = "-122.3 84.285 m -122.3 84.285 -122.2 86.179 -123.03 86.16 c -123.85 86.141 \
-140.3 38.066 -160.83 40.309 c -160.83 40.309 -143.05 32.956 -122.3 84.285 \
c h f";
let mut builder = PathBuilder::default();
let ops = parse_pdf_stream(pdf_stream);
for op in ops {
apply_path_operation(&mut builder, &op);
}
let path = builder.build(PaintMode::Fill, WindingOrder::NonZero);
let draw_op = path_to_op(&path);
if let Op::DrawPolygon { polygon } = draw_op {
assert_eq!(polygon.rings.len(), 1);
assert_eq!(polygon.rings[0].points.len(), 10);
let points = &polygon.rings[0].points;
assert!(!points[0].bezier); assert!(points[1].bezier); assert!(points[2].bezier); assert!(!points[3].bezier); assert!(points[4].bezier); } else {
panic!("Expected DrawPolygon");
}
}
fn decode_show_text_string(
bytes: &[u8],
kind: Option<&FontTextDecodeKind>,
page: usize,
op_id: usize,
warnings: &mut Vec<PdfWarnMsg>,
) -> crate::text::TextItem {
use crate::text::{decode_simple_font_bytes, Codepoint, TextItem};
match kind {
Some(FontTextDecodeKind::Builtin(_)) => {
TextItem::Text(decode_simple_font_bytes(bytes, None))
}
Some(FontTextDecodeKind::Simple { to_unicode }) => {
TextItem::Text(decode_simple_font_bytes(bytes, to_unicode.as_deref()))
}
Some(FontTextDecodeKind::Cid {
to_unicode,
encoding_cmap,
cid_to_gid,
}) => {
let codes: Vec<(u32, u8)> = match encoding_cmap {
Some(cmap) => cmap.split_codes(bytes),
None => {
if !bytes.len().is_multiple_of(2) {
warnings.push(PdfWarnMsg::warning(
page,
op_id,
format!(
"CID font string has odd byte length {}, dropping \
trailing byte",
bytes.len()
),
));
}
bytes
.as_chunks::<2>()
.0
.iter()
.map(|c| (u16::from_be_bytes([c[0], c[1]]) as u32, 2))
.collect()
}
};
let glyphs = codes
.into_iter()
.map(|(code, len)| {
let cid_str = to_unicode
.as_deref()
.and_then(|cmap| cmap.lookup_string(code));
let cid = encoding_cmap
.as_deref()
.map_or(code, |m| m.cid_for_code(code, len));
let cid = u16::try_from(cid).unwrap_or(0);
let gid = cid_to_gid
.as_deref()
.and_then(|m| m.get(&cid).copied())
.unwrap_or(cid);
match cid_str {
Some(s) => Codepoint::with_cid(gid, 0.0, s),
None => Codepoint::new(gid, 0.0),
}
})
.collect();
TextItem::GlyphIds(glyphs)
}
None => {
let items = crate::text::decode_tj_string_as_glyph_ids(bytes);
items
.into_iter()
.next()
.unwrap_or(TextItem::GlyphIds(Vec::new()))
}
}
}
fn decode_show_text_array(
arr: &[Object],
kind: Option<&FontTextDecodeKind>,
page: usize,
op_id: usize,
warnings: &mut Vec<PdfWarnMsg>,
) -> Vec<crate::text::TextItem> {
use crate::text::TextItem;
let mut items = Vec::new();
for obj in arr {
match obj {
Object::String(bytes, _) => {
items.push(decode_show_text_string(bytes, kind, page, op_id, warnings));
}
Object::Integer(i) => items.push(TextItem::Offset(*i as f32)),
Object::Real(r) => items.push(TextItem::Offset(*r)),
_ => {}
}
}
items
}
fn decode_show_text_to_string(
bytes: &[u8],
kind: Option<&FontTextDecodeKind>,
page: usize,
op_id: usize,
warnings: &mut Vec<PdfWarnMsg>,
) -> String {
use crate::text::TextItem;
match decode_show_text_string(bytes, kind, page, op_id, warnings) {
TextItem::Text(s) => s,
TextItem::GlyphIds(glyphs) => glyphs
.iter()
.map(|g| g.cid.clone().unwrap_or_else(|| '\u{FFFD}'.to_string()))
.collect(),
TextItem::Offset(_) => String::new(),
}
}
pub fn parse_op(
page: usize,
op_id: usize,
op: &lopdf::content::Operation,
state: &mut PageState,
_xobjects: &BTreeMap<XObjectId, XObject>,
warnings: &mut Vec<PdfWarnMsg>,
) -> Result<Vec<Op>, String> {
use crate::units::Pt;
let mut out_ops = Vec::new();
match op.operator.as_str() {
"q" => {
let top = state
.transform_stack
.last()
.copied()
.unwrap_or([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
state.transform_stack.push(top);
out_ops.push(Op::SaveGraphicsState);
}
"Q" => {
if state.transform_stack.pop().is_none() {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'Q' with empty transform stack".to_string(),
));
}
out_ops.push(Op::RestoreGraphicsState);
}
"MP" => {
if op.operands.len() != 1 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"MP expects 1 operand".to_string(),
));
return Ok(Vec::new());
}
let id = match as_name(&op.operands[0]) {
Some(name) => name,
None => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"MP operand is not a name".to_string(),
));
return Ok(Vec::new());
}
};
out_ops.push(Op::Marker { id });
}
"CS" => {
if op.operands.len() != 1 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"CS expects 1 operand".to_string(),
));
return Ok(Vec::new());
}
let id = match as_name(&op.operands[0]) {
Some(name) => name,
None => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"CS operand is not a name".to_string(),
));
return Ok(Vec::new());
}
};
out_ops.push(Op::SetColorSpaceStroke { id });
}
"cs" => {
if op.operands.len() != 1 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"cs expects 1 operand".to_string(),
));
return Ok(Vec::new());
}
let id = match as_name(&op.operands[0]) {
Some(name) => name,
None => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"cs operand is not a name".to_string(),
));
return Ok(Vec::new());
}
};
out_ops.push(Op::SetColorSpaceFill { id });
}
"ri" => {
if op.operands.len() != 1 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"ri expects 1 operand".to_string(),
));
return Ok(Vec::new());
}
let intent = match as_name(&op.operands[0]) {
Some(i) => i,
None => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"ri operand is not a name".to_string(),
));
return Ok(Vec::new());
}
};
let intent = match intent.as_str() {
"AbsoluteColorimetric" => RenderingIntent::AbsoluteColorimetric,
"RelativeColorimetric" => RenderingIntent::RelativeColorimetric,
"Saturation" => RenderingIntent::Saturation,
"Perceptual" => RenderingIntent::Perceptual,
other => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!(
"Unknown rendering intent '{}', defaulting to RelativeColorimetric",
other
),
));
RenderingIntent::RelativeColorimetric
}
};
out_ops.push(Op::SetRenderingIntent { intent });
}
"rg" => {
if op.operands.len() == 3 {
let r = to_f32(&op.operands[0]);
let g = to_f32(&op.operands[1]);
let b = to_f32(&op.operands[2]);
out_ops.push(Op::SetFillColor {
col: Color::Rgb(crate::Rgb {
r,
g,
b,
icc_profile: None,
}),
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'rg' expects 3 operands".to_string(),
));
}
}
"RG" => {
if op.operands.len() == 3 {
let r = to_f32(&op.operands[0]);
let g = to_f32(&op.operands[1]);
let b = to_f32(&op.operands[2]);
out_ops.push(Op::SetOutlineColor {
col: Color::Rgb(crate::Rgb {
r,
g,
b,
icc_profile: None,
}),
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'RG' expects 3 operands".to_string(),
));
}
}
"k" => {
if op.operands.len() == 4 {
let c = to_f32(&op.operands[0]);
let m = to_f32(&op.operands[1]);
let y = to_f32(&op.operands[2]);
let k = to_f32(&op.operands[3]);
out_ops.push(Op::SetFillColor {
col: Color::Cmyk(crate::Cmyk {
c,
m,
y,
k,
icc_profile: None,
}),
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'k' expects 4 operands".to_string(),
));
}
}
"K" => {
if op.operands.len() == 4 {
let c = to_f32(&op.operands[0]);
let m = to_f32(&op.operands[1]);
let y = to_f32(&op.operands[2]);
let k = to_f32(&op.operands[3]);
out_ops.push(Op::SetOutlineColor {
col: Color::Cmyk(crate::Cmyk {
c,
m,
y,
k,
icc_profile: None,
}),
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'K' expects 4 operands".to_string(),
));
}
}
"g" => {
if op.operands.len() == 1 {
let gray = to_f32(&op.operands[0]);
out_ops.push(Op::SetFillColor {
col: Color::Greyscale(crate::Greyscale {
percent: gray,
icc_profile: None,
}),
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'g' expects 1 operand".to_string(),
));
}
}
"G" => {
if op.operands.len() == 1 {
let gray = to_f32(&op.operands[0]);
out_ops.push(Op::SetOutlineColor {
col: Color::Greyscale(crate::Greyscale {
percent: gray,
icc_profile: None,
}),
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'G' expects 1 operand".to_string(),
));
}
}
"TJ" => {
if !state.in_text_mode {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'TJ' outside of text mode!".to_string(),
));
}
if op.operands.is_empty() {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'TJ' with no operands".to_string(),
));
} else if let Some(arr) = op.operands.first().and_then(|o| o.as_array().ok()) {
if state.current_font_resource.is_none() {
warnings.push(PdfWarnMsg::warning(
page,
op_id,
"Warning: 'TJ' without prior 'Tf' (font setup). Renderer will use default font.".to_string(),
));
}
let text_items = decode_show_text_array(
arr,
state.current_font_decode.as_ref(),
page,
op_id,
warnings,
);
out_ops.push(Op::ShowText { items: text_items });
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'TJ' operand is not an array".to_string(),
));
}
}
"Tj" => {
if !state.in_text_mode {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'Tj' outside of text mode!".to_string(),
));
}
if op.operands.is_empty() {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'Tj' with no operands".to_string(),
));
} else if let lopdf::Object::String(bytes, _format) = &op.operands[0] {
if state.current_font_resource.is_none() {
warnings.push(PdfWarnMsg::warning(
page,
op_id,
"Warning: 'Tj' without prior 'Tf' (font setup). Renderer will use default font.".to_string(),
));
}
let text_items = vec![decode_show_text_string(
bytes,
state.current_font_decode.as_ref(),
page,
op_id,
warnings,
)];
out_ops.push(Op::ShowText { items: text_items });
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'Tj' operand is not string".to_string(),
));
}
}
"T*" => {
state.text_line[1] -= state.text_leading;
out_ops.push(Op::AddLineBreak);
}
"TL" => {
if op.operands.len() == 1 {
let val = to_f32(&op.operands[0]);
state.text_leading = val;
out_ops.push(Op::SetLineHeight { lh: Pt(val) });
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("Warning: 'TL' expects 1 operand, got {}", op.operands.len()),
));
}
}
"Ts" => {
if op.operands.len() == 1 {
let rise = to_f32(&op.operands[0]);
out_ops.push(Op::SetLineOffset { multiplier: rise });
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("Warning: 'Ts' expects 1 operand, got {}", op.operands.len()),
));
}
}
"Tw" => {
if op.operands.len() == 1 {
let spacing = to_f32(&op.operands[0]);
out_ops.push(Op::SetWordSpacing { pt: Pt(spacing) });
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("Warning: 'Tw' expects 1 operand, got {}", op.operands.len()),
));
}
}
"Tz" => {
if op.operands.len() == 1 {
let scale_percent = to_f32(&op.operands[0]);
out_ops.push(Op::SetHorizontalScaling {
percent: scale_percent,
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("Warning: 'Tz' expects 1 operand, got {}", op.operands.len()),
));
}
}
"TD" => {
if op.operands.len() == 2 {
let tx = to_f32(&op.operands[0]);
let ty = to_f32(&op.operands[1]);
state.text_line[0] += tx;
state.text_line[1] += ty;
state.text_leading = -ty;
out_ops.push(Op::MoveTextCursorAndSetLeading { tx, ty });
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("'TD' expects 2 operands, got {}", op.operands.len()),
));
}
}
"Tm" => {
if op.operands.len() == 6 {
let a = to_f32(&op.operands[0]);
let b = to_f32(&op.operands[1]);
let c = to_f32(&op.operands[2]);
let d = to_f32(&op.operands[3]);
let e = to_f32(&op.operands[4]);
let f = to_f32(&op.operands[5]);
state.text_line = [e, f];
out_ops.push(Op::SetTextMatrix {
matrix: TextMatrix::Raw([a, b, c, d, e, f]),
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'Tm' expects 6 operands".to_string(),
));
}
}
"Tc" => {
if op.operands.len() != 1 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Tc expects 1 operand".to_string(),
));
return Ok(Vec::new());
}
let spacing = to_f32(&op.operands[0]);
out_ops.push(Op::SetCharacterSpacing {
multiplier: spacing,
});
}
"Tr" => {
if op.operands.len() != 1 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Tr expects 1 operand".to_string(),
));
return Ok(Vec::new());
}
let mode = match &op.operands[0] {
Object::Integer(i) => *i,
Object::Real(r) => *r as i64,
_ => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Tr operand is not numeric".to_string(),
));
return Ok(Vec::new());
}
};
out_ops.push(Op::SetTextRenderingMode {
mode: TextRenderingMode::from_i64(mode),
});
}
"gs" => {
if op.operands.len() != 1 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"gs expects 1 operand".to_string(),
));
return Ok(Vec::new());
}
let gs_name = match as_name(&op.operands[0]) {
Some(name) => name,
None => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"gs operand is not a name".to_string(),
));
return Ok(Vec::new());
}
};
out_ops.push(Op::LoadGraphicsState {
gs: ExtendedGraphicsStateId(gs_name),
});
}
"BT" => {
state.in_text_mode = true;
state.text_line = [0.0, 0.0];
out_ops.push(Op::StartTextSection);
}
"ET" => {
state.in_text_mode = false;
out_ops.push(Op::EndTextSection);
}
"Tf" => {
if op.operands.len() == 2 {
if let Some(font_name) = as_name(&op.operands[0]) {
state.current_font = Some(BuiltinOrExternalFontId::from_str(&font_name));
state.current_font_resource = Some(font_name.clone());
state.current_font_decode = match state.font_decode.get(&font_name) {
Some(kind) => Some(kind.clone()),
None => BuiltinFont::from_id(&font_name).map(FontTextDecodeKind::Builtin),
};
}
let size_val = to_f32(&op.operands[1]);
state.current_font_size = Some(crate::units::Pt(size_val));
if let (Some(font_resource), Some(sz)) =
(&state.current_font_resource, &state.current_font_size)
{
let font_handle = match &state.current_font_decode {
Some(FontTextDecodeKind::Builtin(b)) => {
crate::ops::PdfFontHandle::Builtin(*b)
}
Some(_) => crate::ops::PdfFontHandle::External(crate::FontId(
font_resource.clone(),
)),
None => {
if let Some(builtin) = BuiltinFont::from_id(font_resource) {
crate::ops::PdfFontHandle::Builtin(builtin)
} else {
crate::ops::PdfFontHandle::External(crate::FontId(
font_resource.clone(),
))
}
}
};
out_ops.push(Op::SetFont {
font: font_handle,
size: *sz,
});
}
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!(
"Warning: 'Tf' expects 2 operands, got {}",
op.operands.len()
),
));
out_ops.push(Op::Unknown {
key: "Tf".into(),
value: op.operands.iter().map(DictItem::from_lopdf).collect(),
});
}
}
"Td" => {
if op.operands.len() == 2 {
let tx = to_f32(&op.operands[0]);
let ty = to_f32(&op.operands[1]);
state.text_line[0] += tx;
state.text_line[1] += ty;
out_ops.push(Op::SetTextCursor {
pos: crate::graphics::Point {
x: crate::units::Pt(state.text_line[0]),
y: crate::units::Pt(state.text_line[1]),
},
});
}
}
"BDC" => {
if op.operands.len() != 2 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"BDC expects 2 operands".to_string(),
));
return Ok(Vec::new());
}
let Some(marked_content_nm) = as_name(&op.operands[0]) else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"BDC operand is not a name".to_string(),
));
return Ok(Vec::new());
};
if marked_content_nm.as_str() == "OC" {
let Some(layer_nm) = as_name(&op.operands[1]) else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"BDC OC layer operand is not a name".to_string(),
));
return Ok(Vec::new());
};
out_ops.push(Op::BeginLayer {
layer_id: crate::LayerInternalId(layer_nm),
});
} else {
match op.operands[1].as_dict() {
Ok(dict) => {
out_ops.push(Op::BeginMarkedContentWithProperties {
tag: marked_content_nm.clone(),
properties: DictItem::from_lopdf(&Object::Dictionary(dict.to_owned())),
});
}
Err(_err) => {
warnings.push(PdfWarnMsg::warning(
page,
op_id,
"BDC properties is not a dictionary".to_string(),
));
out_ops.push(Op::Unknown {
key: "BDC".into(),
value: op.operands.iter().map(DictItem::from_lopdf).collect(),
});
}
}
}
state.current_marked_content.push(marked_content_nm.clone());
}
"BMC" => {
if op.operands.len() != 1 {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"BMC expects 1 operand".to_string(),
));
return Ok(Vec::new());
}
let marked_content_nm = match as_name(&op.operands[0]) {
Some(t) => t,
None => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"BMC operand is not a name".to_string(),
));
return Ok(Vec::new());
}
};
state.current_marked_content.push(marked_content_nm.clone());
out_ops.push(Op::BeginMarkedContent {
tag: marked_content_nm.clone(),
});
}
"EMC" => {
if let Some(_marked_content_str) = state.current_marked_content.pop() {
out_ops.push(Op::EndMarkedContent {});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'EMC' with no current_marked_content".to_string(),
));
}
}
"cm" => {
if op.operands.len() == 6 {
let floats: Vec<f32> = op.operands.iter().map(to_f32).collect();
if let Some(top) = state.transform_stack.last_mut() {
let combined = crate::matrix::CurTransMat::combine_matrix(
*top,
floats.as_slice().try_into().unwrap(),
);
*top = combined;
}
out_ops.push(Op::SetTransformationMatrix {
matrix: crate::matrix::CurTransMat::Raw(floats.try_into().unwrap()),
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: 'cm' expects 6 floats".to_string(),
));
}
}
"m" => {
let x = to_f32(op.operands.first().unwrap_or(&lopdf::Object::Null));
let y = to_f32(op.operands.get(1).unwrap_or(&lopdf::Object::Null));
state.path_builder.move_to(Point { x: Pt(x), y: Pt(y) });
}
"l" => {
let x = to_f32(op.operands.first().unwrap_or(&lopdf::Object::Null));
let y = to_f32(op.operands.get(1).unwrap_or(&lopdf::Object::Null));
state.path_builder.line_to(Point { x: Pt(x), y: Pt(y) });
}
"c" => {
if op.operands.len() == 6 {
let x1 = to_f32(&op.operands[0]);
let y1 = to_f32(&op.operands[1]);
let x2 = to_f32(&op.operands[2]);
let y2 = to_f32(&op.operands[3]);
let x3 = to_f32(&op.operands[4]);
let y3 = to_f32(&op.operands[5]);
state.path_builder.curve_to(
Point {
x: Pt(x1),
y: Pt(y1),
},
Point {
x: Pt(x2),
y: Pt(y2),
},
Point {
x: Pt(x3),
y: Pt(y3),
},
);
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("'c' expects 6 operands, got {}", op.operands.len()),
));
}
}
"v" => {
if op.operands.len() == 4 {
let x2 = to_f32(&op.operands[0]);
let y2 = to_f32(&op.operands[1]);
let x3 = to_f32(&op.operands[2]);
let y3 = to_f32(&op.operands[3]);
state.path_builder.v_curve_to(
Point {
x: Pt(x2),
y: Pt(y2),
},
Point {
x: Pt(x3),
y: Pt(y3),
},
);
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("'v' expects 4 operands, got {}", op.operands.len()),
));
}
}
"y" => {
if op.operands.len() == 4 {
let x1 = to_f32(&op.operands[0]);
let y1 = to_f32(&op.operands[1]);
let x3 = to_f32(&op.operands[2]);
let y3 = to_f32(&op.operands[3]);
state.path_builder.y_curve_to(
Point {
x: Pt(x1),
y: Pt(y1),
},
Point {
x: Pt(x3),
y: Pt(y3),
},
);
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("'y' expects 4 operands, got {}", op.operands.len()),
));
}
}
"h" => {
state.path_builder.close_path();
}
"re" => {
if op.operands.len() == 4 {
let x = Pt(to_f32(&op.operands[0]));
let y = Pt(to_f32(&op.operands[1]));
let width = Pt(to_f32(&op.operands[2]));
let height = Pt(to_f32(&op.operands[3]));
let corner = |dx: f32, dy: f32| Point {
x: Pt(x.0 + dx),
y: Pt(y.0 + dy),
};
state.path_builder.move_to(corner(0.0, 0.0));
state.path_builder.line_to(corner(width.0, 0.0));
state.path_builder.line_to(corner(width.0, height.0));
state.path_builder.line_to(corner(0.0, height.0));
state.path_builder.close_path();
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"re expects 4 operands".to_string(),
));
}
}
"S" => {
let path = state
.path_builder
.build(PaintMode::Stroke, WindingOrder::NonZero);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state.path_builder.clear();
}
"f" => {
let path = state
.path_builder
.build(PaintMode::Fill, WindingOrder::NonZero);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state.path_builder.clear();
}
"f*" => {
let path = state
.path_builder
.build(PaintMode::Fill, WindingOrder::EvenOdd);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state.path_builder.clear();
}
"B" => {
let path = state
.path_builder
.build(PaintMode::FillStroke, WindingOrder::NonZero);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state.path_builder.clear();
}
"B*" => {
let path = state
.path_builder
.build(PaintMode::FillStroke, WindingOrder::EvenOdd);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state.path_builder.clear();
}
"b" => {
state.path_builder.close_path();
let path = state
.path_builder
.build(PaintMode::FillStroke, WindingOrder::NonZero);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state.path_builder.clear();
}
"b*" => {
state.path_builder.close_path();
let path = state
.path_builder
.build(PaintMode::FillStroke, WindingOrder::EvenOdd);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state.path_builder.clear();
}
"s" => {
state.path_builder.close_path();
let path = state
.path_builder
.build(PaintMode::Stroke, WindingOrder::NonZero);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state.path_builder.clear();
}
"n" => {
state.path_builder.clear();
}
"W" => {
let path = state
.path_builder
.build(PaintMode::Clip, WindingOrder::NonZero);
if !path.subpaths.is_empty() {
out_ops.push(path_to_op(&path));
}
state
.rectangle_builder
.set_winding_order(WindingOrder::NonZero);
}
"W*" => {
let path = state
.path_builder
.build(PaintMode::Clip, WindingOrder::EvenOdd);
if !path.subpaths.is_empty()
|| out_ops.last().is_some_and(|last_op| match last_op {
Op::DrawRectangle { .. } => true,
_ => false,
})
{
out_ops.push(path_to_op(&path));
}
state
.rectangle_builder
.set_winding_order(WindingOrder::EvenOdd);
}
"w" => {
if let Some(val) = op.operands.first() {
let width = to_f32(val);
out_ops.push(Op::SetOutlineThickness {
pt: crate::units::Pt(width),
});
}
}
"M" => {
if let Some(val) = op.operands.first() {
let limit = to_f32(val);
out_ops.push(Op::SetMiterLimit { limit: Pt(limit) });
}
}
"j" => {
if let Some(val) = op.operands.first() {
let style_num = to_f32(val).round() as i64;
let style = match style_num {
0 => crate::graphics::LineJoinStyle::Miter,
1 => crate::graphics::LineJoinStyle::Round,
2 => crate::graphics::LineJoinStyle::Bevel,
_ => crate::graphics::LineJoinStyle::Miter, };
out_ops.push(Op::SetLineJoinStyle { join: style });
}
}
"J" => {
if let Some(val) = op.operands.first() {
let style_num = to_f32(val).round() as i64;
let style = match style_num {
0 => crate::graphics::LineCapStyle::Butt,
1 => crate::graphics::LineCapStyle::Round,
2 => crate::graphics::LineCapStyle::ProjectingSquare,
_ => crate::graphics::LineCapStyle::Butt, };
out_ops.push(Op::SetLineCapStyle { cap: style });
}
}
"d" => {
if op.operands.len() == 2 {
if let Some(arr_obj) = op.operands.first() {
if let Ok(arr) = arr_obj.as_array() {
let pattern = arr.iter().map(to_f32).collect();
let offset = to_f32(&op.operands[1]);
let dash = LineDashPattern { offset, pattern };
out_ops.push(Op::SetLineDashPattern { dash });
}
}
}
}
"sc" | "scn" => {
let floats = op.operands.iter().map(to_f32).collect::<Vec<_>>();
match floats.len() {
1 => {
out_ops.push(Op::SetFillColor {
col: Color::Greyscale(crate::Greyscale {
percent: floats[0],
icc_profile: None,
}),
});
}
3 => {
out_ops.push(Op::SetFillColor {
col: Color::Rgb(crate::Rgb {
r: floats[0],
g: floats[1],
b: floats[2],
icc_profile: None,
}),
});
}
4 => {
out_ops.push(Op::SetFillColor {
col: Color::Cmyk(crate::Cmyk {
c: floats[0],
m: floats[1],
y: floats[2],
k: floats[3],
icc_profile: None,
}),
});
}
_ => {
out_ops.push(Op::Unknown {
key: op.operator.clone(),
value: op.operands.iter().map(DictItem::from_lopdf).collect(),
});
}
}
}
"SC" | "SCN" => {
let floats = op.operands.iter().map(to_f32).collect::<Vec<_>>();
match floats.len() {
1 => {
out_ops.push(Op::SetOutlineColor {
col: Color::Greyscale(crate::Greyscale {
percent: floats[0],
icc_profile: None,
}),
});
}
3 => {
out_ops.push(Op::SetOutlineColor {
col: Color::Rgb(crate::Rgb {
r: floats[0],
g: floats[1],
b: floats[2],
icc_profile: None,
}),
});
}
4 => {
out_ops.push(Op::SetOutlineColor {
col: Color::Cmyk(crate::Cmyk {
c: floats[0],
m: floats[1],
y: floats[2],
k: floats[3],
icc_profile: None,
}),
});
}
_ => {
out_ops.push(Op::Unknown {
key: op.operator.clone(),
value: op.operands.iter().map(DictItem::from_lopdf).collect(),
});
}
}
}
"'" => {
if let Some(lopdf::Object::String(bytes, _)) = op.operands.first() {
let text = decode_show_text_to_string(
bytes,
state.current_font_decode.as_ref(),
page,
op_id,
warnings,
);
out_ops.push(Op::MoveToNextLineShowText { text });
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: `'` operand is not a string".to_string(),
));
}
}
"\"" => {
if op.operands.len() == 3 {
let word_spacing = to_f32(&op.operands[0]);
let char_spacing = to_f32(&op.operands[1]);
if let lopdf::Object::String(bytes, _) = &op.operands[2] {
let text = decode_show_text_to_string(
bytes,
state.current_font_decode.as_ref(),
page,
op_id,
warnings,
);
out_ops.push(Op::SetSpacingMoveAndShowText {
word_spacing,
char_spacing,
text,
});
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
"Warning: `\"` third operand is not a string".to_string(),
));
}
} else {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!(
"Warning: `\"` expects 3 operands, got {}",
op.operands.len()
),
));
}
}
"sh" => {
if let Some(name_str) = as_name(op.operands.first().unwrap_or(&lopdf::Object::Null)) {
out_ops.push(Op::PaintShading {
id: crate::ShadingId(name_str),
});
}
}
"Do" => {
if let Some(name_str) = as_name(op.operands.first().unwrap_or(&lopdf::Object::Null)) {
let xobj_id = crate::XObjectId(name_str);
out_ops.push(Op::UseXobject {
id: xobj_id,
transform: crate::xobject::XObjectTransform {
no_auto_scale: true,
..Default::default()
},
});
}
}
other => {
warnings.push(PdfWarnMsg::error(
page,
op_id,
format!("Info: unhandled operator '{}'", other),
));
out_ops.push(Op::Unknown {
key: op.operator.clone(),
value: op.operands.iter().map(DictItem::from_lopdf).collect(),
});
}
}
Ok(out_ops)
}
fn default_date() -> OffsetDateTime {
OffsetDateTime::from_unix_timestamp(0).unwrap()
}
pub fn parse_document_info(dict: &LopdfDictionary) -> PdfDocumentInfo {
let trapped = dict
.get(b"Trapped")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(s == b"True"),
Object::Name(n) => Some(n == b"True"),
_ => None,
})
.unwrap_or_default();
let creation_date = dict
.get(b"CreationDate")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => parse_pdf_date(String::from_utf8_lossy(s).as_ref()).ok(),
_ => None,
})
.unwrap_or_else(default_date);
let modification_date = dict
.get(b"ModDate")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => parse_pdf_date(String::from_utf8_lossy(s).as_ref()).ok(),
_ => None,
})
.unwrap_or_else(default_date);
let metadata_date = modification_date;
let conformance = dict
.get(b"GTS_PDFXVersion")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(PdfConformance::from_identifier_string(
&String::from_utf8_lossy(s),
)),
_ => None,
})
.unwrap_or_default();
let document_title = dict
.get(b"Title")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(decode_text_from_utf16be(s)),
_ => None,
})
.unwrap_or_default();
let author = dict
.get(b"Author")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(decode_text_from_utf16be(s)),
_ => None,
})
.unwrap_or_default();
let creator = dict
.get(b"Creator")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(decode_text_from_utf16be(s)),
_ => None,
})
.unwrap_or_default();
let producer = dict
.get(b"Producer")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(decode_text_from_utf16be(s)),
_ => None,
})
.unwrap_or_default();
let subject = dict
.get(b"Subject")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(decode_text_from_utf16be(s)),
_ => None,
})
.unwrap_or_default();
let identifier = dict
.get(b"Identifier")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(decode_text_from_utf16be(s)),
_ => None,
})
.unwrap_or_default();
let keywords = dict
.get(b"Keywords")
.ok()
.and_then(|obj| match &obj {
Object::String(s, _) => Some(decode_text_from_utf16be(s)),
_ => None,
})
.map(|joined| {
joined
.split(',')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect()
})
.unwrap_or_default();
PdfDocumentInfo {
trapped,
version: 1, creation_date,
modification_date,
metadata_date,
conformance,
document_title,
author,
creator,
producer,
keywords,
subject,
identifier,
}
}
fn parse_rect(obj: &Object) -> Result<crate::graphics::Rect, String> {
if let Object::Array(arr) = obj {
if arr.len() != 4 {
return Err("Rectangle array does not have 4 elements".to_string());
}
let nums: Result<Vec<f32>, String> = arr
.iter()
.map(|o| match o {
Object::Integer(i) => Ok(*i as f32),
Object::Real(r) => Ok(*r),
_ => Err("Rectangle element is not a number".to_string()),
})
.collect();
let nums = nums?;
let x = nums[0];
let y = nums[1];
let urx = nums[2];
let ury = nums[3];
let width = urx - x;
let height = ury - y;
Ok(crate::graphics::Rect::from_xywh(
crate::units::Pt(x),
crate::units::Pt(y),
crate::units::Pt(width),
crate::units::Pt(height),
))
} else {
Err("Rectangle is not an array".to_string())
}
}
fn to_f32(obj: &Object) -> f32 {
match obj {
Object::Integer(i) => *i as f32,
Object::Real(r) => *r,
_ => 0.0,
}
}
fn as_name(obj: &Object) -> Option<String> {
if let Object::Name(ref bytes) = obj {
Some(String::from_utf8_lossy(bytes).to_string())
} else {
None
}
}
mod links_and_bookmarks {
use std::collections::BTreeMap;
use lopdf::{Dictionary, Document, Object, ObjectId};
use crate::{
annotation::{
Actions, BorderArray, ColorArray, DashPhase, Destination, HighlightingMode,
LinkAnnotation, PageAnnotation,
},
graphics::Rect,
Pt,
};
pub fn extract_bookmarks(doc: &Document) -> Vec<PageAnnotation> {
try_extract_bookmarks(doc).unwrap_or_default()
}
fn try_extract_bookmarks(doc: &Document) -> Option<Vec<PageAnnotation>> {
let catalog_id = match doc.trailer.get(b"Root").ok()? {
Object::Reference(id) => *id,
_ => return None,
};
let catalog = doc.get_object(catalog_id).ok()?.as_dict().ok()?;
let outlines_ref = match catalog.get(b"Outlines").ok()? {
Object::Reference(id) => *id,
_ => return None,
};
let outlines = doc.get_object(outlines_ref).ok()?.as_dict().ok()?;
let mut current_ref = match outlines.get(b"First").ok()? {
Object::Reference(id) => Some(*id),
_ => None,
};
let page_map: BTreeMap<ObjectId, usize> = doc
.get_pages()
.iter()
.map(|(num, id)| (*id, *num as usize))
.collect();
let mut bookmarks = Vec::new();
while let Some(bm_ref) = current_ref {
let bm_dict = doc.get_object(bm_ref).ok()?.as_dict().ok()?;
let title = match bm_dict.get(b"Title").ok()? {
Object::String(s, _) => s.clone(),
_ => {
current_ref = next_bookmark(bm_dict);
continue;
}
};
let dest_array = match bm_dict.get(b"Dest").ok()? {
Object::Array(arr) => arr,
_ => {
current_ref = next_bookmark(bm_dict);
continue;
}
};
let page_num = if let Some(Object::Reference(page_id)) = dest_array.first() {
*page_map.get(page_id).unwrap_or(&0)
} else {
0
};
bookmarks.push(PageAnnotation {
name: decode_possible_utf16be(&title),
page: page_num,
});
current_ref = next_bookmark(bm_dict);
}
Some(bookmarks)
}
fn decode_possible_utf16be(bytes: &[u8]) -> String {
if bytes.len() < 2 || bytes[0] != 0xFE || bytes[1] != 0xFF {
return String::from_utf8_lossy(bytes).to_string();
}
let mut chars = Vec::with_capacity((bytes.len() - 2) / 2);
let mut i = 2;
while i + 1 < bytes.len() {
let high = bytes[i] as u16;
let low = bytes[i + 1] as u16;
let code_point = (high << 8) | low;
chars.push(code_point);
i += 2;
}
if i < bytes.len() {
let high = bytes[i] as u16;
chars.push(high << 8);
}
String::from_utf16_lossy(&chars)
}
fn next_bookmark(dict: &Dictionary) -> Option<ObjectId> {
dict.get(b"Next").ok().and_then(|obj| match obj {
Object::Reference(id) => Some(*id),
_ => None,
})
}
pub fn extract_link_annotations(doc: &Document) -> Vec<(usize, LinkAnnotation)> {
let page_map: BTreeMap<ObjectId, usize> = doc
.get_pages()
.iter()
.map(|(num, id)| (*id, *num as usize))
.collect();
doc.get_pages()
.iter()
.flat_map(|(page_num, &page_id)| {
let page_idx = (*page_num as usize).saturating_sub(1);
let page_dict = doc.get_object(page_id).ok()?.as_dict().ok()?;
let annots = page_dict.get(b"Annots").ok()?.as_array().ok()?;
Some(
annots
.iter()
.filter_map(|annot_obj| {
let annot_dict = match annot_obj {
Object::Reference(id) => {
doc.get_object(*id).ok()?.as_dict().ok()?
}
Object::Dictionary(d) => d,
_ => return None,
};
if annot_dict.get(b"Subtype").ok()?.as_name().ok()? != b"Link" {
return None;
}
let rect_arr = annot_dict.get(b"Rect").ok()?.as_array().ok()?;
if rect_arr.len() != 4 {
return None;
}
let coords: Vec<f32> = rect_arr
.iter()
.filter_map(|obj| match obj {
Object::Real(r) => Some(*r),
Object::Integer(i) => Some(*i as f32),
_ => None,
})
.collect();
if coords.len() != 4 {
return None;
}
let rect = Rect::from_xywh(
Pt(coords[0]),
Pt(coords[1]),
Pt(coords[2] - coords[0]),
Pt(coords[3] - coords[1]),
);
let action_dict = annot_dict.get(b"A").ok()?.as_dict().ok()?;
let actions = extract_action(action_dict, &page_map)?;
let border = annot_dict
.get(b"Border")
.ok()
.and_then(|obj| obj.as_array().ok())
.map(|s| extract_border_array(s.as_slice()))
.unwrap_or_else(BorderArray::default);
let color = annot_dict
.get(b"C")
.ok()
.and_then(|obj| obj.as_array().ok())
.map(|s| extract_color_array(s.as_slice()))
.unwrap_or_else(ColorArray::default);
let highlighting = annot_dict
.get(b"H")
.and_then(|obj| obj.as_name())
.map(|n| match n {
n if n == b"N" => HighlightingMode::None,
n if n == b"I" => HighlightingMode::Invert,
n if n == b"O" => HighlightingMode::Outline,
n if n == b"P" => HighlightingMode::Push,
_ => HighlightingMode::Invert,
})
.unwrap_or(HighlightingMode::Invert);
Some((
page_idx,
LinkAnnotation {
rect,
actions,
border,
color,
highlighting,
},
))
})
.collect::<Vec<_>>(),
)
})
.flatten()
.collect()
}
fn extract_border_array(arr: &[Object]) -> BorderArray {
let nums: Vec<f32> = arr
.iter()
.filter_map(|obj| match obj {
Object::Integer(i) => Some(*i as f32),
Object::Real(r) => Some(*r),
_ => None,
})
.collect();
if nums.len() == 3 {
BorderArray::Solid([nums[0], nums[1], nums[2]])
} else if nums.len() == 4 {
BorderArray::Dashed(
[nums[0], nums[1], nums[2]],
DashPhase {
dash_array: vec![],
phase: nums[3],
},
)
} else {
BorderArray::default()
}
}
fn extract_color_array(arr: &[Object]) -> ColorArray {
let nums: Vec<f32> = arr
.iter()
.filter_map(|obj| match obj {
Object::Integer(i) => Some(*i as f32),
Object::Real(r) => Some(*r),
_ => None,
})
.collect();
match nums.len() {
0 => ColorArray::Transparent,
1 => ColorArray::Gray([nums[0]]),
3 => ColorArray::Rgb([nums[0], nums[1], nums[2]]),
4 => ColorArray::Cmyk([nums[0], nums[1], nums[2], nums[3]]),
_ => ColorArray::default(),
}
}
fn extract_action(dict: &Dictionary, page_map: &BTreeMap<ObjectId, usize>) -> Option<Actions> {
let action_type = dict.get(b"S").ok()?.as_name().ok()?;
match action_type {
n if n == b"GoTo" => {
let arr = dict.get(b"D").ok()?.as_array().ok()?;
let page = if let Some(Object::Reference(page_id)) = arr.first() {
*page_map.get(page_id).unwrap_or(&0)
} else {
0
};
let left = arr.get(2).and_then(|obj| match obj {
Object::Real(r) => Some(*r),
Object::Integer(i) => Some(*i as f32),
_ => None,
});
let top = arr.get(3).and_then(|obj| match obj {
Object::Real(r) => Some(*r),
Object::Integer(i) => Some(*i as f32),
_ => None,
});
let zoom = arr.get(4).and_then(|obj| match obj {
Object::Real(r) => Some(*r),
Object::Integer(i) => Some(*i as f32),
_ => None,
});
Some(Actions::Goto(Destination::Xyz {
page,
left,
top,
zoom,
}))
}
n if n == b"URI" => {
let uri = String::from_utf8_lossy(dict.get(b"URI").ok()?.as_str().ok()?);
Some(Actions::Uri(uri.to_string()))
}
_ => None,
}
}
}
mod layers {
use std::collections::{BTreeMap, BTreeSet};
use lopdf::{Document, Object, ObjectId};
use crate::ops::{Layer, LayerIntent, LayerSubtype};
fn resolve<'a>(doc: &'a Document, obj: &'a Object) -> &'a Object {
match obj {
Object::Reference(r) => doc.get_object(*r).unwrap_or(obj),
other => other,
}
}
pub fn extract_layers(doc: &Document) -> Vec<(Option<String>, Layer)> {
let mut ocg_names: BTreeMap<ObjectId, String> = BTreeMap::new();
for (_page_num, page_id) in doc.get_pages() {
let Ok(page_dict) = doc.get_dictionary(page_id) else {
continue;
};
let Ok(res_obj) = page_dict.get(b"Resources") else {
continue;
};
let Object::Dictionary(res_dict) = resolve(doc, res_obj) else {
continue;
};
let Ok(props_obj) = res_dict.get(b"Properties") else {
continue;
};
let Object::Dictionary(props) = resolve(doc, props_obj) else {
continue;
};
for (name, val) in props.iter() {
if let Object::Reference(ocg_id) = val {
ocg_names
.entry(*ocg_id)
.or_insert_with(|| String::from_utf8_lossy(name).to_string());
}
}
}
let catalog = match doc.trailer.get(b"Root").ok().map(|o| resolve(doc, o)) {
Some(Object::Dictionary(dict)) => dict,
_ => return vec![],
};
let ocprops = match catalog.get(b"OCProperties").ok().map(|o| resolve(doc, o)) {
Some(Object::Dictionary(dict)) => dict,
_ => return vec![],
};
let ocgs = match ocprops.get(b"OCGs").ok().map(|o| resolve(doc, o)) {
Some(Object::Array(arr)) => arr,
_ => return vec![],
};
let default_on = match ocprops.get(b"D").ok().map(|o| resolve(doc, o)) {
Some(Object::Dictionary(d_dict)) => {
if let Ok(Object::Array(on_arr)) = d_dict.get(b"ON") {
on_arr
.iter()
.filter_map(|obj| {
if let Object::Reference(id) = obj {
Some(*id)
} else {
None
}
})
.collect::<BTreeSet<ObjectId>>()
} else {
BTreeSet::new()
}
}
_ => BTreeSet::new(),
};
let mut layers = Vec::new();
for obj in ocgs {
let layer_ref = match obj {
Object::Reference(id) => id,
_ => continue,
};
let layer_dict = match doc.get_object(*layer_ref) {
Ok(Object::Dictionary(dict)) => dict,
_ => continue,
};
let name = match layer_dict.get(b"Name").ok() {
Some(Object::String(s, _)) => s.clone(),
_ => continue,
};
let intent = match layer_dict.get(b"Intent").ok().map(|o| resolve(doc, o)) {
Some(Object::Array(arr)) => match arr.first() {
Some(Object::Name(n)) if n.as_slice() == b"View" => LayerIntent::View,
_ => LayerIntent::Design,
},
Some(Object::Name(n)) if n.as_slice() == b"View" => LayerIntent::View,
_ => LayerIntent::Design,
};
let (creator, usage_subtype) =
match layer_dict.get(b"Usage").ok().map(|o| resolve(doc, o)) {
Some(Object::Dictionary(usage_dict)) => {
if let Some(Object::Dictionary(creator_info)) =
usage_dict.get(b"CreatorInfo").ok().map(|o| resolve(doc, o))
{
let creator = match creator_info.get(b"Creator").ok() {
Some(Object::String(s, _)) => {
String::from_utf8_lossy(s).to_string()
}
_ => String::new(),
};
let usage_subtype = match creator_info.get(b"Subtype").ok() {
Some(Object::Name(n)) => match n.as_slice() {
b"Artwork" => LayerSubtype::Artwork,
_ => LayerSubtype::Artwork,
},
_ => LayerSubtype::Artwork,
};
(creator, usage_subtype)
} else {
(String::new(), LayerSubtype::Artwork)
}
}
_ => (String::new(), LayerSubtype::Artwork),
};
let final_intent = if default_on.contains(layer_ref) {
LayerIntent::View
} else {
intent
};
layers.push((
ocg_names.get(layer_ref).cloned(),
Layer {
name: String::from_utf8_lossy(&name).to_string(),
creator,
intent: final_intent,
usage: usage_subtype,
},
));
}
layers
}
}
mod extgstate {
use std::collections::HashSet;
use lopdf::{Dictionary as LoDictionary, Object};
use crate::{
graphics::{
BlendMode, ChangedField, ExtendedGraphicsState, LineCapStyle, LineDashPattern,
LineJoinStyle, OverprintMode, RenderingIntent,
},
BuiltinFont, BuiltinOrExternalFontId, FontId,
};
pub fn parse_extgstate(dict: &LoDictionary) -> ExtendedGraphicsState {
let mut gs = ExtendedGraphicsState::default();
let mut changed = HashSet::new();
if let Ok(obj) = dict.get(b"LW") {
if let Some(num) = parse_f32(obj) {
gs.line_width = num;
changed.insert(ChangedField::LineWidth);
}
}
if let Ok(obj) = dict.get(b"LC") {
if let Some(num) = parse_i64(obj) {
gs.line_cap = match num {
0 => LineCapStyle::Butt,
1 => LineCapStyle::Round,
2 => LineCapStyle::ProjectingSquare,
_ => LineCapStyle::Butt,
};
changed.insert(ChangedField::LineCap);
}
}
if let Ok(obj) = dict.get(b"LJ") {
if let Some(num) = parse_i64(obj) {
gs.line_join = match num {
0 => LineJoinStyle::Miter,
1 => LineJoinStyle::Round,
2 => LineJoinStyle::Bevel,
_ => LineJoinStyle::Miter,
};
changed.insert(ChangedField::LineJoin);
}
}
if let Ok(obj) = dict.get(b"ML") {
if let Some(num) = parse_f32(obj) {
gs.miter_limit = num;
changed.insert(ChangedField::MiterLimit);
}
}
if let Ok(obj) = dict.get(b"FL") {
if let Some(num) = parse_f32(obj) {
gs.flatness_tolerance = num;
changed.insert(ChangedField::FlatnessTolerance);
}
}
if let Ok(obj) = dict.get(b"RI") {
if let Some(name) = parse_name(obj) {
gs.rendering_intent = match name.as_str() {
"AbsoluteColorimetric" => RenderingIntent::AbsoluteColorimetric,
"RelativeColorimetric" => RenderingIntent::RelativeColorimetric,
"Saturation" => RenderingIntent::Saturation,
"Perceptual" => RenderingIntent::Perceptual,
_ => RenderingIntent::RelativeColorimetric,
};
changed.insert(ChangedField::RenderingIntent);
}
}
if let Ok(obj) = dict.get(b"SA") {
if let Some(b) = parse_bool(obj) {
gs.stroke_adjustment = b;
changed.insert(ChangedField::StrokeAdjustment);
}
}
if let Ok(obj) = dict.get(b"OP") {
if let Some(b) = parse_bool(obj) {
gs.overprint_fill = b;
changed.insert(ChangedField::OverprintFill);
}
}
if let Ok(obj) = dict.get(b"op") {
if let Some(b) = parse_bool(obj) {
gs.overprint_stroke = b;
changed.insert(ChangedField::OverprintStroke);
}
}
if let Ok(obj) = dict.get(b"OPM") {
if let Some(num) = parse_i64(obj) {
gs.overprint_mode = match num {
0 => OverprintMode::EraseUnderlying,
1 => OverprintMode::KeepUnderlying,
_ => OverprintMode::EraseUnderlying,
};
changed.insert(ChangedField::OverprintMode);
}
}
if let Ok(obj) = dict.get(b"CA") {
if let Some(num) = parse_f32(obj) {
gs.current_stroke_alpha = num;
changed.insert(ChangedField::CurrentStrokeAlpha);
}
}
if let Ok(obj) = dict.get(b"ca") {
if let Some(num) = parse_f32(obj) {
gs.current_fill_alpha = num;
changed.insert(ChangedField::CurrentFillAlpha);
}
}
if let Ok(obj) = dict.get(b"BM") {
if let Some(name) = parse_name(obj) {
gs.blend_mode = parse_blend_mode(&name);
changed.insert(ChangedField::BlendMode);
}
}
if let Ok(obj) = dict.get(b"AIS") {
if let Some(b) = parse_bool(obj) {
gs.alpha_is_shape = b;
changed.insert(ChangedField::AlphaIsShape);
}
}
if let Ok(obj) = dict.get(b"TK") {
if let Some(b) = parse_bool(obj) {
gs.text_knockout = b;
changed.insert(ChangedField::TextKnockout);
}
}
if let Ok(obj) = dict.get(b"D") {
if let Ok(arr) = obj.as_array() {
let pattern = arr.iter().filter_map(parse_f32).collect();
gs.line_dash_pattern = Some(LineDashPattern {
offset: 0.0,
pattern,
}); changed.insert(ChangedField::LineDashPattern);
}
}
if let Ok(obj) = dict.get(b"Font") {
if let Some(name) = parse_name(obj) {
gs.font = Some(match BuiltinFont::from_id(&name) {
Some(s) => BuiltinOrExternalFontId::Builtin(s),
None => BuiltinOrExternalFontId::External(FontId(name)),
});
changed.insert(ChangedField::Font);
}
}
if let Ok(obj) = dict.get(b"SM") {
if let Some(name) = parse_name(obj) {
if name == "None" {
gs.soft_mask = None;
} else {
gs.soft_mask = None;
}
changed.insert(ChangedField::SoftMask);
}
}
gs.changed_fields = changed;
gs
}
fn parse_f32(obj: &Object) -> Option<f32> {
match obj {
Object::Real(r) => Some(*r),
Object::Integer(i) => Some(*i as f32),
_ => None,
}
}
fn parse_i64(obj: &Object) -> Option<i64> {
match obj {
Object::Integer(i) => Some(*i),
Object::Real(r) => Some(*r as i64),
_ => None,
}
}
fn parse_bool(obj: &Object) -> Option<bool> {
match obj {
Object::Boolean(b) => Some(*b),
_ => None,
}
}
fn parse_name(obj: &Object) -> Option<String> {
match obj {
Object::Name(bytes) => String::from_utf8(bytes.clone()).ok(),
_ => None,
}
}
fn parse_blend_mode(s: &str) -> BlendMode {
use crate::graphics::{BlendMode, NonSeperableBlendMode, SeperableBlendMode};
match s {
"Normal" => BlendMode::Seperable(SeperableBlendMode::Normal),
"Multiply" => BlendMode::Seperable(SeperableBlendMode::Multiply),
"Screen" => BlendMode::Seperable(SeperableBlendMode::Screen),
"Overlay" => BlendMode::Seperable(SeperableBlendMode::Overlay),
"Darken" => BlendMode::Seperable(SeperableBlendMode::Darken),
"Lighten" => BlendMode::Seperable(SeperableBlendMode::Lighten),
"ColorDodge" => BlendMode::Seperable(SeperableBlendMode::ColorDodge),
"ColorBurn" => BlendMode::Seperable(SeperableBlendMode::ColorBurn),
"HardLight" => BlendMode::Seperable(SeperableBlendMode::HardLight),
"SoftLight" => BlendMode::Seperable(SeperableBlendMode::SoftLight),
"Difference" => BlendMode::Seperable(SeperableBlendMode::Difference),
"Exclusion" => BlendMode::Seperable(SeperableBlendMode::Exclusion),
"Hue" => BlendMode::NonSeperable(NonSeperableBlendMode::Hue),
"Saturation" => BlendMode::NonSeperable(NonSeperableBlendMode::Saturation),
"Color" => BlendMode::NonSeperable(NonSeperableBlendMode::Color),
"Luminosity" => BlendMode::NonSeperable(NonSeperableBlendMode::Luminosity),
_ => BlendMode::Seperable(SeperableBlendMode::Normal),
}
}
}
mod parsefont {
use std::collections::BTreeMap;
use std::sync::Arc;
use lopdf::{Dictionary, Document, Object};
use super::{ParsedExternalFont, ParsedOrBuiltinFont};
use crate::{
cmap::ToUnicodeCMap,
deserialize::{get_dict_or_resolve_ref, PdfWarnMsg},
font::ParsedFont,
BuiltinFont, FontId,
};
fn synthesize_to_unicode(
font: &ParsedFont,
cid_to_gid: Option<&BTreeMap<u16, u16>>,
) -> Option<ToUnicodeCMap> {
let gid_to_cid: Option<BTreeMap<u16, u16>> =
cid_to_gid.map(|m| m.iter().map(|(cid, gid)| (*gid, *cid)).collect());
let mut mappings: BTreeMap<u32, Vec<u32>> = BTreeMap::new();
for cp in 0x0000..=0x10FFFFu32 {
if (0xD800..=0xDFFF).contains(&cp) {
continue;
}
if let Some(gid) = font.lookup_glyph_index(cp) {
let code = gid_to_cid
.as_ref()
.and_then(|m| m.get(&gid).copied())
.unwrap_or(gid);
mappings.entry(code as u32).or_insert_with(|| vec![cp]);
}
}
if mappings.is_empty() {
None
} else {
Some(ToUnicodeCMap { mappings })
}
}
fn extract_encoding_cmap(
doc: &Document,
font_dict: &Dictionary,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<crate::cmap::CidCMap> {
use crate::cmap::CidCMap;
fn from_stream(
s: &lopdf::Stream,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<CidCMap> {
let data = s
.decompressed_content()
.ok()
.or_else(|| Some(s.content.clone()))?;
match CidCMap::parse(&String::from_utf8_lossy(&data)) {
Ok((cmap, unresolved_usecmap)) => {
if let Some(base) = unresolved_usecmap {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"Type0 font {}: embedded CMap uses /{base} as its \
usecmap base, which printpdf cannot resolve — codes \
the CMap itself does not map will decode as notdef",
font_id.0
),
));
}
if cmap.wmode == 1 {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"Type0 font {}: vertical writing (WMode 1) is decoded \
but not preserved — re-saves are horizontal",
font_id.0
),
));
}
Some(cmap)
}
Err(e) => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"Type0 font {}: embedded /Encoding CMap failed to parse \
({e}); assuming 2-byte Identity codes",
font_id.0
),
));
None
}
}
}
match font_dict.get(b"Encoding") {
Ok(Object::Name(n)) if n.as_slice() == b"Identity-H" => None,
Ok(Object::Name(n)) if n.as_slice() == b"Identity-V" => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"Type0 font {} uses Identity-V: codes decode correctly, but \
vertical writing is not preserved — re-saves are horizontal",
font_id.0
),
));
Some(CidCMap::identity(1))
}
Ok(Object::Name(n)) => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"Type0 font {} uses the predefined CMap /{} — printpdf does \
not ship Adobe's CMap data; text may decode and re-save \
incorrectly",
font_id.0,
String::from_utf8_lossy(n),
),
));
None
}
Ok(Object::Stream(s)) => from_stream(s, font_id, warnings, page_num),
Ok(Object::Reference(r)) => match doc.get_object(*r) {
Ok(Object::Stream(s)) => from_stream(s, font_id, warnings, page_num),
_ => None,
},
_ => None, }
}
fn extract_cid_to_gid_map(
doc: &Document,
font_dict: &Dictionary,
font_id: &FontId,
parsed_font: &ParsedFont,
to_unicode: Option<&ToUnicodeCMap>,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<BTreeMap<u16, u16>> {
let descendant = get_descendant_font_dict(doc, font_dict, font_id, warnings, page_num)?;
let stream_to_map = |s: &lopdf::Stream| -> Option<BTreeMap<u16, u16>> {
let data = s.decompressed_content().ok().or_else(|| {
Some(s.content.clone())
})?;
Some(
data.as_chunks::<2>()
.0
.iter()
.enumerate()
.filter_map(|(cid, b)| {
let cid = u16::try_from(cid).ok()?;
let gid = u16::from_be_bytes([b[0], b[1]]);
(gid != cid).then_some((cid, gid))
})
.collect(),
)
};
match descendant.get(b"CIDToGIDMap") {
Ok(Object::Stream(s)) => return stream_to_map(s),
Ok(Object::Reference(r)) => {
if let Ok(Object::Stream(s)) = doc.get_object(*r) {
return stream_to_map(s);
}
}
_ => {} }
#[cfg(feature = "text_layout")]
let bytes = parsed_font.source_bytes()?;
#[cfg(feature = "text_layout")]
let bytes = bytes.as_slice();
#[cfg(not(feature = "text_layout"))]
let bytes = parsed_font.original_bytes.as_slice();
let map: BTreeMap<u16, u16> = crate::font::cff_charset_gid_to_cid_map(bytes, 0)?
.into_iter()
.filter_map(|(gid, cid)| (gid != cid).then_some((cid, gid)))
.collect();
if map.is_empty() {
return None;
}
if let Some(tu) = to_unicode {
let mut spec_hits = 0usize;
let mut identity_hits = 0usize;
for (code, unis) in tu.mappings.iter() {
let Some(&cp) = unis.first() else { continue };
let Ok(code16) = u16::try_from(*code) else {
continue;
};
let Some(cmap_gid) = parsed_font.lookup_glyph_index(cp) else {
continue;
};
let spec_gid = map.get(&code16).copied().unwrap_or(code16);
if spec_gid == cmap_gid {
spec_hits += 1;
}
if code16 == cmap_gid {
identity_hits += 1;
}
}
if identity_hits > spec_hits {
warnings.push(PdfWarnMsg::info(
page_num,
0,
format!(
"font {}: CID-keyed CFF charset is not identity, but the \
document's codes match glyph ids, not CIDs ({} vs {} cmap \
agreements) — treating codes as glyph ids (legacy printpdf \
convention, #280)",
font_id.0, identity_hits, spec_hits
),
));
return None;
}
}
Some(map)
}
pub(crate) fn parse_fonts_enhanced(
doc: &Document,
resources: &Dictionary,
warnings: &mut Vec<PdfWarnMsg>,
page: Option<usize>,
) -> BTreeMap<FontId, ParsedOrBuiltinFont> {
let mut fonts_map = BTreeMap::new();
let page_num = page.unwrap_or_default();
let fonts_dict = match get_fonts_dict_from_resources(doc, resources, warnings, page_num) {
Some(dict) => dict,
None => return fonts_map,
};
for (key, value) in fonts_dict.iter() {
let font_id = FontId(String::from_utf8_lossy(key).to_string());
let font_dict = match get_dict_or_resolve_ref(
&format!("parse_fonts font_dict page {page_num}"),
doc,
value,
warnings,
page,
) {
Some(s) => s,
None => continue,
};
let font_type = match get_font_subtype(font_dict, &font_id, warnings, page_num) {
Some(t) => t,
None => continue,
};
let to_unicode_cmap = extract_to_unicode_cmap(doc, font_dict, warnings, page_num);
if &font_type == b"Type0" {
let encoding_cmap =
extract_encoding_cmap(doc, font_dict, &font_id, warnings, page_num)
.map(Arc::new);
match process_type0_font(doc, font_dict, &font_id, warnings, page_num) {
Some(parsed_font) => {
let cid_to_gid = extract_cid_to_gid_map(
doc,
font_dict,
&font_id,
&parsed_font,
to_unicode_cmap.as_ref(),
warnings,
page_num,
)
.map(Arc::new);
let to_unicode = to_unicode_cmap
.or_else(|| synthesize_to_unicode(&parsed_font, cid_to_gid.as_deref()))
.map(Arc::new);
fonts_map.insert(
font_id,
ParsedOrBuiltinFont::P(ParsedExternalFont {
font: parsed_font,
to_unicode,
is_cid: true,
cid_to_gid,
encoding_cmap,
}),
);
}
None => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"Type0 font {} has no parsable font program; text will decode \
via its ToUnicode CMap but the font cannot be re-embedded",
font_id.0
),
));
fonts_map.insert(
font_id,
ParsedOrBuiltinFont::CidStub(
to_unicode_cmap.map(Arc::new),
encoding_cmap,
),
);
}
}
} else {
match process_standard_font(doc, font_dict, &font_id, warnings, page_num) {
Some(ParsedOrBuiltinFont::B(builtin)) => {
fonts_map.insert(font_id, ParsedOrBuiltinFont::B(builtin));
}
Some(ParsedOrBuiltinFont::P(p)) => {
fonts_map.insert(
font_id,
ParsedOrBuiltinFont::P(ParsedExternalFont {
to_unicode: to_unicode_cmap.map(Arc::new),
..p
}),
);
}
Some(ParsedOrBuiltinFont::CidStub(..)) | None => {}
}
}
}
fonts_map
}
pub(crate) fn extract_to_unicode_cmap(
doc: &Document,
font_dict: &Dictionary,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<ToUnicodeCMap> {
if let Ok(to_unicode_ref) = font_dict.get(b"ToUnicode") {
let to_unicode_stream = match to_unicode_ref {
Object::Stream(s) => Some(s),
Object::Reference(r) => match doc.get_object(*r) {
Ok(Object::Stream(s)) => Some(s),
_ => None,
},
_ => None,
};
if let Some(stream) = to_unicode_stream {
let content = match stream.decompressed_content() {
Ok(data) => data,
Err(_) => stream.content.clone(),
};
if let Ok(cmap_str) = String::from_utf8(content) {
match ToUnicodeCMap::parse(&cmap_str) {
Ok(cmap) => {
return Some(cmap);
}
Err(e) => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!("Failed to parse ToUnicode CMap: {}", e),
));
}
}
}
}
}
None
}
fn get_fonts_dict_from_resources<'a>(
doc: &'a Document,
resources: &'a Dictionary,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<&'a Dictionary> {
let font_map = match resources.get(b"Font") {
Ok(s) => s,
Err(_) => return None,
};
get_dict_or_resolve_ref(
&format!("parse_fonts Font page {page_num}"),
doc,
font_map,
warnings,
Some(page_num),
)
}
fn get_font_subtype(
font_dict: &Dictionary,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<Vec<u8>> {
match font_dict.get(b"Subtype") {
Ok(Object::Name(n)) => Some(n.clone()),
_ => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!("Font {} missing Subtype", font_id.0),
));
None
}
}
}
fn process_type0_font(
doc: &Document,
font_dict: &Dictionary,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<ParsedFont> {
let descendant_font_dict =
get_descendant_font_dict(doc, font_dict, font_id, warnings, page_num)?;
let font_descriptor =
get_font_descriptor(doc, descendant_font_dict, font_id, warnings, page_num)?;
process_font_data(doc, font_dict, font_descriptor, font_id, warnings, page_num)
}
fn get_descendant_font_dict<'a>(
doc: &'a Document,
font_dict: &'a Dictionary,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<&'a Dictionary> {
match font_dict.get(b"DescendantFonts") {
Ok(Object::Array(arr)) if !arr.is_empty() => {
match arr[0].as_dict().ok().or_else(|| {
if let Ok(id) = arr[0].as_reference() {
doc.get_dictionary(id).ok()
} else {
None
}
}) {
Some(d) => Some(d),
None => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!("Cannot resolve descendant font for {}", font_id.0),
));
None
}
}
}
Ok(Object::Reference(id)) => doc.get_dictionary(*id).ok(),
_ => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!("Type0 font {} missing DescendantFonts", font_id.0),
));
None
}
}
}
fn get_font_descriptor<'a>(
doc: &'a Document,
font_dict: &'a Dictionary,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<&'a Dictionary> {
match font_dict.get(b"FontDescriptor") {
Ok(Object::Dictionary(fd)) => Some(fd),
Ok(Object::Reference(r)) => match doc.get_dictionary(*r) {
Ok(fd) => Some(fd),
Err(_) => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!("Cannot resolve FontDescriptor for {}", font_id.0),
));
None
}
},
_ => {
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!("Font {} missing FontDescriptor", font_id.0),
));
None
}
}
}
fn process_font_data(
doc: &Document,
_font_dict: &Dictionary,
font_descriptor: &Dictionary,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<ParsedFont> {
for font_file_key in &[b"FontFile", b"FontFile2" as &[u8], b"FontFile3"] {
if let Ok(font_file_ref) = font_descriptor.get(font_file_key) {
let font_data = get_font_data(doc, font_file_ref, warnings)?;
warnings.push(PdfWarnMsg::info(
page_num,
0,
format!(
"Found font data for {} ({} bytes)",
font_id.0,
font_data.len()
),
));
let mut font_warnings = Vec::new();
if let Some(parsed_font) = ParsedFont::from_bytes(&font_data, 0, &mut font_warnings)
{
for fw in font_warnings {
warnings.push(PdfWarnMsg::warning(page_num, 0, format!("{fw:?}")));
}
return Some(parsed_font);
} else {
warnings.push(PdfWarnMsg::error(
page_num,
0,
format!("Failed to parse font data for {}", font_id.0),
));
}
}
}
None
}
fn get_font_data(
doc: &Document,
font_file_ref: &Object,
_warnings: &mut Vec<PdfWarnMsg>,
) -> Option<Vec<u8>> {
let font_stream = match font_file_ref {
Object::Stream(s) => s,
Object::Reference(r) => match doc.get_object(*r) {
Ok(Object::Stream(s)) => s,
_ => return None,
},
_ => return None,
};
match font_stream.decompressed_content() {
Ok(data) => Some(data),
Err(_) => Some(font_stream.content.clone()),
}
}
#[cfg(feature = "text_layout")]
fn process_type1_font(
doc: &Document,
font_dict: &Dictionary,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<ParsedFont> {
let font_descriptor = get_font_descriptor(doc, font_dict, font_id, warnings, page_num)?;
process_font_data(doc, font_dict, font_descriptor, font_id, warnings, page_num)
}
fn process_standard_font(
_doc: &Document,
font_dict: &Dictionary,
font_id: &FontId,
warnings: &mut Vec<PdfWarnMsg>,
page_num: usize,
) -> Option<ParsedOrBuiltinFont> {
let basefont = match font_dict.get(b"BaseFont") {
Ok(Object::Name(basefont_bytes)) => String::from_utf8_lossy(basefont_bytes).to_string(),
_ => return None,
};
match BuiltinFont::from_id(&basefont) {
Some(builtin) => Some(ParsedOrBuiltinFont::B(builtin)),
None => {
#[cfg(feature = "text_layout")]
{
match process_type1_font(_doc, font_dict, font_id, warnings, page_num) {
Some(parsed_font) => {
Some(ParsedOrBuiltinFont::P(ParsedExternalFont {
font: parsed_font,
to_unicode: None,
is_cid: false,
cid_to_gid: None,
encoding_cmap: None,
}))
}
None => {
let substitute = builtin_substitute(&basefont);
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"Font {} ({}) has no embedded font program; substituting \
builtin {:?}",
font_id.0, basefont, substitute
),
));
Some(ParsedOrBuiltinFont::B(substitute))
}
}
}
#[cfg(not(feature = "text_layout"))]
{
let substitute = builtin_substitute(&basefont);
warnings.push(PdfWarnMsg::warning(
page_num,
0,
format!(
"Font {} ({}) has no embedded font program; substituting builtin \
{:?}",
font_id.0, basefont, substitute
),
));
Some(ParsedOrBuiltinFont::B(substitute))
}
}
}
}
fn builtin_substitute(basefont: &str) -> BuiltinFont {
use crate::BuiltinFont::*;
let name = basefont
.rsplit('+')
.next()
.unwrap_or(basefont)
.to_ascii_lowercase();
let bold = name.contains("bold");
let italic = name.contains("italic") || name.contains("oblique");
if name.contains("times")
|| name.contains("serif")
|| name.contains("georgia")
|| name.contains("garamond")
|| name.contains("book")
{
match (bold, italic) {
(true, true) => TimesBoldItalic,
(true, false) => TimesBold,
(false, true) => TimesItalic,
(false, false) => TimesRoman,
}
} else if name.contains("courier") || name.contains("mono") || name.contains("consol") {
match (bold, italic) {
(true, true) => CourierBoldOblique,
(true, false) => CourierBold,
(false, true) => CourierOblique,
(false, false) => Courier,
}
} else if name.contains("symbol") {
Symbol
} else if name.contains("zapf") || name.contains("dingbat") {
ZapfDingbats
} else {
match (bold, italic) {
(true, true) => HelveticaBoldOblique,
(true, false) => HelveticaBold,
(false, true) => HelveticaOblique,
(false, false) => Helvetica,
}
}
}
}
fn decode_text_from_utf16be(bytes: &[u8]) -> String {
if bytes.len() < 2 {
return String::from_utf8_lossy(bytes).into_owned();
}
if bytes[0] != 0xFE || bytes[1] != 0xFF {
return String::from_utf8_lossy(bytes).into_owned();
}
let (chunks, remainder) = bytes[2..].as_chunks::<2>();
let string = char::decode_utf16(chunks.iter().copied().map(u16::from_be_bytes))
.map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
.collect();
if remainder.is_empty() {
string
} else {
string + "\u{FFFD}"
}
}