Apply XDV color state and page backgrounds at shipout
This commit is contained in:
1 parent
cc147e93d8
commit
8be7867b37
10 files changed
+590
-19
No files matched your search
@@ -41,6 +41,10 @@ pub struct NativeShipout {
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub(super) pdf_graphics_steps: Vec<Option<PdfGraphics>>,
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
pub(super) xdv_color_steps: BTreeMap<usize, Arc<PdfColorProgram>>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) pdf_background: Option<Arc<PdfColorProgram>>,
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
pub(super) pdf_special_images: BTreeMap<usize, (Arc<crate::pdf_image::PdfImageUse>, TokenOrigin)>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub specials: Vec<ShipoutSpecial>,
|
||||
@@ -77,6 +81,7 @@ impl NativeShipout {
|
||||
pdf: self.device.pdf.clone(),
|
||||
pdf_font_bindings: self.pdf_font_bindings.clone(),
|
||||
pdf_links: self.pdf_links.clone(),
|
||||
pdf_background: self.pdf_background.clone(),
|
||||
pdf_text_spaces: self.pdf_text_spaces.clone(),
|
||||
pdf_resources: crate::pdf_device::page_resources::PageResources {
|
||||
entries: self.pdf_resource_entries.clone(),
|
||||
@@ -130,12 +135,17 @@ impl NativeShipout {
|
||||
}
|
||||
boxes::GeometryItem::PdfGraphics { x, y } => {
|
||||
match graphics_steps.next() {
|
||||
Some((_, Some(operation))) => page.items.push(DisplayItem::PdfGraphics {
|
||||
x: self.device.horizontal_origin + x,
|
||||
y: self.device.vertical_origin + self.box_node.height + y,
|
||||
operation: operation.clone(),
|
||||
}),
|
||||
Some((step, None)) => {
|
||||
Some((step, operation)) => {
|
||||
if let Some(operation) = operation {
|
||||
page.items.push(DisplayItem::PdfGraphics {
|
||||
x: self.device.horizontal_origin + x,
|
||||
y: self.device.vertical_origin + self.box_node.height + y,
|
||||
operation: operation.clone(),
|
||||
});
|
||||
}
|
||||
if let Some(program) = self.xdv_color_steps.get(&step) {
|
||||
page.items.push(DisplayItem::PdfColor(program.clone()));
|
||||
}
|
||||
if let Some((image, origin)) = self.pdf_special_images.get(&step) {
|
||||
let [left, bottom] = image.xdv.as_ref().unwrap().layout_offset;
|
||||
page.sources.insert(
|
||||
|
||||
@@ -225,6 +225,7 @@ pub struct Executor {
|
||||
expansion_depth: usize,
|
||||
io: IoState,
|
||||
pdf_colors: crate::pdf_device::PdfColorStacks,
|
||||
xdv_colors: crate::pdf_device::xdv_color::ColorState,
|
||||
pdf_link_stack: Vec<pdf_links::ActiveLink>,
|
||||
pdf_running_links: bool,
|
||||
pdf_interword_spaces: bool,
|
||||
@@ -399,6 +400,7 @@ impl Executor {
|
||||
expansion_depth: 0,
|
||||
io: IoState::default(),
|
||||
pdf_colors: Default::default(),
|
||||
xdv_colors: Default::default(),
|
||||
pdf_link_stack: Vec::new(),
|
||||
pdf_running_links: true,
|
||||
pdf_interword_spaces: false,
|
||||
|
||||
@@ -323,7 +323,11 @@ impl Executor {
|
||||
} else {
|
||||
(Default::default(), Default::default())
|
||||
};
|
||||
let pdf_color_start = if device.pdf.is_some() {
|
||||
use crate::pdf_device::PdfDriver;
|
||||
let xdv = device.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv);
|
||||
let pdf_color_start = if xdv {
|
||||
self.xdv_colors.start_page().into_iter().collect()
|
||||
} else if device.pdf.is_some() {
|
||||
self.pdf_colors.start_page()
|
||||
} else {
|
||||
Vec::new()
|
||||
@@ -343,6 +347,8 @@ impl Executor {
|
||||
pdf_color_start,
|
||||
pdf_color_steps: Vec::new(),
|
||||
pdf_graphics_steps: Vec::new(),
|
||||
xdv_color_steps: Default::default(),
|
||||
pdf_background: None,
|
||||
pdf_special_images: Default::default(),
|
||||
specials: Vec::new(),
|
||||
pdf_text_spaces: None,
|
||||
@@ -442,13 +448,22 @@ impl Executor {
|
||||
(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 pdf.driver == crate::pdf_device::PdfDriver::Xdv
|
||||
&& self.xdv_destination_special(&special, point, &mut shipout.pdf_links)?
|
||||
{
|
||||
if let Some(color) = xdv.then(|| self.xdv_colors.apply(&special.bytes)).flatten() {
|
||||
let color = color.map_err(|error| self.error(error, special.span))?;
|
||||
if let Some(warning) = color.warning {
|
||||
files.observe_diagnostic(
|
||||
WriteDestination::TerminalAndLog, &format!("xdvipdfmx: warning: {warning}\n"),
|
||||
).map_err(|error| self.error(error, special.span))?;
|
||||
}
|
||||
if let Some(program) = color.program {
|
||||
shipout.xdv_color_steps.insert(graphics_step, program);
|
||||
}
|
||||
None
|
||||
} else if pdf.driver == crate::pdf_device::PdfDriver::Xdv
|
||||
&& let Some(image) = crate::pdf_device::xdv_image::ImageSpecial::parse(
|
||||
} else if xdv && self.xdv_destination_special(&special, point, &mut shipout.pdf_links)? {
|
||||
None
|
||||
} else if xdv && let Some(image) = crate::pdf_device::xdv_image::ImageSpecial::parse(
|
||||
&special.bytes, device.magnification,
|
||||
).map_err(|error| self.error(error, special.span))?
|
||||
{
|
||||
@@ -471,11 +486,11 @@ impl Executor {
|
||||
use crate::pdf_device::PdfGraphics;
|
||||
match &graphics {
|
||||
Some(PdfGraphics::Save) => {
|
||||
xdv_graphics_stack.push((special.span, xdv_transform));
|
||||
xdv_graphics_stack.push((special.span, xdv_transform, self.xdv_colors.current()));
|
||||
}
|
||||
Some(operation @ (PdfGraphics::SaveMatrix(matrix) | PdfGraphics::Matrix(matrix))) => {
|
||||
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
|
||||
xdv_graphics_stack.push((special.span, xdv_transform));
|
||||
xdv_graphics_stack.push((special.span, xdv_transform, self.xdv_colors.current()));
|
||||
}
|
||||
let [x, y] = special.position;
|
||||
let pivot = matrix.about(
|
||||
@@ -486,10 +501,13 @@ impl Executor {
|
||||
xdv_transform = xdv_transform.concatenate(pivot);
|
||||
}
|
||||
Some(PdfGraphics::Restore { .. }) => {
|
||||
let Some((_, previous)) = xdv_graphics_stack.pop() else {
|
||||
let Some((_, previous, color_at_save)) = xdv_graphics_stack.pop() else {
|
||||
return Err(self.error("unmatched XDV graphics restore", special.span));
|
||||
};
|
||||
xdv_transform = previous;
|
||||
if let Some(program) = self.xdv_colors.restore_after_save(color_at_save) {
|
||||
shipout.xdv_color_steps.insert(graphics_step, program);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -565,7 +583,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() {
|
||||
@@ -605,9 +623,8 @@ impl Executor {
|
||||
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
|
||||
{
|
||||
if let Some(pdf) = &mut shipout.device.pdf && xdv {
|
||||
shipout.pdf_background = self.xdv_colors.background();
|
||||
self.state.xdv_setup.apply_version(&mut pdf.file);
|
||||
}
|
||||
self.current = current;
|
||||
|
||||
Reference in new issue
Block a user