mathtex-engine 0.1.3

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
use alloc::string::String;
use alloc::vec::Vec;
use core::cell::RefCell;

/// Host runtime services that must remain separate from engine semantics.
pub trait Platform {
    /// Returns the diagnostic sink for this platform.
    fn diagnostics(&self) -> &dyn DiagnosticSink;

    /// Returns host resource limits, defaulting to `HostLimits::default()`.
    fn limits(&self) -> HostLimits {
        HostLimits::default()
    }

    /// Deterministic clock value visible to generated TeX code.
    fn clock(&self) -> HostClock {
        HostClock::default()
    }

    /// Called before the engine begins line break iteration for a paragraph.
    fn linebreak_start(&self, _request: LinebreakRequest<'_>) {}

    /// Returns the next line break position, or `None` to use the built in algorithm.
    fn linebreak_next(&self) -> Option<i32> {
        None
    }

    /// Returns the box for a host owned token, or `None` to typeset a zero size box.
    fn host_box(&self, _request: HostBoxRequest) -> Option<HostBox> {
        None
    }
}

impl<T> Platform for &T
where
    T: Platform,
{
    fn diagnostics(&self) -> &dyn DiagnosticSink {
        (*self).diagnostics()
    }

    fn limits(&self) -> HostLimits {
        (*self).limits()
    }

    fn clock(&self) -> HostClock {
        (*self).clock()
    }

    fn linebreak_start(&self, request: LinebreakRequest<'_>) {
        (*self).linebreak_start(request);
    }

    fn linebreak_next(&self) -> Option<i32> {
        (*self).linebreak_next()
    }

    fn host_box(&self, request: HostBoxRequest) -> Option<HostBox> {
        (*self).host_box(request)
    }
}

/// Receiver for diagnostic messages emitted during engine execution.
pub trait DiagnosticSink {
    /// Accepts and handles a single diagnostic message.
    fn emit(&self, diagnostic: Diagnostic);
}

/// A single diagnostic message emitted by the engine.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Diagnostic {
    /// Severity level of the diagnostic.
    pub severity: DiagnosticSeverity,
    /// Human readable diagnostic text.
    pub message: String,
}

/// Severity level of a diagnostic message.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DiagnosticSeverity {
    /// Informational message, no action required.
    Info,
    /// Warning that may indicate a problem but does not stop rendering.
    Warning,
    /// Error that prevented successful rendering.
    Error,
}

/// Host imposed upper bounds on engine resource consumption.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HostLimits {
    /// Maximum input bytes accepted for one fragment.
    pub max_input_bytes: usize,
    /// Maximum resource requests made while executing one fragment.
    pub max_resource_requests: usize,
    /// Maximum layout nodes an engine session should emit.
    pub max_layout_nodes: usize,
}

impl Default for HostLimits {
    fn default() -> Self {
        Self {
            max_input_bytes: 1 << 20,
            max_resource_requests: 256,
            max_layout_nodes: 1 << 20,
        }
    }
}

/// Host clock value in seconds and microseconds.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct HostClock {
    /// Seconds since the host defined epoch.
    pub seconds: i32,
    /// Microseconds within the current second.
    pub micros: i32,
}

/// Parameters for a host driven line break request passed to the platform.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LinebreakRequest<'a> {
    /// Generated engine font identifier active for the text.
    pub font: i32,
    /// Generated engine locale identifier active for the text.
    pub locale: i32,
    /// Text slice owned by generated engine memory for this call.
    pub text: &'a [u16],
}

/// Style context a host box is placed in, display math collapses to text.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HostBoxStyle {
    /// Text and display math style.
    Text,
    /// First level script style.
    Script,
    /// Second level script style.
    ScriptScript,
}

/// Parameters for a host box request passed to the platform.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HostBoxRequest {
    /// Opaque host owned token identifier scanned from `\hostbox{...}`.
    pub token: i32,
    /// Math style context at the placement site.
    pub style: HostBoxStyle,
    /// Font size context in scaled points at the placement site.
    pub font_size: i32,
}

/// A glyph inside a host box, offsets in scaled points from the box baseline origin, y down.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HostBoxGlyph {
    /// Glyph identifier in the run's font.
    pub glyph: u16,
    /// Horizontal offset from the box baseline origin.
    pub x: i32,
    /// Vertical offset from the baseline, positive downward.
    pub y: i32,
    /// Horizontal advance in scaled points.
    pub advance: i32,
}

/// One same font glyph sequence inside a host box.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HostBoxRun {
    /// Host font name carried into the fragment's glyph run.
    pub font_name: String,
    /// Font size for the run in scaled points.
    pub font_size: i32,
    /// Glyphs of the run in visual order.
    pub glyphs: Vec<HostBoxGlyph>,
}

/// A rule inside a host box, `(x, y)` is the bottom left corner, y down from the baseline.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HostBoxRule {
    /// Horizontal offset of the rule's left edge from the box baseline origin.
    pub x: i32,
    /// Vertical offset of the rule's bottom edge, positive downward.
    pub y: i32,
    /// Rule width in scaled points.
    pub width: i32,
    /// Rule height in scaled points, extending upward from `y`.
    pub height: i32,
}

/// Host supplied box for one `\hostbox` token: metrics plus render data for the fragment.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HostBox {
    /// Box width in scaled points.
    pub width: i32,
    /// Box height above the baseline in scaled points.
    pub height: i32,
    /// Box depth below the baseline in scaled points.
    pub depth: i32,
    /// Glyph runs rendered inside the box.
    pub runs: Vec<HostBoxRun>,
    /// Rules rendered inside the box.
    pub rules: Vec<HostBoxRule>,
}

/// Signals that a host configured resource limit was exceeded.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum LimitError {
    /// Input byte count exceeded the configured maximum.
    InputTooLarge {
        /// Actual byte count seen.
        actual: usize,
        /// Configured byte limit.
        limit: usize,
    },
    /// Resource request count exceeded the configured maximum.
    TooManyResourceRequests {
        /// Actual request count.
        actual: usize,
        /// Configured request limit.
        limit: usize,
    },
    /// Layout node count exceeded the configured maximum.
    TooManyLayoutNodes {
        /// Actual node count.
        actual: usize,
        /// Configured node limit.
        limit: usize,
    },
}

/// Diagnostic sink that discards all messages.
#[derive(Clone, Copy, Debug, Default)]
pub struct NoopDiagnosticSink;

impl DiagnosticSink for NoopDiagnosticSink {
    fn emit(&self, _diagnostic: Diagnostic) {}
}

/// Diagnostic sink that accumulates messages, usable without std.
#[derive(Debug, Default)]
pub struct CollectingDiagnosticSink {
    diagnostics: RefCell<Vec<Diagnostic>>,
}

impl CollectingDiagnosticSink {
    /// Creates an empty collecting sink.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns a copy of all accumulated diagnostics without consuming them.
    #[must_use]
    pub fn snapshot(&self) -> Vec<Diagnostic> {
        self.diagnostics.borrow().clone()
    }

    /// Removes and returns all accumulated diagnostics.
    #[must_use]
    pub fn drain(&self) -> Vec<Diagnostic> {
        self.diagnostics.borrow_mut().drain(..).collect()
    }

    /// Returns the number of accumulated diagnostics.
    #[must_use]
    pub fn len(&self) -> usize {
        self.diagnostics.borrow().len()
    }

    /// Returns true when no diagnostics have been collected.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.diagnostics.borrow().is_empty()
    }

    /// Discards all accumulated diagnostics.
    pub fn clear(&self) {
        self.diagnostics.borrow_mut().clear();
    }
}

impl DiagnosticSink for CollectingDiagnosticSink {
    fn emit(&self, diagnostic: Diagnostic) {
        self.diagnostics.borrow_mut().push(diagnostic);
    }
}

/// Platform implementation composed from a diagnostic sink, host limits, and a clock.
#[derive(Clone, Debug)]
pub struct ConfigurablePlatform<D> {
    diagnostics: D,
    limits: HostLimits,
    clock: HostClock,
}

impl<D> ConfigurablePlatform<D> {
    /// Creates a platform with the given diagnostic sink and limits.
    #[must_use]
    pub fn new(diagnostics: D, limits: HostLimits) -> Self {
        Self {
            diagnostics,
            limits,
            clock: HostClock::default(),
        }
    }

    /// Creates a platform with the given diagnostic sink and default limits.
    #[must_use]
    pub fn with_diagnostics(diagnostics: D) -> Self {
        Self {
            diagnostics,
            limits: HostLimits::default(),
            clock: HostClock::default(),
        }
    }

    /// Returns a copy of this platform with the clock set to `clock`.
    #[must_use]
    pub fn with_clock(mut self, clock: HostClock) -> Self {
        self.clock = clock;
        self
    }

    /// Returns a reference to the underlying diagnostic sink.
    #[must_use]
    pub fn diagnostic_sink(&self) -> &D {
        &self.diagnostics
    }

    /// Returns the configured host limits.
    #[must_use]
    pub fn host_limits(&self) -> HostLimits {
        self.limits
    }

    /// Returns the configured host clock.
    #[must_use]
    pub fn host_clock(&self) -> HostClock {
        self.clock
    }
}

impl<D> Platform for ConfigurablePlatform<D>
where
    D: DiagnosticSink,
{
    fn diagnostics(&self) -> &dyn DiagnosticSink {
        &self.diagnostics
    }

    fn limits(&self) -> HostLimits {
        self.limits
    }

    fn clock(&self) -> HostClock {
        self.clock
    }
}

/// Minimal deterministic platform for tests, embedded use, and early bootstrap.
#[derive(Clone, Copy, Debug, Default)]
pub struct NoopPlatform {
    diagnostics: NoopDiagnosticSink,
    limits: HostLimits,
    clock: HostClock,
}

impl NoopPlatform {
    /// Creates a noop platform with the given host limits.
    #[must_use]
    pub fn with_limits(limits: HostLimits) -> Self {
        Self {
            diagnostics: NoopDiagnosticSink,
            limits,
            clock: HostClock::default(),
        }
    }

    /// Creates a noop platform with the given clock value.
    #[must_use]
    pub fn with_clock(clock: HostClock) -> Self {
        Self {
            diagnostics: NoopDiagnosticSink,
            limits: HostLimits::default(),
            clock,
        }
    }
}

impl Platform for NoopPlatform {
    fn diagnostics(&self) -> &dyn DiagnosticSink {
        &self.diagnostics
    }

    fn limits(&self) -> HostLimits {
        self.limits
    }

    fn clock(&self) -> HostClock {
        self.clock
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn collecting_diagnostic_sink_records_snapshots_and_drains() {
        let sink = CollectingDiagnosticSink::new();

        sink.emit(Diagnostic {
            severity: DiagnosticSeverity::Warning,
            message: "missing glyph".to_string(),
        });

        assert_eq!(sink.len(), 1);
        assert_eq!(sink.snapshot()[0].message, "missing glyph");
        assert_eq!(sink.drain()[0].severity, DiagnosticSeverity::Warning);
        assert!(sink.is_empty());
    }
}