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(())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user