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.
117 lines
3.2 KiB
Rust
117 lines
3.2 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use crate::EngineCommandName;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum NativePrimitive {
|
|
ParagraphBreak,
|
|
LineBreak,
|
|
PageBreak,
|
|
Penalty,
|
|
Kern,
|
|
HorizontalSkip,
|
|
VerticalSkip,
|
|
Advance,
|
|
Multiply,
|
|
Divide,
|
|
Global,
|
|
Long,
|
|
Outer,
|
|
Relax,
|
|
}
|
|
|
|
/// Meaning selected by the Rust engine for a control sequence.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum EngineCommandMeaning {
|
|
Macro,
|
|
Native(NativePrimitive),
|
|
}
|
|
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
|
pub struct NativeCommandTable {
|
|
meanings: HashMap<EngineCommandName, NativePrimitive>,
|
|
groups: Vec<HashMap<EngineCommandName, Option<NativePrimitive>>>,
|
|
}
|
|
|
|
impl Default for NativeCommandTable {
|
|
fn default() -> Self {
|
|
let meanings = [
|
|
("advance", NativePrimitive::Advance),
|
|
("multiply", NativePrimitive::Multiply),
|
|
("divide", NativePrimitive::Divide),
|
|
("global", NativePrimitive::Global),
|
|
("long", NativePrimitive::Long),
|
|
("outer", NativePrimitive::Outer),
|
|
("relax", NativePrimitive::Relax),
|
|
("par", NativePrimitive::ParagraphBreak),
|
|
("newline", NativePrimitive::LineBreak),
|
|
("texrrapagebreak", NativePrimitive::PageBreak),
|
|
("penalty", NativePrimitive::Penalty),
|
|
("kern", NativePrimitive::Kern),
|
|
("hskip", NativePrimitive::HorizontalSkip),
|
|
("vskip", NativePrimitive::VerticalSkip),
|
|
]
|
|
.into_iter()
|
|
.map(|(name, primitive)| {
|
|
(
|
|
EngineCommandName::ControlSequence(name.to_string()),
|
|
primitive,
|
|
)
|
|
})
|
|
.collect();
|
|
|
|
Self {
|
|
meanings,
|
|
groups: Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl NativeCommandTable {
|
|
pub fn get(&self, name: &EngineCommandName) -> Option<NativePrimitive> {
|
|
self.meanings.get(name).copied()
|
|
}
|
|
|
|
pub fn define(&mut self, name: EngineCommandName, primitive: NativePrimitive, global: bool) {
|
|
self.prepare_assignment(&name, global);
|
|
self.meanings.insert(name, primitive);
|
|
}
|
|
|
|
pub fn remove(&mut self, name: EngineCommandName, global: bool) {
|
|
self.prepare_assignment(&name, global);
|
|
self.meanings.remove(&name);
|
|
}
|
|
|
|
pub fn begin_group(&mut self) {
|
|
self.groups.push(HashMap::new());
|
|
}
|
|
|
|
pub fn end_group(&mut self) {
|
|
let Some(group) = self.groups.pop() else {
|
|
return;
|
|
};
|
|
for (name, previous) in group {
|
|
match previous {
|
|
Some(primitive) => {
|
|
self.meanings.insert(name, primitive);
|
|
}
|
|
None => {
|
|
self.meanings.remove(&name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn prepare_assignment(&mut self, name: &EngineCommandName, global: bool) {
|
|
if global {
|
|
for group in &mut self.groups {
|
|
group.remove(name);
|
|
}
|
|
} else if let Some(group) = self.groups.last_mut() {
|
|
group
|
|
.entry(name.clone())
|
|
.or_insert_with(|| self.meanings.get(name).copied());
|
|
}
|
|
}
|
|
}
|