Resolve XDV named destinations at PDF shipout
This commit is contained in:
1 parent
dd695a8945
commit
cc147e93d8
12 files changed
+456
-9
No files matched your search
@@ -276,9 +276,13 @@ impl Executor {
|
||||
InputProfile::TeX8Bit => PdfDriver::PdfTeX,
|
||||
InputProfile::UnicodeScalar | InputProfile::XeTeX => PdfDriver::Xdv,
|
||||
};
|
||||
let mut file = self.state.pdf_file_options.clone().unwrap();
|
||||
if driver == PdfDriver::Xdv {
|
||||
self.state.xdv_setup.apply_version(&mut file);
|
||||
}
|
||||
Ok(Some(PdfPageData {
|
||||
driver,
|
||||
file: self.state.pdf_file_options.clone().unwrap(),
|
||||
file,
|
||||
stream_compression: level.max(0) as u8,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -354,6 +354,7 @@ impl Executor {
|
||||
let device = &shipout.device;
|
||||
let mut graphics_stack = Vec::new();
|
||||
let mut xdv_graphics_stack = Vec::new();
|
||||
let mut xdv_transform = crate::pdf_device::Affine::IDENTITY;
|
||||
let (mut outputs, has_leaders) = b.output_nodes();
|
||||
let leader_effects = if has_leaders {
|
||||
let limit = self
|
||||
@@ -437,8 +438,16 @@ 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 = if let Some(pdf) = &device.pdf {
|
||||
if pdf.driver == crate::pdf_device::PdfDriver::Xdv
|
||||
&& self.xdv_destination_special(&special, point, &mut shipout.pdf_links)?
|
||||
{
|
||||
None
|
||||
} else if pdf.driver == crate::pdf_device::PdfDriver::Xdv
|
||||
&& let Some(image) = crate::pdf_device::xdv_image::ImageSpecial::parse(
|
||||
&special.bytes, device.magnification,
|
||||
).map_err(|error| self.error(error, special.span))?
|
||||
@@ -461,11 +470,26 @@ impl Executor {
|
||||
};
|
||||
use crate::pdf_device::PdfGraphics;
|
||||
match &graphics {
|
||||
Some(PdfGraphics::Save | PdfGraphics::SaveMatrix(_)) => {
|
||||
xdv_graphics_stack.push(special.span);
|
||||
Some(PdfGraphics::Save) => {
|
||||
xdv_graphics_stack.push((special.span, xdv_transform));
|
||||
}
|
||||
Some(PdfGraphics::Restore { .. }) if xdv_graphics_stack.pop().is_none() => {
|
||||
return Err(self.error("unmatched XDV graphics restore", special.span));
|
||||
Some(operation @ (PdfGraphics::SaveMatrix(matrix) | PdfGraphics::Matrix(matrix))) => {
|
||||
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
|
||||
xdv_graphics_stack.push((special.span, xdv_transform));
|
||||
}
|
||||
let [x, y] = special.position;
|
||||
let pivot = matrix.about(
|
||||
(x + horizontal_origin) as f64,
|
||||
(y + vertical_origin + b.height) as f64,
|
||||
true,
|
||||
);
|
||||
xdv_transform = xdv_transform.concatenate(pivot);
|
||||
}
|
||||
Some(PdfGraphics::Restore { .. }) => {
|
||||
let Some((_, previous)) = xdv_graphics_stack.pop() else {
|
||||
return Err(self.error("unmatched XDV graphics restore", special.span));
|
||||
};
|
||||
xdv_transform = previous;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -541,7 +565,7 @@ impl Executor {
|
||||
if output_positions.next().is_some() {
|
||||
return self.fail("unexpected positioned native output node");
|
||||
}
|
||||
if let Some(&span) = xdv_graphics_stack.last() {
|
||||
if let Some(&(span, _)) = xdv_graphics_stack.last() {
|
||||
return Err(self.error("unclosed XDV graphics save at page shipout", span));
|
||||
}
|
||||
if let Some((_, span)) = graphics_stack.last() {
|
||||
@@ -575,9 +599,16 @@ impl Executor {
|
||||
self.write_pdf_object(number, files)?;
|
||||
}
|
||||
if has_link_material {
|
||||
shipout.pdf_links = self.ship_pdf_link_material(
|
||||
let mut links = self.ship_pdf_link_material(
|
||||
b, device, &shipout.pdf_graphics_steps, files,
|
||||
)?;
|
||||
links.xdv_destinations = std::mem::take(&mut shipout.pdf_links.xdv_destinations);
|
||||
shipout.pdf_links = links;
|
||||
}
|
||||
if let Some(pdf) = &mut shipout.device.pdf
|
||||
&& pdf.driver == crate::pdf_device::PdfDriver::Xdv
|
||||
{
|
||||
self.state.xdv_setup.apply_version(&mut pdf.file);
|
||||
}
|
||||
self.current = current;
|
||||
self.native_page.as_mut().unwrap().dead_cycles = 0;
|
||||
|
||||
@@ -49,6 +49,42 @@ impl Executor {
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn xdv_destination_special(
|
||||
&mut self,
|
||||
special: &ShipoutSpecial,
|
||||
point: [i64; 2],
|
||||
links: &mut crate::pdf_device::links::PageLinks,
|
||||
) -> 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, self.shipped_pages + 1, point)
|
||||
.map_err(|error| self.error(error, special.span))?;
|
||||
links.xdv_destinations.push(destination);
|
||||
return Ok(true);
|
||||
}
|
||||
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 {
|
||||
self.state.xdv_setup.set_version(major, operands)
|
||||
.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")
|
||||
{
|
||||
if self.shipped_pages == 0 {
|
||||
self.state.xdv_setup.configure(operands)
|
||||
.map_err(|error| self.error(error, special.span))?;
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
pub(super) fn render_special(
|
||||
&mut self,
|
||||
special: &ShipoutSpecial,
|
||||
|
||||
@@ -748,6 +748,8 @@ pub struct State {
|
||||
pub(super) magnification_set: Option<i32>,
|
||||
pub(super) output_mode_set: Option<i32>,
|
||||
pub(super) pdf_file_options: Option<crate::pdf_device::PdfFileOptions>,
|
||||
#[serde(default, skip_serializing_if = "crate::pdf_device::xdv_destination::Setup::is_default")]
|
||||
pub(super) xdv_setup: crate::pdf_device::xdv_destination::Setup,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) pdf_catalog: Option<Box<crate::pdf_device::catalog::PdfCatalog>>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
@@ -946,6 +948,7 @@ impl Default for State {
|
||||
magnification_set: None,
|
||||
output_mode_set: None,
|
||||
pdf_file_options: None,
|
||||
xdv_setup: Default::default(),
|
||||
pdf_catalog: None,
|
||||
pdf_objects: None,
|
||||
initial_random_seed: None,
|
||||
@@ -1583,6 +1586,7 @@ impl State {
|
||||
self.magnification_set = None;
|
||||
self.output_mode_set = None;
|
||||
self.pdf_file_options = None;
|
||||
self.xdv_setup = Default::default();
|
||||
self.pdf_catalog = None;
|
||||
if !self
|
||||
.pdf_objects
|
||||
@@ -1634,6 +1638,7 @@ impl State {
|
||||
glyph_unicode: &self.glyph_unicode,
|
||||
fonts: &self.fonts,
|
||||
page_tree_attributes: &self.pdf_pages_attributes,
|
||||
xdv_keep_destinations: self.xdv_setup.keep_destinations,
|
||||
catalog: self.pdf_catalog.as_deref(),
|
||||
objects: self.pdf_objects.as_deref(),
|
||||
info: crate::pdf_device::PdfInfo {
|
||||
|
||||
Reference in new issue
Block a user