Apply CMR10 kern pairs in route-D paragraphs

This commit is contained in:
gloamxun committed 2026-10-20 20:52:54 +08:00
1 parent a09398811f
commit 867bace1c9
4 files changed
+99 -5

No files matched your search

+31
View File
@@ -327,6 +327,8 @@ pub fn text_items_with_font(
let hyphenator = hyphenate::Hyphenator::plain_tex_en_us();
let caser = hyphenate::AsciiLowerCaser::default();
let words: Vec<&str> = text.split_whitespace().collect();
let mut previous_box: Option<usize> = None;
let mut previous_character: Option<char> = None;
for (word_index, word) in words.iter().enumerate() {
if word_index > 0 {
items.push(HorizontalItem::Glue {
@@ -346,6 +348,16 @@ pub fn text_items_with_font(
std::collections::HashSet::new()
};
for (character_index, character) in word.chars().enumerate() {
if let (Some(index), Some(left)) = (previous_box, previous_character) {
let kern = font.kern(left, character);
if kern != 0 {
if let Some(HorizontalItem::Box(previous)) = items.get_mut(index) {
previous.width += kern;
}
}
}
previous_box = Some(items.len());
previous_character = Some(character);
items.push(HorizontalItem::Box(HorizontalBox {
width: font.width(character),
height: style.line_height,
@@ -512,6 +524,25 @@ mod tests {
assert!(paragraph.height > 0);
}
#[test]
fn cmr10_kerning_reduces_adjacent_character_width() {
let font = crate::cmr10::Cmr10Metrics::new();
assert!(font.kern('A', 'V') < 0);
let style = ParagraphStyle::default();
let items = text_items_with_font("AV", &style, &font);
let total: i64 = items
.iter()
.map(|item| match item {
HorizontalItem::Box(box_) => box_.width,
HorizontalItem::Glue { width, .. } => *width,
_ => 0,
})
.sum();
assert_eq!(
total,
font.width('A') + font.width('V') + font.kern('A', 'V')
);
}
#[test]
fn left_skip_narrows_the_measure_and_offsets_the_paragraph() {
let narrow = ParagraphStyle {