Use Computer Modern math metrics for route-D formulas
This commit is contained in:
1 parent
c9ad1e209a
commit
8f675bcabd
7 files changed
+108
-14
No files matched your search
@@ -15,6 +15,8 @@ pub enum Cmr10Metrics {
|
||||
BoldItalic,
|
||||
Mono,
|
||||
Sans,
|
||||
/// Math face: italic letters from cmmi10, big operators from cmex10.
|
||||
Math,
|
||||
}
|
||||
|
||||
static CMR10_TFM: &[u8] = include_bytes!("../assets/cmr10.tfm");
|
||||
@@ -23,6 +25,8 @@ 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 CMR10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
@@ -30,6 +34,8 @@ 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 CMMI10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
static CMEX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
|
||||
fn parse(bytes: &[u8]) -> TfmMetrics {
|
||||
TfmMetrics::parse(bytes).expect("embedded TFM must be valid")
|
||||
@@ -59,6 +65,22 @@ fn parse_sans() -> TfmMetrics {
|
||||
parse(CMSS10_TFM)
|
||||
}
|
||||
|
||||
fn parse_math_italic() -> TfmMetrics {
|
||||
parse(CMMI10_TFM)
|
||||
}
|
||||
|
||||
fn parse_math_extension() -> TfmMetrics {
|
||||
parse(CMEX10_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 metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
match font {
|
||||
Cmr10Metrics::Roman => CMR10_METRICS.get_or_init(parse_roman),
|
||||
@@ -67,6 +89,7 @@ fn metrics_for(font: Cmr10Metrics) -> &'static TfmMetrics {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,19 +126,40 @@ impl Cmr10Metrics {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
self.metrics().italic(character)
|
||||
}
|
||||
|
||||
@@ -144,6 +188,40 @@ impl Cmr10Metrics {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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::*;
|
||||
@@ -170,4 +248,23 @@ mod tests {
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user