Apply CMR10 italic corrections at line ends

This commit is contained in:
gloamxun committed 2026-10-20 21:27:39 +08:00
1 parent 7e0090c7a5
commit 060405fca6
2 files changed
+28 -5

No files matched your search

+24 -1
View File
@@ -8,6 +8,9 @@ pub struct HorizontalBox {
pub depth: i64,
/// The source character when this box came from text, for display lists.
pub character: Option<char>,
/// TeX italic correction in scaled points.
#[serde(default)]
pub italic: i64,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
@@ -113,6 +116,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
height: style.line_height,
depth: 0,
character: Some(character),
italic: 0,
}));
}
}
@@ -277,6 +281,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
height: style.line_height,
depth: 0,
character: Some('-'),
italic: 0,
}));
} else if *no_break_width > 0 {
line.push(HorizontalItem::Box(HorizontalBox {
@@ -284,6 +289,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
height: style.line_height,
depth: 0,
character: None,
italic: 0,
}));
}
}
@@ -305,7 +311,11 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
HorizontalItem::Discretionary { .. } => 0,
})
.sum();
max_width = max_width.max(width);
let italic = match line.last() {
Some(HorizontalItem::Box(box_item)) => box_item.italic,
_ => 0,
};
max_width = max_width.max(width + italic);
lines.push(line);
}
Paragraph {
@@ -380,6 +390,7 @@ pub fn text_items_with_font(
height: style.line_height,
depth: 0,
character: Some(character),
italic: font.italic(character),
}));
let boundary = character_index + 1;
if !ligated
@@ -459,6 +470,18 @@ mod tests {
assert!(paragraph.line_count() >= 2);
}
#[test]
fn cmr10_italic_correction_extends_the_line_width() {
let font = crate::cmr10::Cmr10Metrics::default();
let style = ParagraphStyle {
line_width: 400 * 65_536,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font("f", &style, &font);
assert!(paragraph.width > font.width('f'));
assert_eq!(paragraph.width - font.width('f'), font.italic('f'));
}
#[test]
fn cmr10_breaks_match_pdftex_at_180pt() {
let font = crate::cmr10::Cmr10Metrics::new();