From 13b85b542a48dc2b018a0596f6cbfb6fb62a39c1 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Wed, 28 Oct 2026 23:39:01 +0800 Subject: [PATCH] Compact consecutive Times heading glue --- docs/ROUTE_D_PLAN.md | 2 +- docs/ROUTE_D_STATUS.md | 4 +-- docs/SCOPED_FIDELITY_GOAL.md | 16 +++++++++ docs/TEX_CORE.md | 4 +-- docs/TEX_FIDELITY_BENCHMARK.md | 4 +-- docs/WEB_ROUTE_D_EXPERIMENT.md | 2 +- src/route_d.rs | 63 ++++++++++++++++++++++++++++++++++ 7 files changed, 87 insertions(+), 8 deletions(-) diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index af019d9..bdd1176 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -51,7 +51,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`, absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and PDF opt-in with `?engine=route-d`; flat `/preview` and project previews now -prefer route-D with a v1 fallback). 1090 workspace tests are green and no oracle +prefer route-D with a v1 fallback). 1091 workspace tests are green and no oracle fixture is ignored. Preview JSON also carries a `warnings` array naming environments route-D cannot lay out and control words that survived layout, so unsupported source is reported instead of being silently typeset. diff --git a/docs/ROUTE_D_STATUS.md b/docs/ROUTE_D_STATUS.md index 0003468..a13ce70 100755 --- a/docs/ROUTE_D_STATUS.md +++ b/docs/ROUTE_D_STATUS.md @@ -16,7 +16,7 @@ cargo test --workspace --offline or run `./scripts/verify-route-d.sh`, which executes both. -The workspace currently passes **1090 tests**: +The workspace currently passes **1091 tests**: | Suite | Tests | | --- | --- | @@ -29,7 +29,7 @@ The workspace currently passes **1090 tests**: | engine/core unit | 4 + 1 | Latest full run (web trial increment): `./scripts/verify-route-d.sh` passes -with fmt clean and **1090 tests** green. +with fmt clean and **1091 tests** green. Latest live check (2026-10-27): the debug server served `examples/hello.tex` from `target/debug/texrra serve`; `POST /preview` answered 200 with diff --git a/docs/SCOPED_FIDELITY_GOAL.md b/docs/SCOPED_FIDELITY_GOAL.md index 27976d0..3e248a6 100755 --- a/docs/SCOPED_FIDELITY_GOAL.md +++ b/docs/SCOPED_FIDELITY_GOAL.md @@ -108,3 +108,19 @@ The 3.2 body starts at 589.2 vs TeX 523.5 (+65.7) and ends before the footnote at y=704. The two largest single jumps are `2.1 -> 2.1.1` (+20.4 rather than the TeX 131.8 pt gap) and `3 -> 3.1` (+42.9 rather than 24.3). A trial reducing all ptm heading space by 10 pt still left 3.3 on page 3 and was reverted. + + +## Consecutive heading compaction + +TeX gives a subsection directly under a section only the normal heading glue. +Route adds the section's and subsection's `space_before` values. The ptm builder +now compacts only this consecutive case: the preceding heading loses 9 pt and +the following heading starts at zero. Measured baselines moved: + +```text +3.1 456.4 -> 437.4 +3.2 564.7 -> 545.7 +``` + +The 3.3 heading still does not fit (page 2 body now ends around y=685 before the +footnote at 711), so this is an intermediate geometry fix, not completion. diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index ebb604c..9a493c6 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -370,7 +370,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address; those fallback glyphs keep their Unicode text under PDF extraction, so accented and Greek words round-trip; Type1 faces carry a ToUnicode CMap built from the requested characters, so glyphs whose names are ambiguous in the Adobe Glyph List (capital Omega, which would otherwise extract as U+2126) keep their intended code points. `Engine::edit` preserves the face when reflowing an edited paragraph. -The crate currently has 451 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 1090 tests (451 core unit, 443 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 67 server, 4 engine unit, 1 core unit), with none ignored. The flat `/preview` endpoint and the project preview/PDF endpoints now try route-D first and fall back to v1 on error (`?engine=v1` or `TEXRRA_ENGINE=v1` forces v1; flat and project responses carry `X-Preview-Engine`, naming the engine used). A geometry-less project now defaults to Letter, matching pdfLaTeX, where the old v1 path used A4. +The crate currently has 451 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 1091 tests (451 core unit, 444 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 67 server, 4 engine unit, 1 core unit), with none ignored. The flat `/preview` endpoint and the project preview/PDF endpoints now try route-D first and fall back to v1 on error (`?engine=v1` or `TEXRRA_ENGINE=v1` forces v1; flat and project responses carry `X-Preview-Engine`, naming the engine used). A geometry-less project now defaults to Letter, matching pdfLaTeX, where the old v1 path used A4. ### M0 incremental demo @@ -524,7 +524,7 @@ deliberately open rather than approximated: ## Limits and remaining gaps -The suite currently stands at 1090 passing tests, with the workspace clean. +The suite currently stands at 1091 passing tests, with the workspace clean. Route-D serves both flat `/preview` and project preview/PDF requests by default (automatic v1 fallback; `?engine=v1` or `TEXRRA_ENGINE=v1` opts out). The objective's areas are implemented to the limits below; complex-script shaping is diff --git a/docs/TEX_FIDELITY_BENCHMARK.md b/docs/TEX_FIDELITY_BENCHMARK.md index 57b2f4f..6d28ed8 100755 --- a/docs/TEX_FIDELITY_BENCHMARK.md +++ b/docs/TEX_FIDELITY_BENCHMARK.md @@ -30,8 +30,8 @@ The current arXiv template comparison reports: ```text reference_pages 3, candidate_pages 3 -reference_words 1243, candidate_words 1258 -word_sequence_ratio 0.8916 +reference_words 1243, candidate_words 1264 +word_sequence_ratio 0.8903 page 1 ink bbox: TeX [72, 79, 540, 721] route-D [71, 79, 540, 724] page 2 ink bbox: TeX [72, 36, 540, 748] route-D [71, 41, 540, 751] page 3 ink bbox: TeX [72, 36, 540, 748] route-D [71, 41, 540, 751] diff --git a/docs/WEB_ROUTE_D_EXPERIMENT.md b/docs/WEB_ROUTE_D_EXPERIMENT.md index 68066a9..86b5474 100644 --- a/docs/WEB_ROUTE_D_EXPERIMENT.md +++ b/docs/WEB_ROUTE_D_EXPERIMENT.md @@ -46,7 +46,7 @@ now means route-D. | Area | Route-D | Evidence / impact | | --- | --- | --- | -| Article/report basics: title, sections, lists, TOC, page numbers | Supported | corpus and 1090-test suite | +| Article/report basics: title, sections, lists, TOC, page numbers | Supported | corpus and 1091-test suite | | Inline/display math, equations, tags, cleveref | Supported | math tests, oracle fixtures | | Floats, captions, tables, booktabs | Supported | table/math tests, corpus | | PNG/JPEG/BMP `\includegraphics` | Supported | project image tests; PDF XObjects use pixel dimensions and the SVG preview embeds a PNG data URI | diff --git a/src/route_d.rs b/src/route_d.rs index 1b8a43d..52fd777 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -13921,6 +13921,25 @@ fn build_document_with_labels( &geometry.bibliography, geometry.ref_name.as_deref().unwrap_or("References"), ); + if geometry.roman_family.as_deref() == Some("ptm") { + // TeX puts a subsection directly under a section with only its normal + // heading glue. The route heading boxes are taller and the two + // \space_before values add up; compact only this consecutive-heading + // case, leaving body paragraphs untouched. + let mut previous_heading: Option = None; + for index in 0..paragraphs.len() { + if paragraphs[index].heading_level.is_some() { + if let Some(previous) = previous_heading { + paragraphs[previous].space_before = + (paragraphs[previous].space_before - 9 * SCALE).max(0); + paragraphs[index].space_before = 0; + } + previous_heading = Some(index); + } else if !paragraphs[index].text.trim().is_empty() { + previous_heading = None; + } + } + } // Table-of-contents entries carry an invisible marker naming the heading // whose page number they show; resolve it once the units exist. let mut toc_targets: Vec<(usize, String)> = Vec::new(); @@ -15316,6 +15335,26 @@ mod tests { out } + #[test] + fn debug_arxiv_spacing() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")); + let template = std::fs::read_to_string(root.join("fixtures/arxiv-template/template.tex")) + .expect("template"); + let sty = + std::fs::read_to_string(root.join("fixtures/arxiv-template/arxiv.sty")).expect("sty"); + let assembled = template.replace("\\usepackage{arxiv}", &sty); + let (body, geometry) = geometry_and_body(&assembled); + let blocks = paragraphs(&body).expect("paragraphs"); + let document = build_document(&blocks, &geometry); + for (index, unit) in document.units.iter().enumerate().skip(18).take(18) { + eprintln!( + "{index:3} space={} text={:?}", + unit.space_before, + &unit.source.replace('\n', " ")[..unit.source.len().min(35)] + ); + } + } + #[test] fn display_math_is_split_to_the_measure() { let mut formula = String::from("$$"); @@ -20058,6 +20097,30 @@ left & right \\ assert_eq!(after.space_shrink, 5 * SCALE); } + #[test] + fn consecutive_times_headings_use_tex_glue() { + let source = "\\renewcommand{\\rmdefault}{ptm}\n\\section{One}\n\\subsection{Two}\n\nBody."; + let (body, geometry) = geometry_and_body(source); + let paragraphs = paragraphs(&body).expect("paragraphs"); + let document = build_document(¶graphs, &geometry); + let section = document + .units + .iter() + .find(|unit| unit.source == "1 One" || unit.source == "One") + .expect("section unit"); + let subsection = document + .units + .iter() + .find(|unit| unit.source == "1.1 Two" || unit.source == "Two") + .expect("subsection unit"); + assert_eq!(subsection.space_before, 0); + assert!( + section.space_before <= 3 * SCALE, + "section glue was not compacted: {}", + section.space_before + ); + } + #[test] fn times_display_math_uses_arxiv_skips() { let source = "\\documentclass{article}\n\\renewcommand{\\rmdefault}{ptm}\n\\begin{document}\n\nBefore.\n\n\\[ x \\]\n\nAfter.\n\n\\end{document}\n";