hb-ot-math-table.hh 23.4 KB
Newer Older
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
/*
 * Copyright © 2016  Igalia S.L.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Igalia Author(s): Frédéric Wang
 */

27 28
#ifndef HB_OT_MATH_TABLE_HH
#define HB_OT_MATH_TABLE_HH
29

30 31
#include "hb-open-type.hh"
#include "hb-ot-layout-common.hh"
32
#include "hb-ot-math.h"
33 34 35

namespace OT {

36 37 38

struct MathValueRecord
{
39 40 41 42
  inline hb_position_t get_x_value (hb_font_t *font, const void *base) const
  { return font->em_scale_x (value) + (base+deviceTable).get_x_delta (font); }
  inline hb_position_t get_y_value (hb_font_t *font, const void *base) const
  { return font->em_scale_y (value) + (base+deviceTable).get_y_delta (font); }
43 44 45 46 47 48 49

  inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) && deviceTable.sanitize (c, base));
  }

50
  protected:
B
Behdad Esfahbod 已提交
51
  HBINT16			value;		/* The X or Y value in design units */
52
  OffsetTo<Device>	deviceTable;	/* Offset to the device table - from the
E
Ebrahim Byagowi 已提交
53
					 * beginning of parent table.  May be NULL.
54
					 * Suggested format for device table is 1. */
55

56
  public:
57
  DEFINE_SIZE_STATIC (4);
58 59 60 61 62 63 64
};

struct MathConstants
{
  inline bool sanitize_math_value_records (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
65

66
    unsigned int count = ARRAY_LENGTH (mathValueRecords);
67
    for (unsigned int i = 0; i < count; i++)
68 69 70
      if (!mathValueRecords[i].sanitize (c, this))
	return_trace (false);

71 72 73 74 75 76 77 78 79
    return_trace (true);
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) && sanitize_math_value_records(c));
  }

80 81
  inline hb_position_t get_value (hb_ot_math_constant_t constant,
				  hb_font_t *font) const
82 83
  {
    switch (constant) {
84 85 86 87 88

    case HB_OT_MATH_CONSTANT_SCRIPT_PERCENT_SCALE_DOWN:
    case HB_OT_MATH_CONSTANT_SCRIPT_SCRIPT_PERCENT_SCALE_DOWN:
      return percentScaleDown[constant - HB_OT_MATH_CONSTANT_SCRIPT_PERCENT_SCALE_DOWN];

89 90 91 92 93 94 95 96
    case HB_OT_MATH_CONSTANT_DELIMITED_SUB_FORMULA_MIN_HEIGHT:
    case HB_OT_MATH_CONSTANT_DISPLAY_OPERATOR_MIN_HEIGHT:
      return font->em_scale_y (minHeight[constant - HB_OT_MATH_CONSTANT_DELIMITED_SUB_FORMULA_MIN_HEIGHT]);

    case HB_OT_MATH_CONSTANT_RADICAL_KERN_AFTER_DEGREE:
    case HB_OT_MATH_CONSTANT_RADICAL_KERN_BEFORE_DEGREE:
    case HB_OT_MATH_CONSTANT_SKEWED_FRACTION_HORIZONTAL_GAP:
    case HB_OT_MATH_CONSTANT_SPACE_AFTER_SCRIPT:
97
      return mathValueRecords[constant - HB_OT_MATH_CONSTANT_MATH_LEADING].get_x_value(font, this);
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

    case HB_OT_MATH_CONSTANT_ACCENT_BASE_HEIGHT:
    case HB_OT_MATH_CONSTANT_AXIS_HEIGHT:
    case HB_OT_MATH_CONSTANT_FLATTENED_ACCENT_BASE_HEIGHT:
    case HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_DISPLAY_STYLE_SHIFT_DOWN:
    case HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_GAP_MIN:
    case HB_OT_MATH_CONSTANT_FRACTION_DENOMINATOR_SHIFT_DOWN:
    case HB_OT_MATH_CONSTANT_FRACTION_DENOM_DISPLAY_STYLE_GAP_MIN:
    case HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_DISPLAY_STYLE_SHIFT_UP:
    case HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_GAP_MIN:
    case HB_OT_MATH_CONSTANT_FRACTION_NUMERATOR_SHIFT_UP:
    case HB_OT_MATH_CONSTANT_FRACTION_NUM_DISPLAY_STYLE_GAP_MIN:
    case HB_OT_MATH_CONSTANT_FRACTION_RULE_THICKNESS:
    case HB_OT_MATH_CONSTANT_LOWER_LIMIT_BASELINE_DROP_MIN:
    case HB_OT_MATH_CONSTANT_LOWER_LIMIT_GAP_MIN:
    case HB_OT_MATH_CONSTANT_MATH_LEADING:
    case HB_OT_MATH_CONSTANT_OVERBAR_EXTRA_ASCENDER:
    case HB_OT_MATH_CONSTANT_OVERBAR_RULE_THICKNESS:
    case HB_OT_MATH_CONSTANT_OVERBAR_VERTICAL_GAP:
    case HB_OT_MATH_CONSTANT_RADICAL_DISPLAY_STYLE_VERTICAL_GAP:
    case HB_OT_MATH_CONSTANT_RADICAL_EXTRA_ASCENDER:
    case HB_OT_MATH_CONSTANT_RADICAL_RULE_THICKNESS:
    case HB_OT_MATH_CONSTANT_RADICAL_VERTICAL_GAP:
    case HB_OT_MATH_CONSTANT_SKEWED_FRACTION_VERTICAL_GAP:
    case HB_OT_MATH_CONSTANT_STACK_BOTTOM_DISPLAY_STYLE_SHIFT_DOWN:
    case HB_OT_MATH_CONSTANT_STACK_BOTTOM_SHIFT_DOWN:
    case HB_OT_MATH_CONSTANT_STACK_DISPLAY_STYLE_GAP_MIN:
    case HB_OT_MATH_CONSTANT_STACK_GAP_MIN:
    case HB_OT_MATH_CONSTANT_STACK_TOP_DISPLAY_STYLE_SHIFT_UP:
    case HB_OT_MATH_CONSTANT_STACK_TOP_SHIFT_UP:
    case HB_OT_MATH_CONSTANT_STRETCH_STACK_BOTTOM_SHIFT_DOWN:
    case HB_OT_MATH_CONSTANT_STRETCH_STACK_GAP_ABOVE_MIN:
    case HB_OT_MATH_CONSTANT_STRETCH_STACK_GAP_BELOW_MIN:
    case HB_OT_MATH_CONSTANT_STRETCH_STACK_TOP_SHIFT_UP:
    case HB_OT_MATH_CONSTANT_SUBSCRIPT_BASELINE_DROP_MIN:
    case HB_OT_MATH_CONSTANT_SUBSCRIPT_SHIFT_DOWN:
    case HB_OT_MATH_CONSTANT_SUBSCRIPT_TOP_MAX:
    case HB_OT_MATH_CONSTANT_SUB_SUPERSCRIPT_GAP_MIN:
    case HB_OT_MATH_CONSTANT_SUPERSCRIPT_BASELINE_DROP_MAX:
    case HB_OT_MATH_CONSTANT_SUPERSCRIPT_BOTTOM_MAX_WITH_SUBSCRIPT:
    case HB_OT_MATH_CONSTANT_SUPERSCRIPT_BOTTOM_MIN:
    case HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP:
    case HB_OT_MATH_CONSTANT_SUPERSCRIPT_SHIFT_UP_CRAMPED:
    case HB_OT_MATH_CONSTANT_UNDERBAR_EXTRA_DESCENDER:
    case HB_OT_MATH_CONSTANT_UNDERBAR_RULE_THICKNESS:
    case HB_OT_MATH_CONSTANT_UNDERBAR_VERTICAL_GAP:
    case HB_OT_MATH_CONSTANT_UPPER_LIMIT_BASELINE_RISE_MIN:
    case HB_OT_MATH_CONSTANT_UPPER_LIMIT_GAP_MIN:
146
      return mathValueRecords[constant - HB_OT_MATH_CONSTANT_MATH_LEADING].get_y_value(font, this);
147 148 149

    case HB_OT_MATH_CONSTANT_RADICAL_DEGREE_BOTTOM_RAISE_PERCENT:
      return radicalDegreeBottomRaisePercent;
150

151 152
    default:
      return 0;
153 154 155
    }
  }

156
  protected:
B
Behdad Esfahbod 已提交
157 158
  HBINT16 percentScaleDown[2];
  HBUINT16 minHeight[2];
159
  MathValueRecord mathValueRecords[51];
B
Behdad Esfahbod 已提交
160
  HBINT16 radicalDegreeBottomRaisePercent;
161

162
  public:
163
  DEFINE_SIZE_STATIC (214);
164 165
};

166 167 168 169 170 171
struct MathItalicsCorrectionInfo
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
172 173
		  coverage.sanitize (c, this) &&
		  italicsCorrection.sanitize (c, this));
174 175
  }

176 177
  inline hb_position_t get_value (hb_codepoint_t glyph,
				  hb_font_t *font) const
178 179
  {
    unsigned int index = (this+coverage).get_coverage (glyph);
180
    return italicsCorrection[index].get_x_value (font, this);
181 182
  }

183
  protected:
184 185 186 187 188 189 190 191
  OffsetTo<Coverage>       coverage;		/* Offset to Coverage table -
						 * from the beginning of
						 * MathItalicsCorrectionInfo
						 * table. */
  ArrayOf<MathValueRecord> italicsCorrection;	/* Array of MathValueRecords
						 * defining italics correction
						 * values for each
						 * covered glyph. */
192

193
  public:
194
  DEFINE_SIZE_ARRAY (4, italicsCorrection);
195 196 197 198 199 200 201 202
};

struct MathTopAccentAttachment
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
203 204
		  topAccentCoverage.sanitize (c, this) &&
		  topAccentAttachment.sanitize (c, this));
205 206
  }

207 208
  inline hb_position_t get_value (hb_codepoint_t glyph,
				  hb_font_t *font) const
209 210
  {
    unsigned int index = (this+topAccentCoverage).get_coverage (glyph);
211 212 213
    if (index == NOT_COVERED)
      return font->get_glyph_h_advance (glyph) / 2;
    return topAccentAttachment[index].get_x_value(font, this);
214 215
  }

216
  protected:
217
  OffsetTo<Coverage>       topAccentCoverage;   /* Offset to Coverage table -
218 219 220
						 * from the beginning of
						 * MathTopAccentAttachment
						 * table. */
221
  ArrayOf<MathValueRecord> topAccentAttachment; /* Array of MathValueRecords
222 223 224
						 * defining top accent
						 * attachment points for each
						 * covered glyph. */
225

226
  public:
227 228 229 230 231 232 233 234 235 236
  DEFINE_SIZE_ARRAY (2 + 2, topAccentAttachment);
};

struct MathKern
{
  inline bool sanitize_math_value_records (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    unsigned int count = 2 * heightCount + 1;
    for (unsigned int i = 0; i < count; i++)
237
      if (!mathValueRecordsZ.arrayZ[i].sanitize (c, this)) return_trace (false);
238 239 240 241 242 243 244
    return_trace (true);
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
245
		  c->check_array (mathValueRecordsZ.arrayZ, 2 * heightCount + 1) &&
246
		  sanitize_math_value_records (c));
247 248
  }

B
Behdad Esfahbod 已提交
249
  inline hb_position_t get_value (hb_position_t correction_height, hb_font_t *font) const
250
  {
251 252
    const MathValueRecord* correctionHeight = mathValueRecordsZ.arrayZ;
    const MathValueRecord* kernValue = mathValueRecordsZ.arrayZ + heightCount;
253 254 255 256 257 258 259 260 261 262
    int sign = font->y_scale < 0 ? -1 : +1;

    /* The description of the MathKern table is a ambiguous, but interpreting
     * "between the two heights found at those indexes" for 0 < i < len as
     *
     *   correctionHeight[i-1] < correction_height <= correctionHeight[i]
     *
     * makes the result consistent with the limit cases and we can just use the
     * binary search algorithm of std::upper_bound:
     */
263
    unsigned int i = 0;
264 265 266
    unsigned int count = heightCount;
    while (count > 0)
    {
267
      unsigned int half = count / 2;
268 269 270
      hb_position_t height = correctionHeight[i + half].get_y_value(font, this);
      if (sign * height < sign * correction_height)
      {
271 272
	i += half + 1;
	count -= half + 1;
273
      } else
274
	count = half;
275 276 277 278
    }
    return kernValue[i].get_x_value(font, this);
  }

279
  protected:
280 281 282 283 284 285 286 287 288 289
  HBUINT16	heightCount;
  UnsizedArrayOf<MathValueRecord>
		mathValueRecordsZ;	/* Array of correction heights at
					 * which the kern value changes.
					 * Sorted by the height value in
					 * design units (heightCount entries),
					 * Followed by:
					 * Array of kern values corresponding
					 * to heights. (heightCount+1 entries).
					 */
290

291
  public:
292
  DEFINE_SIZE_ARRAY (2, mathValueRecordsZ);
293 294 295 296 297 298 299 300
};

struct MathKernInfoRecord
{
  inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
  {
    TRACE_SANITIZE (this);

301 302 303 304 305 306
    unsigned int count = ARRAY_LENGTH (mathKern);
    for (unsigned int i = 0; i < count; i++)
      if (unlikely (!mathKern[i].sanitize (c, base)))
	return_trace (false);

    return_trace (true);
307
  }
308

B
Behdad Esfahbod 已提交
309 310 311 312
  inline hb_position_t get_kerning (hb_ot_math_kern_t kern,
				    hb_position_t correction_height,
				    hb_font_t *font,
				    const void *base) const
313 314
  {
    unsigned int idx = kern;
B
Behdad Esfahbod 已提交
315 316
    if (unlikely (idx >= ARRAY_LENGTH (mathKern))) return 0;
    return (base+mathKern[idx]).get_value (correction_height, font);
317 318
  }

319
  protected:
320
  /* Offset to MathKern table for each corner -
E
Ebrahim Byagowi 已提交
321
   * from the beginning of MathKernInfo table.  May be NULL. */
322
  OffsetTo<MathKern> mathKern[4];
323

324
  public:
325
  DEFINE_SIZE_STATIC (8);
326 327 328 329 330 331 332 333
};

struct MathKernInfo
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
334 335
		  mathKernCoverage.sanitize (c, this) &&
		  mathKernInfoRecords.sanitize (c, this));
336 337
  }

B
Behdad Esfahbod 已提交
338 339 340 341
  inline hb_position_t get_kerning (hb_codepoint_t glyph,
				    hb_ot_math_kern_t kern,
				    hb_position_t correction_height,
				    hb_font_t *font) const
342 343
  {
    unsigned int index = (this+mathKernCoverage).get_coverage (glyph);
B
Behdad Esfahbod 已提交
344
    return mathKernInfoRecords[index].get_kerning (kern, correction_height, font, this);
345 346
  }

347
  protected:
348 349 350 351 352 353 354 355 356
  OffsetTo<Coverage>		mathKernCoverage;    /* Offset to Coverage table -
						      * from the beginning of the
						      * MathKernInfo table. */
  ArrayOf<MathKernInfoRecord>	mathKernInfoRecords; /* Array of
						      * MathKernInfoRecords,
						      * per-glyph information for
						      * mathematical positioning
						      * of subscripts and
						      * superscripts. */
357

358
  public:
359
  DEFINE_SIZE_ARRAY (4, mathKernInfoRecords);
360 361 362 363 364 365 366 367
};

struct MathGlyphInfo
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
368 369 370 371
		  mathItalicsCorrectionInfo.sanitize (c, this) &&
		  mathTopAccentAttachment.sanitize (c, this) &&
		  extendedShapeCoverage.sanitize (c, this) &&
		  mathKernInfo.sanitize(c, this));
372 373
  }

374
  inline hb_position_t
375 376
  get_italics_correction (hb_codepoint_t  glyph, hb_font_t *font) const
  { return (this+mathItalicsCorrectionInfo).get_value (glyph, font); }
377

378 379 380
  inline hb_position_t
  get_top_accent_attachment (hb_codepoint_t  glyph, hb_font_t *font) const
  { return (this+mathTopAccentAttachment).get_value (glyph, font); }
381 382

  inline bool is_extended_shape (hb_codepoint_t glyph) const
383
  { return (this+extendedShapeCoverage).get_coverage (glyph) != NOT_COVERED; }
384

B
Behdad Esfahbod 已提交
385 386 387 388 389
  inline hb_position_t get_kerning (hb_codepoint_t glyph,
				    hb_ot_math_kern_t kern,
				    hb_position_t correction_height,
				    hb_font_t *font) const
  { return (this+mathKernInfo).get_kerning (glyph, kern, correction_height, font); }
390

391
  protected:
392
  /* Offset to MathItalicsCorrectionInfo table -
393
   * from the beginning of MathGlyphInfo table. */
394 395 396
  OffsetTo<MathItalicsCorrectionInfo> mathItalicsCorrectionInfo;

  /* Offset to MathTopAccentAttachment table -
397
   * from the beginning of MathGlyphInfo table. */
398 399 400
  OffsetTo<MathTopAccentAttachment> mathTopAccentAttachment;

  /* Offset to coverage table for Extended Shape glyphs -
401 402 403
   * from the beginning of MathGlyphInfo table. When the left or right glyph of
   * a box is an extended shape variant, the (ink) box (and not the default
   * position defined by values in MathConstants table) should be used for
E
Ebrahim Byagowi 已提交
404
   * vertical positioning purposes.  May be NULL.. */
405 406 407
  OffsetTo<Coverage> extendedShapeCoverage;

   /* Offset to MathKernInfo table -
408
    * from the beginning of MathGlyphInfo table. */
409 410
  OffsetTo<MathKernInfo> mathKernInfo;

411
  public:
412
  DEFINE_SIZE_STATIC (8);
413 414
};

415 416
struct MathGlyphVariantRecord
{
417 418
  friend struct MathGlyphConstruction;

419 420 421 422 423 424
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this));
  }

425
  protected:
426
  GlyphID variantGlyph;       /* Glyph ID for the variant. */
B
Behdad Esfahbod 已提交
427
  HBUINT16  advanceMeasurement; /* Advance width/height, in design units, of the
428 429 430
			       * variant, in the direction of requested
			       * glyph extension. */

431
  public:
B
Behdad Esfahbod 已提交
432
  DEFINE_SIZE_STATIC (4);
433 434
};

B
Behdad Esfahbod 已提交
435
struct PartFlags : HBUINT16
436 437
{
  enum Flags {
438 439 440
    Extender	= 0x0001u, /* If set, the part can be skipped or repeated. */

    Defined	= 0x0001u, /* All defined flags. */
441 442
  };

443
  public:
444 445 446
  DEFINE_SIZE_STATIC (2);
};

447
struct MathGlyphPartRecord
448 449 450 451 452 453 454
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this));
  }

455
  inline void extract (hb_ot_math_glyph_part_t &out,
456 457 458 459 460 461 462 463 464
		       int scale,
		       hb_font_t *font) const
  {
    out.glyph			= glyph;

    out.start_connector_length	= font->em_scale (startConnectorLength, scale);
    out.end_connector_length	= font->em_scale (endConnectorLength, scale);
    out.full_advance		= font->em_scale (fullAdvance, scale);

465
    static_assert ((unsigned int) HB_MATH_GLYPH_PART_FLAG_EXTENDER ==
466
		   (unsigned int) PartFlags::Extender, "");
467

468
    out.flags = (hb_ot_math_glyph_part_flags_t)
469 470 471 472
		(unsigned int)
		(partFlags & PartFlags::Defined);
  }

473
  protected:
474
  GlyphID   glyph;		  /* Glyph ID for the part. */
B
Behdad Esfahbod 已提交
475
  HBUINT16    startConnectorLength; /* Advance width/ height of the straight bar
476 477 478
				   * connector material, in design units, is at
				   * the beginning of the glyph, in the
				   * direction of the extension. */
B
Behdad Esfahbod 已提交
479
  HBUINT16    endConnectorLength;   /* Advance width/ height of the straight bar
480 481 482
				   * connector material, in design units, is at
				   * the end of the glyph, in the direction of
				   * the extension. */
B
Behdad Esfahbod 已提交
483
  HBUINT16    fullAdvance;	  /* Full advance width/height for this part,
484 485 486 487
				   * in the direction of the extension.
				   * In design units. */
  PartFlags partFlags;		  /* Part qualifiers. */

488
  public:
B
Behdad Esfahbod 已提交
489
  DEFINE_SIZE_STATIC (10);
490 491
};

492
struct MathGlyphAssembly
493 494 495 496 497 498 499 500 501
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
		  italicsCorrection.sanitize(c, this) &&
		  partRecords.sanitize(c));
  }

502 503 504 505
  inline unsigned int get_parts (hb_direction_t direction,
				 hb_font_t *font,
				 unsigned int start_offset,
				 unsigned int *parts_count, /* IN/OUT */
506
				 hb_ot_math_glyph_part_t *parts /* OUT */,
507 508 509 510 511 512 513 514 515
				 hb_position_t *italics_correction /* OUT */) const
  {
    if (parts_count)
    {
      int scale = font->dir_scale (direction);
      const MathGlyphPartRecord *arr =
	    partRecords.sub_array (start_offset, parts_count);
      unsigned int count = *parts_count;
      for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
516
	arr[i].extract (parts[i], scale, font);
517 518 519 520 521 522 523
    }

    if (italics_correction)
      *italics_correction = italicsCorrection.get_x_value (font, this);

    return partRecords.len;
  }
524

525
  protected:
526
  MathValueRecord	   italicsCorrection; /* Italics correction of this
527
					       * MathGlyphAssembly. Should not
528
					       * depend on the assembly size. */
529
  ArrayOf<MathGlyphPartRecord> partRecords;   /* Array of part records, from
530 531 532
					       * left to right and bottom to
					       * top. */

533
  public:
B
Behdad Esfahbod 已提交
534
  DEFINE_SIZE_ARRAY (6, partRecords);
535 536 537 538 539 540 541 542 543 544 545 546
};

struct MathGlyphConstruction
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
		  glyphAssembly.sanitize(c, this) &&
		  mathGlyphVariantRecord.sanitize(c));
  }

547 548 549
  inline const MathGlyphAssembly &get_assembly (void) const
  { return this+glyphAssembly; }

550 551 552 553
  inline unsigned int get_variants (hb_direction_t direction,
				    hb_font_t *font,
				    unsigned int start_offset,
				    unsigned int *variants_count, /* IN/OUT */
554
				    hb_ot_math_glyph_variant_t *variants /* OUT */) const
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  {
    if (variants_count)
    {
      int scale = font->dir_scale (direction);
      const MathGlyphVariantRecord *arr =
	    mathGlyphVariantRecord.sub_array (start_offset, variants_count);
      unsigned int count = *variants_count;
      for (unsigned int i = 0; i < count; i++)
      {
	variants[i].glyph = arr[i].variantGlyph;
	variants[i].advance = font->em_scale (arr[i].advanceMeasurement, scale);
      }
    }
    return mathGlyphVariantRecord.len;
  }

  protected:
  /* Offset to MathGlyphAssembly table for this shape - from the beginning of
E
Ebrahim Byagowi 已提交
573
     MathGlyphConstruction table.  May be NULL. */
574
  OffsetTo<MathGlyphAssembly>	  glyphAssembly;
575 576 577 578

  /* MathGlyphVariantRecords for alternative variants of the glyphs. */
  ArrayOf<MathGlyphVariantRecord> mathGlyphVariantRecord;

579
  public:
B
Behdad Esfahbod 已提交
580
  DEFINE_SIZE_ARRAY (4, mathGlyphVariantRecord);
581 582 583 584 585 586 587 588 589
};

struct MathVariants
{
  inline bool sanitize_offsets (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    unsigned int count = vertGlyphCount + horizGlyphCount;
    for (unsigned int i = 0; i < count; i++)
590
      if (!glyphConstruction.arrayZ[i].sanitize (c, this)) return_trace (false);
591 592 593 594 595 596 597 598 599
    return_trace (true);
  }

  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (c->check_struct (this) &&
		  vertGlyphCoverage.sanitize (c, this) &&
		  horizGlyphCoverage.sanitize (c, this) &&
600
		  c->check_array (glyphConstruction.arrayZ, vertGlyphCount + horizGlyphCount) &&
601 602 603
		  sanitize_offsets (c));
  }

604 605 606
  inline hb_position_t get_min_connector_overlap (hb_direction_t direction,
						  hb_font_t *font) const
  { return font->em_scale_dir (minConnectorOverlap, direction); }
607

608 609 610 611 612
  inline unsigned int get_glyph_variants (hb_codepoint_t glyph,
					  hb_direction_t direction,
					  hb_font_t *font,
					  unsigned int start_offset,
					  unsigned int *variants_count, /* IN/OUT */
613
					  hb_ot_math_glyph_variant_t *variants /* OUT */) const
614 615 616
  { return get_glyph_construction (glyph, direction, font)
	   .get_variants (direction, font, start_offset, variants_count, variants); }

617 618 619 620 621
  inline unsigned int get_glyph_parts (hb_codepoint_t glyph,
				       hb_direction_t direction,
				       hb_font_t *font,
				       unsigned int start_offset,
				       unsigned int *parts_count, /* IN/OUT */
622
				       hb_ot_math_glyph_part_t *parts /* OUT */,
623 624 625 626 627 628 629
				       hb_position_t *italics_correction /* OUT */) const
  { return get_glyph_construction (glyph, direction, font)
	   .get_assembly ()
	   .get_parts (direction, font,
		       start_offset, parts_count, parts,
		       italics_correction); }

630 631 632 633 634 635 636 637 638 639
  private:
  inline const MathGlyphConstruction &
		get_glyph_construction (hb_codepoint_t glyph,
					hb_direction_t direction,
					hb_font_t *font) const
  {
    bool vertical = HB_DIRECTION_IS_VERTICAL (direction);
    unsigned int count = vertical ? vertGlyphCount : horizGlyphCount;
    const OffsetTo<Coverage> &coverage = vertical ? vertGlyphCoverage
						  : horizGlyphCoverage;
640

641 642
    unsigned int index = (this+coverage).get_coverage (glyph);
    if (unlikely (index >= count)) return Null(MathGlyphConstruction);
643

644
    if (!vertical)
645 646
      index += vertGlyphCount;

647
    return this+glyphConstruction[index];
648 649
  }

650
  protected:
B
Behdad Esfahbod 已提交
651
  HBUINT16	     minConnectorOverlap; /* Minimum overlap of connecting
652 653 654 655 656 657 658 659
					   * glyphs during glyph construction,
					   * in design units. */
  OffsetTo<Coverage> vertGlyphCoverage;   /* Offset to Coverage table -
					   * from the beginning of MathVariants
					   * table. */
  OffsetTo<Coverage> horizGlyphCoverage;  /* Offset to Coverage table -
					   * from the beginning of MathVariants
					   * table. */
B
Behdad Esfahbod 已提交
660
  HBUINT16	     vertGlyphCount;      /* Number of glyphs for which
661 662
					   * information is provided for
					   * vertically growing variants. */
B
Behdad Esfahbod 已提交
663
  HBUINT16	     horizGlyphCount;     /* Number of glyphs for which
664 665 666 667 668 669
					   * information is provided for
					   * horizontally growing variants. */

  /* Array of offsets to MathGlyphConstruction tables - from the beginning of
     the MathVariants table, for shapes growing in vertical/horizontal
     direction. */
670 671
  UnsizedArrayOf<OffsetTo<MathGlyphConstruction> >
 			glyphConstruction;
672

673
  public:
B
Behdad Esfahbod 已提交
674
  DEFINE_SIZE_ARRAY (10, glyphConstruction);
675 676
};

677

678
/*
679 680
 * MATH -- Mathematical typesetting
 * https://docs.microsoft.com/en-us/typography/opentype/spec/math
681 682 683 684
 */

struct MATH
{
685
  static const hb_tag_t tableTag	= HB_OT_TAG_MATH;
686

687 688
  inline bool has_data (void) const { return version.to_int () != 0; }

689 690 691 692
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
    return_trace (version.sanitize (c) &&
693 694
		  likely (version.major == 1) &&
		  mathConstants.sanitize (c, this) &&
695 696
		  mathGlyphInfo.sanitize (c, this) &&
		  mathVariants.sanitize (c, this));
697 698
  }

699
  inline hb_position_t get_constant (hb_ot_math_constant_t  constant,
700
				     hb_font_t		   *font) const
701
  { return (this+mathConstants).get_value (constant, font); }
702

B
Behdad Esfahbod 已提交
703 704
  inline const MathGlyphInfo &get_math_glyph_info (void) const
  { return this+mathGlyphInfo; }
705

B
Behdad Esfahbod 已提交
706 707
  inline const MathVariants &get_math_variants (void) const
  { return this+mathVariants; }
708

709
  protected:
710 711 712 713
  FixedVersion<>version;		/* Version of the MATH table
					 * initially set to 0x00010000u */
  OffsetTo<MathConstants> mathConstants;/* MathConstants table */
  OffsetTo<MathGlyphInfo> mathGlyphInfo;/* MathGlyphInfo table */
714
  OffsetTo<MathVariants>  mathVariants;	/* MathVariants table */
715

716
  public:
717
  DEFINE_SIZE_STATIC (10);
718 719
};

B
Typo  
Behdad Esfahbod 已提交
720
} /* namespace OT */
721 722


723
#endif /* HB_OT_MATH_TABLE_HH */