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

29 30
#ifndef HB_OT_LAYOUT_COMMON_PRIVATE_HH
#define HB_OT_LAYOUT_COMMON_PRIVATE_HH
31

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


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

B
Behdad Esfahbod 已提交
40

B
Behdad Esfahbod 已提交
41

42 43 44 45 46 47
/*
 *
 * OpenType Layout Common Table Formats
 *
 */

48

49 50 51 52 53
/*
 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
 */

template <typename Type>
B
Behdad Esfahbod 已提交
54 55
struct Record
{
56
  inline int cmp (hb_tag_t a) const {
57
    return tag.cmp (a);
B
Behdad Esfahbod 已提交
58 59
  }

B
Behdad Esfahbod 已提交
60
  inline bool sanitize (hb_sanitize_context_t *c, void *base) {
61
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
62 63
    return c->check_struct (this)
	&& offset.sanitize (c, base);
B
Behdad Esfahbod 已提交
64 65
  }

66 67 68 69
  Tag		tag;		/* 4-byte Tag identifier */
  OffsetTo<Type>
		offset;		/* Offset from beginning of object holding
				 * the Record */
B
Behdad Esfahbod 已提交
70 71
  public:
  DEFINE_SIZE_STATIC (6);
72 73 74
};

template <typename Type>
B
Behdad Esfahbod 已提交
75
struct RecordArrayOf : SortedArrayOf<Record<Type> > {
B
Behdad Esfahbod 已提交
76 77
  inline const Tag& get_tag (unsigned int i) const
  {
B
Behdad Esfahbod 已提交
78 79 80
    /* We cheat slightly and don't define separate Null objects
     * for Record types.  Instead, we return the correct Null(Tag)
     * here. */
81
    if (unlikely (i >= this->len)) return Null(Tag);
B
Behdad Esfahbod 已提交
82
    return (*this)[i].tag;
83
  }
B
Behdad Esfahbod 已提交
84 85 86
  inline unsigned int get_tags (unsigned int start_offset,
				unsigned int *record_count /* IN/OUT */,
				hb_tag_t     *record_tags /* OUT */) const
87
  {
B
Behdad Esfahbod 已提交
88
    if (record_count) {
B
Behdad Esfahbod 已提交
89
      const Record<Type> *arr = this->sub_array (start_offset, record_count);
B
Behdad Esfahbod 已提交
90
      unsigned int count = *record_count;
B
Behdad Esfahbod 已提交
91
      for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
92
	record_tags[i] = arr[i].tag;
B
Behdad Esfahbod 已提交
93 94
    }
    return this->len;
95 96 97
  }
  inline bool find_index (hb_tag_t tag, unsigned int *index) const
  {
B
Behdad Esfahbod 已提交
98 99
    int i = this->search (tag);
    if (i != -1) {
100 101
        if (index) *index = i;
        return true;
B
Behdad Esfahbod 已提交
102 103 104
    } else {
      if (index) *index = Index::NOT_FOUND_INDEX;
      return false;
105 106 107 108 109 110 111 112
    }
  }
};

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

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


B
Behdad Esfahbod 已提交
122 123 124 125 126 127 128 129 130 131 132 133
struct RangeRecord
{
  inline int cmp (hb_codepoint_t g) const {
    hb_codepoint_t a = start, b = end;
    return g < a ? -1 : g <= b ? 0 : +1 ;
  }

  inline bool sanitize (hb_sanitize_context_t *c) {
    TRACE_SANITIZE ();
    return c->check_struct (this);
  }

134
  inline bool intersects (const hb_set_t *glyphs) const {
135 136 137
    return glyphs->intersects (start, end);
  }

B
Behdad Esfahbod 已提交
138 139 140 141 142 143 144 145 146
  GlyphID	start;		/* First GlyphID in the range */
  GlyphID	end;		/* Last GlyphID in the range */
  USHORT	value;		/* Value */
  public:
  DEFINE_SIZE_STATIC (6);
};
DEFINE_NULL_DATA (RangeRecord, "\000\001");


B
Behdad Esfahbod 已提交
147
struct IndexArray : ArrayOf<Index>
148
{
B
Behdad Esfahbod 已提交
149 150 151
  inline unsigned int get_indexes (unsigned int start_offset,
				   unsigned int *_count /* IN/OUT */,
				   unsigned int *_indexes /* OUT */) const
152
  {
B
Behdad Esfahbod 已提交
153
    if (_count) {
B
Behdad Esfahbod 已提交
154
      const USHORT *arr = this->sub_array (start_offset, _count);
B
Behdad Esfahbod 已提交
155
      unsigned int count = *_count;
B
Behdad Esfahbod 已提交
156
      for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
157
	_indexes[i] = arr[i];
B
Behdad Esfahbod 已提交
158 159
    }
    return this->len;
160 161 162 163
  }
};


164 165 166 167 168
struct Script;
struct LangSys;
struct Feature;


B
Behdad Esfahbod 已提交
169 170
struct LangSys
{
171 172 173 174
  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 已提交
175 176 177 178
  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); }
179

B
Behdad Esfahbod 已提交
180
  inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
B
Behdad Esfahbod 已提交
181
  inline unsigned int get_required_feature_index (void) const
B
Behdad Esfahbod 已提交
182
  {
183
    if (reqFeatureIndex == 0xffff)
B
Behdad Esfahbod 已提交
184
      return Index::NOT_FOUND_INDEX;
185 186 187
   return reqFeatureIndex;;
  }

B
Behdad Esfahbod 已提交
188
  inline bool sanitize (hb_sanitize_context_t *c) {
189
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
190 191
    return c->check_struct (this)
	&& featureIndex.sanitize (c);
B
Behdad Esfahbod 已提交
192 193
  }

194 195 196 197 198
  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 */
199
  IndexArray	featureIndex;	/* Array of indices into the FeatureList */
B
Behdad Esfahbod 已提交
200
  public:
201
  DEFINE_SIZE_ARRAY (6, featureIndex);
202
};
B
Behdad Esfahbod 已提交
203
DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
204 205


B
Behdad Esfahbod 已提交
206 207
struct Script
{
208 209 210 211
  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 已提交
212 213 214 215
  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 已提交
216 217
  inline const LangSys& get_lang_sys (unsigned int i) const
  {
B
Behdad Esfahbod 已提交
218
    if (i == Index::NOT_FOUND_INDEX) return get_default_lang_sys ();
219 220
    return this+langSys[i].offset;
  }
221 222
  inline bool find_lang_sys_index (hb_tag_t tag, unsigned int *index) const
  { return langSys.find_index (tag, index); }
223

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

B
Behdad Esfahbod 已提交
227
  inline bool sanitize (hb_sanitize_context_t *c) {
228
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
229 230
    return defaultLangSys.sanitize (c, this)
	&& langSys.sanitize (c, this);
B
Behdad Esfahbod 已提交
231 232
  }

233 234 235 236
  private:
  OffsetTo<LangSys>
		defaultLangSys;	/* Offset to DefaultLangSys table--from
				 * beginning of Script table--may be Null */
B
Behdad Esfahbod 已提交
237
  RecordArrayOf<LangSys>
238 239
		langSys;	/* Array of LangSysRecords--listed
				 * alphabetically by LangSysTag */
240
  public:
241
  DEFINE_SIZE_ARRAY (4, langSys);
242 243 244 245 246
};

typedef RecordListOf<Script> ScriptList;


B
Behdad Esfahbod 已提交
247 248
struct Feature
{
249 250 251 252
  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 已提交
253 254 255 256
  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); }
257

B
Behdad Esfahbod 已提交
258
  inline bool sanitize (hb_sanitize_context_t *c) {
259
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
260 261
    return c->check_struct (this)
	&& lookupIndex.sanitize (c);
B
Behdad Esfahbod 已提交
262 263
  }

264 265 266 267
  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 */
268
  IndexArray	 lookupIndex;	/* Array of LookupList indices */
269
  public:
270
  DEFINE_SIZE_ARRAY (4, lookupIndex);
271 272 273 274 275
};

typedef RecordListOf<Feature> FeatureList;


B
Behdad Esfahbod 已提交
276 277
struct LookupFlag : USHORT
{
B
Behdad Esfahbod 已提交
278
  enum Flags {
B
Behdad Esfahbod 已提交
279 280 281 282
    RightToLeft		= 0x0001u,
    IgnoreBaseGlyphs	= 0x0002u,
    IgnoreLigatures	= 0x0004u,
    IgnoreMarks		= 0x0008u,
B
Behdad Esfahbod 已提交
283
    IgnoreFlags		= 0x000Eu,
284 285
    UseMarkFilteringSet	= 0x0010u,
    Reserved		= 0x00E0u,
286
    MarkAttachmentType	= 0xFF00u
B
Behdad Esfahbod 已提交
287
  };
288 289
  public:
  DEFINE_SIZE_STATIC (2);
290 291
};

B
Behdad Esfahbod 已提交
292 293 294
struct Lookup
{
  inline unsigned int get_subtable_count (void) const { return subTable.len; }
295 296

  inline unsigned int get_type (void) const { return lookupType; }
297 298 299 300 301

  /* lookup_props is a 32-bit integer where the lower 16-bit is LookupFlag and
   * higher 16-bit is mark-filtering-set if the lookup uses one.
   * Not to be confused with glyph_props which is very similar. */
  inline uint32_t get_props (void) const
302 303
  {
    unsigned int flag = lookupFlag;
304
    if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
305
    {
306
      const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
B
Behdad Esfahbod 已提交
307
      flag += (markFilteringSet << 16);
308 309 310
    }
    return flag;
  }
311

B
Behdad Esfahbod 已提交
312
  inline bool sanitize (hb_sanitize_context_t *c) {
313
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
314
    /* Real sanitize of the subtables is done by GSUB/GPOS/... */
B
Behdad Esfahbod 已提交
315 316
    if (!(c->check_struct (this)
       && subTable.sanitize (c))) return false;
317
    if (unlikely (lookupFlag & LookupFlag::UseMarkFilteringSet))
B
Behdad Esfahbod 已提交
318
    {
319
      USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
B
Behdad Esfahbod 已提交
320
      if (!markFilteringSet.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
321 322 323 324
    }
    return true;
  }

325 326
  USHORT	lookupType;		/* Different enumerations for GSUB and GPOS */
  USHORT	lookupFlag;		/* Lookup qualifiers */
B
Behdad Esfahbod 已提交
327
  ArrayOf<Offset>
328
		subTable;		/* Array of SubTables */
B
Behdad Esfahbod 已提交
329
  USHORT	markFilteringSetX[VAR];	/* Index (base 0) into GDEF mark glyph sets
330 331
					 * structure. This field is only present if bit
					 * UseMarkFilteringSet of lookup flags is set. */
B
Behdad Esfahbod 已提交
332
  public:
333
  DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
334 335 336 337 338 339 340 341 342
};

typedef OffsetListOf<Lookup> LookupList;


/*
 * Coverage Table
 */

B
Behdad Esfahbod 已提交
343 344
struct CoverageFormat1
{
345 346 347
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
348 349
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
350 351
    int i = glyphArray.search (glyph_id);
    if (i != -1)
352 353 354 355
        return i;
    return NOT_COVERED;
  }

B
Behdad Esfahbod 已提交
356
  inline bool sanitize (hb_sanitize_context_t *c) {
357
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
358
    return glyphArray.sanitize (c);
B
Behdad Esfahbod 已提交
359 360
  }

361
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
362 363 364
    return glyphs->has (glyphArray[index]);
  }

B
Behdad Esfahbod 已提交
365
  struct Iter {
B
Behdad Esfahbod 已提交
366 367 368 369 370
    inline void init (const struct CoverageFormat1 &c_) { c = &c_; i = 0; };
    inline bool more (void) { return i < c->glyphArray.len; }
    inline void next (void) { i++; }
    inline uint16_t get_glyph (void) { return c->glyphArray[i]; }
    inline uint16_t get_coverage (void) { return i; }
B
Behdad Esfahbod 已提交
371 372 373 374 375 376

    private:
    const struct CoverageFormat1 *c;
    unsigned int i;
  };

B
Behdad Esfahbod 已提交
377
  private:
378
  USHORT	coverageFormat;	/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
379
  SortedArrayOf<GlyphID>
380
		glyphArray;	/* Array of GlyphIDs--in numerical order */
381
  public:
382
  DEFINE_SIZE_ARRAY (4, glyphArray);
383 384
};

B
Behdad Esfahbod 已提交
385 386
struct CoverageFormat2
{
387 388 389
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
390 391
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
392 393 394 395
    int i = rangeRecord.search (glyph_id);
    if (i != -1) {
      const RangeRecord &range = rangeRecord[i];
      return (unsigned int) range.value + (glyph_id - range.start);
396 397 398 399
    }
    return NOT_COVERED;
  }

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

405
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
406 407 408 409 410 411 412 413 414 415 416 417 418 419
    unsigned int i;
    unsigned int count = rangeRecord.len;
    for (i = 0; i < count; i++) {
      const RangeRecord &range = rangeRecord[i];
      if (range.value <= index &&
	  index < range.value + (range.end - range.start) &&
	  range.intersects (glyphs))
        return true;
      else if (index < range.value)
        return false;
    }
    return false;
  }

B
Behdad Esfahbod 已提交
420
  struct Iter {
B
Behdad Esfahbod 已提交
421 422 423 424 425 426 427 428 429
    inline void init (const CoverageFormat2 &c_) {
      c = &c_;
      coverage = 0;
      i = 0;
      j = c->rangeRecord.len ? c_.rangeRecord[0].start : 0;
    }
    inline bool more (void) { return i < c->rangeRecord.len; }
    inline void next (void) {
      coverage++;
B
Behdad Esfahbod 已提交
430 431
      if (j == c->rangeRecord[i].end) {
        i++;
B
Behdad Esfahbod 已提交
432 433 434
	if (more ())
	  j = c->rangeRecord[i].start;
	return;
B
Behdad Esfahbod 已提交
435
      }
B
Behdad Esfahbod 已提交
436 437
      j++;
    }
B
Behdad Esfahbod 已提交
438 439
    inline uint16_t get_glyph (void) { return j; }
    inline uint16_t get_coverage (void) { return coverage; }
B
Behdad Esfahbod 已提交
440 441 442

    private:
    const struct CoverageFormat2 *c;
B
Behdad Esfahbod 已提交
443
    unsigned int i, j, coverage;
B
Behdad Esfahbod 已提交
444 445
  };

B
Behdad Esfahbod 已提交
446
  private:
447
  USHORT	coverageFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
448
  SortedArrayOf<RangeRecord>
449 450 451
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID. rangeCount entries
				 * long */
452
  public:
453
  DEFINE_SIZE_ARRAY (4, rangeRecord);
454 455
};

B
Behdad Esfahbod 已提交
456 457
struct Coverage
{
B
Behdad Esfahbod 已提交
458
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
B
Behdad Esfahbod 已提交
459

B
Behdad Esfahbod 已提交
460
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
461
  {
462
    switch (u.format) {
B
Behdad Esfahbod 已提交
463 464
    case 1: return u.format1.get_coverage(glyph_id);
    case 2: return u.format2.get_coverage(glyph_id);
465 466 467 468
    default:return NOT_COVERED;
    }
  }

B
Behdad Esfahbod 已提交
469
  inline bool sanitize (hb_sanitize_context_t *c) {
470
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
471
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
472
    switch (u.format) {
B
Behdad Esfahbod 已提交
473 474
    case 1: return u.format1.sanitize (c);
    case 2: return u.format2.sanitize (c);
B
Behdad Esfahbod 已提交
475 476 477
    default:return true;
    }
  }
478

479
  inline bool intersects (const hb_set_t *glyphs) const {
B
Behdad Esfahbod 已提交
480 481 482 483 484 485 486
    /* TODO speed this up */
    Coverage::Iter iter;
    for (iter.init (*this); iter.more (); iter.next ()) {
      if (glyphs->has (iter.get_glyph ()))
        return true;
    }
    return false;
487
  }
B
Behdad Esfahbod 已提交
488

489
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
490 491 492 493 494
    switch (u.format) {
    case 1: return u.format1.intersects_coverage (glyphs, index);
    case 2: return u.format2.intersects_coverage (glyphs, index);
    default:return false;
    }
B
Behdad Esfahbod 已提交
495 496
  }

B
Behdad Esfahbod 已提交
497
  struct Iter {
B
Behdad Esfahbod 已提交
498 499
    Iter (void) : format (0) {};
    inline void init (const Coverage &c_) {
B
Behdad Esfahbod 已提交
500 501 502 503 504 505 506
      format = c_.u.format;
      switch (format) {
      case 1: return u.format1.init (c_.u.format1);
      case 2: return u.format2.init (c_.u.format2);
      default:return;
      }
    }
B
Behdad Esfahbod 已提交
507
    inline bool more (void) {
B
Behdad Esfahbod 已提交
508 509 510 511 512 513
      switch (format) {
      case 1: return u.format1.more ();
      case 2: return u.format2.more ();
      default:return true;
      }
    }
B
Behdad Esfahbod 已提交
514
    inline void next (void) {
B
Behdad Esfahbod 已提交
515 516 517 518 519 520
      switch (format) {
      case 1: u.format1.next (); break;
      case 2: u.format2.next (); break;
      default:                   break;
      }
    }
B
Behdad Esfahbod 已提交
521 522 523 524 525 526 527 528
    inline uint16_t get_glyph (void) {
      switch (format) {
      case 1: return u.format1.get_glyph ();
      case 2: return u.format2.get_glyph ();
      default:return true;
      }
    }
    inline uint16_t get_coverage (void) {
B
Behdad Esfahbod 已提交
529
      switch (format) {
B
Behdad Esfahbod 已提交
530 531
      case 1: return u.format1.get_coverage ();
      case 2: return u.format2.get_coverage ();
B
Behdad Esfahbod 已提交
532 533 534 535 536
      default:return true;
      }
    }

    private:
B
Behdad Esfahbod 已提交
537
    unsigned int format;
B
Behdad Esfahbod 已提交
538 539 540 541 542 543
    union {
    CoverageFormat1::Iter	format1;
    CoverageFormat2::Iter	format2;
    } u;
  };

544 545 546
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
547 548
  CoverageFormat1	format1;
  CoverageFormat2	format2;
549
  } u;
B
Behdad Esfahbod 已提交
550
  public:
B
Behdad Esfahbod 已提交
551
  DEFINE_SIZE_UNION (2, format);
552 553 554 555 556 557 558
};


/*
 * Class Definition Table
 */

B
Behdad Esfahbod 已提交
559 560
struct ClassDefFormat1
{
561 562 563
  friend struct ClassDef;

  private:
564
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
565
  {
566 567 568 569 570
    if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
      return classValue[glyph_id - startGlyph];
    return 0;
  }

B
Behdad Esfahbod 已提交
571
  inline bool sanitize (hb_sanitize_context_t *c) {
572
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
573 574
    return c->check_struct (this)
	&& classValue.sanitize (c);
B
Behdad Esfahbod 已提交
575 576
  }

577
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
578 579 580 581 582 583 584
    unsigned int count = classValue.len;
    for (unsigned int i = 0; i < count; i++)
      if (classValue[i] == klass && glyphs->has (startGlyph + i))
        return true;
    return false;
  }

585 586 587 588
  USHORT	classFormat;		/* Format identifier--format = 1 */
  GlyphID	startGlyph;		/* First GlyphID of the classValueArray */
  ArrayOf<USHORT>
		classValue;		/* Array of Class Values--one per GlyphID */
589
  public:
590
  DEFINE_SIZE_ARRAY (6, classValue);
591 592
};

B
Behdad Esfahbod 已提交
593 594
struct ClassDefFormat2
{
595 596 597
  friend struct ClassDef;

  private:
598
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
599
  {
B
Behdad Esfahbod 已提交
600 601 602
    int i = rangeRecord.search (glyph_id);
    if (i != -1)
      return rangeRecord[i].value;
603 604 605
    return 0;
  }

B
Behdad Esfahbod 已提交
606
  inline bool sanitize (hb_sanitize_context_t *c) {
607
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
608
    return rangeRecord.sanitize (c);
B
Behdad Esfahbod 已提交
609 610
  }

611
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
612 613 614 615 616 617 618
    unsigned int count = rangeRecord.len;
    for (unsigned int i = 0; i < count; i++)
      if (rangeRecord[i].value == klass && rangeRecord[i].intersects (glyphs))
        return true;
    return false;
  }

619
  USHORT	classFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
620
  SortedArrayOf<RangeRecord>
621 622
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID */
623
  public:
624
  DEFINE_SIZE_ARRAY (4, rangeRecord);
625 626
};

B
Behdad Esfahbod 已提交
627 628
struct ClassDef
{
629
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
B
Behdad Esfahbod 已提交
630

631
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
632
  {
633
    switch (u.format) {
B
Behdad Esfahbod 已提交
634 635
    case 1: return u.format1.get_class(glyph_id);
    case 2: return u.format2.get_class(glyph_id);
636 637 638 639
    default:return 0;
    }
  }

B
Behdad Esfahbod 已提交
640
  inline bool sanitize (hb_sanitize_context_t *c) {
641
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
642
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
643
    switch (u.format) {
B
Behdad Esfahbod 已提交
644 645
    case 1: return u.format1.sanitize (c);
    case 2: return u.format2.sanitize (c);
B
Behdad Esfahbod 已提交
646 647 648
    default:return true;
    }
  }
649

650
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
651 652 653 654 655 656 657
    switch (u.format) {
    case 1: return u.format1.intersects_class (glyphs, klass);
    case 2: return u.format2.intersects_class (glyphs, klass);
    default:return false;
    }
  }

658 659 660
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
661 662
  ClassDefFormat1	format1;
  ClassDefFormat2	format2;
663
  } u;
B
Behdad Esfahbod 已提交
664
  public:
B
Behdad Esfahbod 已提交
665
  DEFINE_SIZE_UNION (2, format);
666 667 668 669 670 671 672
};


/*
 * Device Tables
 */

B
Behdad Esfahbod 已提交
673 674
struct Device
{
B
Behdad Esfahbod 已提交
675

676 677
  inline hb_position_t get_x_delta (hb_font_t *font) const
  { return get_delta (font->x_ppem, font->x_scale); }
B
Behdad Esfahbod 已提交
678

679 680
  inline hb_position_t get_y_delta (hb_font_t *font) const
  { return get_delta (font->y_ppem, font->y_scale); }
B
Behdad Esfahbod 已提交
681

682
  inline int get_delta (unsigned int ppem, int scale) const
683 684 685 686 687 688 689 690 691 692 693 694
  {
    if (!ppem) return 0;

    int pixels = get_delta_pixels (ppem);

    if (!pixels) return 0;

    return pixels * (int64_t) scale / ppem;
  }


  inline int get_delta_pixels (unsigned int ppem_size) const
B
Behdad Esfahbod 已提交
695
  {
696
    unsigned int f = deltaFormat;
697
    if (unlikely (f < 1 || f > 3))
698
      return 0;
699

700 701
    if (ppem_size < startSize || ppem_size > endSize)
      return 0;
702

703
    unsigned int s = ppem_size - startSize;
704

705
    unsigned int byte = deltaValue[s >> (4 - f)];
B
Behdad Esfahbod 已提交
706 707
    unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
    unsigned int mask = (0xFFFF >> (16 - (1 << f)));
708 709 710

    int delta = bits & mask;

B
Behdad Esfahbod 已提交
711
    if ((unsigned int) delta >= ((mask + 1) >> 1))
712 713 714 715 716
      delta -= mask + 1;

    return delta;
  }

B
Behdad Esfahbod 已提交
717
  inline unsigned int get_size (void) const
B
Behdad Esfahbod 已提交
718 719
  {
    unsigned int f = deltaFormat;
B
Behdad Esfahbod 已提交
720 721
    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 已提交
722 723
  }

B
Behdad Esfahbod 已提交
724
  inline bool sanitize (hb_sanitize_context_t *c) {
725
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
726 727
    return c->check_struct (this)
	&& c->check_range (this, this->get_size ());
B
Behdad Esfahbod 已提交
728
  }
729 730

  private:
B
Behdad Esfahbod 已提交
731 732
  USHORT	startSize;		/* Smallest size to correct--in ppem */
  USHORT	endSize;		/* Largest size to correct--in ppem */
733 734 735 736 737
  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 已提交
738
  USHORT	deltaValue[VAR];	/* Array of compressed data */
B
Behdad Esfahbod 已提交
739
  public:
740
  DEFINE_SIZE_ARRAY (6, deltaValue);
741 742 743
};


B
Behdad Esfahbod 已提交
744

745
#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */