[HB] Fix context_length checking
[framework/uifw/harfbuzz.git] / src / hb-ot-layout-open-private.h
1 /*
2  * Copyright (C) 2007,2008,2009  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 #ifndef HB_OT_LAYOUT_CC
31 #error "This file should only be included from hb-ot-layout.cc"
32 #endif
33
34 #include "hb-ot-layout-private.h"
35
36
37 #define NO_INDEX                ((unsigned int) 0xFFFF)
38 #define NO_CONTEXT              ((unsigned int) -0x20000)
39 #define NOT_COVERED             ((unsigned int) -1)
40 #define MAX_NESTING_LEVEL       32
41
42
43 /*
44  * Array types
45  */
46
47 /* get_len() is a method returning the number of items in an array-like object */
48 #define DEFINE_LEN(Type, array, num) \
49   inline unsigned int get_len(void) const { return num; } \
50
51 /* An array type is one that contains a variable number of objects
52  * as its last item.  An array object is extended with get_len()
53  * methods, as well as overloaded [] operator. */
54 #define DEFINE_ARRAY_TYPE(Type, array, num) \
55   DEFINE_INDEX_OPERATOR(Type, array, num) \
56   DEFINE_LEN(Type, array, num)
57 #define DEFINE_INDEX_OPERATOR(Type, array, num) \
58   inline const Type& operator[] (unsigned int i) const { \
59     if (HB_UNLIKELY (i >= num)) return Null(Type); \
60     return array[i]; \
61   }
62
63 /* An offset array type is like an array type, but it contains a table
64  * of offsets to the objects, relative to the beginning of the current
65  * object. */
66 #define DEFINE_OFFSET_ARRAY_TYPE(Type, array, num) \
67   DEFINE_OFFSET_INDEX_OPERATOR(Type, array, num) \
68   DEFINE_LEN(Offset, array, num)
69 #define DEFINE_OFFSET_INDEX_OPERATOR(Type, array, num) \
70   inline const Type& operator[] (unsigned int i) const { \
71     if (HB_UNLIKELY (i >= num)) return Null(Type); \
72     if (HB_UNLIKELY (!array[i])) return Null(Type); \
73     return *(const Type *)((const char*)this + array[i]); \
74   }
75
76
77 #define DEFINE_ARRAY_INTERFACE(Type, name) \
78   inline const Type& get_##name (unsigned int i) const { \
79     return (*this)[i]; \
80   } \
81   inline unsigned int get_##name##_count (void) const { \
82     return this->get_len (); \
83   }
84 #define DEFINE_INDEX_ARRAY_INTERFACE(name) \
85   inline unsigned int get_##name##_index (unsigned int i) const { \
86     if (HB_UNLIKELY (i >= get_len ())) return NO_INDEX; \
87     return (*this)[i]; \
88   } \
89   inline unsigned int get_##name##_count (void) const { \
90     return get_len (); \
91   }
92
93
94 /*
95  * List types
96  */
97
98 #define DEFINE_LIST_INTERFACE(Type, name) \
99   inline const Type& get_##name (unsigned int i) const { \
100     return (this+name##List)[i]; \
101   } \
102   inline unsigned int get_##name##_count (void) const { \
103     return (this+name##List).len; \
104   }
105
106 /*
107  * Tag types
108  */
109
110 #define DEFINE_TAG_ARRAY_INTERFACE(Type, name) \
111   DEFINE_ARRAY_INTERFACE (Type, name); \
112   inline const Tag& get_##name##_tag (unsigned int i) const { \
113     return (*this)[i].tag; \
114   }
115 #define DEFINE_TAG_LIST_INTERFACE(Type, name) \
116   DEFINE_LIST_INTERFACE (Type, name); \
117   inline const Tag& get_##name##_tag (unsigned int i) const { \
118     return (this+name##List).get_tag (i); \
119   }
120
121 #define DEFINE_TAG_FIND_INTERFACE(Type, name) \
122   inline bool find_##name##_index (hb_tag_t tag, unsigned int *name##_index) const { \
123     const Tag t = tag; \
124     for (unsigned int i = 0; i < get_##name##_count (); i++) { \
125       if (t == get_##name##_tag (i)) { \
126         if (name##_index) *name##_index = i; \
127         return true; \
128       } \
129     } \
130     if (name##_index) *name##_index = NO_INDEX; \
131     return false; \
132   } \
133   inline const Type& get_##name##_by_tag (hb_tag_t tag) const { \
134     unsigned int i; \
135     if (find_##name##_index (tag, &i)) \
136       return get_##name (i); \
137     else \
138       return Null(Type); \
139   }
140
141 /*
142  * Class features
143  */
144
145
146 /* Null objects */
147
148 /* Global nul-content Null pool.  Enlarge as necessary. */
149 static const char NullPool[16] = "";
150
151 /* Generic template for nul-content sizeof-sized Null objects. */
152 template <typename Type>
153 struct Null {
154   ASSERT_STATIC (sizeof (Type) <= sizeof (NullPool));
155   static inline const Type &get () { return (const Type&) *(const Type*) NullPool; }
156 };
157
158 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
159 #define DEFINE_NULL_DATA(Type, size, data) \
160 template <> \
161 struct Null <Type> { \
162   static inline const Type &get () { static const char bytes[size] = data; return (const Type&) *(const Type*) bytes; } \
163 }
164
165 /* Accessor macro. */
166 #define Null(Type) (Null<Type>::get())
167
168
169 #define ASSERT_SIZE_DATA(Type, size, data) \
170   ASSERT_SIZE (Type, size); \
171   DEFINE_NULL_DATA (Type, size, data)
172
173 /* get_for_data() is a static class method returning a reference to an
174  * instance of Type located at the input data location.  It's just a
175  * fancy, NULL-safe, cast! */
176 #define STATIC_DEFINE_GET_FOR_DATA(Type) \
177   static inline const Type& get_for_data (const char *data) { \
178     if (HB_UNLIKELY (data == NULL)) return Null(Type); \
179     return *(const Type*)data; \
180   } \
181   static inline Type& get_for_data (char *data) { \
182     return *(Type*)data; \
183   }
184
185
186
187 /*
188  *
189  * The OpenType Font File
190  *
191  */
192
193
194 /*
195  * Data Types
196  */
197
198
199 /* "The following data types are used in the OpenType font file.
200  *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
201
202 /*
203  * Int types
204  */
205
206 /* XXX define these as structs of chars on machines that do not allow
207  * unaligned access (using templates?). */
208 #define DEFINE_INT_TYPE1(NAME, TYPE, BIG_ENDIAN) \
209   inline NAME& operator = (TYPE i) { v = BIG_ENDIAN(i); return *this; } \
210   inline operator TYPE(void) const { return BIG_ENDIAN(v); } \
211   inline bool operator== (NAME o) const { return v == o.v; } \
212   private: TYPE v; \
213   public:
214 #define DEFINE_INT_TYPE0(NAME, type) DEFINE_INT_TYPE1 (NAME, type, hb_be_##type)
215 #define DEFINE_INT_TYPE(NAME, u, w)  DEFINE_INT_TYPE0 (NAME, u##int##w##_t)
216 #define DEFINE_INT_TYPE_STRUCT(NAME, u, w) \
217   struct NAME { \
218     DEFINE_INT_TYPE(NAME, u, w) \
219   }; \
220   ASSERT_SIZE (NAME, w / 8)
221
222
223 DEFINE_INT_TYPE_STRUCT (BYTE,    u,  8);        /*  8-bit unsigned integer. */
224 DEFINE_INT_TYPE_STRUCT (CHAR,     ,  8);        /*  8-bit signed integer. */
225 DEFINE_INT_TYPE_STRUCT (USHORT,  u, 16);        /* 16-bit unsigned integer. */
226 DEFINE_INT_TYPE_STRUCT (SHORT,    , 16);        /* 16-bit signed integer. */
227 DEFINE_INT_TYPE_STRUCT (ULONG,   u, 32);        /* 32-bit unsigned integer. */
228 DEFINE_INT_TYPE_STRUCT (LONG,     , 32);        /* 32-bit signed integer. */
229
230 /* Date represented in number of seconds since 12:00 midnight, January 1,
231  * 1904. The value is represented as a signed 64-bit integer. */
232 DEFINE_INT_TYPE_STRUCT (LONGDATETIME, , 64);
233
234 /* 32-bit signed fixed-point number (16.16) */
235 struct Fixed {
236   inline Fixed& operator = (int32_t v) { i = (int16_t) (v >> 16); f = (uint16_t) v; return *this; } \
237   inline operator int32_t(void) const { return (((int32_t) i) << 16) + (uint16_t) f; } \
238   inline bool operator== (Fixed o) const { return i == o.i && f == o.f; } \
239
240   inline operator double(void) const { return (uint32_t) this / 65536.; }
241   inline int16_t int_part (void) const { return i; }
242   inline uint16_t frac_part (void) const { return f; }
243
244   private:
245   SHORT i;
246   USHORT f;
247 };
248 ASSERT_SIZE (Fixed, 4);
249
250 /* Smallest measurable distance in the em space. */
251 struct FUNIT;
252
253 /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */
254 struct FWORD : SHORT {
255 };
256 ASSERT_SIZE (FWORD, 2);
257
258 /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */
259 struct UFWORD : USHORT {
260 };
261 ASSERT_SIZE (UFWORD, 2);
262
263 /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
264 struct F2DOT14 : SHORT {
265   inline operator double() const { return (uint32_t) this / 16384.; }
266 };
267 ASSERT_SIZE (F2DOT14, 2);
268
269 /* Array of four uint8s (length = 32 bits) used to identify a script, language
270  * system, feature, or baseline */
271 struct Tag {
272   inline Tag (void) { v[0] = v[1] = v[2] = v[3] = 0; }
273   inline Tag (uint32_t v) { (ULONG&)(*this) = v; }
274   inline Tag (const char *c) { v[0] = c[0]; v[1] = c[1]; v[2] = c[2]; v[3] = c[3]; }
275   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]; }
276   inline bool operator== (const char *c) const { return v[0]==c[0]&&v[1]==c[1]&&v[2]==c[2]&&v[3]==c[3]; }
277   inline bool operator== (uint32_t i) const { return i == (uint32_t) *this; }
278   inline operator uint32_t(void) const { return (v[0]<<24)+(v[1]<<16) +(v[2]<<8)+v[3]; }
279   /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
280   inline operator const char* (void) const { return (const char *)this; }
281   inline operator char* (void) { return (char *)this; }
282
283   private:
284   char v[4];
285 };
286 ASSERT_SIZE (Tag, 4);
287 #define _NULL_TAG_INIT  {' ', ' ', ' ', ' '}
288 DEFINE_NULL_DATA (Tag, 4, _NULL_TAG_INIT);
289 #undef _NULL_TAG_INIT
290
291 /* Glyph index number, same as uint16 (length = 16 bits) */
292 DEFINE_INT_TYPE_STRUCT (GlyphID, u, 16);
293
294 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
295 DEFINE_INT_TYPE_STRUCT (Offset, u, 16);
296
297 /* Template subclass of Offset that does the dereferencing.  Use: (this+memberName) */
298 template <typename Type>
299 struct OffsetTo : Offset {
300   inline const Type& operator() (const void *base) const {
301     unsigned int offset = *this;
302     if (HB_UNLIKELY (!offset)) return Null(Type);
303     return * (const Type *) ((const char *) base + offset);
304   }
305 };
306 template <typename Base, typename Type>
307 inline const Type& operator + (const Base &base, OffsetTo<Type> offset) {
308   return offset(base);
309 }
310
311
312 /* CheckSum */
313 struct CheckSum : ULONG {
314   static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length) {
315     uint32_t Sum = 0L;
316     ULONG *EndPtr = Table+((Length+3) & ~3) / sizeof(ULONG);
317
318     while (Table < EndPtr)
319       Sum += *Table++;
320     return Sum;
321   }
322 };
323 ASSERT_SIZE (CheckSum, 4);
324
325
326 /*
327  * Version Numbers
328  */
329
330 struct USHORT_Version : USHORT {
331 };
332 ASSERT_SIZE (USHORT_Version, 2);
333
334 struct Fixed_Version : Fixed {
335   inline int16_t major (void) const { return this->int_part(); }
336   inline int16_t minor (void) const { return this->frac_part(); }
337 };
338 ASSERT_SIZE (Fixed_Version, 4);
339
340 /*
341  * Array Types
342  */
343
344 /* An array with a USHORT number of elements. */
345 template <typename Type>
346 struct ArrayOf {
347   inline const Type& operator [] (unsigned int i) const {
348     if (HB_UNLIKELY (i >= len)) return Null(Type);
349     return array[i];
350   }
351   inline unsigned int get_size () const {
352     return sizeof (len) + len * sizeof (array[0]);
353   }
354
355   USHORT len;
356   Type array[];
357 };
358
359 /* An array with a USHORT number of elements,
360  * starting at second element. */
361 template <typename Type>
362 struct HeadlessArrayOf {
363   inline const Type& operator [] (unsigned int i) const {
364     if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
365     return array[i-1];
366   }
367   inline unsigned int get_size () const {
368     return sizeof (len) + (len ? len - 1 : 0) * sizeof (array[0]);
369   }
370
371   USHORT len;
372   Type array[];
373 };
374
375 /* Array of Offset's */
376 template <typename Type>
377 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {
378 };
379
380 /* An array type is one that contains a variable number of objects
381  * as its last item.  An array object is extended with get_len()
382  * methods, as well as overloaded [] operator. */
383 #define DEFINE_ARRAY_TYPE(Type, array, num) \
384   DEFINE_INDEX_OPERATOR(Type, array, num) \
385   DEFINE_LEN(Type, array, num)
386 #define DEFINE_INDEX_OPERATOR(Type, array, num) \
387   inline const Type& operator[] (unsigned int i) const { \
388     if (HB_UNLIKELY (i >= num)) return Null(Type); \
389     return array[i]; \
390   }
391
392
393 /*
394  * Organization of an OpenType Font
395  */
396
397 struct OpenTypeFontFile;
398 struct OffsetTable;
399 struct TTCHeader;
400
401 typedef struct TableDirectory {
402
403   friend struct OpenTypeFontFile;
404   friend struct OffsetTable;
405
406   inline bool is_null (void) const { return length == 0; }
407   inline const Tag& get_tag (void) const { return tag; }
408   inline unsigned long get_checksum (void) const { return checkSum; }
409   inline unsigned long get_offset (void) const { return offset; }
410   inline unsigned long get_length (void) const { return length; }
411
412   private:
413   Tag           tag;            /* 4-byte identifier. */
414   CheckSum      checkSum;       /* CheckSum for this table. */
415   ULONG         offset;         /* Offset from beginning of TrueType font
416                                  * file. */
417   ULONG         length;         /* Length of this table. */
418 } OpenTypeTable;
419 ASSERT_SIZE (TableDirectory, 16);
420
421 typedef struct OffsetTable {
422
423   friend struct OpenTypeFontFile;
424   friend struct TTCHeader;
425
426   DEFINE_TAG_ARRAY_INTERFACE (OpenTypeTable, table);    /* get_table_count(), get_table(i), get_table_tag(i) */
427   DEFINE_TAG_FIND_INTERFACE  (OpenTypeTable, table);    /* find_table_index(tag), get_table_by_tag(tag) */
428
429   private:
430   /* OpenTypeTables, in no particular order */
431   DEFINE_ARRAY_TYPE (TableDirectory, tableDir, numTables);
432
433   private:
434   Tag           sfnt_version;   /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
435   USHORT        numTables;      /* Number of tables. */
436   USHORT        searchRange;    /* (Maximum power of 2 <= numTables) x 16 */
437   USHORT        entrySelector;  /* Log2(maximum power of 2 <= numTables). */
438   USHORT        rangeShift;     /* NumTables x 16-searchRange. */
439   TableDirectory tableDir[];    /* TableDirectory entries. numTables items */
440 } OpenTypeFontFace;
441 ASSERT_SIZE (OffsetTable, 12);
442
443 /*
444  * TrueType Collections
445  */
446
447 struct TTCHeader {
448
449   friend struct OpenTypeFontFile;
450
451   private:
452   /* OpenTypeFontFaces, in no particular order */
453   DEFINE_OFFSET_ARRAY_TYPE (OffsetTable, offsetTable, numFonts);
454   /* XXX check version here? */
455
456   private:
457   Tag   ttcTag;         /* TrueType Collection ID string: 'ttcf' */
458   ULONG version;        /* Version of the TTC Header (1.0 or 2.0),
459                          * 0x00010000 or 0x00020000 */
460   ULONG numFonts;       /* Number of fonts in TTC */
461   ULONG offsetTable[];  /* Array of offsets to the OffsetTable for each font
462                          * from the beginning of the file */
463 };
464 ASSERT_SIZE (TTCHeader, 12);
465
466
467 /*
468  * OpenType Font File
469  */
470
471 struct OpenTypeFontFile {
472
473   static const hb_tag_t TrueTypeTag     = HB_TAG ( 0 , 1 , 0 , 0 );
474   static const hb_tag_t CFFTag          = HB_TAG ('O','T','T','O');
475   static const hb_tag_t TTCTag          = HB_TAG ('t','t','c','f');
476
477   STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
478
479   DEFINE_ARRAY_INTERFACE (OpenTypeFontFace, face);      /* get_face_count(), get_face(i) */
480
481   inline const Tag& get_tag (void) const { return tag; }
482
483   /* This is how you get a table */
484   inline const char* get_table_data (const OpenTypeTable& table) const {
485     return (*this)[table];
486   }
487   inline char* get_table_data (const OpenTypeTable& table) {
488     return (*this)[table];
489   }
490
491   private:
492   inline const char* operator[] (const OpenTypeTable& table) const {
493     if (G_UNLIKELY (table.offset == 0)) return NULL;
494     return ((const char*)this) + table.offset;
495   }
496   inline char* operator[] (const OpenTypeTable& table) {
497     if (G_UNLIKELY (table.offset == 0)) return NULL;
498     return ((char*)this) + table.offset;
499   }
500
501   unsigned int get_len (void) const {
502     switch (tag) {
503     default: return 0;
504     case TrueTypeTag: case CFFTag: return 1;
505     case TTCTag: return ((const TTCHeader&)*this).get_len();
506     }
507   }
508   const OpenTypeFontFace& operator[] (unsigned int i) const {
509     if (HB_UNLIKELY (i >= get_len ())) return Null(OpenTypeFontFace);
510     switch (tag) {
511     default: case TrueTypeTag: case CFFTag: return (const OffsetTable&)*this;
512     case TTCTag: return ((const TTCHeader&)*this)[i];
513     }
514   }
515
516   private:
517   Tag           tag;            /* 4-byte identifier. */
518 };
519 ASSERT_SIZE (OpenTypeFontFile, 4);
520
521
522 #endif /* HB_OT_LAYOUT_OPEN_PRIVATE_H */