//! Computer Modern 10pt metrics backed by the real TFM files. use std::sync::OnceLock; use crate::paragraph::ParagraphFont; use crate::tfm::TfmMetrics; /// One Computer Modern text face at 10pt. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum Cmr10Metrics { #[default] Roman, Bold, Italic, BoldItalic, Mono, Sans, } static CMR10_TFM: &[u8] = include_bytes!("../assets/cmr10.tfm"); static CMBX10_TFM: &[u8] = include_bytes!("../assets/cmbx10.tfm"); static CMTI10_TFM: &[u8] = include_bytes!("../assets/cmti10.tfm"); static CMBXTI10_TFM: &[u8] = include_bytes!("../assets/cmbxti10.tfm"); static CMTT10_TFM: &[u8] = include_bytes!("../assets/cmtt10.tfm"); static CMSS10_TFM: &[u8] = include_bytes!("../assets/cmss10.tfm"); static CMR10_METRICS: OnceLock = OnceLock::new(); static CMBX10_METRICS: OnceLock = OnceLock::new(); static CMTI10_METRICS: OnceLock = OnceLock::new(); static CMBXTI10_METRICS: OnceLock = OnceLock::new(); static CMTT10_METRICS: OnceLock = OnceLock::new(); static CMSS10_METRICS: OnceLock = OnceLock::new(); fn parse(bytes: &[u8]) -> TfmMetrics { TfmMetrics::parse(bytes).expect("embedded TFM must be valid") } fn parse_roman() -> TfmMetrics { parse(CMR10_TFM) } fn parse_bold() -> TfmMetrics { parse(CMBX10_TFM) } fn parse_italic() -> TfmMetrics { parse(CMTI10_TFM) } fn parse_bold_italic() -> TfmMetrics { parse(CMBXTI10_TFM) } fn parse_mono() -> TfmMetrics { parse(CMTT10_TFM) } fn parse_sans() -> TfmMetrics { parse(CMSS10_TFM) } fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics { match font { Cmr10Metrics::Roman => CMR10_METRICS.get_or_init(parse_roman), Cmr10Metrics::Bold => CMBX10_METRICS.get_or_init(parse_bold), Cmr10Metrics::Italic => CMTI10_METRICS.get_or_init(parse_italic), Cmr10Metrics::BoldItalic => CMBXTI10_METRICS.get_or_init(parse_bold_italic), Cmr10Metrics::Mono => CMTT10_METRICS.get_or_init(parse_mono), Cmr10Metrics::Sans => CMSS10_METRICS.get_or_init(parse_sans), } } impl Cmr10Metrics { pub const fn new() -> Self { Self::Roman } /// Select the Computer Modern face that corresponds to a paragraph face. pub const fn for_paragraph_font(font: ParagraphFont) -> Self { match font { ParagraphFont::Roman => Self::Roman, ParagraphFont::Bold => Self::Bold, ParagraphFont::Italic => Self::Italic, ParagraphFont::Mono => Self::Mono, ParagraphFont::Sans => Self::Sans, } } /// Select the face by PDF slot: Roman, bold, italic, bold-italic, mono, sans. pub const fn for_pdf_slot(slot: usize) -> Self { match slot { 1 => Self::Bold, 2 => Self::Italic, 3 => Self::BoldItalic, 4 => Self::Mono, 5 => Self::Sans, _ => Self::Roman, } } fn metrics(self) -> &'static TfmMetrics { metrics_for(self) } pub fn width(&self, character: char) -> i64 { let width = self.metrics().width(character); if width > 0 { width } else { 327_680 } } pub fn height(&self, character: char) -> i64 { self.metrics().height(character) } pub fn depth(&self, character: char) -> i64 { self.metrics().depth(character) } pub fn italic(&self, character: char) -> i64 { self.metrics().italic(character) } pub fn ligature(&self, left: char, right: char) -> Option<(char, char)> { self.metrics().ligature(left, right) } pub fn kern(&self, left: char, right: char) -> i64 { self.metrics().kern(left, right) } pub fn space(&self) -> i64 { self.metrics().space() } pub fn stretch(&self) -> i64 { self.metrics().stretch() } pub fn shrink(&self) -> i64 { self.metrics().shrink() } pub fn extra_space(&self) -> i64 { self.metrics().extra_space() } } #[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); assert!(font.height('A') > 0); assert!(font.depth('g') > 0); } #[test] fn bold_and_italic_tfms_differ_from_roman() { let roman = Cmr10Metrics::Roman; let bold = Cmr10Metrics::Bold; let italic = Cmr10Metrics::Italic; assert_ne!(bold.width('m'), roman.width('m')); assert_ne!(italic.width('m'), roman.width('m')); assert!(bold.space() > 0); assert!(italic.height('A') > 0); } }