Files
texrra-engine/crates/texrra-tex-core/src/cmr10.rs
T
gloamxun aa4d86d735 Distinguish slanted from italic and add italic typewriter
Add cmsl10/cmbxsl10/cmitt10 faces so \slshape and \textsl no longer reuse the italic cmti10 and \ttfamily\itshape reaches cmitt10. The NFSS state tracks italic, slanted and small caps separately, with last-shape-wins semantics for \em and the shape commands.
2026-10-24 12:58:41 +08:00

760 lines
25 KiB
Rust

//! 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,
SansBold,
SansItalic,
/// Roman small caps (cmcsc10).
SmallCaps,
/// Roman slanted (cmsl10).
Slanted,
/// Bold extended slanted (cmbxsl10).
BoldSlanted,
/// Italic typewriter (cmitt10).
MonoItalic,
/// Calligraphic capitals from cmsy10 (`\mathcal`).
MathCalligraphic,
/// Blackboard-bold capitals from msbm10 (`\mathbb`).
MathBlackboard,
/// Euler Fraktur letters from eufm10 (`\mathfrak`).
MathFraktur,
/// 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 CMSSBX10_TFM: &[u8] = include_bytes!("../assets/cmssbx10.tfm");
static CMSSI10_TFM: &[u8] = include_bytes!("../assets/cmssi10.tfm");
static CMCSC10_TFM: &[u8] = include_bytes!("../assets/cmcsc10.tfm");
static CMSL10_TFM: &[u8] = include_bytes!("../assets/cmsl10.tfm");
static CMBXSL10_TFM: &[u8] = include_bytes!("../assets/cmbxsl10.tfm");
static CMITT10_TFM: &[u8] = include_bytes!("../assets/cmitt10.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 MSBM10_TFM: &[u8] = include_bytes!("../assets/msbm10.tfm");
static EUFM10_TFM: &[u8] = include_bytes!("../assets/eufm10.tfm");
static CMR10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMTI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMBXTI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMTT10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMSS10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMSSBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMSSI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMCSC10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMSL10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMBXSL10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMITT10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMMI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMEX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static CMSY10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static MSBM10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
static EUFM10_METRICS: OnceLock<TfmMetrics> = 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_sans_bold() -> TfmMetrics {
parse(CMSSBX10_TFM)
}
fn parse_sans_italic() -> TfmMetrics {
parse(CMSSI10_TFM)
}
fn parse_small_caps() -> TfmMetrics {
parse(CMCSC10_TFM)
}
fn parse_slanted() -> TfmMetrics {
parse(CMSL10_TFM)
}
fn parse_bold_slanted() -> TfmMetrics {
parse(CMBXSL10_TFM)
}
fn parse_mono_italic() -> TfmMetrics {
parse(CMITT10_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 parse_blackboard() -> TfmMetrics {
parse(MSBM10_TFM)
}
fn math_blackboard_metrics() -> &'static TfmMetrics {
MSBM10_METRICS.get_or_init(parse_blackboard)
}
fn parse_fraktur() -> TfmMetrics {
parse(EUFM10_TFM)
}
fn math_fraktur_metrics() -> &'static TfmMetrics {
EUFM10_METRICS.get_or_init(parse_fraktur)
}
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::SansBold => CMSSBX10_METRICS.get_or_init(parse_sans_bold),
Cmr10Metrics::SansItalic => CMSSI10_METRICS.get_or_init(parse_sans_italic),
Cmr10Metrics::SmallCaps => CMCSC10_METRICS.get_or_init(parse_small_caps),
Cmr10Metrics::Slanted => CMSL10_METRICS.get_or_init(parse_slanted),
Cmr10Metrics::BoldSlanted => CMBXSL10_METRICS.get_or_init(parse_bold_slanted),
Cmr10Metrics::MonoItalic => CMITT10_METRICS.get_or_init(parse_mono_italic),
Cmr10Metrics::Math => CMR10_METRICS.get_or_init(parse_roman),
Cmr10Metrics::MathCalligraphic => CMSY10_METRICS.get_or_init(parse_math_symbol),
Cmr10Metrics::MathBlackboard => MSBM10_METRICS.get_or_init(parse_blackboard),
Cmr10Metrics::MathFraktur => EUFM10_METRICS.get_or_init(parse_fraktur),
}
}
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::BoldItalic => Self::BoldItalic,
ParagraphFont::Mono => Self::Mono,
ParagraphFont::Sans => Self::Sans,
ParagraphFont::SansBold => Self::SansBold,
ParagraphFont::SansItalic => Self::SansItalic,
ParagraphFont::SmallCaps => Self::SmallCaps,
ParagraphFont::Slanted => Self::Slanted,
ParagraphFont::BoldSlanted => Self::BoldSlanted,
ParagraphFont::MonoItalic => Self::MonoItalic,
}
}
/// 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,
6 => Self::Math,
7 => Self::Math,
8 => Self::Math,
9 => Self::MathBlackboard,
10 => Self::MathFraktur,
11 => Self::SansBold,
12 => Self::SansItalic,
13 => Self::SmallCaps,
14 => Self::Slanted,
15 => Self::BoldSlanted,
16 => Self::MonoItalic,
_ => Self::Roman,
}
}
fn metrics(self) -> &'static TfmMetrics {
metrics_for(self)
}
pub fn width(&self, character: char) -> i64 {
if matches!(
*self,
Self::MathCalligraphic | Self::MathBlackboard | Self::MathFraktur
) {
let capital_only = *self != Self::MathFraktur;
if character.is_ascii_uppercase() || (!capital_only && character.is_ascii_lowercase()) {
return self.metrics().width(character);
}
return Self::Math.width(character);
}
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;
}
}
if let Some(slot) = math_extension_slot(character) {
let width = math_extension_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 matches!(
*self,
Self::MathCalligraphic | Self::MathBlackboard | Self::MathFraktur
) {
let capital_only = *self != Self::MathFraktur;
if character.is_ascii_uppercase() || (!capital_only && character.is_ascii_lowercase()) {
return self.metrics().height(character);
}
return Self::Math.height(character);
}
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));
}
if let Some(slot) = math_extension_slot(character) {
return math_extension_metrics().height(char::from(slot));
}
}
self.metrics().height(character)
}
pub fn depth(&self, character: char) -> i64 {
if matches!(
*self,
Self::MathCalligraphic | Self::MathBlackboard | Self::MathFraktur
) {
let capital_only = *self != Self::MathFraktur;
if character.is_ascii_uppercase() || (!capital_only && character.is_ascii_lowercase()) {
return self.metrics().depth(character);
}
return Self::Math.depth(character);
}
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));
}
if let Some(slot) = math_extension_slot(character) {
return math_extension_metrics().depth(char::from(slot));
}
}
self.metrics().depth(character)
}
pub fn italic(&self, character: char) -> i64 {
if matches!(
*self,
Self::MathCalligraphic | Self::MathBlackboard | Self::MathFraktur
) {
let capital_only = *self != Self::MathFraktur;
if character.is_ascii_uppercase() || (!capital_only && character.is_ascii_lowercase()) {
return self.metrics().italic(character);
}
return Self::Math.italic(character);
}
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));
}
if let Some(slot) = math_extension_slot(character) {
return math_extension_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" => 80,
"prod" => 81,
"coprod" => 96,
"int" => 82,
"oint" => 72,
"bigcap" => 84,
"bigcup" => 83,
"bigsqcup" => 85,
"bigvee" => 87,
"bigwedge" => 86,
"bigodot" => 74,
"bigotimes" => 78,
"bigoplus" => 76,
"biguplus" => 85,
_ => return None,
};
let metrics = math_extension_metrics();
let width = metrics.width(char::from(slot));
if width == 0 {
return None;
}
Some((
width,
metrics.height(char::from(slot)),
metrics.depth(char::from(slot)),
))
}
/// Map a Unicode n-ary operator to its slot in the bundled cmex10 font.
pub fn math_extension_slot(character: char) -> Option<u8> {
let slot = match character {
'\u{2211}' => 80, // \sum
'\u{220F}' => 81, // \prod
'\u{2210}' => 96, // \coprod
'\u{222B}' => 82, // \int
'\u{222E}' => 72, // \oint
'\u{22C2}' => 84, // \bigcap
'\u{22C3}' => 83, // \bigcup
'\u{2A06}' => 85, // \bigsqcup
'\u{22C1}' => 87, // \bigvee
'\u{22C0}' => 86, // \bigwedge
'\u{2A00}' => 74, // \bigodot
'\u{2A02}' => 78, // \bigotimes
'\u{2A01}' => 76, // \bigoplus
_ => return None,
};
Some(slot)
}
/// Advance width of a `cmsy10` glyph addressed by a Unicode math character.
pub fn math_symbol_width(character: char) -> Option<i64> {
if character.is_ascii_uppercase() {
let width = math_symbol_metrics().width(character);
if width > 0 {
return Some(width);
}
}
math_symbol_slot(character)
.map(|slot| math_symbol_metrics().width(char::from(slot)))
.filter(|width| *width > 0)
}
/// Advance width of a `msbm10` blackboard-bold capital.
pub fn math_blackboard_width(character: char) -> Option<i64> {
if character.is_ascii_uppercase() {
let width = math_blackboard_metrics().width(character);
if width > 0 {
return Some(width);
}
}
None
}
/// Advance width of an `eufm10` Fraktur letter.
pub fn math_fraktur_width(character: char) -> Option<i64> {
if character.is_ascii_alphabetic() {
let width = math_fraktur_metrics().width(character);
if width > 0 {
return Some(width);
}
}
None
}
/// Advance width of a `cmex10` glyph addressed by a Unicode n-ary operator.
pub fn math_extension_width(character: char) -> Option<i64> {
math_extension_slot(character)
.map(|slot| math_extension_metrics().width(char::from(slot)))
.filter(|width| *width > 0)
}
#[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<u8> {
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());
}
#[test]
fn sans_bold_and_italic_use_their_own_metrics() {
let regular = Cmr10Metrics::Sans.width('A');
let bold = Cmr10Metrics::SansBold.width('A');
let italic = Cmr10Metrics::SansItalic.width('A');
assert_eq!(bold, metrics_for(Cmr10Metrics::SansBold).width('A'));
assert_eq!(italic, metrics_for(Cmr10Metrics::SansItalic).width('A'));
assert_ne!(regular, bold);
assert_ne!(Cmr10Metrics::Sans.space(), Cmr10Metrics::SansBold.space());
assert!(italic > 0);
assert_eq!(Cmr10Metrics::for_pdf_slot(11), Cmr10Metrics::SansBold);
assert_eq!(Cmr10Metrics::for_pdf_slot(12), Cmr10Metrics::SansItalic);
assert_eq!(Cmr10Metrics::for_pdf_slot(13), Cmr10Metrics::SmallCaps);
let small_caps = Cmr10Metrics::SmallCaps.width('a');
assert_eq!(small_caps, metrics_for(Cmr10Metrics::SmallCaps).width('a'));
assert_ne!(small_caps, Cmr10Metrics::Roman.width('a'));
assert_eq!(Cmr10Metrics::for_pdf_slot(14), Cmr10Metrics::Slanted);
assert_eq!(Cmr10Metrics::for_pdf_slot(15), Cmr10Metrics::BoldSlanted);
assert_eq!(Cmr10Metrics::for_pdf_slot(16), Cmr10Metrics::MonoItalic);
let slanted = Cmr10Metrics::Slanted.width('a');
assert_eq!(slanted, metrics_for(Cmr10Metrics::Slanted).width('a'));
assert_ne!(slanted, Cmr10Metrics::Italic.width('a'));
assert_eq!(
Cmr10Metrics::BoldSlanted.width('a'),
metrics_for(Cmr10Metrics::BoldSlanted).width('a')
);
assert_eq!(
Cmr10Metrics::MonoItalic.width('a'),
metrics_for(Cmr10Metrics::MonoItalic).width('a')
);
}
}