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