Resolve named XeTeX colors in native PDF and preview

This commit is contained in:
gloamxun committed 2026-11-28 23:45:56 +08:00
1 parent d6ab5a025f
commit 74eb1e9fc4
12 files changed
+723 -97

No files matched your search

@@ -38,6 +38,22 @@ pub(super) struct PdfSpaceProbe {
pub(super) observed_events: usize,
}
// PDF object and color specials need a settled paper size and one shared
// execution order, including live color across captures and graphics saves.
enum XdvOutput {
Color(usize, usize),
SaveColor,
RestoreColor(usize),
Navigation {
index: usize,
graphics_step: usize,
point: [i64; 2],
origin: Option<TokenOrigin>,
content_origin: Option<[i64; 2]>,
text: Option<crate::pdf_device::xdv_document::TextEncoding>,
},
}
struct ShipoutFontUse {
before_output: usize,
before_paint: usize,
@@ -429,9 +445,11 @@ impl Executor {
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()
let initial = self.xdv_colors.start_page();
if initial.as_ref().is_some_and(|program| !program.xdv_resources.is_empty()) {
self.reads.insert(Dependency::PdfCatalogState);
}
initial.into_iter()
.map(crate::pdf_device::ShippedColor::Page)
.collect()
} else if device.pdf.is_some() && !for_form {
@@ -551,7 +569,7 @@ impl Executor {
let mut xdv_annotations = std::collections::BTreeMap::new();
let mut reopened_images = false;
let mut xdv_paper_specials = Vec::new();
let mut xdv_navigation = Vec::new();
let mut xdv_output = Vec::new();
for (index, output) in outputs.iter().copied().enumerate() {
while font_uses.peek().is_some_and(|usage| usage.before_output == index) {
let usage = font_uses.next().unwrap();
@@ -618,16 +636,8 @@ impl Executor {
)?;
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() {
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);
}
if xdv && crate::pdf_device::xdv_color::ColorState::recognizes(&special.bytes) {
xdv_output.push(XdvOutput::Color(shipout.specials.len(), graphics_step));
None
} else if xdv && crate::pdf_device::xdv_annotation::Command::recognize(&special.bytes).is_some() {
xdv_annotations.insert(graphics_step, (shipout.specials.len(), self.xdv_text.clone()));
@@ -636,15 +646,19 @@ impl Executor {
xdv_paper_specials.push(shipout.specials.len());
None
} else if xdv && let Some((kind, _)) = super::specials::xdv_navigation_operand(&special.bytes) {
if kind == b"bxobj" && let Some(program) = self.xdv_colors.start_page() {
shipout.xdv_color_steps.insert(graphics_step, program);
}
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 text = matches!(kind, b"docview" | b"outline").then(|| self.xdv_text.clone());
xdv_navigation.push((shipout.specials.len(), graphics_step, point, *origin, content_origins.last().copied(), text));
xdv_output.push(XdvOutput::Navigation {
index: shipout.specials.len(),
graphics_step,
point,
origin: *origin,
content_origin: content_origins.last().copied(),
text,
});
None
} else if xdv && self.xdv_setup_special(&special)? {
None
@@ -664,11 +678,13 @@ impl Executor {
}
match &graphics {
Some(PdfGraphics::Save | PdfGraphics::Literal { mode: PdfLiteralMode::XdvBeginContent, .. }) => {
xdv_graphics_stack.push((special.span, xdv_transform, self.xdv_colors.current()));
xdv_graphics_stack.push((special.span, xdv_transform));
xdv_output.push(XdvOutput::SaveColor);
}
Some(operation @ (PdfGraphics::SaveMatrix(matrix) | PdfGraphics::Matrix(matrix))) => {
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
xdv_graphics_stack.push((special.span, xdv_transform, self.xdv_colors.current()));
xdv_graphics_stack.push((special.span, xdv_transform));
xdv_output.push(XdvOutput::SaveColor);
}
let [x, y] = special.position;
let pivot = matrix.about(
@@ -679,13 +695,11 @@ impl Executor {
xdv_transform = xdv_transform.concatenate(pivot);
}
Some(PdfGraphics::Restore { .. } | PdfGraphics::Literal { mode: PdfLiteralMode::XdvEndContent, .. }) => {
let Some((_, previous, color_at_save)) = xdv_graphics_stack.pop() else {
let Some((_, previous)) = 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);
}
xdv_output.push(XdvOutput::RestoreColor(graphics_step));
}
_ => {}
}
@@ -861,8 +875,48 @@ impl Executor {
origin_x - device.horizontal_origin,
device.page_height - origin_y - device.vertical_origin - shipout.box_node.height,
];
for (index, graphics_step, [x, y], origin, content_origin, text) in xdv_navigation {
let mut saved_colors = Vec::new();
for operation in xdv_output {
let (index, graphics_step, [x, y], origin, content_origin, text) = match operation {
XdvOutput::SaveColor => {
saved_colors.push(self.xdv_colors.current());
continue;
}
XdvOutput::RestoreColor(step) => {
let previous = saved_colors.pop().expect("matched XDV graphics restore");
if let Some(program) = self.xdv_colors.restore_after_save(previous) {
shipout.xdv_color_steps.insert(step, program);
}
continue;
}
XdvOutput::Color(index, step) => {
let special = &shipout.specials[index];
let color = self.xdv_colors.apply(&special.bytes, &mut self.state.xdv_navigation)
.expect("classified XDV 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 {
if !program.xdv_resources.is_empty() {
self.reads.insert(Dependency::PdfCatalogState);
}
shipout.xdv_color_steps.insert(step, program);
}
continue;
}
XdvOutput::Navigation { index, graphics_step, point, origin, content_origin, text } => {
(index, graphics_step, point, origin, content_origin, text)
}
};
let special = &shipout.specials[index];
if let Some((b"bxobj", _)) = super::specials::xdv_navigation_operand(&special.bytes)
&& let Some(program) = self.xdv_colors.start_page()
{
shipout.xdv_color_steps.insert(graphics_step, program);
}
let position = crate::pdf_device::xdv_object::Position {
page: self.shipped_pages + 1,
coordinates: [x + delta[0], y + delta[1]],
@@ -911,6 +965,9 @@ impl Executor {
}
if let Some(pdf) = &mut shipout.device.pdf && xdv {
shipout.pdf_background = self.xdv_colors.background();
if shipout.pdf_background.as_ref().is_some_and(|program| !program.xdv_resources.is_empty()) {
self.reads.insert(Dependency::PdfCatalogState);
}
self.state.xdv_setup.apply_version(&mut pdf.file);
}
self.current = current;