Use CMR10 metrics in the M1 paragraph builder

This commit is contained in:
gloamxun committed 2026-10-20 12:20:49 +08:00
1 parent e6da4d2fa7
commit bf5dae3797
5 files changed
+150 -8

No files matched your search

+2 -2
View File
@@ -559,6 +559,6 @@ Numeric bibliographies render authors as Surname, I. rather than full given name
core. It does not embed pdfTeX/XeTeX/Tectonic; it is a new Rust engine designed
around page-boundary checkpoints and page-hash diffing. M0 implements vertical
boxes/glue, a simple page builder, serializable checkpoints and a page-local
edit demo. M1 adds the horizontal box/glue/penalty model and a word-level
Knuth-Plass-style paragraph builder. The production v1 engine remains the
edit demo. M1 adds the horizontal box/glue/penalty model, an item-stream Knuth-Plass-style
paragraph builder, and CMR10 character metrics extracted from `cmr10.tfm`. The production v1 engine remains the
default until the new core reaches layout parity. See `docs/TEX_CORE.md`.
+68
View File
@@ -0,0 +1,68 @@
// Computer Modern Roman 10pt character widths, extracted from cmr10.tfm
// with tftopl. Values are scaled points at 10pt design size.
#[derive(Clone, Debug)]
pub struct Cmr10Metrics {}
impl Default for Cmr10Metrics {
fn default() -> Self {
Self::new()
}
}
impl Cmr10Metrics {
pub const fn new() -> Self {
Self {}
}
pub fn width(&self, character: char) -> i64 {
self.widths()
.get(character as usize)
.copied()
.unwrap_or(327_680)
}
pub fn space(&self) -> i64 {
218_454
}
pub fn stretch(&self) -> i64 {
109_227
}
pub fn shrink(&self) -> i64 {
72_818
}
fn widths(&self) -> &'static [i64; 128] {
&CMR10_WIDTHS
}
}
const CMR10_WIDTHS: [i64; 128] = [
409601, 546135, 509727, 455112, 436908, 491521, 473317, 509727, 473317, 509727, 473317, 382295,
364090, 364090, 546135, 546135, 182045, 200250, 327681, 327681, 327681, 327681, 327681, 491521,
291272, 327682, 473317, 509727, 327681, 591647, 664464, 509727, 182045, 182045, 327681, 546135,
327681, 546135, 509727, 182045, 254863, 254863, 327681, 509727, 182045, 218454, 182045, 327681,
327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 182045, 182045,
182045, 509727, 309477, 309477, 509727, 491521, 464215, 473317, 500624, 446010, 427807, 514277,
491521, 236658, 336783, 509727, 409601, 600748, 491521, 509727, 446010, 509727, 482419, 364090,
473317, 491521, 491521, 673567, 491521, 491521, 400499, 182045, 327681, 182045, 327681, 182045,
182045, 327681, 364090, 291272, 364090, 291272, 200250, 327681, 364090, 182045, 200250, 345887,
182045, 546135, 364090, 327681, 364090, 345885, 256684, 258503, 254863, 364090, 345887, 473317,
345887, 345887, 291272, 327681, 655362, 327681, 327681, 327681,
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_cmr10_widths() {
let font = Cmr10Metrics::new();
assert!(font.width('A') > 450_000);
assert!((font.width('a') - 327_681).abs() <= 2);
assert!((font.space() - 218_454).abs() <= 2);
assert!(font.stretch() > 0);
assert!(font.shrink() > 0);
}
}
+6 -1
View File
@@ -1,6 +1,11 @@
pub mod cmr10;
pub mod paragraph;
pub use paragraph::{HorizontalBox, HorizontalItem, Paragraph, ParagraphStyle, layout_paragraph};
pub use cmr10::Cmr10Metrics;
pub use paragraph::{
HorizontalBox, HorizontalItem, Paragraph, ParagraphStyle, layout_items, layout_paragraph,
layout_paragraph_with_font, text_items, text_items_with_font,
};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
+67
View File
@@ -129,6 +129,9 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
let mut best = vec![i64::MAX; count];
let mut previous = vec![0_usize; count];
best[0] = 0;
if style.line_width == 150 * 65_536 {
eprintln!("BP {:?}", breakpoints);
}
for end_index in 1..count {
let end = breakpoints[end_index];
for start_index in 0..end_index {
@@ -203,6 +206,9 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
}
}
if style.line_width == 150 * 65_536 {
eprintln!("BEST {:?} PREV {:?}", best, previous);
}
let mut ranges = Vec::new();
let mut end_index = count - 1;
while end_index > 0 {
@@ -239,6 +245,51 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
}
/// Convert text using TFM character widths and fontdimen glue parameters.
pub fn text_items_with_font(
text: &str,
style: &ParagraphStyle,
font: &crate::cmr10::Cmr10Metrics,
) -> Vec<HorizontalItem> {
let mut items = Vec::new();
for character in text.chars() {
if character.is_whitespace() {
if !matches!(items.last(), Some(HorizontalItem::Glue { .. })) {
items.push(HorizontalItem::Glue {
width: font.space(),
stretch: font.stretch(),
shrink: font.shrink(),
});
}
} else {
items.push(HorizontalItem::Box(HorizontalBox {
width: font.width(character),
height: style.line_height,
depth: 0,
}));
}
}
while matches!(items.last(), Some(HorizontalItem::Glue { .. })) {
items.pop();
}
items.push(HorizontalItem::Penalty {
width: 0,
penalty: 10_000,
flagged: false,
});
items
}
/// Paragraph builder driven by a real TFM font instead of fixed widths.
pub fn layout_paragraph_with_font(
text: &str,
style: &ParagraphStyle,
font: &crate::cmr10::Cmr10Metrics,
) -> Paragraph {
let items = text_items_with_font(text, style, font);
layout_items(&items, style)
}
pub fn layout_paragraph(text: &str, style: &ParagraphStyle) -> Paragraph {
let items = text_items(text, style);
layout_items(&items, style)
@@ -283,6 +334,22 @@ mod tests {
assert!(paragraph.line_count() >= 2);
}
#[test]
fn cmr10_font_drives_character_widths() {
let font = crate::cmr10::Cmr10Metrics::new();
let style = ParagraphStyle {
line_width: 150 * 65_536,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font(
"The quick brown fox jumps over the lazy dog",
&style,
&font,
);
assert!(paragraph.line_count() >= 2);
assert!(paragraph.height > 0);
}
#[test]
fn empty_paragraph_has_no_lines() {
assert_eq!(layout_paragraph("", &ParagraphStyle::default()).height, 0);
+7 -5
View File
@@ -42,8 +42,10 @@ class execution are still ahead.
- `HorizontalItem::{Box, Glue, Penalty}` and `HorizontalBox` as the horizontal
list model;
- `ParagraphStyle` with `line_width`, `par_indent`, `line_height`, fixed
character/space metrics and glue stretch/shrink;
- `ParagraphStyle` with `line_width`, `par_indent`, `line_height` and glue
stretch/shrink;
- `Cmr10Metrics`, a CMR10 ASCII width table extracted from `cmr10.tfm` with
`tftopl`, plus the fontdimen space/stretch/shrink values;
- an item-stream Knuth-Plass-style dynamic program over character boxes, glue and penalty breakpoints that minimizes badness from
glue stretch/shrink, allows overfull lines with an explicit overfull penalty,
applies `par_indent` to the first line, and emits justified glue;
@@ -68,7 +70,7 @@ next paragraph milestones.
| Milestone | Content | Acceptance |
| --- | --- | --- |
| M0 | vertical list, page builder, checkpoint, page hash, edit demo | edit on page 2 rebuilds only page 2 |
| M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item model + word-level breaker implemented; hyphenation/TFM/pdfTeX parity next |
| M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics and breaker implemented; hyphenation/pdfTeX parity next |
| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | one-column article matches reference pages |
| M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | table width/rules match reference |
| M4 | math lists, scripts, fractions, radicals, accents, delimiters | arXiv formulas render without overlap |
@@ -78,8 +80,8 @@ next paragraph milestones.
## Current limits
- The M1 paragraph builder is word-level with fixed metrics; it does not yet
hyphenate, honor all penalties, or use TFM font metrics.
- The M1 paragraph builder uses a CMR10 ASCII metric table; it does not yet
hyphenate, honor all penalties, apply kerning/ligatures, or parse arbitrary TFM files.
- Page breaking only handles vertical boxes/glue; vertical penalties are recorded
but not interpreted.
- No macro state, output routine, math, fonts or PDF output in the new core yet.