Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | ee58aae | 2009-05-17 05:14:33 -0400 | [diff] [blame] | 2 | * Copyright (C) 2007,2008,2009 Red Hat, Inc. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 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 | |
Behdad Esfahbod | 5f5b24f | 2009-08-02 20:03:12 -0400 | [diff] [blame] | 27 | #ifndef HB_OPEN_TYPES_PRIVATE_HH |
| 28 | #define HB_OPEN_TYPES_PRIVATE_HH |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 29 | |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 30 | #include "hb-private.h" |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 31 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 32 | #include "hb-blob.h" |
| 33 | |
Behdad Esfahbod | a16ecbf | 2008-01-23 17:01:55 -0500 | [diff] [blame] | 34 | |
Behdad Esfahbod | 706ab25 | 2008-01-28 05:58:50 -0500 | [diff] [blame] | 35 | #define NO_INDEX ((unsigned int) 0xFFFF) |
Behdad Esfahbod | 2e8fb6c | 2009-05-18 04:37:37 -0400 | [diff] [blame] | 36 | #define NO_CONTEXT ((unsigned int) 0x110000) |
| 37 | #define NOT_COVERED ((unsigned int) 0x110000) |
| 38 | #define MAX_NESTING_LEVEL 8 |
Behdad Esfahbod | 5a0b791 | 2009-04-16 04:45:30 -0400 | [diff] [blame] | 39 | |
Behdad Esfahbod | 706ab25 | 2008-01-28 05:58:50 -0500 | [diff] [blame] | 40 | |
Behdad Esfahbod | 196598b | 2009-08-04 11:04:32 -0400 | [diff] [blame] | 41 | /* |
| 42 | * Casts |
| 43 | */ |
| 44 | |
Behdad Esfahbod | 2b5a59c | 2009-08-04 11:38:50 -0400 | [diff] [blame] | 45 | #define CONST_CHARP(X) (reinterpret_cast<const char *>(X)) |
| 46 | #define DECONST_CHARP(X) ((char *)reinterpret_cast<const char *>(X)) |
| 47 | #define CHARP(X) (reinterpret_cast<char *>(X)) |
| 48 | |
| 49 | #define CONST_CAST(T,X,Ofs) (*(reinterpret_cast<const T *>(CONST_CHARP(&(X)) + Ofs))) |
| 50 | #define DECONST_CAST(T,X,Ofs) (*(reinterpret_cast<T *>((char *)CONST_CHARP(&(X)) + Ofs))) |
| 51 | #define CAST(T,X,Ofs) (*(reinterpret_cast<T *>(CHARP(&(X)) + Ofs))) |
Behdad Esfahbod | 196598b | 2009-08-04 11:04:32 -0400 | [diff] [blame] | 52 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Sanitize |
| 56 | */ |
| 57 | |
| 58 | typedef struct _hb_sanitize_context_t hb_sanitize_context_t; |
| 59 | struct _hb_sanitize_context_t |
| 60 | { |
| 61 | const char *start, *end; |
| 62 | hb_blob_t *blob; |
| 63 | }; |
| 64 | |
| 65 | #define SANITIZE_ARG_DEF \ |
| 66 | hb_sanitize_context_t *context |
| 67 | #define SANITIZE_ARG \ |
| 68 | context |
| 69 | |
| 70 | #define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG)) |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 71 | #define SANITIZE2(X,Y) (SANITIZE (X) && SANITIZE (Y)) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 72 | |
Behdad Esfahbod | 2b5a59c | 2009-08-04 11:38:50 -0400 | [diff] [blame] | 73 | #define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CONST_CHARP(this))) |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 74 | #define SANITIZE_THIS2(X,Y) (SANITIZE_THIS (X) && SANITIZE_THIS (Y)) |
| 75 | #define SANITIZE_THIS3(X,Y,Z) (SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z)) |
| 76 | |
| 77 | #define SANITIZE_BASE(X,B) HB_LIKELY ((X).sanitize (SANITIZE_ARG, B)) |
| 78 | #define SANITIZE_BASE2(X,Y,B) (SANITIZE_BASE (X,B) && SANITIZE_BASE (Y,B)) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 79 | |
| 80 | #define SANITIZE_SELF() SANITIZE_OBJ (*this) |
| 81 | #define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X)) |
Behdad Esfahbod | dc9c4d9 | 2009-08-04 12:26:26 -0400 | [diff] [blame] | 82 | #define SANITIZE_GET_SIZE() SANITIZE_SELF() && SANITIZE_MEM (this, this->get_size ()) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 83 | |
Behdad Esfahbod | 2b5a59c | 2009-08-04 11:38:50 -0400 | [diff] [blame] | 84 | #define SANITIZE_MEM(B,L) HB_LIKELY (context->start <= CONST_CHARP(B) && CONST_CHARP(B) + (L) <= context->end) /* XXX overflow */ |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 85 | |
Behdad Esfahbod | ac26e2a | 2009-08-04 14:10:39 -0400 | [diff] [blame] | 86 | #define NEUTER(Var, Val) (SANITIZE_OBJ (Var) && hb_blob_try_writeable_inplace (context->blob) && ((Var) = (Val), true)) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 87 | |
| 88 | |
Behdad Esfahbod | b6e62bc | 2006-12-22 02:21:55 -0500 | [diff] [blame] | 89 | /* |
Behdad Esfahbod | b6e62bc | 2006-12-22 02:21:55 -0500 | [diff] [blame] | 90 | * Array types |
| 91 | */ |
| 92 | |
| 93 | /* get_len() is a method returning the number of items in an array-like object */ |
| 94 | #define DEFINE_LEN(Type, array, num) \ |
| 95 | inline unsigned int get_len(void) const { return num; } \ |
| 96 | |
Behdad Esfahbod | b6e62bc | 2006-12-22 02:21:55 -0500 | [diff] [blame] | 97 | /* An array type is one that contains a variable number of objects |
Behdad Esfahbod | 238c855 | 2009-05-17 00:22:37 -0400 | [diff] [blame] | 98 | * as its last item. An array object is extended with get_len() |
Behdad Esfahbod | b6e62bc | 2006-12-22 02:21:55 -0500 | [diff] [blame] | 99 | * methods, as well as overloaded [] operator. */ |
| 100 | #define DEFINE_ARRAY_TYPE(Type, array, num) \ |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 101 | DEFINE_INDEX_OPERATOR(Type, array, num) \ |
Behdad Esfahbod | 0dff25f | 2009-05-08 21:12:18 -0400 | [diff] [blame] | 102 | DEFINE_LEN(Type, array, num) |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 103 | #define DEFINE_INDEX_OPERATOR(Type, array, num) \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 104 | inline const Type& operator[] (unsigned int i) const \ |
| 105 | { \ |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 106 | if (HB_UNLIKELY (i >= num)) return Null(Type); \ |
Behdad Esfahbod | b6e62bc | 2006-12-22 02:21:55 -0500 | [diff] [blame] | 107 | return array[i]; \ |
| 108 | } |
| 109 | |
| 110 | /* An offset array type is like an array type, but it contains a table |
| 111 | * of offsets to the objects, relative to the beginning of the current |
| 112 | * object. */ |
| 113 | #define DEFINE_OFFSET_ARRAY_TYPE(Type, array, num) \ |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 114 | DEFINE_OFFSET_INDEX_OPERATOR(Type, array, num) \ |
Behdad Esfahbod | 0dff25f | 2009-05-08 21:12:18 -0400 | [diff] [blame] | 115 | DEFINE_LEN(Offset, array, num) |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 116 | #define DEFINE_OFFSET_INDEX_OPERATOR(Type, array, num) \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 117 | inline const Type& operator[] (unsigned int i) const \ |
| 118 | { \ |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 119 | if (HB_UNLIKELY (i >= num)) return Null(Type); \ |
| 120 | if (HB_UNLIKELY (!array[i])) return Null(Type); \ |
Behdad Esfahbod | 4497af0 | 2009-05-25 03:20:18 -0400 | [diff] [blame] | 121 | return *(const Type)((const char*)this + array[i]); \ |
Behdad Esfahbod | b6e62bc | 2006-12-22 02:21:55 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Behdad Esfahbod | fd92a3d | 2008-01-24 03:11:09 -0500 | [diff] [blame] | 124 | |
| 125 | #define DEFINE_ARRAY_INTERFACE(Type, name) \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 126 | inline const Type& get_##name (unsigned int i) const { return (*this)[i]; } \ |
| 127 | inline unsigned int get_##name##_count (void) const { return this->get_len (); } |
Behdad Esfahbod | fd92a3d | 2008-01-24 03:11:09 -0500 | [diff] [blame] | 128 | #define DEFINE_INDEX_ARRAY_INTERFACE(name) \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 129 | inline unsigned int get_##name##_index (unsigned int i) const \ |
| 130 | { \ |
Behdad Esfahbod | 4a26ea4 | 2008-01-28 07:40:10 -0500 | [diff] [blame] | 131 | if (HB_UNLIKELY (i >= get_len ())) return NO_INDEX; \ |
Behdad Esfahbod | fd92a3d | 2008-01-24 03:11:09 -0500 | [diff] [blame] | 132 | return (*this)[i]; \ |
| 133 | } \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 134 | inline unsigned int get_##name##_count (void) const { return get_len (); } |
Behdad Esfahbod | fd92a3d | 2008-01-24 03:11:09 -0500 | [diff] [blame] | 135 | |
Behdad Esfahbod | fd92a3d | 2008-01-24 03:11:09 -0500 | [diff] [blame] | 136 | |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 137 | /* |
| 138 | * List types |
| 139 | */ |
Behdad Esfahbod | b6e62bc | 2006-12-22 02:21:55 -0500 | [diff] [blame] | 140 | |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 141 | #define DEFINE_LIST_INTERFACE(Type, name) \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 142 | inline const Type& get_##name (unsigned int i) const { return (this+name##List)[i]; } \ |
| 143 | inline unsigned int get_##name##_count (void) const { return (this+name##List).len; } |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Tag types |
| 147 | */ |
| 148 | |
| 149 | #define DEFINE_TAG_ARRAY_INTERFACE(Type, name) \ |
| 150 | DEFINE_ARRAY_INTERFACE (Type, name); \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 151 | inline const Tag& get_##name##_tag (unsigned int i) const { return (*this)[i].tag; } |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 152 | #define DEFINE_TAG_LIST_INTERFACE(Type, name) \ |
| 153 | DEFINE_LIST_INTERFACE (Type, name); \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 154 | inline const Tag& get_##name##_tag (unsigned int i) const { return (this+name##List).get_tag (i); } |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 155 | |
| 156 | #define DEFINE_TAG_FIND_INTERFACE(Type, name) \ |
Behdad Esfahbod | cc6c644 | 2009-05-25 03:10:06 -0400 | [diff] [blame] | 157 | inline bool find_##name##_index (hb_tag_t tag, unsigned int *index) const { \ |
Behdad Esfahbod | 706ab25 | 2008-01-28 05:58:50 -0500 | [diff] [blame] | 158 | const Tag t = tag; \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 159 | for (unsigned int i = 0; i < get_##name##_count (); i++) \ |
| 160 | { \ |
| 161 | if (t == get_##name##_tag (i)) \ |
| 162 | { \ |
Behdad Esfahbod | cc6c644 | 2009-05-25 03:10:06 -0400 | [diff] [blame] | 163 | if (index) *index = i; \ |
Behdad Esfahbod | 706ab25 | 2008-01-28 05:58:50 -0500 | [diff] [blame] | 164 | return true; \ |
| 165 | } \ |
| 166 | } \ |
Behdad Esfahbod | cc6c644 | 2009-05-25 03:10:06 -0400 | [diff] [blame] | 167 | if (index) *index = NO_INDEX; \ |
Behdad Esfahbod | 706ab25 | 2008-01-28 05:58:50 -0500 | [diff] [blame] | 168 | return false; \ |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 169 | } \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 170 | inline const Type& get_##name##_by_tag (hb_tag_t tag) const \ |
| 171 | { \ |
Behdad Esfahbod | 706ab25 | 2008-01-28 05:58:50 -0500 | [diff] [blame] | 172 | unsigned int i; \ |
| 173 | if (find_##name##_index (tag, &i)) \ |
| 174 | return get_##name (i); \ |
Behdad Esfahbod | 40a8131 | 2008-01-28 02:30:48 -0500 | [diff] [blame] | 175 | else \ |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 176 | return Null(Type); \ |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 177 | } |
Behdad Esfahbod | 600e5eb | 2008-01-23 02:01:37 -0500 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Class features |
| 181 | */ |
| 182 | |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 183 | |
| 184 | /* Null objects */ |
| 185 | |
| 186 | /* Global nul-content Null pool. Enlarge as necessary. */ |
| 187 | static const char NullPool[16] = ""; |
| 188 | |
| 189 | /* Generic template for nul-content sizeof-sized Null objects. */ |
| 190 | template <typename Type> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 191 | struct Null |
| 192 | { |
Behdad Esfahbod | f8dc67b | 2009-05-17 19:47:54 -0400 | [diff] [blame] | 193 | ASSERT_STATIC (sizeof (Type) <= sizeof (NullPool)); |
Behdad Esfahbod | 4497af0 | 2009-05-25 03:20:18 -0400 | [diff] [blame] | 194 | static inline const Type &get () { return *(const Type*)NullPool; } |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 195 | }; |
| 196 | |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 197 | /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ |
| 198 | #define DEFINE_NULL_DATA(Type, size, data) \ |
Behdad Esfahbod | cc6c644 | 2009-05-25 03:10:06 -0400 | [diff] [blame] | 199 | static const char _Null##Type[size] = data; \ |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 200 | template <> \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 201 | struct Null <Type> \ |
| 202 | { \ |
Behdad Esfahbod | 4497af0 | 2009-05-25 03:20:18 -0400 | [diff] [blame] | 203 | static inline const Type &get () { return *(const Type*)_Null##Type; } \ |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* Accessor macro. */ |
| 207 | #define Null(Type) (Null<Type>::get()) |
| 208 | |
| 209 | |
| 210 | #define ASSERT_SIZE_DATA(Type, size, data) \ |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 211 | ASSERT_SIZE (Type, size); \ |
| 212 | DEFINE_NULL_DATA (Type, size, data) |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 213 | |
Behdad Esfahbod | 600e5eb | 2008-01-23 02:01:37 -0500 | [diff] [blame] | 214 | /* get_for_data() is a static class method returning a reference to an |
| 215 | * instance of Type located at the input data location. It's just a |
Behdad Esfahbod | 54e5aac | 2008-01-27 21:19:51 -0500 | [diff] [blame] | 216 | * fancy, NULL-safe, cast! */ |
Behdad Esfahbod | 600e5eb | 2008-01-23 02:01:37 -0500 | [diff] [blame] | 217 | #define STATIC_DEFINE_GET_FOR_DATA(Type) \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 218 | static inline const Type& get_for_data (const char *data) \ |
| 219 | { \ |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 220 | if (HB_UNLIKELY (data == NULL)) return Null(Type); \ |
Behdad Esfahbod | 4497af0 | 2009-05-25 03:20:18 -0400 | [diff] [blame] | 221 | return *(const Type*)data; \ |
Behdad Esfahbod | 212aba6 | 2009-05-24 00:50:27 -0400 | [diff] [blame] | 222 | } |
| 223 | /* Like get_for_data(), but checks major version first. */ |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 224 | #define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, MajorMin, MajorMax) \ |
Behdad Esfahbod | 212aba6 | 2009-05-24 00:50:27 -0400 | [diff] [blame] | 225 | static inline const Type& get_for_data (const char *data) \ |
| 226 | { \ |
| 227 | if (HB_UNLIKELY (data == NULL)) return Null(Type); \ |
Behdad Esfahbod | 4497af0 | 2009-05-25 03:20:18 -0400 | [diff] [blame] | 228 | const Type& t = *(const Type*)data; \ |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 229 | if (HB_UNLIKELY (t.version.major < MajorMin || t.version.major > MajorMax)) return Null(Type); \ |
Behdad Esfahbod | 212aba6 | 2009-05-24 00:50:27 -0400 | [diff] [blame] | 230 | return t; \ |
| 231 | } |
Behdad Esfahbod | 600e5eb | 2008-01-23 02:01:37 -0500 | [diff] [blame] | 232 | |
| 233 | |
Behdad Esfahbod | 2d15e72 | 2009-04-15 19:50:16 -0400 | [diff] [blame] | 234 | |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 235 | /* |
| 236 | * |
| 237 | * The OpenType Font File |
| 238 | * |
| 239 | */ |
| 240 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 241 | |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 242 | /* |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 243 | * Data Types |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 244 | */ |
| 245 | |
| 246 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 247 | /* "The following data types are used in the OpenType font file. |
| 248 | * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */ |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 249 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 250 | /* |
| 251 | * Int types |
| 252 | */ |
| 253 | |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 254 | /* TODO On machines that do not allow unaligned access, fix the accessors. */ |
| 255 | #define DEFINE_INT_TYPE1(NAME, TYPE, BIG_ENDIAN, BYTES) \ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 256 | struct NAME \ |
| 257 | { \ |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 258 | inline NAME& operator = (TYPE i) { (TYPE&) v = BIG_ENDIAN (i); return *this; } \ |
| 259 | inline operator TYPE(void) const { return BIG_ENDIAN ((TYPE&) v); } \ |
| 260 | inline bool operator== (NAME o) const { return (TYPE&) v == (TYPE&) o.v; } \ |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 261 | inline bool sanitize (SANITIZE_ARG_DEF) { return SANITIZE_SELF (); } \ |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 262 | private: char v[BYTES]; \ |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 263 | }; \ |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 264 | ASSERT_SIZE (NAME, BYTES) |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 265 | #define DEFINE_INT_TYPE0(NAME, type, b) DEFINE_INT_TYPE1 (NAME, type##_t, hb_be_##type, b) |
| 266 | #define DEFINE_INT_TYPE(NAME, u, w) DEFINE_INT_TYPE0 (NAME, u##int##w, (w / 8)) |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 267 | |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 268 | |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 269 | DEFINE_INT_TYPE (USHORT, u, 16); /* 16-bit unsigned integer. */ |
| 270 | DEFINE_INT_TYPE (SHORT, , 16); /* 16-bit signed integer. */ |
| 271 | DEFINE_INT_TYPE (ULONG, u, 32); /* 32-bit unsigned integer. */ |
| 272 | DEFINE_INT_TYPE (LONG, , 32); /* 32-bit signed integer. */ |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 273 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 274 | /* Array of four uint8s (length = 32 bits) used to identify a script, language |
| 275 | * system, feature, or baseline */ |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 276 | struct Tag : ULONG |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 277 | { |
Behdad Esfahbod | 4497af0 | 2009-05-25 03:20:18 -0400 | [diff] [blame] | 278 | inline Tag (const Tag &o) { *(ULONG*)this = (ULONG&) o; } |
| 279 | inline Tag (uint32_t i) { *(ULONG*)this = i; } |
| 280 | inline Tag (const char *c) { *(ULONG*)this = *(ULONG*)c; } |
| 281 | inline bool operator== (const char *c) const { return *(ULONG*)this == *(ULONG*)c; } |
Behdad Esfahbod | befc022 | 2006-12-25 09:14:52 -0500 | [diff] [blame] | 282 | /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */ |
Behdad Esfahbod | 2b5a59c | 2009-08-04 11:38:50 -0400 | [diff] [blame] | 283 | inline operator const char* (void) const { return CONST_CHARP(this); } |
| 284 | inline operator char* (void) { return CHARP(this); } |
Behdad Esfahbod | 738c54d | 2009-08-04 14:42:46 -0400 | [diff] [blame^] | 285 | |
| 286 | inline bool sanitize (SANITIZE_ARG_DEF) { |
| 287 | /* Note: Only accept ASCII-visible tags (mind DEL) |
| 288 | * This is one of the few times (only time?) we check |
| 289 | * for data integrity, as opposed o just boundary checks |
| 290 | */ |
| 291 | return SANITIZE_SELF () && (((uint32_t) *this) & 0x80808080) == 0; |
| 292 | } |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 293 | }; |
Behdad Esfahbod | 303fe62 | 2008-01-23 00:20:48 -0500 | [diff] [blame] | 294 | ASSERT_SIZE (Tag, 4); |
Behdad Esfahbod | da1097b | 2009-05-17 19:31:18 -0400 | [diff] [blame] | 295 | #define _NULL_TAG_INIT {' ', ' ', ' ', ' '} |
| 296 | DEFINE_NULL_DATA (Tag, 4, _NULL_TAG_INIT); |
| 297 | #undef _NULL_TAG_INIT |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 298 | |
| 299 | /* Glyph index number, same as uint16 (length = 16 bits) */ |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 300 | typedef USHORT GlyphID; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 301 | |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 302 | /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */ |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 303 | typedef USHORT Offset; |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 304 | |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 305 | /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */ |
| 306 | typedef ULONG LongOffset; |
| 307 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 308 | |
| 309 | /* CheckSum */ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 310 | struct CheckSum : ULONG |
| 311 | { |
| 312 | static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length) |
| 313 | { |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 314 | uint32_t Sum = 0L; |
Behdad Esfahbod | 01e4fcb | 2006-12-21 22:31:31 -0500 | [diff] [blame] | 315 | ULONG *EndPtr = Table+((Length+3) & ~3) / sizeof(ULONG); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 316 | |
| 317 | while (Table < EndPtr) |
| 318 | Sum += *Table++; |
| 319 | return Sum; |
| 320 | } |
| 321 | }; |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 322 | ASSERT_SIZE (CheckSum, 4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 323 | |
| 324 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 325 | /* |
| 326 | * Version Numbers |
| 327 | */ |
| 328 | |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 329 | struct FixedVersion |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 330 | { |
Behdad Esfahbod | 09c292e | 2009-05-26 19:48:16 -0400 | [diff] [blame] | 331 | inline operator uint32_t (void) const { return (major << 16) + minor; } |
Behdad Esfahbod | 96908b8 | 2009-05-24 12:30:40 -0400 | [diff] [blame] | 332 | |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 333 | inline bool sanitize (SANITIZE_ARG_DEF) { |
| 334 | return SANITIZE_SELF (); |
| 335 | } |
| 336 | |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 337 | USHORT major; |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 338 | USHORT minor; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 339 | }; |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 340 | ASSERT_SIZE (FixedVersion, 4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 341 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 342 | |
| 343 | |
| 344 | /* |
| 345 | * Template subclasses of Offset and LongOffset that do the dereferencing. |
| 346 | * Use: (this+memberName) |
| 347 | */ |
| 348 | |
| 349 | template <typename OffsetType, typename Type> |
| 350 | struct GenericOffsetTo : OffsetType |
| 351 | { |
| 352 | inline const Type& operator() (const void *base) const |
| 353 | { |
| 354 | unsigned int offset = *this; |
| 355 | if (HB_UNLIKELY (!offset)) return Null(Type); |
Behdad Esfahbod | 95639fc | 2009-08-04 12:05:24 -0400 | [diff] [blame] | 356 | return CONST_CAST(Type, *CONST_CHARP(base), offset); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | inline bool sanitize (SANITIZE_ARG_DEF, const void *base) { |
| 360 | if (!SANITIZE_OBJ (*this)) return false; |
| 361 | unsigned int offset = *this; |
| 362 | if (HB_UNLIKELY (!offset)) return true; |
Behdad Esfahbod | ac26e2a | 2009-08-04 14:10:39 -0400 | [diff] [blame] | 363 | return SANITIZE (CAST(Type, *DECONST_CHARP(base), offset)) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 364 | } |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 365 | inline bool sanitize (SANITIZE_ARG_DEF, const void *base, unsigned int user_data) { |
| 366 | if (!SANITIZE_OBJ (*this)) return false; |
| 367 | unsigned int offset = *this; |
| 368 | if (HB_UNLIKELY (!offset)) return true; |
Behdad Esfahbod | ac26e2a | 2009-08-04 14:10:39 -0400 | [diff] [blame] | 369 | return SANITIZE_BASE (CAST(Type, *DECONST_CHARP(base), offset), user_data) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0); |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 370 | } |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 371 | }; |
| 372 | template <typename Base, typename OffsetType, typename Type> |
| 373 | inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); } |
| 374 | |
| 375 | template <typename Type> |
| 376 | struct OffsetTo : GenericOffsetTo<Offset, Type> {}; |
| 377 | |
| 378 | template <typename Type> |
| 379 | struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {}; |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 380 | /* |
| 381 | * Array Types |
| 382 | */ |
| 383 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 384 | template <typename LenType, typename Type> |
| 385 | struct GenericArrayOf |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 386 | { |
| 387 | inline const Type& operator [] (unsigned int i) const |
| 388 | { |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 389 | if (HB_UNLIKELY (i >= len)) return Null(Type); |
| 390 | return array[i]; |
| 391 | } |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 392 | inline unsigned int get_size () const |
Behdad Esfahbod | 79420ad | 2009-05-26 12:24:16 -0400 | [diff] [blame] | 393 | { return sizeof (len) + len * sizeof (array[0]); } |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 394 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 395 | inline bool sanitize (SANITIZE_ARG_DEF) { |
Behdad Esfahbod | dc9c4d9 | 2009-08-04 12:26:26 -0400 | [diff] [blame] | 396 | if (!SANITIZE_GET_SIZE()) return false; |
Behdad Esfahbod | a97ce57 | 2009-08-04 02:10:48 -0400 | [diff] [blame] | 397 | /* Note; for non-recursive types, this is not much needed |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 398 | unsigned int count = len; |
| 399 | for (unsigned int i = 0; i < count; i++) |
| 400 | if (!SANITIZE (array[i])) |
| 401 | return false; |
Behdad Esfahbod | a97ce57 | 2009-08-04 02:10:48 -0400 | [diff] [blame] | 402 | */ |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 403 | } |
Behdad Esfahbod | 2b5a59c | 2009-08-04 11:38:50 -0400 | [diff] [blame] | 404 | inline bool sanitize (SANITIZE_ARG_DEF, const void *base) { |
Behdad Esfahbod | dc9c4d9 | 2009-08-04 12:26:26 -0400 | [diff] [blame] | 405 | if (!SANITIZE_GET_SIZE()) return false; |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 406 | unsigned int count = len; |
| 407 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | 95639fc | 2009-08-04 12:05:24 -0400 | [diff] [blame] | 408 | if (!array[i].sanitize (SANITIZE_ARG, base)) |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 409 | return false; |
| 410 | } |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 411 | inline bool sanitize (SANITIZE_ARG_DEF, const void *base, unsigned int user_data) { |
| 412 | if (!SANITIZE_GET_SIZE()) return false; |
| 413 | unsigned int count = len; |
| 414 | for (unsigned int i = 0; i < count; i++) |
| 415 | if (!array[i].sanitize (SANITIZE_ARG, base, user_data)) |
| 416 | return false; |
| 417 | } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 418 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 419 | LenType len; |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 420 | Type array[]; |
| 421 | }; |
| 422 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 423 | /* An array with a USHORT number of elements. */ |
| 424 | template <typename Type> |
| 425 | struct ArrayOf : GenericArrayOf<USHORT, Type> {}; |
| 426 | |
| 427 | /* An array with a ULONG number of elements. */ |
| 428 | template <typename Type> |
| 429 | struct LongArrayOf : GenericArrayOf<ULONG, Type> {}; |
| 430 | |
| 431 | /* Array of Offset's */ |
| 432 | template <typename Type> |
| 433 | struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {}; |
| 434 | |
| 435 | /* Array of LongOffset's */ |
| 436 | template <typename Type> |
| 437 | struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {}; |
| 438 | |
| 439 | /* LongArray of LongOffset's */ |
| 440 | template <typename Type> |
| 441 | struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {}; |
| 442 | |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 443 | /* An array with a USHORT number of elements, |
| 444 | * starting at second element. */ |
| 445 | template <typename Type> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 446 | struct HeadlessArrayOf |
| 447 | { |
| 448 | inline const Type& operator [] (unsigned int i) const |
| 449 | { |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 450 | if (HB_UNLIKELY (i >= len || !i)) return Null(Type); |
| 451 | return array[i-1]; |
| 452 | } |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 453 | inline unsigned int get_size () const |
Behdad Esfahbod | 79420ad | 2009-05-26 12:24:16 -0400 | [diff] [blame] | 454 | { return sizeof (len) + (len ? len - 1 : 0) * sizeof (array[0]); } |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 455 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 456 | inline bool sanitize (SANITIZE_ARG_DEF) { |
Behdad Esfahbod | dc9c4d9 | 2009-08-04 12:26:26 -0400 | [diff] [blame] | 457 | if (!SANITIZE_GET_SIZE()) return false; |
Behdad Esfahbod | a97ce57 | 2009-08-04 02:10:48 -0400 | [diff] [blame] | 458 | /* Note; for non-recursive types, this is not much needed |
Behdad Esfahbod | 15164d9 | 2009-08-04 13:57:41 -0400 | [diff] [blame] | 459 | unsigned int count = len ? len - 1 : 0; |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 460 | for (unsigned int i = 0; i < count; i++) |
| 461 | if (!SANITIZE (array[i])) |
| 462 | return false; |
Behdad Esfahbod | a97ce57 | 2009-08-04 02:10:48 -0400 | [diff] [blame] | 463 | */ |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 464 | } |
| 465 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 466 | USHORT len; |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 467 | Type array[]; |
| 468 | }; |
| 469 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 470 | |
Behdad Esfahbod | 5f5b24f | 2009-08-02 20:03:12 -0400 | [diff] [blame] | 471 | #endif /* HB_OPEN_TYPES_PRIVATE_HH */ |