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

+8
View File
@@ -615,6 +615,12 @@ pub struct Document {
/// TeX `\parskip`: vertical glue inserted between paragraphs.
#[serde(default)]
pub par_skip: i64,
/// Extra horizontal displacement of the page origin (`\hoffset`).
#[serde(default)]
pub h_offset: i64,
/// Extra vertical displacement of the page origin (`\voffset`).
#[serde(default)]
pub v_offset: i64,
}
fn default_text_fraction() -> u8 {
@@ -673,6 +679,8 @@ impl Document {
text_float_sep: default_text_float_sep(),
in_text_sep: default_in_text_sep(),
par_skip: 0,
h_offset: 0,
v_offset: 0,
}
}
+27 -2
View File
@@ -235,13 +235,28 @@ fn font_resource(font: FontFace) -> &'static str {
}
}
/// TeX page origin offsets (`\hoffset`, `\voffset`) in scaled points.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PageOrigin {
pub h_offset: i64,
pub v_offset: i64,
}
fn page_content(
list: &DisplayList,
height: i64,
image_names: &BTreeMap<usize, String>,
faces: &'static [PdfFace; 6],
origin: PageOrigin,
) -> String {
let mut content = String::from("q\n0 0 0 rg\n");
if origin != PageOrigin::default() {
content.push_str(&format!(
"1 0 0 1 {} {} cm\n",
pt(origin.h_offset),
pt(-origin.v_offset)
));
}
for item in &list.items {
match item {
DisplayItem::Text {
@@ -318,8 +333,18 @@ fn page_content(
content
}
/// Write several display lists as a multi-page PDF.
/// Write several display lists as a multi-page PDF at the default page origin.
pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8> {
display_lists_to_pdf_with_origin(pages, page_height, PageOrigin::default())
}
/// Write several display lists as a multi-page PDF, translating the page body
/// by TeX page origin offsets.
pub fn display_lists_to_pdf_with_origin(
pages: &[DisplayList],
page_height: i64,
origin: PageOrigin,
) -> Vec<u8> {
let height = page_height.max(792 * 65_536);
let width = pages
.iter()
@@ -473,7 +498,7 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec<u8>
for (index, slot) in &used {
names.insert(*index, format!("Im{slot}"));
}
let content = page_content(list, height, &names, faces);
let content = page_content(list, height, &names, faces, origin);
objects.push(
format!(
"<< /Length {} >>\nstream\n{}endstream",
+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
);
}