Debian Codegraph @

texk/web2c/xetexdir/XeTeXOTMath.cpp

raw
1 /****************************************************************************\
2 Part of the XeTeX typesetting system
3 Copyright (c) 1994-2008 by SIL International
4 Copyright (c) 2009 by Jonathan Kew
5 Copyright (c) 2012-2015 by Khaled Hosny
6
7 SIL Author(s): Jonathan Kew
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
24 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
25 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 Except as contained in this notice, the name of the copyright holders
29 shall not be used in advertising or otherwise to promote the sale,
30 use or other dealings in this Software without prior written
31 authorization from the copyright holders.
32 \****************************************************************************/
33
34 #include <w2c/config.h>
35
36 #include <assert.h>
37 #include <algorithm>
38
39 #include "XeTeXOTMath.h"
40
41 #include "XeTeX_web.h"
42 #include "XeTeXLayoutInterface.h"
43 #include "XeTeXFontInst.h"
44
45 int
46 get_ot_math_constant(int f, int n)
47 {
48 hb_ot_math_constant_t constant = (hb_ot_math_constant_t) n;
49 hb_position_t rval = 0;
50
51 if (fontarea[f] == OTGR_FONT_FLAG) {
52 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
53 hb_font_t* hbFont = font->getHbFont();
54 rval = hb_ot_math_get_constant(hbFont, constant);
55 /* scale according to font size, except the ones that are percentages */
56 switch (constant) {
57 case HB_OT_MATH_CONSTANT_SCRIPT_PERCENT_SCALE_DOWN:
58 case HB_OT_MATH_CONSTANT_SCRIPT_SCRIPT_PERCENT_SCALE_DOWN:
59 case HB_OT_MATH_CONSTANT_RADICAL_DEGREE_BOTTOM_RAISE_PERCENT:
60 break;
61 default:
62 rval = D2Fix(font->unitsToPoints(rval));
63 break;
64 }
65 }
66 return rval;
67 }
68
69 #define unknown ((hb_ot_math_constant_t)-1)
70
71 /* fontdimen IDs for math symbols font (family 2) */
72 #define math_x_height 5
73 #define math_quad 6
74 #define num1 8 /* numerator shift-up in display styles */
75 #define num2 9 /* numerator shift-up in non-display, non-\.{\\atop} */
76 #define num3 10 /* numerator shift-up in non-display \.{\\atop} */
77 #define denom1 11 /* denominator shift-down in display styles */
78 #define denom2 12 /* denominator shift-down in non-display styles */
79 #define sup1 13 /* superscript shift-up in uncramped display style */
80 #define sup2 14 /* superscript shift-up in uncramped non-display */
81 #define sup3 15 /* superscript shift-up in cramped styles */
82 #define sub1 16 /* subscript shift-down if superscript is absent */
83 #define sub2 17 /* subscript shift-down if superscript is present */
84 #define sup_drop 18 /* superscript baseline below top of large box */
85 #define sub_drop 19 /* subscript baseline below bottom of large box */
86 #define delim1 20 /* size of \.{\\atopwithdelims} delimiters */
87 #define delim2 21 /* size of \.{\\atopwithdelims} delimiters in non-displays */
88 #define axis_height 22 /* height of fraction lines above the baseline */
89
90 const hb_ot_math_constant_t TeX_sym_to_OT_map[] = {
91 unknown,
92 unknown,
93 unknown,
94 unknown,
95 unknown,
96 HB_OT_MATH_CONSTANT_ACCENT_BASE_HEIGHT, // x-height
97 unknown, // quad
98 unknown,
99 HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_DISPLAY_STYLE_SHIFT_UP,
100 HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_SHIFT_UP,
101 HB_OT_MATH_CONSTANT_STACK_TOP_SHIFT_UP,
102 HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_DISPLAY_STYLE_SHIFT_DOWN,
103 HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_SHIFT_DOWN,
104 HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP, // ??
105 HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP, // ??
106 HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP_CRAMPED,
107 HB_OT_MATH_CONSTANT_SUBSCRIPT_SHIFT_DOWN, // ??
108 HB_OT_MATH_CONSTANT_SUBSCRIPT_SHIFT_DOWN, // ??
109 HB_OT_MATH_CONSTANT_SUPERSCRIPT_BASELINE_DROP_MAX, // ??
110 HB_OT_MATH_CONSTANT_SUBSCRIPT_BASELINE_DROP_MIN, // ??
111 HB_OT_MATH_CONSTANT_DELIMITED_SUB_FORMULA_MIN_HEIGHT,
112 unknown, // using quad instead for now
113 HB_OT_MATH_CONSTANT_AXIS_HEIGHT
114 };
115
116 int
117 get_native_mathsy_param(int f, int n)
118 {
119 int rval = 0;
120
121 if (n == math_quad) {
122 rval = fontsize[f];
123 }
124 else if (n == delim2) { // XXX not sure what OT parameter we should use here;
125 // for now we use 1.5em, clamped to delim1 height
126 rval = std::min<int>(1.5 * fontsize[f], get_native_mathsy_param(f, delim1));
127 }
128 else {
129 if (n < sizeof(TeX_sym_to_OT_map) / sizeof(hb_ot_math_constant_t)) {
130 hb_ot_math_constant_t ot_index = TeX_sym_to_OT_map[n];
131 if (ot_index != unknown)
132 rval = get_ot_math_constant(f, (int)ot_index);
133 }
134 }
135 // fprintf(stderr, " math_sy(%d, %d) returns %.3f\n", f, n, Fix2D(rval));
136
137 return rval;
138 }
139
140 /* fontdimen IDs for math extension font (family 3) */
141 #define default_rule_thickness 8 /* thickness of \.{\\over} bars */
142 #define big_op_spacing1 9 /* minimum clearance above a displayed op */
143 #define big_op_spacing2 10 /* minimum clearance below a displayed op */
144 #define big_op_spacing3 11 /* minimum baselineskip above displayed op */
145 #define big_op_spacing4 12 /* minimum baselineskip below displayed op */
146 #define big_op_spacing5 13 /* padding above and below displayed limits */
147
148 const hb_ot_math_constant_t TeX_ext_to_OT_map[] = {
149 unknown,
150 unknown,
151 unknown,
152 unknown,
153 unknown,
154 HB_OT_MATH_CONSTANT_ACCENT_BASE_HEIGHT, // x-height
155 unknown, // quad
156 unknown,
157 HB_OT_MATH_CONSTANT_FRACTION_RULE_THICKNESS, // default_rule_thickness
158 HB_OT_MATH_CONSTANT_UPPER_LIMIT_GAP_MIN, // big_op_spacing1
159 HB_OT_MATH_CONSTANT_LOWER_LIMIT_GAP_MIN, // big_op_spacing2
160 HB_OT_MATH_CONSTANT_UPPER_LIMIT_BASELINE_RISE_MIN, // big_op_spacing3
161 HB_OT_MATH_CONSTANT_LOWER_LIMIT_BASELINE_DROP_MIN, // big_op_spacing4
162 HB_OT_MATH_CONSTANT_STACK_GAP_MIN // big_op_spacing5
163 };
164
165 int
166 get_native_mathex_param(int f, int n)
167 {
168 int rval = 0;
169
170 if (n == math_quad)
171 rval = fontsize[f];
172 else {
173 if (n < sizeof(TeX_ext_to_OT_map) / sizeof(hb_ot_math_constant_t)) {
174 hb_ot_math_constant_t ot_index = TeX_ext_to_OT_map[n];
175 if (ot_index != unknown)
176 rval = get_ot_math_constant(f, (int)ot_index);
177 }
178 }
179 // fprintf(stderr, " math_ex(%d, %d) returns %.3f\n", f, n, Fix2D(rval));
180
181 return rval;
182 }
183
184 int
185 get_ot_math_variant(int f, int g, int v, integer* adv, int horiz)
186 {
187 hb_codepoint_t rval = g;
188 *adv = -1;
189
190 if (fontarea[f] == OTGR_FONT_FLAG) {
191 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
192 hb_font_t* hbFont = font->getHbFont();
193 hb_ot_math_glyph_variant_t variant[1];
194 unsigned int count = 1;
195 hb_ot_math_get_glyph_variants(hbFont, g, horiz ? HB_DIRECTION_RTL : HB_DIRECTION_TTB, v, &count, variant);
196 if (count > 0) {
197 rval = variant->glyph;
198 *adv = D2Fix(font->unitsToPoints(variant->advance));
199 }
200 }
201
202 return rval;
203 }
204
205
206 void*
207 get_ot_assembly_ptr(int f, int g, int horiz)
208 {
209 void* rval = NULL;
210
211 if (fontarea[f] == OTGR_FONT_FLAG) {
212 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
213 hb_font_t* hbFont = font->getHbFont();
214
215 unsigned int count = hb_ot_math_get_glyph_assembly(hbFont, g, horiz ? HB_DIRECTION_RTL : HB_DIRECTION_TTB, 0, NULL, NULL, NULL);
216 if (count > 0) {
217 GlyphAssembly* a = (GlyphAssembly*) xmalloc(sizeof(GlyphAssembly));
218 a->count = count;
219 a->parts = (hb_ot_math_glyph_part_t*) xmalloc(count * sizeof(hb_ot_math_glyph_part_t));
220 hb_ot_math_get_glyph_assembly(hbFont, g, horiz ? HB_DIRECTION_RTL : HB_DIRECTION_TTB, 0, &a->count, a->parts, NULL);
221 rval = (void*) a;
222 }
223 }
224
225 return rval;
226 }
227
228 void
229 free_ot_assembly(GlyphAssembly* a)
230 {
231 if (!a)
232 return;
233 free (a->parts);
234 free (a);
235 }
236
237 int
238 get_ot_math_ital_corr(int f, int g)
239 {
240 hb_position_t rval = 0;
241
242 if (fontarea[f] == OTGR_FONT_FLAG) {
243 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
244 hb_font_t* hbFont = font->getHbFont();
245 rval = hb_ot_math_get_glyph_italics_correction(hbFont, g);
246 rval = D2Fix(font->unitsToPoints(rval));
247 }
248
249 return rval;
250 }
251
252 int
253 get_ot_math_accent_pos(int f, int g)
254 {
255 hb_position_t rval = 0x7fffffffUL;
256
257 if (fontarea[f] == OTGR_FONT_FLAG) {
258 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
259 hb_font_t* hbFont = font->getHbFont();
260 rval = hb_ot_math_get_glyph_top_accent_attachment(hbFont, g);
261 rval = D2Fix(font->unitsToPoints(rval));
262 }
263
264 return rval;
265 }
266
267 int
268 ot_min_connector_overlap(int f)
269 {
270 hb_position_t rval = 0;
271
272 if (fontarea[f] == OTGR_FONT_FLAG) {
273 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
274 hb_font_t* hbFont = font->getHbFont();
275 rval = hb_ot_math_get_min_connector_overlap(hbFont, HB_DIRECTION_RTL);
276 rval = D2Fix(font->unitsToPoints(rval));
277 }
278
279 return rval;
280 }
281
282 static int
283 getMathKernAt(int f, int g, int height, hb_ot_math_kern_t side)
284 {
285 hb_position_t rval = 0;
286
287 if (fontarea[f] == OTGR_FONT_FLAG) {
288 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
289 hb_font_t* hbFont = font->getHbFont();
290 rval = hb_ot_math_get_glyph_kerning(hbFont, g, side, height);
291 }
292
293 return rval;
294 }
295
296 static float
297 glyph_height(int f, int g)
298 {
299 float rval = 0.0;
300
301 if (fontarea[f] == OTGR_FONT_FLAG) {
302 XeTeXLayoutEngine engine = (XeTeXLayoutEngine)fontlayoutengine[f];
303 getGlyphHeightDepth(engine, g, &rval, NULL);
304 }
305
306 return rval;
307 }
308
309 static float
310 glyph_depth(int f, int g)
311 {
312 float rval = 0.0;
313
314 if (fontarea[f] == OTGR_FONT_FLAG) {
315 XeTeXLayoutEngine engine = (XeTeXLayoutEngine)fontlayoutengine[f];
316 getGlyphHeightDepth(engine, g, NULL, &rval);
317 }
318
319 return rval;
320 }
321
322 // keep in sync with xetex.web
323 #define sup_cmd 0
324 #define sub_cmd 1
325
326 int
327 get_ot_math_kern(int f, int g, // Base font and glyph number
328 int sf, int sg, // Sub/superscript font and glyph number
329 int cmd, // sup_cmd or sub_cmd
330 int shift_scaled // TeX scaled points
331 )
332 {
333 int rval = 0;
334
335 // Cf. https://docs.microsoft.com/en-us/typography/opentype/spec/math#mathkerninfo-table
336
337 if (fontarea[f] == OTGR_FONT_FLAG && fontarea[sf] == OTGR_FONT_FLAG) {
338 XeTeXFontInst* font = (XeTeXFontInst*) getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
339
340 XeTeXFontInst* sfont = (XeTeXFontInst*) getFont((XeTeXLayoutEngine)fontlayoutengine[sf]);
341
342 // Do calculations in glyph units.
343
344 // The next four lines could be streamlined by having
345 // glyph_height() and glyph_depth() return the metrics in glyph
346 // units, but that would require tweaking getGlyphHeightDepth(),
347 // which is used elsewhere.
348
349 int g_height = font->pointsToUnits(glyph_height(f, g));
350 int g_depth = font->pointsToUnits(glyph_depth(f, g));
351
352 int sg_height = sfont->pointsToUnits(glyph_height(sf, sg));
353 int sg_depth = sfont->pointsToUnits(glyph_depth(sf, sg));
354
355 // Convert the shift amount to base glyph units.
356 int shift = font->pointsToUnits(Fix2D(shift_scaled));
357
358 // Multiply sub/superscript glyph units by scale_factor to make
359 // them commensurate with base glyph units.
360
361 float f_size = font->getPointSize();
362 float sf_size = sfont->getPointSize();
363
364 assert(f_size != 0.0);
365
366 float scale_factor = sf_size / f_size;
367
368 if (cmd == sup_cmd) { // superscript
369 // (1) Calculate the kern at the bottom of the superscript-glyph
370 // bounding box.
371
372 // In base glyph units, the bottom of the superscript is at a
373 // distance of (shift - scale_factor * sg_depth) above the
374 // baseline.
375
376 int kern = getMathKernAt(f, g, shift - scale_factor * sg_depth,
377 HB_OT_MATH_KERN_TOP_RIGHT);
378
379 // In superscript glyph units, the bottom of the superscript is
380 // a distance of sg_depth below the baseline.
381
382 int skern = getMathKernAt(sf, sg, -sg_depth,
383 HB_OT_MATH_KERN_BOTTOM_LEFT);
384
385 int top_kern = kern + scale_factor * skern;
386
387 // (2) Calculate the kern at the top of the base-glyph bounding
388 // box.
389
390 // In base glyph units, the top of the base glyph is a distance
391 // of g_height above the baseline.
392
393 kern = getMathKernAt(f, g, g_height, HB_OT_MATH_KERN_TOP_RIGHT);
394
395 // In superscript glyph units, the top of the base glyph is a
396 // distance of (g_height - shift)/scale_factor above the
397 // baseline.
398
399 skern = getMathKernAt(sf, sg, (g_height - shift) / scale_factor,
400 HB_OT_MATH_KERN_BOTTOM_LEFT);
401
402 int bot_kern = kern + scale_factor * skern;
403
404 // (3) The spec says "Take the minimum of these two sums," but
405 // surely we want the kern that results in the greater
406 // separation between the base and the superscript? That
407 // corresponds to the maximum. (In the case where both kerns
408 // are negative, this is the same as the kern with the minimum
409 // *absolute* value, which is presumably what the spec means.)
410
411 rval = (top_kern > bot_kern) ? top_kern : bot_kern;
412 } else if (cmd == sub_cmd) { // subscript
413 // (1) Calculate the kern at the top of the subscript-glyph
414 // bounding box.
415
416 // In base glyph units,, the top of the subscript is at a height
417 // of scale_factor * sg_height - shift.
418
419 int kern = getMathKernAt(f, g, scale_factor * sg_height - shift,
420 HB_OT_MATH_KERN_BOTTOM_RIGHT);
421
422 // In subscript glyph units, the top of the subscript-glyph
423 // bounding box is sg_height above the baseline.
424
425 int skern = getMathKernAt(sf, sg, sg_height,
426 HB_OT_MATH_KERN_TOP_LEFT);
427
428 int top_kern = kern + scale_factor * skern;
429
430 // (2) Calculate the kern at the bottom of the base-glyph
431 // bounding box.
432
433 // In base glyph units, the bottom of the base-glyph is at
434 // g_depth below the baseline:
435
436 kern = getMathKernAt(f, g, -g_depth,
437 HB_OT_MATH_KERN_BOTTOM_RIGHT);
438
439 // In subscript glyph units, the bottom of the base glyph is at
440 // a height of shift - g_depth above the baseline, translated
441 // into subscript glyph units.
442
443 skern = getMathKernAt(sf, sg, (shift - g_depth) / scale_factor,
444 HB_OT_MATH_KERN_TOP_LEFT);
445
446 int bot_kern = kern + scale_factor * skern;
447
448 // (3) See above.
449
450 rval = (top_kern > bot_kern) ? top_kern : bot_kern;
451 } else {
452 assert(0); // we should not reach here
453 }
454
455 rval = D2Fix(font->unitsToPoints(rval));
456 }
457
458 return rval;
459 }
460
461 int
462 ot_part_count(const GlyphAssembly* a)
463 {
464 return a->count;
465 }
466
467 int
468 ot_part_glyph(const GlyphAssembly* a, int i)
469 {
470 return a->parts[i].glyph;
471 }
472
473 int
474 ot_part_is_extender(const GlyphAssembly* a, int i)
475 {
476 return (a->parts[i].flags & HB_MATH_GLYPH_PART_FLAG_EXTENDER) != 0;
477 }
478
479 int
480 ot_part_start_connector(int f, const GlyphAssembly* a, int i)
481 {
482 int rval = 0;
483
484 if (fontarea[f] == OTGR_FONT_FLAG) {
485 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
486 rval = D2Fix(font->unitsToPoints(a->parts[i].start_connector_length));
487 }
488
489 return rval;
490 }
491
492 int
493 ot_part_end_connector(int f, const GlyphAssembly* a, int i)
494 {
495 int rval = 0;
496
497 if (fontarea[f] == OTGR_FONT_FLAG) {
498 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
499 rval = D2Fix(font->unitsToPoints(a->parts[i].end_connector_length));
500 }
501
502 return rval;
503 }
504
505 int
506 ot_part_full_advance(int f, const GlyphAssembly* a, int i)
507 {
508 int rval = 0;
509
510 if (fontarea[f] == OTGR_FONT_FLAG) {
511 XeTeXFontInst* font = (XeTeXFontInst*)getFont((XeTeXLayoutEngine)fontlayoutengine[f]);
512 rval = D2Fix(font->unitsToPoints(a->parts[i].full_advance));
513 }
514
515 return rval;
516 }