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