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();
+1 -1
View File
@@ -346,7 +346,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
The crate currently has 273 unit tests plus seven route-D integration tests and seventeen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 397 tests (273 route-D unit, 7 route-D integration, 17 oracle, 71 v1 integration, 22 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
The crate currently has 275 unit tests plus seven route-D integration tests and seventeen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 399 tests (275 route-D unit, 7 route-D integration, 17 oracle, 71 v1 integration, 22 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
### M0 incremental demo