use std::collections::HashMap;
pub(crate) fn is_logical_symbol(symbol: &str) -> bool {
matches!(symbol, "∴" | "∵")
}
pub(crate) fn is_binary_symbol(symbol: &str) -> bool {
matches!(symbol, "+" | "-" | "−" | "±" | "∓" | "×" | "÷" | "∙" | "⋅")
}
pub(crate) fn is_relation_symbol(symbol: &str) -> bool {
matches!(
symbol,
"=" | ":"
| "<"
| ">"
| "≤"
| "≥"
| "≠"
| "≈"
| "≡"
| "∼"
| "∝"
| "⊂"
| "⊃"
| "⊆"
| "⊇"
| "⊥"
| "⊤"
| "∥"
| "⫽"
)
}
use std::sync::LazyLock;
static GREEK_LOWER: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
HashMap::from([
("alpha", "α"),
("beta", "β"),
("gamma", "γ"),
("delta", "δ"),
("epsilon", "ε"),
("varepsilon", "ϵ"),
("zeta", "ζ"),
("eta", "η"),
("theta", "θ"),
("vartheta", "ϑ"),
("iota", "ι"),
("kappa", "κ"),
("lambda", "λ"),
("mu", "μ"),
("nu", "ν"),
("xi", "ξ"),
("omicron", "ο"),
("pi", "π"),
("varpi", "ϖ"),
("rho", "ρ"),
("varrho", "ϱ"),
("sigma", "σ"),
("varsigma", "ς"),
("tau", "τ"),
("upsilon", "υ"),
("phi", "φ"),
("varphi", "ϕ"),
("chi", "χ"),
("psi", "ψ"),
("omega", "ω"),
])
});
static GREEK_UPPER: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
HashMap::from([
("Alpha", "Α"),
("Beta", "Β"),
("Gamma", "Γ"),
("Delta", "Δ"),
("Epsilon", "Ε"),
("Zeta", "Ζ"),
("Eta", "Η"),
("Theta", "Θ"),
("Iota", "Ι"),
("Kappa", "Κ"),
("Lambda", "Λ"),
("Mu", "Μ"),
("Nu", "Ν"),
("Xi", "Ξ"),
("Omicron", "Ο"),
("Pi", "Π"),
("Rho", "Ρ"),
("Sigma", "Σ"),
("Tau", "Τ"),
("Upsilon", "Υ"),
("varupsilon", "ϒ"),
("Phi", "Φ"),
("Chi", "Χ"),
("Psi", "Ψ"),
("Omega", "Ω"),
])
});
static SPECIAL_SYMBOLS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
HashMap::from([
("INF", "∞"),
("ALEPH", "ℵ"),
("HBAR", "ℏ"),
("IMATH", "ı"),
("JMATH", "ȷ"),
("ELL", "ℓ"),
("LITER", "ℓ"),
("WP", "℘"),
("IMAG", "ℑ"),
("image", "ℑ"),
("REIMAGE", "ℜ"),
("ANGSTROM", "Å"),
("MHO", "℧"),
("OHM", "Ω"),
("CDOTS", "⋯"),
("LDOTS", "…"),
("VDOTS", "⋮"),
("DDOTS", "⋱"),
("DOTS", "…"),
("QUAD", "\u{2003}"),
("QQUAD", "\u{2003}\u{2003}"),
("THINSPACE", "\u{2009}"),
("MEDSPACE", "\u{205F}"),
("THICKSPACE", "\u{2004}"),
("NEGSPACE", ""),
("ENSPACE", "\u{2002}"),
("TRIANGLE", "△"),
("TRIANGLED", "▽"),
("ANGLE", "∠"),
("MSANGLE", "∡"),
("SANGLE", "∢"),
("RTANGLE", "⊾"),
("BOT", "⊥"),
("TOP", "⊤"),
("LAPLACE", "ℒ"),
("CENTIGRADE", "℃"),
("FAHRENHEIT", "℉"),
("DEG", "°"),
("prime", "′"),
("LSLANT", "/"),
("RSLANT", "\\"),
("ATT", "@"),
("HUND", "‰"),
("THOU", "‱"),
("WELL", "♯"),
("BASE", "△"),
("BENZENE", "⌬"),
])
});
static OPERATORS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
HashMap::from([
("TIMES", "×"),
("DIV", "÷"),
("DIVDE", "÷"),
("DIVIDE", "÷"),
("PLUSMINUS", "±"),
("MINUSPLUS", "∓"),
("CDOT", "⋅"),
("CIRC", "∘"),
("BULLET", "∙"),
("AST", "∗"),
("STAR", "★"),
("DSUM", "⊞"),
("NEQ", "≠"),
("!=", "≠"),
("LE", "≤"),
("LEQ", "≤"),
("GE", "≥"),
("GEQ", "≥"),
("<=", "≤"),
(">=", "≥"),
("<<", "≪"),
(">>", "≫"),
("LLL", "⋘"),
("<<<", "⋘"),
("GGG", "⋙"),
(">>>", "⋙"),
("APPROX", "≈"),
("SIM", "∼"),
("SIMEQ", "≃"),
("CONG", "≅"),
("EQUIV", "≡"),
("==", "≡"),
("ASYMP", "≍"),
("DOTEQ", "≐"),
("PROPTO", "∝"),
("UNION", "∪"),
("SMALLUNION", "∪"),
("CUP", "∪"),
("INTER", "∩"),
("SMALLINTER", "∩"),
("CAP", "∩"),
("SUBSET", "⊂"),
("SUPERSET", "⊃"),
("SUBSETEQ", "⊆"),
("SUPSETEQ", "⊇"),
("SQSUBSET", "⊏"),
("SQSUPSET", "⊐"),
("SQSUBSETEQ", "⊑"),
("SQSUPSETEQ", "⊒"),
("IN", "∈"),
("NOTIN", "∉"),
("OWNS", "∋"),
("NI", "∋"),
("PREC", "≺"),
("SUCC", "≻"),
("FORALL", "∀"),
("EXIST", "∃"),
("LNOT", "¬"),
("WEDGE", "∧"),
("LAND", "∧"),
("VEE", "∨"),
("LOR", "∨"),
("XOR", "⊻"),
("PARTIAL", "∂"),
("EMPTYSET", "∅"),
("THEREFORE", "∴"),
("BECAUSE", "∵"),
("IDENTICAL", "∷"),
("VDASH", "⊢"),
("HLEFT", "⊣"),
("MODELS", "⊨"),
("DAGGER", "†"),
("DDAGGER", "‡"),
("BIGCIRC", "○"),
("DIAMOND", "◇"),
("ISO", "⋄"),
("ne", "≠"),
("neq", "≠"),
("le", "≤"),
("leq", "≤"),
("ge", "≥"),
("geq", "≥"),
("ll", "≪"),
("gg", "≫"),
("approx", "≈"),
("sim", "∼"),
("simeq", "≃"),
("cong", "≅"),
("equiv", "≡"),
("propto", "∝"),
("subset", "⊂"),
("supset", "⊃"),
("subseteq", "⊆"),
("supseteq", "⊇"),
("in", "∈"),
("notin", "∉"),
("ni", "∋"),
("forall", "∀"),
("exists", "∃"),
("nexists", "∄"),
("lnot", "¬"),
("neg", "¬"),
("wedge", "∧"),
("land", "∧"),
("vee", "∨"),
("lor", "∨"),
("nabla", "∇"),
("partial", "∂"),
("emptyset", "∅"),
("infty", "∞"),
("aleph", "ℵ"),
("therefore", "∴"),
("because", "∵"),
("cdot", "⋅"),
("times", "×"),
("div", "÷"),
("cup", "∪"),
("cap", "∩"),
("vdash", "⊢"),
("models", "⊨"),
("oplus", "⊕"),
("otimes", "⊗"),
("dagger", "†"),
("ddagger", "‡"),
("star", "★"),
("circ", "∘"),
("perp", "⊥"),
])
});
static BIG_OPERATORS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
HashMap::from([
("INT", "∫"),
("INTEGRAL", "∫"),
("SMALLINT", "∫"),
("DINT", "∬"),
("TINT", "∭"),
("OINT", "∮"),
("SMALLOINT", "∮"),
("ODINT", "∯"),
("OTINT", "∰"),
("SUM", "∑"),
("SMALLSUM", "Σ"),
("PROD", "∏"),
("SMALLPROD", "∏"),
("COPROD", "∐"),
("SMCOPROD", "∐"),
("AMALG", "∐"),
("BIGCUP", "∪"),
("BIGCAP", "∩"),
("SQCUP", "⊔"),
("BIGSQCUP", "⊔"),
("SQCAP", "⊓"),
("BIGSQCAP", "⊓"),
("UPLUS", "⊎"),
("BIGUPLUS", "⊎"),
("BIGWEDGE", "⋀"),
("BIGVEE", "⋁"),
("OPLUS", "⊕"),
("BIGOPLUS", "⊕"),
("OTIMES", "⊗"),
("BIGOTIMES", "⊗"),
("ODOT", "⊙"),
("BIGODOT", "⊙"),
("OMINUS", "⊖"),
("BIGOMINUS", "⊖"),
("ODIV", "⊘"),
("BIGODIV", "⊘"),
("OSLASH", "⊘"),
("sum", "∑"),
("prod", "∏"),
("coprod", "∐"),
("bigcup", "∪"),
("bigcap", "∩"),
("bigwedge", "⋀"),
("bigvee", "⋁"),
("bigoplus", "⊕"),
("bigotimes", "⊗"),
("int", "∫"),
("iint", "∬"),
("iiint", "∭"),
("oint", "∮"),
])
});
static ARROWS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
HashMap::from([
("larrow", "←"),
("rarrow", "→"),
("uparrow", "↑"),
("downarrow", "↓"),
("lrarrow", "↔"),
("udarrow", "↕"),
("LARROW", "⇐"),
("RARROW", "⇒"),
("UPARROW", "⇑"),
("DOWNARROW", "⇓"),
("LRARROW", "⇔"),
("UDARROW", "⇕"),
("nwarrow", "↖"),
("nearrow", "↗"),
("swarrow", "↙"),
("searrow", "↘"),
("NWARROW", "↖"),
("NEARROW", "↗"),
("SWARROW", "↙"),
("SEARROW", "↘"),
("mapsto", "↦"),
("hookleft", "↩"),
("hookright", "↪"),
("longrightarrow", "⟶"),
("longleftarrow", "⟵"),
("Longrightarrow", "⟹"),
("Longleftarrow", "⟸"),
("longmapsto", "⟼"),
("leftarrow", "←"),
("rightarrow", "→"),
("to", "→"),
("gets", "←"),
("Leftarrow", "⇐"),
("Rightarrow", "⇒"),
("implies", "⇒"),
("iff", "⇔"),
("leftrightarrow", "↔"),
("Leftrightarrow", "⇔"),
("vert", "|"),
("VERT", "‖"),
])
});
static BRACKETS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
HashMap::from([
("LBRACE", "{"),
("RBRACE", "}"),
("LCEIL", "⌈"),
("RCEIL", "⌉"),
("LFLOOR", "⌊"),
("RFLOOR", "⌋"),
("langle", "⟨"),
("rangle", "⟩"),
("LANGLE", "⟨"),
("RANGLE", "⟩"),
("lbrace", "{"),
("rbrace", "}"),
("lceil", "⌈"),
("rceil", "⌉"),
("lfloor", "⌊"),
("rfloor", "⌋"),
("lvert", "|"),
("rvert", "|"),
("lVert", "‖"),
("rVert", "‖"),
])
});
static FUNCTIONS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
HashMap::from([
("sin", "sin"),
("cos", "cos"),
("tan", "tan"),
("cot", "cot"),
("sec", "sec"),
("csc", "csc"),
("arcsin", "arcsin"),
("arccos", "arccos"),
("arctan", "arctan"),
("sinh", "sinh"),
("cosh", "cosh"),
("tanh", "tanh"),
("coth", "coth"),
("log", "log"),
("ln", "ln"),
("lg", "lg"),
("exp", "exp"),
("det", "det"),
("dim", "dim"),
("ker", "ker"),
("hom", "hom"),
("arg", "arg"),
("deg", "deg"),
("gcd", "gcd"),
("lcm", "lcm"),
("max", "max"),
("min", "min"),
("mod", "mod"),
("sup", "sup"),
("inf", "inf"),
("lim", "lim"),
("limsup", "lim sup"),
("liminf", "lim inf"),
("Pr", "Pr"),
])
});
pub static DECORATIONS: LazyLock<HashMap<&'static str, DecoKind>> = LazyLock::new(|| {
HashMap::from([
("box", DecoKind::Box),
("hat", DecoKind::Hat),
("check", DecoKind::Check),
("tilde", DecoKind::Tilde),
("acute", DecoKind::Acute),
("grave", DecoKind::Grave),
("dot", DecoKind::Dot),
("ddot", DecoKind::DDot),
("bar", DecoKind::Bar),
("vec", DecoKind::Vec),
("dyad", DecoKind::Dyad),
("under", DecoKind::Under),
("arch", DecoKind::Arch),
("UNDERLINE", DecoKind::Underline),
("OVERLINE", DecoKind::Overline),
("NOT", DecoKind::StrikeThrough),
("underline", DecoKind::Underline),
("overline", DecoKind::Overline),
("not", DecoKind::StrikeThrough),
("widehat", DecoKind::Hat),
("widetilde", DecoKind::Tilde),
("overrightarrow", DecoKind::Vec),
("overleftarrow", DecoKind::Vec),
("overbrace", DecoKind::Arch),
("underbrace", DecoKind::Under),
])
});
pub static FONT_STYLES: LazyLock<HashMap<&'static str, FontStyleKind>> = LazyLock::new(|| {
HashMap::from([
("rm", FontStyleKind::Roman),
("it", FontStyleKind::Italic),
("bold", FontStyleKind::Bold),
("mathrm", FontStyleKind::Roman),
("mathit", FontStyleKind::Italic),
("mathbf", FontStyleKind::Bold),
("mathbb", FontStyleKind::Blackboard),
("mathcal", FontStyleKind::Calligraphy),
("mathfrak", FontStyleKind::Fraktur),
("mathsf", FontStyleKind::SansSerif),
("mathtt", FontStyleKind::Monospace),
("textbf", FontStyleKind::Bold),
("textrm", FontStyleKind::Roman),
("textit", FontStyleKind::Italic),
])
});
pub fn is_structure_command(cmd: &str) -> bool {
matches!(
cmd,
"OVER"
| "ATOP"
| "SQRT"
| "ROOT"
| "FRAC"
| "DFRAC"
| "TFRAC"
| "TEXT"
| "BEGIN"
| "END"
| "LEFT"
| "RIGHT"
| "BIGG"
| "OPERATORNAME"
| "PHANTOM"
| "VPHANTOM"
| "HPHANTOM"
| "OVERSET"
| "UNDERSET"
| "STACKREL"
| "QUAD"
| "QQUAD"
| "THINSPACE"
| "MEDSPACE"
| "THICKSPACE"
| "NEGSPACE"
| "ENSPACE"
| "MATRIX"
| "PMATRIX"
| "BMATRIX"
| "DMATRIX"
| "VMATRIX"
| "SMALLMATRIX"
| "CASES"
| "PILE"
| "LPILE"
| "RPILE"
| "CHOOSE"
| "BINOM"
| "lim"
| "Lim"
| "REL"
| "BUILDREL"
| "LADDER"
| "SLADDER"
| "LONGDIV"
| "COLOR"
| "SUP"
| "SUB"
| "LSUB"
| "LSUP"
)
}
pub fn is_big_operator(cmd: &str) -> bool {
BIG_OPERATORS.contains_key(cmd)
}
pub fn is_function(cmd: &str) -> bool {
FUNCTIONS.contains_key(cmd)
}
pub(crate) fn max_symbol_name_len() -> usize {
static MAX: LazyLock<usize> = LazyLock::new(|| {
[
&*GREEK_LOWER,
&*GREEK_UPPER,
&*SPECIAL_SYMBOLS,
&*OPERATORS,
&*BIG_OPERATORS,
&*ARROWS,
&*BRACKETS,
]
.iter()
.flat_map(|table| table.keys())
.map(|name| name.len())
.max()
.unwrap_or(0)
});
*MAX
}
pub fn lookup_symbol(cmd: &str) -> Option<&'static str> {
if cmd != "MINUSPLUS" && cmd.eq_ignore_ascii_case("MINUSPLUS") {
return None;
}
if let Some(s) = GREEK_LOWER
.get(cmd)
.or_else(|| GREEK_UPPER.get(cmd))
.or_else(|| SPECIAL_SYMBOLS.get(cmd))
.or_else(|| OPERATORS.get(cmd))
.or_else(|| BIG_OPERATORS.get(cmd))
.or_else(|| ARROWS.get(cmd))
.or_else(|| BRACKETS.get(cmd))
{
return Some(s);
}
if cmd.bytes().all(|ch| ch.is_ascii_uppercase()) {
if let Some(symbol) = GREEK_UPPER.iter().find_map(|(name, symbol)| {
(name.starts_with(|ch: char| ch.is_ascii_uppercase()) && name.eq_ignore_ascii_case(cmd))
.then_some(*symbol)
}) {
return Some(symbol);
}
}
let upper = cmd.to_ascii_uppercase();
if upper != cmd {
if let Some(s) = SPECIAL_SYMBOLS
.get(upper.as_str())
.or_else(|| OPERATORS.get(upper.as_str()))
.or_else(|| BIG_OPERATORS.get(upper.as_str()))
.or_else(|| BRACKETS.get(upper.as_str()))
{
return Some(s);
}
}
if cmd.len() >= 2 && cmd == upper && cmd.bytes().all(|b| b.is_ascii_uppercase()) {
let mut capitalized = String::with_capacity(cmd.len());
capitalized.push_str(&cmd[..1]);
capitalized.push_str(&cmd[1..].to_ascii_lowercase());
if let Some(s) = GREEK_UPPER.get(capitalized.as_str()) {
return Some(s);
}
}
None
}
pub fn lookup_function(cmd: &str) -> Option<&'static str> {
FUNCTIONS.get(cmd).copied()
}
static EQUATION_PUA: LazyLock<HashMap<char, &'static str>> =
LazyLock::new(|| HashMap::from([('\u{E04D}', "|")]));
pub fn lookup_equation_pua(ch: char) -> Option<&'static str> {
EQUATION_PUA.get(&ch).copied()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub enum DecoKind {
Box, Hat, Check, Tilde, Acute, Grave, Dot, DDot, Bar, Vec, Dyad, Under, Arch, Underline, Overline, StrikeThrough, }
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub enum FontStyleKind {
Roman, Italic, Bold, Blackboard, Calligraphy, Fraktur, SansSerif, Monospace, }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_greek_lower() {
assert_eq!(lookup_symbol("alpha"), Some("α"));
assert_eq!(lookup_symbol("omega"), Some("ω"));
}
#[test]
fn test_greek_upper() {
assert_eq!(lookup_symbol("Gamma"), Some("Γ"));
assert_eq!(lookup_symbol("Omega"), Some("Ω"));
}
#[test]
fn test_operators() {
assert_eq!(lookup_symbol("TIMES"), Some("×"));
assert_eq!(lookup_symbol("PLUSMINUS"), Some("±"));
assert_eq!(lookup_symbol("INF"), Some("∞"));
}
#[test]
fn test_case_insensitive_operators() {
assert_eq!(lookup_symbol("times"), Some("×"));
assert_eq!(lookup_symbol("div"), Some("÷"));
assert_eq!(lookup_symbol("neq"), Some("≠"));
assert_eq!(lookup_symbol("leq"), Some("≤"));
assert_eq!(lookup_symbol("geq"), Some("≥"));
assert_eq!(lookup_symbol("inf"), Some("∞"));
assert_eq!(lookup_symbol("pm"), None);
assert_eq!(lookup_symbol("alpha"), Some("α"));
assert_eq!(lookup_symbol("Alpha"), Some("Α"));
assert_ne!(lookup_symbol("alpha"), lookup_symbol("Alpha"));
}
#[test]
fn test_big_operators() {
assert!(is_big_operator("INT"));
assert!(is_big_operator("SUM"));
assert!(is_big_operator("PROD"));
assert!(!is_big_operator("alpha"));
}
#[test]
fn test_set_operators_not_big() {
assert!(!is_big_operator("CAP"));
assert!(!is_big_operator("CUP"));
assert!(!is_big_operator("UNION"));
assert!(!is_big_operator("INTER"));
assert!(!is_big_operator("SMALLINTER"));
assert!(!is_big_operator("SMALLUNION"));
assert!(is_big_operator("BIGCUP"));
assert!(is_big_operator("BIGCAP"));
assert_eq!(lookup_symbol("CAP"), Some("∩"));
assert_eq!(lookup_symbol("CUP"), Some("∪"));
assert_eq!(lookup_symbol("SMALLINTER"), Some("∩"));
assert_eq!(lookup_symbol("SMALLUNION"), Some("∪"));
assert_eq!(lookup_symbol("cap"), Some("∩"));
assert_eq!(lookup_symbol("cup"), Some("∪"));
}
#[test]
fn test_arrows() {
assert_eq!(lookup_symbol("rarrow"), Some("→"));
assert_eq!(lookup_symbol("RARROW"), Some("⇒"));
assert_eq!(lookup_symbol("NEARROW"), Some("↗"));
assert_eq!(lookup_symbol("SEARROW"), Some("↘"));
}
#[test]
fn test_functions() {
assert!(is_function("sin"));
assert!(is_function("log"));
assert!(!is_function("OVER"));
}
#[test]
fn test_structure_commands() {
assert!(is_structure_command("OVER"));
assert!(is_structure_command("SQRT"));
assert!(is_structure_command("lim"));
assert!(!is_structure_command("alpha"));
}
}