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

+49 -21
View File
@@ -624,17 +624,35 @@ fn write_type0_font(
/// The PDF stream can contain an 8-bit TeX literal. Keep its bytes alongside
/// the device origin used by subsequent positioned text, rules, and images.
#[derive(Default)]
struct PageContent {
bytes: Vec<u8>,
origin: crate::pdf_device::DeviceOrigin,
digits: u8,
measuring: bool,
track_legacy_cids: bool,
}
impl PageContent {
fn measuring() -> Self {
Self { measuring: true, ..Self::default() }
fn new(
units: crate::render::DisplayUnits,
digits: u8,
driver: crate::pdf_device::PdfDriver,
) -> Self {
Self {
bytes: Vec::new(),
origin: crate::pdf_device::DeviceOrigin::new(units, digits, driver),
digits,
measuring: false,
track_legacy_cids: false,
}
}
fn measuring(
units: crate::render::DisplayUnits,
digits: u8,
driver: crate::pdf_device::PdfDriver,
) -> Self {
Self { measuring: true, ..Self::new(units, digits, driver) }
}
fn push_str(&mut self, text: &str) {
@@ -649,14 +667,23 @@ impl PageContent {
}
}
fn move_origin(&mut self, point: [i64; 2], units: crate::render::DisplayUnits, digits: u8) {
let Some([h, v]) = self.origin.move_to(point, units, digits) else {
return;
};
fn move_origin(&mut self, point: [i64; 2]) {
if let Some(operands) = self.origin.move_to(point) {
self.write_translation(operands);
}
}
fn move_origin_temporarily(&mut self, point: [i64; 2]) {
if let Some(operands) = self.origin.temporary_translation(point) {
self.write_translation(operands);
}
}
fn write_translation(&mut self, [h, v]: [i64; 2]) {
self.push_str(&format!(
"1 0 0 1 {} {} cm\n",
crate::pdf_device::print_decimal(h, digits),
crate::pdf_device::print_decimal(v, digits),
crate::pdf_device::print_decimal(h, self.digits),
crate::pdf_device::print_decimal(v, self.digits),
));
}
}
@@ -755,9 +782,10 @@ fn page_content(
use crate::render::DisplayUnits;
let text_origin = xdv_page_origin(list.units, height, list.magnification);
let xdv = list.pdf.as_ref().is_some_and(|pdf| pdf.driver == PdfDriver::Xdv);
let driver = list.pdf.as_ref().map_or(PdfDriver::PdfTeX, |pdf| pdf.driver);
let xdv = driver == PdfDriver::Xdv;
let page_origin = form_origin.unwrap_or(if xdv { text_origin } else { [0, 0] });
let mut content = PageContent::default();
let mut content = PageContent::new(list.units, digits, driver);
content.push_str("q\n0 0 0 rg\n");
if list.magnification != 1000 && form_origin.is_none() {
let ratio = f64::from(list.magnification) / 1000.0;
@@ -789,9 +817,9 @@ fn page_content(
// Captured XDV content uses device-relative coordinates. Keep the
// logical origin used by glyphs and specials without emitting the
// enclosing sheet's translation into the Form stream.
content.origin.move_to(page_origin, list.units, digits);
content.origin.move_to(page_origin);
} else {
content.move_origin(page_origin, list.units, digits);
content.move_origin(page_origin);
}
let mut native_text: Option<NativeTextRun> = None;
let mut tex_text: Option<native_text::NativeText> = None;
@@ -844,7 +872,7 @@ fn page_content(
PdfLiteralMode::Page | PdfLiteralMode::Origin => {
native_text::NativeText::finish(&mut tex_text, &mut content);
if mode == PdfLiteralMode::Origin {
content.move_origin([x, height - y], list.units, digits);
content.move_origin([x, height - y]);
}
}
PdfLiteralMode::XdvBeginContent => {
@@ -852,7 +880,7 @@ fn page_content(
let point = [x, height - y];
xdv_content_scopes.push((previous_origin, point));
content.push_str("q\n");
content.move_origin(point, list.units, digits);
content.move_origin(point);
continue;
}
PdfLiteralMode::XdvEndContent => {
@@ -870,7 +898,7 @@ fn page_content(
content.push_str("q\n");
}
if let Some(point) = special_origin {
content.move_origin(point, list.units, digits);
content.move_origin(point);
}
}
}
@@ -880,7 +908,7 @@ fn page_content(
content.push_str("Q\n");
content.origin = previous_origin;
} else if special_origin.is_some() {
content.move_origin(previous_origin.position(), list.units, digits);
content.move_origin(previous_origin.position());
}
continue;
}
@@ -896,7 +924,7 @@ fn page_content(
{
assert!(paint.is_some(), "native glyph program must be resolved");
if xdv && tex_text.is_none() {
content.move_origin(page_origin, list.units, digits);
content.move_origin(page_origin);
}
let _ = native_text::NativeText::glyph(
&mut tex_text,
@@ -930,7 +958,7 @@ fn page_content(
);
} else {
native_text::rule(
&mut content, *x - h, *y + v, *width, *rule_height, height, digits,
&mut content, *x, *y, *width, *rule_height, height, digits,
);
}
continue;
@@ -1273,9 +1301,9 @@ fn page_content(
DisplayItem::PdfGraphics { x, y, operation } => {
let device_origin = list.units == DisplayUnits::TeX && !xdv;
if device_origin {
content.move_origin([*x, height - *y], list.units, digits);
content.move_origin([*x, height - *y]);
} else if xdv {
content.move_origin(page_origin, list.units, digits);
content.move_origin(page_origin);
}
match operation {
PdfGraphics::Save => content.push_str("q\n"),