Support native PDF font dictionaries and running link controls

This commit is contained in:
gloamxun committed 2026-11-21 02:46:57 +08:00
1 parent 60d7411dfe
commit b647bd8dcd
19 files changed
+180 -13

No files matched your search

@@ -41,6 +41,7 @@ pub enum DestinationId<T> {
pub enum LinkNode {
Start(LinkStart),
End,
Running(bool),
Destination {
destination: DestinationId<Vec<Token>>,
view: DestinationView,
@@ -1,4 +1,5 @@
//! Native PDF objects and extra resources captured at page shipout.
use crate::execution::FontId;
use lopdf::{
Dictionary, Object, Stream,
content::{Content, Operation},
@@ -294,6 +295,11 @@ fn parse_entries(bytes: &[u8]) -> Result<Dictionary, String> {
}
}
pub(crate) fn font_attributes_affect_paint(bytes: &[u8]) -> Result<bool, String> {
let attributes = parse_entries(bytes)?;
Ok(attributes.iter().any(|(name, _)| name != b"ToUnicode"))
}
fn encode_value(object: Object) -> Result<Vec<u8>, String> {
if let Object::Stream(stream) = object {
let mut output = encode_value(Object::Dictionary(stream.dict))?;
@@ -405,6 +411,7 @@ pub(crate) struct ResourceOutput {
page_references: BTreeMap<u32, u32>,
link_references: BTreeMap<u32, Option<(usize, usize)>>,
page_entries: Vec<Dictionary>,
font_entries: BTreeMap<FontId, Dictionary>,
annotation_entries: Vec<Vec<Option<Dictionary>>>,
catalog: Option<Dictionary>,
info: Option<Dictionary>,
@@ -412,6 +419,7 @@ pub(crate) struct ResourceOutput {
pub(crate) struct RemappedResources {
pub pages: Vec<Vec<u8>>,
pub fonts: BTreeMap<FontId, Vec<u8>>,
pub page_objects: BTreeMap<usize, usize>,
pub link_objects: BTreeMap<(usize, usize), usize>,
pub annotations: Vec<Vec<Option<Vec<u8>>>>,
@@ -423,6 +431,7 @@ impl ResourceOutput {
pub(crate) fn prepare(
pages: &[crate::render::DisplayList],
objects: Option<&PdfObjects>,
fonts: BTreeMap<FontId, &[u8]>,
catalog: Option<&[u8]>,
info: Option<&[u8]>,
) -> Result<Self, String> {
@@ -440,10 +449,14 @@ impl ResourceOutput {
Ok(entries)
})
.collect::<Result<Vec<_>, String>>()?;
let font_entries = fonts
.into_iter()
.map(|(font, entries)| Ok((font, parse_entries(entries)?)))
.collect::<Result<BTreeMap<_, _>, String>>()?;
let catalog = catalog.map(parse_entries).transpose()?;
let info = info.map(parse_entries).transpose()?;
let mut pending = objects.written.clone();
for dictionary in page_entries.iter().chain(catalog.iter()).chain(info.iter()) {
for dictionary in page_entries.iter().chain(font_entries.values()).chain(&catalog).chain(&info) {
for (_, entry) in dictionary.iter() {
references(entry, &mut pending)?;
}
@@ -509,6 +522,7 @@ impl ResourceOutput {
page_references: objects.page_references.clone(),
link_references,
page_entries,
font_entries,
annotation_entries,
catalog,
info,
@@ -567,6 +581,9 @@ impl ResourceOutput {
};
RemappedResources {
pages: self.page_entries.into_iter().map(&encode).collect(),
fonts: self.font_entries.into_iter()
.map(|(font, dictionary)| (font, encode(dictionary)))
.collect(),
page_objects,
link_objects,
annotations: self.annotation_entries.into_iter()