Shift the route-D page body by TeX page origin offsets

This commit is contained in:
gloamxun committed 2026-10-23 01:55:28 +08:00
1 parent fb33ac6327
commit 5db7acaff2
7 files changed
+148 -17

No files matched your search

+88 -2
View File
@@ -2,8 +2,9 @@
//! incremental paragraph edit.
use texrra_tex_core::{
Document, Engine, ParagraphFont, ParagraphStyle, Unit, cmr10::Cmr10Metrics,
pdf::display_lists_to_pdf,
Document, Engine, ParagraphFont, ParagraphStyle, Unit,
cmr10::Cmr10Metrics,
pdf::{PageOrigin, display_lists_to_pdf, display_lists_to_pdf_with_origin},
};
/// Paragraph style in TeX scaled points, driven by the CMR10 font metrics.
@@ -779,3 +780,88 @@ fn route_d_multiline_bands_track_pdftex() {
}
let _ = std::fs::remove_dir_all(&root);
}
/// TeX `\hoffset`/`\voffset` translate the page body without moving the sheet.
#[test]
fn page_origin_offsets_move_the_page_body() {
use std::path::Path;
use std::process::Command;
fn has(command: &str, version_arg: &str) -> bool {
Command::new(command)
.arg(version_arg)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
if !has("pdftotext", "-v") {
return;
}
fn word_xy(pdf: &Path, out: &Path) -> (f64, f64) {
let status = Command::new("pdftotext")
.arg("-bbox")
.arg(pdf)
.arg(out)
.status()
.expect("pdftotext");
assert!(status.success(), "pdftotext failed for {}", pdf.display());
let xml = std::fs::read_to_string(out).expect("read bbox xml");
let rest = &xml[xml.find("xMin=").expect("xMin")..];
let number = |key: &str| -> f64 {
let at = rest.find(key).expect(key) + key.len();
let tail = &rest[at..];
let end = tail.find('"').expect("quote");
tail[..end].parse().expect("bbox number")
};
(number("xMin=\""), number("yMin=\""))
}
let document = Document::new(
600 * 65_536,
vec![Unit::paragraph_with_style_and_face(
0,
"Origin",
body_style(),
ParagraphFont::Roman,
)],
);
let engine = Engine::layout(&document);
let mut list = engine.pages()[0].display_list();
list.width = list.width.max(612 * 65_536);
list.height = list.height.max(792 * 65_536);
let pages = std::slice::from_ref(&list);
let root = std::env::temp_dir().join(format!("texrra-origin-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("create origin dir");
let plain = root.join("plain.pdf");
let moved = root.join("moved.pdf");
std::fs::write(&plain, display_lists_to_pdf(pages, 792 * 65_536)).expect("write plain pdf");
std::fs::write(
&moved,
display_lists_to_pdf_with_origin(
pages,
792 * 65_536,
PageOrigin {
h_offset: 10 * 65_536,
v_offset: 20 * 65_536,
},
),
)
.expect("write moved pdf");
let (plain_x, plain_y) = word_xy(&plain, &root.join("plain.html"));
let (moved_x, moved_y) = word_xy(&moved, &root.join("moved.html"));
assert!(
(moved_x - plain_x - 10.0).abs() < 0.6,
"hoffset moved x by {}",
moved_x - plain_x
);
assert!(
(moved_y - plain_y - 20.0).abs() < 0.6,
"voffset moved y by {}",
moved_y - plain_y
);
}