Honor XeTeX page setup during native shipout

This commit is contained in:
gloamxun committed 2026-11-27 14:38:19 +08:00
1 parent 9da2ee41cd
commit d944552188
7 files changed
+281 -42

No files matched your search

@@ -13,6 +13,8 @@ pub(crate) struct Setup {
pub(crate) keep_destinations: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) outline_open_depth: Option<usize>,
#[serde(default, skip_serializing_if = "super::xdv_paper::Paper::is_default")]
pub(crate) paper: super::xdv_paper::Paper,
}
impl Setup {
@@ -0,0 +1,56 @@
//! XDV page setup is read in special order before the driver paints a page.
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub(crate) struct Paper {
/// Logical TeX sp. The first shipped page sets the document defaults.
pub(crate) size: [Option<i64>; 2],
/// Driver origin, independent of TeX's horizontal and vertical moves.
pub(crate) offset: [Option<i64>; 2],
}
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 read(
&mut self,
input: &[u8],
magnification: i32,
document_size: Option<[Option<i64>; 2]>,
) -> 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;
while let Some(word) = words.next() {
let destination = match word {
"width" => &mut self.size[0],
"height" => &mut self.size[1],
"xoffset" => &mut self.offset[0],
"yoffset" => &mut self.offset[1],
"default" => {
if let Some(size) = document_size {
self.size = size;
}
self.offset = [Some(default_offset); 2];
continue;
}
word if word.as_bytes().first().is_some_and(u8::is_ascii_alphabetic) => continue,
_ => break,
};
let bp = super::xdv_image::length(&mut words, magnification)
.map_err(|_| "invalid XDV pagesize length")?;
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");
}
*destination = Some(value as i64);
}
Ok(())
}
}