Match pdfTeX graphics-origin movement thresholds

This commit is contained in:
gloamxun committed 2026-11-29 10:32:34 +08:00
1 parent ecb0f60960
commit 330c4bde51
7 files changed
+195 -75

No files matched your search

@@ -1,5 +1,6 @@
//! Graphics state produced by PDF extension whatsits and device specials.
use super::PdfDriver;
use crate::render::DisplayUnits;
use std::sync::Arc;
@@ -97,38 +98,54 @@ impl PdfMatrix {
}
}
/// Native device origin in logical scaled units, with the same rounding as
/// the emitted PDF translation. The returned operands have \`digits\` decimals.
#[derive(Clone, Copy, Default)]
pub(crate) struct DeviceOrigin([i64; 2]);
/// Tracks the logical position represented by the PDF device's translations.
#[derive(Clone, Copy)]
pub(crate) struct DeviceOrigin {
position: [i64; 2],
scale: i64,
precision: u8,
minimum_move: i64,
}
impl DeviceOrigin {
pub(crate) fn position(&self) -> [i64; 2] {
self.0
}
pub(crate) fn move_to(
&mut self,
point: [i64; 2],
units: DisplayUnits,
digits: u8,
) -> Option<[i64; 2]> {
let delta = [point[0] - self.0[0], point[1] - self.0[1]];
if delta == [0, 0] {
return None;
}
pub(crate) fn new(units: DisplayUnits, digits: u8, driver: PdfDriver) -> Self {
let (scale, precision) = match units {
DisplayUnits::TeX => (6_578_176, digits + 2),
DisplayUnits::PostScript => (65_536, digits),
};
let [(h, h_sp), (v, v_sp)] =
delta.map(|value| super::divide_scaled(value, scale, precision));
if h == 0 && v == 0 {
// pdfTeX decides whether to move before rounding either operand.
// If one axis qualifies, the device rounds and emits both axes.
let minimum_move = if units == DisplayUnits::TeX && driver == PdfDriver::PdfTeX {
super::divide_scaled(scale, 10i64.pow(u32::from(precision)), 0).0
} else {
0
};
Self { position: [0, 0], scale, precision, minimum_move }
}
pub(crate) fn position(&self) -> [i64; 2] {
self.position
}
pub(crate) fn move_to(&mut self, point: [i64; 2]) -> Option<[i64; 2]> {
let (operands, advance) = self.translation(point)?;
self.position[0] += advance[0];
self.position[1] += advance[1];
Some(operands)
}
pub(crate) fn temporary_translation(&self, point: [i64; 2]) -> Option<[i64; 2]> {
self.translation(point).map(|(operands, _)| operands)
}
fn translation(&self, point: [i64; 2]) -> Option<([i64; 2], [i64; 2])> {
let delta = [point[0] - self.position[0], point[1] - self.position[1]];
if delta == [0, 0] || delta.iter().all(|d| d.abs() < self.minimum_move) {
return None;
}
self.0[0] += h_sp;
self.0[1] += v_sp;
Some([h, v])
let [(h, h_sp), (v, v_sp)] =
delta.map(|value| super::divide_scaled(value, self.scale, self.precision));
(h != 0 || v != 0).then_some(([h, v], [h_sp, v_sp]))
}
}