hb-ot-layout-common-private.hh 24.6 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


37 38 39
namespace OT {


B
Minor  
Behdad Esfahbod 已提交
40
#define NOT_COVERED		((unsigned int) -1)
B
Behdad Esfahbod 已提交
41 42
#define MAX_NESTING_LEVEL	8

B
Behdad Esfahbod 已提交
43

B
Behdad Esfahbod 已提交
44

45 46 47 48 49 50
/*
 *
 * OpenType Layout Common Table Formats
 *
 */

51

52 53 54 55 56
/*
 * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
 */

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

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

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

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

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

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


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

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

140 141
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const {
142 143 144
    glyphs->add_range (start, end);
  }

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


171 172 173 174 175
struct Script;
struct LangSys;
struct Feature;


B
Behdad Esfahbod 已提交
176 177
struct LangSys
{
178 179 180 181
  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 已提交
182 183 184 185
  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); }
186

B
Behdad Esfahbod 已提交
187
  inline bool has_required_feature (void) const { return reqFeatureIndex != 0xffff; }
B
Behdad Esfahbod 已提交
188
  inline unsigned int get_required_feature_index (void) const
B
Behdad Esfahbod 已提交
189
  {
190
    if (reqFeatureIndex == 0xffff)
B
Behdad Esfahbod 已提交
191
      return Index::NOT_FOUND_INDEX;
192 193 194
   return reqFeatureIndex;;
  }

B
Behdad Esfahbod 已提交
195
  inline bool sanitize (hb_sanitize_context_t *c) {
196
    TRACE_SANITIZE ();
197
    return TRACE_RETURN (c->check_struct (this) && featureIndex.sanitize (c));
B
Behdad Esfahbod 已提交
198 199
  }

200 201 202 203 204
  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 */
205
  IndexArray	featureIndex;	/* Array of indices into the FeatureList */
B
Behdad Esfahbod 已提交
206
  public:
207
  DEFINE_SIZE_ARRAY (6, featureIndex);
208
};
B
Behdad Esfahbod 已提交
209
DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF");
210 211


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

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

B
Behdad Esfahbod 已提交
233
  inline bool sanitize (hb_sanitize_context_t *c) {
234
    TRACE_SANITIZE ();
235
    return TRACE_RETURN (defaultLangSys.sanitize (c, this) && langSys.sanitize (c, this));
B
Behdad Esfahbod 已提交
236 237
  }

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

typedef RecordListOf<Script> ScriptList;


B
Behdad Esfahbod 已提交
252 253
struct Feature
{
254 255 256 257
  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 已提交
258 259 260 261
  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); }
262

B
Behdad Esfahbod 已提交
263
  inline bool sanitize (hb_sanitize_context_t *c) {
264
    TRACE_SANITIZE ();
265
    return TRACE_RETURN (c->check_struct (this) && lookupIndex.sanitize (c));
B
Behdad Esfahbod 已提交
266 267
  }

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

typedef RecordListOf<Feature> FeatureList;


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

B
Behdad Esfahbod 已提交
296 297 298
struct Lookup
{
  inline unsigned int get_subtable_count (void) const { return subTable.len; }
299 300

  inline unsigned int get_type (void) const { return lookupType; }
301 302 303 304 305

  /* 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
306 307
  {
    unsigned int flag = lookupFlag;
308
    if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
309
    {
310
      const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
B
Behdad Esfahbod 已提交
311
      flag += (markFilteringSet << 16);
312 313 314
    }
    return flag;
  }
315

316 317 318 319 320 321 322 323 324 325
  inline bool serialize (hb_serialize_context_t *c,
			 unsigned int lookup_type,
			 uint32_t lookup_props,
			 unsigned int num_subtables)
  {
    TRACE_SERIALIZE ();
    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
    lookupType.set (lookup_type);
    lookupFlag.set (lookup_props & 0xFFFF);
    if (unlikely (!subTable.serialize (c, num_subtables))) return TRACE_RETURN (false);
326
    if (lookupFlag & LookupFlag::UseMarkFilteringSet)
327 328 329 330 331 332 333
    {
      USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
      markFilteringSet.set (lookup_props >> 16);
    }
    return TRACE_RETURN (true);
  }

B
Behdad Esfahbod 已提交
334
  inline bool sanitize (hb_sanitize_context_t *c) {
335
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
336
    /* Real sanitize of the subtables is done by GSUB/GPOS/... */
337
    if (!(c->check_struct (this) && subTable.sanitize (c))) return TRACE_RETURN (false);
338
    if (lookupFlag & LookupFlag::UseMarkFilteringSet)
B
Behdad Esfahbod 已提交
339
    {
340
      USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
341
      if (!markFilteringSet.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
342
    }
343
    return TRACE_RETURN (true);
B
Behdad Esfahbod 已提交
344 345
  }

346 347
  USHORT	lookupType;		/* Different enumerations for GSUB and GPOS */
  USHORT	lookupFlag;		/* Lookup qualifiers */
B
Behdad Esfahbod 已提交
348
  ArrayOf<Offset>
349
		subTable;		/* Array of SubTables */
B
Behdad Esfahbod 已提交
350
  USHORT	markFilteringSetX[VAR];	/* Index (base 0) into GDEF mark glyph sets
351 352
					 * structure. This field is only present if bit
					 * UseMarkFilteringSet of lookup flags is set. */
B
Behdad Esfahbod 已提交
353
  public:
354
  DEFINE_SIZE_ARRAY2 (6, subTable, markFilteringSetX);
355 356 357 358 359 360 361 362 363
};

typedef OffsetListOf<Lookup> LookupList;


/*
 * Coverage Table
 */

B
Behdad Esfahbod 已提交
364 365
struct CoverageFormat1
{
366 367 368
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
369 370
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
371
    int i = glyphArray.search (glyph_id);
B
Minor  
Behdad Esfahbod 已提交
372 373
    ASSERT_STATIC (((unsigned int) -1) == NOT_COVERED);
    return i;
374 375
  }

376
  inline bool serialize (hb_serialize_context_t *c,
377
			 Supplier<GlyphID> &glyphs,
378
			 unsigned int num_glyphs)
379 380
  {
    TRACE_SERIALIZE ();
381 382 383
    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
    glyphArray.len.set (num_glyphs);
    if (unlikely (!c->extend (glyphArray))) return TRACE_RETURN (false);
384
    for (unsigned int i = 0; i < num_glyphs; i++)
385
      glyphArray[i] = glyphs[i];
386
    glyphs.advance (num_glyphs);
387 388 389
    return TRACE_RETURN (true);
  }

B
Behdad Esfahbod 已提交
390
  inline bool sanitize (hb_sanitize_context_t *c) {
391
    TRACE_SANITIZE ();
392
    return TRACE_RETURN (glyphArray.sanitize (c));
B
Behdad Esfahbod 已提交
393 394
  }

395
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
396 397 398
    return glyphs->has (glyphArray[index]);
  }

399 400
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const {
401 402 403 404 405
    unsigned int count = glyphArray.len;
    for (unsigned int i = 0; i < count; i++)
      glyphs->add (glyphArray[i]);
  }

B
Behdad Esfahbod 已提交
406 407
  public:
  /* Older compilers need this to be public. */
B
Behdad Esfahbod 已提交
408
  struct Iter {
B
Behdad Esfahbod 已提交
409 410 411 412 413
    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 已提交
414 415 416 417 418

    private:
    const struct CoverageFormat1 *c;
    unsigned int i;
  };
B
Behdad Esfahbod 已提交
419
  private:
B
Behdad Esfahbod 已提交
420

421
  protected:
422
  USHORT	coverageFormat;	/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
423
  SortedArrayOf<GlyphID>
424
		glyphArray;	/* Array of GlyphIDs--in numerical order */
425
  public:
426
  DEFINE_SIZE_ARRAY (4, glyphArray);
427 428
};

B
Behdad Esfahbod 已提交
429 430
struct CoverageFormat2
{
431 432 433
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
434 435
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
436 437 438 439
    int i = rangeRecord.search (glyph_id);
    if (i != -1) {
      const RangeRecord &range = rangeRecord[i];
      return (unsigned int) range.value + (glyph_id - range.start);
440 441 442 443
    }
    return NOT_COVERED;
  }

444
  inline bool serialize (hb_serialize_context_t *c,
445
			 Supplier<GlyphID> &glyphs,
446
			 unsigned int num_glyphs)
447 448
  {
    TRACE_SERIALIZE ();
449 450 451 452
    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);

    if (unlikely (!num_glyphs)) return TRACE_RETURN (true);

453 454 455 456
    unsigned int num_ranges = 1;
    for (unsigned int i = 1; i < num_glyphs; i++)
      if (glyphs[i - 1] + 1 != glyphs[i])
        num_ranges++;
457 458 459
    rangeRecord.len.set (num_ranges);
    if (unlikely (!c->extend (rangeRecord))) return TRACE_RETURN (false);

460
    unsigned int range = 0;
461
    rangeRecord[range].start = glyphs[0];
462
    rangeRecord[range].value.set (0);
463 464 465
    for (unsigned int i = 1; i < num_glyphs; i++)
      if (glyphs[i - 1] + 1 != glyphs[i]) {
	range++;
466 467 468
	rangeRecord[range].start = glyphs[i];
	rangeRecord[range].value.set (i);
        rangeRecord[range].end = glyphs[i];
469
      } else {
470
        rangeRecord[range].end = glyphs[i];
471
      }
472
    glyphs.advance (num_glyphs);
473 474 475
    return TRACE_RETURN (true);
  }

B
Behdad Esfahbod 已提交
476
  inline bool sanitize (hb_sanitize_context_t *c) {
477
    TRACE_SANITIZE ();
478
    return TRACE_RETURN (rangeRecord.sanitize (c));
B
Behdad Esfahbod 已提交
479 480
  }

481
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
482 483 484 485 486
    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 已提交
487
	  index < (unsigned int) range.value + (range.end - range.start) &&
488 489 490 491 492 493 494 495
	  range.intersects (glyphs))
        return true;
      else if (index < range.value)
        return false;
    }
    return false;
  }

496 497
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const {
498 499 500 501 502
    unsigned int count = rangeRecord.len;
    for (unsigned int i = 0; i < count; i++)
      rangeRecord[i].add_coverage (glyphs);
  }

B
Behdad Esfahbod 已提交
503 504
  public:
  /* Older compilers need this to be public. */
B
Behdad Esfahbod 已提交
505
  struct Iter {
B
Behdad Esfahbod 已提交
506 507 508 509 510 511 512 513 514
    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 已提交
515 516
      if (j == c->rangeRecord[i].end) {
        i++;
B
Behdad Esfahbod 已提交
517 518 519
	if (more ())
	  j = c->rangeRecord[i].start;
	return;
B
Behdad Esfahbod 已提交
520
      }
B
Behdad Esfahbod 已提交
521 522
      j++;
    }
B
Behdad Esfahbod 已提交
523 524
    inline uint16_t get_glyph (void) { return j; }
    inline uint16_t get_coverage (void) { return coverage; }
B
Behdad Esfahbod 已提交
525 526 527

    private:
    const struct CoverageFormat2 *c;
B
Behdad Esfahbod 已提交
528
    unsigned int i, j, coverage;
B
Behdad Esfahbod 已提交
529
  };
B
Behdad Esfahbod 已提交
530
  private:
B
Behdad Esfahbod 已提交
531

532
  protected:
533
  USHORT	coverageFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
534
  SortedArrayOf<RangeRecord>
535 536 537
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID. rangeCount entries
				 * long */
538
  public:
539
  DEFINE_SIZE_ARRAY (4, rangeRecord);
540 541
};

B
Behdad Esfahbod 已提交
542 543
struct Coverage
{
B
Behdad Esfahbod 已提交
544
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
B
Behdad Esfahbod 已提交
545

B
Behdad Esfahbod 已提交
546
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
547
  {
548
    switch (u.format) {
B
Behdad Esfahbod 已提交
549 550
    case 1: return u.format1.get_coverage(glyph_id);
    case 2: return u.format2.get_coverage(glyph_id);
551 552 553 554
    default:return NOT_COVERED;
    }
  }

555
  inline bool serialize (hb_serialize_context_t *c,
556
			 Supplier<GlyphID> &glyphs,
557
			 unsigned int num_glyphs)
558 559
  {
    TRACE_SERIALIZE ();
B
Behdad Esfahbod 已提交
560
    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
561 562 563 564
    unsigned int num_ranges = 1;
    for (unsigned int i = 1; i < num_glyphs; i++)
      if (glyphs[i - 1] + 1 != glyphs[i])
        num_ranges++;
565 566 567 568
    u.format.set (num_glyphs * 2 < num_ranges * 3 ? 1 : 2);
    switch (u.format) {
    case 1: return TRACE_RETURN (u.format1.serialize (c, glyphs, num_glyphs));
    case 2: return TRACE_RETURN (u.format2.serialize (c, glyphs, num_glyphs));
569 570 571 572
    default:return TRACE_RETURN (false);
    }
  }

B
Behdad Esfahbod 已提交
573
  inline bool sanitize (hb_sanitize_context_t *c) {
574
    TRACE_SANITIZE ();
575
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
576
    switch (u.format) {
577 578 579
    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 已提交
580 581
    }
  }
582

583
  inline bool intersects (const hb_set_t *glyphs) const {
B
Behdad Esfahbod 已提交
584 585 586 587 588 589 590
    /* 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;
591
  }
B
Behdad Esfahbod 已提交
592

593
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
594 595 596 597 598
    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 已提交
599 600
  }

601 602
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const {
603 604 605 606 607 608 609
    switch (u.format) {
    case 1: u.format1.add_coverage (glyphs); break;
    case 2: u.format2.add_coverage (glyphs); break;
    default:                                 break;
    }
  }

B
Behdad Esfahbod 已提交
610
  struct Iter {
B
Behdad Esfahbod 已提交
611 612
    Iter (void) : format (0) {};
    inline void init (const Coverage &c_) {
B
Behdad Esfahbod 已提交
613 614 615 616 617 618 619
      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 已提交
620
    inline bool more (void) {
B
Behdad Esfahbod 已提交
621 622 623 624 625 626
      switch (format) {
      case 1: return u.format1.more ();
      case 2: return u.format2.more ();
      default:return true;
      }
    }
B
Behdad Esfahbod 已提交
627
    inline void next (void) {
B
Behdad Esfahbod 已提交
628 629 630 631 632 633
      switch (format) {
      case 1: u.format1.next (); break;
      case 2: u.format2.next (); break;
      default:                   break;
      }
    }
B
Behdad Esfahbod 已提交
634 635 636 637 638 639 640 641
    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 已提交
642
      switch (format) {
B
Behdad Esfahbod 已提交
643 644
      case 1: return u.format1.get_coverage ();
      case 2: return u.format2.get_coverage ();
B
Behdad Esfahbod 已提交
645 646 647 648 649
      default:return true;
      }
    }

    private:
B
Behdad Esfahbod 已提交
650
    unsigned int format;
B
Behdad Esfahbod 已提交
651 652 653 654 655 656
    union {
    CoverageFormat1::Iter	format1;
    CoverageFormat2::Iter	format2;
    } u;
  };

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


/*
 * Class Definition Table
 */

B
Behdad Esfahbod 已提交
672 673
struct ClassDefFormat1
{
674 675 676
  friend struct ClassDef;

  private:
677
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
678
  {
B
Minor  
Behdad Esfahbod 已提交
679
    if (unlikely ((unsigned int) (glyph_id - startGlyph) < classValue.len))
680 681 682 683
      return classValue[glyph_id - startGlyph];
    return 0;
  }

B
Behdad Esfahbod 已提交
684
  inline bool sanitize (hb_sanitize_context_t *c) {
685
    TRACE_SANITIZE ();
686
    return TRACE_RETURN (c->check_struct (this) && classValue.sanitize (c));
B
Behdad Esfahbod 已提交
687 688
  }

689 690 691 692 693 694 695 696
  template <typename set_t>
  inline void add_class (set_t *glyphs, unsigned int klass) const {
    unsigned int count = classValue.len;
    for (unsigned int i = 0; i < count; i++)
      if (classValue[i] == klass)
        glyphs->add (startGlyph + i);
  }

697
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
698 699 700 701 702 703 704
    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;
  }

705
  protected:
706 707 708 709
  USHORT	classFormat;		/* Format identifier--format = 1 */
  GlyphID	startGlyph;		/* First GlyphID of the classValueArray */
  ArrayOf<USHORT>
		classValue;		/* Array of Class Values--one per GlyphID */
710
  public:
711
  DEFINE_SIZE_ARRAY (6, classValue);
712 713
};

B
Behdad Esfahbod 已提交
714 715
struct ClassDefFormat2
{
716 717 718
  friend struct ClassDef;

  private:
719
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
720
  {
B
Behdad Esfahbod 已提交
721 722 723
    int i = rangeRecord.search (glyph_id);
    if (i != -1)
      return rangeRecord[i].value;
724 725 726
    return 0;
  }

B
Behdad Esfahbod 已提交
727
  inline bool sanitize (hb_sanitize_context_t *c) {
728
    TRACE_SANITIZE ();
729
    return TRACE_RETURN (rangeRecord.sanitize (c));
B
Behdad Esfahbod 已提交
730 731
  }

732 733 734 735 736 737 738 739
  template <typename set_t>
  inline void add_class (set_t *glyphs, unsigned int klass) const {
    unsigned int count = rangeRecord.len;
    for (unsigned int i = 0; i < count; i++)
      if (rangeRecord[i].value == klass)
        rangeRecord[i].add_coverage (glyphs);
  }

740
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
741 742 743 744 745 746 747
    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;
  }

748
  protected:
749
  USHORT	classFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
750
  SortedArrayOf<RangeRecord>
751 752
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID */
753
  public:
754
  DEFINE_SIZE_ARRAY (4, rangeRecord);
755 756
};

B
Behdad Esfahbod 已提交
757 758
struct ClassDef
{
759
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
B
Behdad Esfahbod 已提交
760

761
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
762
  {
763
    switch (u.format) {
B
Behdad Esfahbod 已提交
764 765
    case 1: return u.format1.get_class(glyph_id);
    case 2: return u.format2.get_class(glyph_id);
766 767 768 769
    default:return 0;
    }
  }

B
Behdad Esfahbod 已提交
770
  inline bool sanitize (hb_sanitize_context_t *c) {
771
    TRACE_SANITIZE ();
772
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
773
    switch (u.format) {
774 775 776
    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 已提交
777 778
    }
  }
779

780 781 782 783 784 785 786 787
  inline void add_class (hb_set_t *glyphs, unsigned int klass) const {
    switch (u.format) {
    case 1: u.format1.add_class (glyphs, klass); return;
    case 2: u.format2.add_class (glyphs, klass); return;
    default:return;
    }
  }

788
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
789 790 791 792 793 794 795
    switch (u.format) {
    case 1: return u.format1.intersects_class (glyphs, klass);
    case 2: return u.format2.intersects_class (glyphs, klass);
    default:return false;
    }
  }

796
  protected:
797 798
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
799 800
  ClassDefFormat1	format1;
  ClassDefFormat2	format2;
801
  } u;
B
Behdad Esfahbod 已提交
802
  public:
B
Behdad Esfahbod 已提交
803
  DEFINE_SIZE_UNION (2, format);
804 805 806 807 808 809 810
};


/*
 * Device Tables
 */

B
Behdad Esfahbod 已提交
811 812
struct Device
{
B
Behdad Esfahbod 已提交
813

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

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

820
  inline int get_delta (unsigned int ppem, int scale) const
821 822 823 824 825 826 827 828 829 830 831 832
  {
    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 已提交
833
  {
834
    unsigned int f = deltaFormat;
835
    if (unlikely (f < 1 || f > 3))
836
      return 0;
837

838 839
    if (ppem_size < startSize || ppem_size > endSize)
      return 0;
840

841
    unsigned int s = ppem_size - startSize;
842

843
    unsigned int byte = deltaValue[s >> (4 - f)];
B
Behdad Esfahbod 已提交
844 845
    unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
    unsigned int mask = (0xFFFF >> (16 - (1 << f)));
846 847 848

    int delta = bits & mask;

B
Behdad Esfahbod 已提交
849
    if ((unsigned int) delta >= ((mask + 1) >> 1))
850 851 852 853 854
      delta -= mask + 1;

    return delta;
  }

B
Behdad Esfahbod 已提交
855
  inline unsigned int get_size (void) const
B
Behdad Esfahbod 已提交
856 857
  {
    unsigned int f = deltaFormat;
B
Behdad Esfahbod 已提交
858 859
    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 已提交
860 861
  }

B
Behdad Esfahbod 已提交
862
  inline bool sanitize (hb_sanitize_context_t *c) {
863
    TRACE_SANITIZE ();
864
    return TRACE_RETURN (c->check_struct (this) && c->check_range (this, this->get_size ()));
B
Behdad Esfahbod 已提交
865
  }
866

867
  protected:
B
Behdad Esfahbod 已提交
868 869
  USHORT	startSize;		/* Smallest size to correct--in ppem */
  USHORT	endSize;		/* Largest size to correct--in ppem */
870 871 872 873 874
  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 已提交
875
  USHORT	deltaValue[VAR];	/* Array of compressed data */
B
Behdad Esfahbod 已提交
876
  public:
877
  DEFINE_SIZE_ARRAY (6, deltaValue);
878 879 880
};


B
Behdad Esfahbod 已提交
881
} /* namespace OT */
882

B
Behdad Esfahbod 已提交
883

884
#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */