Skip to main content

retex_engine/
lib.rs

1//! The retex TeX engine: a clean-room implementation of LuaTeX's
2//! typesetting machine.
3//!
4//! The [`Engine`] holds all state of a TeX run. Its methods are spread
5//! over modules following the parts of *TeX: The Program*:
6//!
7//! * [`print`] and [`error`] — terminal and log output, error recovery;
8//! * [`hash`], [`eqtb`], [`save`] — control sequences, equivalents, grouping;
9//! * [`input`] — the input stack and the tokenizer (`get_next`);
10//! * [`expand`], [`cond`], [`scan`] — expansion, conditionals, scanning;
11//! * [`prefixed`] — assignments;
12//! * [`maincontrol`] — the chief executive.
13
14#![forbid(unsafe_code)]
15#![allow(clippy::too_many_lines)]
16
17pub mod align;
18pub mod backend;
19mod boxnote;
20pub mod build;
21pub mod clock;
22pub mod cmd;
23mod cmd_gen;
24pub mod codes;
25pub mod cond;
26pub mod diag;
27pub mod dirs;
28pub mod display;
29pub mod dvi;
30pub mod eqtb;
31pub mod error;
32pub mod expand;
33pub mod expansion;
34pub mod files;
35pub mod fonts;
36pub mod format;
37pub mod glue;
38pub mod hash;
39pub mod hmode;
40pub mod hyph;
41pub mod image;
42pub mod init;
43pub mod input;
44pub mod linebreak;
45pub mod maincontrol;
46pub mod math;
47pub mod mathdisplay;
48pub mod mathparams;
49pub mod meaning;
50pub mod misc;
51pub mod mlist;
52pub mod nodeproc;
53pub mod nodes;
54pub mod pack;
55pub mod page;
56pub mod pagenote;
57pub mod pdfext;
58pub(crate) mod pdfship;
59pub mod prefixed;
60pub mod primitives;
61pub mod primitives_gen;
62pub mod print;
63pub mod protrusion;
64pub mod save;
65pub mod scan;
66pub mod shipout;
67pub mod showbox;
68pub mod snapshot;
69pub mod synctex;
70pub mod the;
71pub mod timing;
72pub mod token;
73pub mod typeset;
74pub mod utf8x;
75
76use cmd::Cmd;
77use cond::CondState;
78use eqtb::Eqtb;
79use error::ErrorState;
80use files::FileState;
81use hash::{CsId, CsTable};
82use input::InputState;
83use print::Printer;
84use save::SaveStack;
85use token::Token;
86
87/// Callbacks from the engine into an embedded Lua interpreter.
88///
89/// The engine does not depend on Lua. `\directlua` and related primitives
90/// call this trait, which `retex-lua` implements. The host is shared
91/// (`Rc`), so nested calls (Lua → TeX → Lua) reach it too.
92pub trait LuaHost {
93    /// Runs a chunk of Lua code for `\directlua` (or `\latelua`, etc.).
94    /// `name` is the chunk name. Text printed with `tex.print` must be
95    /// pushed back into the engine's input by the host.
96    fn run(&self, engine: &mut Engine, name: &str, code: &[u8]) -> Result<(), String>;
97
98    /// Data to store in the format file (e.g. `lua.bytecode`).
99    fn dump(&self, _engine: &mut Engine) -> Vec<u8> {
100        Vec::new()
101    }
102
103    /// Restores data stored by [`dump`](Self::dump).
104    fn undump(&self, _engine: &mut Engine, _data: &[u8]) -> Result<(), String> {
105        Ok(())
106    }
107
108    /// Calls callback `name` without arguments, ignoring its results
109    /// (`pre_dump`, `stop_run`, …). Does nothing if none is registered.
110    fn call_simple(&self, _engine: &mut Engine, _name: &str) -> Result<(), String> {
111        Ok(())
112    }
113
114    /// Calls callback `name` with one string argument, ignoring its
115    /// results (`buildpage_filter`, `contribute_filter`).
116    fn call_with_string(
117        &self,
118        _engine: &mut Engine,
119        _name: &str,
120        _arg: &str,
121    ) -> Result<(), String> {
122        Ok(())
123    }
124
125    /// Whether a Lua function is registered for callback `name`.
126    fn has_callback(&self, _name: &str) -> bool {
127        false
128    }
129
130    /// Calls the `define_font` callback. Returns the id of the font it
131    /// defined, or `None` if the font could not be loaded.
132    fn define_font(&self, _engine: &mut Engine, _name: &[u8], _size: i32) -> Option<u32> {
133        None
134    }
135
136    /// Calls a node-list filter callback (`pre_linebreak_filter`,
137    /// `hpack_filter`, `post_linebreak_filter`, …) on the list starting at
138    /// `head`. Returns the new head; [`ListFilter::Flush`] means the list
139    /// must be discarded.
140    fn list_filter(
141        &self,
142        _engine: &mut Engine,
143        _callback: &str,
144        head: nodes::NodeRef,
145        _group: &str,
146    ) -> ListFilter {
147        ListFilter::Keep(head)
148    }
149
150    /// Calls the `hpack_quality` or `vpack_quality` callback `cb` for box
151    /// `b` with `incident` (overfull, underfull, loose, tight) and its
152    /// detail; returns the node the callback gave (null if none).
153    #[allow(clippy::too_many_arguments)]
154    fn pack_quality(
155        &self,
156        _engine: &mut Engine,
157        _cb: &str,
158        _incident: &str,
159        _detail: i32,
160        _b: nodes::NodeRef,
161        _first: i32,
162        _last: i32,
163    ) -> nodes::NodeRef {
164        nodes::NodeRef::NULL
165    }
166
167    /// Runs the `hyphenate`, `ligaturing` or `kerning` callback `cb` on
168    /// the list after `head` (a temporary node), with its last node:
169    /// `true` if a function or `false` is registered, which replaces the
170    /// built-in pass.
171    fn node_pass(&self, _engine: &mut Engine, _cb: &str, _head: nodes::NodeRef) -> bool {
172        false
173    }
174
175    /// Calls the `linebreak_filter` callback on paragraph `head`
176    /// (`display`: a display follows): `Some(list)` if it returned a node
177    /// list, which replaces line breaking; `None` otherwise.
178    fn linebreak_filter(
179        &self,
180        _engine: &mut Engine,
181        _head: nodes::NodeRef,
182        _display: bool,
183    ) -> Option<nodes::NodeRef> {
184        None
185    }
186
187    /// Calls the `mlist_to_hlist` callback on math list `head` in style
188    /// `style` (0–7); `None` if the callback failed or none is set.
189    fn mlist_to_hlist(
190        &self,
191        _engine: &mut Engine,
192        _head: nodes::NodeRef,
193        _style: i32,
194        _penalties: bool,
195    ) -> Option<nodes::NodeRef> {
196        None
197    }
198
199    /// Runs shell command `cmd` under the shell escape rules (as Lua's
200    /// `io.popen` does) and returns its output; `None` if it may not run.
201    fn pipe_read(&self, _engine: &mut Engine, _cmd: &[u8]) -> Option<Vec<u8>> {
202        None
203    }
204
205    /// Calls the `input_level_string` callback with input-stack depth `n`;
206    /// `None` if it returned no string.
207    fn input_level_string(&self, _engine: &mut Engine, _n: i64) -> Option<Vec<u8>> {
208        None
209    }
210
211    /// Calls the `glyph_info` callback for glyph node `p`: `Some(text)` is
212    /// what to print, `None` means the callback returned no string.
213    fn glyph_info(&self, _engine: &mut Engine, _p: nodes::NodeRef) -> Option<Vec<u8>> {
214        None
215    }
216}
217
218/// Result of a node-list filter callback.
219#[derive(Clone, Copy, Debug, PartialEq, Eq)]
220pub enum ListFilter {
221    /// Continue with this list (possibly the original head).
222    Keep(nodes::NodeRef),
223    /// The callback returned `false`: discard the list.
224    Flush,
225}
226
227impl Engine {
228    /// Whether the Lua callback `name` is set.
229    pub fn has_callback(&self, name: &str) -> bool {
230        self.lua.as_ref().is_some_and(|h| h.has_callback(name))
231    }
232
233    /// Calls the notification callback `name` (`buildpage_filter`,
234    /// `contribute_filter`) with `info`, if one is set.
235    pub fn notify_callback(&mut self, name: &str, info: &str) {
236        if let Some(h) = self.lua.clone()
237            && h.has_callback(name)
238            && let Err(e) = h.call_with_string(self, name, info)
239        {
240            self.lua_error(&e);
241        }
242    }
243
244    /// Calls `buildpage_filter` with `info` (where the page builder was
245    /// called from, observed), then builds the page.
246    pub fn build_page_from(&mut self, info: &str) {
247        self.notify_callback("buildpage_filter", info);
248        self.build_page();
249    }
250
251    /// Runs a node-list filter callback if one is registered (the hook
252    /// typesetting code calls at LuaTeX's callback points).
253    pub fn run_list_filter(
254        &mut self,
255        callback: &str,
256        head: nodes::NodeRef,
257        group: &str,
258    ) -> ListFilter {
259        match self.lua.clone() {
260            Some(h) if h.has_callback(callback) => h.list_filter(self, callback, head, group),
261            _ => ListFilter::Keep(head),
262        }
263    }
264}
265
266impl Engine {
267    /// Converts a finished formula's math list into an hlist, through the
268    /// `mlist_to_hlist` callback when one is set (LuaTeX calls it for the
269    /// outermost list of each formula and equation number; observed: not
270    /// for an empty one).
271    pub fn formula_to_hlist(
272        &mut self,
273        head: nodes::NodeRef,
274        style: i32,
275        penalties: bool,
276    ) -> nodes::NodeRef {
277        if head.is_null() {
278            return nodes::NodeRef::NULL;
279        }
280        if let Some(h) = self.lua.clone()
281            && h.has_callback("mlist_to_hlist")
282        {
283            if let Some(r) = h.mlist_to_hlist(self, head, style, penalties) {
284                if !r.is_null() {
285                    self.nodes.set_prev(r, nodes::NodeRef::NULL);
286                }
287                return r;
288            }
289            return nodes::NodeRef::NULL;
290        }
291        self.mlist_to_hlist(head, style, penalties)
292    }
293
294    /// Prints the input level before a line of macro tracing through the
295    /// `input_level_string` callback (LaTeX's `\tracingstacklevels`);
296    /// returns false if no callback is set. A newline in the string ends
297    /// the line.
298    pub fn print_input_level(&mut self) -> bool {
299        let Some(h) = self.lua.clone() else {
300            return false;
301        };
302        if !h.has_callback("input_level_string") {
303            return false;
304        }
305        let n = self.input.stack.len().saturating_sub(1) as i64;
306        if let Some(s) = h.input_level_string(self, n) {
307            for (i, part) in s.split(|&b| b == b'\n').enumerate() {
308                if i > 0 {
309                    self.printer.print_ln();
310                }
311                self.printer.print_bytes(part);
312            }
313        }
314        true
315    }
316
317    /// Runs the `glyph_info` callback for glyph `p` when one is set:
318    /// `None` without a callback, `Some(None)` when it returned no string.
319    pub fn run_glyph_info(&mut self, p: nodes::NodeRef) -> Option<Option<Vec<u8>>> {
320        match self.lua.clone() {
321            Some(h) if h.has_callback("glyph_info") => Some(h.glyph_info(self, p)),
322            _ => None,
323        }
324    }
325}
326
327/// A one-shot callback into the host, run with the engine borrowed.
328pub type EngineHook = Box<dyn FnOnce(&mut Engine)>;
329
330/// Shows diagnostics before the terminal is read (see
331/// [`Engine::prompt_hook`]).
332pub type PromptHook = Box<dyn FnMut(&[diag::Diagnostic], &str) -> bool>;
333
334/// The global state of a TeX run.
335pub struct Engine {
336    /// Control-sequence names.
337    pub hash: CsTable,
338    /// The table of equivalents.
339    pub eqtb: Eqtb,
340    /// The save stack and grouping state.
341    pub save: SaveStack,
342    /// The input stack.
343    pub input: InputState,
344    /// Terminal, log and `\write` output.
345    pub printer: Printer,
346    /// Error-handling state.
347    pub err: ErrorState,
348    /// Conditionals.
349    pub cond: CondState,
350    /// Files: `\openin`/`\read`, `\openout`, input file stack.
351    pub files: FileState,
352    /// The file finder.
353    pub kpse: Option<retex_kpath::Shared>,
354    /// The Lua interpreter, when available.
355    pub lua: Option<std::rc::Rc<dyn LuaHost>>,
356
357    /// The current command code (`cur_cmd`).
358    pub cur_cmd: Cmd,
359    /// The current modifier (`cur_chr`).
360    pub cur_chr: u32,
361    /// The current control sequence (`cur_cs`); [`hash::NO_CS`] for
362    /// characters.
363    pub cur_cs: CsId,
364    /// The current token (`cur_tok`).
365    pub cur_tok: Token,
366
367    /// The value most recently scanned (`cur_val`).
368    pub cur_val: i32,
369    /// The glue most recently scanned, when `cur_val_level` is glue.
370    pub cur_glue: glue::Glue,
371    /// The token list most recently scanned, when `cur_val_level` is tokens.
372    pub cur_toks: Option<token::TokenList>,
373    /// The level of `cur_val`.
374    pub cur_val_level: scan::ValLevel,
375
376    /// Well-known control sequences.
377    pub cs: init::KnownCs,
378    /// Whether the run is in IniTeX mode.
379    pub ini_version: bool,
380    /// Shell escape mode (see [`init::Options::shell_escape`]).
381    pub shell_escape: u8,
382    /// The format identification printed in the banner.
383    pub format_ident: String,
384    /// The job name, once known.
385    pub job_name: Option<Vec<u8>>,
386    /// Token saved by `\afterassignment` (`after_token`).
387    pub after_token: Token,
388    /// Number of nested expansions (LuaTeX's `expand_depth_count`).
389    pub expand_depth: u32,
390    /// The current mode (copy of the innermost nest level's mode).
391    pub mode: maincontrol::Mode,
392    /// The mode last shown by `show_cur_cmd_chr`.
393    pub shown_mode: maincontrol::Mode,
394    /// The format name (`\formatname`).
395    pub format_name: String,
396    /// Node memory.
397    pub nodes: nodes::Nodes,
398    /// The semantic nest; the last entry is the current list.
399    pub nest: Vec<maincontrol::ListState>,
400    /// Loaded fonts.
401    pub fonts: fonts::FontTable,
402    /// The `long_state` of the macro being called.
403    pub long_state: Cmd,
404    /// The control sequence being defined (for diagnostics).
405    pub def_ref_cs: CsId,
406    /// The partial token list printed by `runaway`.
407    pub runaway_list: Vec<Token>,
408    /// The control sequence last made by `\csname` (`\lastnamedcs`).
409    pub last_cs_name: CsId,
410    /// Frozen copies made by `\primitive`.
411    pub frozen_prims: std::collections::HashMap<Vec<u8>, CsId>,
412    /// Mark texts.
413    pub marks: typeset::Marks,
414    /// Set when a `\read` line has been consumed.
415    pub input_read_finished: bool,
416    /// Whether the long `\errmessage` help has been shown.
417    pub long_help_seen: bool,
418    /// `\deadcycles`.
419    pub dead_cycles: i32,
420    /// `\insertpenalties`.
421    pub insert_penalties: i32,
422    /// `\badness`.
423    pub last_badness: i32,
424    /// Token lists show a macro parameter character once, not doubled
425    /// (`\luaescapestring`).
426    pub single_mac_param: bool,
427    /// The direction the next box made by `hpack` or `vpackage` gets
428    /// (from `dir`/`bdir` of a box specification), or -1. It is set before
429    /// packing because over- and underfull box reports show it.
430    pub pack_dir: i8,
431    /// `\randomseed`: the seed `\setrandomseed` last gave (its absolute
432    /// value), or the one chosen at startup.
433    pub random_seed: i32,
434    /// The state of the random number generator (METAFONT's, see
435    /// [`retex_arith::random`]) behind `\uniformdeviate`, `\normaldeviate`
436    /// and the `tex` library's randomizers.
437    pub randoms: retex_arith::random::Randoms,
438    /// `\lastxpos`.
439    pub last_x_pos: i32,
440    /// `\lastypos`.
441    pub last_y_pos: i32,
442    /// The magnification frozen by `prepare_mag`.
443    pub mag_set: i32,
444    /// The font dimension located by `find_font_dimen`.
445    pub font_dimen_loc: Option<(u32, u32)>,
446    /// `\showboxdepth` in effect while displaying boxes.
447    pub depth_threshold: i32,
448    /// `\showboxbreadth` in effect while displaying boxes.
449    pub breadth_max: i32,
450    /// The paragraph-ending control sequence (`\partokenname`).
451    pub par_token_cs: CsId,
452    /// Whether `\dump` ended the run.
453    pub dump_requested: bool,
454    /// Integer parameters set from the command line when the job starts
455    /// ([`init::Options::int_params`]).
456    pub start_ints: Vec<(u32, i32)>,
457    /// `-output-comment`.
458    pub output_comment: Option<String>,
459    /// The job's start time (`\time`, the banner, the PDF creation date).
460    pub start_time: clock::StartTime,
461    /// The page builder.
462    pub page: typeset::PageState,
463    /// Lua functions registered for `\luafunction` (by number).
464    pub lua_functions: std::collections::HashMap<i32, u32>,
465    /// Called to store the format when `\dump` is executed.
466    pub format_hook: Option<EngineHook>,
467    /// Called at the end of the run to finish output files.
468    pub finish_hook: Option<EngineHook>,
469    /// Non-immediate `\write`s waiting for shipout.
470    pub deferred_writes: Vec<(i32, token::TokenList)>,
471    /// Text directions opened in the current box: (group level,
472    /// direction, the text direction before), innermost last (see
473    /// [`dirs`]).
474    pub text_dirs: Vec<(u16, i8, i8)>,
475    /// Box resources to write after the page or form being written, one
476    /// list per open page or form.
477    pub pending_forms: Vec<Vec<u32>>,
478    /// Objects referred to (`\pdfextension refobj`) in the box resources
479    /// being written, one list per open form.
480    pub form_objects: Vec<Vec<pdfext::PdfObject>>,
481    /// The `text_dirs` of the enclosing boxes.
482    pub outer_text_dirs: Vec<Vec<(u16, i8, i8)>>,
483    /// The math and text directions outside the open formulas, with the
484    /// save level of each formula's group.
485    pub outer_math_dirs: Vec<(u16, i32, i32)>,
486    /// Contents of files written by `\write`, for the pass driver.
487    pub written_sink: files::WrittenSink,
488    /// State of the typesetting routines.
489    pub ts: typeset::TsState,
490    /// Tokens printed from Lua waiting to be pushed as a token list.
491    pub pending_lua_tokens: Option<Vec<Token>>,
492    /// Default output mode: errors and warnings recorded for the driver
493    /// (`None` in compatibility mode).
494    pub diagnostics: Option<Vec<diag::Diagnostic>>,
495    /// Default output mode, interactive: shows the diagnostics recorded
496    /// since the last prompt, and the prompt, before the terminal is read.
497    /// Returns `false` if it showed nothing (the error only repeats one
498    /// already reported): TeX then goes on without asking.
499    pub prompt_hook: Option<PromptHook>,
500    /// Default output mode: called when a page is shipped or a file opened,
501    /// to show progress and the errors recorded so far.
502    pub progress_hook: Option<fn(&mut Engine)>,
503    /// The number of diagnostics the prompt hook has shown.
504    pub diagnostics_shown: usize,
505    /// Whether the terminal was read (an error or a `\read` prompt).
506    pub prompted: bool,
507    /// Image resources (`\saveimageresource`), in the order saved (their
508    /// indices are their object numbers).
509    pub images: Vec<image::ImageResource>,
510    /// `\lastsavedimageresourceindex`.
511    pub last_image_index: i32,
512    /// `\lastsavedimageresourcepages`.
513    pub last_image_pages: i32,
514    /// Box resources (`\saveboxresource`), in the order saved (their
515    /// indices are their object numbers).
516    pub box_resources: Vec<image::BoxResource>,
517    /// `\lastsavedboxresourceindex`.
518    pub last_box_index: i32,
519    /// Scratch buffer for macro arguments (reused by `macro_call`).
520    pub arg_scratch: Vec<Token>,
521    /// Scratch buffer for the argument list of a macro call.
522    pub param_scratch: Vec<token::TokenList>,
523    /// The file of each `\write` stream opened (see [`snapshot`]).
524    pub write_paths: Vec<Option<std::path::PathBuf>>,
525    /// The macro at whose expansion [`snapshot_hook`](Self::snapshot_hook)
526    /// runs (LaTeX's `\document`).
527    pub snapshot_cs: Option<CsId>,
528    /// Called once, when `snapshot_cs` is expanded (see [`snapshot`]).
529    pub snapshot_hook: Option<snapshot::Hook>,
530    /// The file and line the user asked to edit at an error (`E`); the
531    /// program runs the editor at the end.
532    pub edit_request: Option<(Vec<u8>, i32)>,
533    /// The output file written at the end: name, pages and bytes.
534    pub output_written: Option<(String, u32, u64)>,
535    /// The line number reported while reading Lua output.
536    pub input_line_for_lua: i32,
537    /// Internal tokens made by `\pdfvariable`, by variable name.
538    pub backend_cs: std::collections::HashMap<&'static str, CsId>,
539    /// Nesting depth of local control loops (`tex.runtoks`).
540    pub local_level: u32,
541    /// The box `token.scan_list` scanned, once finished.
542    pub lua_scanned_box: Option<nodes::NodeRef>,
543    /// Whether `token.scan_list` runs local control for its box.
544    pub lua_scan_waiting: bool,
545    /// The output backend (PDF writer), if any.
546    pub backend: Option<Box<dyn shipout::Backend>>,
547    /// The DVI writer (`\outputmode=0`).
548    pub dvi: dvi::DviWriter,
549    /// Hyphenation languages, by `\language` number.
550    pub languages: Vec<Option<retex_hyph::Language>>,
551    /// Document-level PDF data from `\pdfextension`.
552    pub pdf: pdfext::PdfState,
553    /// Current values of the colour stacks during shipout.
554    pub colorstack_values: std::collections::HashMap<i32, Vec<Vec<u8>>>,
555    /// Number of pages shipped out.
556    pub total_pages: u32,
557    /// Muglue values of `\Umath…` spacing parameters.
558    pub math_glues: Vec<glue::Glue>,
559    /// Whether the parameters set by the first formula are initialized
560    /// (see [`mathparams`]).
561    pub math_params_initialized: bool,
562    /// SyncTeX output.
563    pub synctex: synctex::Synctex,
564}
565
566impl std::fmt::Debug for Engine {
567    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
568        f.debug_struct("Engine")
569            .field("cur_cmd", &self.cur_cmd)
570            .field("cur_chr", &self.cur_chr)
571            .finish_non_exhaustive()
572    }
573}