Use CMR10 metrics in the M1 paragraph builder

This commit is contained in:
gloamxun committed 2026-10-20 12:20:49 +08:00
1 parent e6da4d2fa7
commit bf5dae3797
5 files changed
+150 -8

No files matched your search

+67
View File
@@ -129,6 +129,9 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
let mut best = vec![i64::MAX; count];
let mut previous = vec![0_usize; count];
best[0] = 0;
if style.line_width == 150 * 65_536 {
eprintln!("BP {:?}", breakpoints);
}
for end_index in 1..count {
let end = breakpoints[end_index];
for start_index in 0..end_index {
@@ -203,6 +206,9 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
}
}
if style.line_width == 150 * 65_536 {
eprintln!("BEST {:?} PREV {:?}", best, previous);
}
let mut ranges = Vec::new();
let mut end_index = count - 1;
while end_index > 0 {
@@ -239,6 +245,51 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
}
}
/// Convert text using TFM character widths and fontdimen glue parameters.
pub fn text_items_with_font(
text: &str,
style: &ParagraphStyle,
font: &crate::cmr10::Cmr10Metrics,
) -> Vec<HorizontalItem> {
let mut items = Vec::new();
for character in text.chars() {
if character.is_whitespace() {
if !matches!(items.last(), Some(HorizontalItem::Glue { .. })) {
items.push(HorizontalItem::Glue {
width: font.space(),
stretch: font.stretch(),
shrink: font.shrink(),
});
}
} else {
items.push(HorizontalItem::Box(HorizontalBox {
width: font.width(character),
height: style.line_height,
depth: 0,
}));
}
}
while matches!(items.last(), Some(HorizontalItem::Glue { .. })) {
items.pop();
}
items.push(HorizontalItem::Penalty {
width: 0,
penalty: 10_000,
flagged: false,
});
items
}
/// Paragraph builder driven by a real TFM font instead of fixed widths.
pub fn layout_paragraph_with_font(
text: &str,
style: &ParagraphStyle,
font: &crate::cmr10::Cmr10Metrics,
) -> Paragraph {
let items = text_items_with_font(text, style, font);
layout_items(&items, style)
}
pub fn layout_paragraph(text: &str, style: &ParagraphStyle) -> Paragraph {
let items = text_items(text, style);
layout_items(&items, style)
@@ -283,6 +334,22 @@ mod tests {
assert!(paragraph.line_count() >= 2);
}
#[test]
fn cmr10_font_drives_character_widths() {
let font = crate::cmr10::Cmr10Metrics::new();
let style = ParagraphStyle {
line_width: 150 * 65_536,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font(
"The quick brown fox jumps over the lazy dog",
&style,
&font,
);
assert!(paragraph.line_count() >= 2);
assert!(paragraph.height > 0);
}
#[test]
fn empty_paragraph_has_no_lines() {
assert_eq!(layout_paragraph("", &ParagraphStyle::default()).height, 0);