Initial texrra engine

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.
This commit is contained in:
gloamxun committed 2026-10-19 11:41:46 +08:00
commit bb1d9e0561
147 files changed
+66719

No files matched your search

+279
View File
@@ -0,0 +1,279 @@
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
};
use crate::{TeXGlue, math_expr::MathExpr, source::SourceLocation};
/// Physical page dimensions in millimetres.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PageSize {
pub width_mm: f64,
pub height_mm: f64,
}
impl PageSize {
pub const A4: Self = Self {
width_mm: 210.0,
height_mm: 297.0,
};
pub const A5: Self = Self {
width_mm: 148.0,
height_mm: 210.0,
};
pub const B5: Self = Self {
width_mm: 176.0,
height_mm: 250.0,
};
/// US letter, 8.5 by 11 inches.
pub const LETTER: Self = Self {
width_mm: 215.9,
height_mm: 279.4,
};
/// US legal, 8.5 by 14 inches.
pub const LEGAL: Self = Self {
width_mm: 215.9,
height_mm: 355.6,
};
/// US executive, 7.25 by 10.5 inches.
pub const EXECUTIVE: Self = Self {
width_mm: 184.15,
height_mm: 266.7,
};
pub fn landscape(self) -> Self {
Self {
width_mm: self.height_mm,
height_mm: self.width_mm,
}
}
}
impl Default for PageSize {
fn default() -> Self {
Self::A4
}
}
#[derive(Clone, Default)]
pub struct DocumentModel {
pub title: String,
pub author: String,
pub date: String,
pub page: PageSize,
pub margins: PageMargins,
pub show_title: bool,
pub blocks: Vec<Block>,
pub block_sources: Vec<Option<SourceLocation>>,
pub labels: HashMap<String, String>,
pub citations: HashMap<String, usize>,
}
pub struct ParsedDocument(pub DocumentModel);
#[derive(Clone)]
pub struct PageMargins {
pub top: f64,
pub right: f64,
pub bottom: f64,
pub left: f64,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct EquationNumber {
pub text: String,
pub parenthesized: bool,
}
impl Default for PageMargins {
fn default() -> Self {
Self {
top: 25.0,
right: 25.0,
bottom: 25.0,
left: 25.0,
}
}
}
#[derive(Clone)]
pub enum Block {
Paragraph(Vec<Run>),
Heading {
level: u8,
runs: Vec<Run>,
},
List(ListBlock),
Table {
rows: Vec<Vec<Vec<Run>>>,
caption: Option<Vec<Run>>,
alignments: Vec<ColumnAlignment>,
rules: Vec<usize>,
},
Image {
path: String,
options: String,
caption: Option<Vec<Run>>,
},
Bibliography(Vec<(String, Vec<Run>)>),
/// A box such as `\fbox{...}` (framed) or `\parbox{width}{...}`.
FramedBox {
runs: Vec<Run>,
frame: bool,
width: Option<common::Scaled>,
},
/// Center-aligned content produced by the `center` environment.
Centered {
runs: Vec<Run>,
},
/// A horizontal rule such as `\hrule` (full width when `width` is None).
HorizontalRule {
height: common::Scaled,
width: Option<common::Scaled>,
},
Math {
expression: MathExpr,
equation_numbers: Vec<Option<EquationNumber>>,
},
VerticalKern(common::Scaled),
VerticalGlue(TeXGlue),
Penalty(i32),
PageBreak,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ColumnAlignment {
Left,
Center,
Right,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ListBlock {
pub ordered: bool,
pub items: Vec<ListItem>,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ListItem {
pub runs: Vec<Run>,
pub nested: Vec<ListBlock>,
pub vertical_glue_after: Vec<TeXGlue>,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum UnresolvedReference {
Label(String),
Citation(Vec<String>),
}
/// An unbreakable inline box such as `\mbox{...}` or `\makebox{...}`.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct InlineBox {
pub runs: Vec<Run>,
}
/// A filled rectangle produced by `\rule`.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct RuleBox {
pub width: common::Scaled,
/// Distance from the baseline to the bottom edge of the ink.
///
/// A positive raise leaves a gap between the baseline and the rule; a
/// negative raise moves the rule below the baseline.
pub raise: common::Scaled,
/// Thickness of the rule.
pub height: common::Scaled,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Run {
pub text: String,
pub math: Option<MathExpr>,
pub kern: Option<common::Scaled>,
pub glue: Option<TeXGlue>,
pub penalty: Option<i32>,
pub rule: Option<RuleBox>,
pub box_run: Option<InlineBox>,
pub unresolved_reference: Option<UnresolvedReference>,
pub bold: bool,
pub italic: bool,
pub monospace: bool,
}
impl Run {
pub fn plain(text: String) -> Self {
Self {
text,
math: None,
kern: None,
glue: None,
penalty: None,
rule: None,
box_run: None,
unresolved_reference: None,
bold: false,
italic: false,
monospace: false,
}
}
}
impl DocumentModel {
pub fn math_font_files(&self) -> HashSet<PathBuf> {
fn collect_runs(runs: &[Run], files: &mut HashSet<PathBuf>) {
for run in runs {
if let Some(expression) = &run.math {
expression.collect_math_font_files(files);
}
}
}
fn collect_list(list: &ListBlock, files: &mut HashSet<PathBuf>) {
for item in &list.items {
collect_runs(&item.runs, files);
for nested in &item.nested {
collect_list(nested, files);
}
}
}
let mut files = HashSet::new();
for block in &self.blocks {
match block {
Block::Paragraph(runs) | Block::Heading { runs, .. } => {
collect_runs(runs, &mut files);
}
Block::List(list) => collect_list(list, &mut files),
Block::Table { rows, caption, .. } => {
for cell in rows.iter().flatten() {
collect_runs(cell, &mut files);
}
if let Some(caption) = caption {
collect_runs(caption, &mut files);
}
}
Block::Image { caption, .. } => {
if let Some(caption) = caption {
collect_runs(caption, &mut files);
}
}
Block::FramedBox { runs, .. } | Block::Centered { runs } => {
collect_runs(runs, &mut files);
}
Block::Bibliography(items) => {
for (_, runs) in items {
collect_runs(runs, &mut files);
}
}
Block::Math { expression, .. } => expression.collect_math_font_files(&mut files),
Block::HorizontalRule { .. }
| Block::VerticalKern(_)
| Block::VerticalGlue(_)
| Block::Penalty(_)
| Block::PageBreak => {}
}
}
files
}
}