2 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
4 * This is part of HarfBuzz, an OpenType Layout engine library.
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.
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
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.
24 * Red Hat Author(s): Behdad Esfahbod
27 #ifndef HB_OT_LAYOUT_OPEN_PRIVATE_H
28 #define HB_OT_LAYOUT_OPEN_PRIVATE_H
30 #ifndef HB_OT_LAYOUT_CC
31 #error "This file should only be included from hb-ot-layout.cc"
34 #include "hb-ot-layout-private.h"
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
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; } \
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); \
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
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]); \
77 #define DEFINE_ARRAY_INTERFACE(Type, name) \
78 inline const Type& get_##name (unsigned int i) const { \
81 inline unsigned int get_##name##_count (void) const { \
82 return this->get_len (); \
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; \
89 inline unsigned int get_##name##_count (void) const { \
98 #define DEFINE_LIST_INTERFACE(Type, name) \
99 inline const Type& get_##name (unsigned int i) const { \
100 return (this+name##List)[i]; \
102 inline unsigned int get_##name##_count (void) const { \
103 return (this+name##List).len; \
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; \
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); \
121 #define DEFINE_TAG_FIND_INTERFACE(Type, name) \
122 inline bool find_##name##_index (hb_tag_t tag, unsigned int *name##_index) const { \
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; \
130 if (name##_index) *name##_index = NO_INDEX; \
133 inline const Type& get_##name##_by_tag (hb_tag_t tag) const { \
135 if (find_##name##_index (tag, &i)) \
136 return get_##name (i); \
148 /* Global nul-content Null pool. Enlarge as necessary. */
149 static const char NullPool[16] = "";
151 /* Generic template for nul-content sizeof-sized Null objects. */
152 template <typename Type>
154 ASSERT_STATIC (sizeof (Type) <= sizeof (NullPool));
155 static inline const Type &get () { return (const Type&) *(const Type*) NullPool; }
158 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
159 #define DEFINE_NULL_DATA(Type, size, data) \
161 struct Null <Type> { \
162 static inline const Type &get () { static const char bytes[size] = data; return (const Type&) *(const Type*) bytes; } \
165 /* Accessor macro. */
166 #define Null(Type) (Null<Type>::get())
169 #define ASSERT_SIZE_DATA(Type, size, data) \
170 ASSERT_SIZE (Type, size); \
171 DEFINE_NULL_DATA (Type, size, data)
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; \
181 static inline Type& get_for_data (char *data) { \
182 return *(Type*)data; \
189 * The OpenType Font File
199 /* "The following data types are used in the OpenType font file.
200 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
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; } \
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) \
218 DEFINE_INT_TYPE(NAME, u, w) \
220 ASSERT_SIZE (NAME, w / 8)
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. */
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);
234 /* 32-bit signed fixed-point number (16.16) */
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; } \
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; }
248 ASSERT_SIZE (Fixed, 4);
250 /* Smallest measurable distance in the em space. */
253 /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */
254 struct FWORD : SHORT {
256 ASSERT_SIZE (FWORD, 2);
258 /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */
259 struct UFWORD : USHORT {
261 ASSERT_SIZE (UFWORD, 2);
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.; }
267 ASSERT_SIZE (F2DOT14, 2);
269 /* Array of four uint8s (length = 32 bits) used to identify a script, language
270 * system, feature, or baseline */
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; }
286 ASSERT_SIZE (Tag, 4);
287 #define _NULL_TAG_INIT {' ', ' ', ' ', ' '}
288 DEFINE_NULL_DATA (Tag, 4, _NULL_TAG_INIT);
289 #undef _NULL_TAG_INIT
291 /* Glyph index number, same as uint16 (length = 16 bits) */
292 DEFINE_INT_TYPE_STRUCT (GlyphID, u, 16);
294 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
295 DEFINE_INT_TYPE_STRUCT (Offset, u, 16);
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);
306 template <typename Base, typename Type>
307 inline const Type& operator + (const Base &base, OffsetTo<Type> offset) {
313 struct CheckSum : ULONG {
314 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length) {
316 ULONG *EndPtr = Table+((Length+3) & ~3) / sizeof(ULONG);
318 while (Table < EndPtr)
323 ASSERT_SIZE (CheckSum, 4);
330 struct USHORT_Version : USHORT {
332 ASSERT_SIZE (USHORT_Version, 2);
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(); }
338 ASSERT_SIZE (Fixed_Version, 4);
344 /* An array with a USHORT number of elements. */
345 template <typename Type>
347 inline const Type& operator [] (unsigned int i) const {
348 if (HB_UNLIKELY (i >= len)) return Null(Type);
351 inline unsigned int get_size () const {
352 return sizeof (len) + len * sizeof (array[0]);
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);
367 inline unsigned int get_size () const {
368 return sizeof (len) + (len ? len - 1 : 0) * sizeof (array[0]);
375 /* Array of Offset's */
376 template <typename Type>
377 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {
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); \
394 * Organization of an OpenType Font
397 struct OpenTypeFontFile;
401 typedef struct TableDirectory {
403 friend struct OpenTypeFontFile;
404 friend struct OffsetTable;
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; }
413 Tag tag; /* 4-byte identifier. */
414 CheckSum checkSum; /* CheckSum for this table. */
415 ULONG offset; /* Offset from beginning of TrueType font
417 ULONG length; /* Length of this table. */
419 ASSERT_SIZE (TableDirectory, 16);
421 typedef struct OffsetTable {
423 friend struct OpenTypeFontFile;
424 friend struct TTCHeader;
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) */
430 /* OpenTypeTables, in no particular order */
431 DEFINE_ARRAY_TYPE (TableDirectory, tableDir, numTables);
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 */
441 ASSERT_SIZE (OffsetTable, 12);
444 * TrueType Collections
449 friend struct OpenTypeFontFile;
452 /* OpenTypeFontFaces, in no particular order */
453 DEFINE_OFFSET_ARRAY_TYPE (OffsetTable, offsetTable, numFonts);
454 /* XXX check version here? */
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 */
464 ASSERT_SIZE (TTCHeader, 12);
471 struct OpenTypeFontFile {
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');
477 STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
479 DEFINE_ARRAY_INTERFACE (OpenTypeFontFace, face); /* get_face_count(), get_face(i) */
481 inline const Tag& get_tag (void) const { return tag; }
483 /* This is how you get a table */
484 inline const char* get_table_data (const OpenTypeTable& table) const {
485 return (*this)[table];
487 inline char* get_table_data (const OpenTypeTable& table) {
488 return (*this)[table];
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;
496 inline char* operator[] (const OpenTypeTable& table) {
497 if (G_UNLIKELY (table.offset == 0)) return NULL;
498 return ((char*)this) + table.offset;
501 unsigned int get_len (void) const {
504 case TrueTypeTag: case CFFTag: return 1;
505 case TTCTag: return ((const TTCHeader&)*this).get_len();
508 const OpenTypeFontFace& operator[] (unsigned int i) const {
509 if (HB_UNLIKELY (i >= get_len ())) return Null(OpenTypeFontFace);
511 default: case TrueTypeTag: case CFFTag: return (const OffsetTable&)*this;
512 case TTCTag: return ((const TTCHeader&)*this)[i];
517 Tag tag; /* 4-byte identifier. */
519 ASSERT_SIZE (OpenTypeFontFile, 4);
522 #endif /* HB_OT_LAYOUT_OPEN_PRIVATE_H */