diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 683ae70..08ac74f 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -178,6 +178,9 @@ pub struct Halign { pub width: i64, pub height: i64, pub line_height: i64, + /// Metrics used for natural cell widths; keeps the CMR defaults. + #[serde(default)] + pub font: Option, /// Nested tables painted inside a cell: (row, cell, table). #[serde(default)] pub nested: Vec<(usize, usize, Halign)>, @@ -346,6 +349,7 @@ impl Halign { width: self.width, height, line_height: self.line_height, + font: self.font, }); first_chunk = false; if last { @@ -387,6 +391,7 @@ impl Halign { width: self.width, height, line_height: self.line_height, + font: self.font, }); start = index; height = 0; @@ -414,6 +419,7 @@ impl Halign { width: self.width, height, line_height: self.line_height, + font: self.font, }); chunks } @@ -580,7 +586,10 @@ impl Halign { for cell in &row.cells { let span = cell.span.max(1).min(column_count.saturating_sub(column)); if span == 1 { - let width = crate::text_width(&cell.text); + let width = match self.font { + Some(font) => crate::text_width_with_font(&cell.text, font), + None => crate::text_width(&cell.text), + }; if let Some(slot) = widths.get_mut(column) { *slot = (*slot).max(width); } @@ -624,6 +633,12 @@ impl Halign { .sum(); } + /// Select the text metrics used for table column widths. + pub fn set_font(&mut self, font: crate::paragraph::ParagraphFont) { + self.font = Some(font); + self.refresh_measurements(); + } + /// Uniform intercolumn spacing derived from the table width. fn uniform_intercolumn(&self) -> i64 { if self.column_widths.len() > 1 { @@ -685,7 +700,12 @@ impl Halign { /// Natural width of a run of text in the default 10pt Roman face. pub(crate) fn text_width(text: &str) -> i64 { - let metrics = crate::cmr10::Cmr10Metrics::Roman; + text_width_with_font(text, crate::paragraph::ParagraphFont::Roman) +} + +/// Natural width of a run of text in a selectable paragraph face. +pub(crate) fn text_width_with_font(text: &str, font: crate::paragraph::ParagraphFont) -> i64 { + let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(font); text.chars() .map(|character| { if character.is_whitespace() { @@ -1291,6 +1311,7 @@ impl Unit { width, height: 0, line_height, + font: None, }; halign.height = (0..halign.rows.len()) .map(|index| halign.row_height(index) + halign.row_gap(index)) diff --git a/docs/TEX_FIDELITY_BENCHMARK.md b/docs/TEX_FIDELITY_BENCHMARK.md index 71bf3f7..08063b7 100755 --- a/docs/TEX_FIDELITY_BENCHMARK.md +++ b/docs/TEX_FIDELITY_BENCHMARK.md @@ -34,7 +34,7 @@ reference_words 1243, candidate_words 1262 word_sequence_ratio 0.8958 page 1 ink bbox: TeX [72, 79, 540, 721] route-D [71, 79, 540, 724] page 2 ink bbox: TeX [72, 36, 540, 748] route-D [71, 41, 540, 751] -page 3 ink bbox: TeX [72, 36, 540, 748] route-D [71, 41, 553, 751] +page 3 ink bbox: TeX [72, 36, 540, 748] route-D [71, 41, 540, 751] page ink fraction: TeX 0.0416/0.0448/0.0432, route-D 0.0434/0.0492/0.0434 ``` diff --git a/src/route_d.rs b/src/route_d.rs index 5049f0a..d9af7b4 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -4269,6 +4269,11 @@ fn tabular_unit( 12, Vec::new(), ); + if geometry.roman_family.as_deref() == Some("ptm") + && let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() + { + halign.set_font(texrra_tex_core::paragraph::ParagraphFont::TimesRoman); + } let stretch = geometry.array_stretch.unwrap_or(1000); if separator != 6 * SCALE || stretch != 1000 { if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {