Resolve named PDF image destinations through the page tree
This commit is contained in:
1 parent
4c532f9396
commit
58012d9397
5 files changed
+196
-17
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::{
|
use crate::{
|
||||||
pdf_image::{PageBox, PdfImage, PdfImageObject, PdfImageUse, XdvImagePlacement},
|
pdf_image::{PageBox, PageSelection, PdfImage, PdfImageObject, PdfImageUse, XdvImagePlacement},
|
||||||
resources::ResourceKind,
|
resources::ResourceKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -67,13 +67,12 @@ impl Executor {
|
|||||||
} else {
|
} else {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
};
|
};
|
||||||
if self.keyword("named", files)? {
|
let page = if self.keyword("named", files)? {
|
||||||
return self.fail("native PDF image named destinations require PDF object support");
|
PageSelection::Named(self.scan_pdf_bytes(files)?)
|
||||||
}
|
} else if self.keyword("page", files)? {
|
||||||
let page = if self.keyword("page", files)? {
|
PageSelection::Number(self.integer(files)?)
|
||||||
self.integer(files)?
|
|
||||||
} else {
|
} else {
|
||||||
1
|
PageSelection::Number(1)
|
||||||
};
|
};
|
||||||
if self.keyword("colorspace", files)? {
|
if self.keyword("colorspace", files)? {
|
||||||
return self.fail("native PDF image colorspace requires PDF object support");
|
return self.fail("native PDF image colorspace requires PDF object support");
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
//! Native image XObjects and the resource objects reachable from their content.
|
//! Native image XObjects and the resource objects reachable from their content.
|
||||||
mod bmp;
|
mod bmp;
|
||||||
|
mod destinations;
|
||||||
mod jpeg;
|
mod jpeg;
|
||||||
mod icc;
|
mod icc;
|
||||||
mod imported_fonts;
|
mod imported_fonts;
|
||||||
@@ -16,6 +17,11 @@ use std::{
|
|||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub(crate) enum PageSelection {
|
||||||
|
Number(i32),
|
||||||
|
Named(Vec<u8>),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Default)]
|
#[derive(Clone, Copy, Debug, Default)]
|
||||||
pub(crate) enum PageBox {
|
pub(crate) enum PageBox {
|
||||||
Media,
|
Media,
|
||||||
@@ -554,14 +560,17 @@ impl PdfImage {
|
|||||||
pub(crate) fn read(
|
pub(crate) fn read(
|
||||||
resource: LoadedResource,
|
resource: LoadedResource,
|
||||||
doc: Document,
|
doc: Document,
|
||||||
page: i32,
|
selection: PageSelection,
|
||||||
kind: PageBox,
|
kind: PageBox,
|
||||||
remap_type1: bool,
|
remap_type1: bool,
|
||||||
) -> Result<Self, String> {
|
) -> Result<Self, String> {
|
||||||
Self::read_page(resource, doc, kind.keys(), false, remap_type1, |pages| {
|
Self::read_page(resource, doc, kind.keys(), false, remap_type1, |doc, pages| {
|
||||||
u32::try_from(page)
|
match selection {
|
||||||
.ok()
|
PageSelection::Number(page) => {
|
||||||
.filter(|&page| page > 0 && page as usize <= pages)
|
u32::try_from(page).map_err(|_| "required page does not exist".into())
|
||||||
|
}
|
||||||
|
PageSelection::Named(name) => destinations::page(doc, pages, &name),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.map(|(image, _)| image)
|
.map(|(image, _)| image)
|
||||||
}
|
}
|
||||||
@@ -580,14 +589,16 @@ impl PdfImage {
|
|||||||
b"ArtBox",
|
b"ArtBox",
|
||||||
];
|
];
|
||||||
let doc = Self::open_pdf(&resource)?;
|
let doc = Self::open_pdf(&resource)?;
|
||||||
Self::read_page(resource, doc, &keys, true, false, |pages| {
|
Self::read_page(resource, doc, &keys, true, false, |_, pages| {
|
||||||
let pages = i64::try_from(pages).ok()?;
|
let pages = i64::try_from(pages.len()).map_err(|_| "required page does not exist")?;
|
||||||
let page = match page {
|
let page = match page {
|
||||||
0 => 1,
|
0 => 1,
|
||||||
page if page < 0 => pages + i64::from(page) + 1,
|
page if page < 0 => pages + i64::from(page) + 1,
|
||||||
page => i64::from(page),
|
page => i64::from(page),
|
||||||
};
|
};
|
||||||
(pages > 0).then(|| page.clamp(1, pages) as u32)
|
(pages > 0)
|
||||||
|
.then(|| page.clamp(1, pages) as u32)
|
||||||
|
.ok_or_else(|| "required page does not exist".into())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -614,10 +625,10 @@ impl PdfImage {
|
|||||||
box_keys: &[&[u8]],
|
box_keys: &[&[u8]],
|
||||||
keep_page_origin: bool,
|
keep_page_origin: bool,
|
||||||
remap_type1: bool,
|
remap_type1: bool,
|
||||||
select_page: impl FnOnce(usize) -> Option<u32>,
|
select_page: impl FnOnce(&Document, &BTreeMap<u32, ObjectId>) -> Result<u32, String>,
|
||||||
) -> Result<(Self, [f64; 2]), String> {
|
) -> Result<(Self, [f64; 2]), String> {
|
||||||
let pages = doc.get_pages();
|
let pages = doc.get_pages();
|
||||||
let page = select_page(pages.len()).ok_or("required page does not exist")?;
|
let page = select_page(&doc, &pages)?;
|
||||||
let page_id = pages
|
let page_id = pages
|
||||||
.get(&page)
|
.get(&page)
|
||||||
.copied()
|
.copied()
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
//! Resolve local PDF destinations to pages in the document's actual page tree.
|
||||||
|
use super::resolved;
|
||||||
|
use lopdf::{Dictionary, Document, Object, ObjectId};
|
||||||
|
use std::{
|
||||||
|
cmp::Ordering,
|
||||||
|
collections::{BTreeMap, BTreeSet},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub(super) fn page(
|
||||||
|
doc: &Document,
|
||||||
|
pages: &BTreeMap<u32, ObjectId>,
|
||||||
|
name: &[u8],
|
||||||
|
) -> Result<u32, String> {
|
||||||
|
// pdfTeX hands the expanded TeX bytes to the PDF reader as a C string.
|
||||||
|
let name = name.split(|&byte| byte == 0).next().unwrap_or_default();
|
||||||
|
let invalid = || {
|
||||||
|
format!(
|
||||||
|
"PDF inclusion: invalid destination <{}>",
|
||||||
|
String::from_utf8_lossy(name)
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let value = destination(doc, name).ok_or_else(invalid)?;
|
||||||
|
let destination = match value {
|
||||||
|
Object::Dictionary(dict) => entry(doc, dict, b"D").ok_or_else(invalid)?,
|
||||||
|
value => value,
|
||||||
|
};
|
||||||
|
let [reference, view, positions @ ..] = destination
|
||||||
|
.as_array()
|
||||||
|
.ok()
|
||||||
|
.map(Vec::as_slice)
|
||||||
|
.ok_or_else(invalid)?
|
||||||
|
else {
|
||||||
|
return Err(invalid());
|
||||||
|
};
|
||||||
|
let view = resolved(doc, view)
|
||||||
|
.ok()
|
||||||
|
.and_then(|view| view.as_name().ok());
|
||||||
|
let valid = match view {
|
||||||
|
Some(b"XYZ") => positions.iter().take(3).all(|position| {
|
||||||
|
matches!(
|
||||||
|
resolved(doc, position),
|
||||||
|
Ok(Object::Null | Object::Integer(_) | Object::Real(_))
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
Some(b"Fit" | b"FitB") => true,
|
||||||
|
Some(b"FitH" | b"FitV" | b"FitBH" | b"FitBV") => !positions.is_empty(),
|
||||||
|
Some(b"FitR") => positions.len() >= 4,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
if !valid {
|
||||||
|
return Err(invalid());
|
||||||
|
}
|
||||||
|
let page = match reference {
|
||||||
|
Object::Reference(id) => pages
|
||||||
|
.iter()
|
||||||
|
.find_map(|(&number, page)| (page == id).then_some(number)),
|
||||||
|
// Integer destinations can address remote pages. A local inclusion
|
||||||
|
// needs an actual page reference in this document.
|
||||||
|
Object::Integer(_) => None,
|
||||||
|
_ => return Err(invalid()),
|
||||||
|
};
|
||||||
|
page.ok_or_else(|| "PDF inclusion: destination is not a page".into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn entry<'a>(doc: &'a Document, dict: &'a Dictionary, key: &[u8]) -> Option<&'a Object> {
|
||||||
|
resolved(doc, dict.get(key).ok()?).ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn destination<'a>(doc: &'a Document, name: &[u8]) -> Option<&'a Object> {
|
||||||
|
let catalog = entry(doc, &doc.trailer, b"Root")?.as_dict().ok()?;
|
||||||
|
if let Some(value) = entry(doc, catalog, b"Dests")
|
||||||
|
.and_then(|dests| dests.as_dict().ok())
|
||||||
|
.and_then(|dests| entry(doc, dests, name))
|
||||||
|
.filter(|value| !matches!(value, Object::Null))
|
||||||
|
{
|
||||||
|
return Some(value);
|
||||||
|
}
|
||||||
|
let names = entry(doc, catalog, b"Names")?.as_dict().ok()?;
|
||||||
|
let mut node = names.get(b"Dests").ok()?;
|
||||||
|
let mut seen = BTreeSet::new();
|
||||||
|
// Follow only the matching Limits range. Track visited references so
|
||||||
|
// malformed cycles terminate without limiting a well-formed tree's depth.
|
||||||
|
loop {
|
||||||
|
if let Object::Reference(id) = node
|
||||||
|
&& !seen.insert(*id)
|
||||||
|
{
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let dict = resolved(doc, node).ok()?.as_dict().ok()?;
|
||||||
|
if let Some(names) = entry(doc, dict, b"Names").and_then(|names| names.as_array().ok()) {
|
||||||
|
for pair in names.chunks_exact(2) {
|
||||||
|
let Some(key) = resolved(doc, &pair[0])
|
||||||
|
.ok()
|
||||||
|
.and_then(|key| key.as_str().ok())
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
match name.cmp(key) {
|
||||||
|
Ordering::Equal => return resolved(doc, &pair[1]).ok(),
|
||||||
|
Ordering::Less => break,
|
||||||
|
Ordering::Greater => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let kids = entry(doc, dict, b"Kids")?.as_array().ok()?;
|
||||||
|
node = kids.iter().find(|kid| {
|
||||||
|
let bounds = || {
|
||||||
|
let child = resolved(doc, kid).ok()?.as_dict().ok()?;
|
||||||
|
let limits = entry(doc, child, b"Limits")?.as_array().ok()?;
|
||||||
|
let [low, high, ..] = limits.as_slice() else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
Some((
|
||||||
|
resolved(doc, low).ok()?.as_str().ok()?,
|
||||||
|
resolved(doc, high).ok()?.as_str().ok()?,
|
||||||
|
))
|
||||||
|
};
|
||||||
|
bounds().is_some_and(|(low, high)| low <= name && name <= high)
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2175,6 +2175,43 @@ PDF,恢复实际 PFB 并反转斜切时复用 1277 步,只替换第二页。
|
|||||||
全量编译一致。incremental-receipt.json 的 SHA-256 为
|
全量编译一致。incremental-receipt.json 的 SHA-256 为
|
||||||
bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。
|
bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。
|
||||||
|
|
||||||
|
### pdfTeX 按命名目标导入实际 PDF 页面
|
||||||
|
|
||||||
|
\pdfximage named 使用展开后的 TeX 字节查找实际目标。先读取
|
||||||
|
Catalog 的旧式 Dests 字典,再沿 Names/Dests 名称树的 Limits
|
||||||
|
查找子树和叶子。目标数组和间接 D 字典都解析为页树中的实际
|
||||||
|
页面引用。名称不存在、视图无效或引用不属于页树时给出对应的
|
||||||
|
导入诊断。版本检查先于选择。选中的页进入数字页码使用的同一
|
||||||
|
裁边、旋转、缩放和资源图导入。
|
||||||
|
[参考的目标查找与树遍历](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/libs/xpdf/xpdf-src/xpdf/Catalog.cc#L389-L514)
|
||||||
|
[参考将目标引用映射到页面](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/pdftexdir/pdftoepdf.cc#L745-L760)
|
||||||
|
|
||||||
|
24 个对照含 8 个预期失败。覆盖多层树、旧字典优先、空值回落、
|
||||||
|
原始 8 位字符、零字节截断、旋转及显式页框。80 层间接树与参考
|
||||||
|
一致,循环通过已访问引用终止。九对命名和数字页选择得到字节
|
||||||
|
相同的原生 PDF,五个命名结果的 144 DPI 灰度与参考逐像素一致。
|
||||||
|
三页 XDV 数值选页出图与前版逐字节一致。
|
||||||
|
|
||||||
|
同一项目只改 PDF 的目标从第二页指到第三页,再移除目标并恢复。
|
||||||
|
两次成功跨文件修订各复用 108 步和两个文件调用,只替换第二页。
|
||||||
|
PDF、SVG、实际读取的资源、来源映射及辅助文件与独立全量对照。
|
||||||
|
目标缺失的错误与全量一致,同时保留上一版 PDF 和 aux。
|
||||||
|
|
||||||
|
原样 Springer Nature 的旧 schema27 复编在改动前后得出字节
|
||||||
|
相同的 12 页 PDF。两份都少了参考 aux 的两行表单定义。实查
|
||||||
|
hyperref 使用 \pdflastlink 存在性选择该初始化分支,旧镜像
|
||||||
|
缺该命令。随后采用已由原样 pdflatex.ini 生成的 schema79,
|
||||||
|
核验三项 LaTeX 核心输入身份、当前模板、字体映射与实际 EPS
|
||||||
|
转换件。新格式三轮 aux 和真实 BibTeX 的 bbl 与参考逐字节相同。
|
||||||
|
12 页 3357 个词的顺序一致,最大词框差 0.002395 bp。144 DPI 灰度差异为 216 个像素(第7页 90 个像素、第11页 126 个像素)。
|
||||||
|
旧格式结果及两行差异原样保留。
|
||||||
|
|
||||||
|
回执 target/p3-pdf-named-pages/receipt.json 的 SHA-256 为
|
||||||
|
ab811e792535c69be6282543ce8b3dffce58581f1e71d87359cd299c3eecb6db。
|
||||||
|
下一项为 \pdfpagebox 和旧页框控制。实际命名页的参考预览已
|
||||||
|
量得默认裁边、指定 media、显式 trim 和强制覆盖的不同尺寸。
|
||||||
|
参考及原生缺少参数的诊断在同目录 next-pagebox。
|
||||||
|
|
||||||
### pdfTeX 导入版本检查与设备初始化时机
|
### pdfTeX 导入版本检查与设备初始化时机
|
||||||
|
|
||||||
首个图像声明固定作业的 PDF 头版本、输出模式与对象压缩。
|
首个图像声明固定作业的 PDF 头版本、输出模式与对象压缩。
|
||||||
|
|||||||
@@ -977,6 +977,16 @@ LuaLaTex、languages 和 argues 的断行与冻结参考一致。PDF 提取的
|
|||||||
字形提取仍待对照,当前整页版面门禁保持未通过。
|
字形提取仍待对照,当前整页版面门禁保持未通过。
|
||||||
阶段回执 `target/p3-xetex-native-hyphenation/receipt.json`。
|
阶段回执 `target/p3-xetex-native-hyphenation/receipt.json`。
|
||||||
|
|
||||||
|
### Springer Nature 的原样图像与辅助文件复验
|
||||||
|
|
||||||
|
原样 16 项期刊交付和实际 EPS 转换件沿用冻结身份。旧 schema27
|
||||||
|
在前版和首个命名导入候选上给出字节相同的 12 页 PDF。该格式
|
||||||
|
未登记 \pdflastlink,hyperref 的两行辅助定义因此缺失。用原样
|
||||||
|
pdflatex.ini 生成且包含该原语的 schema79 复编后,三轮 aux 与
|
||||||
|
实跑的 BibTeX bbl 均与参考逐字节相同。
|
||||||
|
12 页 3357 个词的顺序一致,最大词框差 0.002395 bp。144 DPI 灰度差异为 216 个像素(第7页 90 个像素、第11页 126 个像素)。
|
||||||
|
[阶段记录](P2_M1_PROGRESS.md#pdftex-按命名目标导入实际-pdf-页面)
|
||||||
|
|
||||||
### Springer LNCS 的实际 PDF 导入复验
|
### Springer LNCS 的实际 PDF 导入复验
|
||||||
|
|
||||||
原样 Springer LNCS 会议样稿沿用冻结的格式、字体映射和官方
|
原样 Springer LNCS 会议样稿沿用冻结的格式、字体映射和官方
|
||||||
|
|||||||
Reference in new issue
Block a user