deduxit 0.4.0

A collection of logic suites: decides, checks, proves step by step and exports sequents of linear logic and of classical, intuitionistic and minimal propositional logic, with checked proofs, Kripke countermodels and Rocq certificates.
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
// deduxit © Fabian Lukas Grubmüller 2026
// Licensed under the EUPL

//! What the layout knows of the font: the advance (the width a character
//! takes on its line) of every character the drawings set, and the
//! vertical metrics of a line.
//!
//! The advances come from tables of five math fonts, each 1000 units per
//! em:
//! - Euler Math 0.75, the default;
//! - STIX Two Math 2.12;
//! - Garamond-Math 2022-01-03;
//! - Asana Math 0.962;
//! - New Computer Modern Math 7.1.1, in its Book weight.
//!
//! Each table is the font file's `hmtx` advances, committed with the
//! code, so nothing is measured at run time. A test reads each file and
//! checks its table. The vertical metrics are Euler Math's for every
//! font: measured against each file, every font's raised `⊥` and comma
//! fit within them.

use std::collections::BTreeMap;

/// The font a drawing asks for, and the advances its layout gives the
/// characters. [`Font::euler`] is the default, [`MathFont::font`] gives
/// each math font, and [`Font::monospace`] suits a viewer without a math
/// font.
///
/// In JSON (feature `serialize`) the family and the advances, or the name
/// of a [`MathFont`] alone, such as `"stix-two"`, which reads as that
/// font's [`MathFont::font`].
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub struct Font {
    /// The font families the document asks for, as a CSS `font-family`
    /// lists them, such as `'Euler Math', 'Neo Euler', serif`.
    pub family: String,
    /// The advance of every character.
    pub advances: Advances,
}

impl Font {
    /// Returns Euler Math, with Neo Euler and any serif font after it, and
    /// Euler Math's advances.
    pub fn euler() -> Self {
        Self {
            family: "'Euler Math', 'Neo Euler', serif".to_owned(),
            advances: Advances::Euler,
        }
    }

    /// Returns any monospace font, every character [`MONOSPACE`]
    /// thousandths of an em wide: for a viewer without a math font.
    pub fn monospace() -> Self {
        Self {
            family: "monospace".to_owned(),
            advances: Advances::Fixed(MONOSPACE),
        }
    }

    /// Returns the font with another [`family`](Self::family).
    #[must_use]
    pub fn with_family(self, family: String) -> Self {
        Self { family, ..self }
    }

    /// Returns the font with another [`advances`](Self::advances).
    #[must_use]
    pub fn with_advances(self, advances: Advances) -> Self {
        Self { advances, ..self }
    }
}

impl Default for Font {
    /// Returns [`Font::euler`].
    fn default() -> Self {
        Self::euler()
    }
}

#[cfg(feature = "serialize")]
impl<'de> serde::Deserialize<'de> for Font {
    /// Reads a font's family and advances, or a [`MathFont`]'s name.
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        /// A font's fields as JSON writes them, each left out the default
        /// font's.
        #[derive(serde::Deserialize)]
        #[serde(default, deny_unknown_fields)]
        struct Fields {
            /// [`Font::family`].
            family: String,
            /// [`Font::advances`].
            advances: Advances,
        }

        impl Default for Fields {
            /// Returns the default font's fields.
            fn default() -> Self {
                let Font { family, advances } = Font::default();
                Self { family, advances }
            }
        }

        /// Reads either form.
        struct Visitor;

        impl<'de> serde::de::Visitor<'de> for Visitor {
            type Value = Font;

            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                f.write_str("a font's family and advances, or the name of a math font")
            }

            fn visit_str<E: serde::de::Error>(self, name: &str) -> Result<Font, E> {
                use serde::Deserialize;
                use serde::de::IntoDeserializer;
                MathFont::deserialize(name.into_deserializer()).map(MathFont::font)
            }

            fn visit_map<A: serde::de::MapAccess<'de>>(self, map: A) -> Result<Font, A::Error> {
                use serde::Deserialize;
                let Fields { family, advances } =
                    Fields::deserialize(serde::de::value::MapAccessDeserializer::new(map))?;
                Ok(Font { family, advances })
            }
        }

        deserializer.deserialize_any(Visitor)
    }
}

/// A math font whose advances the layout knows, read from its file. Each
/// has a glyph for every character the drawings set. A renderer needs the
/// font's file, which it finds by the [`family`](Self::family) name.
///
/// In JSON (feature `serialize`) `"euler"`, `"stix-two"`, `"garamond"`,
/// `"asana"` or `"new-computer-modern"`.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(rename_all = "kebab-case"))]
pub enum MathFont {
    /// Euler Math 0.75, Hermann Zapf's upright Euler for the AMS as an
    /// OpenType math font, under the SIL Open Font License.
    #[default]
    Euler,
    /// STIX Two Math 2.12, a Times-like font of the STIX project, under
    /// the SIL Open Font License.
    StixTwo,
    /// Garamond-Math 2022-01-03, matching EB Garamond, under the SIL Open
    /// Font License.
    Garamond,
    /// Asana Math 0.962, Palatino-like, under the SIL Open Font License.
    Asana,
    /// New Computer Modern Math 7.1.1 in its Book weight, the Computer
    /// Modern of TeX, under the GUST Font License (the LaTeX Project
    /// Public License 1.3c).
    NewComputerModern,
}

impl MathFont {
    /// Every math font, the default first: a slice, since its length
    /// grows with the next font.
    pub const ALL: &'static [Self] = &[
        Self::Euler,
        Self::StixTwo,
        Self::Garamond,
        Self::Asana,
        Self::NewComputerModern,
    ];

    /// Returns the family name in the font's file, which a renderer
    /// looks the font up by.
    pub const fn family(self) -> &'static str {
        match self {
            Self::Euler => "Euler Math",
            Self::StixTwo => "STIX Two Math",
            Self::Garamond => "Garamond-Math",
            Self::Asana => "Asana Math",
            Self::NewComputerModern => "NewComputerModernMath",
        }
    }

    /// Returns the column of the font's advances in the table.
    const fn column(self) -> usize {
        match self {
            Self::Euler => 0,
            Self::StixTwo => 1,
            Self::Garamond => 2,
            Self::Asana => 3,
            Self::NewComputerModern => 4,
        }
    }

    /// Returns the font a drawing is set in: this family, then any serif
    /// font (Euler Math with Neo Euler between, [`Font::euler`]), and its
    /// advances.
    pub fn font(self) -> Font {
        match self {
            Self::Euler => Font::euler(),
            Self::StixTwo => Font {
                family: "'STIX Two Math', serif".to_owned(),
                advances: Advances::StixTwo,
            },
            Self::Garamond => Font {
                family: "'Garamond-Math', serif".to_owned(),
                advances: Advances::Garamond,
            },
            Self::Asana => Font {
                family: "'Asana Math', serif".to_owned(),
                advances: Advances::Asana,
            },
            Self::NewComputerModern => Font {
                family: "'NewComputerModernMath', serif".to_owned(),
                advances: Advances::NewComputerModern,
            },
        }
    }
}

/// The advance of a character in [`Font::monospace`], in thousandths of
/// an em: what most monospace fonts have.
pub const MONOSPACE: u32 = 600;

/// The advance widths a layout gives characters, in thousandths of an em:
/// a math font's committed table, one width for every character, or a
/// table of your own.
///
/// In JSON (feature `serialize`) a committed table by its name
/// (`"euler"`, `"stix-two"`, `"garamond"`, `"asana"`,
/// `"new-computer-modern"`), `{"fixed": 600}`, or
/// `{"table": {"table": {"a": 500}, "fallback": 650}}`.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(rename_all = "kebab-case"))]
pub enum Advances {
    /// The committed table of Euler Math, with a fallback of 650 outside
    /// it.
    Euler,
    /// The committed table of STIX Two Math, with the same fallback.
    StixTwo,
    /// The committed table of Garamond-Math, with the same fallback.
    Garamond,
    /// The committed table of Asana Math, with the same fallback.
    Asana,
    /// The committed table of New Computer Modern Math (Book), with the
    /// same fallback.
    NewComputerModern,
    /// One advance for every character, as [`Font::monospace`] has.
    Fixed(u32),
    /// A table of the user's, and the advance of a character outside it.
    #[non_exhaustive]
    Table {
        /// The advance of each character.
        table: BTreeMap<char, u32>,
        /// The advance of a character outside the table.
        fallback: u32,
    },
}

impl Advances {
    /// Returns the advances of a table of the caller's: each character's
    /// in `table`, and `fallback` for a character outside it.
    pub fn table(table: BTreeMap<char, u32>, fallback: u32) -> Self {
        Self::Table { table, fallback }
    }

    /// Returns how far below the baseline an edge under a written formula
    /// of a net begins, in thousandths of an em, so that no edge runs into
    /// a descender such as the tail of a `g`.
    ///
    /// For four of the math fonts, this is the deepest descender of the
    /// table, measured in each file (fontTools' bounds of every glyph of
    /// the table): STIX Two Math's `g` 235, Garamond-Math's `g` 290, Asana
    /// Math's `μ` 286, New Computer Modern Math's `/` 250. Euler Math and a
    /// font of the caller's take [`DEPTH`], the depth every line is laid
    /// out with, which keeps their drawings as they are; Euler Math's `q`
    /// reaches 243, a little below it.
    pub(super) const fn depth(&self) -> i64 {
        match self {
            Self::StixTwo => 235,
            Self::Garamond => 290,
            Self::Asana => 286,
            Self::NewComputerModern => 250,
            Self::Euler | Self::Fixed(_) | Self::Table { .. } => DEPTH,
        }
    }

    /// Returns the advance of a character, in thousandths of an em.
    pub fn advance(&self, c: char) -> u32 {
        match self {
            Self::Euler => advance(c, MathFont::Euler),
            Self::StixTwo => advance(c, MathFont::StixTwo),
            Self::Garamond => advance(c, MathFont::Garamond),
            Self::Asana => advance(c, MathFont::Asana),
            Self::NewComputerModern => advance(c, MathFont::NewComputerModern),
            Self::Fixed(advance) => *advance,
            Self::Table { table, fallback } => table.get(&c).copied().unwrap_or(*fallback),
        }
    }
}

/// Every character the drawings set, sorted: printable ASCII, the
/// mathematical italic Latin letters, Greek, and the connectives, units,
/// turnstile and vertical dots. They are kept apart from the advances, so
/// that the binary search walks the characters alone, not rows that also
/// carry five advances each.
const CHARACTERS: [char; 208] = [
    ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2',
    '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E',
    'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
    'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
    '¬', 'Α', 'Β', 'Γ', 'Δ', 'Ε', 'Ζ', 'Η', 'Θ', 'Ι', 'Κ', 'Λ', 'Μ', 'Ν', 'Ξ', 'Ο', 'Π', 'Ρ', 'Σ',
    'Τ', 'Υ', 'Φ', 'Χ', 'Ψ', 'Ω', 'α', 'β', 'γ', 'δ', 'ε', 'ζ', 'η', 'θ', 'ι', 'κ', 'λ', 'μ', 'ν',
    'ξ', 'ο', 'π', 'ρ', 'σ', 'τ', 'υ', 'φ', 'χ', 'ψ', 'ω', 'ℎ', '⅋', '→', '↔', '∧', '∨', '⊕', '⊗',
    '⊢', '⊤', '⊥', '⊸', '⋮', '𝐴', '𝐵', '𝐶', '𝐷', '𝐸', '𝐹', '𝐺', '𝐻', '𝐼', '𝐽', '𝐾', '𝐿', '𝑀', '𝑁',
    '𝑂', '𝑃', '𝑄', '𝑅', '𝑆', '𝑇', '𝑈', '𝑉', '𝑊', '𝑋', '𝑌', '𝑍', '𝑎', '𝑏', '𝑐', '𝑑', '𝑒', '𝑓', '𝑔',
    '𝑖', '𝑗', '𝑘', '𝑙', '𝑚', '𝑛', '𝑜', '𝑝', '𝑞', '𝑟', '𝑠', '𝑡', '𝑢', '𝑣', '𝑤', '𝑥', '𝑦', '𝑧',
];

/// The advance width of each of the [`CHARACTERS`], in thousandths of an
/// em, in the same order: one table per [`MathFont`], in the order of
/// [`MathFont::ALL`]. Each entry is the font file's `hmtx` advance, which
/// `every_table_is_its_font_s` checks against the file.
#[rustfmt::skip]
const ADVANCES: [[u16; 208]; 5] = [
    [
        333, 295, 214, 500, 557, 840, 737, 212, 328, 328, 277, 755,
        277, 544, 277, 501, 499, 499, 499, 499, 499, 499, 499, 499,
        499, 499, 216, 216, 800, 756, 800, 362, 744, 770, 655, 714,
        828, 604, 499, 765, 783, 394, 402, 668, 559, 1044, 829, 803,
        576, 828, 609, 557, 514, 774, 646, 986, 666, 555, 666, 290,
        501, 290, 499, 450, 201, 609, 588, 486, 603, 499, 419, 568,
        621, 360, 331, 555, 365, 915, 664, 563, 589, 605, 432, 455,
        416, 642, 495, 812, 526, 593, 470, 345, 213, 345, 551, 773,
        770, 655, 428, 713, 604, 666, 783, 757, 394, 668, 770, 1044,
        829, 596, 803, 722, 576, 646, 492, 716, 833, 666, 703, 875,
        658, 662, 608, 501, 486, 512, 560, 554, 334, 555, 541, 617,
        599, 553, 563, 609, 548, 605, 513, 587, 763, 576, 754, 851,
        621, 737, 1000, 1000, 775, 775, 668, 668, 698, 874, 874, 1010,
        768, 770, 655, 714, 828, 604, 499, 765, 783, 394, 402, 668,
        559, 1044, 829, 803, 576, 828, 609, 557, 492, 774, 646, 986,
        666, 555, 666, 609, 588, 486, 603, 499, 419, 568, 360, 331,
        555, 365, 915, 664, 563, 589, 605, 432, 455, 416, 642, 495,
        812, 526, 593, 470,
    ],
    [
        235, 283, 334, 702, 495, 692, 736, 187, 357, 357, 410, 720,
        245, 315, 245, 386, 495, 495, 495, 495, 495, 495, 495, 495,
        495, 495, 245, 245, 720, 720, 720, 411, 905, 718, 618, 670,
        730, 626, 588, 720, 777, 357, 396, 702, 585, 895, 742, 738,
        569, 737, 659, 504, 603, 752, 680, 944, 664, 647, 594, 349,
        385, 349, 480, 512, 222, 480, 522, 446, 539, 464, 307, 484,
        577, 285, 263, 542, 283, 852, 576, 514, 537, 521, 380, 395,
        315, 570, 481, 749, 479, 483, 458, 327, 267, 327, 557, 720,
        718, 618, 569, 667, 626, 594, 777, 738, 357, 702, 710, 895,
        742, 648, 738, 759, 569, 604, 603, 647, 718, 664, 720, 777,
        561, 530, 466, 479, 381, 386, 510, 514, 259, 495, 506, 552,
        453, 390, 475, 591, 515, 545, 392, 491, 603, 462, 620, 660,
        569, 736, 948, 948, 636, 636, 862, 862, 720, 720, 720, 869,
        519, 681, 593, 656, 702, 614, 583, 697, 757, 343, 398, 655,
        569, 869, 714, 707, 569, 707, 620, 534, 610, 731, 640, 913,
        637, 607, 606, 555, 518, 414, 556, 424, 577, 503, 274, 431,
        533, 278, 830, 565, 486, 598, 517, 412, 386, 338, 565, 504,
        727, 559, 510, 493,
    ],
    [
        200, 250, 330, 468, 441, 650, 756, 200, 375, 375, 340, 600,
        230, 274, 230, 489, 489, 489, 489, 489, 489, 489, 489, 489,
        489, 489, 248, 228, 554, 566, 554, 381, 749, 692, 587, 710,
        754, 564, 512, 729, 810, 340, 340, 681, 584, 901, 792, 763,
        552, 763, 713, 465, 670, 738, 672, 916, 707, 578, 603, 341,
        489, 341, 500, 500, 200, 399, 515, 407, 506, 390, 318, 435,
        515, 245, 226, 469, 240, 778, 528, 495, 519, 522, 334, 323,
        314, 527, 438, 685, 430, 438, 377, 416, 352, 416, 500, 600,
        692, 587, 517, 676, 564, 603, 810, 773, 340, 681, 692, 901,
        792, 640, 763, 828, 552, 603, 670, 739, 745, 707, 745, 830,
        452, 510, 430, 418, 334, 329, 460, 430, 238, 468, 469, 537,
        405, 374, 400, 521, 426, 478, 410, 434, 537, 438, 560, 562,
        500, 756, 1000, 1000, 572, 572, 706, 706, 553, 554, 554, 685,
        210, 707, 623, 671, 780, 626, 603, 699, 821, 424, 483, 767,
        552, 1062, 858, 727, 627, 751, 647, 557, 635, 774, 693, 1065,
        752, 614, 661, 490, 462, 353, 570, 379, 577, 485, 340, 420,
        473, 339, 758, 509, 416, 522, 515, 422, 327, 347, 494, 431,
        642, 498, 451, 431,
    ],
    [
        249, 277, 370, 499, 499, 839, 777, 277, 332, 332, 388, 668,
        249, 332, 249, 286, 499, 443, 499, 499, 499, 499, 499, 499,
        499, 499, 249, 249, 668, 668, 668, 443, 746, 777, 610, 708,
        793, 610, 555, 762, 831, 336, 332, 725, 610, 945, 830, 785,
        603, 785, 667, 524, 612, 777, 721, 1000, 666, 666, 666, 332,
        286, 332, 605, 499, 277, 499, 552, 443, 610, 478, 332, 555,
        581, 290, 233, 555, 290, 882, 581, 545, 600, 600, 394, 423,
        325, 602, 564, 833, 515, 555, 499, 332, 210, 332, 605, 673,
        777, 610, 555, 688, 610, 666, 831, 785, 336, 725, 753, 945,
        830, 692, 785, 812, 603, 684, 612, 733, 755, 666, 870, 824,
        594, 512, 581, 497, 480, 504, 531, 613, 310, 571, 618, 610,
        497, 547, 545, 653, 566, 552, 519, 547, 681, 608, 695, 715,
        499, 777, 1013, 1013, 687, 687, 668, 668, 748, 748, 748, 1016,
        249, 751, 610, 687, 777, 610, 555, 721, 777, 332, 332, 666,
        555, 953, 777, 777, 610, 777, 666, 555, 610, 777, 721, 943,
        721, 666, 666, 477, 462, 406, 499, 388, 550, 499, 277, 277,
        468, 277, 777, 555, 443, 499, 462, 388, 388, 332, 555, 499,
        721, 499, 499, 443,
    ],
    [
        332, 278, 374, 833, 500, 833, 778, 278, 389, 389, 500, 778,
        278, 333, 278, 500, 500, 500, 500, 500, 500, 500, 500, 500,
        500, 500, 278, 278, 778, 778, 778, 472, 778, 750, 708, 722,
        764, 681, 653, 785, 750, 361, 514, 778, 625, 917, 750, 778,
        681, 778, 736, 556, 722, 750, 750, 1028, 750, 750, 611, 278,
        500, 278, 556, 333, 500, 500, 556, 444, 556, 444, 306, 500,
        556, 278, 306, 528, 278, 833, 556, 500, 556, 528, 392, 394,
        389, 556, 528, 722, 528, 528, 444, 500, 278, 500, 556, 667,
        750, 708, 625, 833, 681, 611, 750, 778, 361, 778, 694, 917,
        750, 667, 778, 750, 681, 722, 722, 778, 722, 750, 778, 722,
        641, 558, 586, 474, 535, 530, 530, 488, 308, 530, 537, 530,
        502, 530, 500, 586, 488, 599, 530, 530, 641, 641, 641, 641,
        576, 778, 1000, 1000, 667, 667, 782, 782, 611, 778, 778, 948,
        218, 750, 759, 715, 828, 738, 643, 786, 831, 440, 555, 849,
        681, 970, 803, 763, 642, 791, 759, 613, 584, 683, 583, 944,
        828, 581, 683, 529, 429, 433, 520, 466, 490, 477, 345, 412,
        521, 298, 878, 600, 485, 503, 446, 451, 469, 361, 572, 485,
        716, 572, 490, 465,
    ],
];

/// The advance of a character outside the table, in every font, in
/// thousandths of an em. A viewer sets such a character in some other
/// font, and `textLength` makes the line fit whatever that font's width
/// is.
const FALLBACK: u32 = 650;

/// How far the highest character of a line reaches above the baseline, a
/// raised `⊥` of a negated atom, in thousandths of an em.
pub(super) const HEIGHT: i64 = 890;

/// How far the lowest character of a line reaches below the baseline, the
/// comma, in thousandths of an em.
pub(super) const DEPTH: i64 = 210;

/// The height of the math axis above the baseline, where `⊗` and `⊸`
/// sit, in thousandths of an em. A rule's name stands with its axis on
/// the inference line.
pub(super) const AXIS: i64 = 250;

/// The size of a superscript or subscript, in thousandths of the size of
/// the text it belongs to.
pub(super) const SCRIPT: i64 = 700;

/// How far a superscript's baseline is raised, in thousandths of an em of
/// the text it belongs to.
pub(super) const RAISE: i64 = 400;

/// How far a subscript's baseline is lowered, in thousandths of an em of
/// the text it belongs to.
pub(super) const LOWER: i64 = 150;

/// Returns the advance of a character in a math font, in thousandths of
/// an em.
fn advance(c: char, font: MathFont) -> u32 {
    match CHARACTERS.binary_search(&c) {
        Ok(i) => u32::from(ADVANCES[font.column()][i]),
        Err(_) => FALLBACK,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use skrifa::prelude::{FontRef, LocationRef, MetadataProvider, Size};

    /// The characters are sorted, so that the binary search finds every
    /// entry, and each font's column is where [`MathFont::ALL`] puts it.
    #[test]
    fn advances_are_sorted() {
        assert!(CHARACTERS.windows(2).all(|w| w[0] < w[1]));
        assert_eq!(advance('⊗', MathFont::Euler), 668);
        assert_eq!(advance('\u{FFFD}', MathFont::StixTwo), FALLBACK);
        assert!(
            MathFont::ALL
                .iter()
                .enumerate()
                .all(|(i, font)| font.column() == i)
        );
    }

    /// Each font's column is the advances of its file (the command's copy
    /// in `cli/fonts`), and the file has a glyph for every character of
    /// the table, so no character a drawing sets falls back to another
    /// font.
    #[test]
    fn every_table_is_its_font_s() {
        let files = [
            "Euler-Math.otf",
            "STIXTwoMath-Regular.otf",
            "Garamond-Math.otf",
            "Asana-Math.otf",
            "NewCMMath-Book.otf",
        ];
        for (&font, file) in MathFont::ALL.iter().zip(files) {
            let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("../cli/fonts")
                .join(file);
            let data = std::fs::read(&path).unwrap();
            let face = FontRef::new(&data).unwrap();
            assert_eq!(
                face.metrics(Size::unscaled(), LocationRef::default())
                    .units_per_em,
                1000
            );
            let (charmap, metrics) = (
                face.charmap(),
                face.glyph_metrics(Size::unscaled(), LocationRef::default()),
            );
            for (&c, &advance) in CHARACTERS.iter().zip(&ADVANCES[font.column()]) {
                let glyph = charmap.map(c).filter(|g| g.to_u32() != 0);
                let glyph = glyph.unwrap_or_else(|| panic!("{file} has no glyph for {c:?}"));
                let width = metrics.advance_width(glyph).unwrap();
                assert_eq!(width, f32::from(advance), "{file}: {c:?}");
            }
        }
    }
}