Scope XDV PDF content at the driver page origin

This commit is contained in:
gloamxun committed 2026-11-28 07:25:57 +08:00
1 parent ab6b1093ec
commit 526ec9b3b0
9 files changed
+176 -46

No files matched your search

@@ -1,7 +1,8 @@
//! Project TeX paint through the PDF device and any literal graphics state.
use crate::{
pdf_device::{
Affine, DeviceOrigin, PdfDriver, PdfGraphics, PdfLiteralMode, page_resources::check_pdf_nesting,
Affine, DeviceOrigin, PdfDriver, PdfGraphics, PdfLiteralMode,
page_resources::check_pdf_nesting, xdv_page_origin,
},
render::{DisplayItem, DisplayList, DisplayUnits},
};
@@ -18,9 +19,8 @@ pub(super) struct PdfProjection {
graphics: Option<GraphicsState>,
stack: Vec<GraphicsState>,
units: DisplayUnits,
xdv: bool,
xdv_origin: Option<[i64; 2]>,
height: i64,
magnification: i32,
digits: u8,
units_per_bp: f64,
text_active: bool,
@@ -44,19 +44,23 @@ impl PdfProjection {
matrix: Affine::IDENTITY,
modified_text_parameters: false,
};
Some(Self {
let xdv_origin = page.pdf.as_ref()
.filter(|pdf| pdf.driver == PdfDriver::Xdv)
.map(|_| xdv_page_origin(page.units, page.height, page.magnification));
let mut projection = Self {
origin: DeviceOrigin::default(),
graphics: Some(graphics),
stack: vec![graphics], // The page writer begins with q.
units: page.units,
xdv: page.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv),
xdv_origin,
height: page.height,
magnification: page.magnification,
digits: page.pdf.as_ref().map_or(3, |page| page.file.decimal_digits),
units_per_bp: 1.0 / page.units.bp(1),
text_active: false,
modified_text_position: false,
})
};
projection.move_origin(xdv_origin.unwrap_or([0, 0]));
Some(projection)
}
fn concatenate(&mut self, matrix: Affine) {
@@ -136,8 +140,11 @@ impl PdfProjection {
if *mode != PdfLiteralMode::Direct {
self.text_active = false;
}
let previous_origin = self.origin.position();
let special_origin = mode.xdv_origin([x, y], self.height, self.magnification);
let previous_origin = self.origin;
if *mode == PdfLiteralMode::XdvContent {
self.save();
}
let special_origin = mode.xdv_origin([x, y], self.xdv_origin.unwrap_or([0, 0]));
if *mode == PdfLiteralMode::Origin {
self.move_origin([x, y]);
} else if let Some(point) = special_origin {
@@ -147,8 +154,11 @@ impl PdfProjection {
self.graphics = None;
self.stack.clear();
}
if special_origin.is_some() {
self.move_origin(previous_origin);
if *mode == PdfLiteralMode::XdvContent {
self.restore();
self.origin = previous_origin;
} else if special_origin.is_some() {
self.move_origin(previous_origin.position());
}
return None;
}
@@ -156,11 +166,11 @@ impl PdfProjection {
if matches!(operation, PdfGraphics::TextBoundary) {
return None;
}
let native = self.units == DisplayUnits::TeX && !self.xdv;
let native = self.units == DisplayUnits::TeX && self.xdv_origin.is_none();
if native {
self.move_origin([x, y]);
} else if self.xdv {
self.move_origin([0, 0]);
} else if let Some(origin) = self.xdv_origin {
self.move_origin(origin);
}
match operation {
PdfGraphics::Save => self.save(),
@@ -174,7 +184,12 @@ impl PdfProjection {
if matches!(operation, PdfGraphics::SaveMatrix(_)) {
self.save();
}
let [x, y] = if native { [0, 0] } else { [x, y] };
let [x, y] = if native {
[0, 0]
} else {
let [h, v] = self.origin.position();
[x - h, y - v]
};
self.concatenate(matrix.about(x as f64, y as f64, false));
}
PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary => {
@@ -187,7 +202,7 @@ impl PdfProjection {
if *operation == crate::pdf_device::text_spaces::SpaceOperation::Explicit
&& self.units == DisplayUnits::TeX && !self.text_active
{
self.move_origin([0, 0]);
self.move_origin(self.xdv_origin.unwrap_or([0, 0]));
self.text_active = true;
self.modified_text_position = false;
}
@@ -195,7 +210,7 @@ impl PdfProjection {
}
DisplayItem::NativeGlyph { .. } if self.units == DisplayUnits::TeX => {
if !self.text_active {
self.move_origin([0, 0]);
self.move_origin(self.xdv_origin.unwrap_or([0, 0]));
self.text_active = true;
self.modified_text_position = false;
}