Replace font glue with explicit spaceskip and xspaceskip
This commit is contained in:
1 parent
4acccaee86
commit
4be0ff5787
7 files changed
+161
-25
No files matched your search
@@ -577,7 +577,8 @@ edit demo. M1 adds the horizontal box/glue/penalty model, an item-stream Knuth-P
|
||||
paragraph builder, CMR10 metrics extracted from `cmr10.tfm`, and a matching pdfTeX line-break fixture. Paragraphs are split at line
|
||||
boundaries when they cross a page, and the pdfLaTeX oracle covers the
|
||||
100-240pt measure matrix plus register, `\hangindent`/`\parshape`,
|
||||
`\parfillskip`, sentence-spacing with custom `\sfcode`, `\looseness` and vertical-glue fixtures. Overflowing
|
||||
`\parfillskip`, sentence-spacing with custom `\sfcode`,
|
||||
`\spaceskip`/`\xspaceskip`, `\looseness` and vertical-glue fixtures. Overflowing
|
||||
insertion material is split at item boundaries, with the tail held over to the
|
||||
next page. The production v1 engine remains the
|
||||
default until the new core reaches layout parity. See `docs/TEX_CORE.md`. The complete phased plan (P0 oracle harness, TeX-exact paragraphs,
|
||||
|
||||
@@ -159,6 +159,14 @@ pub struct ParagraphStyle {
|
||||
pub space_width: i64,
|
||||
pub space_stretch: i64,
|
||||
pub space_shrink: i64,
|
||||
/// TeX `\spaceskip`: explicit interword glue `(width, stretch, shrink)`
|
||||
/// that replaces the font glue when set.
|
||||
#[serde(default)]
|
||||
pub space_skip: Option<(i64, i64, i64)>,
|
||||
/// TeX `\xspaceskip`: glue used instead of `\spaceskip` once the space
|
||||
/// factor reaches 2000.
|
||||
#[serde(default)]
|
||||
pub xspace_skip: Option<(i64, i64, i64)>,
|
||||
}
|
||||
|
||||
fn default_par_fillskip_fil() -> bool {
|
||||
@@ -254,6 +262,8 @@ impl Default for ParagraphStyle {
|
||||
space_width: 4,
|
||||
space_stretch: 6,
|
||||
space_shrink: 2,
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -947,33 +957,68 @@ pub fn text_items_with_font(
|
||||
for (word_index, word) in words.iter().enumerate() {
|
||||
if word_index > 0 {
|
||||
let factor = space_factor.max(1000);
|
||||
// TeX's `\spaceskip`/`\xspaceskip` replace the font's interword
|
||||
// glue; the space factor still scales stretch and shrink, and
|
||||
// `\fontdimen7` is still added from factor 2000 on.
|
||||
// TeX uses `\xspaceskip` verbatim once the space factor reaches
|
||||
// 2000 (pdfTeX dumps it as 8.0 plus 4.0 minus 2.0 even at 3000).
|
||||
// `\spaceskip` keeps its width but its stretch/shrink are scaled
|
||||
// by the space factor like normal font glue; fontdimen7 is never
|
||||
// added when an explicit skip is in force.
|
||||
let (width, stretch, shrink) = if factor >= 2000
|
||||
&& let Some((width, stretch, shrink)) = style.xspace_skip
|
||||
{
|
||||
(width, stretch, shrink)
|
||||
} else if let Some((width, stretch, shrink)) = style.space_skip {
|
||||
(width, stretch * factor / 1000, shrink * 1000 / factor)
|
||||
} else {
|
||||
(
|
||||
font.space()
|
||||
+ if factor >= 2000 {
|
||||
font.extra_space()
|
||||
} else {
|
||||
0
|
||||
},
|
||||
font.stretch() * factor / 1000,
|
||||
font.shrink() * 1000 / factor,
|
||||
)
|
||||
};
|
||||
items.push(HorizontalItem::Glue {
|
||||
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,
|
||||
width,
|
||||
stretch,
|
||||
shrink,
|
||||
});
|
||||
// TeX never kerns across interword glue.
|
||||
previous_box = None;
|
||||
previous_character = None;
|
||||
}
|
||||
let points: std::collections::HashSet<usize> = if word.is_ascii()
|
||||
&& word
|
||||
.chars()
|
||||
let original: Vec<char> = word.chars().collect();
|
||||
// TeX hyphenates the letter run of a word: leading or trailing
|
||||
// punctuation stays on the word and does not suppress the pattern
|
||||
// lookup (for example `epsilon:` can still break as `ep-`).
|
||||
let lead = original
|
||||
.iter()
|
||||
.take_while(|character| !character.is_ascii_alphabetic())
|
||||
.count();
|
||||
let trail = original
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|character| !character.is_ascii_alphabetic())
|
||||
.count();
|
||||
let core_len = original.len().saturating_sub(lead + trail);
|
||||
let points: std::collections::HashSet<usize> = if core_len >= 5
|
||||
&& original[lead..lead + core_len]
|
||||
.iter()
|
||||
.all(|character| character.is_ascii_alphabetic())
|
||||
&& word.len() >= 5
|
||||
{
|
||||
hyphenator.calculate_indices(&caser, word).collect()
|
||||
let core: String = original[lead..lead + core_len].iter().collect();
|
||||
hyphenator
|
||||
.calculate_indices(&caser, &core)
|
||||
.map(|boundary| boundary + lead)
|
||||
.collect()
|
||||
} else {
|
||||
std::collections::HashSet::new()
|
||||
};
|
||||
let original: Vec<char> = word.chars().collect();
|
||||
let mut resolved: Vec<(char, char)> = Vec::new();
|
||||
let mut cursor = 0;
|
||||
while cursor < original.len() {
|
||||
@@ -1014,7 +1059,7 @@ pub fn text_items_with_font(
|
||||
if !ligated
|
||||
&& points.contains(&boundary)
|
||||
&& boundary >= 2
|
||||
&& boundary + 3 <= word.chars().count()
|
||||
&& boundary + 3 <= lead + core_len
|
||||
{
|
||||
items.push(HorizontalItem::Discretionary {
|
||||
pre_break_width: font.width('-'),
|
||||
@@ -1586,6 +1631,55 @@ mod tests {
|
||||
assert_eq!(width, font.space());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_spaceskip_replaces_the_font_glue() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
let style = ParagraphStyle {
|
||||
space_skip: Some((6 * 65_536, 65_536, 32_768)),
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
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, 6 * 65_536);
|
||||
assert_eq!(stretch, 65_536);
|
||||
assert_eq!(shrink, 32_768);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xspaceskip_applies_after_sentence_punctuation() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
let style = ParagraphStyle {
|
||||
space_skip: Some((6 * 65_536, 65_536, 32_768)),
|
||||
xspace_skip: Some((8 * 65_536, 4 * 65_536, 2 * 65_536)),
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
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, 8 * 65_536);
|
||||
assert_eq!(stretch, 4 * 65_536);
|
||||
assert_eq!(shrink, 2 * 65_536);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn baselines_use_baselineskip_then_lineskip() {
|
||||
let font = crate::cmr10::Cmr10Metrics::new();
|
||||
|
||||
@@ -363,6 +363,25 @@ fn sentence_spacing_matches_pdftex_at_150pt() {
|
||||
assert_lines_match("sentence spacing 150pt", &ours, &reference);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spaceskip_matches_pdftex() {
|
||||
let prefix = "\\spaceskip=6pt plus 1pt minus 0.5pt\\relax ";
|
||||
compare_fixture_with(WORDS, 150, prefix, |style| {
|
||||
style.space_skip = Some((6 * 65_536, 65_536, 32_768));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xspaceskip_matches_pdftex() {
|
||||
let text = "alpha. beta gamma, delta; epsilon: zeta eta theta iota kappa lambda mu";
|
||||
let prefix = "\\spaceskip=6pt plus 1pt minus 0.5pt\\relax \
|
||||
\\xspaceskip=8pt plus 4pt minus 2pt\\relax ";
|
||||
compare_fixture_with(text, 150, prefix, |style| {
|
||||
style.space_skip = Some((6 * 65_536, 65_536, 32_768));
|
||||
style.xspace_skip = Some((8 * 65_536, 4 * 65_536, 2 * 65_536));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_sfcode_matches_pdftex() {
|
||||
let width = 150;
|
||||
|
||||
@@ -43,6 +43,8 @@ fn body_style() -> ParagraphStyle {
|
||||
space_width: font.space(),
|
||||
space_stretch: font.stretch(),
|
||||
space_shrink: font.shrink(),
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,6 +212,8 @@ fn route_d_page_matches_pdftex_raster_geometry() {
|
||||
space_width: 4 * 65_536,
|
||||
space_stretch: 6 * 65_536,
|
||||
space_shrink: 2 * 65_536,
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
};
|
||||
let document = Document::new(
|
||||
700 * 65_536,
|
||||
@@ -386,6 +390,8 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
|
||||
space_width: 4 * 65_536,
|
||||
space_stretch: 6 * 65_536,
|
||||
space_shrink: 2 * 65_536,
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
};
|
||||
let document = Document::new(
|
||||
20 * 65_536,
|
||||
@@ -688,6 +694,8 @@ fn route_d_multiline_bands_track_pdftex() {
|
||||
space_width: 4 * 65_536,
|
||||
space_stretch: 6 * 65_536,
|
||||
space_shrink: 2 * 65_536,
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
};
|
||||
let document = Document::new(
|
||||
700 * 65_536,
|
||||
|
||||
@@ -59,7 +59,8 @@ Known gaps, in dependency order:
|
||||
`\hangindent`/`\hangafter`/`\parshape` line geometry, `\parfillskip`
|
||||
with or without `fil`, default `\sfcode` sentence spacing,
|
||||
`\looseness`, and `\baselineskip`/`\lineskip`/`\parskip` interline
|
||||
glue), custom `\sfcode` overrides, but not yet generic `fil`/`fill` glue
|
||||
glue), custom `\sfcode` overrides, explicit `\spaceskip`/`\xspaceskip`, but not
|
||||
yet generic `fil`/`fill` glue
|
||||
orders or `\prevgraf`;
|
||||
2. the default builder is still the scrolling builder with fraction
|
||||
policies, now with line-level paragraph splits; `page::PageBuilder`
|
||||
@@ -193,6 +194,10 @@ 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.
|
||||
|
||||
`\spaceskip` and `\xspaceskip` replace the interword glue; pdfTeX dumps
|
||||
`\xspaceskip` verbatim above factor 2000 while `\spaceskip` keeps its width
|
||||
but scales stretch/shrink, and 150pt oracle fixtures pin both.
|
||||
|
||||
`\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
|
||||
|
||||
+13
-6
@@ -90,7 +90,11 @@ 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, 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
|
||||
justified like any other line (the default `0pt plus 1fil` leaves it short).
|
||||
Two 150pt fixtures pin explicit interword glue: `\spaceskip` replaces the
|
||||
font glue (width kept, stretch/shrink following the space factor) and
|
||||
`\xspaceskip` is used verbatim once the factor reaches 2000; neither adds
|
||||
`\fontdimen7`. `route_d_multiline_bands_track_pdftex` rasterizes the same
|
||||
paragraph in both engines and compares ink bands, covering baseline placement
|
||||
and justification.
|
||||
|
||||
@@ -109,7 +113,7 @@ and justification.
|
||||
| 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 stream, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing, `\looseness`, `\baselineskip`/`\lineskip`/`\parskip` interline glue, pdfLaTeX oracle matrix 100-240pt plus register, geometry, `\sfcode` and vertical-glue fixtures; generic fil glue and `\prevgraf` next |
|
||||
| M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing, `\spaceskip`/`\xspaceskip`, `\looseness`, `\baselineskip`/`\lineskip`/`\parskip` interline glue, pdfLaTeX oracle matrix 100-240pt plus register, geometry, `\sfcode` and vertical-glue fixtures; generic fil glue and `\prevgraf` next |
|
||||
| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top/bottom `Float` placement; `page::PageBuilder` scores candidate breakpoints with TeX page badness, glue setting, `\topskip`/`\maxdepth`, and `Engine::layout_paged` uses it for documents whose units are single vertical items, while the scrolling builder remains the default; interpreted `\output` next |
|
||||
| M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | **partial**: source parsing, natural column widths, spans, rule rows, 10pt display-list painting and row-level page splitting; no booktabs/longtable parity yet |
|
||||
| M4 | math lists, scripts, fractions, radicals, accents, delimiters | **partial**: token parser, atom boxes with CMR10 metrics, delimiter scaling, accents and a display-list painter; no CMEX size variants or TeX math spacing classes yet |
|
||||
@@ -131,7 +135,10 @@ and justification.
|
||||
by it, `\fontdimen7` is added from factor 2000, and `\frenchspacing`
|
||||
disables the effect. `ParagraphStyle::space_factor_codes` carries explicit
|
||||
`\sfcode` assignments, which replace the built-in table for that character;
|
||||
a zero code leaves the running factor unchanged, as in TeX. `\looseness` shifts the paragraph by whole lines when
|
||||
a zero code leaves the running factor unchanged, as in TeX. Explicit
|
||||
`\spaceskip`/`\xspaceskip` replace the font glue, and hyphenation applies
|
||||
to a word's letter core so trailing punctuation does not suppress the
|
||||
pattern lookup (pdfTeX breaks `epsilon:` as `ep-`). `\looseness` shifts the paragraph by whole lines when
|
||||
the requested count lies between one and nine lines away from the natural
|
||||
count (the adjustment is reverted when no feasible layout exists).
|
||||
Interline glue follows TeX: each baseline advances by `\baselineskip` when
|
||||
@@ -282,7 +289,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, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing with default and custom `\sfcode`/`\frenchspacing`, `\baselineskip`/`\lineskip`/`\parskip` interline glue, pdfTeX oracle matrix 100-240pt plus register, geometry and vertical-glue fixtures |
|
||||
| M1 horizontal/paragraphs | partial | boxes/glue/penalties, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing with default and custom `\sfcode`/`\frenchspacing`, explicit `\spaceskip`/`\xspaceskip`, `\baselineskip`/`\lineskip`/`\parskip` interline glue, pdfTeX oracle matrix 100-240pt plus register, geometry and vertical-glue fixtures |
|
||||
| M2 vertical/page builder | partial | vsize, topskip, insertions, marks, Rust output hook, top/bottom floats, float-page/text fractions, rules, vskip, penalties; `page::PageBuilder` scores breakpoints with page badness and penalties and models `\topskip`/`\maxdepth`; `Engine::layout_paged` builds pages through it for single-item documents, while floats, insertions and incremental replay still use the scrolling path; 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 |
|
||||
@@ -352,8 +359,8 @@ deliberately open rather than approximated:
|
||||
`\prevgraf` are still open; the tolerance, penalty and demerit registers,
|
||||
`\hyphenation` exceptions, `\hangindent`/`\parshape`, `\parfillskip` with
|
||||
or without `fil`, `\looseness`, `\baselineskip`/`\lineskip`/`\parskip`
|
||||
interline glue, and the default `\sfcode`/`\frenchspacing` sentence spacing
|
||||
are modelled.
|
||||
interline glue, the default `\sfcode`/`\frenchspacing` sentence spacing,
|
||||
and explicit `\spaceskip`/`\xspaceskip` are modelled.
|
||||
- **M2**: the scrolling builder still drives `Document`; `page::PageBuilder`
|
||||
implements TeX's candidate-breakpoint cost (badness + penalty, glue
|
||||
stretch/shrink, `\topskip`, `\maxdepth`) and `Engine::layout_paged` runs it
|
||||
|
||||
@@ -417,6 +417,8 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
|
||||
space_width: 4 * SCALE,
|
||||
space_stretch: 6 * SCALE,
|
||||
space_shrink: 2 * SCALE,
|
||||
space_skip: None,
|
||||
xspace_skip: None,
|
||||
};
|
||||
let mut unit = Unit::paragraph_with_style(index as u64, paragraph.text.clone(), style);
|
||||
if let Some(VerticalItem::Paragraph(paragraph_box)) = unit.items.first_mut() {
|
||||
|
||||
Reference in new issue
Block a user