hb-ot-layout-common-private.hh 15.8 KB
Newer Older
1 2
/*
 * Copyright (C) 2007,2008,2009  Red Hat, Inc.
3
 * Copyright (C) 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

34
#include "hb-open-type-private.hh"
35 36


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

B
Behdad Esfahbod 已提交
41 42 43
HB_BEGIN_DECLS
HB_END_DECLS

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 ();
B
Behdad Esfahbod 已提交
65 66
    return c->check_struct (this)
	&& offset.sanitize (c, base);
B
Behdad Esfahbod 已提交
67 68
  }

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

template <typename Type>
B
Behdad Esfahbod 已提交
78
struct RecordArrayOf : SortedArrayOf<Record<Type> > {
B
Behdad Esfahbod 已提交
79 80
  inline const Tag& get_tag (unsigned int i) const
  {
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) {
89
      const Record<Type> *array = this->sub_array (start_offset, record_count);
B
Behdad Esfahbod 已提交
90
      unsigned int count = *record_count;
B
Behdad Esfahbod 已提交
91 92 93 94
      for (unsigned int i = 0; i < count; i++)
	record_tags[i] = array[i].tag;
    }
    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 134 135 136 137 138 139 140 141 142
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);
  }

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


160 161 162 163 164
struct Script;
struct LangSys;
struct Feature;


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

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

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

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


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

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

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

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

typedef RecordListOf<Script> ScriptList;


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

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

B
Behdad Esfahbod 已提交
260 261
  /* LONGTERMTODO: implement get_feature_parameters() */
  /* LONGTERMTODO: implement FeatureSize and other special features? */
262 263 264 265
  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 */
266
  IndexArray	 lookupIndex;	/* Array of LookupList indices */
267
  public:
268
  DEFINE_SIZE_ARRAY (4, lookupIndex);
269 270 271 272 273
};

typedef RecordListOf<Feature> FeatureList;


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

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

  inline unsigned int get_type (void) const { return lookupType; }
295 296 297
  inline unsigned int get_flag (void) const
  {
    unsigned int flag = lookupFlag;
298
    if (unlikely (flag & LookupFlag::UseMarkFilteringSet))
299
    {
300
      const USHORT &markFilteringSet = StructAfter<USHORT> (subTable);
B
Behdad Esfahbod 已提交
301
      flag += (markFilteringSet << 16);
302 303 304
    }
    return flag;
  }
305

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

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

typedef OffsetListOf<Lookup> LookupList;


/*
 * Coverage Table
 */

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

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

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

  private:
356
  USHORT	coverageFormat;	/* Format identifier--format = 1 */
B
Behdad Esfahbod 已提交
357
  SortedArrayOf<GlyphID>
358
		glyphArray;	/* Array of GlyphIDs--in numerical order */
359
  public:
360
  DEFINE_SIZE_ARRAY (4, glyphArray);
361 362
};

B
Behdad Esfahbod 已提交
363 364
struct CoverageFormat2
{
365 366 367
  friend struct Coverage;

  private:
B
Behdad Esfahbod 已提交
368 369
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
370 371 372 373
    int i = rangeRecord.search (glyph_id);
    if (i != -1) {
      const RangeRecord &range = rangeRecord[i];
      return (unsigned int) range.value + (glyph_id - range.start);
374 375 376 377
    }
    return NOT_COVERED;
  }

B
Behdad Esfahbod 已提交
378
  inline bool sanitize (hb_sanitize_context_t *c) {
379
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
380
    return rangeRecord.sanitize (c);
B
Behdad Esfahbod 已提交
381 382 383
  }

  private:
384
  USHORT	coverageFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
385
  SortedArrayOf<RangeRecord>
386 387 388
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID. rangeCount entries
				 * long */
389
  public:
390
  DEFINE_SIZE_ARRAY (4, rangeRecord);
391 392
};

B
Behdad Esfahbod 已提交
393 394
struct Coverage
{
B
Behdad Esfahbod 已提交
395
  inline unsigned int operator () (hb_codepoint_t glyph_id) const { return get_coverage (glyph_id); }
B
Behdad Esfahbod 已提交
396

B
Behdad Esfahbod 已提交
397
  inline unsigned int get_coverage (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
398
  {
399
    switch (u.format) {
B
Behdad Esfahbod 已提交
400 401
    case 1: return u.format1.get_coverage(glyph_id);
    case 2: return u.format2.get_coverage(glyph_id);
402 403 404 405
    default:return NOT_COVERED;
    }
  }

B
Behdad Esfahbod 已提交
406
  inline bool sanitize (hb_sanitize_context_t *c) {
407
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
408
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
409
    switch (u.format) {
B
Behdad Esfahbod 已提交
410 411
    case 1: return u.format1.sanitize (c);
    case 2: return u.format2.sanitize (c);
B
Behdad Esfahbod 已提交
412 413 414
    default:return true;
    }
  }
415 416 417 418

  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
419 420
  CoverageFormat1	format1;
  CoverageFormat2	format2;
421
  } u;
B
Behdad Esfahbod 已提交
422
  public:
B
Behdad Esfahbod 已提交
423
  DEFINE_SIZE_UNION (2, format);
424 425 426 427 428 429 430
};


/*
 * Class Definition Table
 */

B
Behdad Esfahbod 已提交
431 432
struct ClassDefFormat1
{
433 434 435
  friend struct ClassDef;

  private:
B
Behdad Esfahbod 已提交
436 437
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
  {
438 439 440 441 442
    if ((unsigned int) (glyph_id - startGlyph) < classValue.len)
      return classValue[glyph_id - startGlyph];
    return 0;
  }

B
Behdad Esfahbod 已提交
443
  inline bool sanitize (hb_sanitize_context_t *c) {
444
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
445 446
    return c->check_struct (this)
	&& classValue.sanitize (c);
B
Behdad Esfahbod 已提交
447 448
  }

449 450 451 452
  USHORT	classFormat;		/* Format identifier--format = 1 */
  GlyphID	startGlyph;		/* First GlyphID of the classValueArray */
  ArrayOf<USHORT>
		classValue;		/* Array of Class Values--one per GlyphID */
453
  public:
454
  DEFINE_SIZE_ARRAY (6, classValue);
455 456
};

B
Behdad Esfahbod 已提交
457 458
struct ClassDefFormat2
{
459 460 461
  friend struct ClassDef;

  private:
B
Behdad Esfahbod 已提交
462 463
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
  {
B
Behdad Esfahbod 已提交
464 465 466
    int i = rangeRecord.search (glyph_id);
    if (i != -1)
      return rangeRecord[i].value;
467 468 469
    return 0;
  }

B
Behdad Esfahbod 已提交
470
  inline bool sanitize (hb_sanitize_context_t *c) {
471
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
472
    return rangeRecord.sanitize (c);
B
Behdad Esfahbod 已提交
473 474
  }

475
  USHORT	classFormat;	/* Format identifier--format = 2 */
B
Behdad Esfahbod 已提交
476
  SortedArrayOf<RangeRecord>
477 478
		rangeRecord;	/* Array of glyph ranges--ordered by
				 * Start GlyphID */
479
  public:
480
  DEFINE_SIZE_ARRAY (4, rangeRecord);
481 482
};

B
Behdad Esfahbod 已提交
483 484
struct ClassDef
{
B
Behdad Esfahbod 已提交
485
  inline hb_ot_layout_class_t operator () (hb_codepoint_t glyph_id) const { return get_class (glyph_id); }
B
Behdad Esfahbod 已提交
486

B
Behdad Esfahbod 已提交
487
  inline hb_ot_layout_class_t get_class (hb_codepoint_t glyph_id) const
B
Behdad Esfahbod 已提交
488
  {
489
    switch (u.format) {
B
Behdad Esfahbod 已提交
490 491
    case 1: return u.format1.get_class(glyph_id);
    case 2: return u.format2.get_class(glyph_id);
492 493 494 495
    default:return 0;
    }
  }

B
Behdad Esfahbod 已提交
496
  inline bool sanitize (hb_sanitize_context_t *c) {
497
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
498
    if (!u.format.sanitize (c)) return false;
B
Behdad Esfahbod 已提交
499
    switch (u.format) {
B
Behdad Esfahbod 已提交
500 501
    case 1: return u.format1.sanitize (c);
    case 2: return u.format2.sanitize (c);
B
Behdad Esfahbod 已提交
502 503 504
    default:return true;
    }
  }
505

506 507 508
  private:
  union {
  USHORT		format;		/* Format identifier */
B
Behdad Esfahbod 已提交
509 510
  ClassDefFormat1	format1;
  ClassDefFormat2	format2;
511
  } u;
B
Behdad Esfahbod 已提交
512
  public:
B
Behdad Esfahbod 已提交
513
  DEFINE_SIZE_UNION (2, format);
514 515 516 517 518 519 520
};


/*
 * Device Tables
 */

B
Behdad Esfahbod 已提交
521 522
struct Device
{
B
Behdad Esfahbod 已提交
523 524

  inline hb_position_t get_x_delta (hb_ot_layout_context_t *c) const
525
  { return get_delta (c->font->x_ppem, c->font->x_scale); }
B
Behdad Esfahbod 已提交
526 527

  inline hb_position_t get_y_delta (hb_ot_layout_context_t *c) const
528
  { return get_delta (c->font->y_ppem, c->font->y_scale); }
B
Behdad Esfahbod 已提交
529

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  inline int get_delta (unsigned int ppem, unsigned int scale) const
  {
    if (!ppem) return 0;

    int pixels = get_delta_pixels (ppem);

    if (!pixels) return 0;

    /* pixels is at most in the -8..7 range.  So 64-bit arithmetic is
     * not really necessary here.  A simple cast to int may just work
     * as well.  But since this code is not reached that often and
     * for the sake of correctness, we do a 64bit operation. */
    return pixels * (int64_t) scale / ppem;
  }


  inline int get_delta_pixels (unsigned int ppem_size) const
B
Behdad Esfahbod 已提交
547
  {
548
    unsigned int f = deltaFormat;
549
    if (unlikely (f < 1 || f > 3))
550
      return 0;
551

552 553
    if (ppem_size < startSize || ppem_size > endSize)
      return 0;
554

555
    unsigned int s = ppem_size - startSize;
556

557
    unsigned int byte = deltaValue[s >> (4 - f)];
B
Behdad Esfahbod 已提交
558 559
    unsigned int bits = (byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f)));
    unsigned int mask = (0xFFFF >> (16 - (1 << f)));
560 561 562

    int delta = bits & mask;

B
Behdad Esfahbod 已提交
563
    if ((unsigned int) delta >= ((mask + 1) >> 1))
564 565 566 567 568
      delta -= mask + 1;

    return delta;
  }

B
Behdad Esfahbod 已提交
569 570 571
  inline unsigned int get_size () const
  {
    unsigned int f = deltaFormat;
B
Behdad Esfahbod 已提交
572 573
    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 已提交
574 575
  }

B
Behdad Esfahbod 已提交
576
  inline bool sanitize (hb_sanitize_context_t *c) {
577
    TRACE_SANITIZE ();
B
Behdad Esfahbod 已提交
578 579
    return c->check_struct (this)
	&& c->check_range (this, this->get_size ());
B
Behdad Esfahbod 已提交
580
  }
581 582

  private:
B
Behdad Esfahbod 已提交
583 584
  USHORT	startSize;		/* Smallest size to correct--in ppem */
  USHORT	endSize;		/* Largest size to correct--in ppem */
585 586 587 588 589
  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 已提交
590
  USHORT	deltaValue[VAR];	/* Array of compressed data */
B
Behdad Esfahbod 已提交
591
  public:
592
  DEFINE_SIZE_ARRAY (6, deltaValue);
593 594 595
};


B
Behdad Esfahbod 已提交
596 597 598
HB_BEGIN_DECLS
HB_END_DECLS

599
#endif /* HB_OT_LAYOUT_COMMON_PRIVATE_HH */