use crate::tfm::{store_scaled, Tag, Tfm};
use std::sync::Arc;
pub type Scaled = i64;
pub const TEXT_SIZE: usize = 0;
pub const SCRIPT_SIZE: usize = 1;
pub const SCRIPT_SCRIPT_SIZE: usize = 2;
const FAMILY_FILES: [[&str; 3]; 4] = [
["cmr10", "cmr7", "cmr5"],
["cmmi10", "cmmi7", "cmmi5"],
["cmsy10", "cmsy7", "cmsy5"],
["cmex10", "cmex10", "cmex10"],
];
const FAMILY_AT: [[f64; 3]; 4] = [
[10.0, 7.0, 5.0],
[10.0, 7.0, 5.0],
[10.0, 7.0, 5.0],
[10.0, 10.0, 10.0],
];
pub struct MathFont {
pub tfm: Arc<Tfm>,
pub at: f64,
pub size: Scaled,
pub params: Vec<Scaled>,
pub skew_char: i64,
pub node_font: Option<usize>,
pub exten: Vec<[u8; 4]>,
}
impl MathFont {
pub fn new(tfm: Arc<Tfm>, size: Scaled, exten: Vec<[u8; 4]>) -> MathFont {
let params = tfm
.raw_params
.iter()
.enumerate()
.map(|(i, &v)| match i {
0 => (v * 65536.0).round() as Scaled,
_ => store_scaled(v, size),
})
.collect();
MathFont {
tfm,
at: size as f64 / 65536.0,
size,
params,
skew_char: -1,
node_font: None,
exten,
}
}
pub fn exten_of(path: &std::path::Path) -> Vec<[u8; 4]> {
std::fs::read(path)
.map(|b| exten_table(&b))
.unwrap_or_default()
}
pub fn sp(&self, v: f64) -> Scaled {
store_scaled(v, self.size)
}
pub fn param(&self, n: usize) -> Scaled {
match n.checked_sub(1).and_then(|i| self.params.get(i)) {
Some(v) => *v,
None => 0,
}
}
pub fn metrics(&self, code: u8) -> Option<CharDim> {
let m = self.tfm.char(code)?;
Some(CharDim {
width: self.sp(m.width),
height: self.sp(m.height),
depth: self.sp(m.depth),
italic: self.sp(m.italic),
})
}
pub fn height_plus_depth(&self, code: u8) -> Scaled {
self.metrics(code).map(|m| m.height + m.depth).unwrap_or(0)
}
pub fn next_larger(&self, code: u8) -> Option<u8> {
match self.tfm.tag(code) {
Tag::List => self.tfm.char(code).map(|c| c.remainder),
_ => None,
}
}
pub fn recipe(&self, code: u8) -> Option<[u8; 4]> {
match self.tfm.tag(code) {
Tag::Extensible => {
let at = self.tfm.char(code)?.remainder as usize;
self.exten.get(at).copied()
}
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CharDim {
pub width: Scaled,
pub height: Scaled,
pub depth: Scaled,
pub italic: Scaled,
}
pub struct MathFonts {
fonts: Vec<[Option<MathFont>; 3]>,
pub size: f64,
pub params: super::noad::MathParams,
}
impl MathFonts {
pub fn load(size: f64) -> MathFonts {
let ratio = size / 10.0;
let mut fonts: Vec<[Option<MathFont>; 3]> = (0..16).map(|_| Default::default()).collect();
for fam in 0..4 {
for s in 0..3 {
let Some(path) = crate::typeset::find_font(FAMILY_FILES[fam][s]) else {
continue;
};
let Ok(bytes) = std::fs::read(&path) else {
continue;
};
let Ok(tfm) = Tfm::parse(&bytes) else {
continue;
};
let at = (FAMILY_AT[fam][s] * ratio * 65536.0).round() as Scaled;
let mut font = MathFont::new(Arc::new(tfm), at, exten_table(&bytes));
font.skew_char = super::noad::SKEW_CHAR[fam].map_or(-1, i64::from);
fonts[fam][s] = Some(font);
}
}
MathFonts {
fonts,
size,
params: super::noad::MathParams::plain(),
}
}
pub fn from_families(
fonts: Vec<[Option<MathFont>; 3]>,
params: super::noad::MathParams,
) -> MathFonts {
MathFonts {
fonts,
size: 10.0,
params,
}
}
pub fn usable(&self) -> bool {
self.fonts[2][0].is_some() && self.fonts[3][0].is_some()
}
pub fn font(&self, fam: usize, size: usize) -> Option<&MathFont> {
self.fonts.get(fam)?.get(size)?.as_ref()
}
pub fn at(&self, fam: usize, size: usize) -> f64 {
self.font(fam, size)
.map(|f| f.at)
.unwrap_or(FAMILY_AT[fam.min(3)][size.min(2)] * self.size / 10.0)
}
pub fn sigma(&self, size: usize, n: usize) -> Scaled {
self.font(2, size).map(|f| f.param(n)).unwrap_or(0)
}
pub fn xi(&self, size: usize, n: usize) -> Scaled {
self.font(3, size).map(|f| f.param(n)).unwrap_or(0)
}
pub fn math_x_height(&self, size: usize) -> Scaled {
self.sigma(size, 5)
}
pub fn math_quad(&self, size: usize) -> Scaled {
self.sigma(size, 6)
}
pub fn num1(&self, size: usize) -> Scaled {
self.sigma(size, 8)
}
pub fn num2(&self, size: usize) -> Scaled {
self.sigma(size, 9)
}
pub fn num3(&self, size: usize) -> Scaled {
self.sigma(size, 10)
}
pub fn denom1(&self, size: usize) -> Scaled {
self.sigma(size, 11)
}
pub fn denom2(&self, size: usize) -> Scaled {
self.sigma(size, 12)
}
pub fn sup1(&self, size: usize) -> Scaled {
self.sigma(size, 13)
}
pub fn sup2(&self, size: usize) -> Scaled {
self.sigma(size, 14)
}
pub fn sup3(&self, size: usize) -> Scaled {
self.sigma(size, 15)
}
pub fn sub1(&self, size: usize) -> Scaled {
self.sigma(size, 16)
}
pub fn sub2(&self, size: usize) -> Scaled {
self.sigma(size, 17)
}
pub fn sup_drop(&self, size: usize) -> Scaled {
self.sigma(size, 18)
}
pub fn sub_drop(&self, size: usize) -> Scaled {
self.sigma(size, 19)
}
pub fn delim1(&self, size: usize) -> Scaled {
self.sigma(size, 20)
}
pub fn delim2(&self, size: usize) -> Scaled {
self.sigma(size, 21)
}
pub fn axis_height(&self, size: usize) -> Scaled {
self.sigma(size, 22)
}
pub fn default_rule_thickness(&self, size: usize) -> Scaled {
self.xi(size, 8)
}
pub fn big_op_spacing1(&self, size: usize) -> Scaled {
self.xi(size, 9)
}
pub fn big_op_spacing2(&self, size: usize) -> Scaled {
self.xi(size, 10)
}
pub fn big_op_spacing3(&self, size: usize) -> Scaled {
self.xi(size, 11)
}
pub fn big_op_spacing4(&self, size: usize) -> Scaled {
self.xi(size, 12)
}
pub fn big_op_spacing5(&self, size: usize) -> Scaled {
self.xi(size, 13)
}
}
fn exten_table(bytes: &[u8]) -> Vec<[u8; 4]> {
let half = |at: usize| -> Option<usize> {
bytes
.get(at * 2..at * 2 + 2)
.map(|b| u16::from_be_bytes([b[0], b[1]]) as usize)
};
let (Some(lf), Some(lh), Some(bc), Some(ec)) = (half(0), half(1), half(2), half(3)) else {
return Vec::new();
};
let (Some(nw), Some(nh), Some(nd), Some(ni)) = (half(4), half(5), half(6), half(7)) else {
return Vec::new();
};
let (Some(nl), Some(nk), Some(ne), Some(np)) = (half(8), half(9), half(10), half(11)) else {
return Vec::new();
};
if bc > ec + 1 || ec > 255 {
return Vec::new();
}
if lf != 6 + lh + (ec + 1 - bc) + nw + nh + nd + ni + nl + nk + ne + np {
return Vec::new();
}
let exten = 6 + lh + (ec + 1 - bc) + nw + nh + nd + ni + nl + nk;
let mut out = Vec::with_capacity(ne);
for i in 0..ne {
let at = (exten + i) * 4;
match bytes.get(at..at + 4) {
Some(w) => out.push([w[0], w[1], w[2], w[3]]),
None => break,
}
}
out
}
pub fn unicode(fam: usize, code: u8) -> Option<char> {
match fam {
0 => roman(code),
1 => italic(code),
2 => symbol(code),
3 => extension(code),
_ => None,
}
}
fn roman(code: u8) -> Option<char> {
const GREEK: [char; 11] = ['Γ', 'Δ', 'Θ', 'Λ', 'Ξ', 'Π', 'Σ', 'Υ', 'Φ', 'Ψ', 'Ω'];
match code {
0x00..=0x0A => Some(GREEK[code as usize]),
0x12 => Some('\u{2CB}'),
0x13 => Some('\u{2CA}'),
0x14 => Some('\u{2C7}'),
0x15 => Some('\u{2D8}'),
0x16 => Some('\u{AF}'),
0x20..=0x5E => Some(code as char),
0x5F => Some('\u{2D9}'),
0x60..=0x7A => Some(code as char),
0x7B => Some('\u{2013}'),
0x7C => Some('\u{2014}'),
0x7D => Some('\u{2DD}'),
0x7E => Some('\u{2DC}'),
0x7F => Some('\u{A8}'),
_ => None,
}
}
fn italic(code: u8) -> Option<char> {
const CAPS: [char; 11] = ['Γ', 'Δ', 'Θ', 'Λ', 'Ξ', 'Π', 'Σ', 'Υ', 'Φ', 'Ψ', 'Ω'];
const LOWER: [char; 23] = [
'α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν', 'ξ', 'π', 'ρ', 'σ', 'τ',
'υ', 'φ', 'χ', 'ψ', 'ω',
];
const VARIANT: [char; 6] = ['ε', 'ϑ', 'ϖ', 'ϱ', 'ς', 'φ'];
match code {
0x00..=0x0A => Some(CAPS[code as usize]),
0x0B..=0x21 => Some(LOWER[(code - 0x0B) as usize]),
0x22..=0x27 => Some(VARIANT[(code - 0x22) as usize]),
0x28 => Some('↼'),
0x29 => Some('↽'),
0x2A => Some('⇀'),
0x2B => Some('⇁'),
0x2E => Some('▹'),
0x2F => Some('◃'),
0x30..=0x39 => Some((b'0' + (code - 0x30)) as char),
0x3A => Some('.'),
0x3B => Some(','),
0x3C => Some('<'),
0x3D => Some('/'),
0x3E => Some('>'),
0x3F => Some('⋆'),
0x40 => Some('∂'),
0x41..=0x5A => Some(code as char),
0x5B => Some('♭'),
0x5C => Some('♮'),
0x5D => Some('♯'),
0x5E => Some('⌣'),
0x5F => Some('⌢'),
0x60 => Some('ℓ'),
0x61..=0x7A => Some(code as char),
0x7B => Some('ı'),
0x7C => Some('ȷ'),
0x7D => Some('℘'),
0x7E => Some('⃗'),
_ => None,
}
}
fn symbol(code: u8) -> Option<char> {
const LOW: [char; 32] = [
'−', '·', '×', '∗', '÷', '⋄', '±', '∓', '⊕', '⊖', '⊗', '⊘', '⊙', '○', '∘', '•', '≍', '≡',
'⊆', '⊇', '≤', '≥', '⪯', '⪰', '∼', '≈', '⊂', '⊃', '≪', '≫', '≺', '≻',
];
const ARROWS: [char; 32] = [
'←', '→', '↑', '↓', '↔', '↗', '↘', '≃', '⇐', '⇒', '⇑', '⇓', '⇔', '↖', '↙', '∝', '′', '∞',
'∈', '∋', '△', '▽', '̸', '↦', '∀', '∃', '¬', '∅', 'ℜ', 'ℑ', '⊤', '⊥',
];
const HIGH: [char; 30] = [
'ℵ', '∪', '∩', '⊎', '∧', '∨', '⊢', '⊣', '⌊', '⌋', '⌈', '⌉', '{', '}', '⟨', '⟩', '|', '‖',
'↕', '⇕', '∖', '≀', '√', '∐', '∇', '∫', '⊔', '⊓', '⊑', '⊒',
];
match code {
0x00..=0x1F => Some(LOW[code as usize]),
0x20..=0x3F => Some(ARROWS[(code - 0x20) as usize]),
0x40 => Some(HIGH[0]),
0x41..=0x5A => Some(code as char),
0x5B..=0x77 => HIGH.get((code - 0x5A) as usize).copied(),
0x78 => Some('§'),
0x79 => Some('†'),
0x7A => Some('‡'),
0x7B => Some('¶'),
0x7C => Some('♣'),
0x7D => Some('♦'),
0x7E => Some('♥'),
0x7F => Some('♠'),
_ => None,
}
}
fn extension(code: u8) -> Option<char> {
match code {
0x00 | 0x10 => Some('('),
0x01 | 0x11 => Some(')'),
0x02 | 0x12 => Some('['),
0x03 | 0x13 => Some(']'),
0x04 | 0x14 => Some('⌊'),
0x05 | 0x15 => Some('⌋'),
0x06 | 0x16 => Some('⌈'),
0x07 | 0x17 => Some('⌉'),
0x08 | 0x18 => Some('{'),
0x09 | 0x19 => Some('}'),
0x0A | 0x1A => Some('⟨'),
0x0B | 0x1B => Some('⟩'),
0x0C | 0x1C => Some('|'),
0x0D | 0x1D => Some('‖'),
0x0E => Some('/'),
0x0F => Some('∖'),
0x46 | 0x47 => Some('⊔'),
0x48 | 0x49 => Some('∮'),
0x4A | 0x4B => Some('⊙'),
0x4C | 0x4D => Some('⊕'),
0x4E | 0x4F => Some('⊗'),
0x50 | 0x58 => Some('∑'),
0x51 | 0x59 => Some('∏'),
0x52 | 0x5A => Some('∫'),
0x53 | 0x5B => Some('∪'),
0x54 | 0x5C => Some('∩'),
0x55 | 0x5D => Some('⊎'),
0x56 | 0x5E => Some('∧'),
0x57 | 0x5F => Some('∨'),
0x60 | 0x61 => Some('∐'),
0x62..=0x64 => Some('\u{2C6}'),
0x65..=0x67 => Some('\u{2DC}'),
0x70..=0x75 => Some('√'),
_ => None,
}
}