summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/xetexdir/XeTeXOTMath.cpp
blob: 667f3c62558908b7757593bf26aebf0c4f06e500 (plain)
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
/****************************************************************************\
 Part of the XeTeX typesetting system
 Copyright (c) 1994-2008 by SIL International
 Copyright (c) 2009 by Jonathan Kew
 Copyright (c) 2012-2015 by Khaled Hosny

 SIL Author(s): Jonathan Kew

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the copyright holders
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written
authorization from the copyright holders.
\****************************************************************************/

#include <w2c/config.h>

#include <assert.h>
#include <algorithm>

#include "XeTeXOTMath.h"

#include "XeTeX_web.h"
#include "XeTeXLayoutInterface.h"
#include "XeTeXFontInst.h"

int
get_ot_math_constant(int f, int n)
{
    hb_ot_math_constant_t constant = (hb_ot_math_constant_t) n;
    hb_position_t rval = 0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        hb_font_t* hbFont = font->getHbFont();
        rval = hb_ot_math_get_constant(hbFont, constant);
        /* scale according to font size, except the ones that are percentages */
        switch (constant) {
            case HB_OT_MATH_CONSTANT_SCRIPT_PERCENT_SCALE_DOWN:
            case HB_OT_MATH_CONSTANT_SCRIPT_SCRIPT_PERCENT_SCALE_DOWN:
            case HB_OT_MATH_CONSTANT_RADICAL_DEGREE_BOTTOM_RAISE_PERCENT:
                break;
            default:
                rval = D2Fix(font->unitsToPoints(rval));
                break;
        }
    }
    return rval;
}

#define unknown ((hb_ot_math_constant_t)-1)

/* fontdimen IDs for math symbols font (family 2) */
#define math_x_height   5
#define math_quad       6
#define num1            8   /* numerator shift-up in display styles */
#define num2            9   /* numerator shift-up in non-display, non-\.{\\atop} */
#define num3            10  /* numerator shift-up in non-display \.{\\atop} */
#define denom1          11  /* denominator shift-down in display styles */
#define denom2          12  /* denominator shift-down in non-display styles */
#define sup1            13  /* superscript shift-up in uncramped display style */
#define sup2            14  /* superscript shift-up in uncramped non-display */
#define sup3            15  /* superscript shift-up in cramped styles */
#define sub1            16  /* subscript shift-down if superscript is absent */
#define sub2            17  /* subscript shift-down if superscript is present */
#define sup_drop        18  /* superscript baseline below top of large box */
#define sub_drop        19  /* subscript baseline below bottom of large box */
#define delim1          20  /* size of \.{\\atopwithdelims} delimiters */
#define delim2          21  /* size of \.{\\atopwithdelims} delimiters in non-displays */
#define axis_height     22  /* height of fraction lines above the baseline */

const hb_ot_math_constant_t TeX_sym_to_OT_map[] = {
    unknown,
    unknown,
    unknown,
    unknown,
    unknown,
    HB_OT_MATH_CONSTANT_ACCENT_BASE_HEIGHT, // x-height
    unknown, // quad
    unknown,
    HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_DISPLAY_STYLE_SHIFT_UP,
    HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_SHIFT_UP,
    HB_OT_MATH_CONSTANT_STACK_TOP_SHIFT_UP,
    HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_DISPLAY_STYLE_SHIFT_DOWN,
    HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_SHIFT_DOWN,
    HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP, // ??
    HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP, // ??
    HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP_CRAMPED,
    HB_OT_MATH_CONSTANT_SUBSCRIPT_SHIFT_DOWN, // ??
    HB_OT_MATH_CONSTANT_SUBSCRIPT_SHIFT_DOWN, // ??
    HB_OT_MATH_CONSTANT_SUPERSCRIPT_BASELINE_DROP_MAX, // ??
    HB_OT_MATH_CONSTANT_SUBSCRIPT_BASELINE_DROP_MIN, // ??
    HB_OT_MATH_CONSTANT_DELIMITED_SUB_FORMULA_MIN_HEIGHT,
    unknown, // using quad instead for now
    HB_OT_MATH_CONSTANT_AXIS_HEIGHT
};

int
get_native_mathsy_param(int f, int n)
{
    int rval = 0;

    if (n == math_quad) {
        rval = fontsize[f];
    }
    else if (n == delim2) { // XXX not sure what OT parameter we should use here;
                            // for now we use 1.5em, clamped to delim1 height
        rval = std::min<int>(1.5 * fontsize[f], get_native_mathsy_param(f, delim1));
    }
    else {
        if (n < sizeof(TeX_sym_to_OT_map) / sizeof(hb_ot_math_constant_t)) {
            hb_ot_math_constant_t ot_index = TeX_sym_to_OT_map[n];
            if (ot_index != unknown)
                rval = get_ot_math_constant(f, (int)ot_index);
        }
    }
//  fprintf(stderr, " math_sy(%d, %d) returns %.3f\n", f, n, Fix2D(rval));

    return rval;
}

/* fontdimen IDs for math extension font (family 3) */
#define default_rule_thickness  8   /* thickness of \.{\\over} bars */
#define big_op_spacing1         9   /* minimum clearance above a displayed op */
#define big_op_spacing2         10  /* minimum clearance below a displayed op */
#define big_op_spacing3         11  /* minimum baselineskip above displayed op */
#define big_op_spacing4         12  /* minimum baselineskip below displayed op */
#define big_op_spacing5         13  /* padding above and below displayed limits */

const hb_ot_math_constant_t TeX_ext_to_OT_map[] = {
    unknown,
    unknown,
    unknown,
    unknown,
    unknown,
    HB_OT_MATH_CONSTANT_ACCENT_BASE_HEIGHT, // x-height
    unknown, // quad
    unknown,
    HB_OT_MATH_CONSTANT_FRACTION_RULE_THICKNESS, // default_rule_thickness
    HB_OT_MATH_CONSTANT_UPPER_LIMIT_GAP_MIN, // big_op_spacing1
    HB_OT_MATH_CONSTANT_LOWER_LIMIT_GAP_MIN, // big_op_spacing2
    HB_OT_MATH_CONSTANT_UPPER_LIMIT_BASELINE_RISE_MIN, // big_op_spacing3
    HB_OT_MATH_CONSTANT_LOWER_LIMIT_BASELINE_DROP_MIN, // big_op_spacing4
    HB_OT_MATH_CONSTANT_STACK_GAP_MIN // big_op_spacing5
};

int
get_native_mathex_param(int f, int n)
{
    int rval = 0;

    if (n == math_quad)
        rval = fontsize[f];
    else {
        if (n < sizeof(TeX_ext_to_OT_map) / sizeof(hb_ot_math_constant_t)) {
            hb_ot_math_constant_t ot_index = TeX_ext_to_OT_map[n];
            if (ot_index != unknown)
                rval = get_ot_math_constant(f, (int)ot_index);
        }
    }
//  fprintf(stderr, " math_ex(%d, %d) returns %.3f\n", f, n, Fix2D(rval));

    return rval;
}

int
get_ot_math_variant(int f, int g, int v, integer* adv, int horiz)
{
    hb_codepoint_t rval = g;
    *adv = -1;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        hb_font_t* hbFont = font->getHbFont();
        hb_ot_math_glyph_variant_t variant[1];
        unsigned int count = 1;
        hb_ot_math_get_glyph_variants(hbFont, g, horiz ? HB_DIRECTION_RTL : HB_DIRECTION_TTB, v, &count, variant);
        if (count > 0) {
            rval = variant->glyph;
            *adv = D2Fix(font->unitsToPoints(variant->advance));
        }
    }

    return rval;
}


void*
get_ot_assembly_ptr(int f, int g, int horiz)
{
    void*   rval = NULL;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        hb_font_t* hbFont = font->getHbFont();

        unsigned int count = hb_ot_math_get_glyph_assembly(hbFont, g, horiz ? HB_DIRECTION_RTL : HB_DIRECTION_TTB, 0, NULL, NULL, NULL);
        if (count > 0) {
            GlyphAssembly* a = (GlyphAssembly*) xmalloc(sizeof(GlyphAssembly));
            a->count = count;
            a->parts = (hb_ot_math_glyph_part_t*) xmalloc(count * sizeof(hb_ot_math_glyph_part_t));
            hb_ot_math_get_glyph_assembly(hbFont, g, horiz ? HB_DIRECTION_RTL : HB_DIRECTION_TTB, 0, &a->count, a->parts, NULL);
            rval = (void*) a;
        }
    }

    return rval;
}

void
free_ot_assembly(GlyphAssembly* a)
{
    if (!a)
        return;
    free (a->parts);
    free (a);
}

int
get_ot_math_ital_corr(int f, int g)
{
    hb_position_t rval = 0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        hb_font_t* hbFont = font->getHbFont();
        rval = hb_ot_math_get_glyph_italics_correction(hbFont, g);
        rval = D2Fix(font->unitsToPoints(rval));
    }

    return rval;
}

int
get_ot_math_accent_pos(int f, int g)
{
    hb_position_t rval = 0x7fffffffUL;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        hb_font_t* hbFont = font->getHbFont();
        rval = hb_ot_math_get_glyph_top_accent_attachment(hbFont, g);
        rval = D2Fix(font->unitsToPoints(rval));
    }

    return rval;
}

int
ot_min_connector_overlap(int f)
{
    hb_position_t rval = 0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        hb_font_t* hbFont = font->getHbFont();
        rval = hb_ot_math_get_min_connector_overlap(hbFont, HB_DIRECTION_RTL);
        rval = D2Fix(font->unitsToPoints(rval));
    }

    return rval;
}

static int
getMathKernAt(int f, int g, int height, hb_ot_math_kern_t side)
{
    hb_position_t rval = 0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        hb_font_t* hbFont = font->getHbFont();
        rval = hb_ot_math_get_glyph_kerning(hbFont, g, side, height);
    }

    return rval;
}

static float
glyph_height(int f, int g)
{
    float rval = 0.0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXLayoutEngine engine = (XeTeXLayoutEngine)fontlayoutengine[f];
        getGlyphHeightDepth(engine, g, &rval, NULL);
    }

    return rval;
}

static float
glyph_depth(int f, int g)
{
    float rval = 0.0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXLayoutEngine engine = (XeTeXLayoutEngine)fontlayoutengine[f];
        getGlyphHeightDepth(engine, g, NULL, &rval);
    }

    return rval;
}

// keep in sync with xetex.web
#define sup_cmd 0
#define sub_cmd 1

int
get_ot_math_kern(int f,  int g,    // Base font and glyph number
                 int sf, int sg,   // Sub/superscript font and glyph number
                 int cmd,          // sup_cmd or sub_cmd
                 int shift_scaled  // TeX scaled points
                 )
{
  int rval = 0;

  // Cf. https://docs.microsoft.com/en-us/typography/opentype/spec/math#mathkerninfo-table

  if (fontarea[f] == OTGR_FONT_FLAG && fontarea[sf] == OTGR_FONT_FLAG) {
    XeTeXFontInst* font  = (XeTeXFontInst*) getFont((XeTeXLayoutEngine)fontlayoutengine[f]);

    XeTeXFontInst* sfont = (XeTeXFontInst*) getFont((XeTeXLayoutEngine)fontlayoutengine[sf]);

    // Do calculations in glyph units.

    // The next four lines could be streamlined by having
    // glyph_height() and glyph_depth() return the metrics in glyph
    // units, but that would require tweaking getGlyphHeightDepth(),
    // which is used elsewhere.

    int g_height = font->pointsToUnits(glyph_height(f, g));
    int g_depth  = font->pointsToUnits(glyph_depth(f, g));

    int sg_height = sfont->pointsToUnits(glyph_height(sf, sg));
    int sg_depth  = sfont->pointsToUnits(glyph_depth(sf, sg));

    // Convert the shift amount to base glyph units.
    int shift = font->pointsToUnits(Fix2D(shift_scaled));

    // Multiply sub/superscript glyph units by scale_factor to make
    // them commensurate with base glyph units.

    float f_size  = font->getPointSize();
    float sf_size = sfont->getPointSize();

    assert(f_size != 0.0);

    float scale_factor = sf_size / f_size;

    if (cmd == sup_cmd) { // superscript
      // (1) Calculate the kern at the bottom of the superscript-glyph
      // bounding box.

      // In base glyph units, the bottom of the superscript is at a
      // distance of (shift - scale_factor * sg_depth) above the
      // baseline.

      int kern = getMathKernAt(f, g, shift - scale_factor * sg_depth,
                               HB_OT_MATH_KERN_TOP_RIGHT);

      // In superscript glyph units, the bottom of the superscript is
      // a distance of sg_depth below the baseline.

      int skern = getMathKernAt(sf, sg, -sg_depth,
                                HB_OT_MATH_KERN_BOTTOM_LEFT);

      int top_kern = kern + scale_factor * skern;

      // (2) Calculate the kern at the top of the base-glyph bounding
      // box.

      // In base glyph units, the top of the base glyph is a distance
      // of g_height above the baseline.

      kern = getMathKernAt(f, g, g_height, HB_OT_MATH_KERN_TOP_RIGHT);

      // In superscript glyph units, the top of the base glyph is a
      // distance of (g_height - shift)/scale_factor above the
      // baseline.

      skern = getMathKernAt(sf, sg, (g_height - shift) / scale_factor,
                            HB_OT_MATH_KERN_BOTTOM_LEFT);

      int bot_kern = kern + scale_factor * skern;

      // (3) The spec says "Take the minimum of these two sums," but
      // surely we want the kern that results in the greater
      // separation between the base and the superscript?  That
      // corresponds to the maximum.  (In the case where both kerns
      // are negative, this is the same as the kern with the minimum
      // *absolute* value, which is presumably what the spec means.)

      rval = (top_kern > bot_kern) ? top_kern : bot_kern;
    } else if (cmd == sub_cmd) { // subscript
      // (1) Calculate the kern at the top of the subscript-glyph
      // bounding box.

      // In base glyph units,, the top of the subscript is at a height
      // of scale_factor * sg_height - shift.

      int kern = getMathKernAt(f, g, scale_factor * sg_height - shift,
                               HB_OT_MATH_KERN_BOTTOM_RIGHT);

      // In subscript glyph units, the top of the subscript-glyph
      // bounding box is sg_height above the baseline.

      int skern = getMathKernAt(sf, sg, sg_height,
                                HB_OT_MATH_KERN_TOP_LEFT);

      int top_kern = kern + scale_factor * skern;

      // (2) Calculate the kern at the bottom of the base-glyph
      // bounding box.

      // In base glyph units, the bottom of the base-glyph is at
      // g_depth below the baseline:

      kern = getMathKernAt(f, g, -g_depth,
                           HB_OT_MATH_KERN_BOTTOM_RIGHT);

      // In subscript glyph units, the bottom of the base glyph is at
      // a height of shift - g_depth above the baseline, translated
      // into subscript glyph units.

      skern = getMathKernAt(sf, sg, (shift - g_depth) / scale_factor,
                            HB_OT_MATH_KERN_TOP_LEFT);

      int bot_kern = kern + scale_factor * skern;

      // (3) See above.

      rval = (top_kern > bot_kern) ? top_kern : bot_kern;
    } else {
      assert(0); // we should not reach here
    }

    rval = D2Fix(font->unitsToPoints(rval));
  }

  return rval;
}

int
ot_part_count(const GlyphAssembly* a)
{
    return a->count;
}

int
ot_part_glyph(const GlyphAssembly* a, int i)
{
    return a->parts[i].glyph;
}

int
ot_part_is_extender(const GlyphAssembly* a, int i)
{
    return (a->parts[i].flags & HB_MATH_GLYPH_PART_FLAG_EXTENDER) != 0;
}

int
ot_part_start_connector(int f, const GlyphAssembly* a, int i)
{
    int rval = 0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        rval = D2Fix(font->unitsToPoints(a->parts[i].start_connector_length));
    }

    return rval;
}

int
ot_part_end_connector(int f, const GlyphAssembly* a, int i)
{
    int rval = 0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        rval = D2Fix(font->unitsToPoints(a->parts[i].end_connector_length));
    }

    return rval;
}

int
ot_part_full_advance(int f, const GlyphAssembly* a, int i)
{
    int rval = 0;

    if (fontarea[f] == OTGR_FONT_FLAG) {
        XeTeXFontInst*  font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
        rval = D2Fix(font->unitsToPoints(a->parts[i].full_advance));
    }

    return rval;
}