Minor cleanup, add LIKELY and UNLIKELY annotations
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-open-private.h
1 #ifndef HB_OT_LAYOUT_OPEN_PRIVATE_H
2 #define HB_OT_LAYOUT_OPEN_PRIVATE_H
3
4 #include "hb-private.h"
5 #include "hb-ot-layout.h"
6
7 /*
8  * Int types
9  */
10
11 #define DEFINE_INT_TYPE1(NAME, TYPE, BIG_ENDIAN) \
12   inline NAME& operator = (TYPE i) { v = BIG_ENDIAN(i); return *this; } \
13   inline operator TYPE(void) const { return BIG_ENDIAN(v); } \
14   inline bool operator== (NAME o) const { return v == o.v; } \
15   private: TYPE v; \
16   public:
17 #define DEFINE_INT_TYPE0(NAME, type) DEFINE_INT_TYPE1 (NAME, type, hb_be_##type)
18 #define DEFINE_INT_TYPE(NAME, u, w)  DEFINE_INT_TYPE0 (NAME, u##int##w##_t)
19 #define DEFINE_INT_TYPE_STRUCT(NAME, u, w) \
20   struct NAME { \
21     DEFINE_INT_TYPE(NAME, u, w) \
22   }
23
24 /*
25  * Array types
26  */
27
28 /* get_len() is a method returning the number of items in an array-like object */
29 #define DEFINE_LEN(Type, array, num) \
30   inline unsigned int get_len(void) const { return num; } \
31
32 /* get_size() is a method returning the size in bytes of an array-like object */
33 #define DEFINE_SIZE(Type, array, num) \
34   inline unsigned int get_size(void) const { return sizeof (*this) + sizeof (Type) * num; }
35
36 #define DEFINE_LEN_AND_SIZE(Type, array, num) \
37   DEFINE_LEN(Type, array, num) \
38   DEFINE_SIZE(Type, array, num)
39
40 /* An array type is one that contains a variable number of objects
41  * as its last item.  An array object is extended with len() and size()
42  * methods, as well as overloaded [] operator. */
43 #define DEFINE_ARRAY_TYPE(Type, array, num) \
44   DEFINE_INDEX_OPERATOR(Type, array, num) \
45   DEFINE_LEN_AND_SIZE(Type, array, num)
46 #define DEFINE_INDEX_OPERATOR(Type, array, num) \
47   inline const Type& operator[] (unsigned int i) const { \
48     if (HB_UNLIKELY (i >= num)) return Null##Type; \
49     return array[i]; \
50   }
51
52 /* An offset array type is like an array type, but it contains a table
53  * of offsets to the objects, relative to the beginning of the current
54  * object. */
55 #define DEFINE_OFFSET_ARRAY_TYPE(Type, array, num) \
56   DEFINE_OFFSET_INDEX_OPERATOR(Type, array, num) \
57   DEFINE_LEN_AND_SIZE(Offset, array, num)
58 #define DEFINE_OFFSET_INDEX_OPERATOR(Type, array, num) \
59   inline const Type& operator[] (unsigned int i) const { \
60     if (HB_UNLIKELY (i >= num)) return Null##Type; \
61     if (HB_UNLIKELY (!array[i])) return Null##Type; \
62     return *(const Type *)((const char*)this + array[i]); \
63   }
64
65 #define DEFINE_RECORD_ARRAY_TYPE(Type, array, num) \
66   DEFINE_RECORD_ACCESSOR(Type, array, num) \
67   DEFINE_LEN_AND_SIZE(Record, array, num)
68 #define DEFINE_RECORD_ACCESSOR(Type, array, num) \
69   inline const Type& operator[] (unsigned int i) const { \
70     if (HB_UNLIKELY (i >= num)) return Null##Type; \
71     if (HB_UNLIKELY (!array[i].offset)) return Null##Type; \
72     return *(const Type *)((const char*)this + array[i].offset); \
73   } \
74   inline const Tag& get_tag (unsigned int i) const { \
75     if (HB_UNLIKELY (i >= num)) return NullTag; \
76     return array[i].tag; \
77   } \
78   /* TODO: implement find_tag() */
79
80 /*
81  * List types
82  */
83
84 #define DEFINE_LIST_ACCESSOR(Type, name) \
85   inline const Type##List& get_##name##_list (void) const { \
86     if (HB_UNLIKELY (!name##List)) return Null##Type##List; \
87     return *(const Type##List *)((const char*)this + name##List); \
88   } \
89   inline const Type& get_##name (unsigned int i) const { \
90     return get_##name##_list()[i]; \
91   }
92
93 /*
94  * Class features
95  */
96
97 /* makes class uninstantiable.  should be used for union classes that don't
98  * contain any complete type */
99 #define DEFINE_NON_INSTANTIABLE(Type) \
100   private: inline Type() {} /* cannot be instantiated */ \
101   public:
102
103 /* defines Null##Type as a safe nil instance of Type */
104 #define DEFINE_NULL_DATA(Type, size, data) \
105   static const unsigned char Null##Type##Data[size] = data; \
106   DEFINE_NULL_ALIAS (Type, Type)
107 #define DEFINE_NULL(Type, size) \
108         DEFINE_NULL_DATA(Type, size, "")
109 #define DEFINE_NULL_ASSERT_SIZE(Type, size) \
110         DEFINE_NULL_ASSERT_SIZE_DATA(Type, size, "")
111 #define DEFINE_NULL_ASSERT_SIZE_DATA(Type, size, data) \
112   ASSERT_SIZE (Type, size); \
113   DEFINE_NULL_DATA (Type, size, data)
114 #define DEFINE_NULL_ALIAS(NewType, OldType) \
115   static const NewType &Null##NewType = *(NewType *)Null##OldType##Data
116
117 /* get_for_data() is a static class method returning a reference to an
118  * instance of Type located at the input data location.  It's just a
119  * fancy cast! */
120 #define STATIC_DEFINE_GET_FOR_DATA0(const, Type) \
121   static inline const Type& get_for_data (const char *data) { \
122     return *(const Type*)data; \
123   }
124 #define STATIC_DEFINE_GET_FOR_DATA(Type) \
125         STATIC_DEFINE_GET_FOR_DATA0(const, Type) \
126         STATIC_DEFINE_GET_FOR_DATA0(     , Type)
127
128
129 #define DEFINE_ACCESSOR(Type, name, Name) \
130   inline const Type& name (void) const { \
131     if (HB_UNLIKELY (!Name)) return Null##Type; \
132     return *(const Type*)((const char*)this + Name); \
133   }
134
135
136
137 /*
138  *
139  * The OpenType Font File
140  *
141  */
142
143
144
145 /*
146  * Data Types
147  */
148
149
150 /* "The following data types are used in the OpenType font file.
151  *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
152
153
154 DEFINE_INT_TYPE_STRUCT (BYTE,    u,  8);        /*  8-bit unsigned integer. */
155 DEFINE_NULL_ASSERT_SIZE (BYTE, 1);
156 DEFINE_INT_TYPE_STRUCT (CHAR,     ,  8);        /*  8-bit signed integer. */
157 DEFINE_NULL_ASSERT_SIZE (CHAR, 1);
158 DEFINE_INT_TYPE_STRUCT (USHORT,  u, 16);        /* 16-bit unsigned integer. */
159 DEFINE_NULL_ASSERT_SIZE (USHORT, 2);
160 DEFINE_INT_TYPE_STRUCT (SHORT,    , 16);        /* 16-bit signed integer. */
161 DEFINE_NULL_ASSERT_SIZE (SHORT, 2);
162 DEFINE_INT_TYPE_STRUCT (ULONG,   u, 32);        /* 32-bit unsigned integer. */
163 DEFINE_NULL_ASSERT_SIZE (ULONG, 4);
164 DEFINE_INT_TYPE_STRUCT (LONG,     , 32);        /* 32-bit signed integer. */
165 DEFINE_NULL_ASSERT_SIZE (LONG, 4);
166
167 /* Date represented in number of seconds since 12:00 midnight, January 1,
168  * 1904. The value is represented as a signed 64-bit integer. */
169 DEFINE_INT_TYPE_STRUCT (LONGDATETIME, , 64);
170
171 /* 32-bit signed fixed-point number (16.16) */
172 struct Fixed {
173   inline Fixed& operator = (int32_t v) { i = (int16_t) (v >> 16); f = (uint16_t) v; return *this; } \
174   inline operator int32_t(void) const { return (((int32_t) i) << 16) + (uint16_t) f; } \
175   inline bool operator== (Fixed o) const { return i == o.i && f == o.f; } \
176
177   inline operator double(void) const { return (uint32_t) this / 65536.; }
178   inline int16_t int_part (void) const { return i; }
179   inline uint16_t frac_part (void) const { return f; }
180
181   private:
182   SHORT i;
183   USHORT f;
184 };
185 DEFINE_NULL_ASSERT_SIZE (Fixed, 4);
186
187 /* Smallest measurable distance in the em space. */
188 struct FUNIT;
189
190 /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */
191 struct FWORD : SHORT {
192 };
193 DEFINE_NULL_ASSERT_SIZE (FWORD, 2);
194
195 /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */
196 struct UFWORD : USHORT {
197 };
198 DEFINE_NULL_ASSERT_SIZE (UFWORD, 2);
199
200 /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
201 struct F2DOT14 : SHORT {
202   inline operator double() const { return (uint32_t) this / 16384.; }
203 };
204 DEFINE_NULL_ASSERT_SIZE (F2DOT14, 2);
205
206 /* Array of four uint8s (length = 32 bits) used to identify a script, language
207  * system, feature, or baseline */
208 struct Tag {
209   inline Tag (void) { v[0] = v[1] = v[2] = v[3] = 0; }
210   inline Tag (uint32_t v) { (ULONG&)(*this) = v; }
211   inline Tag (const char *c) { v[0] = c[0]; v[1] = c[1]; v[2] = c[2]; v[3] = c[3]; }
212   inline bool operator== (Tag o) const { return v[0]==o.v[0]&&v[1]==o.v[1]&&v[2]==o.v[2]&&v[3]==o.v[3]; }
213   inline bool operator== (const char *c) const { return v[0]==c[0]&&v[1]==c[1]&&v[2]==c[2]&&v[3]==c[3]; }
214   inline bool operator== (uint32_t i) const { return i == (uint32_t) *this; }
215   inline operator uint32_t(void) const { return (v[0]<<24)+(v[1]<<16) +(v[2]<<8)+v[3]; }
216   /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
217   inline operator const char* (void) const { return (const char *)this; }
218   inline operator char* (void) { return (char *)this; }
219
220   private:
221   char v[4];
222 };
223 ASSERT_SIZE (Tag, 4);
224 DEFINE_NULL_DATA (Tag, 5, "    ");
225
226 /* Glyph index number, same as uint16 (length = 16 bits) */
227 DEFINE_INT_TYPE_STRUCT (GlyphID, u, 16);
228 DEFINE_NULL_ASSERT_SIZE (GlyphID, 2);
229
230 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
231 DEFINE_INT_TYPE_STRUCT (Offset, u, 16);
232 DEFINE_NULL_ASSERT_SIZE (Offset, 2);
233
234 /* CheckSum */
235 struct CheckSum : ULONG {
236   static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length) {
237     uint32_t Sum = 0L;
238     ULONG *EndPtr = Table+((Length+3) & ~3) / sizeof(ULONG);
239
240     while (Table < EndPtr)
241       Sum += *Table++;
242     return Sum;
243   }
244 };
245 DEFINE_NULL_ASSERT_SIZE (CheckSum, 4);
246
247
248 /*
249  * Version Numbers
250  */
251
252 struct USHORT_Version : USHORT {
253 };
254 DEFINE_NULL_ASSERT_SIZE (USHORT_Version, 2);
255
256 struct Fixed_Version : Fixed {
257   inline int16_t major (void) const { return this->int_part(); }
258   inline int16_t minor (void) const { return this->frac_part(); }
259 };
260 DEFINE_NULL_ASSERT_SIZE (Fixed_Version, 4);
261
262
263 /*
264  * Organization of an OpenType Font
265  */
266
267 struct OpenTypeFontFile;
268 struct OffsetTable;
269 struct TTCHeader;
270
271 typedef struct TableDirectory {
272
273   friend struct OpenTypeFontFile;
274
275   inline const Tag& get_tag (void) const { return tag; }
276   inline unsigned long get_checksum (void) const { return checkSum; }
277   inline unsigned long get_offset (void) const { return offset; }
278   inline unsigned long get_length (void) const { return length; }
279
280   private:
281   Tag           tag;            /* 4-byte identifier. */
282   CheckSum      checkSum;       /* CheckSum for this table. */
283   ULONG         offset;         /* Offset from beginning of TrueType font
284                                  * file. */
285   ULONG         length;         /* Length of this table. */
286 } OpenTypeTable;
287 DEFINE_NULL_ASSERT_SIZE (TableDirectory, 16);
288
289 typedef struct OffsetTable {
290   /* OpenTypeTables, in no particular order */
291   DEFINE_ARRAY_TYPE (TableDirectory, tableDir, numTables);
292   // TODO: Implement find_table
293
294   private:
295   Tag           sfnt_version;   /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
296   USHORT        numTables;      /* Number of tables. */
297   USHORT        searchRange;    /* (Maximum power of 2 <= numTables) x 16 */
298   USHORT        entrySelector;  /* Log2(maximum power of 2 <= numTables). */
299   USHORT        rangeShift;     /* NumTables x 16-searchRange. */
300   TableDirectory tableDir[];    /* TableDirectory entries. numTables items */
301 } OpenTypeFontFace;
302 DEFINE_NULL_ASSERT_SIZE (OffsetTable, 12);
303 DEFINE_NULL_ALIAS (OpenTypeFontFace, OffsetTable);
304
305 /*
306  * TrueType Collections
307  */
308
309 struct TTCHeader {
310   /* OpenTypeFontFaces, in no particular order */
311   DEFINE_OFFSET_ARRAY_TYPE (OffsetTable, offsetTable, numFonts);
312
313   private:
314   Tag   ttcTag;         /* TrueType Collection ID string: 'ttcf' */
315   ULONG version;        /* Version of the TTC Header (1.0 or 2.0),
316                          * 0x00010000 or 0x00020000 */
317   ULONG numFonts;       /* Number of fonts in TTC */
318   ULONG offsetTable[];  /* Array of offsets to the OffsetTable for each font
319                          * from the beginning of the file */
320 };
321 DEFINE_NULL_ASSERT_SIZE (TTCHeader, 12);
322
323
324 /*
325  * OpenType Font File
326  */
327
328 struct OpenTypeFontFile {
329   DEFINE_NON_INSTANTIABLE(OpenTypeFontFile);
330   static const hb_tag_t TrueTypeTag     = HB_TAG ( 0 , 1 , 0 , 0 );
331   static const hb_tag_t CFFTag          = HB_TAG ('O','T','T','O');
332   static const hb_tag_t TTCTag          = HB_TAG ('t','t','c','f');
333
334   STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
335
336   /* This is how you get a table */
337   inline const char* get_table (const OpenTypeTable& table) const {
338     return ((const char*)this) + table.offset;
339   }
340   inline char* get_table (const OpenTypeTable& table) {
341     return ((char*)this) + table.offset;
342   }
343   inline const char* operator[] (const OpenTypeTable& table) const {
344     return ((const char*)this) + table.offset;
345   }
346   inline char* operator[] (const OpenTypeTable& table) {
347     return ((char*)this) + table.offset;
348   }
349
350   /* Array interface sans get_size() */
351   inline unsigned int get_len (void) const {
352     switch (tag) {
353     default: return 0;
354     case TrueTypeTag: case CFFTag: return 1;
355     case TTCTag: return ((const TTCHeader&)*this).get_len();
356     }
357   }
358   inline const OpenTypeFontFace& operator[] (unsigned int i) const {
359     if (HB_UNLIKELY (i >= get_len ())) return NullOpenTypeFontFace;
360     switch (tag) {
361     default: case TrueTypeTag: case CFFTag: return (const OffsetTable&)*this;
362     case TTCTag: return ((const TTCHeader&)*this)[i];
363     }
364   }
365   inline const Tag& get_tag (void) const { return tag; }
366
367   private:
368   Tag           tag;            /* 4-byte identifier. */
369 };
370 DEFINE_NULL_ASSERT_SIZE (OpenTypeFontFile, 4);
371
372
373
374 /*
375  *
376  * OpenType Layout Common Table Formats
377  *
378  */
379
380 /*
381  * Script, ScriptList, LangSys, Feature, FeatureList, Lookup, LookupList
382  */
383
384 typedef struct Record {
385   Tag           tag;            /* 4-byte Tag identifier */
386   Offset        offset;         /* Offset from beginning of object holding
387                                  * the Record */
388 } ScriptRecord, LangSysRecord, FeatureRecord;
389 DEFINE_NULL_ASSERT_SIZE (Record, 6);
390
391 struct LangSys {
392   /* Feature indices, in no particular order */
393   DEFINE_ARRAY_TYPE (USHORT, featureIndex, featureCount);
394   
395   /* Returns -1 if none */
396   inline int get_required_feature_index (void) const {
397     if (reqFeatureIndex == 0xffff)
398       return -1;
399     return reqFeatureIndex;;
400   }
401
402   /* TODO implement find_feature */
403
404   private:
405   Offset        lookupOrder;    /* = Null (reserved for an offset to a
406                                  * reordering table) */
407   USHORT        reqFeatureIndex;/* Index of a feature required for this
408                                  * language system--if no required features
409                                  * = 0xFFFF */
410   USHORT        featureCount;   /* Number of FeatureIndex values for this
411                                  * language system--excludes the required
412                                  * feature */
413   USHORT        featureIndex[]; /* Array of indices into the FeatureList--in
414                                  * arbitrary order. featureCount entires long */
415 };
416 DEFINE_NULL_ASSERT_SIZE_DATA (LangSys, 6, "\0\0\xFF\xFF");
417
418 struct Script {
419   /* LangSys', in sorted alphabetical tag order */
420   DEFINE_RECORD_ARRAY_TYPE (LangSys, langSysRecord, langSysCount);
421
422   inline const bool has_default_language_system (void) const {
423     return defaultLangSys != 0;
424   }
425   inline const LangSys& get_default_language_system (void) const {
426     if (HB_UNLIKELY (!defaultLangSys))
427       return NullLangSys;
428     return *(LangSys*)((const char*)this + defaultLangSys);
429   }
430
431   /* TODO implement find_language_system based on find_tag */
432
433   private:
434   Offset        defaultLangSys; /* Offset to DefaultLangSys table--from
435                                  * beginning of Script table--may be Null */
436   USHORT        langSysCount;   /* Number of LangSysRecords for this script--
437                                  * excluding the DefaultLangSys */
438   LangSysRecord langSysRecord[];/* Array of LangSysRecords--listed
439                                  * alphabetically by LangSysTag */
440 };
441 DEFINE_NULL_ASSERT_SIZE (Script, 4);
442
443 struct ScriptList {
444   /* Scripts, in sorted alphabetical tag order */
445   DEFINE_RECORD_ARRAY_TYPE (Script, scriptRecord, scriptCount);
446
447 private:
448   USHORT        scriptCount;    /* Number of ScriptRecords */
449   ScriptRecord  scriptRecord[]; /* Array of ScriptRecords--listed alphabetically
450                                  * by ScriptTag */
451 };
452 DEFINE_NULL_ASSERT_SIZE (ScriptList, 2);
453
454 struct Feature {
455   /* LookupList indices, in no particular order */
456   DEFINE_ARRAY_TYPE (USHORT, lookupIndex, lookupCount);
457
458   private:
459   Offset        featureParams;  /* Offset to Feature Parameters table (if one
460                                  * has been defined for the feature), relative
461                                  * to the beginning of the Feature Table; = Null 
462                                  * if not required */
463   USHORT        lookupCount;    /* Number of LookupList indices for this
464                                  * feature */
465   USHORT        lookupIndex[];  /* Array of LookupList indices for this
466                                  * feature--zero-based (first lookup is
467                                  * LookupListIndex = 0) */
468 };
469 DEFINE_NULL_ASSERT_SIZE (Feature, 4);
470
471 struct FeatureList {
472   /* Feature indices, in sorted alphabetical tag order */
473   DEFINE_RECORD_ARRAY_TYPE (Feature, featureRecord, featureCount);
474
475   private:
476   USHORT        featureCount;   /* Number of FeatureRecords in this table */
477   FeatureRecord featureRecord[];/* Array of FeatureRecords--zero-based (first
478                                  * feature has FeatureIndex = 0)--listed
479                                  * alphabetically by FeatureTag */
480 };
481 DEFINE_NULL_ASSERT_SIZE (FeatureList, 2);
482
483 struct LookupFlag : USHORT {
484   static const uint16_t RightToLeft             = 0x0001u;
485   static const uint16_t IgnoreBaseGlyphs        = 0x0002u;
486   static const uint16_t IgnoreLigatures         = 0x0004u;
487   static const uint16_t IgnoreMarks             = 0x0008u;
488   static const uint16_t Reserved                = 0x00F0u;
489   static const uint16_t MarkAttachmentType      = 0xFF00u;
490 };
491 DEFINE_NULL_ASSERT_SIZE (LookupFlag, 2);
492
493 struct LookupSubTable {
494   DEFINE_NON_INSTANTIABLE(LookupSubTable);
495
496   private:
497   USHORT        format;         /* Subtable format.  Different for GSUB and GPOS */
498 };
499 DEFINE_NULL_ASSERT_SIZE (LookupSubTable, 2);
500
501
502 struct Lookup {
503   /* SubTables, in the desired order */
504   DEFINE_OFFSET_ARRAY_TYPE (LookupSubTable, subTableOffset, subTableCount);
505
506   inline bool is_right_to_left  (void) const { return lookupFlag & LookupFlag::RightToLeft; }
507   inline bool ignore_base_glyphs(void) const { return lookupFlag & LookupFlag::IgnoreBaseGlyphs; }
508   inline bool ignore_ligatures  (void) const { return lookupFlag & LookupFlag::IgnoreLigatures; }
509   inline bool ignore_marks      (void) const { return lookupFlag & LookupFlag::IgnoreMarks; }
510   inline bool get_mark_attachment_type (void) const { return lookupFlag & LookupFlag::MarkAttachmentType; }
511
512   inline uint16_t get_type (void) const { return lookupType; }
513   inline uint16_t get_flag (void) const { return lookupFlag; }
514
515   private:
516   USHORT        lookupType;     /* Different enumerations for GSUB and GPOS */
517   USHORT        lookupFlag;     /* Lookup qualifiers */
518   USHORT        subTableCount;  /* Number of SubTables for this lookup */
519   Offset        subTableOffset[];/* Array of offsets to SubTables-from
520                                   * beginning of Lookup table */
521 };
522 DEFINE_NULL_ASSERT_SIZE (Lookup, 6);
523
524 struct LookupList {
525   /* Lookup indices, in sorted alphabetical tag order */
526   DEFINE_OFFSET_ARRAY_TYPE (Lookup, lookupOffset, lookupCount);
527
528   private:
529   USHORT        lookupCount;    /* Number of lookups in this table */
530   Offset        lookupOffset[]; /* Array of offsets to Lookup tables--from
531                                  * beginning of LookupList--zero based (first
532                                  * lookup is Lookup index = 0) */
533 };
534 DEFINE_NULL_ASSERT_SIZE (LookupList, 2);
535
536 /*
537  * Coverage Table
538  */
539
540 struct CoverageFormat1 {
541   /* GlyphIDs, in sorted numerical order */
542   DEFINE_ARRAY_TYPE (GlyphID, glyphArray, glyphCount);
543
544   inline int get_coverage (uint16_t glyph_id) const {
545     GlyphID gid;
546     gid = glyph_id;
547     // TODO: bsearch
548     for (unsigned int i = 0; i < glyphCount; i++)
549       if (gid == glyphArray[i])
550         return i;
551     return -1;
552   }
553
554   private:
555   USHORT        coverageFormat; /* Format identifier--format = 1 */
556   USHORT        glyphCount;     /* Number of glyphs in the GlyphArray */
557   GlyphID       glyphArray[];   /* Array of GlyphIDs--in numerical order */
558 };
559 ASSERT_SIZE (CoverageFormat1, 4);
560
561 struct CoverageRangeRecord {
562
563   inline int get_coverage (uint16_t glyph_id) const {
564     if (glyph_id >= start && glyph_id <= end)
565       return startCoverageIndex + (glyph_id - start);
566     return -1;
567   }
568
569   private:
570   GlyphID       start;                  /* First GlyphID in the range */
571   GlyphID       end;                    /* Last GlyphID in the range */
572   USHORT        startCoverageIndex;     /* Coverage Index of first GlyphID in
573                                          * range */
574 };
575 DEFINE_NULL_ASSERT_SIZE_DATA (CoverageRangeRecord, 6, "\001");
576
577 struct CoverageFormat2 {
578   /* CoverageRangeRecords, in sorted numerical start order */
579   DEFINE_ARRAY_TYPE (CoverageRangeRecord, rangeRecord, rangeCount);
580
581   inline int get_coverage (uint16_t glyph_id) const {
582     // TODO: bsearch
583     for (unsigned int i = 0; i < rangeCount; i++) {
584       int coverage = rangeRecord[i].get_coverage (glyph_id);
585       if (coverage >= 0)
586         return coverage;
587     }
588     return -1;
589   }
590
591   private:
592   USHORT                coverageFormat; /* Format identifier--format = 2 */
593   USHORT                rangeCount;     /* Number of CoverageRangeRecords */
594   CoverageRangeRecord   rangeRecord[];  /* Array of glyph ranges--ordered by
595                                          * Start GlyphID. rangeCount entries
596                                          * long */
597 };
598 ASSERT_SIZE (CoverageFormat2, 4);
599
600 struct Coverage {
601   DEFINE_NON_INSTANTIABLE(Coverage);
602
603   inline unsigned int get_size (void) const {
604     switch (u.coverageFormat) {
605     case 1: return u.format1.get_size ();
606     case 2: return u.format2.get_size ();
607     default:return sizeof (u.coverageFormat);
608     }
609   }
610
611   /* Returns -1 if not covered. */
612   inline int get_coverage (uint16_t glyph_id) const {
613     switch (u.coverageFormat) {
614     case 1: return u.format1.get_coverage(glyph_id);
615     case 2: return u.format2.get_coverage(glyph_id);
616     default:return -1;
617     }
618   }
619
620   private:
621   union {
622   USHORT                coverageFormat; /* Format identifier */
623   CoverageFormat1       format1;
624   CoverageFormat2       format2;
625   } u;
626 };
627 DEFINE_NULL (Coverage, 2);
628
629 /*
630  * Class Definition Table
631  */
632
633 struct ClassDefFormat1 {
634   /* GlyphIDs, in sorted numerical order */
635   DEFINE_ARRAY_TYPE (USHORT, classValueArray, glyphCount);
636
637   inline int get_class (uint16_t glyph_id) const {
638     if (glyph_id >= startGlyph && glyph_id < startGlyph + glyphCount)
639       return classValueArray[glyph_id - startGlyph];
640     return 0;
641   }
642
643   private:
644   USHORT        classFormat;            /* Format identifier--format = 1 */
645   GlyphID       startGlyph;             /* First GlyphID of the classValueArray */
646   USHORT        glyphCount;             /* Size of the classValueArray */
647   USHORT        classValueArray[];      /* Array of Class Values--one per GlyphID */
648 };
649 ASSERT_SIZE (ClassDefFormat1, 6);
650
651 struct ClassRangeRecord {
652
653   inline int get_class (uint16_t glyph_id) const {
654     if (glyph_id >= start && glyph_id <= end)
655       return classValue;
656     return 0;
657   }
658
659   private:
660   GlyphID       start;          /* First GlyphID in the range */
661   GlyphID       end;            /* Last GlyphID in the range */
662   USHORT        classValue;     /* Applied to all glyphs in the range */
663 };
664 DEFINE_NULL_ASSERT_SIZE_DATA (ClassRangeRecord, 6, "\001");
665
666 struct ClassDefFormat2 {
667   /* ClassRangeRecords, in sorted numerical start order */
668   DEFINE_ARRAY_TYPE (ClassRangeRecord, rangeRecord, rangeCount);
669
670   inline int get_class (uint16_t glyph_id) const {
671     // TODO: bsearch
672     for (int i = 0; i < rangeCount; i++) {
673       int classValue = rangeRecord[i].get_class (glyph_id);
674       if (classValue > 0)
675         return classValue;
676     }
677     return 0;
678   }
679
680   private:
681   USHORT                classFormat;    /* Format identifier--format = 2 */
682   USHORT                rangeCount;     /* Number of Number of ClassRangeRecords */
683   ClassRangeRecord      rangeRecord[];  /* Array of glyph ranges--ordered by
684                                          * Start GlyphID */
685 };
686 ASSERT_SIZE (ClassDefFormat2, 4);
687
688 struct ClassDef {
689   DEFINE_NON_INSTANTIABLE(ClassDef);
690
691   inline unsigned int get_size (void) const {
692     switch (u.classFormat) {
693     case 1: return u.format1.get_size ();
694     case 2: return u.format2.get_size ();
695     default:return sizeof (u.classFormat);
696     }
697   }
698
699   /* Returns 0 if not found. */
700   inline int get_class (uint16_t glyph_id) const {
701     switch (u.classFormat) {
702     case 1: return u.format1.get_class(glyph_id);
703     case 2: return u.format2.get_class(glyph_id);
704     default:return 0;
705     }
706   }
707
708   private:
709   union {
710   USHORT                classFormat;    /* Format identifier */
711   ClassDefFormat1       format1;
712   ClassDefFormat2       format2;
713   } u;
714 };
715 DEFINE_NULL (ClassDef, 2);
716
717 /*
718  * Device Tables
719  */
720
721 struct Device {
722
723   inline unsigned int get_size (void) const {
724     int count = endSize - startSize + 1;
725     if (count < 0) count = 0;
726     switch (deltaFormat) {
727     case 1: return sizeof (Device) + sizeof (USHORT) * ((count+7)/8);
728     case 2: return sizeof (Device) + sizeof (USHORT) * ((count+3)/4);
729     case 3: return sizeof (Device) + sizeof (USHORT) * ((count+1)/2);
730     default:return sizeof (Device);
731     }
732   }
733
734   inline int get_delta (int ppem_size) const {
735     if (ppem_size >= startSize && ppem_size <= endSize &&
736         deltaFormat >= 1 && deltaFormat <= 3) {
737       int s = ppem_size - startSize;
738       int f = deltaFormat;
739
740       uint16_t byte = deltaValue[s >> (4 - f)];
741       uint16_t bits = byte >> (16 - (((s & ((1 << (4 - f)) - 1)) + 1) << f));
742       uint16_t mask = 0xFFFF >> (16 - (1 << f));
743       
744       int delta = bits & mask;
745
746       if (delta >= ((mask + 1) >> 1))
747         delta -= mask + 1;
748
749       return delta;
750     }
751     return 0;
752   }
753
754   private:
755   USHORT        startSize;      /* Smallest size to correct--in ppem */
756   USHORT        endSize;        /* Largest size to correct--in ppem */
757   USHORT        deltaFormat;    /* Format of DeltaValue array data: 1, 2, or 3 */
758   USHORT        deltaValue[];   /* Array of compressed data */
759 };
760 DEFINE_NULL_ASSERT_SIZE (Device, 6);
761
762 /*
763  * GSUB/GPOS Header
764  */
765
766 typedef struct GSUBGPOSHeader {
767   static const hb_tag_t GSUBTag         = HB_TAG ('G','S','U','B');
768   static const hb_tag_t GPOSTag         = HB_TAG ('G','P','O','S');
769
770   STATIC_DEFINE_GET_FOR_DATA (GSUBGPOSHeader);
771
772   DEFINE_LIST_ACCESSOR(Script, script);  /* get_script_list, get_script(i) */
773   DEFINE_LIST_ACCESSOR(Feature, feature);/* get_feature_list, get_feature(i) */
774   DEFINE_LIST_ACCESSOR(Lookup, lookup);  /* get_lookup_list, get_lookup(i) */
775
776   /* TODO implement find_script */
777
778   private:
779   Fixed_Version version;        /* Version of the GSUB/GPOS table--initially set
780                                  * to 0x00010000 */
781   Offset        scriptList;     /* Offset to ScriptList table--from beginning of
782                                  * GSUB/GPOS table */
783   Offset        featureList;    /* Offset to FeatureList table--from beginning of
784                                  * GSUB/GPOS table */
785   Offset        lookupList;     /* Offset to LookupList table--from beginning of
786                                  * GSUB/GPOS table */
787 } GSUBHeader, GPOSHeader;
788 DEFINE_NULL_ASSERT_SIZE (GSUBGPOSHeader, 10);
789 DEFINE_NULL_ALIAS (GSUBHeader, GSUBGPOSHeader);
790 DEFINE_NULL_ALIAS (GPOSHeader, GSUBGPOSHeader);
791
792 #endif /* HB_OT_LAYOUT_OPEN_PRIVATE_H */