latex-rust 3.0.2

Pure-Rust LaTeX math renderer. Parse to AST, TeX-faithful layout on exact rationals, emit SVG, PNG, or egui shapes. No JavaScript, no webview, no runtime.
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
//! `MathBox` → PNG via `tiny-skia`. Feature `png` only.
//!
//! Pixel coordinates are [`Dim`](crate::Dim) until the tiny-skia call, then
//! IEEE-754 bits via [`Dim::to_ieee32_bits`]. No layout `f32`/`f64` arithmetic.
//!
//! SIMD is automatic: AVX2 on x86_64, NEON on Apple Silicon. No compile flags.

use std::collections::HashMap;

use skrifa::outline::OutlinePen;
use tiny_skia::{FillRule, Paint, Path, PathBuilder, Pixmap, Rect, Stroke, Transform};

use super::{PngBackground, PngOptions};
use crate::color::Color;
use crate::dim::Dim;
use crate::error::{Error, FontError};
use crate::font::MathFont;
use crate::layout::{BoxContent, MathBox};

pub(super) fn render(
    tree: &MathBox,
    font: &MathFont,
    options: &PngOptions,
) -> Result<Vec<u8>, Error> {
    validate_options(options)?;
    let em_px = pixels_per_em(&options.font_size_pt, &options.dpi);
    let fu_px = &em_px / &Dim::from_i64(i64::from(font.units_per_em()));
    let w_dim = &tree.width * &em_px;
    let h_dim = (&tree.height + &tree.depth) * &em_px;
    let w = w_dim.ceil_to_u32()?.max(1);
    let h = h_dim.ceil_to_u32()?.max(1);
    let mut pixmap = Pixmap::new(w, h).ok_or_else(|| Error::InvalidOption {
        what: format!("png pixmap {w}x{h}"),
    })?;
    fill_background(&mut pixmap, options.background);
    let baseline = &tree.height * &em_px;
    let mut cache: HashMap<u16, Path> = HashMap::new();
    emit(
        tree,
        font,
        &em_px,
        &fu_px,
        &Dim::zero(),
        &baseline,
        options.color,
        &mut cache,
        &mut pixmap,
    )?;
    pixmap.encode_png().map_err(|_| Error::Unsupported {
        what: "png encode".into(),
    })
}

fn validate_options(options: &PngOptions) -> Result<(), Error> {
    if options.dpi.is_nan() || options.font_size_pt.is_nan() {
        return Err(Error::InvalidOption {
            what: "png dpi or font size is NaN".into(),
        });
    }
    if options.dpi.is_zero()
        || matches!(
            options.dpi.cmp(&Dim::zero()),
            Some(core::cmp::Ordering::Less) | Some(core::cmp::Ordering::Equal)
        )
    {
        return Err(Error::InvalidOption {
            what: "png dpi must be positive".into(),
        });
    }
    if matches!(
        options.dpi.cmp(&Dim::from_i64(2400)),
        Some(core::cmp::Ordering::Greater)
    ) {
        return Err(Error::InvalidOption {
            what: "png dpi exceeds 2400".into(),
        });
    }
    Ok(())
}

fn pixels_per_em(font_size_pt: &Dim, dpi: &Dim) -> Dim {
    font_size_pt * dpi / Dim::from_i64(72)
}

fn fill_background(pixmap: &mut Pixmap, bg: PngBackground) {
    match bg {
        PngBackground::Transparent => {}
        PngBackground::White => pixmap.fill(tiny_skia::Color::WHITE),
        PngBackground::Color(c) => pixmap.fill(skia_color(c, 255)),
    }
}

fn skia_color(c: Color, a: u8) -> tiny_skia::Color {
    let [r, g, b, _] = c.to_rgba8();
    tiny_skia::Color::from_rgba8(r, g, b, a)
}

fn paint(c: Color) -> Paint<'static> {
    let mut p = Paint::default();
    p.set_color(skia_color(c, 255));
    p.anti_alias = true;
    p
}

fn px(d: &Dim) -> f32 {
    f32::from_bits(d.to_ieee32_bits())
}

struct PngTask<'a> {
    bx: &'a MathBox,
    origin_x: Dim,
    parent_baseline: Dim,
    fill: Color,
}

#[allow(clippy::too_many_arguments)]
fn emit(
    bx: &MathBox,
    font: &MathFont,
    em_px: &Dim,
    fu_px: &Dim,
    origin_x: &Dim,
    parent_baseline: &Dim,
    fill: Color,
    cache: &mut HashMap<u16, Path>,
    pixmap: &mut Pixmap,
) -> Result<(), Error> {
    let mut stack = vec![PngTask {
        bx,
        origin_x: origin_x.clone(),
        parent_baseline: parent_baseline.clone(),
        fill,
    }];
    while let Some(PngTask {
        bx,
        origin_x,
        parent_baseline,
        fill,
    }) = stack.pop()
    {
        emit_one(
            bx,
            font,
            em_px,
            fu_px,
            &origin_x,
            &parent_baseline,
            fill,
            cache,
            pixmap,
            &mut stack,
        )?;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn emit_one<'a>(
    bx: &'a MathBox,
    font: &MathFont,
    em_px: &Dim,
    fu_px: &Dim,
    origin_x: &Dim,
    parent_baseline: &Dim,
    fill: Color,
    cache: &mut HashMap<u16, Path>,
    pixmap: &mut Pixmap,
    stack: &mut Vec<PngTask<'a>>,
) -> Result<(), Error> {
    let baseline = parent_baseline - &(bx.shift.clone() * em_px);
    match &bx.content {
        BoxContent::Empty | BoxContent::Kern(_) => Ok(()),
        BoxContent::Glyph {
            glyph_id, scale, ..
        } => {
            let path = glyph_path(font, *glyph_id, cache)?;
            let k = fu_px * scale;
            let sx = px(&k);
            let sy = px(&-k);
            let tx = px(origin_x);
            let ty = px(&baseline);
            let t = Transform::from_row(sx, 0.0, 0.0, sy, tx, ty);
            pixmap.fill_path(&path, &paint(fill), FillRule::Winding, t, None);
            Ok(())
        }
        BoxContent::Rule => fill_box_rect(bx, origin_x, &baseline, em_px, fill, pixmap),
        BoxContent::HList(kids) => {
            let mut x = origin_x.clone();
            let mut placed = Vec::with_capacity(kids.len());
            for k in kids {
                placed.push((k, x.clone()));
                x = &x + &(k.width.clone() * em_px);
            }
            for (k, x) in placed.into_iter().rev() {
                stack.push(PngTask {
                    bx: k,
                    origin_x: x,
                    parent_baseline: baseline.clone(),
                    fill,
                });
            }
            Ok(())
        }
        BoxContent::VList(kids) => {
            if kids.is_empty() {
                return Ok(());
            }
            let mut placed = Vec::with_capacity(kids.len());
            placed.push((&kids[0], baseline.clone()));
            let mut y_below = kids[0].depth.clone() * em_px;
            for k in kids.iter().skip(1) {
                let child_base = &baseline + &y_below + &(k.height.clone() * em_px);
                placed.push((k, child_base));
                y_below = &y_below + &((&k.height + &k.depth) * em_px);
            }
            for (k, child_base) in placed.into_iter().rev() {
                stack.push(PngTask {
                    bx: k,
                    origin_x: origin_x.clone(),
                    parent_baseline: child_base,
                    fill,
                });
            }
            Ok(())
        }
        BoxContent::Overlap(kids) => {
            for k in kids.iter().rev() {
                stack.push(PngTask {
                    bx: k,
                    origin_x: origin_x.clone(),
                    parent_baseline: baseline.clone(),
                    fill,
                });
            }
            Ok(())
        }
        BoxContent::Color(c, inner) => {
            stack.push(PngTask {
                bx: inner,
                origin_x: origin_x.clone(),
                parent_baseline: baseline,
                fill: *c,
            });
            Ok(())
        }
        BoxContent::BackColor(c, inner) => {
            fill_box_rect(bx, origin_x, &baseline, em_px, *c, pixmap)?;
            stack.push(PngTask {
                bx: inner,
                origin_x: origin_x.clone(),
                parent_baseline: baseline,
                fill,
            });
            Ok(())
        }
        BoxContent::Line {
            x1,
            y1,
            x2,
            y2,
            thickness,
        } => {
            let x1p = origin_x + &(x1.clone() * em_px);
            let y1p = &baseline - &(y1.clone() * em_px);
            let x2p = origin_x + &(x2.clone() * em_px);
            let y2p = &baseline - &(y2.clone() * em_px);
            let mut pb = PathBuilder::new();
            pb.move_to(px(&x1p), px(&y1p));
            pb.line_to(px(&x2p), px(&y2p));
            let Some(path) = pb.finish() else {
                return Ok(());
            };
            let stroke = Stroke {
                width: px(&(thickness.clone() * em_px)),
                ..Stroke::default()
            };
            pixmap.stroke_path(&path, &paint(fill), &stroke, Transform::identity(), None);
            Ok(())
        }
        BoxContent::Frame {
            thickness,
            stroke,
            inner,
        } => {
            let w = &bx.width * em_px;
            let h_ink = (&bx.height + &bx.depth) * em_px;
            let t = thickness.clone() * em_px;
            let half = &t / &Dim::from_i64(2);
            let x = origin_x + &half;
            let y = &(&baseline - &(bx.height.clone() * em_px)) + &half;
            let rw = (&w - &t).max(&Dim::zero());
            let rh = (&h_ink - &t).max(&Dim::zero());
            if !rw.is_zero() && !rh.is_zero() {
                if let Some(rect) = Rect::from_xywh(px(&x), px(&y), px(&rw), px(&rh)) {
                    let st = Stroke {
                        width: px(&t),
                        ..Stroke::default()
                    };
                    let border = stroke.unwrap_or(fill);
                    pixmap.stroke_path(
                        &PathBuilder::from_rect(rect),
                        &paint(border),
                        &st,
                        Transform::identity(),
                        None,
                    );
                }
            }
            stack.push(PngTask {
                bx: inner,
                origin_x: origin_x.clone(),
                parent_baseline: baseline,
                fill,
            });
            Ok(())
        }
    }
}

fn fill_box_rect(
    bx: &MathBox,
    origin_x: &Dim,
    baseline: &Dim,
    em_px: &Dim,
    fill: Color,
    pixmap: &mut Pixmap,
) -> Result<(), Error> {
    let w = &bx.width * em_px;
    let h_ink = (&bx.height + &bx.depth) * em_px;
    if w.is_zero() || h_ink.is_zero() {
        return Ok(());
    }
    let y = baseline - &(bx.height.clone() * em_px);
    if let Some(rect) = Rect::from_xywh(px(origin_x), px(&y), px(&w), px(&h_ink)) {
        pixmap.fill_rect(rect, &paint(fill), Transform::identity(), None);
    }
    Ok(())
}

fn glyph_path(
    font: &MathFont,
    glyph_id: u16,
    cache: &mut HashMap<u16, Path>,
) -> Result<Path, Error> {
    if let Some(p) = cache.get(&glyph_id) {
        return Ok(p.clone());
    }
    let path = outline_path(font, glyph_id)?;
    cache.insert(glyph_id, path.clone());
    Ok(path)
}

fn outline_path(font: &MathFont, glyph_id: u16) -> Result<Path, Error> {
    let mut b = DimOutline {
        pb: PathBuilder::new(),
    };
    if !font.draw_glyph(glyph_id, &mut b) {
        return Err(FontError::MissingGlyph { ch: '\u{FFFD}' }.into());
    }
    b.pb.finish()
        .ok_or_else(|| FontError::MissingGlyph { ch: '\u{FFFD}' }.into())
}

/// TrueType `f32` points become [`Dim`] immediately, then IEEE bits for tiny-skia.
struct DimOutline {
    pb: PathBuilder,
}

impl DimOutline {
    fn pt(x: f32) -> f32 {
        f32::from_bits(Dim::from_ieee32_bits(x.to_bits()).to_ieee32_bits())
    }
}

impl OutlinePen for DimOutline {
    fn move_to(&mut self, x: f32, y: f32) {
        self.pb.move_to(Self::pt(x), Self::pt(y));
    }

    fn line_to(&mut self, x: f32, y: f32) {
        self.pb.line_to(Self::pt(x), Self::pt(y));
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        self.pb
            .quad_to(Self::pt(x1), Self::pt(y1), Self::pt(x), Self::pt(y));
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        self.pb.cubic_to(
            Self::pt(x1),
            Self::pt(y1),
            Self::pt(x2),
            Self::pt(y2),
            Self::pt(x),
            Self::pt(y),
        );
    }

    fn close(&mut self) {
        self.pb.close();
    }
}