//! 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, /// Math face: italic letters from cmmi10, big operators from cmex10. Math, } 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 CMMI10_TFM: &[u8] = include_bytes!("../assets/cmmi10.tfm"); static CMEX10_TFM: &[u8] = include_bytes!("../assets/cmex10.tfm"); static CMSY10_TFM: &[u8] = include_bytes!("../assets/cmsy10.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(); static CMMI10_METRICS: OnceLock = OnceLock::new(); static CMEX10_METRICS: OnceLock = OnceLock::new(); static CMSY10_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 parse_math_italic() -> TfmMetrics { parse(CMMI10_TFM) } fn parse_math_extension() -> TfmMetrics { parse(CMEX10_TFM) } fn parse_math_symbol() -> TfmMetrics { parse(CMSY10_TFM) } fn math_italic_metrics() -> &'static TfmMetrics { CMMI10_METRICS.get_or_init(parse_math_italic) } fn math_extension_metrics() -> &'static TfmMetrics { CMEX10_METRICS.get_or_init(parse_math_extension) } fn math_symbol_metrics() -> &'static TfmMetrics { CMSY10_METRICS.get_or_init(parse_math_symbol) } 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), Cmr10Metrics::Math => CMR10_METRICS.get_or_init(parse_roman), } } 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 { if self.is_math() && character.is_ascii_alphabetic() { let width = math_italic_metrics().width(character); if width > 0 { return width; } } if self.is_math() { if let Some(slot) = math_symbol_slot(character) { let width = math_symbol_metrics().width(char::from(slot)); if width > 0 { return width; } } } let width = self.metrics().width(character); if width > 0 { width } else { 327_680 } } pub fn height(&self, character: char) -> i64 { if self.is_math() && character.is_ascii_alphabetic() { let height = math_italic_metrics().height(character); if height > 0 { return height; } } if self.is_math() { if let Some(slot) = math_symbol_slot(character) { return math_symbol_metrics().height(char::from(slot)); } } self.metrics().height(character) } pub fn depth(&self, character: char) -> i64 { if self.is_math() && character.is_ascii_alphabetic() { let depth = math_italic_metrics().depth(character); if depth > 0 { return depth; } } if self.is_math() { if let Some(slot) = math_symbol_slot(character) { return math_symbol_metrics().depth(char::from(slot)); } } self.metrics().depth(character) } pub fn italic(&self, character: char) -> i64 { if self.is_math() && character.is_ascii_alphabetic() { return math_italic_metrics().italic(character); } if self.is_math() { if let Some(slot) = math_symbol_slot(character) { return math_symbol_metrics().italic(char::from(slot)); } } 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() } } /// Whether this face is the math face. impl Cmr10Metrics { pub const fn is_math(self) -> bool { matches!(self, Self::Math) } } /// Metrics for a named Computer Modern extension big operator (`\sum` and friends). pub fn cmex_operator(name: &str) -> Option<(i64, i64, i64)> { let slot = match name { "sum" => 'P', "prod" => 'Q', "coprod" => 'R', "int" => 'S', "oint" => 'T', "bigcap" => 'U', "bigcup" => 'V', "bigsqcup" => 'W', "bigvee" => 'X', "bigwedge" => 'Y', "bigodot" => 'Z', "bigotimes" => '[', "bigoplus" => '\\', "biguplus" => ']', _ => return None, }; let metrics = math_extension_metrics(); let width = metrics.width(slot); if width == 0 { return None; } Some((width, metrics.height(slot), metrics.depth(slot))) } #[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); } #[test] fn math_face_uses_cmmi_for_letters() { let roman = Cmr10Metrics::Roman; let math = Cmr10Metrics::Math; assert_ne!(math.width('f'), roman.width('f')); assert_eq!(math.width('2'), roman.width('2')); assert!(math.is_math()); } #[test] fn cmex_provides_big_operator_metrics() { let (sum_width, _, sum_depth) = cmex_operator("sum").expect("cmex has a sum glyph"); let (int_width, _, _) = cmex_operator("int").expect("cmex has an integral glyph"); assert!(sum_width > 600_000, "sum width {sum_width}"); assert!(sum_depth > 500_000, "sum depth {sum_depth}"); assert!(int_width < sum_width); assert!(cmex_operator("not-an-operator").is_none()); } } /// Map a Unicode math symbol to its slot in the bundled cmsy10 font. pub fn math_symbol_slot(character: char) -> Option { let slot = match character { '\u{00B7}' => 2, // \cdot '\u{00D7}' => 3, // \times '\u{00F7}' => 5, // \div '\u{00B1}' => 7, // \pm '\u{2213}' => 8, // \mp '\u{2295}' => 9, // \oplus '\u{2296}' => 10, // \ominus '\u{2297}' => 11, // \otimes '\u{2298}' => 12, // \oslash '\u{2299}' => 13, // \odot '\u{224D}' => 17, // \asymp '\u{2261}' => 18, // \equiv '\u{2286}' => 19, // \subseteq '\u{2287}' => 20, // \supseteq '\u{2264}' => 21, // \leq '\u{2265}' => 22, // \geq '\u{2AAF}' => 23, // \preceq '\u{2AB0}' => 24, // \succeq '\u{223C}' => 25, // \sim '\u{2248}' => 26, // \approx '\u{2282}' => 27, // \subset '\u{2283}' => 28, // \supset '\u{226A}' => 29, // \ll '\u{226B}' => 30, // \gg '\u{227A}' => 31, // \prec '\u{227B}' => 32, // \succ '\u{2190}' => 33, // \leftarrow '\u{2192}' => 34, // \rightarrow '\u{2191}' => 35, // \uparrow '\u{2193}' => 36, // \downarrow '\u{2194}' => 37, // \leftrightarrow '\u{2197}' => 38, // \nearrow '\u{2198}' => 39, // \searrow '\u{2243}' => 40, // \simeq '\u{21D0}' => 41, // \Leftarrow '\u{21D2}' => 42, // \Rightarrow '\u{21D1}' => 43, // \Uparrow '\u{21D3}' => 44, // \Downarrow '\u{21D4}' => 45, // \Leftrightarrow '\u{2196}' => 46, // \nwarrow '\u{2199}' => 47, // \swarrow '\u{221D}' => 48, // \propto '\u{2032}' => 49, // \prime '\u{221E}' => 50, // \infty '\u{2208}' => 51, // \in '\u{220B}' => 52, // \ni '\u{25B3}' => 53, // \triangle '\u{25BD}' => 54, // \triangledown '\u{21A6}' => 56, // \mapsto '\u{2200}' => 57, // \forall '\u{2203}' => 58, // \exists '\u{00AC}' => 59, // \neg '\u{2205}' => 60, // \emptyset '\u{211C}' => 61, // \Re '\u{2111}' => 62, // \Im '\u{22A4}' => 63, // \top '\u{22A5}' => 64, // \bot '\u{2135}' => 65, // \aleph '\u{222A}' => 92, // \cup '\u{2229}' => 93, // \cap '\u{228E}' => 94, // \uplus '\u{2227}' => 95, // \wedge '\u{2228}' => 96, // \vee '\u{22A2}' => 97, // \vdash '\u{22A3}' => 98, // \dashv '\u{230A}' => 99, // \lfloor '\u{230B}' => 100, // \rfloor '\u{2308}' => 101, // \lceil '\u{2309}' => 102, // \rceil '\u{007C}' => 107, // \vert '\u{2016}' => 108, // \Vert '\u{2195}' => 109, // \updownarrow '\u{21D5}' => 110, // \Updownarrow '\u{2240}' => 112, // \wr '\u{2210}' => 114, // \coprod '\u{2207}' => 115, // \nabla '\u{222C}' | '\u{222D}' => 117, '\u{2294}' => 117, // \sqcup '\u{2293}' => 118, // \sqcap '\u{00A7}' => 121, // \S '\u{2020}' => 122, // \dagger '\u{2021}' => 123, // \ddagger '\u{00B6}' => 124, // \P '\u{2663}' => 125, // \clubsuit '\u{2662}' => 126, // \diamondsuit '\u{2661}' => 127, // \heartsuit '\u{2660}' => 128, // \spadesuit _ => return None, }; Some(slot) } #[cfg(test)] mod cmsy_tests { use super::*; #[test] fn cmsy_provides_math_symbol_metrics() { let font = Cmr10Metrics::Math; let times = font.width('\u{00D7}'); let slot = math_symbol_metrics().width(char::from(3u8)); assert_eq!(times, slot); assert!(times > 0); let less_equal = font.width('\u{2264}'); assert!(less_equal > 0); assert_ne!(times, less_equal); assert_eq!(Cmr10Metrics::Roman.width('\u{00D7}'), 327_680); } } /// Metrics for a CMEX delimiter size variant: size 1 = `\big`, 2 = `\Big`, /// 3 = `\bigg`, 4 = `\Bigg`. pub fn cmex_delimiter(delimiter: char, size: u8) -> Option<(i64, i64, i64)> { let slot = match (delimiter, size) { ('(', 1) => 1, (')', 1) => 2, ('[', 1) => 3, (']', 1) => 4, ('\u{230A}', 1) => 5, ('\u{230B}', 1) => 6, ('\u{2308}', 1) => 7, ('\u{2309}', 1) => 8, ('{', 1) => 9, ('}', 1) => 10, ('\u{27E8}', 1) => 11, ('\u{27E9}', 1) => 12, ('(', 2) => 17, (')', 2) => 18, ('[', 2) => 105, (']', 2) => 106, ('\u{230A}', 2) => 107, ('\u{230B}', 2) => 108, ('\u{2308}', 2) => 109, ('\u{2309}', 2) => 110, ('{', 2) => 111, ('}', 2) => 112, ('\u{27E8}', 2) => 69, ('\u{27E9}', 2) => 70, ('(', 3) => 19, (')', 3) => 20, ('[', 3) => 21, (']', 3) => 22, ('\u{230A}', 3) => 23, ('\u{230B}', 3) => 24, ('\u{2308}', 3) => 25, ('\u{2309}', 3) => 26, ('{', 3) => 27, ('}', 3) => 28, ('\u{27E8}', 3) => 29, ('\u{27E9}', 3) => 30, ('(', 4) => 33, (')', 4) => 34, ('[', 4) => 35, (']', 4) => 36, ('\u{230A}', 4) => 37, ('\u{230B}', 4) => 38, ('\u{2308}', 4) => 39, ('\u{2309}', 4) => 40, ('{', 4) => 41, ('}', 4) => 42, ('\u{27E8}', 4) => 43, ('\u{27E9}', 4) => 44, _ => return None, }; let metrics = math_extension_metrics(); let code = char::from(slot); // CMEX delimiters sit almost entirely below the baseline; TeX centres them // on the math axis (a quarter em at 10pt) before reporting height/depth. let extent = metrics.height(code) + metrics.depth(code); let axis = 655_360 / 4; Some(( metrics.width(code), extent / 2 + axis, (extent / 2 - axis).max(0), )) } #[cfg(test)] mod cmex_delimiter_tests { use super::*; #[test] fn cmex_delimiters_have_distinct_size_variants() { let big = cmex_delimiter('(', 1).expect("cmex has a big paren"); let big_big = cmex_delimiter('(', 2).expect("cmex has a Big paren"); let bigg = cmex_delimiter('(', 3).expect("cmex has a bigg paren"); let bigg_bigg = cmex_delimiter('(', 4).expect("cmex has a Bigg paren"); assert!(big.0 > 0 && big.1 > 0); assert!(big_big.1 > big.1); assert!(bigg.1 > big_big.1); assert!(bigg_bigg.1 > bigg.1); let extent = |m: (i64, i64, i64)| m.1 + m.2; assert!(extent(big_big) > extent(big)); assert!(extent(bigg) > extent(big_big)); assert!(extent(bigg_bigg) > extent(bigg)); assert!(cmex_delimiter('.', 1).is_none()); assert!(cmex_delimiter('x', 1).is_none()); } }