1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/harfbuzz/src/hb-private.hh Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,918 @@ 1.4 +/* 1.5 + * Copyright © 2007,2008,2009 Red Hat, Inc. 1.6 + * Copyright © 2011,2012 Google, Inc. 1.7 + * 1.8 + * This is part of HarfBuzz, a text shaping library. 1.9 + * 1.10 + * Permission is hereby granted, without written agreement and without 1.11 + * license or royalty fees, to use, copy, modify, and distribute this 1.12 + * software and its documentation for any purpose, provided that the 1.13 + * above copyright notice and the following two paragraphs appear in 1.14 + * all copies of this software. 1.15 + * 1.16 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR 1.17 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 1.18 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 1.19 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 1.20 + * DAMAGE. 1.21 + * 1.22 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 1.23 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 1.24 + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 1.25 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO 1.26 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 1.27 + * 1.28 + * Red Hat Author(s): Behdad Esfahbod 1.29 + * Google Author(s): Behdad Esfahbod 1.30 + */ 1.31 + 1.32 +#ifndef HB_PRIVATE_HH 1.33 +#define HB_PRIVATE_HH 1.34 + 1.35 +#ifdef HAVE_CONFIG_H 1.36 +#include "config.h" 1.37 +#endif 1.38 + 1.39 +#include "hb.h" 1.40 +#define HB_H_IN 1.41 +#ifdef HAVE_OT 1.42 +#include "hb-ot.h" 1.43 +#define HB_OT_H_IN 1.44 +#endif 1.45 + 1.46 +#include <stdlib.h> 1.47 +#include <stddef.h> 1.48 +#include <string.h> 1.49 +#include <assert.h> 1.50 + 1.51 +/* We only use these two for debug output. However, the debug code is 1.52 + * always seen by the compiler (and optimized out in non-debug builds. 1.53 + * If including these becomes a problem, we can start thinking about 1.54 + * someway around that. */ 1.55 +#include <stdio.h> 1.56 +#include <errno.h> 1.57 +#include <stdarg.h> 1.58 + 1.59 + 1.60 + 1.61 +/* Essentials */ 1.62 + 1.63 +#ifndef NULL 1.64 +# define NULL ((void *) 0) 1.65 +#endif 1.66 + 1.67 + 1.68 +/* Void! */ 1.69 +struct _hb_void_t {}; 1.70 +typedef const _hb_void_t &hb_void_t; 1.71 +#define HB_VOID (* (const _hb_void_t *) NULL) 1.72 + 1.73 + 1.74 +/* Basics */ 1.75 + 1.76 + 1.77 +#undef MIN 1.78 +template <typename Type> 1.79 +static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } 1.80 + 1.81 +#undef MAX 1.82 +template <typename Type> 1.83 +static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } 1.84 + 1.85 +static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b) 1.86 +{ return (a + (b - 1)) / b; } 1.87 + 1.88 + 1.89 +#undef ARRAY_LENGTH 1.90 +template <typename Type, unsigned int n> 1.91 +static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; } 1.92 +/* A const version, but does not detect erratically being called on pointers. */ 1.93 +#define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) 1.94 + 1.95 +#define HB_STMT_START do 1.96 +#define HB_STMT_END while (0) 1.97 + 1.98 +#define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] 1.99 +#define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) 1.100 +#define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) 1.101 + 1.102 +#define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1])) 1.103 +#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1])) 1.104 + 1.105 +#define _PASTE1(a,b) a##b 1.106 +#define PASTE(a,b) _PASTE1(a,b) 1.107 + 1.108 +/* Lets assert int types. Saves trouble down the road. */ 1.109 + 1.110 +ASSERT_STATIC (sizeof (int8_t) == 1); 1.111 +ASSERT_STATIC (sizeof (uint8_t) == 1); 1.112 +ASSERT_STATIC (sizeof (int16_t) == 2); 1.113 +ASSERT_STATIC (sizeof (uint16_t) == 2); 1.114 +ASSERT_STATIC (sizeof (int32_t) == 4); 1.115 +ASSERT_STATIC (sizeof (uint32_t) == 4); 1.116 +ASSERT_STATIC (sizeof (int64_t) == 8); 1.117 +ASSERT_STATIC (sizeof (uint64_t) == 8); 1.118 + 1.119 +ASSERT_STATIC (sizeof (hb_codepoint_t) == 4); 1.120 +ASSERT_STATIC (sizeof (hb_position_t) == 4); 1.121 +ASSERT_STATIC (sizeof (hb_mask_t) == 4); 1.122 +ASSERT_STATIC (sizeof (hb_var_int_t) == 4); 1.123 + 1.124 + 1.125 +/* We like our types POD */ 1.126 + 1.127 +#define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; } 1.128 +#define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type) 1.129 +#define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type) 1.130 + 1.131 +#ifdef __GNUC__ 1.132 +# define _ASSERT_INSTANCE_POD1(_line, _instance) \ 1.133 + HB_STMT_START { \ 1.134 + typedef __typeof__(_instance) _type_##_line; \ 1.135 + _ASSERT_TYPE_POD1 (_line, _type_##_line); \ 1.136 + } HB_STMT_END 1.137 +#else 1.138 +# define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested 1.139 +#endif 1.140 +# define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance) 1.141 +# define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance) 1.142 + 1.143 +/* Check _assertion in a method environment */ 1.144 +#define _ASSERT_POD1(_line) \ 1.145 + inline void _static_assertion_on_line_##_line (void) const \ 1.146 + { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ } 1.147 +# define _ASSERT_POD0(_line) _ASSERT_POD1 (_line) 1.148 +# define ASSERT_POD() _ASSERT_POD0 (__LINE__) 1.149 + 1.150 + 1.151 + 1.152 +/* Misc */ 1.153 + 1.154 + 1.155 +#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 1.156 +#define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0) 1.157 +#define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1)) 1.158 +#define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0)) 1.159 +#else 1.160 +#define likely(expr) (expr) 1.161 +#define unlikely(expr) (expr) 1.162 +#endif 1.163 + 1.164 +#ifndef __GNUC__ 1.165 +#undef __attribute__ 1.166 +#define __attribute__(x) 1.167 +#endif 1.168 + 1.169 +#if __GNUC__ >= 3 1.170 +#define HB_PURE_FUNC __attribute__((pure)) 1.171 +#define HB_CONST_FUNC __attribute__((const)) 1.172 +#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) 1.173 +#else 1.174 +#define HB_PURE_FUNC 1.175 +#define HB_CONST_FUNC 1.176 +#define HB_PRINTF_FUNC(format_idx, arg_idx) 1.177 +#endif 1.178 +#if __GNUC__ >= 4 1.179 +#define HB_UNUSED __attribute__((unused)) 1.180 +#else 1.181 +#define HB_UNUSED 1.182 +#endif 1.183 + 1.184 +#ifndef HB_INTERNAL 1.185 +# ifndef __MINGW32__ 1.186 +# define HB_INTERNAL __attribute__((__visibility__("hidden"))) 1.187 +# else 1.188 +# define HB_INTERNAL 1.189 +# endif 1.190 +#endif 1.191 + 1.192 + 1.193 +#if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER) 1.194 +#define snprintf _snprintf 1.195 +#endif 1.196 + 1.197 +#ifdef _MSC_VER 1.198 +#undef inline 1.199 +#define inline __inline 1.200 +#endif 1.201 + 1.202 +#ifdef __STRICT_ANSI__ 1.203 +#undef inline 1.204 +#define inline __inline__ 1.205 +#endif 1.206 + 1.207 + 1.208 +#if __GNUC__ >= 3 1.209 +#define HB_FUNC __PRETTY_FUNCTION__ 1.210 +#elif defined(_MSC_VER) 1.211 +#define HB_FUNC __FUNCSIG__ 1.212 +#else 1.213 +#define HB_FUNC __func__ 1.214 +#endif 1.215 + 1.216 + 1.217 +/* Return the number of 1 bits in mask. */ 1.218 +static inline HB_CONST_FUNC unsigned int 1.219 +_hb_popcount32 (uint32_t mask) 1.220 +{ 1.221 +#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 1.222 + return __builtin_popcount (mask); 1.223 +#else 1.224 + /* "HACKMEM 169" */ 1.225 + register uint32_t y; 1.226 + y = (mask >> 1) &033333333333; 1.227 + y = mask - y - ((y >>1) & 033333333333); 1.228 + return (((y + (y >> 3)) & 030707070707) % 077); 1.229 +#endif 1.230 +} 1.231 + 1.232 +/* Returns the number of bits needed to store number */ 1.233 +static inline HB_CONST_FUNC unsigned int 1.234 +_hb_bit_storage (unsigned int number) 1.235 +{ 1.236 +#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 1.237 + return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0; 1.238 +#else 1.239 + register unsigned int n_bits = 0; 1.240 + while (number) { 1.241 + n_bits++; 1.242 + number >>= 1; 1.243 + } 1.244 + return n_bits; 1.245 +#endif 1.246 +} 1.247 + 1.248 +/* Returns the number of zero bits in the least significant side of number */ 1.249 +static inline HB_CONST_FUNC unsigned int 1.250 +_hb_ctz (unsigned int number) 1.251 +{ 1.252 +#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) 1.253 + return likely (number) ? __builtin_ctz (number) : 0; 1.254 +#else 1.255 + register unsigned int n_bits = 0; 1.256 + if (unlikely (!number)) return 0; 1.257 + while (!(number & 1)) { 1.258 + n_bits++; 1.259 + number >>= 1; 1.260 + } 1.261 + return n_bits; 1.262 +#endif 1.263 +} 1.264 + 1.265 +static inline bool 1.266 +_hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) 1.267 +{ 1.268 + return (size > 0) && (count >= ((unsigned int) -1) / size); 1.269 +} 1.270 + 1.271 + 1.272 +/* Type of bsearch() / qsort() compare function */ 1.273 +typedef int (*hb_compare_func_t) (const void *, const void *); 1.274 + 1.275 + 1.276 + 1.277 + 1.278 +/* arrays and maps */ 1.279 + 1.280 + 1.281 +#define HB_PREALLOCED_ARRAY_INIT {0} 1.282 +template <typename Type, unsigned int StaticSize> 1.283 +struct hb_prealloced_array_t 1.284 +{ 1.285 + unsigned int len; 1.286 + unsigned int allocated; 1.287 + Type *array; 1.288 + Type static_array[StaticSize]; 1.289 + 1.290 + void init (void) { memset (this, 0, sizeof (*this)); } 1.291 + 1.292 + inline Type& operator [] (unsigned int i) { return array[i]; } 1.293 + inline const Type& operator [] (unsigned int i) const { return array[i]; } 1.294 + 1.295 + inline Type *push (void) 1.296 + { 1.297 + if (!array) { 1.298 + array = static_array; 1.299 + allocated = ARRAY_LENGTH (static_array); 1.300 + } 1.301 + if (likely (len < allocated)) 1.302 + return &array[len++]; 1.303 + 1.304 + /* Need to reallocate */ 1.305 + unsigned int new_allocated = allocated + (allocated >> 1) + 8; 1.306 + Type *new_array = NULL; 1.307 + 1.308 + if (array == static_array) { 1.309 + new_array = (Type *) calloc (new_allocated, sizeof (Type)); 1.310 + if (new_array) 1.311 + memcpy (new_array, array, len * sizeof (Type)); 1.312 + } else { 1.313 + bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); 1.314 + if (likely (!overflows)) { 1.315 + new_array = (Type *) realloc (array, new_allocated * sizeof (Type)); 1.316 + } 1.317 + } 1.318 + 1.319 + if (unlikely (!new_array)) 1.320 + return NULL; 1.321 + 1.322 + array = new_array; 1.323 + allocated = new_allocated; 1.324 + return &array[len++]; 1.325 + } 1.326 + 1.327 + inline void pop (void) 1.328 + { 1.329 + len--; 1.330 + } 1.331 + 1.332 + inline void remove (unsigned int i) 1.333 + { 1.334 + if (unlikely (i >= len)) 1.335 + return; 1.336 + memmove (static_cast<void *> (&array[i]), 1.337 + static_cast<void *> (&array[i + 1]), 1.338 + (len - i - 1) * sizeof (Type)); 1.339 + len--; 1.340 + } 1.341 + 1.342 + inline void shrink (unsigned int l) 1.343 + { 1.344 + if (l < len) 1.345 + len = l; 1.346 + } 1.347 + 1.348 + template <typename T> 1.349 + inline Type *find (T v) { 1.350 + for (unsigned int i = 0; i < len; i++) 1.351 + if (array[i] == v) 1.352 + return &array[i]; 1.353 + return NULL; 1.354 + } 1.355 + template <typename T> 1.356 + inline const Type *find (T v) const { 1.357 + for (unsigned int i = 0; i < len; i++) 1.358 + if (array[i] == v) 1.359 + return &array[i]; 1.360 + return NULL; 1.361 + } 1.362 + 1.363 + inline void sort (void) 1.364 + { 1.365 + qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 1.366 + } 1.367 + 1.368 + inline void sort (unsigned int start, unsigned int end) 1.369 + { 1.370 + qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp); 1.371 + } 1.372 + 1.373 + template <typename T> 1.374 + inline Type *bsearch (T *key) 1.375 + { 1.376 + return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 1.377 + } 1.378 + template <typename T> 1.379 + inline const Type *bsearch (T *key) const 1.380 + { 1.381 + return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); 1.382 + } 1.383 + 1.384 + inline void finish (void) 1.385 + { 1.386 + if (array != static_array) 1.387 + free (array); 1.388 + array = NULL; 1.389 + allocated = len = 0; 1.390 + } 1.391 +}; 1.392 + 1.393 +#define HB_AUTO_ARRAY_PREALLOCED 16 1.394 +template <typename Type> 1.395 +struct hb_auto_array_t : hb_prealloced_array_t <Type, HB_AUTO_ARRAY_PREALLOCED> 1.396 +{ 1.397 + hb_auto_array_t (void) { hb_prealloced_array_t<Type, HB_AUTO_ARRAY_PREALLOCED>::init (); } 1.398 + ~hb_auto_array_t (void) { hb_prealloced_array_t<Type, HB_AUTO_ARRAY_PREALLOCED>::finish (); } 1.399 +}; 1.400 + 1.401 + 1.402 +#define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT} 1.403 +template <typename item_t, typename lock_t> 1.404 +struct hb_lockable_set_t 1.405 +{ 1.406 + hb_prealloced_array_t <item_t, 2> items; 1.407 + 1.408 + inline void init (void) { items.init (); } 1.409 + 1.410 + template <typename T> 1.411 + inline item_t *replace_or_insert (T v, lock_t &l, bool replace) 1.412 + { 1.413 + l.lock (); 1.414 + item_t *item = items.find (v); 1.415 + if (item) { 1.416 + if (replace) { 1.417 + item_t old = *item; 1.418 + *item = v; 1.419 + l.unlock (); 1.420 + old.finish (); 1.421 + } 1.422 + else { 1.423 + item = NULL; 1.424 + l.unlock (); 1.425 + } 1.426 + } else { 1.427 + item = items.push (); 1.428 + if (likely (item)) 1.429 + *item = v; 1.430 + l.unlock (); 1.431 + } 1.432 + return item; 1.433 + } 1.434 + 1.435 + template <typename T> 1.436 + inline void remove (T v, lock_t &l) 1.437 + { 1.438 + l.lock (); 1.439 + item_t *item = items.find (v); 1.440 + if (item) { 1.441 + item_t old = *item; 1.442 + *item = items[items.len - 1]; 1.443 + items.pop (); 1.444 + l.unlock (); 1.445 + old.finish (); 1.446 + } else { 1.447 + l.unlock (); 1.448 + } 1.449 + } 1.450 + 1.451 + template <typename T> 1.452 + inline bool find (T v, item_t *i, lock_t &l) 1.453 + { 1.454 + l.lock (); 1.455 + item_t *item = items.find (v); 1.456 + if (item) 1.457 + *i = *item; 1.458 + l.unlock (); 1.459 + return !!item; 1.460 + } 1.461 + 1.462 + template <typename T> 1.463 + inline item_t *find_or_insert (T v, lock_t &l) 1.464 + { 1.465 + l.lock (); 1.466 + item_t *item = items.find (v); 1.467 + if (!item) { 1.468 + item = items.push (); 1.469 + if (likely (item)) 1.470 + *item = v; 1.471 + } 1.472 + l.unlock (); 1.473 + return item; 1.474 + } 1.475 + 1.476 + inline void finish (lock_t &l) 1.477 + { 1.478 + if (!items.len) { 1.479 + /* No need for locking. */ 1.480 + items.finish (); 1.481 + return; 1.482 + } 1.483 + l.lock (); 1.484 + while (items.len) { 1.485 + item_t old = items[items.len - 1]; 1.486 + items.pop (); 1.487 + l.unlock (); 1.488 + old.finish (); 1.489 + l.lock (); 1.490 + } 1.491 + items.finish (); 1.492 + l.unlock (); 1.493 + } 1.494 + 1.495 +}; 1.496 + 1.497 + 1.498 + 1.499 + 1.500 +/* Big-endian handling */ 1.501 + 1.502 +static inline uint16_t hb_be_uint16 (const uint16_t v) 1.503 +{ 1.504 + const uint8_t *V = (const uint8_t *) &v; 1.505 + return (V[0] << 8) | V[1]; 1.506 +} 1.507 + 1.508 +static inline uint16_t hb_uint16_swap (const uint16_t v) 1.509 +{ 1.510 + return (v >> 8) | (v << 8); 1.511 +} 1.512 + 1.513 +static inline uint32_t hb_uint32_swap (const uint32_t v) 1.514 +{ 1.515 + return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); 1.516 +} 1.517 + 1.518 +/* Note, of the following macros, uint16_get is the one called many many times. 1.519 + * If there is any optimizations to be done, it's in that macro. However, I 1.520 + * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which 1.521 + * results in a single ror instruction, does NOT speed this up. In fact, it 1.522 + * resulted in a minor slowdown. At any rate, note that v may not be correctly 1.523 + * aligned, so I think the current implementation is optimal. 1.524 + */ 1.525 + 1.526 +#define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END 1.527 +#define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1]) 1.528 +#define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1]) 1.529 + 1.530 +#define hb_be_uint32_put(v,V) HB_STMT_START { v[0] = (V>>24); v[1] = (V>>16); v[2] = (V>>8); v[3] = (V); } HB_STMT_END 1.531 +#define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3]) 1.532 +#define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]) 1.533 + 1.534 +#define hb_be_uint24_put(v,V) HB_STMT_START { v[0] = (V>>16); v[1] = (V>>8); v[2] = (V); } HB_STMT_END 1.535 +#define hb_be_uint24_get(v) (uint32_t) ((v[0] << 16) + (v[1] << 8) + v[2]) 1.536 +#define hb_be_uint24_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) 1.537 + 1.538 + 1.539 +/* ASCII tag/character handling */ 1.540 + 1.541 +static inline bool ISALPHA (unsigned char c) 1.542 +{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } 1.543 +static inline bool ISALNUM (unsigned char c) 1.544 +{ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } 1.545 +static inline bool ISSPACE (unsigned char c) 1.546 +{ return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; } 1.547 +static inline unsigned char TOUPPER (unsigned char c) 1.548 +{ return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } 1.549 +static inline unsigned char TOLOWER (unsigned char c) 1.550 +{ return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } 1.551 + 1.552 +#define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \ 1.553 + ((const char *) s)[1], \ 1.554 + ((const char *) s)[2], \ 1.555 + ((const char *) s)[3])) 1.556 + 1.557 + 1.558 +/* C++ helpers */ 1.559 + 1.560 +/* Makes class uncopyable. Use in private: section. */ 1.561 +#define NO_COPY(T) \ 1.562 + T (const T &o); \ 1.563 + T &operator = (const T &o) 1.564 + 1.565 + 1.566 +/* Debug */ 1.567 + 1.568 + 1.569 +#ifndef HB_DEBUG 1.570 +#define HB_DEBUG 0 1.571 +#endif 1.572 + 1.573 +static inline bool 1.574 +_hb_debug (unsigned int level, 1.575 + unsigned int max_level) 1.576 +{ 1.577 + return level < max_level; 1.578 +} 1.579 + 1.580 +#define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT)) 1.581 +#define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0)) 1.582 + 1.583 +template <int max_level> static inline void 1.584 +_hb_debug_msg_va (const char *what, 1.585 + const void *obj, 1.586 + const char *func, 1.587 + bool indented, 1.588 + unsigned int level, 1.589 + int level_dir, 1.590 + const char *message, 1.591 + va_list ap) 1.592 +{ 1.593 + if (!_hb_debug (level, max_level)) 1.594 + return; 1.595 + 1.596 + fprintf (stderr, "%-10s", what ? what : ""); 1.597 + 1.598 + if (obj) 1.599 + fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj); 1.600 + else 1.601 + fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), ""); 1.602 + 1.603 + if (indented) { 1.604 +/* One may want to add ASCII version of these. See: 1.605 + * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */ 1.606 +#define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */ 1.607 +#define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */ 1.608 +#define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */ 1.609 +#define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */ 1.610 +#define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */ 1.611 + static const char bars[] = VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR; 1.612 + fprintf (stderr, "%2u %s" VRBAR "%s", 1.613 + level, 1.614 + bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level), 1.615 + level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR); 1.616 + } else 1.617 + fprintf (stderr, " " VRBAR LBAR); 1.618 + 1.619 + if (func) 1.620 + { 1.621 + unsigned int func_len = strlen (func); 1.622 +#ifndef HB_DEBUG_VERBOSE 1.623 + /* Skip "typename" */ 1.624 + if (0 == strncmp (func, "typename ", 9)) 1.625 + func += 9; 1.626 + /* Skip return type */ 1.627 + const char *space = strchr (func, ' '); 1.628 + if (space) 1.629 + func = space + 1; 1.630 + /* Skip parameter list */ 1.631 + const char *paren = strchr (func, '('); 1.632 + if (paren) 1.633 + func_len = paren - func; 1.634 +#endif 1.635 + fprintf (stderr, "%.*s: ", func_len, func); 1.636 + } 1.637 + 1.638 + if (message) 1.639 + vfprintf (stderr, message, ap); 1.640 + 1.641 + fprintf (stderr, "\n"); 1.642 +} 1.643 +template <> inline void 1.644 +_hb_debug_msg_va<0> (const char *what HB_UNUSED, 1.645 + const void *obj HB_UNUSED, 1.646 + const char *func HB_UNUSED, 1.647 + bool indented HB_UNUSED, 1.648 + unsigned int level HB_UNUSED, 1.649 + int level_dir HB_UNUSED, 1.650 + const char *message HB_UNUSED, 1.651 + va_list ap HB_UNUSED) {} 1.652 + 1.653 +template <int max_level> static inline void 1.654 +_hb_debug_msg (const char *what, 1.655 + const void *obj, 1.656 + const char *func, 1.657 + bool indented, 1.658 + unsigned int level, 1.659 + int level_dir, 1.660 + const char *message, 1.661 + ...) HB_PRINTF_FUNC(7, 8); 1.662 +template <int max_level> static inline void 1.663 +_hb_debug_msg (const char *what, 1.664 + const void *obj, 1.665 + const char *func, 1.666 + bool indented, 1.667 + unsigned int level, 1.668 + int level_dir, 1.669 + const char *message, 1.670 + ...) 1.671 +{ 1.672 + va_list ap; 1.673 + va_start (ap, message); 1.674 + _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap); 1.675 + va_end (ap); 1.676 +} 1.677 +template <> inline void 1.678 +_hb_debug_msg<0> (const char *what HB_UNUSED, 1.679 + const void *obj HB_UNUSED, 1.680 + const char *func HB_UNUSED, 1.681 + bool indented HB_UNUSED, 1.682 + unsigned int level HB_UNUSED, 1.683 + int level_dir HB_UNUSED, 1.684 + const char *message HB_UNUSED, 1.685 + ...) HB_PRINTF_FUNC(7, 8); 1.686 +template <> inline void 1.687 +_hb_debug_msg<0> (const char *what HB_UNUSED, 1.688 + const void *obj HB_UNUSED, 1.689 + const char *func HB_UNUSED, 1.690 + bool indented HB_UNUSED, 1.691 + unsigned int level HB_UNUSED, 1.692 + int level_dir HB_UNUSED, 1.693 + const char *message HB_UNUSED, 1.694 + ...) {} 1.695 + 1.696 +#define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__) 1.697 +#define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__) 1.698 +#define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__) 1.699 + 1.700 + 1.701 +/* 1.702 + * Printer 1.703 + */ 1.704 + 1.705 +template <typename T> 1.706 +struct hb_printer_t {}; 1.707 + 1.708 +template <> 1.709 +struct hb_printer_t<bool> { 1.710 + const char *print (bool v) { return v ? "true" : "false"; } 1.711 +}; 1.712 + 1.713 +template <> 1.714 +struct hb_printer_t<hb_void_t> { 1.715 + const char *print (hb_void_t) { return ""; } 1.716 +}; 1.717 + 1.718 + 1.719 +/* 1.720 + * Trace 1.721 + */ 1.722 + 1.723 +template <typename T> 1.724 +static inline void _hb_warn_no_return (bool returned) 1.725 +{ 1.726 + if (unlikely (!returned)) { 1.727 + fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n"); 1.728 + } 1.729 +} 1.730 +template <> 1.731 +inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED) 1.732 +{} 1.733 + 1.734 +template <int max_level, typename ret_t> 1.735 +struct hb_auto_trace_t { 1.736 + explicit inline hb_auto_trace_t (unsigned int *plevel_, 1.737 + const char *what_, 1.738 + const void *obj_, 1.739 + const char *func, 1.740 + const char *message, 1.741 + ...) : plevel (plevel_), what (what_), obj (obj_), returned (false) 1.742 + { 1.743 + if (plevel) ++*plevel; 1.744 + 1.745 + va_list ap; 1.746 + va_start (ap, message); 1.747 + _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap); 1.748 + va_end (ap); 1.749 + } 1.750 + inline ~hb_auto_trace_t (void) 1.751 + { 1.752 + _hb_warn_no_return<ret_t> (returned); 1.753 + if (!returned) { 1.754 + _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " "); 1.755 + } 1.756 + if (plevel) --*plevel; 1.757 + } 1.758 + 1.759 + inline ret_t ret (ret_t v, unsigned int line = 0) 1.760 + { 1.761 + if (unlikely (returned)) { 1.762 + fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n"); 1.763 + return v; 1.764 + } 1.765 + 1.766 + _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, 1.767 + "return %s (line %d)", 1.768 + hb_printer_t<ret_t>().print (v), line); 1.769 + if (plevel) --*plevel; 1.770 + plevel = NULL; 1.771 + returned = true; 1.772 + return v; 1.773 + } 1.774 + 1.775 + private: 1.776 + unsigned int *plevel; 1.777 + const char *what; 1.778 + const void *obj; 1.779 + bool returned; 1.780 +}; 1.781 +template <typename ret_t> /* Optimize when tracing is disabled */ 1.782 +struct hb_auto_trace_t<0, ret_t> { 1.783 + explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED, 1.784 + const char *what HB_UNUSED, 1.785 + const void *obj HB_UNUSED, 1.786 + const char *func HB_UNUSED, 1.787 + const char *message HB_UNUSED, 1.788 + ...) {} 1.789 + 1.790 + inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; } 1.791 +}; 1.792 + 1.793 +#define TRACE_RETURN(RET) trace.ret (RET, __LINE__) 1.794 + 1.795 +/* Misc */ 1.796 + 1.797 + 1.798 +/* Pre-mature optimization: 1.799 + * Checks for lo <= u <= hi but with an optimization if lo and hi 1.800 + * are only different in a contiguous set of lower-most bits. 1.801 + */ 1.802 +template <typename T> static inline bool 1.803 +hb_in_range (T u, T lo, T hi) 1.804 +{ 1.805 + if ( ((lo^hi) & lo) == 0 && 1.806 + ((lo^hi) & hi) == (lo^hi) && 1.807 + ((lo^hi) & ((lo^hi) + 1)) == 0 ) 1.808 + return (u & ~(lo^hi)) == lo; 1.809 + else 1.810 + return lo <= u && u <= hi; 1.811 +} 1.812 + 1.813 +template <typename T> static inline bool 1.814 +hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2) 1.815 +{ 1.816 + return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2); 1.817 +} 1.818 + 1.819 +template <typename T> static inline bool 1.820 +hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3) 1.821 +{ 1.822 + return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3); 1.823 +} 1.824 + 1.825 + 1.826 +/* Useful for set-operations on small enums. 1.827 + * For example, for testing "x ∈ {x1, x2, x3}" use: 1.828 + * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) 1.829 + */ 1.830 +#define FLAG(x) (1<<(x)) 1.831 +#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x)) 1.832 + 1.833 + 1.834 +template <typename T, typename T2> inline void 1.835 +hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2) 1.836 +{ 1.837 + if (unlikely (!len)) 1.838 + return; 1.839 + 1.840 + unsigned int k = len - 1; 1.841 + do { 1.842 + unsigned int new_k = 0; 1.843 + 1.844 + for (unsigned int j = 0; j < k; j++) 1.845 + if (compar (&array[j], &array[j+1]) > 0) 1.846 + { 1.847 + { 1.848 + T t; 1.849 + t = array[j]; 1.850 + array[j] = array[j + 1]; 1.851 + array[j + 1] = t; 1.852 + } 1.853 + if (array2) 1.854 + { 1.855 + T2 t; 1.856 + t = array2[j]; 1.857 + array2[j] = array2[j + 1]; 1.858 + array2[j + 1] = t; 1.859 + } 1.860 + 1.861 + new_k = j; 1.862 + } 1.863 + k = new_k; 1.864 + } while (k); 1.865 +} 1.866 + 1.867 +template <typename T> inline void 1.868 +hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) 1.869 +{ 1.870 + hb_bubble_sort (array, len, compar, (int *) NULL); 1.871 +} 1.872 + 1.873 +static inline hb_bool_t 1.874 +hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out) 1.875 +{ 1.876 + /* Pain because we don't know whether s is nul-terminated. */ 1.877 + char buf[64]; 1.878 + len = MIN (ARRAY_LENGTH (buf) - 1, len); 1.879 + strncpy (buf, s, len); 1.880 + buf[len] = '\0'; 1.881 + 1.882 + char *end; 1.883 + errno = 0; 1.884 + unsigned long v = strtoul (buf, &end, base); 1.885 + if (errno) return false; 1.886 + if (*end) return false; 1.887 + *out = v; 1.888 + return true; 1.889 +} 1.890 + 1.891 + 1.892 +/* Global runtime options. */ 1.893 + 1.894 +struct hb_options_t 1.895 +{ 1.896 + int initialized : 1; 1.897 + int uniscribe_bug_compatible : 1; 1.898 +}; 1.899 + 1.900 +union hb_options_union_t { 1.901 + int i; 1.902 + hb_options_t opts; 1.903 +}; 1.904 +ASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t)); 1.905 + 1.906 +HB_INTERNAL void 1.907 +_hb_options_init (void); 1.908 + 1.909 +extern HB_INTERNAL hb_options_union_t _hb_options; 1.910 + 1.911 +static inline hb_options_t 1.912 +hb_options (void) 1.913 +{ 1.914 + if (unlikely (!_hb_options.i)) 1.915 + _hb_options_init (); 1.916 + 1.917 + return _hb_options.opts; 1.918 +} 1.919 + 1.920 + 1.921 +#endif /* HB_PRIVATE_HH */