Apply TeX sentence spacing to interword glue
This commit is contained in:
1 parent
bd109d03c5
commit
295c934438
8 files changed
+137
-23
No files matched your search
@@ -138,6 +138,10 @@ impl Cmr10Metrics {
|
||||
pub fn shrink(&self) -> i64 {
|
||||
self.metrics().shrink()
|
||||
}
|
||||
|
||||
pub fn extra_space(&self) -> i64 {
|
||||
self.metrics().extra_space()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -94,6 +94,10 @@ pub struct ParagraphStyle {
|
||||
/// Whether `\parfillskip` ends in `fil` glue (the TeX default).
|
||||
#[serde(default = "default_par_fillskip_fil")]
|
||||
pub par_fillskip_fil: bool,
|
||||
/// `\frenchspacing`: every character gets space factor 1000, so
|
||||
/// punctuation adds no sentence spacing.
|
||||
#[serde(default)]
|
||||
pub frenchspacing: bool,
|
||||
/// Center each line in the measure instead of justifying it.
|
||||
#[serde(default)]
|
||||
pub centered: bool,
|
||||
@@ -206,6 +210,7 @@ impl Default for ParagraphStyle {
|
||||
par_fillskip: 0,
|
||||
par_fillskip_stretch: 0,
|
||||
par_fillskip_fil: true,
|
||||
frenchspacing: false,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
par_indent: 0,
|
||||
@@ -835,12 +840,24 @@ pub fn text_items_with_font(
|
||||
let words: Vec<&str> = text.split_whitespace().collect();
|
||||
let mut previous_box: Option<usize> = None;
|
||||
let mut previous_character: Option<char> = None;
|
||||
// TeX's space factor starts at 1000 and is updated by each character's
|
||||
// `\sfcode`; it scales the next interword glue (and adds `\fontdimen7`
|
||||
// once it reaches 2000).
|
||||
let mut space_factor = 1000_i64;
|
||||
for (word_index, word) in words.iter().enumerate() {
|
||||
if word_index > 0 {
|
||||
let factor = space_factor.max(1000);
|
||||
items.push(HorizontalItem::Glue {
|
||||
width: font.space(),
|
||||
stretch: font.stretch(),
|
||||
shrink: font.shrink(),
|
||||
width: font.space()
|
||||
+ if factor >= 2000 {
|
||||
font.extra_space()
|
||||
} else {
|
||||
0
|
||||
},
|
||||
stretch: font.stretch() * factor / 1000,
|
||||
// TeX divides the shrink by the space factor (`\sfcode`
|
||||
// shrinks less after punctuation) while multiplying stretch.
|
||||
shrink: font.shrink() * 1000 / factor,
|
||||
});
|
||||
// TeX never kerns across interword glue.
|
||||
previous_box = None;
|
||||
@@ -905,6 +922,7 @@ pub fn text_items_with_font(
|
||||
});
|
||||
}
|
||||
}
|
||||
space_factor = space_factor_after(word, style.frenchspacing);
|
||||
}
|
||||
while matches!(items.last(), Some(HorizontalItem::Glue { .. })) {
|
||||
items.pop();
|
||||
@@ -917,6 +935,21 @@ pub fn text_items_with_font(
|
||||
items
|
||||
}
|
||||
|
||||
/// TeX's default `\sfcode` values for sentence punctuation (`plain.tex`'s
|
||||
/// `\nonfrenchspacing`); every other character is 1000.
|
||||
fn space_factor_after(word: &str, frenchspacing: bool) -> i64 {
|
||||
if frenchspacing {
|
||||
return 1000;
|
||||
}
|
||||
match word.chars().last() {
|
||||
Some('.') | Some('?') | Some('!') => 3000,
|
||||
Some(':') => 2000,
|
||||
Some(';') => 1500,
|
||||
Some(',') => 1250,
|
||||
_ => 1000,
|
||||
}
|
||||
}
|
||||
|
||||
/// Paragraph builder driven by a real TFM font instead of fixed widths.
|
||||
pub fn layout_paragraph_with_font(
|
||||
text: &str,
|
||||
@@ -1374,4 +1407,49 @@ mod tests {
|
||||
.sum();
|
||||
assert_eq!(last_width, 100, "parfillskip leaves its width at the end");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sentence_punctuation_scales_the_interword_glue() {
|
||||
let style = ParagraphStyle::default();
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
let items = text_items_with_font("alpha. beta", &style, &font);
|
||||
let (width, stretch, shrink) = items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
HorizontalItem::Glue {
|
||||
width,
|
||||
stretch,
|
||||
shrink,
|
||||
} => Some((*width, *stretch, *shrink)),
|
||||
_ => None,
|
||||
})
|
||||
.expect("interword glue");
|
||||
assert_eq!(width, font.space() + font.extra_space());
|
||||
assert_eq!(stretch, font.stretch() * 3);
|
||||
assert_eq!(shrink, font.shrink() * 1000 / 3000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frenchspacing_keeps_a_plain_space() {
|
||||
let style = ParagraphStyle {
|
||||
frenchspacing: true,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
let items = text_items_with_font("alpha. beta", &style, &font);
|
||||
let (width, stretch, shrink) = items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
HorizontalItem::Glue {
|
||||
width,
|
||||
stretch,
|
||||
shrink,
|
||||
} => Some((*width, *stretch, *shrink)),
|
||||
_ => None,
|
||||
})
|
||||
.expect("interword glue");
|
||||
assert_eq!(width, font.space());
|
||||
assert_eq!(stretch, font.stretch());
|
||||
assert_eq!(shrink, font.shrink());
|
||||
}
|
||||
}
|
||||
@@ -255,6 +255,12 @@ impl TfmMetrics {
|
||||
pub fn quad(&self) -> i64 {
|
||||
self.param(5)
|
||||
}
|
||||
|
||||
/// `\fontdimen7`: extra space inserted after punctuation with a space
|
||||
/// factor of 2000 or more.
|
||||
pub fn extra_space(&self) -> i64 {
|
||||
self.param(6)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_kerns(
|
||||
|
||||
@@ -349,6 +349,20 @@ fn parfillskip_without_fil_justifies_the_last_line() {
|
||||
assert_lines_match("parfillskip 120pt", &ours, &reference);
|
||||
}
|
||||
|
||||
/// Sentence punctuation changes the space factor, so the interword glue and
|
||||
/// the resulting line breaks must match pdfLaTeX.
|
||||
#[test]
|
||||
fn sentence_spacing_matches_pdftex_at_150pt() {
|
||||
let width = 150;
|
||||
let text = "alpha. beta gamma, delta; epsilon: zeta eta theta iota kappa lambda mu";
|
||||
let Some(reference) = pdftex_lines(¶graph_fixture_with(text, width, "")) else {
|
||||
eprintln!("skipping: pdflatex/pdftotext are not installed");
|
||||
return;
|
||||
};
|
||||
let ours = route_d_lines_with(text, f64::from(width), |_| {});
|
||||
assert_lines_match("sentence spacing 150pt", &ours, &reference);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraph_breaks_match_pdftex_at_150pt() {
|
||||
compare_fixture(WORDS, 150);
|
||||
|
||||
@@ -21,6 +21,7 @@ fn body_style() -> ParagraphStyle {
|
||||
par_fillskip: 0,
|
||||
par_fillskip_stretch: 0,
|
||||
par_fillskip_fil: true,
|
||||
frenchspacing: false,
|
||||
par_indent: 0,
|
||||
emergencystretch: 0,
|
||||
pretolerance: 100,
|
||||
@@ -173,6 +174,7 @@ fn route_d_page_matches_pdftex_raster_geometry() {
|
||||
par_fillskip: 0,
|
||||
par_fillskip_stretch: 0,
|
||||
par_fillskip_fil: true,
|
||||
frenchspacing: false,
|
||||
par_indent: 0,
|
||||
emergencystretch: 0,
|
||||
pretolerance: 100,
|
||||
@@ -336,6 +338,7 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
|
||||
par_fillskip: 0,
|
||||
par_fillskip_stretch: 0,
|
||||
par_fillskip_fil: true,
|
||||
frenchspacing: false,
|
||||
par_indent: 0,
|
||||
emergencystretch: 0,
|
||||
pretolerance: 100,
|
||||
@@ -629,6 +632,7 @@ fn route_d_multiline_bands_track_pdftex() {
|
||||
par_fillskip: 0,
|
||||
par_fillskip_stretch: 0,
|
||||
par_fillskip_fil: true,
|
||||
frenchspacing: false,
|
||||
par_indent: 0,
|
||||
emergencystretch: 0,
|
||||
pretolerance: 100,
|
||||
|
||||
+11
-5
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
|
||||
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
||||
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`). 296 workspace tests are green and no oracle
|
||||
PDF opt-in with `?engine=route-d`). 299 workspace tests are green and no oracle
|
||||
fixture is ignored.
|
||||
|
||||
Known gaps, in dependency order:
|
||||
@@ -57,8 +57,9 @@ Known gaps, in dependency order:
|
||||
`\adjdemerits`, `\doublehyphendemerits`, `\finalhyphendemerits`,
|
||||
`\hyphenpenalty`, `\exhyphenpenalty`, `\hyphenation` exceptions,
|
||||
`\hangindent`/`\hangafter`/`\parshape` line geometry and `\parfillskip`
|
||||
with or without `fil`), but not yet `\looseness`, spacefactor or generic
|
||||
`fil`/`fill` glue orders;
|
||||
with or without `fil` and default `\sfcode` sentence spacing), but not yet
|
||||
`\looseness`, custom `\sfcode` overrides or generic `fil`/`fill` glue
|
||||
orders;
|
||||
2. the page builder is a scrolling builder with fraction policies, not a TeX
|
||||
page builder with badness and an interpreted `\output`;
|
||||
3. `\insert` classes are data structures, not TeX registers and an output
|
||||
@@ -190,8 +191,13 @@ left short, and a finite/zero `\parfillskip` makes the final line justify like
|
||||
any other; a 120pt `\parfillskip=0pt` oracle fixture confirms pdfLaTeX's
|
||||
rebalancing of the preceding lines.
|
||||
|
||||
Still open: generic `fil`/`fill` glue orders, `\looseness`, spacefactor/
|
||||
`\sfcode`, vertical-list skips (`\baselineskip`, `\lineskip`, `\parskip`),
|
||||
Sentence spacing is modelled with TeX's `\nonfrenchspacing` defaults:
|
||||
stretch is multiplied and shrink divided by the space factor, `\fontdimen7`
|
||||
is added from 2000, and `\frenchspacing` switches it off; a 150pt punctuated
|
||||
oracle fixture matches pdfLaTeX's line breaks and word positions.
|
||||
|
||||
Still open: generic `fil`/`fill` glue orders, `\looseness`, custom `\sfcode`
|
||||
overrides, vertical-list skips (`\baselineskip`, `\lineskip`, `\parskip`),
|
||||
double/final hyphen demerit oracle fixtures, and more width fixtures.
|
||||
|
||||
### P2 — Vertical list, page builder, `\output` (L2)
|
||||
|
||||
+16
-15
@@ -125,16 +125,17 @@ and justification.
|
||||
tolerance bounds, break penalties and flagged demerits are modelled,
|
||||
`\hyphenation` exceptions are inserted into the pattern trie, and
|
||||
`\hangindent`/`\hangafter`/`\parshape` change each line's measure and left
|
||||
offset. Remaining gaps are `\looseness`, spacefactor/`\sfcode`,
|
||||
generic `fil`/`fill` glue orders outside `\parfillskip`, and the
|
||||
offset, and `\parfillskip` (with or without `fil`) decides whether the last
|
||||
line is justified. Sentence spacing follows TeX's default `\sfcode` values:
|
||||
interword glue stretch is multiplied by the space factor, shrink is divided
|
||||
by it, `\fontdimen7` is added from factor 2000, and `\frenchspacing`
|
||||
disables the effect. Remaining gaps are `\looseness`, custom `\sfcode`
|
||||
overrides, generic `fil`/`fill` glue orders outside `\parfillskip`, and the
|
||||
vertical-list skips (`\baselineskip`, `\lineskip`, `\parskip`).
|
||||
|
||||
The pdfLaTeX oracle pins six measures (100-240pt) and all match; no oracle
|
||||
fixture is ignored.
|
||||
|
||||
The pdfLaTeX oracle currently pins two measures (150pt and 180pt). The earlier
|
||||
multi-width table was produced by the old single-cost breaker and is no longer
|
||||
evidence of divergence; 120pt and 200pt should be added as fixtures next.
|
||||
The pdfLaTeX oracle pins the 100-240pt measure matrix plus `\hangindent`,
|
||||
`\parfillskip` and sentence-spacing fixtures; all match and no oracle fixture
|
||||
is ignored.
|
||||
|
||||
- `\middle|` inside `\left...\right` is accepted as a delimiter symbol (not yet stretched).
|
||||
- `\sqrt[n]{x}` carries an optional degree atom; `\left.` is an empty
|
||||
@@ -261,7 +262,7 @@ for a later overlaid slash.
|
||||
| Milestone | State | Notes |
|
||||
| --- | --- | --- |
|
||||
| M0 checkpoint engine | done | page list, page hashes, snapshot/restore, edit replay |
|
||||
| M1 horizontal/paragraphs | partial | boxes/glue/penalties, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, pdfTeX oracle matrix 100-240pt |
|
||||
| M1 horizontal/paragraphs | partial | boxes/glue/penalties, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing (`\sfcode`/`\frenchspacing`), pdfTeX oracle matrix 100-240pt plus three register fixtures |
|
||||
| M2 vertical/page builder | partial | vsize, topskip, insertions, marks, Rust output hook, top/bottom floats, float-page/text fractions, rules, vskip, penalties; interpreted `\output` absent |
|
||||
| M3 halign | partial | natural columns, spans, omit, hline rows, templates, noalign skip, rules; no PDF painter |
|
||||
| M4 math | partial | atoms, scripts, fractions, roots, delimiters, accents, operators, classes, braces, boxed, negated; no math painter/shrink wrapping |
|
||||
@@ -299,7 +300,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`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
||||
|
||||
The crate currently has 186 unit tests plus six route-D integration tests and seven oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 296 tests (186 route-D unit, 6 route-D integration, 7 oracle, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
|
||||
The crate currently has 188 unit tests plus six route-D integration tests and eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 299 tests (188 route-D unit, 6 route-D integration, 8 oracle, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
|
||||
|
||||
### M0 incremental demo
|
||||
|
||||
@@ -327,11 +328,11 @@ The staged program that closes these milestones is described in
|
||||
Route D currently covers M0-M4/M6 as a working subset. The remaining milestones are
|
||||
deliberately open rather than approximated:
|
||||
|
||||
- **M1**: `\looseness`, spacefactor/`\sfcode`, generic `fil`/`fill` glue
|
||||
orders and the vertical-list skips are still open; the tolerance, penalty and
|
||||
demerit registers, `\hyphenation` exceptions,
|
||||
`\hangindent`/`\parshape`, and `\parfillskip` with or without `fil` are
|
||||
modelled.
|
||||
- **M1**: `\looseness`, custom `\sfcode` overrides, generic `fil`/`fill`
|
||||
glue orders and the vertical-list skips are still open; the tolerance, penalty
|
||||
and demerit registers, `\hyphenation` exceptions,
|
||||
`\hangindent`/`\parshape`, `\parfillskip` with or without `fil`, and the
|
||||
default `\sfcode`/`\frenchspacing` sentence spacing are modelled.
|
||||
- **M5**: `cmr10.tfm` is parsed at runtime with widths, glyph heights/depths and kern pairs. only used text faces embed Liberation Serif/Mono as Type0/Identity-H; each face is subset to the glyphs actually used (composite components included, glyph IDs retained); FontFile2 and ToUnicode streams use Flate compression; the ToUnicode CMap follows the used characters; ff/fi/fl/ffi/ffl ligatures come from TFM, and CMR10 italic corrections are applied at line ends; full NFSS family switching is not implemented.
|
||||
- **M6**: `\output`, `\insert`, mark/float output policies, class-file execution and
|
||||
math limit policies are only partially modeled; `OutputRoutine::wants_break` can force a page break between units, and insertion material is stacked by class from the page bottom; `\output` is still Rust code rather than interpreted tokens.
|
||||
|
||||
@@ -339,6 +339,7 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
|
||||
par_fillskip: 0,
|
||||
par_fillskip_stretch: 0,
|
||||
par_fillskip_fil: true,
|
||||
frenchspacing: false,
|
||||
par_indent: 0,
|
||||
emergencystretch: 0,
|
||||
pretolerance: 100,
|
||||
|
||||
Reference in new issue
Block a user