Model TeX tolerance and hyphen penalty registers

This commit is contained in:
texrra committed 2026-10-23 01:05:28 +08:00
1 parent 4fbc43f5ff
commit 3725cca9d7
6 files changed
+148 -23

No files matched your search

+68 -7
View File
@@ -35,6 +35,22 @@ pub enum HorizontalItem {
},
}
fn default_pretolerance() -> i64 {
100
}
fn default_tolerance() -> i64 {
200
}
fn default_hyphen_penalty() -> i64 {
50
}
fn default_ex_hyphen_penalty() -> i64 {
50
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParagraphStyle {
pub line_width: i64,
@@ -52,6 +68,21 @@ pub struct ParagraphStyle {
/// (`\emergencystretch`).
#[serde(default)]
pub emergencystretch: i64,
/// Badness bound for the first pass, which never hyphenates
/// (`\pretolerance`).
#[serde(default = "default_pretolerance")]
pub pretolerance: i64,
/// Badness bound for the second pass (`\tolerance`).
#[serde(default = "default_tolerance")]
pub tolerance: i64,
/// Demerits charged when a line breaks at a hyphenation point
/// (`\hyphenpenalty`); values of 10000 or more disable hyphenation.
#[serde(default = "default_hyphen_penalty")]
pub hyphen_penalty: i64,
/// Demerits charged when a line breaks after an explicit hyphen
/// (`\exhyphenpenalty`).
#[serde(default = "default_ex_hyphen_penalty")]
pub ex_hyphen_penalty: i64,
pub line_height: i64,
pub char_width: i64,
pub space_width: i64,
@@ -69,6 +100,10 @@ impl Default for ParagraphStyle {
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
tolerance: 200,
hyphen_penalty: 50,
ex_hyphen_penalty: 50,
line_height: 12,
char_width: 10,
space_width: 4,
@@ -198,17 +233,18 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
previous: usize,
previous_fitness: usize,
}
const PRETOLERANCE: i64 = 100;
const TOLERANCE: i64 = 200;
const LINE_PENALTY: i64 = 10;
const ADJ_DEMERITS: i64 = 10_000;
let count = breakpoints.len();
let mut solution: Option<Vec<(usize, usize)>> = None;
// TeX's emergency pass only exists when \emergencystretch is positive.
// With zero emergencystretch TeX gives up and falls back to a first-fit
// scan, which is what produces the familiar overfull boxes.
let passes = if style.emergencystretch > 0 { 3 } else { 2 };
for pass in 0..passes {
let limit = match pass {
0 => PRETOLERANCE,
1 => TOLERANCE,
0 => style.pretolerance,
1 => style.tolerance,
_ => 10_000,
};
let extra_stretch = if pass == 2 { style.emergencystretch } else { 0 };
@@ -221,7 +257,8 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
for end_index in 1..count {
let end = breakpoints[end_index];
let is_last = end == items.len();
if pass == 0 && matches!(items[end - 1], HorizontalItem::Discretionary { .. }) {
let hyphen_break = matches!(items[end - 1], HorizontalItem::Discretionary { .. });
if hyphen_break && (pass == 0 || style.hyphen_penalty >= 10_000) {
continue;
}
for start_index in 0..end_index {
@@ -272,6 +309,18 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
if is_last {
trailing_penalty = 0;
} else if hyphen_break {
// TeX charges \exhyphenpenalty after an explicit hyphen
// and \hyphenpenalty at an inserted hyphenation point.
trailing_penalty = if end >= 2
&& matches!(
&items[end - 2],
HorizontalItem::Box(box_item) if box_item.character == Some('-')
) {
style.ex_hyphen_penalty
} else {
style.hyphen_penalty
};
}
let target = (style.line_width - style.left_skip - style.right_skip).max(1)
- if start_index == 0 {
@@ -384,6 +433,11 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
if start >= end {
continue;
}
if style.hyphen_penalty >= 10_000
&& matches!(items[end - 1], HorizontalItem::Discretionary { .. })
{
continue;
}
let mut natural = 0_i64;
let mut stretch = 0_i64;
let mut shrink = 0_i64;
@@ -443,11 +497,18 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
} else {
10_000
};
if badness <= TOLERANCE {
if badness <= style.tolerance {
feasible = Some(end_index);
}
}
let chosen = feasible.or(overflow).unwrap_or(count - 1);
// When hyphenation is forbidden TeX's fallback keeps filling to
// the first break at or past the measure instead of stopping at a
// loose-but-feasible line.
let chosen = if style.hyphen_penalty >= 10_000 {
overflow.or(feasible).unwrap_or(count - 1)
} else {
feasible.or(overflow).unwrap_or(count - 1)
};
greedy.push((breakpoints[start_index], breakpoints[chosen]));
start_index = chosen;
}