Honor XeTeX page setup during native shipout
This commit is contained in:
1 parent
9da2ee41cd
commit
d944552188
7 files changed
+281
-42
No files matched your search
@@ -292,7 +292,8 @@ impl Executor {
|
||||
origin(i64::from(self.read_parameter(Parameter::Pdfhorigin).width())) + h_offset;
|
||||
let vertical_origin =
|
||||
origin(i64::from(self.read_parameter(Parameter::Pdfvorigin).width())) + v_offset;
|
||||
let mut page_width = i64::from(self.read_parameter(Parameter::Pdfpagewidth).width());
|
||||
let requested_page_width = i64::from(self.read_parameter(Parameter::Pdfpagewidth).width());
|
||||
let mut page_width = requested_page_width;
|
||||
let requested_page_height = i64::from(self.read_parameter(Parameter::Pdfpageheight).width());
|
||||
let default_canvas = |bp: i64| origin(bp * 803 * 65536 / 800);
|
||||
if page_width == 0 {
|
||||
@@ -414,6 +415,17 @@ impl Executor {
|
||||
let mut pdf_objects = Vec::new();
|
||||
let mut xdv_annotations = std::collections::BTreeMap::new();
|
||||
let mut reopened_images = false;
|
||||
let mut xdv_paper = self.state.xdv_setup.paper;
|
||||
if xdv {
|
||||
// XeTeX emits an implicit pagesize at the start of every page.
|
||||
if requested_page_width > 0 && requested_page_height > 0 {
|
||||
xdv_paper.size = [Some(requested_page_width), Some(requested_page_height)];
|
||||
} else {
|
||||
xdv_paper.offset = [None; 2];
|
||||
}
|
||||
}
|
||||
let mut xdv_navigation = Vec::new();
|
||||
let first_page = self.shipped_pages == 0;
|
||||
for (index, output) in outputs.into_iter().enumerate() {
|
||||
while font_uses.peek().is_some_and(|usage| usage.before_output == index) {
|
||||
self.current = current;
|
||||
@@ -458,10 +470,6 @@ impl Executor {
|
||||
let special = self.resolve_special(
|
||||
output, position.expect("special needs a shipout position"), files,
|
||||
)?;
|
||||
let point = xdv_transform.point(
|
||||
(special.position[0] + horizontal_origin) as f64,
|
||||
(special.position[1] + vertical_origin + b.height) as f64,
|
||||
).map(|coordinate| coordinate.round() as i64);
|
||||
let graphics_step = shipout.pdf_graphics_steps.len();
|
||||
let graphics = if let Some(pdf) = &device.pdf {
|
||||
if let Some(color) = xdv.then(|| self.xdv_colors.apply(&special.bytes)).flatten() {
|
||||
@@ -478,18 +486,23 @@ impl Executor {
|
||||
} else if xdv && crate::pdf_device::xdv_annotation::Command::recognize(&special.bytes).is_some() {
|
||||
xdv_annotations.insert(graphics_step, shipout.specials.len());
|
||||
None
|
||||
} else if xdv && self.xdv_page_special(
|
||||
&special,
|
||||
crate::pdf_device::xdv_object::Position {
|
||||
page: self.shipped_pages + 1,
|
||||
coordinates: point,
|
||||
height: device.page_height,
|
||||
magnification: device.magnification,
|
||||
},
|
||||
graphics_step,
|
||||
&mut shipout.pdf_links,
|
||||
files,
|
||||
)? {
|
||||
} else if xdv && let Some(operands) = super::specials::xdv_pagesize_operand(&special.bytes) {
|
||||
if let Err(message) = xdv_paper.read(
|
||||
operands, magnification, (!first_page).then_some(self.state.xdv_setup.paper.size),
|
||||
) {
|
||||
files.observe_diagnostic(
|
||||
WriteDestination::TerminalAndLog, &format!("xdvipdfmx: warning: {message}\n"),
|
||||
).map_err(|error| self.error(error, special.span))?;
|
||||
}
|
||||
None
|
||||
} else if xdv && super::specials::xdv_navigation_operand(&special.bytes).is_some() {
|
||||
let point = xdv_transform.point(
|
||||
(special.position[0] + horizontal_origin) as f64,
|
||||
(special.position[1] + vertical_origin + b.height) as f64,
|
||||
).map(|coordinate| coordinate.round() as i64);
|
||||
xdv_navigation.push((shipout.specials.len(), graphics_step, point));
|
||||
None
|
||||
} else if xdv && self.xdv_setup_special(&special)? {
|
||||
None
|
||||
} else if xdv && let Some(image) = crate::pdf_device::xdv_image::ImageSpecial::parse(
|
||||
&special.bytes, device.magnification,
|
||||
@@ -657,6 +670,41 @@ impl Executor {
|
||||
if reopened_images {
|
||||
self.rebind_shipped_format_images(&mut shipout.box_node)?;
|
||||
}
|
||||
if xdv {
|
||||
// Page setup applies to the entire page, including graphics and
|
||||
// destination specials emitted before the paper special.
|
||||
let setup = &mut self.state.xdv_setup.paper;
|
||||
if first_page {
|
||||
setup.size = xdv_paper.size;
|
||||
}
|
||||
setup.offset = xdv_paper.offset;
|
||||
let device = &mut shipout.device;
|
||||
if let Some(width) = xdv_paper.size[0] {
|
||||
device.page_width = width;
|
||||
}
|
||||
if let Some(height) = xdv_paper.size[1] {
|
||||
device.page_height = height;
|
||||
}
|
||||
if let Some(offset) = xdv_paper.offset[0] {
|
||||
device.horizontal_origin = offset + h_offset;
|
||||
}
|
||||
if let Some(offset) = xdv_paper.offset[1] {
|
||||
device.vertical_origin = offset + v_offset;
|
||||
}
|
||||
let delta = [device.horizontal_origin - horizontal_origin, device.vertical_origin - vertical_origin];
|
||||
for (index, graphics_step, [x, y]) in xdv_navigation {
|
||||
self.ship_xdv_navigation(
|
||||
&shipout.specials[index],
|
||||
crate::pdf_device::xdv_object::Position {
|
||||
page: self.shipped_pages + 1,
|
||||
coordinates: [x + delta[0], y + delta[1]],
|
||||
height: device.page_height,
|
||||
magnification,
|
||||
},
|
||||
graphics_step, &mut shipout.pdf_links, files,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
if xdv && (!xdv_annotations.is_empty() || self.xdv_annotations.is_active()) {
|
||||
shipout.pdf_links.xdv_annotations = self.ship_xdv_annotations(&shipout, &xdv_annotations, files)?;
|
||||
}
|
||||
|
||||
@@ -49,23 +49,44 @@ impl Executor {
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn xdv_page_special(
|
||||
pub(super) fn ship_xdv_navigation(
|
||||
&mut self,
|
||||
special: &ShipoutSpecial,
|
||||
position: crate::pdf_device::xdv_object::Position,
|
||||
graphics_step: usize,
|
||||
links: &mut crate::pdf_device::links::PageLinks,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<(), Error> {
|
||||
let Some((kind, operands)) = xdv_navigation_operand(&special.bytes) else {
|
||||
return self.fail("missing shipped XDV navigation operands");
|
||||
};
|
||||
if kind == b"dest" {
|
||||
let destination = crate::pdf_device::xdv_destination::Destination::parse(
|
||||
operands, position.page, position.coordinates,
|
||||
).map_err(|error| self.error(error, special.span))?;
|
||||
links.xdv_destinations.push(destination);
|
||||
return Ok(());
|
||||
}
|
||||
self.reads.insert(Dependency::PdfCatalogState);
|
||||
let navigation = Arc::make_mut(self.state.xdv_navigation.get_or_insert_with(Default::default));
|
||||
let warnings = if kind == b"docview" {
|
||||
navigation.merge_catalog(operands, position, self.limits.tokens)
|
||||
} else {
|
||||
navigation.add_outline(operands, position, (position.page, graphics_step, 1), self.limits.tokens)
|
||||
}.map_err(|error| self.error(error, special.span))?;
|
||||
for warning in warnings {
|
||||
files.observe_diagnostic(WriteDestination::TerminalAndLog, &warning)
|
||||
.map_err(|error| self.error(error, special.span))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn xdv_setup_special(
|
||||
&mut self,
|
||||
special: &ShipoutSpecial,
|
||||
) -> Result<bool, Error> {
|
||||
use crate::pdf_device::xdv_destination::Destination;
|
||||
let data = special.bytes.trim_ascii_start();
|
||||
if let Some(body) = data.strip_prefix(b"pdf:").map(<[u8]>::trim_ascii_start) {
|
||||
if let Some(operands) = xdv_operand(body, b"dest").or_else(|| xdv_operand(body, b"destination")) {
|
||||
let destination = Destination::parse(operands, position.page, position.coordinates)
|
||||
.map_err(|error| self.error(error, special.span))?;
|
||||
links.xdv_destinations.push(destination);
|
||||
return Ok(true);
|
||||
}
|
||||
if let Some(body) = xdv_pdf_body(data) {
|
||||
for (name, major) in [(b"majorversion".as_slice(), true), (b"minorversion", false)] {
|
||||
if let Some(operands) = xdv_operand(body, name) {
|
||||
if self.shipped_pages == 0 {
|
||||
@@ -75,22 +96,6 @@ impl Executor {
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
let catalog = xdv_operand(body, b"docview");
|
||||
let outline = xdv_operand(body, b"outline").or_else(|| xdv_operand(body, b"out"));
|
||||
if let Some(operands) = catalog.or(outline) {
|
||||
self.reads.insert(Dependency::PdfCatalogState);
|
||||
let navigation = Arc::make_mut(self.state.xdv_navigation.get_or_insert_with(Default::default));
|
||||
let warnings = if catalog.is_some() {
|
||||
navigation.merge_catalog(operands, position, self.limits.tokens)
|
||||
} else {
|
||||
navigation.add_outline(operands, position, (position.page, graphics_step, 1), self.limits.tokens)
|
||||
}.map_err(|error| self.error(error, special.span))?;
|
||||
for warning in warnings {
|
||||
files.observe_diagnostic(WriteDestination::TerminalAndLog, &warning)
|
||||
.map_err(|error| self.error(error, special.span))?;
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
} else if let Some(body) = data.strip_prefix(b"dvipdfmx:").map(<[u8]>::trim_ascii_start)
|
||||
&& let Some(operands) = xdv_operand(body, b"config")
|
||||
{
|
||||
@@ -204,6 +209,27 @@ impl Executor {
|
||||
}
|
||||
}
|
||||
|
||||
fn xdv_pdf_body(data: &[u8]) -> Option<&[u8]> {
|
||||
data.trim_ascii_start().strip_prefix(b"pdf:").map(<[u8]>::trim_ascii_start)
|
||||
}
|
||||
|
||||
pub(super) fn xdv_pagesize_operand(data: &[u8]) -> Option<&[u8]> {
|
||||
xdv_operand(xdv_pdf_body(data)?, b"pagesize")
|
||||
}
|
||||
|
||||
pub(super) fn xdv_navigation_operand(data: &[u8]) -> Option<(&'static [u8], &[u8])> {
|
||||
let body = xdv_pdf_body(data)?;
|
||||
for (name, kind) in [
|
||||
(b"dest".as_slice(), b"dest".as_slice()), (b"destination", b"dest"),
|
||||
(b"docview", b"docview"), (b"outline", b"outline"), (b"out", b"outline"),
|
||||
] {
|
||||
if let Some(operands) = xdv_operand(body, name) {
|
||||
return Some((kind, operands));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn xdv_operand<'a>(data: &'a [u8], command: &[u8]) -> Option<&'a [u8]> {
|
||||
data.strip_prefix(command).filter(|rest| {
|
||||
!rest.first().is_some_and(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b':'))
|
||||
|
||||
@@ -124,6 +124,7 @@ pub(crate) mod xdv_document;
|
||||
pub(crate) mod xdv_image;
|
||||
pub(crate) mod xdv_navigation;
|
||||
pub(crate) mod xdv_object;
|
||||
pub(crate) mod xdv_paper;
|
||||
mod graphics;
|
||||
pub(crate) use color::{ColorResult, PdfColorStacks, ShippedColor};
|
||||
pub use color::{PdfColorProgram, PdfColorStackAction};
|
||||
|
||||
@@ -13,6 +13,8 @@ pub(crate) struct Setup {
|
||||
pub(crate) keep_destinations: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) outline_open_depth: Option<usize>,
|
||||
#[serde(default, skip_serializing_if = "super::xdv_paper::Paper::is_default")]
|
||||
pub(crate) paper: super::xdv_paper::Paper,
|
||||
}
|
||||
|
||||
impl Setup {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
//! XDV page setup is read in special order before the driver paints a page.
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub(crate) struct Paper {
|
||||
/// Logical TeX sp. The first shipped page sets the document defaults.
|
||||
pub(crate) size: [Option<i64>; 2],
|
||||
/// Driver origin, independent of TeX's horizontal and vertical moves.
|
||||
pub(crate) offset: [Option<i64>; 2],
|
||||
}
|
||||
|
||||
impl Paper {
|
||||
pub(crate) fn is_default(&self) -> bool {
|
||||
*self == Self::default()
|
||||
}
|
||||
|
||||
/// The first page updates its own defaults. Subsequent pages start from
|
||||
/// those dimensions, while the physical origin carries across pages.
|
||||
pub(crate) fn read(
|
||||
&mut self,
|
||||
input: &[u8],
|
||||
magnification: i32,
|
||||
document_size: Option<[Option<i64>; 2]>,
|
||||
) -> Result<(), &'static str> {
|
||||
let data = std::str::from_utf8(input).map_err(|_| "invalid XDV pagesize operands")?;
|
||||
let mut words = data.split_ascii_whitespace().peekable();
|
||||
let sp_per_bp = 65536.0 * 803.0 / 800.0;
|
||||
let default_offset = (72.0 * sp_per_bp * 1000.0 / f64::from(magnification)).round() as i64;
|
||||
while let Some(word) = words.next() {
|
||||
let destination = match word {
|
||||
"width" => &mut self.size[0],
|
||||
"height" => &mut self.size[1],
|
||||
"xoffset" => &mut self.offset[0],
|
||||
"yoffset" => &mut self.offset[1],
|
||||
"default" => {
|
||||
if let Some(size) = document_size {
|
||||
self.size = size;
|
||||
}
|
||||
self.offset = [Some(default_offset); 2];
|
||||
continue;
|
||||
}
|
||||
word if word.as_bytes().first().is_some_and(u8::is_ascii_alphabetic) => continue,
|
||||
_ => break,
|
||||
};
|
||||
let bp = super::xdv_image::length(&mut words, magnification)
|
||||
.map_err(|_| "invalid XDV pagesize length")?;
|
||||
let value = (bp * sp_per_bp).round();
|
||||
let size = matches!(word, "width" | "height");
|
||||
if !(-1_073_741_823.0..=1_073_741_823.0).contains(&value) || (size && value <= 0.0) {
|
||||
return Err("XDV pagesize length out of range");
|
||||
}
|
||||
*destination = Some(value as i64);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -712,6 +712,29 @@ PDF、SVG 和逐页来源映射与独立全量相同。六项图像回归通过
|
||||
|
||||
回执 `target/p3-format-xdv-images/receipt.json` 的 SHA-256 为 `152fe604aefb4a47e52d41e556cb674e7e19e7388447f18338840898afc3495a`。
|
||||
|
||||
### XeTeX 纸张指令进入实际发页
|
||||
|
||||
XeTeX 每页隐式纸张设置与文档内的 `pdf:pagesize` 按发页顺序生效。
|
||||
首页建立默认纸张,后续设置只覆盖当页尺寸。纸张原语都为正时,隐式
|
||||
设置使用两个实际寄存器并保留驱动偏移。否则隐式 `default` 将偏移重置
|
||||
为 72 bp。显式设置支持单轴覆盖、`default`、x/y 偏移和带 `true` 的
|
||||
量。晚展开读取发页时的值,错误长度保留此前已消费的有效设置并警告。
|
||||
PDF、SVG、来源映射以及纸张指令前后的命名目标使用同一页设备几何。
|
||||
[TeX Live 的 XeTeX 发页入口](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/web2c/xetexdir/xetex.web#L14042-L14066)
|
||||
[TeX Live 的纸张扫描](https://github.com/TeX-Live/texlive-source/blob/fb6158926661cb7a7246b3a94a0cb170a9624d5a/texk/dvipdfm-x/dvi.c#L2489-L2521)
|
||||
|
||||
七个定向作业的 24 页加上原先失败的图像格式作业三页,纸张尺寸
|
||||
及 144 DPI 灰度光栅与 XeTeX 和 xdvipdfmx 一致。提前声明的命名
|
||||
目标坐标同为 25.99、211.03 bp。原有 schema84 格式在图像作业中
|
||||
直接重载。三项设备回归通过。项目会话在更改第二页纸张和偏移时
|
||||
安全续跑,预览只替换第二、三页。PDF、SVG 与逐页来源映射等于
|
||||
同版本的独立全量结果。
|
||||
|
||||
后续保留独立的 `papersize=…` 和 `landscape` 设备入口。原版探测的
|
||||
五页显示了全局基准修改和续页回落,原生仍拒绝 `papersize=…`。
|
||||
回执 `target/p3-xdv-pagesize/receipt.json` 的 SHA-256 为
|
||||
`72494e67dc133a1139cb8280a37547fc9fe4ffb832c6c40bfc8098bb4a7e8bc6`。
|
||||
|
||||
### PDF 预览按实际页面资源复用
|
||||
|
||||
编译会话的 SVG 缓存采用预览读取器解析出的页面、有效页框、旋转和资源。
|
||||
|
||||
@@ -136,3 +136,86 @@ fn illegal_or_incompatible_magnification_and_huge_pages_protect_existing_pdf() {
|
||||
}
|
||||
fs::remove_dir_all(p).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xdv_page_setup_edit_updates_the_local_sheet_and_inherited_origin() {
|
||||
use std::sync::Arc;
|
||||
use texrra_engine::native::{
|
||||
CompileRequest, Entry, NativeCompiler, ProjectDefinition, ProjectSnapshot,
|
||||
};
|
||||
use texrra_tex_core::{
|
||||
execution::{InputProfile, JobTime},
|
||||
resources::WorldConfig,
|
||||
};
|
||||
|
||||
let root = directory().canonicalize().unwrap();
|
||||
let entry = root.join("probe.tex");
|
||||
let sheet = root.join("sheet.tex");
|
||||
let prefix = r"\catcode123=1 \catcode125=2
|
||||
\pdfpagewidth=11182899sp \pdfpageheight=16445440sp
|
||||
\shipout\hbox{\special{pdf:pagesize width 170bp height 250bp xoffset 24bp}\vrule width10pt height4pt}
|
||||
";
|
||||
let padding = format!("{}\n", "\\relax ".repeat(32)).repeat(48);
|
||||
let tail = r"\input sheet.tex
|
||||
\shipout\hbox{\sheet\vrule width10pt height4pt}
|
||||
\shipout\hbox{\vrule width10pt height4pt}
|
||||
\shipout\hbox{\special{pdf:pagesize default}\vrule width10pt height4pt}
|
||||
\end
|
||||
";
|
||||
let from_sheet = |width, x| -> Arc<[u8]> {
|
||||
format!("\\def\\sheet{{\\special{{pdf:pagesize width {width}bp xoffset {x}bp}}}}\n")
|
||||
.into_bytes()
|
||||
.into()
|
||||
};
|
||||
let world = WorldConfig::new(&root);
|
||||
let snapshot = ProjectSnapshot::new(ProjectDefinition {
|
||||
configuration: world.clone(),
|
||||
revision: 1,
|
||||
roots: [root.clone()].into(),
|
||||
files: [
|
||||
(
|
||||
entry.clone(),
|
||||
format!("{prefix}{padding}{tail}").into_bytes().into(),
|
||||
),
|
||||
(sheet.clone(), from_sheet(190, 36)),
|
||||
]
|
||||
.into(),
|
||||
directories: Default::default(),
|
||||
aliases: Default::default(),
|
||||
generated_roots: [root.clone()].into(),
|
||||
generated: Default::default(),
|
||||
})
|
||||
.unwrap();
|
||||
let time = JobTime::new(2026, 11, 27, 0).unwrap();
|
||||
let mut request = CompileRequest::new(world, entry, "result.pdf", time);
|
||||
request.entry = Entry::Initex;
|
||||
request.input_profile = InputProfile::XeTeX;
|
||||
request.etex = true;
|
||||
let compiler = NativeCompiler::default().with_entry_checkpoints(prefix.len() + 60);
|
||||
let first = compiler
|
||||
.compile(request.clone().with_project(snapshot.clone()))
|
||||
.unwrap();
|
||||
let updated = snapshot
|
||||
.with_file_edits(2, [(sheet, from_sheet(205, 46))].into())
|
||||
.unwrap();
|
||||
let revised = request.with_project(updated);
|
||||
let next = compiler.compile(revised.clone()).unwrap();
|
||||
assert!(next.entry_checkpoint_work.reused_steps > 0);
|
||||
let widths: Vec<_> = next.pages.iter().map(|page| page.width).collect();
|
||||
assert_eq!(
|
||||
widths,
|
||||
[170, 205, 170, 170].map(|bp| (f64::from(bp) * 65_781.76).round() as i64)
|
||||
);
|
||||
let delta = next.preview_since(Some(first.revision())).unwrap();
|
||||
let changed: Vec<_> = delta.pages.iter().map(|page| page.index).collect();
|
||||
assert_eq!(changed, [1, 2]);
|
||||
|
||||
let full = NativeCompiler::default().compile(revised).unwrap();
|
||||
assert_eq!(next.pdf, full.pdf);
|
||||
assert_eq!(next.source_map.pages, full.source_map.pages);
|
||||
assert_eq!(
|
||||
next.preview_since(None).unwrap().pages,
|
||||
full.preview_since(None).unwrap().pages
|
||||
);
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user