Use CMR10 advances for route-D paragraphs and PDF text

This commit is contained in:
gloamxun committed 2026-10-20 22:55:38 +08:00
1 parent 8251e35de2
commit 67915402a6
7 files changed
+41 -15

No files matched your search

+1 -1
View File
@@ -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`. 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 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 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 `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 document and returns the same page list plus `changedPages` and
+22 -5
View File
@@ -461,7 +461,8 @@ impl Unit {
style: ParagraphStyle, style: ParagraphStyle,
) -> Self { ) -> Self {
let source = source.into(); let source = source.into();
let paragraph = layout_paragraph(&source, &style); let paragraph =
layout_paragraph_with_font(&source, &style, &crate::cmr10::Cmr10Metrics::new());
Self { Self {
id, id,
source, source,
@@ -1353,15 +1354,16 @@ mod tests {
let mut document = Document::new( let mut document = Document::new(
100, 100,
vec![ vec![
Unit::paragraph(1, "a ".repeat(200)), Unit::paragraph(1, "a ".repeat(20)),
Unit::paragraph(2, "b ".repeat(200)), Unit::vertical(2, vec![VerticalItem::Penalty(-10_000)]),
Unit::paragraph(3, "b ".repeat(200)),
], ],
); );
let mut engine = Engine::layout(&document); let mut engine = Engine::layout(&document);
let before = engine.pages()[0].hash; 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); assert_eq!(engine.pages()[0].hash, before);
document.units[1].source = "c ".repeat(200); document.units[2].source = "c ".repeat(200);
} }
#[test] #[test]
@@ -1598,6 +1600,21 @@ mod tests {
assert_eq!(before, after); 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] #[test]
fn tall_insertions_split_across_pages() { fn tall_insertions_split_across_pages() {
let document = Document::new( let document = Document::new(
+11 -2
View File
@@ -8,6 +8,7 @@ use std::sync::OnceLock;
use rusttype::Font; use rusttype::Font;
use crate::cmr10::Cmr10Metrics;
use crate::render::{DisplayItem, DisplayList, FontFace}; use crate::render::{DisplayItem, DisplayList, FontFace};
struct PdfFace { struct PdfFace {
@@ -160,16 +161,24 @@ fn cid_widths(face: &'static PdfFace) -> String {
let font = face_font(face); let font = face_font(face);
let units = font.units_per_em() as f32; let units = font.units_per_em() as f32;
let scale = 1000.0 / units; let scale = 1000.0 / units;
let metrics = Cmr10Metrics::new();
let mut entries = Vec::new(); let mut entries = Vec::new();
for character in (0x20u8..=0x7e).map(char::from) { for character in (0x20u8..=0x7e).map(char::from) {
let glyph = font.glyph(character); let glyph = font.glyph(character);
let id = glyph.id().0; let id = glyph.id().0;
let width = (glyph // 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)) .scaled(rusttype::Scale::uniform(units))
.h_metrics() .h_metrics()
.advance_width .advance_width
* scale) * scale)
.round() as i64; .round() as i64
};
entries.push((id, width)); entries.push((id, width));
} }
entries.sort_unstable(); entries.sort_unstable();
+2 -2
View File
@@ -280,7 +280,7 @@ impl Halign {
font: FontFace::Roman, font: FontFace::Roman,
x, x,
y: baseline, y: baseline,
size: self.line_height * 5 / 6, size: 10 * 65_536,
text: cell.text.clone(), text: cell.text.clone(),
}); });
} }
@@ -375,7 +375,7 @@ impl Page {
}; };
let lines = paragraph.lines.len().max(1) as i64; let lines = paragraph.lines.len().max(1) as i64;
let line_height = (paragraph.height / lines).max(1); 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 &paragraph.lines { for line in &paragraph.lines {
let baseline = y + ((line_height as f64) * 0.8) as i64; let baseline = y + ((line_height as f64) * 0.8) as i64;
let natural: i64 = line let natural: i64 = line
+1 -1
View File
@@ -11,7 +11,7 @@ fn paragraph(id: u64, marker: &str) -> Unit {
#[test] #[test]
fn route_d_article_exports_pdf_and_reflows_only_the_edited_page() { fn route_d_article_exports_pdf_and_reflows_only_the_edited_page() {
let document = Document::new( let document = Document::new(
300, 20,
vec![ vec![
paragraph(1, "ONE"), paragraph(1, "ONE"),
paragraph(2, "TWO"), paragraph(2, "TWO"),
+2 -2
View File
@@ -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. 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. 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. 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 ### M0 incremental demo
+2 -2
View File
@@ -355,7 +355,7 @@ fn route_d_maps_sections_and_emphasis() {
#[test] #[test]
fn route_d_edit_keeps_unaffected_page_hashes() { fn route_d_edit_keeps_unaffected_page_hashes() {
let server = TestServer::start("route-d-hashes"); let server = TestServer::start("route-d-hashes");
let paragraph = "word ".repeat(400); let paragraph = "word ".repeat(1200);
let body = format!( let body = format!(
"{paragraph} "{paragraph}
@@ -575,7 +575,7 @@ fn route_d_renders_flushright_blocks() {
.unwrap() .unwrap()
.parse() .parse()
.unwrap(); .unwrap();
assert!(x > 100.0, "{x}"); assert!(x > 50.0, "{x}");
} }
#[test] #[test]