retex-engine 1.3.0-rc.3

The TeX typesetting engine for retex
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
//! LuaTeX's math parameters (`\Umath…`) and how font assignments set them.
//!
//! LuaTeX takes every font-dependent value of math typesetting from a
//! parameter of the current style. Assigning a TFM font to family 2 or 3
//! (`\textfont2=\tensy`, …) sets the parameters of the styles of that
//! size from the font dimensions (LuaTeX manual, "Font-based math
//! parameters"); the assignments obey grouping. Observed details:
//!
//! * family 2 needs at least 22 font dimensions, family 3 at least 13;
//!   other fonts leave the parameters alone;
//! * `\Umathsubshiftdrop` and `\Umathsupshiftdrop` of a style come from
//!   the font one size smaller (TeX's `sub_drop(t)`), so `\scriptfont2`
//!   sets them for the display and text styles and `\scriptscriptfont2`
//!   for the script and scriptscript styles;
//! * the skewed fraction gaps are set to zero, the radical degree
//!   placement to 5/18 and −10/18 quad and 60%;
//! * `\Umathradicalvgap` is set when family 3 is assigned (with the
//!   x-height of the family-2 font of the same size in display styles);
//! * `\Umathspaceafterscript` is set from `\scriptspace` (globally, for all
//!   styles still undefined) when the first formula is typeset, and never
//!   again.
//!
//! A font with OpenType `MathConstants` sets the parameters of its size
//! from them instead, whatever the family (manual table; `\Umathquad` is
//! the font size and the limit kerns are zero).

use crate::Engine;
use crate::codes::UNDEFINED_MATH_PARAM;
use crate::eqtb::{Loc, Val};
use crate::primitives_gen::assign_int as ip;

/// Math parameter numbers (the `chr` codes of the `\Umath…` primitives).
#[allow(missing_docs)]
pub mod mp {
    pub const QUAD: u32 = 0;
    pub const AXIS: u32 = 1;
    pub const OPERATOR_SIZE: u32 = 2;
    pub const OVERBAR_KERN: u32 = 3;
    pub const OVERBAR_RULE: u32 = 4;
    pub const OVERBAR_VGAP: u32 = 5;
    pub const UNDERBAR_KERN: u32 = 6;
    pub const UNDERBAR_RULE: u32 = 7;
    pub const UNDERBAR_VGAP: u32 = 8;
    pub const RADICAL_KERN: u32 = 9;
    pub const RADICAL_RULE: u32 = 10;
    pub const RADICAL_VGAP: u32 = 11;
    pub const RADICAL_DEGREE_BEFORE: u32 = 12;
    pub const RADICAL_DEGREE_AFTER: u32 = 13;
    pub const RADICAL_DEGREE_RAISE: u32 = 14;
    pub const STACK_VGAP: u32 = 15;
    pub const STACK_NUM_UP: u32 = 16;
    pub const STACK_DENOM_DOWN: u32 = 17;
    pub const FRACTION_RULE: u32 = 18;
    pub const FRACTION_NUM_VGAP: u32 = 19;
    pub const FRACTION_NUM_UP: u32 = 20;
    pub const FRACTION_DENOM_VGAP: u32 = 21;
    pub const FRACTION_DENOM_DOWN: u32 = 22;
    pub const FRACTION_DEL_SIZE: u32 = 23;
    pub const SKEWED_FRACTION_HGAP: u32 = 24;
    pub const SKEWED_FRACTION_VGAP: u32 = 25;
    pub const LIMIT_ABOVE_VGAP: u32 = 26;
    pub const LIMIT_ABOVE_BGAP: u32 = 27;
    pub const LIMIT_ABOVE_KERN: u32 = 28;
    pub const LIMIT_BELOW_VGAP: u32 = 29;
    pub const LIMIT_BELOW_BGAP: u32 = 30;
    pub const LIMIT_BELOW_KERN: u32 = 31;
    pub const NOLIMIT_SUB_FACTOR: u32 = 32;
    pub const NOLIMIT_SUP_FACTOR: u32 = 33;
    pub const UNDER_DELIMITER_VGAP: u32 = 34;
    pub const UNDER_DELIMITER_BGAP: u32 = 35;
    pub const OVER_DELIMITER_VGAP: u32 = 36;
    pub const OVER_DELIMITER_BGAP: u32 = 37;
    pub const SUB_SHIFT_DROP: u32 = 38;
    pub const SUP_SHIFT_DROP: u32 = 39;
    pub const SUB_SHIFT_DOWN: u32 = 40;
    pub const SUB_SUP_SHIFT_DOWN: u32 = 41;
    pub const SUB_TOP_MAX: u32 = 42;
    pub const SUP_SHIFT_UP: u32 = 43;
    pub const SUP_BOTTOM_MIN: u32 = 44;
    pub const SUP_SUB_BOTTOM_MAX: u32 = 45;
    pub const SUB_SUP_VGAP: u32 = 46;
    pub const SPACE_AFTER_SCRIPT: u32 = 47;
    pub const CONNECTOR_OVERLAP_MIN: u32 = 48;
}

/// The styles of a size: text size covers display and text styles.
fn size_styles(size: u32) -> &'static [u32] {
    match size {
        0 => &[0, 1, 2, 3],
        1 => &[4, 5],
        _ => &[6, 7],
    }
}

impl Engine {
    /// Defines math parameter `p` for style `s`. Assignments are traced
    /// only when `\tracingassigns` is 2 or more (LuaTeX manual, "Less
    /// tracing").
    pub fn define_math_param(&mut self, global: bool, p: u32, s: u32, v: i32) {
        let ta = self.eqtb.int(ip::TRACINGASSIGNS);
        let quiet = ta > 0 && ta < 2;
        if quiet {
            self.eqtb.int_par[ip::TRACINGASSIGNS as usize].val = 0;
        }
        self.define(global, Loc::MathParam(p * 8 + s), Val::Int(i64::from(v)));
        if quiet {
            self.eqtb.int_par[ip::TRACINGASSIGNS as usize].val = ta;
        }
    }

    /// Sets the math parameters that follow from assigning font `f` to
    /// family `fam` in size `size` (0 text, 1 script, 2 scriptscript).
    pub fn set_font_math_params(&mut self, global: bool, size: u32, fam: u32, f: u32) {
        if self.set_opentype_math_params(global, size, f) {
            return;
        }
        let params = self.font(f).params.clone();
        let par = |n: usize| params.get(n).copied().unwrap_or(0);
        let np = params.len().saturating_sub(1);
        let styles = size_styles(size);
        if fam == 2 && np >= 22 {
            let xh = par(5);
            let quad = par(6);
            let deg = |n: i32| retex_arith::xn_over_d(quad, n, 18).map_or(0, |r| r.quotient);
            for &s in styles {
                let display = s < 2;
                let cramped = s % 2 == 1;
                let set = |e: &mut Engine, p: u32, v: i32| e.define_math_param(global, p, s, v);
                set(self, mp::QUAD, quad);
                set(self, mp::AXIS, par(22));
                set(
                    self,
                    mp::FRACTION_DEL_SIZE,
                    if display { par(20) } else { par(21) },
                );
                set(
                    self,
                    mp::FRACTION_DENOM_DOWN,
                    if display { par(11) } else { par(12) },
                );
                set(
                    self,
                    mp::FRACTION_NUM_UP,
                    if display { par(8) } else { par(9) },
                );
                set(
                    self,
                    mp::STACK_NUM_UP,
                    if display { par(8) } else { par(10) },
                );
                set(
                    self,
                    mp::STACK_DENOM_DOWN,
                    if display { par(11) } else { par(12) },
                );
                set(self, mp::SKEWED_FRACTION_HGAP, 0);
                set(self, mp::SKEWED_FRACTION_VGAP, 0);
                set(self, mp::SUB_SHIFT_DOWN, par(16));
                set(self, mp::SUB_SUP_SHIFT_DOWN, par(17));
                set(self, mp::SUB_TOP_MAX, (xh * 4).abs() / 5);
                set(self, mp::SUP_BOTTOM_MIN, (xh / 4).abs());
                set(self, mp::SUP_SUB_BOTTOM_MAX, (xh * 4).abs() / 5);
                let sup = if cramped {
                    par(15)
                } else if display {
                    par(13)
                } else {
                    par(14)
                };
                set(self, mp::SUP_SHIFT_UP, sup);
                set(self, mp::RADICAL_DEGREE_BEFORE, deg(5));
                set(self, mp::RADICAL_DEGREE_AFTER, -deg(10));
                set(self, mp::RADICAL_DEGREE_RAISE, 60);
            }
            // The drops of the styles one size larger.
            let drop_styles: &[u32] = match size {
                1 => &[0, 1, 2, 3],
                2 => &[4, 5, 6, 7],
                _ => &[],
            };
            for &s in drop_styles {
                self.define_math_param(global, mp::SUB_SHIFT_DROP, s, par(19));
                self.define_math_param(global, mp::SUP_SHIFT_DROP, s, par(18));
            }
        } else if fam == 3 && np >= 13 {
            let drt = par(8);
            let f2 = self.fam_fnt(2, size);
            let xh2 = self.font(f2).params.get(5).copied().unwrap_or(0);
            for &s in styles {
                let display = s < 2;
                let set = |e: &mut Engine, p: u32, v: i32| e.define_math_param(global, p, s, v);
                set(self, mp::OVERBAR_KERN, drt);
                set(self, mp::OVERBAR_RULE, drt);
                set(self, mp::OVERBAR_VGAP, 3 * drt);
                set(self, mp::UNDERBAR_KERN, drt);
                set(self, mp::UNDERBAR_RULE, drt);
                set(self, mp::UNDERBAR_VGAP, 3 * drt);
                set(self, mp::RADICAL_KERN, drt);
                set(
                    self,
                    mp::RADICAL_VGAP,
                    if display {
                        drt + xh2.abs() / 4
                    } else {
                        drt + drt.abs() / 4
                    },
                );
                set(
                    self,
                    mp::STACK_VGAP,
                    if display { 7 * drt } else { 3 * drt },
                );
                set(self, mp::FRACTION_RULE, drt);
                set(
                    self,
                    mp::FRACTION_NUM_VGAP,
                    if display { 3 * drt } else { drt },
                );
                set(
                    self,
                    mp::FRACTION_DENOM_VGAP,
                    if display { 3 * drt } else { drt },
                );
                set(self, mp::LIMIT_ABOVE_VGAP, par(9));
                set(self, mp::LIMIT_ABOVE_BGAP, par(11));
                set(self, mp::LIMIT_ABOVE_KERN, par(13));
                set(self, mp::LIMIT_BELOW_VGAP, par(10));
                set(self, mp::LIMIT_BELOW_BGAP, par(12));
                set(self, mp::LIMIT_BELOW_KERN, par(13));
                set(self, mp::UNDER_DELIMITER_VGAP, par(10));
                set(self, mp::UNDER_DELIMITER_BGAP, par(12));
                set(self, mp::OVER_DELIMITER_VGAP, par(9));
                set(self, mp::OVER_DELIMITER_BGAP, par(11));
                set(self, mp::SUB_SUP_VGAP, 4 * drt);
                set(self, mp::CONNECTOR_OVERLAP_MIN, 0);
            }
        }
    }

    /// Sets the math parameters from an OpenType MATH table; returns false
    /// if font `f` has none.
    pub fn set_opentype_math_params(&mut self, global: bool, size: u32, f: u32) -> bool {
        let Some(mc) = self.font(f).math_constants.clone() else {
            return false;
        };
        let font_size = self.font(f).size;
        // (parameter, constant for D and D', constant for the other styles)
        // from the LuaTeX manual's "Font-based math parameters"; a
        // constant the font lacks leaves the parameter alone.
        const MAP: &[(u32, &str, &str)] = &[
            (mp::AXIS, "AxisHeight", "AxisHeight"),
            (mp::OPERATOR_SIZE, "DisplayOperatorMinHeight", ""),
            (
                mp::FRACTION_DEL_SIZE,
                "FractionDelimiterDisplayStyleSize",
                "FractionDelimiterSize",
            ),
            (
                mp::FRACTION_DENOM_DOWN,
                "FractionDenominatorDisplayStyleShiftDown",
                "FractionDenominatorShiftDown",
            ),
            (
                mp::FRACTION_DENOM_VGAP,
                "FractionDenominatorDisplayStyleGapMin",
                "FractionDenominatorGapMin",
            ),
            (
                mp::FRACTION_NUM_UP,
                "FractionNumeratorDisplayStyleShiftUp",
                "FractionNumeratorShiftUp",
            ),
            (
                mp::FRACTION_NUM_VGAP,
                "FractionNumeratorDisplayStyleGapMin",
                "FractionNumeratorGapMin",
            ),
            (
                mp::FRACTION_RULE,
                "FractionRuleThickness",
                "FractionRuleThickness",
            ),
            (
                mp::SKEWED_FRACTION_HGAP,
                "SkewedFractionHorizontalGap",
                "SkewedFractionHorizontalGap",
            ),
            (
                mp::SKEWED_FRACTION_VGAP,
                "SkewedFractionVerticalGap",
                "SkewedFractionVerticalGap",
            ),
            (
                mp::LIMIT_ABOVE_BGAP,
                "UpperLimitBaselineRiseMin",
                "UpperLimitBaselineRiseMin",
            ),
            (mp::LIMIT_ABOVE_VGAP, "UpperLimitGapMin", "UpperLimitGapMin"),
            (
                mp::LIMIT_BELOW_BGAP,
                "LowerLimitBaselineDropMin",
                "LowerLimitBaselineDropMin",
            ),
            (mp::LIMIT_BELOW_VGAP, "LowerLimitGapMin", "LowerLimitGapMin"),
            (
                mp::OVER_DELIMITER_VGAP,
                "StretchStackGapBelowMin",
                "StretchStackGapBelowMin",
            ),
            (
                mp::OVER_DELIMITER_BGAP,
                "StretchStackTopShiftUp",
                "StretchStackTopShiftUp",
            ),
            (
                mp::UNDER_DELIMITER_VGAP,
                "StretchStackGapAboveMin",
                "StretchStackGapAboveMin",
            ),
            (
                mp::UNDER_DELIMITER_BGAP,
                "StretchStackBottomShiftDown",
                "StretchStackBottomShiftDown",
            ),
            (
                mp::OVERBAR_KERN,
                "OverbarExtraAscender",
                "OverbarExtraAscender",
            ),
            (
                mp::OVERBAR_RULE,
                "OverbarRuleThickness",
                "OverbarRuleThickness",
            ),
            (mp::OVERBAR_VGAP, "OverbarVerticalGap", "OverbarVerticalGap"),
            (
                mp::RADICAL_KERN,
                "RadicalExtraAscender",
                "RadicalExtraAscender",
            ),
            (
                mp::RADICAL_RULE,
                "RadicalRuleThickness",
                "RadicalRuleThickness",
            ),
            (
                mp::RADICAL_VGAP,
                "RadicalDisplayStyleVerticalGap",
                "RadicalVerticalGap",
            ),
            (
                mp::RADICAL_DEGREE_BEFORE,
                "RadicalKernBeforeDegree",
                "RadicalKernBeforeDegree",
            ),
            (
                mp::RADICAL_DEGREE_AFTER,
                "RadicalKernAfterDegree",
                "RadicalKernAfterDegree",
            ),
            (
                mp::RADICAL_DEGREE_RAISE,
                "RadicalDegreeBottomRaisePercent",
                "RadicalDegreeBottomRaisePercent",
            ),
            (
                mp::SPACE_AFTER_SCRIPT,
                "SpaceAfterScript",
                "SpaceAfterScript",
            ),
            (
                mp::STACK_DENOM_DOWN,
                "StackBottomDisplayStyleShiftDown",
                "StackBottomShiftDown",
            ),
            (
                mp::STACK_NUM_UP,
                "StackTopDisplayStyleShiftUp",
                "StackTopShiftUp",
            ),
            (mp::STACK_VGAP, "StackDisplayStyleGapMin", "StackGapMin"),
            (
                mp::SUB_SHIFT_DOWN,
                "SubscriptShiftDown",
                "SubscriptShiftDown",
            ),
            (
                mp::SUB_SHIFT_DROP,
                "SubscriptBaselineDropMin",
                "SubscriptBaselineDropMin",
            ),
            (
                mp::SUB_SUP_SHIFT_DOWN,
                "SubscriptShiftDownWithSuperscript",
                "SubscriptShiftDownWithSuperscript",
            ),
            (mp::SUB_TOP_MAX, "SubscriptTopMax", "SubscriptTopMax"),
            (
                mp::SUB_SUP_VGAP,
                "SubSuperscriptGapMin",
                "SubSuperscriptGapMin",
            ),
            (
                mp::SUP_BOTTOM_MIN,
                "SuperscriptBottomMin",
                "SuperscriptBottomMin",
            ),
            (
                mp::SUP_SHIFT_DROP,
                "SuperscriptBaselineDropMax",
                "SuperscriptBaselineDropMax",
            ),
            (
                mp::SUP_SUB_BOTTOM_MAX,
                "SuperscriptBottomMaxWithSubscript",
                "SuperscriptBottomMaxWithSubscript",
            ),
            (
                mp::UNDERBAR_KERN,
                "UnderbarExtraDescender",
                "UnderbarExtraDescender",
            ),
            (
                mp::UNDERBAR_RULE,
                "UnderbarRuleThickness",
                "UnderbarRuleThickness",
            ),
            (
                mp::UNDERBAR_VGAP,
                "UnderbarVerticalGap",
                "UnderbarVerticalGap",
            ),
            (
                mp::CONNECTOR_OVERLAP_MIN,
                "MinConnectorOverlap",
                "MinConnectorOverlap",
            ),
        ];
        for &s in size_styles(size) {
            let display = s < 2;
            let cramped = s % 2 == 1;
            for &(p, d, o) in MAP {
                let key = if display { d } else { o };
                if let Some(&v) = mc.get(key) {
                    self.define_math_param(global, p, s, v);
                }
            }
            let sup = if cramped {
                "SuperscriptShiftUpCramped"
            } else {
                "SuperscriptShiftUp"
            };
            if let Some(&v) = mc.get(sup) {
                self.define_math_param(global, mp::SUP_SHIFT_UP, s, v);
            }
            // Observed: without SubscriptShiftDownWithSuperscript (as in
            // Latin Modern Math) SubscriptShiftDown is used.
            if !mc.contains_key("SubscriptShiftDownWithSuperscript")
                && let Some(&v) = mc.get("SubscriptShiftDown")
            {
                self.define_math_param(global, mp::SUB_SUP_SHIFT_DOWN, s, v);
            }
            // Not in the MATH table (manual, note 1).
            self.define_math_param(global, mp::QUAD, s, font_size);
            self.define_math_param(global, mp::LIMIT_ABOVE_KERN, s, 0);
            self.define_math_param(global, mp::LIMIT_BELOW_KERN, s, 0);
        }
        true
    }

    /// Initializes the parameters LuaTeX sets when the first formula is
    /// typeset (`\Umathspaceafterscript` from `\scriptspace`).
    pub fn init_math_params_once(&mut self) {
        if self.math_params_initialized {
            return;
        }
        self.math_params_initialized = true;
        let ss = self
            .eqtb
            .dimen(crate::primitives_gen::assign_dimen::SCRIPTSPACE);
        for s in 0..8 {
            if self.math_param_value(mp::SPACE_AFTER_SCRIPT, s) == UNDEFINED_MATH_PARAM {
                let loc = Loc::MathParam(mp::SPACE_AFTER_SCRIPT * 8 + s);
                self.eqtb
                    .set(loc, Val::Int(i64::from(ss)), crate::eqtb::LEVEL_ONE);
            }
        }
    }
}