blob: f46e0234847017095b590cc2ddbdeeb96dfc1c99 [file] [log] [blame]
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05001/*
Behdad Esfahbodee58aae2009-05-17 05:14:33 -04002 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05003 *
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
Behdad Esfahbod8dd1c8b2008-01-23 05:00:30 -050027#ifndef HB_OT_LAYOUT_OPEN_PRIVATE_H
28#define HB_OT_LAYOUT_OPEN_PRIVATE_H
Behdad Esfahbod12c45682006-12-28 06:10:59 -050029
Behdad Esfahbod54e5aac2008-01-27 21:19:51 -050030#ifndef HB_OT_LAYOUT_CC
Behdad Esfahbod8b835802009-05-16 22:48:14 -040031#error "This file should only be included from hb-ot-layout.cc"
Behdad Esfahbod54e5aac2008-01-27 21:19:51 -050032#endif
33
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050034#include "hb-ot-layout-private.h"
Behdad Esfahbod12c45682006-12-28 06:10:59 -050035
Behdad Esfahboda16ecbf2008-01-23 17:01:55 -050036
Behdad Esfahbod706ab252008-01-28 05:58:50 -050037#define NO_INDEX ((unsigned int) 0xFFFF)
Behdad Esfahbod2e8fb6c2009-05-18 04:37:37 -040038#define NO_CONTEXT ((unsigned int) 0x110000)
39#define NOT_COVERED ((unsigned int) 0x110000)
40#define MAX_NESTING_LEVEL 8
Behdad Esfahbod5a0b7912009-04-16 04:45:30 -040041
Behdad Esfahbod706ab252008-01-28 05:58:50 -050042
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -050043/*
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -050044 * 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
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -050051/* An array type is one that contains a variable number of objects
Behdad Esfahbod238c8552009-05-17 00:22:37 -040052 * as its last item. An array object is extended with get_len()
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -050053 * methods, as well as overloaded [] operator. */
54#define DEFINE_ARRAY_TYPE(Type, array, num) \
Behdad Esfahbod1f437e62008-01-23 04:36:40 -050055 DEFINE_INDEX_OPERATOR(Type, array, num) \
Behdad Esfahbod0dff25f2009-05-08 21:12:18 -040056 DEFINE_LEN(Type, array, num)
Behdad Esfahbod1f437e62008-01-23 04:36:40 -050057#define DEFINE_INDEX_OPERATOR(Type, array, num) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040058 inline const Type& operator[] (unsigned int i) const \
59 { \
Behdad Esfahbod8b835802009-05-16 22:48:14 -040060 if (HB_UNLIKELY (i >= num)) return Null(Type); \
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -050061 return array[i]; \
62 }
63
64/* An offset array type is like an array type, but it contains a table
65 * of offsets to the objects, relative to the beginning of the current
66 * object. */
67#define DEFINE_OFFSET_ARRAY_TYPE(Type, array, num) \
Behdad Esfahbod1f437e62008-01-23 04:36:40 -050068 DEFINE_OFFSET_INDEX_OPERATOR(Type, array, num) \
Behdad Esfahbod0dff25f2009-05-08 21:12:18 -040069 DEFINE_LEN(Offset, array, num)
Behdad Esfahbod1f437e62008-01-23 04:36:40 -050070#define DEFINE_OFFSET_INDEX_OPERATOR(Type, array, num) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040071 inline const Type& operator[] (unsigned int i) const \
72 { \
Behdad Esfahbod8b835802009-05-16 22:48:14 -040073 if (HB_UNLIKELY (i >= num)) return Null(Type); \
74 if (HB_UNLIKELY (!array[i])) return Null(Type); \
Behdad Esfahbod4497af02009-05-25 03:20:18 -040075 return *(const Type)((const char*)this + array[i]); \
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -050076 }
77
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050078
79#define DEFINE_ARRAY_INTERFACE(Type, name) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040080 inline const Type& get_##name (unsigned int i) const { return (*this)[i]; } \
81 inline unsigned int get_##name##_count (void) const { return this->get_len (); }
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050082#define DEFINE_INDEX_ARRAY_INTERFACE(name) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040083 inline unsigned int get_##name##_index (unsigned int i) const \
84 { \
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -050085 if (HB_UNLIKELY (i >= get_len ())) return NO_INDEX; \
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050086 return (*this)[i]; \
87 } \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040088 inline unsigned int get_##name##_count (void) const { return get_len (); }
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050089
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050090
Behdad Esfahbod1f437e62008-01-23 04:36:40 -050091/*
92 * List types
93 */
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -050094
Behdad Esfahbod40a81312008-01-28 02:30:48 -050095#define DEFINE_LIST_INTERFACE(Type, name) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -040096 inline const Type& get_##name (unsigned int i) const { return (this+name##List)[i]; } \
97 inline unsigned int get_##name##_count (void) const { return (this+name##List).len; }
Behdad Esfahbod40a81312008-01-28 02:30:48 -050098
99/*
100 * Tag types
101 */
102
103#define DEFINE_TAG_ARRAY_INTERFACE(Type, name) \
104 DEFINE_ARRAY_INTERFACE (Type, name); \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400105 inline const Tag& get_##name##_tag (unsigned int i) const { return (*this)[i].tag; }
Behdad Esfahbod40a81312008-01-28 02:30:48 -0500106#define DEFINE_TAG_LIST_INTERFACE(Type, name) \
107 DEFINE_LIST_INTERFACE (Type, name); \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400108 inline const Tag& get_##name##_tag (unsigned int i) const { return (this+name##List).get_tag (i); }
Behdad Esfahbod40a81312008-01-28 02:30:48 -0500109
110#define DEFINE_TAG_FIND_INTERFACE(Type, name) \
Behdad Esfahbodcc6c6442009-05-25 03:10:06 -0400111 inline bool find_##name##_index (hb_tag_t tag, unsigned int *index) const { \
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500112 const Tag t = tag; \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400113 for (unsigned int i = 0; i < get_##name##_count (); i++) \
114 { \
115 if (t == get_##name##_tag (i)) \
116 { \
Behdad Esfahbodcc6c6442009-05-25 03:10:06 -0400117 if (index) *index = i; \
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500118 return true; \
119 } \
120 } \
Behdad Esfahbodcc6c6442009-05-25 03:10:06 -0400121 if (index) *index = NO_INDEX; \
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500122 return false; \
Behdad Esfahbod40a81312008-01-28 02:30:48 -0500123 } \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400124 inline const Type& get_##name##_by_tag (hb_tag_t tag) const \
125 { \
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500126 unsigned int i; \
127 if (find_##name##_index (tag, &i)) \
128 return get_##name (i); \
Behdad Esfahbod40a81312008-01-28 02:30:48 -0500129 else \
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400130 return Null(Type); \
Behdad Esfahbod1f437e62008-01-23 04:36:40 -0500131 }
Behdad Esfahbod600e5eb2008-01-23 02:01:37 -0500132
133/*
134 * Class features
135 */
136
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400137
138/* Null objects */
139
140/* Global nul-content Null pool. Enlarge as necessary. */
141static const char NullPool[16] = "";
142
143/* Generic template for nul-content sizeof-sized Null objects. */
144template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400145struct Null
146{
Behdad Esfahbodf8dc67b2009-05-17 19:47:54 -0400147 ASSERT_STATIC (sizeof (Type) <= sizeof (NullPool));
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400148 static inline const Type &get () { return *(const Type*)NullPool; }
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400149};
150
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400151/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
152#define DEFINE_NULL_DATA(Type, size, data) \
Behdad Esfahbodcc6c6442009-05-25 03:10:06 -0400153static const char _Null##Type[size] = data; \
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400154template <> \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400155struct Null <Type> \
156{ \
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400157 static inline const Type &get () { return *(const Type*)_Null##Type; } \
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400158}
159
160/* Accessor macro. */
161#define Null(Type) (Null<Type>::get())
162
163
164#define ASSERT_SIZE_DATA(Type, size, data) \
Behdad Esfahbod1f437e62008-01-23 04:36:40 -0500165 ASSERT_SIZE (Type, size); \
166 DEFINE_NULL_DATA (Type, size, data)
Behdad Esfahbod1f437e62008-01-23 04:36:40 -0500167
Behdad Esfahbod600e5eb2008-01-23 02:01:37 -0500168/* get_for_data() is a static class method returning a reference to an
169 * instance of Type located at the input data location. It's just a
Behdad Esfahbod54e5aac2008-01-27 21:19:51 -0500170 * fancy, NULL-safe, cast! */
Behdad Esfahbod600e5eb2008-01-23 02:01:37 -0500171#define STATIC_DEFINE_GET_FOR_DATA(Type) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400172 static inline const Type& get_for_data (const char *data) \
173 { \
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400174 if (HB_UNLIKELY (data == NULL)) return Null(Type); \
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400175 return *(const Type*)data; \
Behdad Esfahbod212aba62009-05-24 00:50:27 -0400176 }
177/* Like get_for_data(), but checks major version first. */
178#define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, Major) \
179 static inline const Type& get_for_data (const char *data) \
180 { \
181 if (HB_UNLIKELY (data == NULL)) return Null(Type); \
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400182 const Type& t = *(const Type*)data; \
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400183 if (HB_UNLIKELY (!t.version.major || t.version.major > Major)) return Null(Type); \
Behdad Esfahbod212aba62009-05-24 00:50:27 -0400184 return t; \
185 }
Behdad Esfahbod600e5eb2008-01-23 02:01:37 -0500186
187
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400188
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500189/*
190 *
191 * The OpenType Font File
192 *
193 */
194
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500195
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500196/*
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500197 * Data Types
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500198 */
199
200
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500201/* "The following data types are used in the OpenType font file.
202 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500203
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400204/*
205 * Int types
206 */
207
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400208/* TODO On machines that do not allow unaligned access, fix the accessors. */
209#define DEFINE_INT_TYPE1(NAME, TYPE, BIG_ENDIAN, BYTES) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400210 struct NAME \
211 { \
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400212 inline NAME& operator = (TYPE i) { (TYPE&) v = BIG_ENDIAN (i); return *this; } \
213 inline operator TYPE(void) const { return BIG_ENDIAN ((TYPE&) v); } \
214 inline bool operator== (NAME o) const { return (TYPE&) v == (TYPE&) o.v; } \
215 private: char v[BYTES]; \
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400216 }; \
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400217 ASSERT_SIZE (NAME, BYTES)
Behdad Esfahboddf660282009-08-01 20:46:02 -0400218#define DEFINE_INT_TYPE0(NAME, type, b) DEFINE_INT_TYPE1 (NAME, type##_t, hb_be_##type, b)
219#define DEFINE_INT_TYPE(NAME, u, w) DEFINE_INT_TYPE0 (NAME, u##int##w, (w / 8))
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400220
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500221
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400222DEFINE_INT_TYPE (USHORT, u, 16); /* 16-bit unsigned integer. */
223DEFINE_INT_TYPE (SHORT, , 16); /* 16-bit signed integer. */
224DEFINE_INT_TYPE (ULONG, u, 32); /* 32-bit unsigned integer. */
225DEFINE_INT_TYPE (LONG, , 32); /* 32-bit signed integer. */
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500226
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500227/* Array of four uint8s (length = 32 bits) used to identify a script, language
228 * system, feature, or baseline */
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400229struct Tag : ULONG
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400230{
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400231 inline Tag (const Tag &o) { *(ULONG*)this = (ULONG&) o; }
232 inline Tag (uint32_t i) { *(ULONG*)this = i; }
233 inline Tag (const char *c) { *(ULONG*)this = *(ULONG*)c; }
234 inline bool operator== (const char *c) const { return *(ULONG*)this == *(ULONG*)c; }
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500235 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
Behdad Esfahbod808dbe22006-12-25 06:18:52 -0500236 inline operator const char* (void) const { return (const char *)this; }
237 inline operator char* (void) { return (char *)this; }
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500238};
Behdad Esfahbod303fe622008-01-23 00:20:48 -0500239ASSERT_SIZE (Tag, 4);
Behdad Esfahbodda1097b2009-05-17 19:31:18 -0400240#define _NULL_TAG_INIT {' ', ' ', ' ', ' '}
241DEFINE_NULL_DATA (Tag, 4, _NULL_TAG_INIT);
242#undef _NULL_TAG_INIT
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500243
244/* Glyph index number, same as uint16 (length = 16 bits) */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400245typedef USHORT GlyphID;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500246
Behdad Esfahbod1f437e62008-01-23 04:36:40 -0500247/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400248typedef USHORT Offset;
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400249
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400250/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
251typedef ULONG LongOffset;
252
253/* Template subclasses of Offset and LongOffset that do the dereferencing. Use: (this+memberName) */
254
Behdad Esfahbod9e4d9d72009-05-17 00:09:20 -0400255template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400256struct OffsetTo : Offset
257{
258 inline const Type& operator() (const void *base) const
259 {
Behdad Esfahbod9e4d9d72009-05-17 00:09:20 -0400260 unsigned int offset = *this;
261 if (HB_UNLIKELY (!offset)) return Null(Type);
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400262 return *(const Type*)((const char *) base + offset);
Behdad Esfahbod9e4d9d72009-05-17 00:09:20 -0400263 }
264};
Behdad Esfahbod9e4d9d72009-05-17 00:09:20 -0400265template <typename Base, typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400266inline const Type& operator + (const Base &base, OffsetTo<Type> offset) { return offset (base); }
Behdad Esfahbod9e4d9d72009-05-17 00:09:20 -0400267
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400268template <typename Type>
269struct LongOffsetTo : LongOffset
270{
271 inline const Type& operator() (const void *base) const
272 {
273 unsigned int offset = *this;
274 if (HB_UNLIKELY (!offset)) return Null(Type);
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400275 return *(const Type*)((const char *) base + offset);
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400276 }
277};
278template <typename Base, typename Type>
279inline const Type& operator + (const Base &base, LongOffsetTo<Type> offset) { return offset (base); }
280
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500281
282/* CheckSum */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400283struct CheckSum : ULONG
284{
285 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
286 {
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500287 uint32_t Sum = 0L;
Behdad Esfahbod01e4fcb2006-12-21 22:31:31 -0500288 ULONG *EndPtr = Table+((Length+3) & ~3) / sizeof(ULONG);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500289
290 while (Table < EndPtr)
291 Sum += *Table++;
292 return Sum;
293 }
294};
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400295ASSERT_SIZE (CheckSum, 4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500296
297
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500298/*
299 * Version Numbers
300 */
301
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400302struct FixedVersion
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400303{
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400304 inline operator uint32_t (void) const { return (major << 16) + minor; }
Behdad Esfahbod96908b82009-05-24 12:30:40 -0400305
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400306 USHORT major;
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400307 USHORT minor;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500308};
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400309ASSERT_SIZE (FixedVersion, 4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500310
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400311/*
312 * Array Types
313 */
314
315/* An array with a USHORT number of elements. */
316template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400317struct ArrayOf
318{
319 inline const Type& operator [] (unsigned int i) const
320 {
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400321 if (HB_UNLIKELY (i >= len)) return Null(Type);
322 return array[i];
323 }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400324 inline unsigned int get_size () const
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400325 { return sizeof (len) + len * sizeof (array[0]); }
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400326
327 USHORT len;
328 Type array[];
329};
330
331/* An array with a USHORT number of elements,
332 * starting at second element. */
333template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400334struct HeadlessArrayOf
335{
336 inline const Type& operator [] (unsigned int i) const
337 {
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400338 if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
339 return array[i-1];
340 }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400341 inline unsigned int get_size () const
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400342 { return sizeof (len) + (len ? len - 1 : 0) * sizeof (array[0]); }
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400343
344 USHORT len;
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400345 Type array[];
346};
347
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400348/* An array with a ULONG number of elements. */
349template <typename Type>
350struct LongArrayOf
351{
352 inline const Type& operator [] (unsigned int i) const
353 {
354 if (HB_UNLIKELY (i >= len)) return Null(Type);
355 return array[i];
356 }
357 inline unsigned int get_size () const
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400358 { return sizeof (len) + len * sizeof (array[0]); }
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400359
360 ULONG len;
361 Type array[];
362};
363
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400364/* Array of Offset's */
365template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400366struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400367
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400368/* Array of LongOffset's */
369template <typename Type>
370struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
371
372/* LongArray of LongOffset's */
373template <typename Type>
374struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
375
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400376/* An array type is one that contains a variable number of objects
377 * as its last item. An array object is extended with get_len()
378 * methods, as well as overloaded [] operator. */
379#define DEFINE_ARRAY_TYPE(Type, array, num) \
380 DEFINE_INDEX_OPERATOR(Type, array, num) \
381 DEFINE_LEN(Type, array, num)
382#define DEFINE_INDEX_OPERATOR(Type, array, num) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400383 inline const Type& operator[] (unsigned int i) const \
384 { \
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400385 if (HB_UNLIKELY (i >= num)) return Null(Type); \
386 return array[i]; \
387 }
388
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500389
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500390/*
391 * Organization of an OpenType Font
392 */
393
Behdad Esfahbod3158d842006-12-27 20:08:07 -0500394struct OpenTypeFontFile;
Behdad Esfahbodb739c052006-12-25 05:39:20 -0500395struct OffsetTable;
396struct TTCHeader;
397
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400398typedef struct TableDirectory
399{
Behdad Esfahbod01e4fcb2006-12-21 22:31:31 -0500400 Tag tag; /* 4-byte identifier. */
401 CheckSum checkSum; /* CheckSum for this table. */
402 ULONG offset; /* Offset from beginning of TrueType font
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500403 * file. */
Behdad Esfahbod01e4fcb2006-12-21 22:31:31 -0500404 ULONG length; /* Length of this table. */
Behdad Esfahbod808dbe22006-12-25 06:18:52 -0500405} OpenTypeTable;
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400406ASSERT_SIZE (TableDirectory, 16);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500407
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400408typedef struct OffsetTable
409{
Behdad Esfahbod75860892008-01-23 18:02:28 -0500410 friend struct OpenTypeFontFile;
411 friend struct TTCHeader;
412
Behdad Esfahbod40a81312008-01-28 02:30:48 -0500413 DEFINE_TAG_ARRAY_INTERFACE (OpenTypeTable, table); /* get_table_count(), get_table(i), get_table_tag(i) */
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500414 DEFINE_TAG_FIND_INTERFACE (OpenTypeTable, table); /* find_table_index(tag), get_table_by_tag(tag) */
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -0500415
416 private:
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500417 /* OpenTypeTables, in no particular order */
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -0500418 DEFINE_ARRAY_TYPE (TableDirectory, tableDir, numTables);
419
Behdad Esfahbod303fe622008-01-23 00:20:48 -0500420 private:
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -0500421 Tag sfnt_version; /* '\0\001\0\00' if TrueType / 'OTTO' if CFF */
422 USHORT numTables; /* Number of tables. */
423 USHORT searchRange; /* (Maximum power of 2 <= numTables) x 16 */
424 USHORT entrySelector; /* Log2(maximum power of 2 <= numTables). */
425 USHORT rangeShift; /* NumTables x 16-searchRange. */
426 TableDirectory tableDir[]; /* TableDirectory entries. numTables items */
Behdad Esfahbod13346612006-12-27 19:58:32 -0500427} OpenTypeFontFace;
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400428ASSERT_SIZE (OffsetTable, 12);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500429
430/*
431 * TrueType Collections
432 */
433
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400434struct TTCHeader
435{
Behdad Esfahbod75860892008-01-23 18:02:28 -0500436 friend struct OpenTypeFontFile;
437
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400438 STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION (TTCHeader, 2);
Behdad Esfahbodb6e62bc2006-12-22 02:21:55 -0500439
Behdad Esfahbod303fe622008-01-23 00:20:48 -0500440 private:
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400441 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */
442 FixedVersion version; /* Version of the TTC Header (1.0 or 2.0),
443 * 0x00010000 or 0x00020000 */
444 LongOffsetLongArrayOf<OffsetTable>
445 table; /* Array of offsets to the OffsetTable for each font
446 * from the beginning of the file */
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500447};
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400448ASSERT_SIZE (TTCHeader, 12);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500449
450
Behdad Esfahbodb739c052006-12-25 05:39:20 -0500451/*
452 * OpenType Font File
453 */
454
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400455struct OpenTypeFontFile
456{
Behdad Esfahbodc81efca2006-12-25 06:22:08 -0500457 static const hb_tag_t TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 );
458 static const hb_tag_t CFFTag = HB_TAG ('O','T','T','O');
459 static const hb_tag_t TTCTag = HB_TAG ('t','t','c','f');
Behdad Esfahbodb739c052006-12-25 05:39:20 -0500460
Behdad Esfahbod600e5eb2008-01-23 02:01:37 -0500461 STATIC_DEFINE_GET_FOR_DATA (OpenTypeFontFile);
Behdad Esfahbodb739c052006-12-25 05:39:20 -0500462
Behdad Esfahbod3ec00922009-05-25 02:34:25 -0400463 unsigned int get_face_count (void) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400464 {
Behdad Esfahbod808dbe22006-12-25 06:18:52 -0500465 switch (tag) {
466 default: return 0;
Behdad Esfahbodc81efca2006-12-25 06:22:08 -0500467 case TrueTypeTag: case CFFTag: return 1;
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400468 case TTCTag: return TTCHeader::get_for_data ((const char *) this).table.len;
Behdad Esfahbod808dbe22006-12-25 06:18:52 -0500469 }
470 }
Behdad Esfahbod3ec00922009-05-25 02:34:25 -0400471 const OpenTypeFontFace& get_face (unsigned int i) const
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400472 {
Behdad Esfahbod808dbe22006-12-25 06:18:52 -0500473 switch (tag) {
Behdad Esfahbod23af7672009-08-01 19:10:41 -0400474 default: return Null(OpenTypeFontFace);
475 /* Note: for non-collection SFNT data we ignore index. This is because
476 * Apple dfont container is a container of SFNT's. So each SFNT is a
477 * non-TTC, but the index is more than zero. */
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400478 case TrueTypeTag: case CFFTag: return *(const OffsetTable*)this;
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400479 case TTCTag: return this+TTCHeader::get_for_data ((const char *) this).table[i];
Behdad Esfahbod808dbe22006-12-25 06:18:52 -0500480 }
481 }
Behdad Esfahbod808dbe22006-12-25 06:18:52 -0500482
Behdad Esfahbod3ec00922009-05-25 02:34:25 -0400483 /* This is how you get a table */
484 inline const char* get_table_data (const OpenTypeTable& table) const
485 {
486 if (HB_UNLIKELY (table.offset == 0)) return NULL;
487 return ((const char*) this) + table.offset;
488 }
489
Behdad Esfahbod808dbe22006-12-25 06:18:52 -0500490 Tag tag; /* 4-byte identifier. */
Behdad Esfahbodb739c052006-12-25 05:39:20 -0500491};
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400492ASSERT_SIZE (OpenTypeFontFile, 4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500493
494
Behdad Esfahbod8dd1c8b2008-01-23 05:00:30 -0500495#endif /* HB_OT_LAYOUT_OPEN_PRIVATE_H */