Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2017 Google, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping 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 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
| 27 | #ifndef HB_AAT_LAYOUT_COMMON_PRIVATE_HH |
| 28 | #define HB_AAT_LAYOUT_COMMON_PRIVATE_HH |
| 29 | |
| 30 | #include "hb-aat-layout-private.hh" |
| 31 | |
| 32 | |
| 33 | namespace AAT { |
| 34 | |
| 35 | using namespace OT; |
| 36 | |
| 37 | |
| 38 | /* |
| 39 | * Binary Searching Tables |
| 40 | */ |
| 41 | |
| 42 | struct BinSearchHeader |
| 43 | { |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 44 | |
| 45 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 46 | { |
| 47 | TRACE_SANITIZE (this); |
| 48 | return_trace (c->check_struct (this)); |
| 49 | } |
| 50 | |
Behdad Esfahbod | 7c7cb42 | 2018-01-08 14:32:55 +0000 | [diff] [blame] | 51 | UINT16 unitSize; /* Size of a lookup unit for this search in bytes. */ |
| 52 | UINT16 nUnits; /* Number of units of the preceding size to be searched. */ |
| 53 | UINT16 searchRange; /* The value of unitSize times the largest power of 2 |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 54 | * that is less than or equal to the value of nUnits. */ |
Behdad Esfahbod | 7c7cb42 | 2018-01-08 14:32:55 +0000 | [diff] [blame] | 55 | UINT16 entrySelector; /* The log base 2 of the largest power of 2 less than |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 56 | * or equal to the value of nUnits. */ |
Behdad Esfahbod | 7c7cb42 | 2018-01-08 14:32:55 +0000 | [diff] [blame] | 57 | UINT16 rangeShift; /* The value of unitSize times the difference of the |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 58 | * value of nUnits minus the largest power of 2 less |
| 59 | * than or equal to the value of nUnits. */ |
| 60 | public: |
| 61 | DEFINE_SIZE_STATIC (10); |
| 62 | }; |
| 63 | |
| 64 | template <typename Type> |
| 65 | struct BinSearchArrayOf |
| 66 | { |
| 67 | inline const Type& operator [] (unsigned int i) const |
| 68 | { |
| 69 | if (unlikely (i >= header.nUnits)) return Null(Type); |
| 70 | return StructAtOffset<Type> (bytes, i * header.unitSize); |
| 71 | } |
| 72 | inline Type& operator [] (unsigned int i) |
| 73 | { |
| 74 | return StructAtOffset<Type> (bytes, i * header.unitSize); |
| 75 | } |
| 76 | inline unsigned int get_size (void) const |
| 77 | { return header.static_size + header.nUnits * header.unitSize; } |
| 78 | |
| 79 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 80 | { |
| 81 | TRACE_SANITIZE (this); |
| 82 | if (unlikely (!sanitize_shallow (c))) return_trace (false); |
| 83 | |
| 84 | /* Note: for structs that do not reference other structs, |
| 85 | * we do not need to call their sanitize() as we already did |
| 86 | * a bound check on the aggregate array size. We just include |
| 87 | * a small unreachable expression to make sure the structs |
| 88 | * pointed to do have a simple sanitize(), ie. they do not |
| 89 | * reference other structs via offsets. |
| 90 | */ |
| 91 | (void) (false && StructAtOffset<Type> (bytes, 0).sanitize (c)); |
| 92 | |
| 93 | return_trace (true); |
| 94 | } |
| 95 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 96 | { |
| 97 | TRACE_SANITIZE (this); |
| 98 | if (unlikely (!sanitize_shallow (c))) return_trace (false); |
| 99 | unsigned int count = header.nUnits; |
| 100 | for (unsigned int i = 0; i < count; i++) |
| 101 | if (unlikely (!(*this)[i].sanitize (c, base))) |
| 102 | return_trace (false); |
| 103 | return_trace (true); |
| 104 | } |
| 105 | |
| 106 | template <typename T> |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 107 | inline const Type *bsearch (const T &key) const |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 108 | { |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 109 | unsigned int size = header.unitSize; |
| 110 | int min = 0, max = (int) header.nUnits - 1; |
| 111 | while (min <= max) |
| 112 | { |
| 113 | int mid = (min + max) / 2; |
| 114 | const Type *p = (const Type *) (((const char *) bytes) + (mid * size)); |
| 115 | int c = p->cmp (key); |
| 116 | if (c < 0) |
| 117 | max = mid - 1; |
| 118 | else if (c > 0) |
| 119 | min = mid + 1; |
| 120 | else |
| 121 | return p; |
| 122 | } |
| 123 | return NULL; |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | private: |
| 127 | inline bool sanitize_shallow (hb_sanitize_context_t *c) const |
| 128 | { |
| 129 | TRACE_SANITIZE (this); |
| 130 | return_trace (header.sanitize (c) && |
| 131 | Type::static_size >= header.unitSize && |
| 132 | c->check_array (bytes, header.unitSize, header.nUnits)); |
| 133 | } |
| 134 | |
| 135 | protected: |
| 136 | BinSearchHeader header; |
Behdad Esfahbod | 7c7cb42 | 2018-01-08 14:32:55 +0000 | [diff] [blame] | 137 | UINT8 bytes[VAR]; |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 138 | public: |
| 139 | DEFINE_SIZE_ARRAY (10, bytes); |
| 140 | }; |
| 141 | |
| 142 | |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 143 | /* TODO Move this to hb-open-type-private.hh and use it in ArrayOf, HeadlessArrayOf, |
| 144 | * and other places around the code base?? */ |
| 145 | template <typename Type> |
| 146 | struct UnsizedArrayOf |
| 147 | { |
| 148 | inline const Type& operator [] (unsigned int i) const { return arrayZ[i]; } |
| 149 | inline Type& operator [] (unsigned int i) { return arrayZ[i]; } |
| 150 | |
| 151 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const |
| 152 | { |
| 153 | TRACE_SANITIZE (this); |
| 154 | |
| 155 | /* Note: for structs that do not reference other structs, |
| 156 | * we do not need to call their sanitize() as we already did |
| 157 | * a bound check on the aggregate array size. We just include |
| 158 | * a small unreachable expression to make sure the structs |
| 159 | * pointed to do have a simple sanitize(), ie. they do not |
| 160 | * reference other structs via offsets. |
| 161 | */ |
| 162 | (void) (false && count && arrayZ->sanitize (c)); |
| 163 | |
| 164 | return_trace (c->check_array (arrayZ, arrayZ[0].static_size, count)); |
| 165 | } |
| 166 | |
| 167 | protected: |
| 168 | Type arrayZ[VAR]; |
| 169 | public: |
| 170 | DEFINE_SIZE_ARRAY (0, arrayZ); |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | /* |
| 175 | * Lookup Table |
| 176 | */ |
| 177 | |
| 178 | template <typename T> struct Lookup; |
| 179 | |
| 180 | template <typename T> |
| 181 | struct LookupFormat0 |
| 182 | { |
| 183 | friend struct Lookup<T>; |
| 184 | |
| 185 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 186 | inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 187 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 188 | if (unlikely (glyph_id >= num_glyphs)) return nullptr; |
| 189 | return &arrayZ[glyph_id]; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 193 | { |
| 194 | TRACE_SANITIZE (this); |
| 195 | return_trace (arrayZ.sanitize (c, c->num_glyphs)); |
| 196 | } |
| 197 | |
| 198 | protected: |
| 199 | UINT16 format; /* Format identifier--format = 0 */ |
| 200 | UnsizedArrayOf<T> |
| 201 | arrayZ; /* Array of lookup values, indexed by glyph index. */ |
| 202 | public: |
| 203 | DEFINE_SIZE_ARRAY (2, arrayZ); |
| 204 | }; |
| 205 | |
| 206 | |
| 207 | template <typename T> |
| 208 | struct LookupSegmentSingle |
| 209 | { |
| 210 | inline int cmp (hb_codepoint_t g) const { |
| 211 | return g < first ? -1 : g <= last ? 0 : +1 ; |
| 212 | } |
| 213 | |
| 214 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 215 | { |
| 216 | TRACE_SANITIZE (this); |
| 217 | return_trace (c->check_struct (this) && value.sanitize (c)); |
| 218 | } |
| 219 | |
| 220 | GlyphID last; /* Last GlyphID in this segment */ |
| 221 | GlyphID first; /* First GlyphID in this segment */ |
| 222 | T value; /* The lookup value (only one) */ |
| 223 | public: |
| 224 | DEFINE_SIZE_STATIC (4 + sizeof (T)); |
| 225 | }; |
| 226 | |
| 227 | template <typename T> |
| 228 | struct LookupFormat2 |
| 229 | { |
| 230 | friend struct Lookup<T>; |
| 231 | |
| 232 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 233 | inline const T* get_value (hb_codepoint_t glyph_id) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 234 | { |
| 235 | const LookupSegmentSingle<T> *v = segments.bsearch (glyph_id); |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 236 | return v ? &v->value : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 240 | { |
| 241 | TRACE_SANITIZE (this); |
| 242 | return_trace (segments.sanitize (c)); |
| 243 | } |
| 244 | |
| 245 | protected: |
| 246 | UINT16 format; /* Format identifier--format = 2 */ |
| 247 | BinSearchArrayOf<LookupSegmentSingle<T> > |
| 248 | segments; /* The actual segments. These must already be sorted, |
| 249 | * according to the first word in each one (the last |
| 250 | * glyph in each segment). */ |
| 251 | public: |
| 252 | DEFINE_SIZE_ARRAY (8, segments); |
| 253 | }; |
| 254 | |
| 255 | template <typename T> |
| 256 | struct LookupSegmentArray |
| 257 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 258 | inline const T* get_value (hb_codepoint_t glyph_id, const void *base) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 259 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 260 | return first <= glyph_id && glyph_id <= last ? &(base+valuesZ)[glyph_id - first] : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | inline int cmp (hb_codepoint_t g) const { |
| 264 | return g < first ? -1 : g <= last ? 0 : +1 ; |
| 265 | } |
| 266 | |
| 267 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 268 | { |
| 269 | TRACE_SANITIZE (this); |
| 270 | return_trace (c->check_struct (this) && |
| 271 | first <= last && |
| 272 | valuesZ.sanitize (c, base, last - first + 1)); |
| 273 | } |
| 274 | |
| 275 | GlyphID last; /* Last GlyphID in this segment */ |
| 276 | GlyphID first; /* First GlyphID in this segment */ |
| 277 | OffsetTo<UnsizedArrayOf<T> > |
| 278 | valuesZ; /* A 16-bit offset from the start of |
| 279 | * the table to the data. */ |
| 280 | public: |
| 281 | DEFINE_SIZE_STATIC (6); |
| 282 | }; |
| 283 | |
| 284 | template <typename T> |
| 285 | struct LookupFormat4 |
| 286 | { |
| 287 | friend struct Lookup<T>; |
| 288 | |
| 289 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 290 | inline const T* get_value (hb_codepoint_t glyph_id) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 291 | { |
| 292 | const LookupSegmentArray<T> *v = segments.bsearch (glyph_id); |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 293 | return v ? v->get_value (glyph_id, this) : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 297 | { |
| 298 | TRACE_SANITIZE (this); |
| 299 | return_trace (segments.sanitize (c, this)); |
| 300 | } |
| 301 | |
| 302 | protected: |
| 303 | UINT16 format; /* Format identifier--format = 2 */ |
| 304 | BinSearchArrayOf<LookupSegmentArray<T> > |
| 305 | segments; /* The actual segments. These must already be sorted, |
| 306 | * according to the first word in each one (the last |
| 307 | * glyph in each segment). */ |
| 308 | public: |
| 309 | DEFINE_SIZE_ARRAY (8, segments); |
| 310 | }; |
| 311 | |
| 312 | template <typename T> |
| 313 | struct LookupSingle |
| 314 | { |
| 315 | inline int cmp (hb_codepoint_t g) const { return glyph.cmp (g); } |
| 316 | |
| 317 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 318 | { |
| 319 | TRACE_SANITIZE (this); |
| 320 | return_trace (c->check_struct (this) && value.sanitize (c)); |
| 321 | } |
| 322 | |
| 323 | GlyphID glyph; /* Last GlyphID */ |
| 324 | T value; /* The lookup value (only one) */ |
| 325 | public: |
| 326 | DEFINE_SIZE_STATIC (4 + sizeof (T)); |
| 327 | }; |
| 328 | |
| 329 | template <typename T> |
| 330 | struct LookupFormat6 |
| 331 | { |
| 332 | friend struct Lookup<T>; |
| 333 | |
| 334 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 335 | inline const T* get_value (hb_codepoint_t glyph_id) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 336 | { |
| 337 | const LookupSingle<T> *v = entries.bsearch (glyph_id); |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 338 | return v ? &v->value : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 342 | { |
| 343 | TRACE_SANITIZE (this); |
| 344 | return_trace (entries.sanitize (c)); |
| 345 | } |
| 346 | |
| 347 | protected: |
| 348 | UINT16 format; /* Format identifier--format = 6 */ |
| 349 | BinSearchArrayOf<LookupSingle<T> > |
| 350 | entries; /* The actual entries, sorted by glyph index. */ |
| 351 | public: |
| 352 | DEFINE_SIZE_ARRAY (8, entries); |
| 353 | }; |
| 354 | |
| 355 | template <typename T> |
| 356 | struct LookupFormat8 |
| 357 | { |
| 358 | friend struct Lookup<T>; |
| 359 | |
| 360 | private: |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 361 | inline const T* get_value (hb_codepoint_t glyph_id) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 362 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 363 | return firstGlyph <= glyph_id && glyph_id - firstGlyph < glyphCount ? &valueArrayZ[glyph_id - firstGlyph] : nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 367 | { |
| 368 | TRACE_SANITIZE (this); |
| 369 | return_trace (c->check_struct (this) && valueArrayZ.sanitize (c, glyphCount)); |
| 370 | } |
| 371 | |
| 372 | protected: |
| 373 | UINT16 format; /* Format identifier--format = 6 */ |
| 374 | GlyphID firstGlyph; /* First glyph index included in the trimmed array. */ |
| 375 | UINT16 glyphCount; /* Total number of glyphs (equivalent to the last |
| 376 | * glyph minus the value of firstGlyph plus 1). */ |
| 377 | UnsizedArrayOf<T> |
| 378 | valueArrayZ; /* The lookup values (indexed by the glyph index |
| 379 | * minus the value of firstGlyph). */ |
| 380 | public: |
| 381 | DEFINE_SIZE_ARRAY (6, valueArrayZ); |
| 382 | }; |
| 383 | |
| 384 | template <typename T> |
| 385 | struct Lookup |
| 386 | { |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 387 | inline const T* get_value (hb_codepoint_t glyph_id, unsigned int num_glyphs) const |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 388 | { |
| 389 | switch (u.format) { |
| 390 | case 0: return u.format0.get_value (glyph_id, num_glyphs); |
| 391 | case 2: return u.format2.get_value (glyph_id); |
| 392 | case 4: return u.format4.get_value (glyph_id); |
| 393 | case 6: return u.format6.get_value (glyph_id); |
| 394 | case 8: return u.format8.get_value (glyph_id); |
Behdad Esfahbod | 748b989 | 2018-01-09 17:55:17 +0100 | [diff] [blame^] | 395 | default:return nullptr; |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 400 | { |
| 401 | TRACE_SANITIZE (this); |
| 402 | if (!u.format.sanitize (c)) return_trace (false); |
| 403 | switch (u.format) { |
| 404 | case 0: return_trace (u.format0.sanitize (c)); |
| 405 | case 2: return_trace (u.format2.sanitize (c)); |
| 406 | case 4: return_trace (u.format4.sanitize (c)); |
| 407 | case 6: return_trace (u.format6.sanitize (c)); |
| 408 | case 8: return_trace (u.format8.sanitize (c)); |
| 409 | default:return_trace (true); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | protected: |
| 414 | union { |
| 415 | UINT16 format; /* Format identifier */ |
| 416 | LookupFormat0<T> format0; |
| 417 | LookupFormat2<T> format2; |
| 418 | LookupFormat4<T> format4; |
| 419 | LookupFormat6<T> format6; |
| 420 | LookupFormat8<T> format8; |
| 421 | } u; |
| 422 | public: |
| 423 | DEFINE_SIZE_UNION (2, format); |
| 424 | }; |
| 425 | |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 426 | |
Behdad Esfahbod | a0175e7 | 2017-08-17 16:55:54 -0700 | [diff] [blame] | 427 | } /* namespace AAT */ |
| 428 | |
| 429 | |
| 430 | #endif /* HB_AAT_LAYOUT_COMMON_PRIVATE_HH */ |