Handle unclaimed XeTeX specials at shipout

This commit is contained in:
gloamxun committed 2026-11-28 06:04:09 +08:00
1 parent e2af618e65
commit 885911485e
10 files changed
+273 -21

No files matched your search

@@ -627,7 +627,11 @@ impl Executor {
_ => {}
}
shipout.specials.push(special);
shipout.pdf_graphics_steps.push(graphics);
// The XDV driver enters graphics mode for every special, even
// an empty special or one consumed without painting.
shipout.pdf_graphics_steps.push(graphics.or_else(|| {
xdv.then_some(PdfGraphics::TextBoundary)
}));
} else if output.is_pdf_graphics() {
if device.pdf.is_none() {
return self.fail("cannot ship a PDF graphics operation to DVI");
@@ -355,7 +355,7 @@ impl PdfLinkShipout {
previous.1 || *dx != 0 || *dy != 0);
}
}
Some(PdfGraphics::Literal { .. }) | None => {}
Some(PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary) | None => {}
}
}
LinkPaint::RowStart(row) if executor.pdf_running_links => {
@@ -141,12 +141,17 @@ impl Executor {
(PdfLiteralMode::Origin, body)
}
} else {
let data = data.trim_ascii_start();
if data.is_empty() {
let raw = data;
let data = xdv_skip_white(raw);
if data.is_empty()
|| [b"src:".as_slice(), b"pos:", b"om:"]
.iter()
.any(|prefix| data.starts_with(*prefix))
{
return Ok(None);
}
if let Some(graphics) = xdv_transform(data)
.map_err(|message| self.error(message, special.span))?
if let Some(graphics) =
xdv_transform(data).map_err(|message| self.error(message, special.span))?
{
return Ok(Some(graphics));
}
@@ -159,7 +164,8 @@ impl Executor {
return self.fail("native PDF information payload limit exceeded");
}
for warning in warnings {
files.observe_diagnostic(WriteDestination::TerminalAndLog, &warning)
files
.observe_diagnostic(WriteDestination::TerminalAndLog, &warning)
.map_err(|error| self.error(error, special.span))?;
}
self.reads.insert(Dependency::PdfInfoState);
@@ -167,17 +173,30 @@ impl Executor {
return Ok(None);
}
let Some(mut body) = body.and_then(|body| xdv_operand(body, b"literal")) else {
let name = data
.split(|b| b.is_ascii_whitespace())
.next()
.unwrap_or(data);
return Err(self.error(
format!(
"unsupported native XeTeX device special: {}",
String::from_utf8_lossy(name)
),
special.span,
));
if xdv_claims_special(raw, data) {
let name = data
.split(|b| b.is_ascii_whitespace())
.next()
.unwrap_or(data);
return Err(self.error(
format!(
"unsupported native XeTeX device special: {}",
String::from_utf8_lossy(name)
),
special.span,
));
}
// The raw whatsit stays in the shipout. An unclaimed special
// has a diagnostic and no PDF paint or resource effect.
let preview = String::from_utf8_lossy(&data[..data.len().min(50)]);
let ellipsis = if data.len() >= 50 { "..." } else { "" };
let warning = format!(
"xdvipdfmx:warning: Unparsed material at end of special ignored.\n\nCurrent input buffer is -->{preview}{ellipsis}<--\n"
);
files
.observe_diagnostic(WriteDestination::TerminalAndLog, &warning)
.map_err(|error| self.error(error, special.span))?;
return Ok(None);
};
let mut mode = PdfLiteralMode::XdvOrigin;
body = body.trim_ascii_start();
@@ -209,6 +228,80 @@ impl Executor {
}
}
fn xdv_is_space(byte: u8) -> bool {
matches!(byte, 0 | b'\t' | b'\n' | 12 | b'\r' | b' ')
}
fn xdv_skip_white(mut data: &[u8]) -> &[u8] {
loop {
data = &data[data.iter().take_while(|&&byte| xdv_is_space(byte)).count()..];
if data.first() != Some(&b'%') {
return data;
}
data = &data[data
.iter()
.position(|&byte| matches!(byte, b'\n' | b'\r'))
.unwrap_or(data.len())..];
}
}
fn xdv_identifier(data: &[u8]) -> &[u8] {
&data[..data
.iter()
.take_while(|byte| byte.is_ascii_alphanumeric() || **byte == b'_')
.count()]
}
// Keep an explicit failure for device protocols with a claiming handler
// whose operation has yet to be implemented. The prefixes and identifiers
// follow xdvipdfmx's PDF, XeTeX, dvips, color, tpic, HTML and compat probes.
fn xdv_claims_special(raw: &[u8], data: &[u8]) -> bool {
let prefixes: &[&[u8]] = &[
b"pdf:",
b"x:",
b"dvipdfmx:",
b"ps:",
b"PS:",
b"pst:",
b"PST:",
b"header",
b"PSfile",
b"psfile",
&[b'"', b' '],
b"postscriptbox",
b"pdfcolorstackinit",
b"pdfcolorstack",
b"pdffontattr",
b"landscape",
b"papersize",
];
let blank = &raw[raw.iter().take_while(|&&byte| xdv_is_space(byte)).count()..];
let html = &raw[raw
.iter()
.take_while(|byte| byte.is_ascii_whitespace() || **byte == 11)
.count()..];
let tpic = blank.strip_prefix(b"tpic:").unwrap_or(blank);
prefixes.iter().any(|prefix| data.starts_with(prefix))
|| html.starts_with(b"html:")
|| matches!(xdv_identifier(blank), b"color" | b"background")
|| matches!(
xdv_identifier(tpic),
b"pn"
| b"pa"
| b"fp"
| b"ip"
| b"da"
| b"dt"
| b"sp"
| b"ar"
| b"ia"
| b"sh"
| b"wh"
| b"bk"
| b"tx",
)
}
fn xdv_pdf_body(data: &[u8]) -> Option<&[u8]> {
data.trim_ascii_start().strip_prefix(b"pdf:").map(<[u8]>::trim_ascii_start)
}
+5 -1
View File
@@ -1201,6 +1201,8 @@ fn page_content(
content.push(b'\n');
}
DisplayItem::PdfColor { .. } => unreachable!("color handled before text"),
// The text runs above are closed without changing the graphics transform.
DisplayItem::PdfGraphics { operation: PdfGraphics::TextBoundary, .. } => {}
DisplayItem::PdfGraphics { x, y, operation } => {
let xdv = list.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv);
let device_origin = list.units == DisplayUnits::TeX && !xdv;
@@ -1238,7 +1240,9 @@ fn page_content(
.map(crate::pdf_image::real);
content.push_str(&format!("{a} {b} {c} {d} {e} {f} cm\n"));
}
PdfGraphics::Literal { .. } => unreachable!("literal handled before text"),
PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary => {
unreachable!("handled before graphics transforms")
}
}
}
}
@@ -37,6 +37,8 @@ pub enum PdfGraphics {
Restore { shift: [i64; 2] },
Literal { mode: PdfLiteralMode, bytes: Arc<[u8]> },
SaveMatrix(PdfMatrix),
/// An XDV special switched the device from text to graphics mode.
TextBoundary,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
+1
View File
@@ -812,6 +812,7 @@ impl DisplayList {
DisplayItem::PdfGraphics { x, y, operation } => {
use crate::pdf_device::PdfGraphics;
match operation {
PdfGraphics::TextBoundary => {}
PdfGraphics::Literal { .. } => unreachable!("PDF literal requires PDF rendering"),
PdfGraphics::Save | PdfGraphics::Matrix(_) | PdfGraphics::SaveMatrix(_) => {
if matches!(operation, PdfGraphics::Save | PdfGraphics::SaveMatrix(_)) {
+1 -1
View File
@@ -343,7 +343,7 @@ impl SourceMap {
continue;
}
match operation {
PdfGraphics::Literal { .. } => {}
PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary => {}
PdfGraphics::Save => graphics_stack.push(transform),
PdfGraphics::Matrix(matrix) | PdfGraphics::SaveMatrix(matrix) => {
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
@@ -153,6 +153,9 @@ impl PdfProjection {
return None;
}
self.text_active = false;
if matches!(operation, PdfGraphics::TextBoundary) {
return None;
}
let native = self.units == DisplayUnits::TeX && !self.xdv;
if native {
self.move_origin([x, y]);
@@ -174,7 +177,9 @@ impl PdfProjection {
let [x, y] = if native { [0, 0] } else { [x, y] };
self.concatenate(matrix.about(x as f64, y as f64, false));
}
PdfGraphics::Literal { .. } => unreachable!("handled before typed graphics"),
PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary => {
unreachable!("handled before graphics transforms")
}
}
return None;
}