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

78 lines
2.0 KiB
Rust

//! One code table for the classic and Unicode views of math characters.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(transparent)]
pub struct MathCode(u32);
impl MathCode {
pub(super) const ACTIVE: Self = Self(0x1f_ffff);
pub(super) fn new(class: u8, family: u8, slot: u32) -> Self {
Self((u32::from(family) << 24) | (u32::from(class) << 21) | slot)
}
pub(super) fn default_for(character: u32) -> Self {
match character {
65..=90 | 97..=122 => Self::new(7, 1, character),
48..=57 => Self::new(7, 0, character),
_ => Self::new(0, 0, character),
}
}
pub(super) fn from_legacy(value: u16) -> Self {
if value == 0x8000 {
Self::ACTIVE
} else {
Self::new(
(value >> 12) as u8,
((value >> 8) & 15) as u8,
u32::from(value & 255),
)
}
}
pub(super) fn from_word(value: i32) -> Result<Self, &'static str> {
let code = Self(value as u32);
if code == Self::ACTIVE || code.slot() <= 0x10_ffff {
Ok(code)
} else if code.is_active() {
Err("Bad active XeTeX math code")
} else {
Err("Bad XeTeX math character code")
}
}
pub(super) fn legacy(self) -> Option<u16> {
if self.is_active() {
Some(0x8000)
} else if self.slot() <= 255 && self.family() < 16 {
Some(
(u16::from(self.class()) << 12)
| (u16::from(self.family()) << 8)
| self.slot() as u16,
)
} else {
None
}
}
pub fn word(self) -> i32 {
self.0 as i32
}
pub fn is_active(self) -> bool {
self.slot() == Self::ACTIVE.0
}
pub fn slot(self) -> u32 {
self.0 & Self::ACTIVE.0
}
pub fn class(self) -> u8 {
((self.0 >> 21) & 7) as u8
}
pub fn family(self) -> u8 {
(self.0 >> 24) as u8
}
}