Map LaTeX tabular sources onto route-D halign

This commit is contained in:
gloamxun committed 2026-10-24 01:22:36 +08:00
1 parent 20011eadd3
commit 95e677b5c7
5 files changed
+171 -5

No files matched your search

+142
View File
@@ -139,6 +139,8 @@ struct PageGeometry {
line_penalty: Option<i64>,
adj_demerits: Option<i64>,
space_factor_codes: Vec<(char, i64)>,
/// Captured `tabular` environments: (column spec, halign source).
tabular: Vec<(String, String)>,
}
/// Pull `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight`/`\vsize`/
@@ -148,8 +150,31 @@ struct PageGeometry {
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
let mut geometry = PageGeometry::default();
let mut body = String::new();
let mut tabular: Option<(String, String)> = None;
for line in source.lines() {
let trimmed = line.trim();
if let Some((_, text)) = tabular.as_mut() {
if trimmed == "\\end{tabular}" {
let (template, text) = tabular.take().unwrap();
geometry.tabular.push((template, text));
} else {
if !text.is_empty() {
text.push('\n');
}
text.push_str(trimmed);
}
continue;
}
if let Some(rest) = trimmed.strip_prefix("\\begin{tabular}") {
if let Some((template, _)) = braced(rest.trim_start()) {
body.push_str(&format!(
"\n\n\\texrratable{{{}}}\n\n",
geometry.tabular.len()
));
tabular = Some((template.to_string(), String::new()));
}
continue;
}
if let Some(value) = dimension_assignment(trimmed, "hoffset") {
geometry.origin.h_offset = value;
} else if let Some(value) = dimension_assignment(trimmed, "voffset") {
@@ -233,9 +258,86 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
body.push('\n');
}
}
if let Some((template, text)) = tabular {
geometry.tabular.push((template, text));
}
(body, geometry)
}
fn braced(text: &str) -> Option<(&str, &str)> {
let mut depth = 0_i32;
for (index, character) in text.char_indices() {
match character {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
return Some((&text[1..index], &text[index + 1..]));
}
}
_ => {}
}
}
None
}
fn engine_template(spec: &str) -> String {
let mut out = String::new();
let mut characters = spec.chars().peekable();
while let Some(character) = characters.next() {
match character {
'l' | 'c' | 'r' => out.push(character),
'p' | 'm' | 'b' => {
out.push('p');
if characters.peek() == Some(&'{') {
let mut depth = 0_i32;
for inner in characters.by_ref() {
out.push(inner);
match inner {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
break;
}
}
_ => {}
}
}
}
}
'|' | ' ' | '\t' => {}
'@' | '!' | '>' | '<' | '*' => {
if characters.peek() == Some(&'{') {
let mut depth = 0_i32;
for inner in characters.by_ref() {
match inner {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
break;
}
}
_ => {}
}
}
}
}
_ => {}
}
}
if out.is_empty() { "l".to_string() } else { out }
}
fn table_reference(text: &str) -> Option<usize> {
text.trim()
.strip_prefix("\\texrratable{")?
.strip_suffix('}')?
.parse()
.ok()
}
/// Strip geometry assignments from the whole source (preamble included) and
/// return the LaTeX body with them removed.
fn geometry_and_body(source: &str) -> (String, PageGeometry) {
@@ -588,6 +690,24 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
let mut pending = (0_i64, 0_i64, 0_i64);
let mut pending_break = false;
for chunk in paragraphs {
if let Some(index) = table_reference(chunk) {
result.push(SourceParagraph {
text: format!("\\texrratable{{{index}}}"),
font: ParagraphFont::Roman,
line_height: 12,
math: false,
left_skip: 0,
centered: false,
right_aligned: false,
space_before: pending.0,
space_stretch: pending.1,
space_shrink: pending.2,
page_break: pending_break,
});
pending = (0, 0, 0);
pending_break = false;
continue;
}
if chunk == "\\newpage" || chunk == "\\clearpage" {
pending_break = true;
continue;
@@ -789,6 +909,21 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
.iter()
.enumerate()
.map(|(index, paragraph)| {
if let Some(table) = table_reference(&paragraph.text) {
if let Some((template, source)) = geometry.tabular.get(table) {
let mut unit = Unit::halign_from_source_with_template_and_tabskips(
index as u64,
&engine_template(template),
source,
6 * SCALE,
Vec::new(),
);
unit.space_before = paragraph.space_before;
unit.space_stretch = paragraph.space_stretch;
unit.space_shrink = paragraph.space_shrink;
return unit;
}
}
if paragraph.math {
let mut unit = Unit::math(
index as u64,
@@ -931,6 +1066,13 @@ fn escape_json(value: &str) -> String {
mod tests {
use super::*;
#[test]
fn engine_template_keeps_paragraph_columns() {
assert_eq!(engine_template("{|l|c|p{3cm}|}"), "lcp{3cm}");
assert_eq!(engine_template("|p{2cm}|r|"), "p{2cm}r");
assert_eq!(engine_template(""), "l");
}
fn multi_page_source() -> String {
let mut source = String::from("\\documentclass{article}\n\\begin{document}\n\n");
for index in 0..36 {