pub mod agl;
pub mod aot;
pub mod aot_runtime;
pub mod banner;
pub mod bib;
pub mod bst;
pub mod bstvm;
pub mod bundle;
pub mod catcode;
pub mod cff;
pub mod charcodes;
pub mod cli;
pub mod colour;
pub mod compiler;
pub mod corpus;
pub mod dap;
pub mod dimen;
pub mod docs;
pub mod document;
pub mod dvi;
pub mod expand;
pub mod fontmap;
pub mod format;
pub mod geturl;
pub mod glue;
pub mod image;
pub mod intercepts;
pub mod io;
pub mod ir;
pub mod itar;
pub mod latex;
pub mod lexer;
pub mod lower;
pub mod lsp;
pub mod parallel;
pub mod parity;
pub mod pdf;
pub mod pk;
pub mod repl;
pub mod runtime;
pub mod rust_ffi;
pub mod script_cache;
pub mod sfnt;
pub mod special;
pub mod status;
pub mod tfm;
pub mod tiers;
pub mod tikz;
pub mod token;
pub mod type1;
pub mod typeset;
pub mod vf;
pub use expand::{Engine, TexError};
pub fn run_messages_list(src: &str) -> Result<Vec<String>, TexError> {
let chunk = compile(src)?;
crate::runtime::run(chunk).map_err(TexError)
}
fn without_color_marks(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut in_spec = false;
for ch in text.chars() {
match ch {
'\u{1}' => in_spec = true,
'\u{2}' => in_spec = false,
'\u{3}' => {}
c if in_spec => {
let _ = c;
}
c => out.push(c),
}
}
out
}
pub fn run_text_marked(src: &str) -> Result<String, TexError> {
let chunk = compile_text(src)?;
let _ = crate::runtime::run(chunk).map_err(TexError)?;
Ok(crate::runtime::take_text())
}
pub fn run_text(src: &str) -> Result<String, TexError> {
Ok(without_color_marks(&run_text_marked(src)?))
}
pub fn compile_text(src: &str) -> Result<fusevm::Chunk, TexError> {
let src = crate::rust_ffi::desugar(src);
let mut lowerer = crate::lower::Lowerer::new().with_text_output();
if crate::latex::looks_like_latex(&src) {
lowerer.preload(crate::latex::PRELUDE)?;
}
let cmds = lowerer.lower(&src)?;
crate::compiler::Compiler::new().compile(&cmds)
}
pub fn run_text_cached(path: &std::path::Path, src: &str) -> Result<String, TexError> {
Ok(without_color_marks(&run_text_marked_cached(path, src)?))
}
pub fn run_text_marked_cached(path: &std::path::Path, src: &str) -> Result<String, TexError> {
let chunk = compile_text_cached(path, src)?;
let _ = crate::runtime::run(chunk).map_err(TexError)?;
Ok(crate::runtime::take_text())
}
fn compile_text_cached(path: &std::path::Path, src: &str) -> Result<fusevm::Chunk, TexError> {
if let Some(chunk) = crate::script_cache::try_load_mode(path, "text") {
return Ok(chunk);
}
let src_d = crate::rust_ffi::desugar(src);
let mut lowerer = crate::lower::Lowerer::new().with_text_output();
if crate::latex::looks_like_latex(&src_d) {
lowerer.preload(crate::latex::PRELUDE)?;
}
let cmds = lowerer.lower(&src_d)?;
let chunk = crate::compiler::Compiler::new().compile(&cmds)?;
crate::script_cache::store_mode(path, "text", &chunk);
Ok(chunk)
}
pub fn run_pdf(src: &str) -> Result<Vec<u8>, TexError> {
run_pdf_at(None, src)
}
pub fn run_pdf_at(path: Option<&std::path::Path>, src: &str) -> Result<Vec<u8>, TexError> {
let src_d = crate::rust_ffi::desugar(src);
let mut lowerer = crate::lower::Lowerer::new().with_text_output();
if crate::latex::looks_like_latex(&src_d) {
lowerer.preload(crate::latex::PRELUDE)?;
}
let cmds = lowerer.lower(&src_d)?;
let families = lowerer.fonts.clone();
let page_colour = lowerer.page_colour;
let chunk = crate::compiler::Compiler::new().compile(&cmds)?;
let _ = crate::runtime::run(chunk).map_err(TexError)?;
let text = crate::runtime::take_text();
Ok(crate::typeset::to_pdf(
&text,
&families,
&crate::typeset::Layout::default(),
page_colour,
path.and_then(|p| p.parent()),
))
}
pub fn run_dvi_fallback(
path: Option<&std::path::Path>,
src: &str,
chain: &crate::typeset::FontChain,
layout: &crate::typeset::Layout,
) -> Result<Vec<u8>, TexError> {
let text = match path {
Some(p) => run_text_marked_cached(p, src)?,
None => run_text_marked(src)?,
};
Ok(crate::typeset::to_dvi_chain(&text, chain, layout))
}
pub fn run_dvi_cached(
path: &std::path::Path,
src: &str,
font_path: &std::path::Path,
layout: &crate::typeset::Layout,
) -> Result<Vec<u8>, TexError> {
let text = run_text_cached(path, src)?;
let font = crate::tfm::Tfm::open(font_path).map_err(TexError)?;
let name = font_path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("cmr10");
Ok(crate::typeset::to_dvi(&text, &font, name, layout))
}
pub fn run_dvi(
src: &str,
font_path: &std::path::Path,
layout: &crate::typeset::Layout,
) -> Result<Vec<u8>, TexError> {
let text = run_text(src)?;
let font = crate::tfm::Tfm::open(font_path).map_err(TexError)?;
let name = font_path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("cmr10");
Ok(crate::typeset::to_dvi(&text, &font, name, layout))
}
pub fn run_messages(src: &str) -> Result<String, TexError> {
let chunk = compile(src)?;
let msgs = crate::runtime::run(chunk).map_err(TexError)?;
Ok(msgs.join(" "))
}
pub fn run_messages_cached(path: &std::path::Path, src: &str) -> Result<String, TexError> {
let chunk = compile_cached(path, src)?;
let msgs = crate::runtime::run(chunk).map_err(TexError)?;
Ok(msgs.join(" "))
}
pub fn compile_cached(path: &std::path::Path, src: &str) -> Result<fusevm::Chunk, TexError> {
if let Some(chunk) = crate::script_cache::try_load(path) {
return Ok(chunk);
}
let chunk = compile(src)?;
crate::script_cache::store(path, &chunk);
Ok(chunk)
}
pub fn compile(src: &str) -> Result<fusevm::Chunk, TexError> {
let cmds = commands(src)?;
crate::compiler::Compiler::new().compile(&cmds)
}
pub fn commands(src: &str) -> Result<Vec<crate::ir::Cmd>, TexError> {
let src = crate::rust_ffi::desugar(src);
let mut lowerer = crate::lower::Lowerer::new();
if crate::latex::looks_like_latex(&src) {
lowerer.preload(crate::latex::PRELUDE)?;
}
lowerer.lower(&src)
}
pub fn source_ends_run(src: &str) -> bool {
let src = crate::rust_ffi::desugar(src);
let mut lowerer = crate::lower::Lowerer::new();
let _ = lowerer.lower(&src);
lowerer.ended
}
pub fn compile_located(src: &str) -> Result<fusevm::Chunk, (TexError, u32)> {
let src = crate::rust_ffi::desugar(src);
let cmds = crate::lower::Lowerer::new().lower_located(&src)?;
crate::compiler::Compiler::new()
.compile(&cmds)
.map_err(|e| (e, 0))
}
pub fn compile_debug(src: &str) -> Result<fusevm::Chunk, TexError> {
let src = crate::rust_ffi::desugar(src);
let cmds = crate::lower::Lowerer::new().lower(&src)?;
crate::compiler::Compiler::new_debug().compile(&cmds)
}
pub fn run_messages_debug(src: &str) -> Result<String, TexError> {
let chunk = compile_debug(src)?;
let msgs = crate::runtime::run_debug(chunk, crate::dap::on_debug_line).map_err(TexError)?;
Ok(msgs.join(" "))
}