hb-ot-layout-common-private.hh 20.1 KB
Newer Older
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2007,2008,2009  Red Hat, Inc.
B
Behdad Esfahbod 已提交
3
 * Copyright © 2010,2012  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"
B
Behdad Esfahbod 已提交
34
#include "hb-set-private.hh"
35 36


B
Behdad Esfahbod 已提交
37 38 39
#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 ();
62
    return TRACE_RETURN (c->check_struct (this) && offset.sanitize (c, base));
B
Behdad Esfahbod 已提交
63 64
  }

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

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

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

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


B
Behdad Esfahbod 已提交
121 122 123 124 125 126 127 128 129
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 ();
130
    return TRACE_RETURN (c->check_struct (this));
B
Behdad Esfahbod 已提交
131 132
  }

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

B
Behdad Esfahbod 已提交
137 138 139 140 141 142 143 144 145
  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 已提交
146
struct IndexArray : ArrayOf<Index>
147
{
B
Behdad Esfahbod 已提交
148 149 150
  inline unsigned int get_indexes (unsigned int start_offset,
				   unsigned int *_count /* IN/OUT */,
				   unsigned int *_indexes /* OUT */) const
151
  {
B
Behdad Esfahbod 已提交
152
    if (_count) {
B
Behdad Esfahbod 已提交
153
      const USHORT *arr = this->sub_array (start_offset, _count);
B
Behdad Esfahbod 已提交
154
      unsigned int count = *_count;
B
Behdad Esfahbod 已提交
155
      for (unsigned int i = 0; i < count; i++)
B
Behdad Esfahbod 已提交
156
	_indexes[i] = arr[i];
B
Behdad Esfahbod 已提交
157 158
    }
    return this->len;
159 160 161 162
  }
};


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


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

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

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

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


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

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

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

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

typedef RecordListOf<Script> ScriptList;


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

B
Behdad Esfahbod 已提交
255
  inline bool sanitize (hb_sanitize_context_t *c) {
256
    TRACE_SANITIZE ();
257
    return TRACE_RETURN (c->check_struct (this) && lookupIndex.sanitize (c));
B
Behdad Esfahbod 已提交
258 259
  }

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

typedef RecordListOf<Feature> FeatureList;


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

B
Behdad Esfahbod 已提交
288 289 290
struct Lookup
{
  inline unsigned int get_subtable_count (void) const { return subTable.len; }
291 292

  inline unsigned int get_type (void) const { return lookupType; }
293 294 295 296 297

  /* 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
298 299
  {
    unsigned int flag = lookupFlag;
300
    if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
301
    {
302
      const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
B
Behdad Esfahbod 已提交
303
      flag += (markFilteringSet << 16);
304 305 306
    }
    return flag;
  }
307

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

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

typedef OffsetListOf<Lookup> LookupList;


/*
 * Coverage Table
 */

B
Behdad Esfahbod 已提交
338 339
struct CoverageFormat1
{
340 341 342
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
343 344
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
345 346
    int i = glyphArray.search (glyph_id);
    if (i != -1)
347 348 349 350
        return i;
    return NOT_COVERED;
  }

B
Behdad Esfahbod 已提交
351
  inline bool sanitize (hb_sanitize_context_t *c) {
352
    TRACE_SANITIZE ();
353
    return TRACE_RETURN (glyphArray.sanitize (c));
B
Behdad Esfahbod 已提交
354 355
  }

356
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
357 358 359
    return glyphs->has (glyphArray[index]);
  }

B
Behdad Esfahbod 已提交
360
  struct Iter {
B
Behdad Esfahbod 已提交
361 362 363 364 365
    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 已提交
366 367 368 369 370 371

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

B
Behdad Esfahbod 已提交
372
  private:
373
  USHORT	coverageFormat;	/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
374
  SortedArrayOf<GlyphID>
375
		glyphArray;	/* Array of GlyphIDs--in numerical order */
376
  public:
377
  DEFINE_SIZE_ARRAY (4, glyphArray);
378 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 388 389 390
    int i = rangeRecord.search (glyph_id);
    if (i != -1) {
      const RangeRecord &range = rangeRecord[i];
      return (unsigned int) range.value + (glyph_id - range.start);
391 392 393 394
    }
    return NOT_COVERED;
  }

B
Behdad Esfahbod 已提交
395
  inline bool sanitize (hb_sanitize_context_t *c) {
396
    TRACE_SANITIZE ();
397
    return TRACE_RETURN (rangeRecord.sanitize (c));
B
Behdad Esfahbod 已提交
398 399
  }

400
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
401 402 403 404 405
    unsigned int i;
    unsigned int count = rangeRecord.len;
    for (i = 0; i < count; i++) {
      const RangeRecord &range = rangeRecord[i];
      if (range.value <= index &&
B
Behdad Esfahbod 已提交
406
	  index < (unsigned int) range.value + (range.end - range.start) &&
407 408 409 410 411 412 413 414
	  range.intersects (glyphs))
        return true;
      else if (index < range.value)
        return false;
    }
    return false;
  }

B
Behdad Esfahbod 已提交
415
  struct Iter {
B
Behdad Esfahbod 已提交
416 417 418 419 420 421 422 423 424
    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 已提交
425 426
      if (j == c->rangeRecord[i].end) {
        i++;
B
Behdad Esfahbod 已提交
427 428 429
	if (more ())
	  j = c->rangeRecord[i].start;
	return;
B
Behdad Esfahbod 已提交
430
      }
B
Behdad Esfahbod 已提交
431 432
      j++;
    }
B
Behdad Esfahbod 已提交
433 434
    inline uint16_t get_glyph (void) { return j; }
    inline uint16_t get_coverage (void) { return coverage; }
B
Behdad Esfahbod 已提交
435 436 437

    private:
    const struct CoverageFormat2 *c;
B
Behdad Esfahbod 已提交
438
    unsigned int i, j, coverage;
B
Behdad Esfahbod 已提交
439 440
  };

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

B
Behdad Esfahbod 已提交
451 452
struct Coverage
{
B
Behdad Esfahbod 已提交
453
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
B
Behdad Esfahbod 已提交
454

B
Behdad Esfahbod 已提交
455
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
456
  {
457
    switch (u.format) {
B
Behdad Esfahbod 已提交
458 459
    case 1: return u.format1.get_coverage(glyph_id);
    case 2: return u.format2.get_coverage(glyph_id);
460 461 462 463
    default:return NOT_COVERED;
    }
  }

B
Behdad Esfahbod 已提交
464
  inline bool sanitize (hb_sanitize_context_t *c) {
465
    TRACE_SANITIZE ();
466
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
467
    switch (u.format) {
468 469 470
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    case 2: return TRACE_RETURN (u.format2.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
471 472
    }
  }
473

474
  inline bool intersects (const hb_set_t *glyphs) const {
B
Behdad Esfahbod 已提交
475 476 477 478 479 480 481
    /* 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;
482
  }
B
Behdad Esfahbod 已提交
483

484
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
485 486 487 488 489
    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 已提交
490 491
  }

B
Behdad Esfahbod 已提交
492
  struct Iter {
B
Behdad Esfahbod 已提交
493 494
    Iter (void) : format (0) {};
    inline void init (const Coverage &c_) {
B
Behdad Esfahbod 已提交
495 496 497 498 499 500 501
      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 已提交
502
    inline bool more (void) {
B
Behdad Esfahbod 已提交
503 504 505 506 507 508
      switch (format) {
      case 1: return u.format1.more ();
      case 2: return u.format2.more ();
      default:return true;
      }
    }
B
Behdad Esfahbod 已提交
509
    inline void next (void) {
B
Behdad Esfahbod 已提交
510 511 512 513 514 515
      switch (format) {
      case 1: u.format1.next (); break;
      case 2: u.format2.next (); break;
      default:                   break;
      }
    }
B
Behdad Esfahbod 已提交
516 517 518 519 520 521 522 523
    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 已提交
524
      switch (format) {
B
Behdad Esfahbod 已提交
525 526
      case 1: return u.format1.get_coverage ();
      case 2: return u.format2.get_coverage ();
B
Behdad Esfahbod 已提交
527 528 529 530 531
      default:return true;
      }
    }

    private:
B
Behdad Esfahbod 已提交
532
    unsigned int format;
B
Behdad Esfahbod 已提交
533 534 535 536 537 538
    union {
    CoverageFormat1::Iter	format1;
    CoverageFormat2::Iter	format2;
    } u;
  };

539 540 541
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
542 543
  CoverageFormat1	format1;
  CoverageFormat2	format2;
544
  } u;
B
Behdad Esfahbod 已提交
545
  public:
B
Behdad Esfahbod 已提交
546
  DEFINE_SIZE_UNION (2, format);
547 548 549 550 551 552 553
};


/*
 * Class Definition Table
 */

B
Behdad Esfahbod 已提交
554 555
struct ClassDefFormat1
{
556 557 558
  friend struct ClassDef;

  private:
559
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
560
  {
561 562 563 564 565
    if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
      return classValue[glyph_id - startGlyph];
    return 0;
  }

B
Behdad Esfahbod 已提交
566
  inline bool sanitize (hb_sanitize_context_t *c) {
567
    TRACE_SANITIZE ();
568
    return TRACE_RETURN (c->check_struct (this) && classValue.sanitize (c));
B
Behdad Esfahbod 已提交
569 570
  }

571
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
572 573 574 575 576 577 578
    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;
  }

579 580 581 582
  USHORT	classFormat;		/* Format identifier--format = 1 */
  GlyphID	startGlyph;		/* First GlyphID of the classValueArray */
  ArrayOf<USHORT>
		classValue;		/* Array of Class Values--one per GlyphID */
583
  public:
584
  DEFINE_SIZE_ARRAY (6, classValue);
585 586
};

B
Behdad Esfahbod 已提交
587 588
struct ClassDefFormat2
{
589 590 591
  friend struct ClassDef;

  private:
592
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
593
  {
B
Behdad Esfahbod 已提交
594 595 596
    int i = rangeRecord.search (glyph_id);
    if (i != -1)
      return rangeRecord[i].value;
597 598 599
    return 0;
  }

B
Behdad Esfahbod 已提交
600
  inline bool sanitize (hb_sanitize_context_t *c) {
601
    TRACE_SANITIZE ();
602
    return TRACE_RETURN (rangeRecord.sanitize (c));
B
Behdad Esfahbod 已提交
603 604
  }

605
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
606 607 608 609 610 611 612
    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;
  }

613
  USHORT	classFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
614
  SortedArrayOf<RangeRecord>
615 616
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID */
617
  public:
618
  DEFINE_SIZE_ARRAY (4, rangeRecord);
619 620
};

B
Behdad Esfahbod 已提交
621 622
struct ClassDef
{
623
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
B
Behdad Esfahbod 已提交
624

625
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
626
  {
627
    switch (u.format) {
B
Behdad Esfahbod 已提交
628 629
    case 1: return u.format1.get_class(glyph_id);
    case 2: return u.format2.get_class(glyph_id);
630 631 632 633
    default:return 0;
    }
  }

B
Behdad Esfahbod 已提交
634
  inline bool sanitize (hb_sanitize_context_t *c) {
635
    TRACE_SANITIZE ();
636
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
637
    switch (u.format) {
638 639 640
    case 1: return TRACE_RETURN (u.format1.sanitize (c));
    case 2: return TRACE_RETURN (u.format2.sanitize (c));
    default:return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
641 642
    }
  }
643

644
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
645 646 647 648 649 650 651
    switch (u.format) {
    case 1: return u.format1.intersects_class (glyphs, klass);
    case 2: return u.format2.intersects_class (glyphs, klass);
    default:return false;
    }
  }

652 653 654
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
655 656
  ClassDefFormat1	format1;
  ClassDefFormat2	format2;
657
  } u;
B
Behdad Esfahbod 已提交
658
  public:
B
Behdad Esfahbod 已提交
659
  DEFINE_SIZE_UNION (2, format);
660 661 662 663 664 665 666
};


/*
 * Device Tables
 */

B
Behdad Esfahbod 已提交
667 668
struct Device
{
B
Behdad Esfahbod 已提交
669

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

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

676
  inline int get_delta (unsigned int ppem, int scale) const
677 678 679 680 681 682 683 684 685 686 687 688
  {
    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 已提交
689
  {
690
    unsigned int f = deltaFormat;
691
    if (unlikely (f < 1 || f > 3))
692
      return 0;
693

694 695
    if (ppem_size < startSize || ppem_size > endSize)
      return 0;
696

697
    unsigned int s = ppem_size - startSize;
698

699
    unsigned int byte = deltaValue[s >> (4 - f)];
B
Behdad Esfahbod 已提交
700 701
    unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
    unsigned int mask = (0xFFFF >> (16 - (1 << f)));
702 703 704

    int delta = bits & mask;

B
Behdad Esfahbod 已提交
705
    if ((unsigned int) delta >= ((mask + 1) >> 1))
706 707 708 709 710
      delta -= mask + 1;

    return delta;
  }

B
Behdad Esfahbod 已提交
711
  inline unsigned int get_size (void) const
B
Behdad Esfahbod 已提交
712 713
  {
    unsigned int f = deltaFormat;
B
Behdad Esfahbod 已提交
714 715
    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 已提交
716 717
  }

B
Behdad Esfahbod 已提交
718
  inline bool sanitize (hb_sanitize_context_t *c) {
719
    TRACE_SANITIZE ();
720
    return TRACE_RETURN (c->check_struct (this) && c->check_range (this, this->get_size ()));
B
Behdad Esfahbod 已提交
721
  }
722 723

  private:
B
Behdad Esfahbod 已提交
724 725
  USHORT	startSize;		/* Smallest size to correct--in ppem */
  USHORT	endSize;		/* Largest size to correct--in ppem */
726 727 728 729 730
  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 已提交
731
  USHORT	deltaValue[VAR];	/* Array of compressed data */
B
Behdad Esfahbod 已提交
732
  public:
733
  DEFINE_SIZE_ARRAY (6, deltaValue);
734 735 736
};


B
Behdad Esfahbod 已提交
737

738
#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */