Rename the crate, binary, core crate, font families, env vars, and internal commands from texcraft/tex-engine to texrra. Serve the separately versioned texrra-web frontend from TEXRRA_WEB_DIR, ../web, or ./web instead of embedding index.html. Add starred section heading support and an arXiv template conformance fixture.
277 lines
7.7 KiB
Rust
277 lines
7.7 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use crate::{MathClass, MathCode};
|
|
|
|
static DEFAULT_MATH_CODE: MathCode = MathCode(0);
|
|
static UNSET_UNICODE_MATH_CODE: i32 = -1;
|
|
static UNSET_DELIMITER_CODE: i32 = -1;
|
|
|
|
/// Character math codes owned by the Rust TeX engine.
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
|
pub struct MathCodeTable {
|
|
low: Vec<MathCode>,
|
|
high: HashMap<usize, MathCode>,
|
|
groups: Vec<MathCodeGroup>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
|
|
struct MathCodeGroup {
|
|
values: HashMap<usize, Option<MathCode>>,
|
|
}
|
|
|
|
impl MathCodeTable {
|
|
pub fn get_index_ref(&self, index: usize) -> &MathCode {
|
|
self.low
|
|
.get(index)
|
|
.or_else(|| self.high.get(&index))
|
|
.unwrap_or(&DEFAULT_MATH_CODE)
|
|
}
|
|
|
|
pub fn get_index_mut_ref(&mut self, index: usize) -> &mut MathCode {
|
|
if let Some(math_code) = self.low.get_mut(index) {
|
|
return math_code;
|
|
}
|
|
self.high.entry(index).or_default()
|
|
}
|
|
|
|
pub fn prepare_assignment(&mut self, index: usize, global: bool) {
|
|
if global {
|
|
for group in &mut self.groups {
|
|
group.values.remove(&index);
|
|
}
|
|
} else if !self.groups.is_empty() {
|
|
let previous = self.stored_value(index);
|
|
self.groups
|
|
.last_mut()
|
|
.expect("math code group exists")
|
|
.values
|
|
.entry(index)
|
|
.or_insert(previous);
|
|
}
|
|
}
|
|
|
|
pub fn begin_group(&mut self) {
|
|
self.groups.push(MathCodeGroup::default());
|
|
}
|
|
|
|
pub fn end_group(&mut self) {
|
|
let Some(group) = self.groups.pop() else {
|
|
return;
|
|
};
|
|
for (index, value) in group.values {
|
|
self.set_index(index, value);
|
|
}
|
|
}
|
|
|
|
fn stored_value(&self, index: usize) -> Option<MathCode> {
|
|
self.low
|
|
.get(index)
|
|
.copied()
|
|
.or_else(|| self.high.get(&index).copied())
|
|
}
|
|
|
|
fn set_index(&mut self, index: usize, value: Option<MathCode>) {
|
|
if let Some(math_code) = self.low.get_mut(index) {
|
|
if let Some(value) = value {
|
|
*math_code = value;
|
|
}
|
|
return;
|
|
}
|
|
match value {
|
|
Some(math_code) => {
|
|
self.high.insert(index, math_code);
|
|
}
|
|
None => {
|
|
self.high.remove(&index);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for MathCodeTable {
|
|
fn default() -> Self {
|
|
Self {
|
|
low: vec![MathCode::default(); 128],
|
|
high: HashMap::new(),
|
|
groups: Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
|
pub struct UnicodeMathCode {
|
|
pub class: u8,
|
|
pub family: u8,
|
|
pub glyph_slot: u32,
|
|
}
|
|
|
|
impl UnicodeMathCode {
|
|
pub fn pack(self) -> u32 {
|
|
(u32::from(self.family) << 24) | (u32::from(self.class) << 21) | self.glyph_slot
|
|
}
|
|
|
|
pub fn unpack(code: u32) -> Self {
|
|
Self {
|
|
class: ((code >> 21) & 0x7) as u8,
|
|
family: (code >> 24) as u8,
|
|
glyph_slot: code & 0x1F_FFFF,
|
|
}
|
|
}
|
|
|
|
pub fn character(self) -> char {
|
|
char::from_u32(self.glyph_slot)
|
|
.expect("Unicode math glyph slots contain valid scalar values")
|
|
}
|
|
|
|
pub fn math_class(self) -> MathClass {
|
|
match self.class {
|
|
0 | 7 => MathClass::Ordinary,
|
|
1 => MathClass::Operator,
|
|
2 => MathClass::Binary,
|
|
3 => MathClass::Relation,
|
|
4 => MathClass::Opening,
|
|
5 => MathClass::Closing,
|
|
6 => MathClass::Punctuation,
|
|
_ => unreachable!("math character classes use three bits"),
|
|
}
|
|
}
|
|
|
|
pub fn math_family(self, active_family: Option<i32>) -> u8 {
|
|
if self.class == 7 {
|
|
active_family
|
|
.filter(|family| (0..=255).contains(family))
|
|
.map(|family| family as u8)
|
|
.unwrap_or(self.family)
|
|
} else {
|
|
self.family
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
|
pub struct UnicodeMathCodeTable {
|
|
values: HashMap<usize, i32>,
|
|
groups: Vec<UnicodeMathCodeGroup>,
|
|
}
|
|
|
|
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
|
struct UnicodeMathCodeGroup {
|
|
values: HashMap<usize, Option<i32>>,
|
|
}
|
|
|
|
impl UnicodeMathCodeTable {
|
|
pub fn get(&self, codepoint: usize) -> Option<UnicodeMathCode> {
|
|
let value = self.values.get(&codepoint).copied()?;
|
|
(value != UNSET_UNICODE_MATH_CODE).then(|| UnicodeMathCode::unpack(value as u32))
|
|
}
|
|
|
|
pub fn get_index_ref(&self, codepoint: usize) -> &i32 {
|
|
self.values
|
|
.get(&codepoint)
|
|
.unwrap_or(&UNSET_UNICODE_MATH_CODE)
|
|
}
|
|
|
|
pub fn get_index_mut_ref(&mut self, codepoint: usize) -> &mut i32 {
|
|
self.values
|
|
.entry(codepoint)
|
|
.or_insert(UNSET_UNICODE_MATH_CODE)
|
|
}
|
|
|
|
pub fn prepare_assignment(&mut self, codepoint: usize, global: bool) {
|
|
if global {
|
|
for group in &mut self.groups {
|
|
group.values.remove(&codepoint);
|
|
}
|
|
} else if !self.groups.is_empty() {
|
|
let previous = self.values.get(&codepoint).copied();
|
|
self.groups
|
|
.last_mut()
|
|
.expect("Unicode math code group exists")
|
|
.values
|
|
.entry(codepoint)
|
|
.or_insert(previous);
|
|
}
|
|
}
|
|
|
|
pub fn begin_group(&mut self) {
|
|
self.groups.push(UnicodeMathCodeGroup::default());
|
|
}
|
|
|
|
pub fn end_group(&mut self) {
|
|
let Some(group) = self.groups.pop() else {
|
|
return;
|
|
};
|
|
for (codepoint, value) in group.values {
|
|
match value {
|
|
Some(value) => {
|
|
self.values.insert(codepoint, value);
|
|
}
|
|
None => {
|
|
self.values.remove(&codepoint);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
|
pub struct DelimiterCodeTable {
|
|
values: HashMap<usize, i32>,
|
|
groups: Vec<DelimiterCodeGroup>,
|
|
}
|
|
|
|
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
|
struct DelimiterCodeGroup {
|
|
values: HashMap<usize, Option<i32>>,
|
|
}
|
|
|
|
impl DelimiterCodeTable {
|
|
pub fn get_index_ref(&self, codepoint: usize) -> &i32 {
|
|
self.values.get(&codepoint).unwrap_or(&UNSET_DELIMITER_CODE)
|
|
}
|
|
|
|
pub fn get_index_mut_ref(&mut self, codepoint: usize) -> &mut i32 {
|
|
self.values.entry(codepoint).or_insert(UNSET_DELIMITER_CODE)
|
|
}
|
|
|
|
pub fn prepare_assignment(&mut self, codepoint: usize, global: bool) {
|
|
if global {
|
|
for group in &mut self.groups {
|
|
group.values.remove(&codepoint);
|
|
}
|
|
} else if !self.groups.is_empty() {
|
|
let previous = self.values.get(&codepoint).copied();
|
|
self.groups
|
|
.last_mut()
|
|
.expect("delimiter code group exists")
|
|
.values
|
|
.entry(codepoint)
|
|
.or_insert(previous);
|
|
}
|
|
}
|
|
|
|
pub fn begin_group(&mut self) {
|
|
self.groups.push(DelimiterCodeGroup::default());
|
|
}
|
|
|
|
pub fn end_group(&mut self) {
|
|
let Some(group) = self.groups.pop() else {
|
|
return;
|
|
};
|
|
for (codepoint, value) in group.values {
|
|
match value {
|
|
Some(value) => {
|
|
self.values.insert(codepoint, value);
|
|
}
|
|
None => {
|
|
self.values.remove(&codepoint);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn assign(&mut self, codepoint: usize, value: i32) {
|
|
self.values.insert(codepoint, value);
|
|
}
|
|
}
|