From 67915402a6db7598de85e53605a81dcf3093ce83 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 22:55:38 +0800 Subject: [PATCH] Use CMR10 advances for route-D paragraphs and PDF text --- README.md | 2 +- crates/texrra-tex-core/src/lib.rs | 27 ++++++++++++++++++++----- crates/texrra-tex-core/src/pdf.rs | 21 +++++++++++++------ crates/texrra-tex-core/src/render.rs | 4 ++-- crates/texrra-tex-core/tests/route_d.rs | 2 +- docs/TEX_CORE.md | 4 ++-- tests/server.rs | 4 ++-- 7 files changed, 45 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index dc6bd0d..dc25530 100644 --- a/README.md +++ b/README.md @@ -566,7 +566,7 @@ default until the new core reaches layout parity. See `docs/TEX_CORE.md`. The first M7 service integration is available as `POST /api/route-d/preview`, with PDF export at `POST /api/route-d/pdf`. It accepts blank-line-separated source with a small `\section`/`\textbf`/`\emph`/display-math/list subset, lays it out with the route-D page builder, and returns the v1-compatible preview shape plus per-page hashes and -SVG. The v1 `/preview` and project APIs are unchanged and remain the default. +SVG. Route-D paragraphs are laid out with CMR10 advances, kerns and ligatures, and the PDF `/W` array reports the same advances, so extracted text keeps word boundaries. The v1 `/preview` and project APIs are unchanged and remain the default. `POST /api/route-d/edit/{unit}` applies one paragraph edit to the last route-D document and returns the same page list plus `changedPages` and diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index ca1f93a..9dae77e 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -461,7 +461,8 @@ impl Unit { style: ParagraphStyle, ) -> Self { let source = source.into(); - let paragraph = layout_paragraph(&source, &style); + let paragraph = + layout_paragraph_with_font(&source, &style, &crate::cmr10::Cmr10Metrics::new()); Self { id, source, @@ -1353,15 +1354,16 @@ mod tests { let mut document = Document::new( 100, vec![ - Unit::paragraph(1, "a ".repeat(200)), - Unit::paragraph(2, "b ".repeat(200)), + Unit::paragraph(1, "a ".repeat(20)), + Unit::vertical(2, vec![VerticalItem::Penalty(-10_000)]), + Unit::paragraph(3, "b ".repeat(200)), ], ); let mut engine = Engine::layout(&document); let before = engine.pages()[0].hash; - engine.edit(&document, 2, "c ".repeat(200)); + engine.edit(&document, 3, "c ".repeat(200)); assert_eq!(engine.pages()[0].hash, before); - document.units[1].source = "c ".repeat(200); + document.units[2].source = "c ".repeat(200); } #[test] @@ -1598,6 +1600,21 @@ mod tests { assert_eq!(before, after); } + #[test] + fn paragraph_widths_come_from_cmr10_not_fixed_cells() { + let width = |source: &str| { + let unit = Unit::paragraph(1, source); + match &unit.items[0] { + VerticalItem::Paragraph(paragraph) => paragraph.width, + other => panic!("expected paragraph, got {other:?}"), + } + }; + assert!( + width("mmm") > width("iii"), + "Computer Modern m is wider than i" + ); + } + #[test] fn tall_insertions_split_across_pages() { let document = Document::new( diff --git a/crates/texrra-tex-core/src/pdf.rs b/crates/texrra-tex-core/src/pdf.rs index 79aadcc..74c52c1 100755 --- a/crates/texrra-tex-core/src/pdf.rs +++ b/crates/texrra-tex-core/src/pdf.rs @@ -8,6 +8,7 @@ use std::sync::OnceLock; use rusttype::Font; +use crate::cmr10::Cmr10Metrics; use crate::render::{DisplayItem, DisplayList, FontFace}; struct PdfFace { @@ -160,16 +161,24 @@ fn cid_widths(face: &'static PdfFace) -> String { let font = face_font(face); let units = font.units_per_em() as f32; let scale = 1000.0 / units; + let metrics = Cmr10Metrics::new(); let mut entries = Vec::new(); for character in (0x20u8..=0x7e).map(char::from) { let glyph = font.glyph(character); let id = glyph.id().0; - let width = (glyph - .scaled(rusttype::Scale::uniform(units)) - .h_metrics() - .advance_width - * scale) - .round() as i64; + // The layout positions every glyph with CMR10 advances, independent of + // the face used for painting. Reporting the CMR10 advance in `/W` keeps + // PDF text extraction from inserting spaces between letters. + let width = if character.is_ascii() { + metrics.width(character) * 1000 / (10 * 65_536) + } else { + (glyph + .scaled(rusttype::Scale::uniform(units)) + .h_metrics() + .advance_width + * scale) + .round() as i64 + }; entries.push((id, width)); } entries.sort_unstable(); diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 4f05d6f..6d0a567 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -280,7 +280,7 @@ impl Halign { font: FontFace::Roman, x, y: baseline, - size: self.line_height * 5 / 6, + size: 10 * 65_536, text: cell.text.clone(), }); } @@ -375,7 +375,7 @@ impl Page { }; let lines = paragraph.lines.len().max(1) as i64; let line_height = (paragraph.height / lines).max(1); - let size = ((line_height as f64) * 0.7) as i64; + let size = 10 * 65_536; for line in ¶graph.lines { let baseline = y + ((line_height as f64) * 0.8) as i64; let natural: i64 = line diff --git a/crates/texrra-tex-core/tests/route_d.rs b/crates/texrra-tex-core/tests/route_d.rs index 62da5ed..d7d3c72 100755 --- a/crates/texrra-tex-core/tests/route_d.rs +++ b/crates/texrra-tex-core/tests/route_d.rs @@ -11,7 +11,7 @@ fn paragraph(id: u64, marker: &str) -> Unit { #[test] fn route_d_article_exports_pdf_and_reflows_only_the_edited_page() { let document = Document::new( - 300, + 20, vec![ paragraph(1, "ONE"), paragraph(2, "TWO"), diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 8cb22b8..29819c1 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -254,7 +254,7 @@ M3 painter: `Page::display_list()` and `Halign::display_list()` produce absolute PDF writer: `crates/texrra-tex-core/src/pdf.rs` writes multi-page PDF 1.4 with five embedded Liberation Type0/CID faces (Identity-H + ToUnicode + `/W` arrays), filled rectangles, diagonal rules, raw-RGB `/XObject` images, `/Annots` URI links and arbitrary `/special` operators. Ligature substitution and line-end italic correction are implemented; full NFSS family switching remains open. -M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; true NFSS family switching remains open. PDF text is still emitted one glyph at a time, so pdftotext may run adjacent words together; merging same-baseline glyphs into word runs is future work (an experiment that painted space glyphs separately made extraction worse by adding line breaks). +M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic,Mono}`. Roman/Bold/Italic/BoldItalic/Mono text are painted with embedded Liberation Type0/CID fonts, each with a ToUnicode CMap and `/W` width array. `cmr10.tfm` is parsed at runtime for widths, heights/depths, kern pairs and ff/fi/fl/ffi/ffl ligatures; line-end and math-script italic corrections and monospace/sans paragraph faces are applied; true NFSS family switching remains open. `Unit::paragraph` now builds its item stream with `layout_paragraph_with_font`, so CMR10 advances, kerns and ligatures drive paragraph widths; the painter emits 10pt glyphs and the Type0 `/W` array reports CMR10 advances, so `pdftotext` keeps word boundaries instead of inserting spaces between letters. M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments. @@ -264,7 +264,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits. -The crate currently has 138 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 234 tests (139 route-D unit, 1 route-D integration, 71 v1 integration, 18 server, 4 v1 unit, 1 doctest). v1 remains the live server path. +The crate currently has 140 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 235 tests (140 route-D unit, 1 route-D integration, 71 v1 integration, 18 server, 4 v1 unit, 1 doctest). v1 remains the live server path. ### M0 incremental demo diff --git a/tests/server.rs b/tests/server.rs index 9b74ff4..0e832af 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -355,7 +355,7 @@ fn route_d_maps_sections_and_emphasis() { #[test] fn route_d_edit_keeps_unaffected_page_hashes() { let server = TestServer::start("route-d-hashes"); - let paragraph = "word ".repeat(400); + let paragraph = "word ".repeat(1200); let body = format!( "{paragraph} @@ -575,7 +575,7 @@ fn route_d_renders_flushright_blocks() { .unwrap() .parse() .unwrap(); - assert!(x > 100.0, "{x}"); + assert!(x > 50.0, "{x}"); } #[test]