mathtex-engine 0.1.0

Portable TeX engine wrapper for mathtex: profiles, formats, resources, fonts, fragment sessions, IR lowering
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::profile::ProfileId;
use crate::resource::ResourceProvider;

/// Cached initialized macro/package state for an engine profile.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FormatImage {
    /// Format identifier string.
    pub id: String,
    /// Engine profile this format targets.
    pub profile: ProfileId,
    /// Packages incorporated during format initialization.
    pub packages: Vec<FormatPackage>,
    /// Opaque initialized state snapshot.
    pub state: FormatState,
}

impl FormatImage {
    /// Create an empty format image for early bootstrapping and tests.
    #[must_use]
    pub fn empty(id: impl Into<String>, profile: ProfileId) -> Self {
        Self {
            id: id.into(),
            profile,
            packages: Vec::new(),
            state: FormatState::Empty,
        }
    }

    /// Create a format image wrapping an already-built structured snapshot.
    #[must_use]
    pub fn with_snapshot(
        id: impl Into<String>,
        profile: ProfileId,
        snapshot: FormatSnapshot,
    ) -> Self {
        Self {
            id: id.into(),
            profile,
            packages: snapshot.packages.clone(),
            state: FormatState::Structured(snapshot),
        }
    }

    /// Create a [`FormatInitializer`] builder for this format id and profile.
    #[must_use]
    pub fn initializer(id: impl Into<String>, profile: ProfileId) -> FormatInitializer {
        FormatInitializer::new(id, profile)
    }

    /// Start building the plain TeX format; the provider must supply `plain.tex`.
    #[must_use]
    pub fn plain(profile: ProfileId) -> FormatInitializer {
        Self::initializer("plain", profile).preload_tex_input("plain.tex")
    }

    /// Start building the LaTeX format; the provider must supply `latex.ltx`.
    #[must_use]
    pub fn latex(profile: ProfileId) -> FormatInitializer {
        Self::initializer("latex", profile).preload_tex_input("latex.ltx")
    }

    /// Clone the format's initialized state into a fresh mutable `SessionState`.
    #[must_use]
    pub fn instantiate_session_state(&self) -> SessionState {
        match &self.state {
            FormatState::Empty => SessionState::default(),
            FormatState::Snapshot(bytes) => SessionState {
                opaque_engine_state: bytes.clone(),
                ..SessionState::default()
            },
            FormatState::Structured(snapshot) => SessionState {
                macros: snapshot.macros.clone(),
                packages: snapshot.packages.clone(),
                resources: snapshot.resources.clone(),
                opaque_engine_state: snapshot.opaque_engine_state.clone(),
                registers: snapshot.registers.clone(),
            },
        }
    }
}

/// Builder for a cached [`FormatImage`] initialized from package resources.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FormatInitializer {
    id: String,
    profile: ProfileId,
    tex_inputs: Vec<String>,
    packages: Vec<String>,
    macros: Vec<MacroDefinition>,
    registers: RegisterSnapshot,
    opaque_engine_state: Vec<u8>,
}

impl FormatInitializer {
    /// Create a new empty format initializer for the given id and profile.
    #[must_use]
    pub fn new(id: impl Into<String>, profile: ProfileId) -> Self {
        Self {
            id: id.into(),
            profile,
            tex_inputs: Vec::new(),
            packages: Vec::new(),
            macros: Vec::new(),
            registers: RegisterSnapshot::default(),
            opaque_engine_state: Vec::new(),
        }
    }

    /// Queue a TeX input file to load during format initialization.
    #[must_use]
    pub fn preload_tex_input(mut self, input: impl Into<String>) -> Self {
        self.tex_inputs.push(input.into());
        self
    }

    /// Queue a package style file to load during format initialization.
    #[must_use]
    pub fn preload_package(mut self, package: impl Into<String>) -> Self {
        self.packages.push(package.into());
        self
    }

    /// Add a macro definition to embed in the format.
    #[must_use]
    pub fn macro_definition(
        mut self,
        name: impl Into<String>,
        replacement: impl Into<Vec<u8>>,
    ) -> Self {
        self.macros.push(MacroDefinition {
            name: name.into(),
            replacement: replacement.into(),
        });
        self
    }

    /// Set the initial register snapshot to embed in the format.
    #[must_use]
    pub fn registers(mut self, registers: RegisterSnapshot) -> Self {
        self.registers = registers;
        self
    }

    /// Append opaque engine seed bytes to embed in the format.
    #[must_use]
    pub fn opaque_engine_state(mut self, bytes: impl Into<Vec<u8>>) -> Self {
        self.opaque_engine_state.extend(bytes.into());
        self
    }

    /// Build the cached format by resolving preloaded resources once.
    pub fn build<R>(self, resources: &R) -> Result<FormatImage, FormatInitError>
    where
        R: ResourceProvider,
    {
        let mut packages = Vec::with_capacity(self.packages.len());
        let mut format_resources = Vec::with_capacity(self.tex_inputs.len() + self.packages.len());
        let mut opaque_engine_state = self.opaque_engine_state;

        for input in self.tex_inputs {
            let resource =
                resources
                    .read_tex_input(&input)
                    .map_err(|error| FormatInitError::TexInput {
                        name: input.clone(),
                        message: format!("{error:?}"),
                    })?;
            opaque_engine_state.extend(&resource.bytes);
            format_resources.push(FormatResource {
                name: resource.canonical_name,
                kind: FormatResourceKind::TexInput,
                bytes: resource.bytes,
            });
        }

        for package in self.packages {
            let resource_name = package_resource_name(&package);
            let resource = resources.read_package(&resource_name).map_err(|error| {
                FormatInitError::Package {
                    name: resource_name.clone(),
                    message: format!("{error:?}"),
                }
            })?;
            let package = FormatPackage {
                name: package,
                version: None,
            };
            opaque_engine_state.extend(&resource.bytes);
            format_resources.push(FormatResource {
                name: resource.canonical_name,
                kind: FormatResourceKind::Package,
                bytes: resource.bytes,
            });
            packages.push(package);
        }

        let snapshot = FormatSnapshot {
            macros: self.macros,
            packages,
            resources: format_resources,
            opaque_engine_state,
            registers: self.registers,
        };

        Ok(FormatImage::with_snapshot(self.id, self.profile, snapshot))
    }
}

/// Package that contributed to a cached format.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FormatPackage {
    /// Package name.
    pub name: String,
    /// Package version, if known.
    pub version: Option<String>,
}

/// Structured initialized format state that can be cloned into sessions.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FormatSnapshot {
    /// Macro definitions or macro state records preloaded into the format.
    pub macros: Vec<MacroDefinition>,
    /// Packages incorporated during format initialization.
    pub packages: Vec<FormatPackage>,
    /// Resources captured during format initialization.
    pub resources: Vec<FormatResource>,
    /// Opaque bytes for generated engine state during bootstrap.
    pub opaque_engine_state: Vec<u8>,
    /// Register values captured at format initialization time.
    pub registers: RegisterSnapshot,
}

/// Resource captured while initializing a cached format.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FormatResource {
    /// Canonical resource name.
    pub name: String,
    /// Role of this resource in the format.
    pub kind: FormatResourceKind,
    /// Resource bytes captured at format initialization time.
    pub bytes: Vec<u8>,
}

/// Resource role captured in a cached format.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FormatResourceKind {
    /// A TeX input file loaded during format initialization.
    TexInput,
    /// A package style file loaded during format initialization.
    Package,
}

/// Macro definition captured in a cached format.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MacroDefinition {
    /// Control sequence name without leading backslash.
    pub name: String,
    /// Replacement text or opaque translated representation.
    pub replacement: Vec<u8>,
}

/// Register values captured in a cached format.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RegisterSnapshot {
    /// Snapshot of TeX count registers.
    pub counts: Vec<i32>,
    /// Snapshot of TeX dimension registers in scaled points.
    pub dimensions: Vec<i32>,
    /// Snapshot of TeX token list registers as opaque bytes.
    pub token_lists: Vec<Vec<u8>>,
}

/// Mutable session state cloned from [`FormatImage`].
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SessionState {
    /// Macro definitions active in this session.
    pub macros: Vec<MacroDefinition>,
    /// Packages loaded into this session.
    pub packages: Vec<FormatPackage>,
    /// Resources captured for this session.
    pub resources: Vec<FormatResource>,
    /// Opaque engine state bytes for this session.
    pub opaque_engine_state: Vec<u8>,
    /// Register values active in this session.
    pub registers: RegisterSnapshot,
}

impl SessionState {
    /// Add or replace a macro in the session state.
    pub fn define_macro(&mut self, name: impl Into<String>, replacement: impl Into<Vec<u8>>) {
        let name = name.into();
        let replacement = replacement.into();
        if let Some(existing) = self
            .macros
            .iter_mut()
            .find(|macro_def| macro_def.name == name)
        {
            existing.replacement = replacement;
            return;
        }

        self.macros.push(MacroDefinition { name, replacement });
    }

    /// Look up a macro definition by control sequence name.
    #[must_use]
    pub fn macro_definition(&self, name: &str) -> Option<&MacroDefinition> {
        self.macros.iter().find(|macro_def| macro_def.name == name)
    }

    /// Look up a loaded package by name.
    #[must_use]
    pub fn package(&self, name: &str) -> Option<&FormatPackage> {
        self.packages.iter().find(|package| package.name == name)
    }

    /// Look up a captured resource by canonical name.
    #[must_use]
    pub fn resource(&self, name: &str) -> Option<&FormatResource> {
        self.resources.iter().find(|resource| resource.name == name)
    }
}

/// Format initialization failure.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FormatInitError {
    /// A requested TeX input could not be loaded.
    TexInput {
        /// Name of the TeX input file.
        name: String,
        /// Description of the load failure.
        message: String,
    },
    /// A requested package could not be loaded.
    Package {
        /// Name of the package resource file.
        name: String,
        /// Description of the load failure.
        message: String,
    },
}

fn package_resource_name(name: &str) -> String {
    if name.ends_with(".sty") {
        name.to_string()
    } else {
        format!("{name}.sty")
    }
}

/// Opaque initialized state snapshot.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum FormatState {
    /// Empty state used for bootstrapping and tests.
    #[default]
    Empty,
    /// Opaque engine bytes; reserved for a future structured snapshot.
    Snapshot(Vec<u8>),
    /// Structured snapshot used to initialize each session state.
    Structured(FormatSnapshot),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::profile::ProfileId;
    use crate::{InMemoryResourceProvider, ResourceKind};

    #[test]
    fn format_image_instantiates_mutable_session_state() {
        let snapshot = FormatSnapshot {
            macros: alloc::vec![MacroDefinition {
                name: "text".into(),
                replacement: b"text macro".to_vec(),
            }],
            packages: alloc::vec![FormatPackage {
                name: "latex".into(),
                version: Some("2026".into()),
            }],
            resources: alloc::vec![FormatResource {
                name: "plain.tex".into(),
                kind: FormatResourceKind::TexInput,
                bytes: b"plain".to_vec(),
            }],
            opaque_engine_state: b"engine".to_vec(),
            registers: RegisterSnapshot {
                counts: alloc::vec![1, 2],
                dimensions: alloc::vec![3],
                token_lists: alloc::vec![b"tokens".to_vec()],
            },
        };
        let format = FormatImage::with_snapshot("latex", ProfileId("tex"), snapshot);

        let mut session = format.instantiate_session_state();
        session.define_macro("text", b"changed".to_vec());
        session.define_macro("frac", b"fraction".to_vec());

        assert_eq!(
            session
                .macro_definition("text")
                .expect("text macro")
                .replacement,
            b"changed".to_vec()
        );
        assert_eq!(
            session
                .macro_definition("frac")
                .expect("frac macro")
                .replacement,
            b"fraction".to_vec()
        );
        assert_eq!(
            session.resource("plain.tex").expect("plain resource").bytes,
            b"plain".to_vec()
        );
        assert_eq!(session.registers.counts, alloc::vec![1, 2]);

        match &format.state {
            FormatState::Structured(snapshot) => {
                assert_eq!(snapshot.macros[0].replacement, b"text macro".to_vec());
                assert_eq!(snapshot.resources[0].bytes, b"plain".to_vec());
                assert_eq!(snapshot.registers.counts, alloc::vec![1, 2]);
            }
            other => panic!("unexpected format state: {other:?}"),
        }
    }

    #[test]
    fn initializer_preloads_packages_through_resource_provider() {
        let resources = InMemoryResourceProvider::new()
            .with_resource("latex.ltx.sty", ResourceKind::Package, b"latex")
            .with_resource("amsmath.sty", ResourceKind::Package, b"ams");

        let format = FormatImage::initializer("latex+amsmath", ProfileId("tex"))
            .preload_package("latex.ltx.sty")
            .preload_package("amsmath")
            .macro_definition("text", b"text macro")
            .opaque_engine_state(b"seed".to_vec())
            .build(&resources)
            .expect("format should initialize");

        assert_eq!(format.packages.len(), 2);
        assert_eq!(format.packages[1].name, "amsmath");
        let session = format.instantiate_session_state();
        assert!(session.package("amsmath").is_some());
        assert!(session.resource("amsmath.sty").is_some());
        assert_eq!(
            session
                .macro_definition("text")
                .expect("text macro")
                .replacement,
            b"text macro".to_vec()
        );
        assert_eq!(session.opaque_engine_state, b"seedlatexams".to_vec());
    }

    #[test]
    fn initializer_reports_missing_preloaded_package() {
        let resources = InMemoryResourceProvider::new();

        let error = FormatImage::initializer("latex+missing", ProfileId("tex"))
            .preload_package("missing")
            .build(&resources)
            .expect_err("missing package should fail initialization");

        match error {
            FormatInitError::TexInput { .. } => panic!("unexpected TeX input error"),
            FormatInitError::Package { name, message } => {
                assert_eq!(name, "missing.sty");
                assert!(message.contains("NotFound"));
            }
        }
    }

    #[test]
    fn initializer_preloads_tex_input_resources() {
        let resources = InMemoryResourceProvider::new().with_resource(
            "plain.tex",
            ResourceKind::TexInput,
            br"\def\plainchar{A}".to_vec(),
        );

        let format = FormatImage::initializer("plain", ProfileId("tex"))
            .preload_tex_input("plain.tex")
            .build(&resources)
            .expect("format should preload TeX input bytes");

        let snapshot = match format.state {
            FormatState::Structured(snapshot) => snapshot,
            other => panic!("unexpected format state: {other:?}"),
        };
        assert_eq!(snapshot.resources.len(), 1);
        assert_eq!(snapshot.resources[0].name, "plain.tex");
        assert_eq!(snapshot.resources[0].kind, FormatResourceKind::TexInput);
        assert_eq!(snapshot.resources[0].bytes, br"\def\plainchar{A}".to_vec());
    }

    #[test]
    fn real_format_constructors_preload_engine_entrypoints() {
        let resources = InMemoryResourceProvider::new()
            .with_resource("plain.tex", ResourceKind::TexInput, br"\dump".to_vec())
            .with_resource("latex.ltx", ResourceKind::TexInput, br"\dump".to_vec());

        let plain = FormatImage::plain(ProfileId("tex"))
            .build(&resources)
            .expect("plain format entrypoint should resolve");
        let latex = FormatImage::latex(ProfileId("tex"))
            .build(&resources)
            .expect("latex format entrypoint should resolve");

        assert_eq!(plain.id, "plain");
        assert_eq!(latex.id, "latex");

        let plain_snapshot = match plain.state {
            FormatState::Structured(snapshot) => snapshot,
            other => panic!("unexpected plain format state: {other:?}"),
        };
        let latex_snapshot = match latex.state {
            FormatState::Structured(snapshot) => snapshot,
            other => panic!("unexpected latex format state: {other:?}"),
        };

        assert_eq!(plain_snapshot.resources[0].name, "plain.tex");
        assert_eq!(latex_snapshot.resources[0].name, "latex.ltx");
    }
}