Honour declared paper size in route-D output

This commit is contained in:
gloamxun committed 2026-10-23 03:53:32 +08:00
1 parent 043ae7f072
commit 7b9d543e49
7 files changed
+142 -33

No files matched your search

+43 -18
View File
@@ -11,12 +11,10 @@ use std::sync::{Mutex, OnceLock};
use texrra_tex_core::cmr10::Cmr10Metrics;
use texrra_tex_core::math::parse_math;
use texrra_tex_core::pdf::{PageOrigin, display_lists_to_pdf_with_origin};
use texrra_tex_core::pdf::{PageOrigin, display_lists_to_pdf_with_paper};
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit, VerticalItem};
const SCALE: i64 = 65_536;
const PAGE_HEIGHT_PT: i64 = 792;
const PAGE_WIDTH_PT: i64 = 612;
thread_local! {
static ROUTE_D_SESSION: RefCell<Option<(Document, Engine)>> = const { RefCell::new(None) };
@@ -89,10 +87,13 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
Ok(json)
}
/// Pull `\hoffset`/`\voffset` assignments out of the source and return the
/// remaining body. Both commands take a TeX dimension.
fn strip_page_offsets(source: &str) -> (String, PageOrigin) {
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight` assignments out of
/// the source and return the remaining body. Each command takes a TeX
/// dimension.
fn strip_page_geometry(source: &str) -> (String, PageOrigin, Option<i64>, Option<i64>) {
let mut origin = PageOrigin::default();
let mut paper_width = None;
let mut paper_height = None;
let mut body = String::new();
for line in source.lines() {
let trimmed = line.trim();
@@ -100,12 +101,16 @@ fn strip_page_offsets(source: &str) -> (String, PageOrigin) {
origin.h_offset = value;
} else if let Some(value) = dimension_assignment(trimmed, "voffset") {
origin.v_offset = value;
} else if let Some(value) = dimension_assignment(trimmed, "paperwidth") {
paper_width = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "paperheight") {
paper_height = Some(value);
} else {
body.push_str(line);
body.push('\n');
}
}
(body, origin)
(body, origin, paper_width, paper_height)
}
fn dimension_assignment(line: &str, name: &str) -> Option<i64> {
@@ -135,8 +140,15 @@ fn parse_dimension(text: &str) -> Option<i64> {
/// Build a JSON preview from blank-line-separated plain-text paragraphs.
pub fn preview_json(source: &str) -> Result<String, String> {
let paragraphs = paragraphs(source)?;
let document = build_document(&paragraphs);
let (body, _origin, paper_width, paper_height) = strip_page_geometry(source);
let paragraphs = paragraphs(&body)?;
let mut document = build_document(&paragraphs);
if let Some(width) = paper_width {
document.paper_width = width;
}
if let Some(height) = paper_height {
document.paper_height = height;
}
let engine = Engine::layout(&document);
let json = render_json(&document, &engine, None);
// Keep the layout so a following `/edit` request can diff against it and
@@ -149,11 +161,17 @@ pub fn preview_json(source: &str) -> Result<String, String> {
/// Render the same checkpointed document as a route-D PDF.
pub fn pdf_bytes(source: &str) -> Result<Vec<u8>, String> {
let (body, origin) = strip_page_offsets(latex_body(source));
let (body, origin, paper_width, paper_height) = strip_page_geometry(latex_body(source));
let paragraphs = paragraphs(&body)?;
let mut document = build_document(&paragraphs);
document.h_offset = origin.h_offset;
document.v_offset = origin.v_offset;
if let Some(width) = paper_width {
document.paper_width = width;
}
if let Some(height) = paper_height {
document.paper_height = height;
}
let engine = Engine::layout(&document);
let pages: Vec<_> = engine
.pages()
@@ -161,14 +179,15 @@ pub fn pdf_bytes(source: &str) -> Result<Vec<u8>, String> {
.map(|page| {
let mut list =
page.display_list_with_layout(document.page_goal, document.text_float_sep);
list.width = list.width.max(PAGE_WIDTH_PT * SCALE);
list.height = list.height.max(PAGE_HEIGHT_PT * SCALE);
list.width = list.width.max(document.paper_width);
list.height = list.height.max(document.paper_height);
list
})
.collect();
Ok(display_lists_to_pdf_with_origin(
Ok(display_lists_to_pdf_with_paper(
&pages,
PAGE_HEIGHT_PT,
document.paper_width,
document.paper_height,
origin,
))
}
@@ -433,11 +452,17 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
fn render_json(document: &Document, engine: &Engine, edit: Option<(&[usize], usize)>) -> String {
let mut json = String::new();
json.push_str("{\"status\":\"ok\",\"pageWidthMm\":215.90,\"pageHeightMm\":279.40,\"pages\":[");
let page_width_mm = document.paper_width as f64 / SCALE as f64 * 25.4 / 72.0;
let page_height_mm = document.paper_height as f64 / SCALE as f64 * 25.4 / 72.0;
let page_height_pt = document.paper_height as f64 / SCALE as f64;
let _ = write!(
json,
"{{\"status\":\"ok\",\"pageWidthMm\":{page_width_mm:.2},\"pageHeightMm\":{page_height_mm:.2},\"pages\":["
);
for (index, page) in engine.pages().iter().enumerate() {
let mut list = page.display_list_with_layout(document.page_goal, document.text_float_sep);
list.width = list.width.max(PAGE_WIDTH_PT * SCALE);
list.height = list.height.max(PAGE_HEIGHT_PT * SCALE);
list.width = list.width.max(document.paper_width);
list.height = list.height.max(document.paper_height);
if index > 0 {
json.push(',');
}
@@ -456,7 +481,7 @@ fn render_json(document: &Document, engine: &Engine, edit: Option<(&[usize], usi
changed
);
}
json.push_str(",\"pageHeightPt\":792}");
let _ = write!(json, ",\"pageHeightPt\":{page_height_pt:.0}}}");
json
}