Files
texrra-engine/crates/texrra-tex-core/src/math_font.rs
T

202 lines
8.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Shared exact font identity for native math PDF and SVG glyphs.
use crate::math_engine_tex::MathFont;
use crate::tfm::TfmMetrics;
use crate::type1::Type1Font;
use std::sync::OnceLock;
#[derive(
Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct MathFontId(pub u8);
impl MathFontId {
pub fn new(family: MathFont, design_size: i64) -> Self {
let size = match design_size {
458_752 => 1,
327_680 => 2,
_ => 0,
};
Self(match family {
MathFont::Roman => size,
MathFont::Italic => 3 + size,
MathFont::Symbol => 6 + size,
MathFont::Extension => 9,
})
}
pub fn family(self) -> MathFont {
match self.0 {
0..=2 => MathFont::Roman,
3..=5 => MathFont::Italic,
6..=8 => MathFont::Symbol,
_ => MathFont::Extension,
}
}
/// Invert the actual Type1 encoding for ToUnicode metadata; paint uses slot.
pub fn character(self, slot: u8) -> char {
// Accent metadata follows the actual built-in glyph name. Geometry
// and painting continue to use the raw font slot, including variants.
match self.type1().name_for_code(slot) {
Some("circumflex" | "hatwide" | "hatwider" | "hatwidest") => return 'ˆ',
Some("tilde" | "tildewide" | "tildewider" | "tildewidest") => return '˜',
Some("vector") => return '\u{20d7}',
Some("grave") => return '`',
Some("acute") => return '´',
Some("dieresis") => return '¨',
Some("macron") => return '¯',
Some("breve") => return '˘',
Some("caron") => return 'ˇ',
Some("dotaccent") => return '˙',
Some("ring") => return '˚',
Some("hungarumlaut") => return '˝',
Some("parenleftbig" | "parenleftBig" | "parenleftbigg" | "parenleftBigg") => {
return '(';
}
Some("parenrightbig" | "parenrightBig" | "parenrightbigg" | "parenrightBigg") => {
return ')';
}
Some("bracketleftbig" | "bracketleftBig" | "bracketleftbigg" | "bracketleftBigg") => {
return '[';
}
Some(
"bracketrightbig" | "bracketrightBig" | "bracketrightbigg" | "bracketrightBigg",
) => return ']',
Some(
"braceleft" | "braceleftbig" | "braceleftBig" | "braceleftbigg" | "braceleftBigg",
) => return '{',
Some(
"braceright" | "bracerightbig" | "bracerightBig" | "bracerightbigg"
| "bracerightBigg",
) => return '}',
Some(
"floorleft" | "floorleftbig" | "floorleftBig" | "floorleftbigg" | "floorleftBigg",
) => return '⌊',
Some(
"floorright" | "floorrightbig" | "floorrightBig" | "floorrightbigg"
| "floorrightBigg",
) => return '⌋',
Some(
"ceilingleft" | "ceilingleftbig" | "ceilingleftBig" | "ceilingleftbigg"
| "ceilingleftBigg",
) => return '⌈',
Some(
"ceilingright" | "ceilingrightbig" | "ceilingrightBig" | "ceilingrightbigg"
| "ceilingrightBigg",
) => return '⌉',
Some(
"angbracketleft" | "angbracketleftbig" | "angbracketleftBig" | "angbracketleftbigg"
| "angbracketleftBigg",
) => return '⟨',
Some(
"angbracketright"
| "angbracketrightbig"
| "angbracketrightBig"
| "angbracketrightbigg"
| "angbracketrightBigg",
) => return '⟩',
Some("slashbig" | "slashBig" | "slashbigg" | "slashBigg") => return '/',
Some(
"backslash" | "backslashbig" | "backslashBig" | "backslashbigg" | "backslashBigg",
) => return '\\',
Some("parenlefttp") => return '⎛',
Some("parenleftex") => return '⎜',
Some("parenleftbt") => return '⎝',
Some("parenrighttp") => return '⎞',
Some("parenrightex") => return '⎟',
Some("parenrightbt") => return '⎠',
Some("bracketlefttp") => return '⎡',
Some("bracketleftex") => return '⎢',
Some("bracketleftbt") => return '⎣',
Some("bracketrighttp") => return '⎤',
Some("bracketrightex") => return '⎥',
Some("bracketrightbt") => return '⎦',
Some("bracelefttp") => return '⎧',
Some("braceleftmid") => return '⎨',
Some("braceleftbt") => return '⎩',
Some("braceex") => return '⎪',
Some("bracerighttp") => return '⎫',
Some("bracerightmid") => return '⎬',
Some("bracerightbt") => return '⎭',
Some("vextendsingle" | "arrowvertex") => return '⏐',
Some("vextenddouble" | "arrowvertexdbl" | "bardbl") => return '‖',
Some("bar") => return '|',
Some("arrowup" | "arrowtp") => return '↑',
Some("arrowdown" | "arrowbt") => return '↓',
Some("arrowdblup" | "arrowdbltp") => return '⇑',
Some("arrowdbldown" | "arrowdblbt") => return '⇓',
Some("arrowbothv") => return '↕',
Some("arrowdblbothv") => return '⇕',
_ => {}
}
let candidates = (32u32..127).filter_map(char::from_u32).chain(
"ΓΔΘΛΞΠΣΥΦΨΩαβγδϵζηθικλμνξπρστυϕχψωεϑϖϱςφ−±∓×÷·≤≥≠≈≡∈∉⊂⊃∪∩∞→←↔⇒⇐⇔∑∏∫√∂∇ℓℏfffiflffiffl".chars(),
);
candidates
.into_iter()
.find(|c| self.type1().code_for(*c) == Some(slot))
.unwrap_or('�')
}
pub fn name(self) -> &'static str {
[
"CMR10", "CMR7", "CMR5", "CMMI10", "CMMI7", "CMMI5", "CMSY10", "CMSY7", "CMSY5",
"CMEX10",
][usize::from(self.0)]
}
pub fn design_size(self) -> i64 {
if self.0 == 9 {
655_360
} else {
[655_360, 458_752, 327_680][usize::from(self.0 % 3)]
}
}
pub fn type1(self) -> &'static Type1Font {
static FONTS: OnceLock<Vec<Type1Font>> = OnceLock::new();
&FONTS.get_or_init(|| {
[
include_bytes!("../../../assets/fonts/cmr10.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmr7.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmr5.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmmi10.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmmi7.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmmi5.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmsy10.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmsy7.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmsy5.pfb").as_slice(),
include_bytes!("../../../assets/fonts/cmex10.pfb").as_slice(),
]
.map(|bytes| Type1Font::from_pfb(bytes).expect("embedded math PFB"))
.into()
})[usize::from(self.0)]
}
pub fn metrics(self) -> &'static TfmMetrics {
static FONTS: OnceLock<Vec<TfmMetrics>> = OnceLock::new();
&FONTS.get_or_init(|| {
[
include_bytes!("../assets/cmr10.tfm").as_slice(),
include_bytes!("../assets/cmr7.tfm").as_slice(),
include_bytes!("../assets/cmr5.tfm").as_slice(),
include_bytes!("../assets/cmmi10.tfm").as_slice(),
include_bytes!("../assets/cmmi7.tfm").as_slice(),
include_bytes!("../assets/cmmi5.tfm").as_slice(),
include_bytes!("../assets/cmsy10.tfm").as_slice(),
include_bytes!("../assets/cmsy7.tfm").as_slice(),
include_bytes!("../assets/cmsy5.tfm").as_slice(),
include_bytes!("../assets/cmex10.tfm").as_slice(),
]
.map(|bytes| TfmMetrics::parse(bytes).expect("embedded math TFM"))
.into()
})[usize::from(self.0)]
}
pub fn svg_path(self, slot: u8) -> &'static str {
static PATHS: OnceLock<Vec<Vec<String>>> = OnceLock::new();
&PATHS.get_or_init(|| {
serde_json::from_str(include_str!("../assets/math-outlines.json"))
.expect("generated math outlines")
})[usize::from(self.0)][usize::from(slot)]
}
}