hb-ot-layout-common-private.hh 16.7 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2007,2008,2009  Red Hat, Inc.
 *
B
Behdad Esfahbod 已提交
4
 *  This is part of HarfBuzz, a text shaping library.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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.
 *
 * Red Hat Author(s): Behdad Esfahbod
 */

27 28
#ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH
#define HB_OT_LAYOUT_COMMON_PRIVATE_HH
29

30 31
#include "hb-ot-layout-private.h"

32
#include "hb-open-type-private.hh"
33 34


B
Behdad Esfahbod 已提交
35 36 37 38 39
#define NO_CONTEXT		((unsigned int) 0x110000)
#define NOT_COVERED		((unsigned int) 0x110000)
#define MAX_NESTING_LEVEL	8


40 41 42 43 44 45
/*
 *
 * OpenType Layout Common Table Formats
 *
 */

46

47 48 49 50 51
/*
 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
 */

template <typename Type>
B
Behdad Esfahbod 已提交
52 53
struct Record
{
54
  inline bool sanitize (hb_sanitize_context_t *context, void *base) {
55
    TRACE_SANITIZE ();
56
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
57
	&& offset.sanitize (context, base);
B
Behdad Esfahbod 已提交
58 59
  }

60 61 62 63
  Tag		tag;		/* 4-byte Tag identifier */
  OffsetTo<Type>
		offset;		/* Offset from beginning of object holding
				 * the Record */
B
Behdad Esfahbod 已提交
64 65
  public:
  DEFINE_SIZE_STATIC (6);
66 67 68
};

template <typename Type>
69
struct RecordArrayOf : ArrayOf<Record<Type> > {
B
Behdad Esfahbod 已提交
70 71
  inline const Tag& get_tag (unsigned int i) const
  {
72
    if (unlikely (i >= this->len)) return Null(Tag);
B
Behdad Esfahbod 已提交
73
    return (*this)[i].tag;
74
  }
B
Behdad Esfahbod 已提交
75 76 77
  inline unsigned int get_tags (unsigned int start_offset,
				unsigned int *record_count /* IN/OUT */,
				hb_tag_t     *record_tags /* OUT */) const
78
  {
B
Behdad Esfahbod 已提交
79
    if (record_count) {
80
      const Record<Type> *array = this->sub_array (start_offset, record_count);
B
Behdad Esfahbod 已提交
81
      unsigned int count = *record_count;
B
Behdad Esfahbod 已提交
82 83 84 85
      for (unsigned int i = 0; i < count; i++)
	record_tags[i] = array[i].tag;
    }
    return this->len;
86 87 88
  }
  inline bool find_index (hb_tag_t tag, unsigned int *index) const
  {
B
Behdad Esfahbod 已提交
89 90
    Tag t;
    t.set (tag);
B
Behdad Esfahbod 已提交
91
    /* TODO: bsearch (need to sort in sanitize) */
92
    const Record<Type> *a = this->array();
93 94 95
    unsigned int count = this->len;
    for (unsigned int i = 0; i < count; i++)
    {
B
Behdad Esfahbod 已提交
96
      if (t == a[i].tag)
97 98 99 100 101 102 103 104 105 106 107 108 109 110
      {
        if (index) *index = i;
        return true;
      }
    }
    if (index) *index = NO_INDEX;
    return false;
  }
};

template <typename Type>
struct RecordListOf : RecordArrayOf<Type>
{
  inline const Type& operator [] (unsigned int i) const
B
Behdad Esfahbod 已提交
111
  { return this+RecordArrayOf<Type>::operator [](i).offset; }
B
Behdad Esfahbod 已提交
112

113
  inline bool sanitize (hb_sanitize_context_t *context) {
114
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
115
    return RecordArrayOf<Type>::sanitize (context, this);
B
Behdad Esfahbod 已提交
116
  }
117 118 119
};


120 121 122 123
struct IndexArray : ArrayOf<USHORT>
{
  inline unsigned int operator [] (unsigned int i) const
  {
124
    if (unlikely (i >= this->len))
125
      return NO_INDEX;
126
    return this->array()[i];
127
  }
B
Behdad Esfahbod 已提交
128 129 130
  inline unsigned int get_indexes (unsigned int start_offset,
				   unsigned int *_count /* IN/OUT */,
				   unsigned int *_indexes /* OUT */) const
131
  {
B
Behdad Esfahbod 已提交
132
    if (_count) {
133
      const USHORT *array = this->sub_array (start_offset, _count);
B
Behdad Esfahbod 已提交
134
      unsigned int count = *_count;
B
Behdad Esfahbod 已提交
135 136 137 138
      for (unsigned int i = 0; i < count; i++)
	_indexes[i] = array[i];
    }
    return this->len;
139 140 141 142
  }
};


143 144 145 146 147
struct Script;
struct LangSys;
struct Feature;


B
Behdad Esfahbod 已提交
148 149
struct LangSys
{
150 151 152 153
  inline unsigned int get_feature_count (void) const
  { return featureIndex.len; }
  inline hb_tag_t get_feature_index (unsigned int i) const
  { return featureIndex[i]; }
B
Behdad Esfahbod 已提交
154 155 156 157
  inline unsigned int get_feature_indexes (unsigned int start_offset,
					   unsigned int *feature_count /* IN/OUT */,
					   unsigned int *feature_indexes /* OUT */) const
  { return featureIndex.get_indexes (start_offset, feature_count, feature_indexes); }
158

B
Behdad Esfahbod 已提交
159
  inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
B
Behdad Esfahbod 已提交
160 161
  inline int get_required_feature_index (void) const
  {
162 163 164 165 166
    if (reqFeatureIndex == 0xffff)
      return NO_INDEX;
   return reqFeatureIndex;;
  }

167
  inline bool sanitize (hb_sanitize_context_t *context) {
168
    TRACE_SANITIZE ();
169
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
170
	&& featureIndex.sanitize (context);
B
Behdad Esfahbod 已提交
171 172
  }

173 174 175 176 177
  Offset	lookupOrder;	/* = Null (reserved for an offset to a
				 * reordering table) */
  USHORT	reqFeatureIndex;/* Index of a feature required for this
				 * language system--if no required features
				 * = 0xFFFF */
178
  IndexArray	featureIndex;	/* Array of indices into the FeatureList */
B
Behdad Esfahbod 已提交
179
  public:
B
Behdad Esfahbod 已提交
180
  DEFINE_SIZE_VAR (6, USHORT);
181
};
B
Behdad Esfahbod 已提交
182
DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
183 184


B
Behdad Esfahbod 已提交
185 186
struct Script
{
187 188 189 190
  inline unsigned int get_lang_sys_count (void) const
  { return langSys.len; }
  inline const Tag& get_lang_sys_tag (unsigned int i) const
  { return langSys.get_tag (i); }
B
Behdad Esfahbod 已提交
191 192 193 194
  inline unsigned int get_lang_sys_tags (unsigned int start_offset,
					 unsigned int *lang_sys_count /* IN/OUT */,
					 hb_tag_t     *lang_sys_tags /* OUT */) const
  { return langSys.get_tags (start_offset, lang_sys_count, lang_sys_tags); }
B
Behdad Esfahbod 已提交
195 196
  inline const LangSys& get_lang_sys (unsigned int i) const
  {
197 198 199
    if (i == NO_INDEX) return get_default_lang_sys ();
    return this+langSys[i].offset;
  }
200 201
  inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
  { return langSys.find_index (tag, index); }
202

B
Behdad Esfahbod 已提交
203
  inline bool has_default_lang_sys (void) const { return defaultLangSys != 0; }
B
Behdad Esfahbod 已提交
204
  inline const LangSys& get_default_lang_sys (void) const { return this+defaultLangSys; }
205

206
  inline bool sanitize (hb_sanitize_context_t *context) {
207
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
208 209
    return defaultLangSys.sanitize (context, this)
	&& langSys.sanitize (context, this);
B
Behdad Esfahbod 已提交
210 211
  }

212 213 214 215
  private:
  OffsetTo<LangSys>
		defaultLangSys;	/* Offset to DefaultLangSys table--from
				 * beginning of Script table--may be Null */
B
Behdad Esfahbod 已提交
216
  RecordArrayOf<LangSys>
217 218
		langSys;	/* Array of LangSysRecords--listed
				 * alphabetically by LangSysTag */
219
  public:
B
Behdad Esfahbod 已提交
220
  DEFINE_SIZE_VAR (4, Record<LangSys>);
221 222 223 224 225
};

typedef RecordListOf<Script> ScriptList;


B
Behdad Esfahbod 已提交
226 227
struct Feature
{
228 229 230 231
  inline unsigned int get_lookup_count (void) const
  { return lookupIndex.len; }
  inline hb_tag_t get_lookup_index (unsigned int i) const
  { return lookupIndex[i]; }
B
Behdad Esfahbod 已提交
232 233 234 235
  inline unsigned int get_lookup_indexes (unsigned int start_index,
					  unsigned int *lookup_count /* IN/OUT */,
					  unsigned int *lookup_tags /* OUT */) const
  { return lookupIndex.get_indexes (start_index, lookup_count, lookup_tags); }
236

237
  inline bool sanitize (hb_sanitize_context_t *context) {
238
    TRACE_SANITIZE ();
239
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
240
	&& lookupIndex.sanitize (context);
B
Behdad Esfahbod 已提交
241 242
  }

B
Behdad Esfahbod 已提交
243 244
  /* LONGTERMTODO: implement get_feature_parameters() */
  /* LONGTERMTODO: implement FeatureSize and other special features? */
245 246 247 248
  Offset	featureParams;	/* Offset to Feature Parameters table (if one
				 * has been defined for the feature), relative
				 * to the beginning of the Feature Table; = Null
				 * if not required */
249
  IndexArray	 lookupIndex;	/* Array of LookupList indices */
250
  public:
B
Behdad Esfahbod 已提交
251
  DEFINE_SIZE_VAR (4, USHORT);
252 253 254 255 256
};

typedef RecordListOf<Feature> FeatureList;


B
Behdad Esfahbod 已提交
257 258
struct LookupFlag : USHORT
{
B
Behdad Esfahbod 已提交
259 260 261 262 263
  enum {
    RightToLeft		= 0x0001u,
    IgnoreBaseGlyphs	= 0x0002u,
    IgnoreLigatures	= 0x0004u,
    IgnoreMarks		= 0x0008u,
B
Behdad Esfahbod 已提交
264
    IgnoreFlags		= 0x000Eu,
265 266
    UseMarkFilteringSet	= 0x0010u,
    Reserved		= 0x00E0u,
267
    MarkAttachmentType	= 0xFF00u
B
Behdad Esfahbod 已提交
268
  };
269 270
  public:
  DEFINE_SIZE_STATIC (2);
271 272
};

B
Behdad Esfahbod 已提交
273 274 275
struct Lookup
{
  inline unsigned int get_subtable_count (void) const { return subTable.len; }
276 277

  inline unsigned int get_type (void) const { return lookupType; }
278 279 280
  inline unsigned int get_flag (void) const
  {
    unsigned int flag = lookupFlag;
281
    if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
282
    {
283
      const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
B
Behdad Esfahbod 已提交
284
      flag += (markFilteringSet << 16);
285 286 287
    }
    return flag;
  }
288

289
  inline bool sanitize (hb_sanitize_context_t *context) {
290
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
291
    /* Real sanitize of the subtables is done by GSUB/GPOS/... */
292
    if (!(context->check_struct (this)
B
Behdad Esfahbod 已提交
293
       && likely (subTable.sanitize (context)))) return false;
294
    if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
B
Behdad Esfahbod 已提交
295
    {
296
      USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
B
Behdad Esfahbod 已提交
297
      if (!markFilteringSet.sanitize (context)) return false;
B
Behdad Esfahbod 已提交
298 299 300 301
    }
    return true;
  }

302 303
  USHORT	lookupType;		/* Different enumerations for GSUB and GPOS */
  USHORT	lookupFlag;		/* Lookup qualifiers */
B
Behdad Esfahbod 已提交
304
  ArrayOf<Offset>
305
		subTable;		/* Array of SubTables */
B
Behdad Esfahbod 已提交
306
  USHORT	markFilteringSetX[VAR];	/* Index (base 0) into GDEF mark glyph sets
307 308
					 * structure. This field is only present if bit
					 * UseMarkFilteringSet of lookup flags is set. */
B
Behdad Esfahbod 已提交
309
  public:
B
Behdad Esfahbod 已提交
310
  DEFINE_SIZE_VAR2 (6, Offset, USHORT);
311 312 313 314 315 316 317 318 319
};

typedef OffsetListOf<Lookup> LookupList;


/*
 * Coverage Table
 */

B
Behdad Esfahbod 已提交
320 321
struct CoverageFormat1
{
322 323 324
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
325 326
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
327
    if (unlikely (glyph_id > 0xFFFF))
328
      return NOT_COVERED;
329
    GlyphID gid;
B
Behdad Esfahbod 已提交
330
    gid.set (glyph_id);
B
Behdad Esfahbod 已提交
331
    /* TODO: bsearch (need to sort in sanitize) */
332 333 334 335 336 337 338
    unsigned int num_glyphs = glyphArray.len;
    for (unsigned int i = 0; i < num_glyphs; i++)
      if (gid == glyphArray[i])
        return i;
    return NOT_COVERED;
  }

339
  inline bool sanitize (hb_sanitize_context_t *context) {
340
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
341
    return glyphArray.sanitize (context);
B
Behdad Esfahbod 已提交
342 343 344
  }

  private:
345 346 347
  USHORT	coverageFormat;	/* Format identifier--format = 1 */
  ArrayOf<GlyphID>
		glyphArray;	/* Array of GlyphIDs--in numerical order */
348
  public:
B
Behdad Esfahbod 已提交
349
  DEFINE_SIZE_VAR (4, GlyphID);
350 351
};

B
Behdad Esfahbod 已提交
352 353
struct CoverageRangeRecord
{
354 355 356
  friend struct CoverageFormat2;

  private:
B
Behdad Esfahbod 已提交
357 358
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
359
    if (glyph_id >= start && glyph_id <= end)
360
      return (unsigned int) startCoverageIndex + (glyph_id - start);
361 362 363
    return NOT_COVERED;
  }

B
Behdad Esfahbod 已提交
364
  public:
365
  inline bool sanitize (hb_sanitize_context_t *context) {
366
    TRACE_SANITIZE ();
367
    return context->check_struct (this);
B
Behdad Esfahbod 已提交
368 369
  }

370 371 372 373 374
  private:
  GlyphID	start;			/* First GlyphID in the range */
  GlyphID	end;			/* Last GlyphID in the range */
  USHORT	startCoverageIndex;	/* Coverage Index of first GlyphID in
					 * range */
B
Behdad Esfahbod 已提交
375 376
  public:
  DEFINE_SIZE_STATIC (6);
377
};
B
Behdad Esfahbod 已提交
378
DEFINE_NULL_DATA (CoverageRangeRecord, "\000\001");
379

B
Behdad Esfahbod 已提交
380 381
struct CoverageFormat2
{
382 383 384
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
385 386
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
387
    /* TODO: bsearch (need to sort in sanitize) */
388
    unsigned int count = rangeRecord.len;
B
Behdad Esfahbod 已提交
389 390
    for (unsigned int i = 0; i < count; i++)
    {
391 392
      unsigned int coverage = rangeRecord[i].get_coverage (glyph_id);
      if (coverage != NOT_COVERED)
393 394 395 396 397
        return coverage;
    }
    return NOT_COVERED;
  }

398
  inline bool sanitize (hb_sanitize_context_t *context) {
399
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
400
    return rangeRecord.sanitize (context);
B
Behdad Esfahbod 已提交
401 402 403
  }

  private:
404 405 406 407 408
  USHORT	coverageFormat;	/* Format identifier--format = 2 */
  ArrayOf<CoverageRangeRecord>
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID. rangeCount entries
				 * long */
409
  public:
B
Behdad Esfahbod 已提交
410
  DEFINE_SIZE_VAR (4, CoverageRangeRecord);
411 412
};

B
Behdad Esfahbod 已提交
413 414
struct Coverage
{
B
Behdad Esfahbod 已提交
415
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
B
Behdad Esfahbod 已提交
416

B
Behdad Esfahbod 已提交
417
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
418
  {
419 420 421 422 423 424 425
    switch (u.format) {
    case 1: return u.format1->get_coverage(glyph_id);
    case 2: return u.format2->get_coverage(glyph_id);
    default:return NOT_COVERED;
    }
  }

426
  inline bool sanitize (hb_sanitize_context_t *context) {
427
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
428
    if (!u.format.sanitize (context)) return false;
B
Behdad Esfahbod 已提交
429
    switch (u.format) {
430 431
    case 1: return u.format1->sanitize (context);
    case 2: return u.format2->sanitize (context);
B
Behdad Esfahbod 已提交
432 433 434
    default:return true;
    }
  }
435 436 437 438

  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
439 440
  CoverageFormat1	format1[VAR];
  CoverageFormat2	format2[VAR];
441
  } u;
B
Behdad Esfahbod 已提交
442 443
  public:
  DEFINE_SIZE_MIN (2);
444 445 446 447 448 449 450
};


/*
 * Class Definition Table
 */

B
Behdad Esfahbod 已提交
451 452
struct ClassDefFormat1
{
453 454 455
  friend struct ClassDef;

  private:
B
Behdad Esfahbod 已提交
456 457
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
  {
458 459 460 461 462
    if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
      return classValue[glyph_id - startGlyph];
    return 0;
  }

463
  inline bool sanitize (hb_sanitize_context_t *context) {
464
    TRACE_SANITIZE ();
465
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
466
	&& classValue.sanitize (context);
B
Behdad Esfahbod 已提交
467 468
  }

469 470 471 472
  USHORT	classFormat;		/* Format identifier--format = 1 */
  GlyphID	startGlyph;		/* First GlyphID of the classValueArray */
  ArrayOf<USHORT>
		classValue;		/* Array of Class Values--one per GlyphID */
473
  public:
B
Behdad Esfahbod 已提交
474
  DEFINE_SIZE_VAR (6, USHORT);
475 476
};

B
Behdad Esfahbod 已提交
477 478
struct ClassRangeRecord
{
479 480 481
  friend struct ClassDefFormat2;

  private:
B
Behdad Esfahbod 已提交
482 483
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
  {
484 485 486 487 488
    if (glyph_id >= start && glyph_id <= end)
      return classValue;
    return 0;
  }

B
Behdad Esfahbod 已提交
489
  public:
490
  inline bool sanitize (hb_sanitize_context_t *context) {
491
    TRACE_SANITIZE ();
492
    return context->check_struct (this);
B
Behdad Esfahbod 已提交
493 494
  }

495 496 497 498
  private:
  GlyphID	start;		/* First GlyphID in the range */
  GlyphID	end;		/* Last GlyphID in the range */
  USHORT	classValue;	/* Applied to all glyphs in the range */
B
Behdad Esfahbod 已提交
499 500
  public:
  DEFINE_SIZE_STATIC (6);
501
};
B
Behdad Esfahbod 已提交
502
DEFINE_NULL_DATA (ClassRangeRecord, "\000\001");
503

B
Behdad Esfahbod 已提交
504 505
struct ClassDefFormat2
{
506 507 508
  friend struct ClassDef;

  private:
B
Behdad Esfahbod 已提交
509 510
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
511
    /* TODO: bsearch (need to sort in sanitize) */
512
    unsigned int count = rangeRecord.len;
B
Behdad Esfahbod 已提交
513 514
    for (unsigned int i = 0; i < count; i++)
    {
515 516 517 518 519 520 521
      int classValue = rangeRecord[i].get_class (glyph_id);
      if (classValue > 0)
        return classValue;
    }
    return 0;
  }

522
  inline bool sanitize (hb_sanitize_context_t *context) {
523
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
524
    return rangeRecord.sanitize (context);
B
Behdad Esfahbod 已提交
525 526
  }

527 528 529 530
  USHORT	classFormat;	/* Format identifier--format = 2 */
  ArrayOf<ClassRangeRecord>
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID */
531
  public:
B
Behdad Esfahbod 已提交
532
  DEFINE_SIZE_VAR (4, ClassRangeRecord);
533 534
};

B
Behdad Esfahbod 已提交
535 536
struct ClassDef
{
B
Behdad Esfahbod 已提交
537
  inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
B
Behdad Esfahbod 已提交
538

B
Behdad Esfahbod 已提交
539
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
540
  {
541 542 543 544 545 546 547
    switch (u.format) {
    case 1: return u.format1->get_class(glyph_id);
    case 2: return u.format2->get_class(glyph_id);
    default:return 0;
    }
  }

548
  inline bool sanitize (hb_sanitize_context_t *context) {
549
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
550
    if (!u.format.sanitize (context)) return false;
B
Behdad Esfahbod 已提交
551
    switch (u.format) {
552 553
    case 1: return u.format1->sanitize (context);
    case 2: return u.format2->sanitize (context);
B
Behdad Esfahbod 已提交
554 555 556
    default:return true;
    }
  }
557

558 559 560
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
561 562
  ClassDefFormat1	format1[VAR];
  ClassDefFormat2	format2[VAR];
563
  } u;
B
Behdad Esfahbod 已提交
564 565
  public:
  DEFINE_SIZE_MIN (2);
566 567 568 569 570 571 572
};


/*
 * Device Tables
 */

B
Behdad Esfahbod 已提交
573 574
struct Device
{
B
Behdad Esfahbod 已提交
575
  inline int operator () (unsigned int ppem_size) const { return get_delta (ppem_size); }
B
Behdad Esfahbod 已提交
576

B
Behdad Esfahbod 已提交
577
  inline int get_delta (unsigned int ppem_size) const
B
Behdad Esfahbod 已提交
578
  {
579
    unsigned int f = deltaFormat;
580
    if (unlikely (f < 1 || f > 3))
581
      return 0;
582

583 584
    if (ppem_size < startSize || ppem_size > endSize)
      return 0;
585

586
    unsigned int s = ppem_size - startSize;
587

588
    unsigned int byte = deltaValue[s >> (4 - f)];
B
Behdad Esfahbod 已提交
589 590
    unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
    unsigned int mask = (0xFFFF >> (16 - (1 << f)));
591 592 593

    int delta = bits & mask;

B
Behdad Esfahbod 已提交
594
    if ((unsigned int) delta >= ((mask + 1) >> 1))
595 596 597 598 599
      delta -= mask + 1;

    return delta;
  }

B
Behdad Esfahbod 已提交
600 601 602
  inline unsigned int get_size () const
  {
    unsigned int f = deltaFormat;
603
    if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::get_size ();
604
    return USHORT::get_size () * (4 + ((endSize - startSize) >> (4 - f)));
B
Behdad Esfahbod 已提交
605 606
  }

607
  inline bool sanitize (hb_sanitize_context_t *context) {
608
    TRACE_SANITIZE ();
609
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
610
	&& context->check_range (this, this->get_size ());
B
Behdad Esfahbod 已提交
611
  }
612 613

  private:
B
Behdad Esfahbod 已提交
614 615
  USHORT	startSize;		/* Smallest size to correct--in ppem */
  USHORT	endSize;		/* Largest size to correct--in ppem */
616 617 618 619 620
  USHORT	deltaFormat;		/* Format of DeltaValue array data: 1, 2, or 3
					 * 1	Signed 2-bit value, 8 values per uint16
					 * 2	Signed 4-bit value, 4 values per uint16
					 * 3	Signed 8-bit value, 2 values per uint16
					 */
B
Behdad Esfahbod 已提交
621
  USHORT	deltaValue[VAR];	/* Array of compressed data */
B
Behdad Esfahbod 已提交
622 623
  public:
  DEFINE_SIZE_VAR (6, USHORT);
624 625 626
};


627
#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */