Handle legacy XeTeX paper and landscape specials

This commit is contained in:
gloamxun committed 2026-11-27 14:47:39 +08:00
1 parent d944552188
commit 7ff988031f
5 files changed
+274 -55

No files matched your search

@@ -7,25 +7,137 @@ pub(crate) struct Paper {
pub(crate) size: [Option<i64>; 2],
/// Driver origin, independent of TeX's horizontal and vertical moves.
pub(crate) offset: [Option<i64>; 2],
#[serde(default, skip_serializing_if = "is_false")]
pub(crate) landscape: bool,
}
fn is_false(value: &bool) -> bool {
!value
}
#[derive(Clone, Copy)]
pub(crate) enum Special<'a> {
PageSize(&'a [u8]),
PaperSize(&'a [u8]),
Landscape,
}
impl<'a> Special<'a> {
pub(crate) fn parse(data: &'a [u8]) -> Option<Self> {
let (name, rest) = identifier(data);
let mut pdf = false;
let (name, rest) = if matches!(name, b"pdf" | b"x" | b"dvipdfmx")
&& let Some(body) = rest.trim_ascii_start().strip_prefix(b":")
{
pdf = name == b"pdf";
identifier(body)
} else {
(name, rest)
};
match name {
b"pagesize" if pdf => Some(Self::PageSize(rest)),
b"papersize" => Some(Self::PaperSize(rest)),
b"landscape" => Some(Self::Landscape),
_ => None,
}
}
}
fn identifier(input: &[u8]) -> (&[u8], &[u8]) {
let input = input.trim_ascii_start();
let end = input
.iter()
.position(|byte| !(byte.is_ascii_alphanumeric() || *byte == b'_'))
.unwrap_or(input.len());
input.split_at(end)
}
const SP_PER_BP: f64 = 65536.0 * 803.0 / 800.0;
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 start_page(&mut self, requested: [i64; 2]) {
if requested.into_iter().all(|length| length > 0) {
self.size = requested.map(Some);
} else {
// XeTeX writes an implicit "pdf:pagesize default" before the
// box. Keep the configured native origin for this implicit reset.
self.offset = [None; 2];
}
}
/// During the pre-scan, the first page is also the document default.
/// Later scans keep a separate current sheet and mutable document paper.
pub(crate) fn read(
&mut self,
special: Special<'_>,
magnification: i32,
document: Option<&mut Paper>,
) -> Result<(), &'static str> {
match special {
Special::PageSize(input) => self.read_pagesize(input, magnification, document),
Special::PaperSize(input) => self.read_papersize(input, magnification, document),
Special::Landscape => {
self.landscape = true;
Ok(())
}
}
}
fn read_papersize(
&mut self,
input: &[u8],
magnification: i32,
document_size: Option<[Option<i64>; 2]>,
document: Option<&mut Paper>,
) -> Result<(), &'static str> {
let input = std::str::from_utf8(input).map_err(|_| "invalid XDV papersize operands")?;
let input = input.trim_ascii_start();
let input = input.strip_prefix('=').unwrap_or(input).trim_ascii_start();
let (lengths, closed) = match input.chars().next() {
Some(quote @ ('\'' | '"')) => (&input[1..])
.split_once(quote)
.map_or((&input[1..], false), |(lengths, _)| (lengths, true)),
_ => (input, true),
};
let lengths = lengths.replace(',', " , ");
let mut words = lengths.split_ascii_whitespace().peekable();
let width = super::xdv_image::length(&mut words, 1000)
.map_err(|_| "invalid XDV papersize width")?;
if words.peek() == Some(&",") {
words.next();
}
let height = super::xdv_image::length(&mut words, 1000)
.map_err(|_| "invalid XDV papersize height")?;
let unit = SP_PER_BP * 1000.0 / f64::from(magnification);
let size = [width, height].map(|bp| (bp * unit).round());
if !size
.into_iter()
.all(|length| (1.0..=1_073_741_823.0).contains(&length))
{
return Err("XDV papersize length out of range");
}
self.size = size.map(|length| Some(length as i64));
if !closed || (input.starts_with(['\'', '"']) && words.next().is_some()) {
return Err("unclosed XDV papersize quote");
}
if let Some(document) = document {
document.size = self.size;
}
Ok(())
}
fn read_pagesize(
&mut self,
input: &[u8],
magnification: i32,
document: Option<&mut Paper>,
) -> 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;
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],
@@ -33,8 +145,9 @@ impl Paper {
"xoffset" => &mut self.offset[0],
"yoffset" => &mut self.offset[1],
"default" => {
if let Some(size) = document_size {
self.size = size;
if let Some(document) = document.as_deref() {
self.size = document.size;
self.landscape = document.landscape;
}
self.offset = [Some(default_offset); 2];
continue;
@@ -44,7 +157,7 @@ impl Paper {
};
let bp = super::xdv_image::length(&mut words, magnification)
.map_err(|_| "invalid XDV pagesize length")?;
let value = (bp * sp_per_bp).round();
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");