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

+68
View File
@@ -0,0 +1,68 @@
// Computer Modern Roman 10pt character widths, extracted from cmr10.tfm
// with tftopl. Values are scaled points at 10pt design size.
#[derive(Clone, Debug)]
pub struct Cmr10Metrics {}
impl Default for Cmr10Metrics {
fn default() -> Self {
Self::new()
}
}
impl Cmr10Metrics {
pub const fn new() -> Self {
Self {}
}
pub fn width(&self, character: char) -> i64 {
self.widths()
.get(character as usize)
.copied()
.unwrap_or(327_680)
}
pub fn space(&self) -> i64 {
218_454
}
pub fn stretch(&self) -> i64 {
109_227
}
pub fn shrink(&self) -> i64 {
72_818
}
fn widths(&self) -> &'static [i64; 128] {
&CMR10_WIDTHS
}
}
const CMR10_WIDTHS: [i64; 128] = [
409601, 546135, 509727, 455112, 436908, 491521, 473317, 509727, 473317, 509727, 473317, 382295,
364090, 364090, 546135, 546135, 182045, 200250, 327681, 327681, 327681, 327681, 327681, 491521,
291272, 327682, 473317, 509727, 327681, 591647, 664464, 509727, 182045, 182045, 327681, 546135,
327681, 546135, 509727, 182045, 254863, 254863, 327681, 509727, 182045, 218454, 182045, 327681,
327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 327681, 182045, 182045,
182045, 509727, 309477, 309477, 509727, 491521, 464215, 473317, 500624, 446010, 427807, 514277,
491521, 236658, 336783, 509727, 409601, 600748, 491521, 509727, 446010, 509727, 482419, 364090,
473317, 491521, 491521, 673567, 491521, 491521, 400499, 182045, 327681, 182045, 327681, 182045,
182045, 327681, 364090, 291272, 364090, 291272, 200250, 327681, 364090, 182045, 200250, 345887,
182045, 546135, 364090, 327681, 364090, 345885, 256684, 258503, 254863, 364090, 345887, 473317,
345887, 345887, 291272, 327681, 655362, 327681, 327681, 327681,
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_cmr10_widths() {
let font = Cmr10Metrics::new();
assert!(font.width('A') > 450_000);
assert!((font.width('a') - 327_681).abs() <= 2);
assert!((font.space() - 218_454).abs() <= 2);
assert!(font.stretch() > 0);
assert!(font.shrink() > 0);
}
}
+6 -1
View File
@@ -1,6 +1,11 @@
pub mod cmr10;
pub mod paragraph;
pub use paragraph::{HorizontalBox, HorizontalItem, Paragraph, ParagraphStyle, layout_paragraph};
pub use cmr10::Cmr10Metrics;
pub use paragraph::{
HorizontalBox, HorizontalItem, Paragraph, ParagraphStyle, layout_items, layout_paragraph,
layout_paragraph_with_font, text_items, text_items_with_font,
};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
+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);