Cover final and double hyphen demerits

This commit is contained in:
gloamxun committed 2026-10-23 12:36:36 +08:00
1 parent 54a8488fa2
commit 140e1b4c50
2 files changed
+100 -1

No files matched your search

+99
View File
@@ -1403,6 +1403,105 @@ mod tests {
assert_eq!(paragraph.line_count(), 3);
}
fn line_ends_with_hyphen(line: &[HorizontalItem]) -> bool {
matches!(line.last(), Some(HorizontalItem::Box(item)) if item.character == Some('-'))
}
#[test]
fn final_hyphen_demerits_suppress_a_penultimate_hyphen() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let text = "hyphenation demonstration algorithm parameter memory hyphenation \
demonstration algorithm parameter memory";
let mut observed = false;
let mut suppressed = false;
// An emergency pass keeps a dynamic-programming solution alive; without
// it the greedy fallback decides and ignores demerits entirely.
for width in (30..=90).step_by(2) {
let base = ParagraphStyle {
line_width: width * 65_536,
line_height: 12 * 65_536,
emergencystretch: 10 * 65_536,
..ParagraphStyle::default()
};
let penalised = ParagraphStyle {
final_hyphen_demerits: 1_000_000_000_000_000,
..base.clone()
};
let base_lines = layout_paragraph_with_font(text, &base, &font);
let strict_lines = layout_paragraph_with_font(text, &penalised, &font);
let base_hyphens: Vec<bool> = base_lines
.lines
.iter()
.map(|line| line_ends_with_hyphen(line))
.collect();
let strict_hyphens: Vec<bool> = strict_lines
.lines
.iter()
.map(|line| line_ends_with_hyphen(line))
.collect();
let base_penultimate = base_hyphens.iter().rev().nth(1).copied().unwrap_or(false);
let strict_penultimate = strict_hyphens.iter().rev().nth(1).copied().unwrap_or(false);
if base_penultimate {
observed = true;
if !strict_penultimate {
suppressed = true;
}
}
}
assert!(observed, "fixture never hyphenated the penultimate line");
assert!(
suppressed,
"final_hyphen_demerits never suppressed a penultimate hyphen"
);
}
#[test]
fn double_hyphen_demerits_suppress_consecutive_hyphenated_lines() {
let font = crate::cmr10::Cmr10Metrics::Roman;
let text = "hyphenation demonstration algorithm parameter memory hyphenation \
demonstration algorithm parameter memory";
let mut observed = false;
let mut avoided = false;
// An emergency pass keeps a dynamic-programming solution alive; without
// it the greedy fallback decides and ignores demerits entirely.
for width in (30..=90).step_by(2) {
let base = ParagraphStyle {
line_width: width * 65_536,
line_height: 12 * 65_536,
emergencystretch: 10 * 65_536,
..ParagraphStyle::default()
};
let penalised = ParagraphStyle {
double_hyphen_demerits: 1_000_000_000_000_000,
..base.clone()
};
let base_lines = layout_paragraph_with_font(text, &base, &font);
let strict_lines = layout_paragraph_with_font(text, &penalised, &font);
let base_hyphens: Vec<bool> = base_lines
.lines
.iter()
.map(|line| line_ends_with_hyphen(line))
.collect();
let strict_hyphens: Vec<bool> = strict_lines
.lines
.iter()
.map(|line| line_ends_with_hyphen(line))
.collect();
let consecutive = |flags: &[bool]| flags.windows(2).any(|pair| pair[0] && pair[1]);
if consecutive(&base_hyphens) {
observed = true;
if !consecutive(&strict_hyphens) {
avoided = true;
}
}
}
assert!(observed, "fixture never hyphenated two consecutive lines");
assert!(
avoided,
"double_hyphen_demerits never removed consecutive hyphenated lines"
);
}
#[test]
fn hyphenation_inserts_discretionary_breaks() {
let font = crate::cmr10::Cmr10Metrics::default();