Read paragraph registers from route-D sources and measure with TFM advances

This commit is contained in:
gloamxun committed 2026-10-23 13:28:43 +08:00
1 parent be23fe9686
commit 4f4171a6d1
4 files changed
+105 -23

No files matched your search

+64 -20
View File
@@ -12,7 +12,7 @@ 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_paper};
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit, VerticalItem};
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit};
const SCALE: i64 = 65_536;
@@ -45,7 +45,7 @@ fn latex_body(source: &str) -> &str {
pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, String> {
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs(&body)?;
let mut document = build_document(&paragraphs);
let mut document = build_document(&paragraphs, &geometry);
apply_geometry(&mut document, &geometry);
let mut sessions = project_sessions()
.lock()
@@ -107,6 +107,14 @@ struct PageGeometry {
paper_height: Option<i64>,
vsize: Option<i64>,
textheight: Option<i64>,
par_indent: Option<i64>,
par_skip: Option<i64>,
baseline_skip: Option<i64>,
left_skip: Option<i64>,
right_skip: Option<i64>,
tolerance: Option<i64>,
pretolerance: Option<i64>,
emergency_stretch: Option<i64>,
}
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight`/`\vsize`/
@@ -130,6 +138,22 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.vsize = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "textheight") {
geometry.textheight = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "parindent") {
geometry.par_indent = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "parskip") {
geometry.par_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "baselineskip") {
geometry.baseline_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "leftskip") {
geometry.left_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "rightskip") {
geometry.right_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "emergencystretch") {
geometry.emergency_stretch = Some(value);
} else if let Some(value) = integer_assignment(trimmed, "tolerance") {
geometry.tolerance = Some(value);
} else if let Some(value) = integer_assignment(trimmed, "pretolerance") {
geometry.pretolerance = Some(value);
} else {
body.push_str(line);
body.push('\n');
@@ -157,6 +181,20 @@ fn apply_geometry(document: &mut Document, geometry: &PageGeometry) {
if let Some(height) = geometry.textheight.or(geometry.vsize) {
document.page_goal = height;
}
if let Some(skip) = geometry.par_skip {
document.par_skip = skip;
}
}
/// Read a plain integer assignment such as `\tolerance=1000`.
fn integer_assignment(line: &str, name: &str) -> Option<i64> {
let rest = line.strip_prefix('\\')?;
let rest = rest.strip_prefix(name)?;
rest.trim_start().strip_prefix('=')?;
rest.trim()
.parse::<i64>()
.ok()
.map(|value| value.clamp(0, 10_000))
}
fn dimension_assignment(line: &str, name: &str) -> Option<i64> {
@@ -188,7 +226,7 @@ fn parse_dimension(text: &str) -> Option<i64> {
pub fn preview_json(source: &str) -> Result<String, String> {
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs(&body)?;
let mut document = build_document(&paragraphs);
let mut document = build_document(&paragraphs, &geometry);
apply_geometry(&mut document, &geometry);
let engine = Engine::layout(&document);
let json = render_json(&document, &engine, None);
@@ -204,7 +242,7 @@ pub fn preview_json(source: &str) -> Result<String, String> {
pub fn pdf_bytes(source: &str) -> Result<Vec<u8>, String> {
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs(&body)?;
let mut document = build_document(&paragraphs);
let mut document = build_document(&paragraphs, &geometry);
apply_geometry(&mut document, &geometry);
let engine = Engine::layout(&document);
let pages: Vec<_> = engine
@@ -229,14 +267,15 @@ pub fn pdf_bytes(source: &str) -> Result<Vec<u8>, String> {
/// Apply one paragraph edit to the last route-D document and report page
/// checkpoints that were reused versus rebuilt.
pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {
let paragraphs = paragraphs(source)?;
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs(&body)?;
if unit >= paragraphs.len() {
return Err(format!(
"unit {unit} is out of range for {} paragraphs",
paragraphs.len()
));
}
let next = build_document(&paragraphs);
let next = build_document(&paragraphs, &geometry);
ROUTE_D_SESSION.with(|slot| {
let mut slot = slot.borrow_mut();
let (_, mut engine) = match slot.take() {
@@ -426,7 +465,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
}
}
fn build_document(paragraphs: &[SourceParagraph]) -> Document {
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
let units = paragraphs
.iter()
.enumerate()
@@ -440,11 +479,11 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
}
let style = ParagraphStyle {
line_width: 480 * SCALE,
left_skip: paragraph.left_skip * SCALE,
left_skip: paragraph.left_skip * SCALE + geometry.left_skip.unwrap_or(0),
centered: paragraph.centered,
right_aligned: paragraph.right_aligned,
ragged_right: false,
right_skip: 0,
right_skip: geometry.right_skip.unwrap_or(0),
hang_indent: 0,
hang_after: 1,
par_shape: Vec::new(),
@@ -453,13 +492,13 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
par_fillskip_fil: true,
frenchspacing: false,
space_factor_codes: Vec::new(),
baselineskip: 12 * 65536,
baselineskip: geometry.baseline_skip.unwrap_or(12 * 65536),
lineskip: 65536,
lineskiplimit: 0,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
tolerance: 200,
par_indent: geometry.par_indent.unwrap_or(0),
emergencystretch: geometry.emergency_stretch.unwrap_or(0),
pretolerance: geometry.pretolerance.unwrap_or(100),
tolerance: geometry.tolerance.unwrap_or(200),
hyphen_penalty: 50,
ex_hyphen_penalty: 50,
double_hyphen_demerits: 10_000,
@@ -467,7 +506,11 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
line_penalty: 10,
adj_demerits: 10_000,
hyphenation_exceptions: Vec::new(),
line_height: paragraph.line_height * SCALE,
line_height: if paragraph.line_height == 12 {
geometry.baseline_skip.unwrap_or(12 * 65536)
} else {
paragraph.line_height * SCALE
},
char_width: 10 * SCALE,
space_width: 4 * SCALE,
space_stretch: 6 * SCALE,
@@ -475,11 +518,12 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
space_skip: None,
xspace_skip: None,
};
let mut unit = Unit::paragraph_with_style(index as u64, paragraph.text.clone(), style);
if let Some(VerticalItem::Paragraph(paragraph_box)) = unit.items.first_mut() {
paragraph_box.font = paragraph.font;
}
unit
Unit::paragraph_with_style_and_face(
index as u64,
paragraph.text.clone(),
style,
paragraph.font,
)
})
.collect();
Document::new(700 * SCALE, units)