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