Turn TeX hyphen runs into en and em dashes

This commit is contained in:
gloamxun committed 2026-10-21 01:08:22 +08:00
1 parent ff97f386da
commit 95760e4a1c
2 files changed
+35 -1

No files matched your search

+34
View File
@@ -353,6 +353,10 @@ pub fn text_items_with_font(
let mut items = Vec::new();
let hyphenator = hyphenate::Hyphenator::plain_tex_en_us();
let caser = hyphenate::AsciiLowerCaser::default();
// TeX's text fonts turn runs of hyphens into en/em dashes. cmr10's TFM
// does not carry these ligatures, so apply the same mapping before
// shaping the paragraph.
let text = text.replace("---", "\u{2014}").replace("--", "\u{2013}");
let words: Vec<&str> = text.split_whitespace().collect();
let mut previous_box: Option<usize> = None;
let mut previous_character: Option<char> = None;
@@ -569,6 +573,36 @@ mod tests {
assert!(paragraph.lines.len() > 1);
}
#[test]
fn double_hyphen_becomes_one_en_dash_box() {
let font = crate::cmr10::Cmr10Metrics::new();
let style = ParagraphStyle::default();
let items = text_items_with_font("--", &style, &font);
let boxes: Vec<_> = items
.iter()
.filter_map(|item| match item {
HorizontalItem::Box(horizontal) => horizontal.character,
_ => None,
})
.collect();
assert_eq!(boxes, vec!['\u{2013}']);
}
#[test]
fn triple_hyphen_becomes_one_em_dash_box() {
let font = crate::cmr10::Cmr10Metrics::new();
let style = ParagraphStyle::default();
let items = text_items_with_font("---", &style, &font);
let boxes: Vec<_> = items
.iter()
.filter_map(|item| match item {
HorizontalItem::Box(horizontal) => horizontal.character,
_ => None,
})
.collect();
assert_eq!(boxes, vec!['\u{2014}']);
}
#[test]
fn fi_ligature_reduces_character_boxes() {
let font = crate::cmr10::Cmr10Metrics::new();