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);
|
||||
}
|
||||
}
|
||||
@@ -652,6 +652,93 @@ fn parse_delimiter(chars: &[char], index: usize) -> (char, usize) {
|
||||
}
|
||||
}
|
||||
|
||||
fn math_symbol_command(name: &str) -> Option<char> {
|
||||
let character = match name {
|
||||
"cdot" => '\u{00B7}',
|
||||
"times" => '\u{00D7}',
|
||||
"div" => '\u{00F7}',
|
||||
"pm" => '\u{00B1}',
|
||||
"mp" => '\u{2213}',
|
||||
"oplus" => '\u{2295}',
|
||||
"ominus" => '\u{2296}',
|
||||
"otimes" => '\u{2297}',
|
||||
"oslash" => '\u{2298}',
|
||||
"odot" => '\u{2299}',
|
||||
"asymp" => '\u{224D}',
|
||||
"equiv" => '\u{2261}',
|
||||
"subseteq" => '\u{2286}',
|
||||
"supseteq" => '\u{2287}',
|
||||
"leq" | "le" => '\u{2264}',
|
||||
"geq" | "ge" => '\u{2265}',
|
||||
"preceq" => '\u{2AAF}',
|
||||
"succeq" => '\u{2AB0}',
|
||||
"sim" => '\u{223C}',
|
||||
"approx" => '\u{2248}',
|
||||
"subset" => '\u{2282}',
|
||||
"supset" => '\u{2283}',
|
||||
"ll" => '\u{226A}',
|
||||
"gg" => '\u{226B}',
|
||||
"prec" => '\u{227A}',
|
||||
"succ" => '\u{227B}',
|
||||
"leftarrow" | "gets" => '\u{2190}',
|
||||
"rightarrow" | "to" => '\u{2192}',
|
||||
"uparrow" => '\u{2191}',
|
||||
"downarrow" => '\u{2193}',
|
||||
"leftrightarrow" => '\u{2194}',
|
||||
"nearrow" => '\u{2197}',
|
||||
"searrow" => '\u{2198}',
|
||||
"simeq" => '\u{2243}',
|
||||
"Leftarrow" => '\u{21D0}',
|
||||
"Rightarrow" => '\u{21D2}',
|
||||
"Uparrow" => '\u{21D1}',
|
||||
"Downarrow" => '\u{21D3}',
|
||||
"Leftrightarrow" => '\u{21D4}',
|
||||
"nwarrow" => '\u{2196}',
|
||||
"swarrow" => '\u{2199}',
|
||||
"propto" => '\u{221D}',
|
||||
"prime" => '\u{2032}',
|
||||
"infty" => '\u{221E}',
|
||||
"in" => '\u{2208}',
|
||||
"ni" => '\u{220B}',
|
||||
"triangle" => '\u{25B3}',
|
||||
"triangledown" => '\u{25BD}',
|
||||
"mapsto" => '\u{21A6}',
|
||||
"forall" => '\u{2200}',
|
||||
"exists" => '\u{2203}',
|
||||
"neg" | "lnot" => '\u{00AC}',
|
||||
"emptyset" | "varnothing" => '\u{2205}',
|
||||
"Re" => '\u{211C}',
|
||||
"Im" => '\u{2111}',
|
||||
"top" => '\u{22A4}',
|
||||
"bot" => '\u{22A5}',
|
||||
"aleph" => '\u{2135}',
|
||||
"cup" => '\u{222A}',
|
||||
"cap" => '\u{2229}',
|
||||
"uplus" => '\u{228E}',
|
||||
"wedge" | "land" => '\u{2227}',
|
||||
"vee" | "lor" => '\u{2228}',
|
||||
"vdash" => '\u{22A2}',
|
||||
"dashv" => '\u{22A3}',
|
||||
"updownarrow" => '\u{2195}',
|
||||
"Updownarrow" => '\u{21D5}',
|
||||
"wr" => '\u{2240}',
|
||||
"coprod" => '\u{2210}',
|
||||
"nabla" => '\u{2207}',
|
||||
"sqcup" => '\u{2294}',
|
||||
"sqcap" => '\u{2293}',
|
||||
"S" => '\u{00A7}',
|
||||
"dagger" => '\u{2020}',
|
||||
"ddagger" => '\u{2021}',
|
||||
"P" => '\u{00B6}',
|
||||
"clubsuit" => '\u{2663}',
|
||||
"diamondsuit" => '\u{2662}',
|
||||
"heartsuit" => '\u{2661}',
|
||||
"spadesuit" => '\u{2660}',
|
||||
_ => return None,
|
||||
};
|
||||
Some(character)
|
||||
}
|
||||
|
||||
fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
let mut index = index;
|
||||
while chars.get(index) == Some(&'\u{0}') {
|
||||
@@ -1143,7 +1230,10 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
||||
after,
|
||||
)
|
||||
}
|
||||
_ => (MathAtom::Symbol('?'), next),
|
||||
other => match math_symbol_command(other) {
|
||||
Some(character) => (MathAtom::Symbol(character), next),
|
||||
None => (MathAtom::Symbol('?'), next),
|
||||
},
|
||||
};
|
||||
}
|
||||
(MathAtom::Symbol(character), index + 1)
|
||||
@@ -1739,3 +1829,26 @@ mod tests {
|
||||
assert!(matches!(&atoms[2], MathAtom::Sqrt { .. }));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod cmsy_symbol_tests {
|
||||
use super::*;
|
||||
use crate::cmr10::Cmr10Metrics;
|
||||
|
||||
#[test]
|
||||
fn symbol_commands_use_cmsy_metrics() {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let atom = parse_math(r"\times");
|
||||
let MathAtom::Row(row) = &atom else {
|
||||
panic!("atom: {atom:?}");
|
||||
};
|
||||
assert!(
|
||||
matches!(row.first(), Some(MathAtom::Symbol('\u{00D7}'))),
|
||||
"row: {row:?}"
|
||||
);
|
||||
let laid_out = layout_math(&atom, &font);
|
||||
assert_eq!(laid_out.width, font.width('\u{00D7}'));
|
||||
assert!(laid_out.width > 0);
|
||||
assert_ne!(laid_out.width, Cmr10Metrics::Roman.width('\u{00D7}'));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user