Substitute CMR10 ligatures in route-D paragraphs

This commit is contained in:
gloamxun committed 2026-10-20 21:04:24 +08:00
1 parent 641fc1c14b
commit ac63a3db92
4 files changed
+84 -10

No files matched your search

+40 -3
View File
@@ -347,7 +347,24 @@ pub fn text_items_with_font(
} else {
std::collections::HashSet::new()
};
for (character_index, character) in word.chars().enumerate() {
let original: Vec<char> = word.chars().collect();
let mut resolved: Vec<(char, char)> = Vec::new();
let mut cursor = 0;
while cursor < original.len() {
if cursor + 1 < original.len() {
if let Some((unicode, slot)) = font.ligature(original[cursor], original[cursor + 1])
{
resolved.push((unicode, slot));
cursor += 2;
continue;
}
}
let character = original[cursor];
resolved.push((character, character));
cursor += 1;
}
let ligated = resolved.len() != original.len();
for (character_index, (character, slot)) in resolved.into_iter().enumerate() {
if let (Some(index), Some(left)) = (previous_box, previous_character) {
let kern = font.kern(left, character);
if kern != 0 {
@@ -359,13 +376,17 @@ pub fn text_items_with_font(
previous_box = Some(items.len());
previous_character = Some(character);
items.push(HorizontalItem::Box(HorizontalBox {
width: font.width(character),
width: font.width(slot),
height: style.line_height,
depth: 0,
character: Some(character),
}));
let boundary = character_index + 1;
if points.contains(&boundary) && boundary >= 2 && boundary + 2 <= word.chars().count() {
if !ligated
&& points.contains(&boundary)
&& boundary >= 2
&& boundary + 2 <= word.chars().count()
{
items.push(HorizontalItem::Discretionary {
pre_break_width: font.width('-'),
no_break_width: 0,
@@ -508,6 +529,22 @@ mod tests {
assert!(paragraph.lines.len() > 1);
}
#[test]
fn fi_ligature_reduces_character_boxes() {
let font = crate::cmr10::Cmr10Metrics::new();
let style = ParagraphStyle::default();
let items = text_items_with_font("fi", &style, &font);
let boxes: Vec<_> = items
.iter()
.filter_map(|item| match item {
HorizontalItem::Box(box_) => Some(box_),
_ => None,
})
.collect();
assert_eq!(boxes.len(), 1);
assert_eq!(boxes[0].character, Some('\u{FB01}'));
}
#[test]
fn cmr10_font_drives_character_widths() {
let font = crate::cmr10::Cmr10Metrics::new();