Apply e-TeX line penalty arrays to native pagination

This commit is contained in:
gloamxun committed 2026-11-28 13:44:30 +08:00
1 parent 09cd19594c
commit c094d9a81d
12 files changed
+243 -24

No files matched your search

@@ -19,6 +19,11 @@ pub(super) struct ParagraphShapeLine {
pub width: i32,
}
/// A one-based index, extended by the final element of a nonempty array.
pub(super) fn line_penalty_at(values: &[i32], index: usize) -> Option<i32> {
values.get(index.min(values.len()).wrapping_sub(1)).copied()
}
pub(super) fn shaped_line(shape: &[ParagraphShapeLine], line: usize) -> Option<ParagraphShapeLine> {
shape.get(line).or_else(|| shape.last()).copied()
}
@@ -1524,6 +1529,7 @@ pub(super) fn finish(
let mut pre_display_size = -1073741823;
let directions = super::direction::has_directions(&b.nodes);
let mut direction_stack = Vec::new();
let widow_kind = LinePenaltyKind::for_widow_parameter(final_widow);
for (i, (point, bad)) in path.iter().enumerate() {
let mut material = line_material(
&b.nodes,
@@ -1580,25 +1586,23 @@ pub(super) fn finish(
let disc_break = i + 1 < path.len() && point.hyphen;
discretionary_breaks.push(disc_break);
if i + 1 < path.len() {
// TeX post_line_break: all applicable terms add, including both
// club and widow for a two-line paragraph. Never penalize the end.
let get = |p| parameters[&p].width() as i64;
let penalty = get(Parameter::Interlinepenalty)
+ if i == 0 {
get(Parameter::Clubpenalty)
} else {
0
}
+ if i + 2 == path.len() {
get(final_widow)
} else {
0
}
+ if disc_break {
get(Parameter::Brokenpenalty)
} else {
0
};
// The base array uses prevgraf, including lines before a display.
// Club counts from this segment's start and widow from its end.
let from_start = i + 1;
let from_end = path.len() - from_start;
let term = |kind, index, fallback, applies| {
line_penalty_at(state.line_penalties(kind), index)
.map(i64::from)
.unwrap_or_else(|| if applies { i64::from(parameters[&fallback].width()) } else { 0 })
};
let penalty = term(
LinePenaltyKind::Interline,
b.previous_lines + from_start,
Parameter::Interlinepenalty,
true,
) + term(LinePenaltyKind::Club, from_start, Parameter::Clubpenalty, from_start == 1)
+ term(widow_kind, from_end, final_widow, from_end == 1)
+ if disc_break { i64::from(parameters[&Parameter::Brokenpenalty].width()) } else { 0 };
interline_penalties
.push(i32::try_from(penalty).map_err(|_| "native interline penalty sum overflow")?);
}