Model TeX interline glue and parskip

This commit is contained in:
gloamxun committed 2026-10-23 01:36:26 +08:00
1 parent 8cb09ea5a8
commit 49bd9b63e6
8 files changed
+200 -32

No files matched your search

+82
View File
@@ -98,6 +98,15 @@ pub struct ParagraphStyle {
/// punctuation adds no sentence spacing.
#[serde(default)]
pub frenchspacing: bool,
/// TeX `\baselineskip` (12pt for 10pt text).
#[serde(default = "default_baselineskip")]
pub baselineskip: i64,
/// TeX `\lineskip`, used when `\baselineskip` would be too tight.
#[serde(default = "default_lineskip")]
pub lineskip: i64,
/// TeX `\lineskiplimit`: below this interline distance use `\lineskip`.
#[serde(default)]
pub lineskiplimit: i64,
/// Center each line in the measure instead of justifying it.
#[serde(default)]
pub centered: bool,
@@ -152,6 +161,14 @@ fn default_par_fillskip_fil() -> bool {
true
}
fn default_baselineskip() -> i64 {
12 * 65_536
}
fn default_lineskip() -> i64 {
65_536
}
impl ParagraphStyle {
/// `(indent, measure)` for a zero-based line number, honouring
/// `\parshape` and `\hangindent`/`\hangafter`.
@@ -211,6 +228,9 @@ impl Default for ParagraphStyle {
par_fillskip_stretch: 0,
par_fillskip_fil: true,
frenchspacing: false,
baselineskip: 12 * 65_536,
lineskip: 65_536,
lineskiplimit: 0,
centered: false,
right_aligned: false,
par_indent: 0,
@@ -261,6 +281,9 @@ pub struct Paragraph {
/// Left offset for each line, from `\hangindent`/`\parshape`.
#[serde(default)]
pub line_indents: Vec<i64>,
/// Baseline of each line relative to the top of the paragraph box.
#[serde(default)]
pub line_baselines: Vec<i64>,
#[serde(default)]
pub centered: bool,
#[serde(default)]
@@ -325,6 +348,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
font: ParagraphFont::Roman,
left_skip: style.left_skip,
line_indents: Vec::new(),
line_baselines: Vec::new(),
centered: style.centered,
right_aligned: style.right_aligned,
};
@@ -798,18 +822,53 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
let line_indents: Vec<i64> = (0..lines.len())
.map(|line| style.line_geometry(line).0 + style.par_indent_for(line))
.collect();
let extents: Vec<(i64, i64)> = lines.iter().map(|line| line_extent(line)).collect();
let mut line_baselines = Vec::with_capacity(extents.len());
let mut height = 0;
if let Some((first_ascent, _)) = extents.first().copied() {
let mut baseline = first_ascent;
line_baselines.push(baseline);
for index in 1..extents.len() {
let (ascent, _) = extents[index];
let (_, previous_descent) = extents[index - 1];
let natural = style.baselineskip - previous_descent - ascent;
let distance = if natural >= style.lineskiplimit {
style.baselineskip
} else {
previous_descent + ascent + style.lineskip
};
baseline += distance;
line_baselines.push(baseline);
}
}
Paragraph {
// The page builder still budgets paragraphs on the line grid; the
// baseline table below carries TeX's interline glue for painting.
height: lines.len() as i64 * style.line_height,
width: max_width,
lines,
font: ParagraphFont::Roman,
left_skip: style.left_skip,
line_indents,
line_baselines,
centered: style.centered,
right_aligned: style.right_aligned,
}
}
/// `(ascent, descent)` of the tallest box on a line.
fn line_extent(line: &[HorizontalItem]) -> (i64, i64) {
let mut ascent = 0;
let mut descent = 0;
for item in line {
if let HorizontalItem::Box(box_) = item {
ascent = ascent.max(box_.height);
descent = descent.max(box_.depth);
}
}
(ascent, descent)
}
/// TeX Live's American English hyphenation patterns (`hyph-en-us`, 2005),
/// which are what pdfLaTeX loads for `\language` 0 with the `english` option.
/// The vendored plain-TeX patterns differ (for example `kappa`), so route-D
@@ -1429,6 +1488,29 @@ mod tests {
assert_eq!(shrink, font.shrink() * 1000 / 3000);
}
#[test]
fn baselines_use_baselineskip_then_lineskip() {
let font = crate::cmr10::Cmr10Metrics::new();
let mut style = ParagraphStyle {
line_width: font.width('a') * 3,
line_height: 12 * 65_536,
..ParagraphStyle::default()
};
let first = layout_paragraph_with_font("a b c", &style, &font);
assert_eq!(first.lines.len(), 2);
assert_eq!(
first.line_baselines[1] - first.line_baselines[0],
12 * 65_536
);
style.lineskiplimit = 100 * 65_536;
let second = layout_paragraph_with_font("a b c", &style, &font);
assert_eq!(
second.line_baselines[1] - second.line_baselines[0],
font.height('a') + 65_536
);
}
#[test]
fn frenchspacing_keeps_a_plain_space() {
let style = ParagraphStyle {