Carry plus and minus on inter-paragraph skip glue

This commit is contained in:
gloamxun committed 2026-10-23 14:53:00 +08:00
1 parent 6f1310a876
commit 08a312be82
4 files changed
+101 -13

No files matched your search

+74 -8
View File
@@ -670,6 +670,12 @@ pub struct Document {
/// TeX `\parskip`: vertical glue inserted between paragraphs.
#[serde(default)]
pub par_skip: i64,
/// TeX stretch for the `\parskip` glue inserted between paragraphs.
#[serde(default)]
pub par_skip_stretch: i64,
/// TeX shrink for the `\parskip` glue inserted between paragraphs.
#[serde(default)]
pub par_skip_shrink: i64,
/// Extra horizontal displacement of the page origin (`\hoffset`).
#[serde(default)]
pub h_offset: i64,
@@ -767,6 +773,8 @@ impl Document {
text_float_sep: default_text_float_sep(),
in_text_sep: default_in_text_sep(),
par_skip: 0,
par_skip_stretch: 0,
par_skip_shrink: 0,
h_offset: 0,
v_offset: 0,
max_deadcycles: default_max_deadcycles(),
@@ -887,11 +895,15 @@ fn document_nodes(
if position == 0 && previous_paragraph && matches!(item, VerticalItem::Paragraph(_)) {
nodes.push(VerticalNode::Glue(VerticalGlue::new(
document.par_skip,
0,
0,
document.par_skip_stretch,
document.par_skip_shrink,
)));
owners.push(index);
items.push(VerticalItem::Glue(document.par_skip));
items.push(VerticalItem::StretchGlue(VerticalGlue::new(
document.par_skip,
document.par_skip_stretch,
document.par_skip_shrink,
)));
}
let node = match item {
VerticalItem::Box(vertical_box) => VerticalNode::Box {
@@ -1104,6 +1116,8 @@ fn place_paragraph_flow(
page_goal: i64,
top_skip: i64,
par_skip: i64,
par_skip_stretch: i64,
par_skip_shrink: i64,
bottom: i64,
) {
let mut pending = paragraph.clone();
@@ -1114,6 +1128,16 @@ fn place_paragraph_flow(
} else {
par_skip
};
let skip_stretch = if current.items.is_empty() {
0
} else {
par_skip_stretch
};
let skip_shrink = if current.items.is_empty() {
0
} else {
par_skip_shrink
};
let base = if at_top { top_skip } else { *used + skip };
let available = page_goal - *insertion_height - bottom - base;
let lines = pending.lines.len();
@@ -1121,8 +1145,14 @@ fn place_paragraph_flow(
if at_top {
*used = top_skip.max(pending.height);
} else {
if skip > 0 {
current.items.push(VerticalItem::Glue(skip));
if skip != 0 || skip_stretch != 0 || skip_shrink != 0 {
current
.items
.push(VerticalItem::StretchGlue(VerticalGlue::new(
skip,
skip_stretch,
skip_shrink,
)));
}
*used = base + pending.height;
}
@@ -1148,8 +1178,14 @@ fn place_paragraph_flow(
if at_top {
*used = top_skip.max(head.height);
} else {
if skip > 0 {
current.items.push(VerticalItem::Glue(skip));
if skip != 0 || skip_stretch != 0 || skip_shrink != 0 {
current
.items
.push(VerticalItem::StretchGlue(VerticalGlue::new(
skip,
skip_stretch,
skip_shrink,
)));
}
*used = base + head.height;
}
@@ -1533,6 +1569,8 @@ impl Engine {
document.page_goal,
document.top_skip,
document.par_skip.max(0),
document.par_skip_stretch.max(0),
document.par_skip_shrink.max(0),
bottom,
);
}
@@ -2296,7 +2334,10 @@ impl Engine {
// replaying at the unit's first real item instead.
while first_node < nodes.len()
&& owners[first_node] == unit_index
&& matches!(node_items[first_node], VerticalItem::Glue(_))
&& matches!(
node_items[first_node],
VerticalItem::Glue(_) | VerticalItem::StretchGlue(_)
)
&& !matches!(
document.units[unit_index].items.first(),
Some(VerticalItem::Glue(_))
@@ -2595,6 +2636,8 @@ impl Engine {
next.page_goal,
next.top_skip,
next.par_skip.max(0),
next.par_skip_stretch.max(0),
next.par_skip_shrink.max(0),
bottom,
);
}
@@ -5887,6 +5930,29 @@ mod tests {
assert_eq!(engine.pages()[0].sources, vec![(1, source_hash(""))]);
}
#[test]
fn paragraph_skip_carries_its_stretch_into_pages() {
let first = Unit::paragraph(1, "alpha beta gamma delta epsilon zeta");
let second = Unit::paragraph(2, "eta theta iota kappa lambda mu nu");
let mut document = Document::new(550 * 65_536, vec![first, second]);
document.top_skip = 12 * 65_536;
document.par_skip_stretch = 6 * 65_536;
document.par_skip_shrink = 2 * 65_536;
let engine = Engine::layout_paged(&document).expect("paged layout");
let page = &engine.pages()[0];
assert!(
page.items.iter().any(|item| matches!(
item,
VerticalItem::StretchGlue(glue)
if glue.width == 0
&& glue.stretch == 6 * 65_536
&& glue.shrink == 2 * 65_536
)),
"parskip stretch missing from page items: {:?}",
page.items
);
}
#[test]
fn paged_layout_inserts_par_skip_between_paragraphs() {
let mut style = ParagraphStyle::default();
+2 -1
View File
@@ -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`). 447 workspace tests are green and no oracle
PDF opt-in with `?engine=route-d`). 449 workspace tests are green and no oracle
fixture is ignored.
Known gaps, in dependency order:
@@ -63,6 +63,7 @@ source-level `\hyphenation{...}` exceptions and `\parshape` measures,
with or without `fil`, default `\sfcode` sentence spacing,
`\looseness`, and `\baselineskip`/`\lineskip`/`\parskip` interline
glue), custom `\sfcode` overrides, explicit `\spaceskip`/`\xspaceskip`,
stretch and shrink on the inter-paragraph `\parskip` glue,
`\raggedright` (infinite `\rightskip` fil), explicit
`\hfil`/`\hfill` glue orders, and `\prevgraf` visibility in the output
routine;
+3 -2
View File
@@ -358,7 +358,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 296 unit tests plus seven route-D integration tests and thirty oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 447 tests (296 route-D unit, 7 route-D integration, 30 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 12 binary units), with none ignored. v1 remains the live server path.
The crate currently has 297 unit tests plus seven route-D integration tests and thirty oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 449 tests (297 route-D unit, 7 route-D integration, 30 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 12 binary units), with none ignored. v1 remains the live server path.
### M0 incremental demo
@@ -391,7 +391,8 @@ deliberately open rather than approximated:
`\hyphenation` exceptions, `\hangindent`/`\parshape`, `\parfillskip` with
or without `fil`, `\looseness`, `\baselineskip`/`\lineskip`/`\parskip`
interline glue, the default `\sfcode`/`\frenchspacing` sentence spacing,
explicit `\spaceskip`/`\xspaceskip`, and `\raggedright` (infinite `\rightskip` fil) are modelled.
explicit `\spaceskip`/`\xspaceskip`, stretch and shrink on the
inter-paragraph `\parskip` glue, and `\raggedright` (infinite `\rightskip` fil) are modelled.
- **M2**: the scrolling builder still drives `Document`; `page::PageBuilder`
implements TeX's candidate-breakpoint cost (badness + penalty, glue
stretch/shrink, `\topskip`, `\maxdepth`); a break at glue is only legal
+22 -2
View File
@@ -109,6 +109,8 @@ struct PageGeometry {
textheight: Option<i64>,
par_indent: Option<i64>,
par_skip: Option<i64>,
par_skip_stretch: Option<i64>,
par_skip_shrink: Option<i64>,
baseline_skip: Option<i64>,
left_skip: Option<i64>,
right_skip: Option<i64>,
@@ -161,8 +163,10 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.textheight = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "parindent") {
geometry.par_indent = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "parskip") {
geometry.par_skip = Some(value);
} else if let Some((width, stretch, shrink)) = glue_assignment(trimmed, "parskip") {
geometry.par_skip = Some(width);
geometry.par_skip_stretch = Some(stretch);
geometry.par_skip_shrink = Some(shrink);
} else if let Some(value) = dimension_assignment(trimmed, "baselineskip") {
geometry.baseline_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "leftskip") {
@@ -254,6 +258,12 @@ fn apply_geometry(document: &mut Document, geometry: &PageGeometry) {
if let Some(skip) = geometry.par_skip {
document.par_skip = skip;
}
if let Some(stretch) = geometry.par_skip_stretch {
document.par_skip_stretch = stretch;
}
if let Some(shrink) = geometry.par_skip_shrink {
document.par_skip_shrink = shrink;
}
if let Some(skip) = geometry.top_skip {
document.top_skip = skip;
}
@@ -1064,6 +1074,16 @@ mod source_tests {
assert_eq!(style.lineskip, 2 * SCALE);
assert_eq!(style.lineskiplimit, 3 * SCALE);
}
#[test]
fn geometry_reads_stretchy_parskip() {
let source = "\\documentclass{article}\n\\parskip=2pt plus 5pt minus 1pt\n\\begin{document}\n\nSome words here.\n\n\\end{document}\n";
let (body, geometry) = geometry_and_body(source);
assert!(!body.contains("parskip"));
assert_eq!(geometry.par_skip, Some(2 * 65_536));
assert_eq!(geometry.par_skip_stretch, Some(5 * 65_536));
assert_eq!(geometry.par_skip_shrink, Some(65_536));
}
#[test]
fn geometry_reads_page_skip_registers() {
let source = "\\documentclass{article}\n\\topskip=10pt\n\\maxdepth=5pt\n\\splittopskip=3pt\n\\begin{document}\n\nSome words here.\n\n\\end{document}\n";