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
#include "hb-ot-layout-private.hh"
31

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) */
B
Behdad Esfahbod 已提交
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
      {
        if (index) *index = i;
        return true;
      }
    }
B
Behdad Esfahbod 已提交
102
    if (index) *index = Index::NOT_FOUND_INDEX;
103 104 105 106 107 108 109 110
    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
};


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


137 138 139 140 141
struct Script;
struct LangSys;
struct Feature;


B
Behdad Esfahbod 已提交
142 143
struct LangSys
{
144 145 146 147
  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 已提交
148 149 150 151
  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); }
152

B
Behdad Esfahbod 已提交
153
  inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
B
Behdad Esfahbod 已提交
154
  inline unsigned int get_required_feature_index (void) const
B
Behdad Esfahbod 已提交
155
  {
156
    if (reqFeatureIndex == 0xffff)
B
Behdad Esfahbod 已提交
157
      return Index::NOT_FOUND_INDEX;
158 159 160
   return reqFeatureIndex;;
  }

161
  inline bool sanitize (hb_sanitize_context_t *context) {
162
    TRACE_SANITIZE ();
163
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
164
	&& featureIndex.sanitize (context);
B
Behdad Esfahbod 已提交
165 166
  }

167 168 169 170 171
  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 */
172
  IndexArray	featureIndex;	/* Array of indices into the FeatureList */
B
Behdad Esfahbod 已提交
173
  public:
174
  DEFINE_SIZE_ARRAY (6, featureIndex);
175
};
B
Behdad Esfahbod 已提交
176
DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
177 178


B
Behdad Esfahbod 已提交
179 180
struct Script
{
181 182 183 184
  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 已提交
185 186 187 188
  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 已提交
189 190
  inline const LangSys& get_lang_sys (unsigned int i) const
  {
B
Behdad Esfahbod 已提交
191
    if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
192 193
    return this+langSys[i].offset;
  }
194 195
  inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
  { return langSys.find_index (tag, index); }
196

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

200
  inline bool sanitize (hb_sanitize_context_t *context) {
201
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
202 203
    return defaultLangSys.sanitize (context, this)
	&& langSys.sanitize (context, this);
B
Behdad Esfahbod 已提交
204 205
  }

206 207 208 209
  private:
  OffsetTo<LangSys>
		defaultLangSys;	/* Offset to DefaultLangSys table--from
				 * beginning of Script table--may be Null */
B
Behdad Esfahbod 已提交
210
  RecordArrayOf<LangSys>
211 212
		langSys;	/* Array of LangSysRecords--listed
				 * alphabetically by LangSysTag */
213
  public:
214
  DEFINE_SIZE_ARRAY (4, langSys);
215 216 217 218 219
};

typedef RecordListOf<Script> ScriptList;


B
Behdad Esfahbod 已提交
220 221
struct Feature
{
222 223 224 225
  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 已提交
226 227 228 229
  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); }
230

231
  inline bool sanitize (hb_sanitize_context_t *context) {
232
    TRACE_SANITIZE ();
233
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
234
	&& lookupIndex.sanitize (context);
B
Behdad Esfahbod 已提交
235 236
  }

B
Behdad Esfahbod 已提交
237 238
  /* LONGTERMTODO: implement get_feature_parameters() */
  /* LONGTERMTODO: implement FeatureSize and other special features? */
239 240 241 242
  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 */
243
  IndexArray	 lookupIndex;	/* Array of LookupList indices */
244
  public:
245
  DEFINE_SIZE_ARRAY (4, lookupIndex);
246 247 248 249 250
};

typedef RecordListOf<Feature> FeatureList;


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

B
Behdad Esfahbod 已提交
267 268 269
struct Lookup
{
  inline unsigned int get_subtable_count (void) const { return subTable.len; }
270 271

  inline unsigned int get_type (void) const { return lookupType; }
272 273 274
  inline unsigned int get_flag (void) const
  {
    unsigned int flag = lookupFlag;
275
    if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
276
    {
277
      const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
B
Behdad Esfahbod 已提交
278
      flag += (markFilteringSet << 16);
279 280 281
    }
    return flag;
  }
282

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

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

typedef OffsetListOf<Lookup> LookupList;


/*
 * Coverage Table
 */

B
Behdad Esfahbod 已提交
314 315
struct CoverageFormat1
{
316 317 318
  friend struct Coverage;

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

333
  inline bool sanitize (hb_sanitize_context_t *context) {
334
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
335
    return glyphArray.sanitize (context);
B
Behdad Esfahbod 已提交
336 337 338
  }

  private:
339 340 341
  USHORT	coverageFormat;	/* Format identifier--format = 1 */
  ArrayOf<GlyphID>
		glyphArray;	/* Array of GlyphIDs--in numerical order */
342
  public:
343
  DEFINE_SIZE_ARRAY (4, glyphArray);
344 345
};

B
Behdad Esfahbod 已提交
346 347
struct CoverageRangeRecord
{
348 349 350
  friend struct CoverageFormat2;

  private:
B
Behdad Esfahbod 已提交
351 352
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
353
    if (glyph_id >= start && glyph_id <= end)
354
      return (unsigned int) startCoverageIndex + (glyph_id - start);
355 356 357
    return NOT_COVERED;
  }

B
Behdad Esfahbod 已提交
358
  public:
359
  inline bool sanitize (hb_sanitize_context_t *context) {
360
    TRACE_SANITIZE ();
361
    return context->check_struct (this);
B
Behdad Esfahbod 已提交
362 363
  }

364 365 366 367 368
  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 已提交
369 370
  public:
  DEFINE_SIZE_STATIC (6);
371
};
B
Behdad Esfahbod 已提交
372
DEFINE_NULL_DATA (CoverageRangeRecord, "\000\001");
373

B
Behdad Esfahbod 已提交
374 375
struct CoverageFormat2
{
376 377 378
  friend struct Coverage;

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

392
  inline bool sanitize (hb_sanitize_context_t *context) {
393
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
394
    return rangeRecord.sanitize (context);
B
Behdad Esfahbod 已提交
395 396 397
  }

  private:
398 399 400 401 402
  USHORT	coverageFormat;	/* Format identifier--format = 2 */
  ArrayOf<CoverageRangeRecord>
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID. rangeCount entries
				 * long */
403
  public:
404
  DEFINE_SIZE_ARRAY (4, rangeRecord);
405 406
};

B
Behdad Esfahbod 已提交
407 408
struct Coverage
{
B
Behdad Esfahbod 已提交
409
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
B
Behdad Esfahbod 已提交
410

B
Behdad Esfahbod 已提交
411
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
412
  {
413
    switch (u.format) {
B
Behdad Esfahbod 已提交
414 415
    case 1: return u.format1.get_coverage(glyph_id);
    case 2: return u.format2.get_coverage(glyph_id);
416 417 418 419
    default:return NOT_COVERED;
    }
  }

420
  inline bool sanitize (hb_sanitize_context_t *context) {
421
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
422
    if (!u.format.sanitize (context)) return false;
B
Behdad Esfahbod 已提交
423
    switch (u.format) {
B
Behdad Esfahbod 已提交
424 425
    case 1: return u.format1.sanitize (context);
    case 2: return u.format2.sanitize (context);
B
Behdad Esfahbod 已提交
426 427 428
    default:return true;
    }
  }
429 430 431 432

  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
433 434
  CoverageFormat1	format1;
  CoverageFormat2	format2;
435
  } u;
B
Behdad Esfahbod 已提交
436
  public:
B
Behdad Esfahbod 已提交
437
  DEFINE_SIZE_UNION (2, format);
438 439 440 441 442 443 444
};


/*
 * Class Definition Table
 */

B
Behdad Esfahbod 已提交
445 446
struct ClassDefFormat1
{
447 448 449
  friend struct ClassDef;

  private:
B
Behdad Esfahbod 已提交
450 451
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
  {
452 453 454 455 456
    if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
      return classValue[glyph_id - startGlyph];
    return 0;
  }

457
  inline bool sanitize (hb_sanitize_context_t *context) {
458
    TRACE_SANITIZE ();
459
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
460
	&& classValue.sanitize (context);
B
Behdad Esfahbod 已提交
461 462
  }

463 464 465 466
  USHORT	classFormat;		/* Format identifier--format = 1 */
  GlyphID	startGlyph;		/* First GlyphID of the classValueArray */
  ArrayOf<USHORT>
		classValue;		/* Array of Class Values--one per GlyphID */
467
  public:
468
  DEFINE_SIZE_ARRAY (6, classValue);
469 470
};

B
Behdad Esfahbod 已提交
471 472
struct ClassRangeRecord
{
473 474 475
  friend struct ClassDefFormat2;

  private:
B
Behdad Esfahbod 已提交
476 477
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
  {
478 479 480 481 482
    if (glyph_id >= start && glyph_id <= end)
      return classValue;
    return 0;
  }

B
Behdad Esfahbod 已提交
483
  public:
484
  inline bool sanitize (hb_sanitize_context_t *context) {
485
    TRACE_SANITIZE ();
486
    return context->check_struct (this);
B
Behdad Esfahbod 已提交
487 488
  }

489 490 491 492
  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 已提交
493 494
  public:
  DEFINE_SIZE_STATIC (6);
495
};
B
Behdad Esfahbod 已提交
496
DEFINE_NULL_DATA (ClassRangeRecord, "\000\001");
497

B
Behdad Esfahbod 已提交
498 499
struct ClassDefFormat2
{
500 501 502
  friend struct ClassDef;

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

516
  inline bool sanitize (hb_sanitize_context_t *context) {
517
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
518
    return rangeRecord.sanitize (context);
B
Behdad Esfahbod 已提交
519 520
  }

521 522 523 524
  USHORT	classFormat;	/* Format identifier--format = 2 */
  ArrayOf<ClassRangeRecord>
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID */
525
  public:
526
  DEFINE_SIZE_ARRAY (4, rangeRecord);
527 528
};

B
Behdad Esfahbod 已提交
529 530
struct ClassDef
{
B
Behdad Esfahbod 已提交
531
  inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
B
Behdad Esfahbod 已提交
532

B
Behdad Esfahbod 已提交
533
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
534
  {
535
    switch (u.format) {
B
Behdad Esfahbod 已提交
536 537
    case 1: return u.format1.get_class(glyph_id);
    case 2: return u.format2.get_class(glyph_id);
538 539 540 541
    default:return 0;
    }
  }

542
  inline bool sanitize (hb_sanitize_context_t *context) {
543
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
544
    if (!u.format.sanitize (context)) return false;
B
Behdad Esfahbod 已提交
545
    switch (u.format) {
B
Behdad Esfahbod 已提交
546 547
    case 1: return u.format1.sanitize (context);
    case 2: return u.format2.sanitize (context);
B
Behdad Esfahbod 已提交
548 549 550
    default:return true;
    }
  }
551

552 553 554
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
555 556
  ClassDefFormat1	format1;
  ClassDefFormat2	format2;
557
  } u;
B
Behdad Esfahbod 已提交
558
  public:
B
Behdad Esfahbod 已提交
559
  DEFINE_SIZE_UNION (2, format);
560 561 562 563 564 565 566
};


/*
 * Device Tables
 */

B
Behdad Esfahbod 已提交
567 568
struct Device
{
B
Behdad Esfahbod 已提交
569
  inline int operator () (unsigned int ppem_size) const { return get_delta (ppem_size); }
B
Behdad Esfahbod 已提交
570

B
Behdad Esfahbod 已提交
571
  inline int get_delta (unsigned int ppem_size) const
B
Behdad Esfahbod 已提交
572
  {
573
    unsigned int f = deltaFormat;
574
    if (unlikely (f < 1 || f > 3))
575
      return 0;
576

577 578
    if (ppem_size < startSize || ppem_size > endSize)
      return 0;
579

580
    unsigned int s = ppem_size - startSize;
581

582
    unsigned int byte = deltaValue[s >> (4 - f)];
B
Behdad Esfahbod 已提交
583 584
    unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
    unsigned int mask = (0xFFFF >> (16 - (1 << f)));
585 586 587

    int delta = bits & mask;

B
Behdad Esfahbod 已提交
588
    if ((unsigned int) delta >= ((mask + 1) >> 1))
589 590 591 592 593
      delta -= mask + 1;

    return delta;
  }

B
Behdad Esfahbod 已提交
594 595 596
  inline unsigned int get_size () const
  {
    unsigned int f = deltaFormat;
B
Behdad Esfahbod 已提交
597 598
    if (unlikely (f < 1 || f > 3 || startSize > endSize)) return 3 * USHORT::static_size;
    return USHORT::static_size * (4 + ((endSize - startSize) >> (4 - f)));
B
Behdad Esfahbod 已提交
599 600
  }

601
  inline bool sanitize (hb_sanitize_context_t *context) {
602
    TRACE_SANITIZE ();
603
    return context->check_struct (this)
B
Behdad Esfahbod 已提交
604
	&& context->check_range (this, this->get_size ());
B
Behdad Esfahbod 已提交
605
  }
606 607

  private:
B
Behdad Esfahbod 已提交
608 609
  USHORT	startSize;		/* Smallest size to correct--in ppem */
  USHORT	endSize;		/* Largest size to correct--in ppem */
610 611 612 613 614
  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 已提交
615
  USHORT	deltaValue[VAR];	/* Array of compressed data */
B
Behdad Esfahbod 已提交
616
  public:
617
  DEFINE_SIZE_ARRAY (6, deltaValue);
618 619 620
};


621
#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */