Honour finite parfillskip on the last line

This commit is contained in:
gloamxun committed 2026-10-23 01:22:16 +08:00
1 parent 8993186005
commit bd109d03c5
6 files changed
+146 -16

No files matched your search

+94 -4
View File
@@ -85,6 +85,15 @@ pub struct ParagraphStyle {
/// Explicit `(indent, width)` pairs for `\parshape`.
#[serde(default)]
pub par_shape: Vec<(i64, i64)>,
/// `\parfillskip` width, inserted before the paragraph's final break.
#[serde(default)]
pub par_fillskip: i64,
/// Finite stretch of `\parfillskip`.
#[serde(default)]
pub par_fillskip_stretch: i64,
/// Whether `\parfillskip` ends in `fil` glue (the TeX default).
#[serde(default = "default_par_fillskip_fil")]
pub par_fillskip_fil: bool,
/// Center each line in the measure instead of justifying it.
#[serde(default)]
pub centered: bool,
@@ -135,6 +144,10 @@ pub struct ParagraphStyle {
pub space_shrink: i64,
}
fn default_par_fillskip_fil() -> bool {
true
}
impl ParagraphStyle {
/// `(indent, measure)` for a zero-based line number, honouring
/// `\parshape` and `\hangindent`/`\hangafter`.
@@ -190,6 +203,9 @@ impl Default for ParagraphStyle {
hang_indent: 0,
hang_after: 1,
par_shape: Vec::new(),
par_fillskip: 0,
par_fillskip_stretch: 0,
par_fillskip_fil: true,
centered: false,
right_aligned: false,
par_indent: 0,
@@ -413,6 +429,10 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
if is_last {
trailing_penalty = 0;
if !style.par_fillskip_fil {
natural += style.par_fillskip;
stretch += style.par_fillskip_stretch;
}
} else if hyphen_break {
// TeX charges \exhyphenpenalty after an explicit hyphen
// and \hyphenpenalty at an inserted hyphenation point.
@@ -449,8 +469,9 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
} else {
(0.0, 0)
};
// TeX's final line has \parfillskip's infinite stretch.
let badness = if is_last && natural <= target {
// TeX's default final line has \parfillskip's infinite
// stretch; without fil the last line is set like any other.
let badness = if is_last && style.par_fillskip_fil && natural <= target {
0
} else {
badness
@@ -583,10 +604,15 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
if !has_box {
continue;
}
let final_line = end == items.len();
if final_line && !style.par_fillskip_fil {
natural += style.par_fillskip;
stretch += style.par_fillskip_stretch;
}
if overflow.is_none() && natural >= target {
overflow = Some(end_index);
}
let badness = if end == items.len() && natural <= target {
let badness = if final_line && style.par_fillskip_fil && natural <= target {
0
} else if natural < target {
if stretch > 0 {
@@ -682,7 +708,19 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
}
let is_last = line_index + 1 == line_count;
if !is_last {
let fillskip = if is_last && !style.par_fillskip_fil {
style.par_fillskip
} else {
0
};
let fillskip_stretch = if is_last && !style.par_fillskip_fil {
style.par_fillskip_stretch
} else {
0
};
if !is_last || !style.par_fillskip_fil {
natural += fillskip;
stretch += fillskip_stretch;
// TeX can stretch normal glue past its nominal stretch (badness
// simply exceeds 100), but it never shrinks past the declared
// shrink: an overfull line keeps its overflow instead of pulling
@@ -1284,4 +1322,56 @@ mod tests {
"exceptions insert a discretionary point"
);
}
#[test]
fn parfillskip_without_fil_stretches_the_last_line() {
let text = "alpha beta";
let style = ParagraphStyle {
line_width: 120,
..ParagraphStyle::default()
};
let default = layout_paragraph(text, &style);
let mut flush = style.clone();
flush.par_fillskip_fil = false;
let justified = layout_paragraph(text, &flush);
let last_width = |paragraph: &Paragraph| {
paragraph
.lines
.last()
.expect("a line")
.iter()
.map(|item| match item {
HorizontalItem::Box(box_item) => box_item.width,
HorizontalItem::Glue { width, .. } => *width,
_ => 0,
})
.sum::<i64>()
};
assert!(last_width(&default) < 120, "fil leaves the last line short");
assert_eq!(last_width(&justified), 120, "0pt parfillskip is flush");
}
#[test]
fn parfillskip_width_leaves_a_gap_on_the_last_line() {
let text = "alpha beta";
let style = ParagraphStyle {
line_width: 120,
par_fillskip: 20,
par_fillskip_fil: false,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph(text, &style);
let last_width: i64 = paragraph
.lines
.last()
.expect("a line")
.iter()
.map(|item| match item {
HorizontalItem::Box(box_item) => box_item.width,
HorizontalItem::Glue { width, .. } => *width,
_ => 0,
})
.sum();
assert_eq!(last_width, 100, "parfillskip leaves its width at the end");
}
}
+16
View File
@@ -333,6 +333,22 @@ fn hang_indent_shifts_lines_like_tex() {
);
}
/// A paragraph whose `\parfillskip` has no `fil` must justify its final
/// line, so every line matches pdfLaTeX.
#[test]
fn parfillskip_without_fil_justifies_the_last_line() {
let width = 120;
let Some(reference) = pdftex_lines(&paragraph_fixture_with(WORDS, width, "\\parfillskip=0pt "))
else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_d_lines_with(WORDS, f64::from(width), |style| {
style.par_fillskip_fil = false;
});
assert_lines_match("parfillskip 120pt", &ours, &reference);
}
#[test]
fn paragraph_breaks_match_pdftex_at_150pt() {
compare_fixture(WORDS, 150);
+12
View File
@@ -18,6 +18,9 @@ fn body_style() -> ParagraphStyle {
hang_indent: 0,
hang_after: 1,
par_shape: Vec::new(),
par_fillskip: 0,
par_fillskip_stretch: 0,
par_fillskip_fil: true,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
@@ -167,6 +170,9 @@ fn route_d_page_matches_pdftex_raster_geometry() {
hang_indent: 0,
hang_after: 1,
par_shape: Vec::new(),
par_fillskip: 0,
par_fillskip_stretch: 0,
par_fillskip_fil: true,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
@@ -327,6 +333,9 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
hang_indent: 0,
hang_after: 1,
par_shape: Vec::new(),
par_fillskip: 0,
par_fillskip_stretch: 0,
par_fillskip_fil: true,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
@@ -617,6 +626,9 @@ fn route_d_multiline_bands_track_pdftex() {
hang_indent: 0,
hang_after: 1,
par_shape: Vec::new(),
par_fillskip: 0,
par_fillskip_stretch: 0,
par_fillskip_fil: true,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
+11 -5
View File
@@ -48,16 +48,17 @@ 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`). 293 workspace tests are green and no oracle
PDF opt-in with `?engine=route-d`). 296 workspace tests are green and no oracle
fixture is ignored.
Known gaps, in dependency order:
1. paragraph breaking reproduces TeX's passes and demerits (including
`\adjdemerits`, `\doublehyphendemerits`, `\finalhyphendemerits`,
`\hyphenpenalty`, `\exhyphenpenalty`, `\hyphenation` exceptions and
`\hangindent`/`\hangafter`/`\parshape` line geometry), but not yet
`\looseness`, spacefactor or `\parfillskip`/fil glue orders;
`\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;
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
@@ -184,7 +185,12 @@ and left offset; the DP remembers a candidate's line number so the geometry can
vary per line, and a 120pt oracle fixture checks every line origin against
pdfLaTeX.
Still open: `\parfillskip`/fil glue orders, `\looseness`, spacefactor/
`\parfillskip` is modelled: with the default `0pt plus 1fil` the last line is
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`),
double/final hyphen demerit oracle fixtures, and more width fixtures.
+10 -7
View File
@@ -88,7 +88,9 @@ solutions. Two further fixtures pin register behaviour at 120pt:
`\pretolerance=10000` stops before hyphenation and `\hyphenpenalty=10000`
inhibits it, leaving the overfull first-fit lines TeX produces. A 120pt
fixture with `\hangindent=20pt \hangafter=1` checks every line origin
against pdfLaTeX as well as the word positions inside each line. `route_d_multiline_bands_track_pdftex` rasterizes the same
against pdfLaTeX as well as the word positions inside each line, and a
`\parfillskip=0pt` fixture checks that a final line without `fil` glue is
justified like any other line (the default `0pt plus 1fil` leaves it short). `route_d_multiline_bands_track_pdftex` rasterizes the same
paragraph in both engines and compares ink bands, covering baseline placement
and justification.
@@ -124,8 +126,8 @@ and justification.
`\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`,
`\parfillskip`/fil glue orders, and the vertical-list skips
(`\baselineskip`, `\lineskip`, `\parskip`).
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.
@@ -297,7 +299,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 184 unit tests plus six route-D integration tests and six oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 293 tests (184 route-D unit, 6 route-D integration, 6 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 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.
### M0 incremental demo
@@ -325,9 +327,10 @@ 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`, `\parfillskip`/fil glue orders
and the vertical-list skips are still open; the tolerance, penalty and demerit
registers, `\hyphenation` exceptions, and `\hangindent`/`\parshape` are
- **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.
- **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
+3
View File
@@ -336,6 +336,9 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
hang_indent: 0,
hang_after: 1,
par_shape: Vec::new(),
par_fillskip: 0,
par_fillskip_stretch: 0,
par_fillskip_fil: true,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,