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

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

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

typedef OffsetListOf<Lookup> LookupList;


/*
 * Coverage Table
 */

B
Behdad Esfahbod 已提交
346 347
struct CoverageFormat1
{
348 349 350
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
351 352
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
353
    int i = glyphArray.search (glyph_id);
B
Minor  
Behdad Esfahbod 已提交
354 355
    ASSERT_STATIC (((unsigned int) -1) == NOT_COVERED);
    return i;
356 357
  }

358 359 360
  inline bool serialize (hb_serialize_context_t *c,
			 const USHORT *glyphs,
			 unsigned int num_glyphs)
361 362
  {
    TRACE_SERIALIZE ();
363 364 365
    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
    glyphArray.len.set (num_glyphs);
    if (unlikely (!c->extend (glyphArray))) return TRACE_RETURN (false);
366
    for (unsigned int i = 0; i < num_glyphs; i++)
367
      glyphArray[i].set (glyphs[i]);
368 369 370
    return TRACE_RETURN (true);
  }

B
Behdad Esfahbod 已提交
371
  inline bool sanitize (hb_sanitize_context_t *c) {
372
    TRACE_SANITIZE ();
373
    return TRACE_RETURN (glyphArray.sanitize (c));
B
Behdad Esfahbod 已提交
374 375
  }

376
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
377 378 379
    return glyphs->has (glyphArray[index]);
  }

380 381
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const {
382 383 384 385 386
    unsigned int count = glyphArray.len;
    for (unsigned int i = 0; i < count; i++)
      glyphs->add (glyphArray[i]);
  }

B
Behdad Esfahbod 已提交
387
  struct Iter {
B
Behdad Esfahbod 已提交
388 389 390 391 392
    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 已提交
393 394 395 396 397 398

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

399
  protected:
400
  USHORT	coverageFormat;	/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
401
  SortedArrayOf<GlyphID>
402
		glyphArray;	/* Array of GlyphIDs--in numerical order */
403
  public:
404
  DEFINE_SIZE_ARRAY (4, glyphArray);
405 406
};

B
Behdad Esfahbod 已提交
407 408
struct CoverageFormat2
{
409 410 411
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
412 413
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
414 415 416 417
    int i = rangeRecord.search (glyph_id);
    if (i != -1) {
      const RangeRecord &range = rangeRecord[i];
      return (unsigned int) range.value + (glyph_id - range.start);
418 419 420 421
    }
    return NOT_COVERED;
  }

422 423 424
  inline bool serialize (hb_serialize_context_t *c,
			 const USHORT *glyphs,
			 unsigned int num_glyphs)
425 426
  {
    TRACE_SERIALIZE ();
427 428 429 430
    if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);

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

431 432 433 434
    unsigned int num_ranges = 1;
    for (unsigned int i = 1; i < num_glyphs; i++)
      if (glyphs[i - 1] + 1 != glyphs[i])
        num_ranges++;
435 436 437
    rangeRecord.len.set (num_ranges);
    if (unlikely (!c->extend (rangeRecord))) return TRACE_RETURN (false);

438
    unsigned int range = 0;
439 440
    rangeRecord[range].start.set (glyphs[0]);
    rangeRecord[range].value.set (0);
441 442
    for (unsigned int i = 1; i < num_glyphs; i++)
      if (glyphs[i - 1] + 1 != glyphs[i]) {
443 444
	rangeRecord[range].start.set (glyphs[i]);
	rangeRecord[range].value.set (i);
445 446
	range++;
      } else {
447
        rangeRecord[range].end = glyphs[i];
448 449 450 451
      }
    return TRACE_RETURN (true);
  }

B
Behdad Esfahbod 已提交
452
  inline bool sanitize (hb_sanitize_context_t *c) {
453
    TRACE_SANITIZE ();
454
    return TRACE_RETURN (rangeRecord.sanitize (c));
B
Behdad Esfahbod 已提交
455 456
  }

457
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
458 459 460 461 462
    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 已提交
463
	  index < (unsigned int) range.value + (range.end - range.start) &&
464 465 466 467 468 469 470 471
	  range.intersects (glyphs))
        return true;
      else if (index < range.value)
        return false;
    }
    return false;
  }

472 473
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const {
474 475 476 477 478
    unsigned int count = rangeRecord.len;
    for (unsigned int i = 0; i < count; i++)
      rangeRecord[i].add_coverage (glyphs);
  }

B
Behdad Esfahbod 已提交
479
  struct Iter {
B
Behdad Esfahbod 已提交
480 481 482 483 484 485 486 487 488
    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 已提交
489 490
      if (j == c->rangeRecord[i].end) {
        i++;
B
Behdad Esfahbod 已提交
491 492 493
	if (more ())
	  j = c->rangeRecord[i].start;
	return;
B
Behdad Esfahbod 已提交
494
      }
B
Behdad Esfahbod 已提交
495 496
      j++;
    }
B
Behdad Esfahbod 已提交
497 498
    inline uint16_t get_glyph (void) { return j; }
    inline uint16_t get_coverage (void) { return coverage; }
B
Behdad Esfahbod 已提交
499 500 501

    private:
    const struct CoverageFormat2 *c;
B
Behdad Esfahbod 已提交
502
    unsigned int i, j, coverage;
B
Behdad Esfahbod 已提交
503 504
  };

505
  protected:
506
  USHORT	coverageFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
507
  SortedArrayOf<RangeRecord>
508 509 510
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID. rangeCount entries
				 * long */
511
  public:
512
  DEFINE_SIZE_ARRAY (4, rangeRecord);
513 514
};

B
Behdad Esfahbod 已提交
515 516
struct Coverage
{
B
Behdad Esfahbod 已提交
517
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
B
Behdad Esfahbod 已提交
518

B
Behdad Esfahbod 已提交
519
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
520
  {
521
    switch (u.format) {
B
Behdad Esfahbod 已提交
522 523
    case 1: return u.format1.get_coverage(glyph_id);
    case 2: return u.format2.get_coverage(glyph_id);
524 525 526 527
    default:return NOT_COVERED;
    }
  }

528 529 530
  inline bool serialize (hb_serialize_context_t *c,
			 const USHORT *glyphs,
			 unsigned int num_glyphs)
531 532
  {
    TRACE_SERIALIZE ();
533
    if (unlikely (c->extend_min (*this))) return TRACE_RETURN (false);
534 535 536 537
    unsigned int num_ranges = 1;
    for (unsigned int i = 1; i < num_glyphs; i++)
      if (glyphs[i - 1] + 1 != glyphs[i])
        num_ranges++;
538 539 540 541
    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));
542 543 544 545
    default:return TRACE_RETURN (false);
    }
  }

B
Behdad Esfahbod 已提交
546
  inline bool sanitize (hb_sanitize_context_t *c) {
547
    TRACE_SANITIZE ();
548
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
549
    switch (u.format) {
550 551 552
    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 已提交
553 554
    }
  }
555

556
  inline bool intersects (const hb_set_t *glyphs) const {
B
Behdad Esfahbod 已提交
557 558 559 560 561 562 563
    /* 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;
564
  }
B
Behdad Esfahbod 已提交
565

566
  inline bool intersects_coverage (const hb_set_t *glyphs, unsigned int index) const {
567 568 569 570 571
    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 已提交
572 573
  }

574 575
  template <typename set_t>
  inline void add_coverage (set_t *glyphs) const {
576 577 578 579 580 581 582
    switch (u.format) {
    case 1: u.format1.add_coverage (glyphs); break;
    case 2: u.format2.add_coverage (glyphs); break;
    default:                                 break;
    }
  }

B
Behdad Esfahbod 已提交
583
  struct Iter {
B
Behdad Esfahbod 已提交
584 585
    Iter (void) : format (0) {};
    inline void init (const Coverage &c_) {
B
Behdad Esfahbod 已提交
586 587 588 589 590 591 592
      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 已提交
593
    inline bool more (void) {
B
Behdad Esfahbod 已提交
594 595 596 597 598 599
      switch (format) {
      case 1: return u.format1.more ();
      case 2: return u.format2.more ();
      default:return true;
      }
    }
B
Behdad Esfahbod 已提交
600
    inline void next (void) {
B
Behdad Esfahbod 已提交
601 602 603 604 605 606
      switch (format) {
      case 1: u.format1.next (); break;
      case 2: u.format2.next (); break;
      default:                   break;
      }
    }
B
Behdad Esfahbod 已提交
607 608 609 610 611 612 613 614
    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 已提交
615
      switch (format) {
B
Behdad Esfahbod 已提交
616 617
      case 1: return u.format1.get_coverage ();
      case 2: return u.format2.get_coverage ();
B
Behdad Esfahbod 已提交
618 619 620 621 622
      default:return true;
      }
    }

    private:
B
Behdad Esfahbod 已提交
623
    unsigned int format;
B
Behdad Esfahbod 已提交
624 625 626 627 628 629
    union {
    CoverageFormat1::Iter	format1;
    CoverageFormat2::Iter	format2;
    } u;
  };

630
  protected:
631 632
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
633 634
  CoverageFormat1	format1;
  CoverageFormat2	format2;
635
  } u;
B
Behdad Esfahbod 已提交
636
  public:
B
Behdad Esfahbod 已提交
637
  DEFINE_SIZE_UNION (2, format);
638 639 640 641 642 643 644
};


/*
 * Class Definition Table
 */

B
Behdad Esfahbod 已提交
645 646
struct ClassDefFormat1
{
647 648 649
  friend struct ClassDef;

  private:
650
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
651
  {
B
Minor  
Behdad Esfahbod 已提交
652
    if (unlikely ((unsigned int) (glyph_id - startGlyph) < classValue.len))
653 654 655 656
      return classValue[glyph_id - startGlyph];
    return 0;
  }

B
Behdad Esfahbod 已提交
657
  inline bool sanitize (hb_sanitize_context_t *c) {
658
    TRACE_SANITIZE ();
659
    return TRACE_RETURN (c->check_struct (this) && classValue.sanitize (c));
B
Behdad Esfahbod 已提交
660 661
  }

662
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
663 664 665 666 667 668 669
    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;
  }

670
  protected:
671 672 673 674
  USHORT	classFormat;		/* Format identifier--format = 1 */
  GlyphID	startGlyph;		/* First GlyphID of the classValueArray */
  ArrayOf<USHORT>
		classValue;		/* Array of Class Values--one per GlyphID */
675
  public:
676
  DEFINE_SIZE_ARRAY (6, classValue);
677 678
};

B
Behdad Esfahbod 已提交
679 680
struct ClassDefFormat2
{
681 682 683
  friend struct ClassDef;

  private:
684
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
685
  {
B
Behdad Esfahbod 已提交
686 687 688
    int i = rangeRecord.search (glyph_id);
    if (i != -1)
      return rangeRecord[i].value;
689 690 691
    return 0;
  }

B
Behdad Esfahbod 已提交
692
  inline bool sanitize (hb_sanitize_context_t *c) {
693
    TRACE_SANITIZE ();
694
    return TRACE_RETURN (rangeRecord.sanitize (c));
B
Behdad Esfahbod 已提交
695 696
  }

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

705
  protected:
706
  USHORT	classFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
707
  SortedArrayOf<RangeRecord>
708 709
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID */
710
  public:
711
  DEFINE_SIZE_ARRAY (4, rangeRecord);
712 713
};

B
Behdad Esfahbod 已提交
714 715
struct ClassDef
{
716
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
B
Behdad Esfahbod 已提交
717

718
  inline unsigned int get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
719
  {
720
    switch (u.format) {
B
Behdad Esfahbod 已提交
721 722
    case 1: return u.format1.get_class(glyph_id);
    case 2: return u.format2.get_class(glyph_id);
723 724 725 726
    default:return 0;
    }
  }

B
Behdad Esfahbod 已提交
727
  inline bool sanitize (hb_sanitize_context_t *c) {
728
    TRACE_SANITIZE ();
729
    if (!u.format.sanitize (c)) return TRACE_RETURN (false);
B
Behdad Esfahbod 已提交
730
    switch (u.format) {
731 732 733
    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 已提交
734 735
    }
  }
736

737
  inline bool intersects_class (const hb_set_t *glyphs, unsigned int klass) const {
738 739 740 741 742 743 744
    switch (u.format) {
    case 1: return u.format1.intersects_class (glyphs, klass);
    case 2: return u.format2.intersects_class (glyphs, klass);
    default:return false;
    }
  }

745
  protected:
746 747
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
748 749
  ClassDefFormat1	format1;
  ClassDefFormat2	format2;
750
  } u;
B
Behdad Esfahbod 已提交
751
  public:
B
Behdad Esfahbod 已提交
752
  DEFINE_SIZE_UNION (2, format);
753 754 755 756 757 758 759
};


/*
 * Device Tables
 */

B
Behdad Esfahbod 已提交
760 761
struct Device
{
B
Behdad Esfahbod 已提交
762

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

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

769
  inline int get_delta (unsigned int ppem, int scale) const
770 771 772 773 774 775 776 777 778 779 780 781
  {
    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 已提交
782
  {
783
    unsigned int f = deltaFormat;
784
    if (unlikely (f < 1 || f > 3))
785
      return 0;
786

787 788
    if (ppem_size < startSize || ppem_size > endSize)
      return 0;
789

790
    unsigned int s = ppem_size - startSize;
791

792
    unsigned int byte = deltaValue[s >> (4 - f)];
B
Behdad Esfahbod 已提交
793 794
    unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
    unsigned int mask = (0xFFFF >> (16 - (1 << f)));
795 796 797

    int delta = bits & mask;

B
Behdad Esfahbod 已提交
798
    if ((unsigned int) delta >= ((mask + 1) >> 1))
799 800 801 802 803
      delta -= mask + 1;

    return delta;
  }

B
Behdad Esfahbod 已提交
804
  inline unsigned int get_size (void) const
B
Behdad Esfahbod 已提交
805 806
  {
    unsigned int f = deltaFormat;
B
Behdad Esfahbod 已提交
807 808
    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 已提交
809 810
  }

B
Behdad Esfahbod 已提交
811
  inline bool sanitize (hb_sanitize_context_t *c) {
812
    TRACE_SANITIZE ();
813
    return TRACE_RETURN (c->check_struct (this) && c->check_range (this, this->get_size ()));
B
Behdad Esfahbod 已提交
814
  }
815

816
  protected:
B
Behdad Esfahbod 已提交
817 818
  USHORT	startSize;		/* Smallest size to correct--in ppem */
  USHORT	endSize;		/* Largest size to correct--in ppem */
819 820 821 822 823
  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 已提交
824
  USHORT	deltaValue[VAR];	/* Array of compressed data */
B
Behdad Esfahbod 已提交
825
  public:
826
  DEFINE_SIZE_ARRAY (6, deltaValue);
827 828 829
};


830 831
} // namespace OT

B
Behdad Esfahbod 已提交
832

833
#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */