Use cmsy10 symbol metrics in route-D formulas
This commit is contained in:
1 parent
8f675bcabd
commit
03afc828f3
5 files changed
+263
-4
No files matched your search
@@ -27,6 +27,7 @@ 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<TfmMetrics> = OnceLock::new();
|
||||
static CMBX10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
@@ -36,6 +37,7 @@ 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();
|
||||
static CMSY10_METRICS: OnceLock<TfmMetrics> = OnceLock::new();
|
||||
|
||||
fn parse(bytes: &[u8]) -> TfmMetrics {
|
||||
TfmMetrics::parse(bytes).expect("embedded TFM must be valid")
|
||||
@@ -73,6 +75,10 @@ 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)
|
||||
}
|
||||
@@ -81,6 +87,10 @@ 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),
|
||||
@@ -132,6 +142,14 @@ impl Cmr10Metrics {
|
||||
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 }
|
||||
}
|
||||
@@ -143,6 +161,11 @@ impl Cmr10Metrics {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -153,6 +176,11 @@ impl Cmr10Metrics {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -160,6 +188,11 @@ impl Cmr10Metrics {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -268,3 +301,116 @@ mod tests {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user