Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / harfbuzz-ng / src / hb-private.hh
blob8112934324d85bc18e99afc4d36ebcf921f6c13f
1 /*
2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2011,2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_PRIVATE_HH
30 #define HB_PRIVATE_HH
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include "hb.h"
37 #define HB_H_IN
38 #ifdef HAVE_OT
39 #include "hb-ot.h"
40 #define HB_OT_H_IN
41 #endif
43 #include <stdlib.h>
44 #include <stddef.h>
45 #include <string.h>
46 #include <assert.h>
48 /* We only use these two for debug output. However, the debug code is
49 * always seen by the compiler (and optimized out in non-debug builds.
50 * If including these becomes a problem, we can start thinking about
51 * someway around that. */
52 #include <stdio.h>
53 #include <errno.h>
54 #include <stdarg.h>
57 /* Compiler attributes */
60 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
61 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
62 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
63 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
64 #else
65 #define likely(expr) (expr)
66 #define unlikely(expr) (expr)
67 #endif
69 #ifndef __GNUC__
70 #undef __attribute__
71 #define __attribute__(x)
72 #endif
74 #if __GNUC__ >= 3
75 #define HB_PURE_FUNC __attribute__((pure))
76 #define HB_CONST_FUNC __attribute__((const))
77 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
78 #else
79 #define HB_PURE_FUNC
80 #define HB_CONST_FUNC
81 #define HB_PRINTF_FUNC(format_idx, arg_idx)
82 #endif
83 #if __GNUC__ >= 4
84 #define HB_UNUSED __attribute__((unused))
85 #else
86 #define HB_UNUSED
87 #endif
89 #ifndef HB_INTERNAL
90 # if !defined(__MINGW32__) && !defined(__CYGWIN__)
91 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
92 # else
93 # define HB_INTERNAL
94 # endif
95 #endif
97 #if __GNUC__ >= 3
98 #define HB_FUNC __PRETTY_FUNCTION__
99 #elif defined(_MSC_VER)
100 #define HB_FUNC __FUNCSIG__
101 #else
102 #define HB_FUNC __func__
103 #endif
105 #if defined(_WIN32) || defined(__CYGWIN__)
106 /* We need Windows Vista for both Uniscribe backend and for
107 * MemoryBarrier. We don't support compiling on Windows XP,
108 * though we run on it fine. */
109 # if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
110 # undef _WIN32_WINNT
111 # endif
112 # ifndef _WIN32_WINNT
113 # define _WIN32_WINNT 0x0600
114 # endif
115 # ifndef WIN32_LEAN_AND_MEAN
116 # define WIN32_LEAN_AND_MEAN 1
117 # endif
118 # ifndef STRICT
119 # define STRICT 1
120 # endif
122 # if defined(_WIN32_WCE)
123 /* Some things not defined on Windows CE. */
124 # define strdup _strdup
125 # define getenv(Name) NULL
126 # if _WIN32_WCE < 0x800
127 # define setlocale(Category, Locale) "C"
128 static int errno = 0; /* Use something better? */
129 # endif
130 # elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
131 # define getenv(Name) NULL
132 # endif
133 # if defined(_MSC_VER) && _MSC_VER < 1900
134 # define snprintf _snprintf
135 # endif
136 #endif
138 #if HAVE_ATEXIT
139 /* atexit() is only safe to be called from shared libraries on certain
140 * platforms. Whitelist.
141 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
142 # if defined(__linux) && defined(__GLIBC_PREREQ)
143 # if __GLIBC_PREREQ(2,3)
144 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
145 # define HB_USE_ATEXIT 1
146 # endif
147 # elif defined(_MSC_VER) || defined(__MINGW32__)
148 /* For MSVC:
149 * http://msdn.microsoft.com/en-ca/library/tze57ck3.aspx
150 * http://msdn.microsoft.com/en-ca/library/zk17ww08.aspx
151 * mingw32 headers say atexit is safe to use in shared libraries.
153 # define HB_USE_ATEXIT 1
154 # elif defined(__ANDROID__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
155 /* This was fixed in Android NKD r8 or r8b:
156 * https://code.google.com/p/android/issues/detail?id=6455
157 * which introduced GCC 4.6:
158 * https://developer.android.com/tools/sdk/ndk/index.html
160 # define HB_USE_ATEXIT 1
161 # endif
162 #endif
164 /* Basics */
167 #ifndef NULL
168 # define NULL ((void *) 0)
169 #endif
171 #undef MIN
172 template <typename Type>
173 static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
175 #undef MAX
176 template <typename Type>
177 static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
179 static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b)
180 { return (a + (b - 1)) / b; }
183 #undef ARRAY_LENGTH
184 template <typename Type, unsigned int n>
185 static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
186 /* A const version, but does not detect erratically being called on pointers. */
187 #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
189 #define HB_STMT_START do
190 #define HB_STMT_END while (0)
192 #define _ASSERT_STATIC1(_line, _cond) HB_UNUSED typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
193 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
194 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
196 /* Note: C++ allows sizeof() of variable-lengh arrays. So, if _cond is not
197 * constant, it still compiles (ouch!), but at least we'll get a -Wvla warning. */
198 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
200 #define _PASTE1(a,b) a##b
201 #define PASTE(a,b) _PASTE1(a,b)
203 /* Lets assert int types. Saves trouble down the road. */
205 ASSERT_STATIC (sizeof (int8_t) == 1);
206 ASSERT_STATIC (sizeof (uint8_t) == 1);
207 ASSERT_STATIC (sizeof (int16_t) == 2);
208 ASSERT_STATIC (sizeof (uint16_t) == 2);
209 ASSERT_STATIC (sizeof (int32_t) == 4);
210 ASSERT_STATIC (sizeof (uint32_t) == 4);
211 ASSERT_STATIC (sizeof (int64_t) == 8);
212 ASSERT_STATIC (sizeof (uint64_t) == 8);
214 ASSERT_STATIC (sizeof (hb_codepoint_t) == 4);
215 ASSERT_STATIC (sizeof (hb_position_t) == 4);
216 ASSERT_STATIC (sizeof (hb_mask_t) == 4);
217 ASSERT_STATIC (sizeof (hb_var_int_t) == 4);
220 /* We like our types POD */
222 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
223 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
224 #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
226 #ifdef __GNUC__
227 # define _ASSERT_INSTANCE_POD1(_line, _instance) \
228 HB_STMT_START { \
229 typedef __typeof__(_instance) _type_##_line; \
230 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
231 } HB_STMT_END
232 #else
233 # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
234 #endif
235 # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
236 # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
238 /* Check _assertion in a method environment */
239 #define _ASSERT_POD1(_line) \
240 HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \
241 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
242 # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
243 # define ASSERT_POD() _ASSERT_POD0 (__LINE__)
247 /* Misc */
249 /* Void! */
250 struct _hb_void_t {};
251 typedef const _hb_void_t *hb_void_t;
252 #define HB_VOID ((const _hb_void_t *) NULL)
254 /* Return the number of 1 bits in mask. */
255 static inline HB_CONST_FUNC unsigned int
256 _hb_popcount32 (uint32_t mask)
258 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
259 return __builtin_popcount (mask);
260 #else
261 /* "HACKMEM 169" */
262 uint32_t y;
263 y = (mask >> 1) &033333333333;
264 y = mask - y - ((y >>1) & 033333333333);
265 return (((y + (y >> 3)) & 030707070707) % 077);
266 #endif
269 /* Returns the number of bits needed to store number */
270 static inline HB_CONST_FUNC unsigned int
271 _hb_bit_storage (unsigned int number)
273 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
274 return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0;
275 #else
276 unsigned int n_bits = 0;
277 while (number) {
278 n_bits++;
279 number >>= 1;
281 return n_bits;
282 #endif
285 /* Returns the number of zero bits in the least significant side of number */
286 static inline HB_CONST_FUNC unsigned int
287 _hb_ctz (unsigned int number)
289 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
290 return likely (number) ? __builtin_ctz (number) : 0;
291 #else
292 unsigned int n_bits = 0;
293 if (unlikely (!number)) return 0;
294 while (!(number & 1)) {
295 n_bits++;
296 number >>= 1;
298 return n_bits;
299 #endif
302 static inline bool
303 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
305 return (size > 0) && (count >= ((unsigned int) -1) / size);
309 /* Type of bsearch() / qsort() compare function */
310 typedef int (*hb_compare_func_t) (const void *, const void *);
315 /* arrays and maps */
318 #define HB_PREALLOCED_ARRAY_INIT {0, 0, NULL}
319 template <typename Type, unsigned int StaticSize=16>
320 struct hb_prealloced_array_t
322 unsigned int len;
323 unsigned int allocated;
324 Type *array;
325 Type static_array[StaticSize];
327 void init (void) { memset (this, 0, sizeof (*this)); }
329 inline Type& operator [] (unsigned int i) { return array[i]; }
330 inline const Type& operator [] (unsigned int i) const { return array[i]; }
332 inline Type *push (void)
334 if (!array) {
335 array = static_array;
336 allocated = ARRAY_LENGTH (static_array);
338 if (likely (len < allocated))
339 return &array[len++];
341 /* Need to reallocate */
342 unsigned int new_allocated = allocated + (allocated >> 1) + 8;
343 Type *new_array = NULL;
345 if (array == static_array) {
346 new_array = (Type *) calloc (new_allocated, sizeof (Type));
347 if (new_array)
348 memcpy (new_array, array, len * sizeof (Type));
349 } else {
350 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
351 if (likely (!overflows)) {
352 new_array = (Type *) realloc (array, new_allocated * sizeof (Type));
356 if (unlikely (!new_array))
357 return NULL;
359 array = new_array;
360 allocated = new_allocated;
361 return &array[len++];
364 inline void pop (void)
366 len--;
369 inline void remove (unsigned int i)
371 if (unlikely (i >= len))
372 return;
373 memmove (static_cast<void *> (&array[i]),
374 static_cast<void *> (&array[i + 1]),
375 (len - i - 1) * sizeof (Type));
376 len--;
379 inline void shrink (unsigned int l)
381 if (l < len)
382 len = l;
385 template <typename T>
386 inline Type *find (T v) {
387 for (unsigned int i = 0; i < len; i++)
388 if (array[i] == v)
389 return &array[i];
390 return NULL;
392 template <typename T>
393 inline const Type *find (T v) const {
394 for (unsigned int i = 0; i < len; i++)
395 if (array[i] == v)
396 return &array[i];
397 return NULL;
400 inline void qsort (void)
402 ::qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
405 inline void qsort (unsigned int start, unsigned int end)
407 ::qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp);
410 template <typename T>
411 inline Type *bsearch (T *key)
413 return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
415 template <typename T>
416 inline const Type *bsearch (T *key) const
418 return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp);
421 inline void finish (void)
423 if (array != static_array)
424 free (array);
425 array = NULL;
426 allocated = len = 0;
430 template <typename Type>
431 struct hb_auto_array_t : hb_prealloced_array_t <Type>
433 hb_auto_array_t (void) { hb_prealloced_array_t<Type>::init (); }
434 ~hb_auto_array_t (void) { hb_prealloced_array_t<Type>::finish (); }
438 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
439 template <typename item_t, typename lock_t>
440 struct hb_lockable_set_t
442 hb_prealloced_array_t <item_t, 2> items;
444 inline void init (void) { items.init (); }
446 template <typename T>
447 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
449 l.lock ();
450 item_t *item = items.find (v);
451 if (item) {
452 if (replace) {
453 item_t old = *item;
454 *item = v;
455 l.unlock ();
456 old.finish ();
458 else {
459 item = NULL;
460 l.unlock ();
462 } else {
463 item = items.push ();
464 if (likely (item))
465 *item = v;
466 l.unlock ();
468 return item;
471 template <typename T>
472 inline void remove (T v, lock_t &l)
474 l.lock ();
475 item_t *item = items.find (v);
476 if (item) {
477 item_t old = *item;
478 *item = items[items.len - 1];
479 items.pop ();
480 l.unlock ();
481 old.finish ();
482 } else {
483 l.unlock ();
487 template <typename T>
488 inline bool find (T v, item_t *i, lock_t &l)
490 l.lock ();
491 item_t *item = items.find (v);
492 if (item)
493 *i = *item;
494 l.unlock ();
495 return !!item;
498 template <typename T>
499 inline item_t *find_or_insert (T v, lock_t &l)
501 l.lock ();
502 item_t *item = items.find (v);
503 if (!item) {
504 item = items.push ();
505 if (likely (item))
506 *item = v;
508 l.unlock ();
509 return item;
512 inline void finish (lock_t &l)
514 if (!items.len) {
515 /* No need for locking. */
516 items.finish ();
517 return;
519 l.lock ();
520 while (items.len) {
521 item_t old = items[items.len - 1];
522 items.pop ();
523 l.unlock ();
524 old.finish ();
525 l.lock ();
527 items.finish ();
528 l.unlock ();
534 /* ASCII tag/character handling */
536 static inline bool ISALPHA (unsigned char c)
537 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
538 static inline bool ISALNUM (unsigned char c)
539 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
540 static inline bool ISSPACE (unsigned char c)
541 { return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; }
542 static inline unsigned char TOUPPER (unsigned char c)
543 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
544 static inline unsigned char TOLOWER (unsigned char c)
545 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
547 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
548 ((const char *) s)[1], \
549 ((const char *) s)[2], \
550 ((const char *) s)[3]))
553 /* C++ helpers */
555 /* Makes class uncopyable. Use in private: section. */
556 #define NO_COPY(T) \
557 T (const T &o); \
558 T &operator = (const T &o)
561 /* Debug */
564 #ifndef HB_DEBUG
565 #define HB_DEBUG 0
566 #endif
568 static inline bool
569 _hb_debug (unsigned int level,
570 unsigned int max_level)
572 return level < max_level;
575 #define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
576 #define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0))
578 static inline void
579 _hb_print_func (const char *func)
581 if (func)
583 unsigned int func_len = strlen (func);
584 /* Skip "static" */
585 if (0 == strncmp (func, "static ", 7))
586 func += 7;
587 /* Skip "typename" */
588 if (0 == strncmp (func, "typename ", 9))
589 func += 9;
590 /* Skip return type */
591 const char *space = strchr (func, ' ');
592 if (space)
593 func = space + 1;
594 /* Skip parameter list */
595 const char *paren = strchr (func, '(');
596 if (paren)
597 func_len = paren - func;
598 fprintf (stderr, "%.*s", func_len, func);
602 template <int max_level> static inline void
603 _hb_debug_msg_va (const char *what,
604 const void *obj,
605 const char *func,
606 bool indented,
607 unsigned int level,
608 int level_dir,
609 const char *message,
610 va_list ap) HB_PRINTF_FUNC(7, 0);
611 template <int max_level> static inline void
612 _hb_debug_msg_va (const char *what,
613 const void *obj,
614 const char *func,
615 bool indented,
616 unsigned int level,
617 int level_dir,
618 const char *message,
619 va_list ap)
621 if (!_hb_debug (level, max_level))
622 return;
624 fprintf (stderr, "%-10s", what ? what : "");
626 if (obj)
627 fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj);
628 else
629 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
631 if (indented) {
632 /* One may want to add ASCII version of these. See:
633 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
634 #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
635 #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
636 #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
637 #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
638 #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
639 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;
640 fprintf (stderr, "%2u %s" VRBAR "%s",
641 level,
642 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level),
643 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
644 } else
645 fprintf (stderr, " " VRBAR LBAR);
647 _hb_print_func (func);
649 if (message)
651 fprintf (stderr, ": ");
652 vfprintf (stderr, message, ap);
655 fprintf (stderr, "\n");
657 template <> inline void
658 _hb_debug_msg_va<0> (const char *what HB_UNUSED,
659 const void *obj HB_UNUSED,
660 const char *func HB_UNUSED,
661 bool indented HB_UNUSED,
662 unsigned int level HB_UNUSED,
663 int level_dir HB_UNUSED,
664 const char *message HB_UNUSED,
665 va_list ap HB_UNUSED) {}
667 template <int max_level> static inline void
668 _hb_debug_msg (const char *what,
669 const void *obj,
670 const char *func,
671 bool indented,
672 unsigned int level,
673 int level_dir,
674 const char *message,
675 ...) HB_PRINTF_FUNC(7, 8);
676 template <int max_level> static inline void
677 _hb_debug_msg (const char *what,
678 const void *obj,
679 const char *func,
680 bool indented,
681 unsigned int level,
682 int level_dir,
683 const char *message,
684 ...)
686 va_list ap;
687 va_start (ap, message);
688 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
689 va_end (ap);
691 template <> inline void
692 _hb_debug_msg<0> (const char *what HB_UNUSED,
693 const void *obj HB_UNUSED,
694 const char *func HB_UNUSED,
695 bool indented HB_UNUSED,
696 unsigned int level HB_UNUSED,
697 int level_dir HB_UNUSED,
698 const char *message HB_UNUSED,
699 ...) HB_PRINTF_FUNC(7, 8);
700 template <> inline void
701 _hb_debug_msg<0> (const char *what HB_UNUSED,
702 const void *obj HB_UNUSED,
703 const char *func HB_UNUSED,
704 bool indented HB_UNUSED,
705 unsigned int level HB_UNUSED,
706 int level_dir HB_UNUSED,
707 const char *message HB_UNUSED,
708 ...) {}
710 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
711 #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
712 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
716 * Printer
719 template <typename T>
720 struct hb_printer_t {
721 const char *print (const T&) { return "something"; }
724 template <>
725 struct hb_printer_t<bool> {
726 const char *print (bool v) { return v ? "true" : "false"; }
729 template <>
730 struct hb_printer_t<hb_void_t> {
731 const char *print (hb_void_t) { return ""; }
736 * Trace
739 template <typename T>
740 static inline void _hb_warn_no_return (bool returned)
742 if (unlikely (!returned)) {
743 fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n");
746 template <>
747 /*static*/ inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED)
750 template <int max_level, typename ret_t>
751 struct hb_auto_trace_t {
752 explicit inline hb_auto_trace_t (unsigned int *plevel_,
753 const char *what_,
754 const void *obj_,
755 const char *func,
756 const char *message,
757 ...) : plevel (plevel_), what (what_), obj (obj_), returned (false)
759 if (plevel) ++*plevel;
761 va_list ap;
762 va_start (ap, message);
763 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
764 va_end (ap);
766 inline ~hb_auto_trace_t (void)
768 _hb_warn_no_return<ret_t> (returned);
769 if (!returned) {
770 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " ");
772 if (plevel) --*plevel;
775 inline ret_t ret (ret_t v, unsigned int line = 0)
777 if (unlikely (returned)) {
778 fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
779 return v;
782 _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1,
783 "return %s (line %d)",
784 hb_printer_t<ret_t>().print (v), line);
785 if (plevel) --*plevel;
786 plevel = NULL;
787 returned = true;
788 return v;
791 private:
792 unsigned int *plevel;
793 const char *what;
794 const void *obj;
795 bool returned;
797 template <typename ret_t> /* Optimize when tracing is disabled */
798 struct hb_auto_trace_t<0, ret_t> {
799 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED,
800 const char *what HB_UNUSED,
801 const void *obj HB_UNUSED,
802 const char *func HB_UNUSED,
803 const char *message HB_UNUSED,
804 ...) {}
806 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; }
809 #define TRACE_RETURN(RET) trace.ret (RET, __LINE__)
811 /* Misc */
813 template <typename T> class hb_assert_unsigned_t;
814 template <> class hb_assert_unsigned_t<unsigned char> {};
815 template <> class hb_assert_unsigned_t<unsigned short> {};
816 template <> class hb_assert_unsigned_t<unsigned int> {};
817 template <> class hb_assert_unsigned_t<unsigned long> {};
819 template <typename T> static inline bool
820 hb_in_range (T u, T lo, T hi)
822 /* The sizeof() is here to force template instantiation.
823 * I'm sure there are better ways to do this but can't think of
824 * one right now. Declaring a variable won't work as HB_UNUSED
825 * is unusable on some platforms and unused types are less likely
826 * to generate a warning than unused variables. */
827 ASSERT_STATIC (sizeof (hb_assert_unsigned_t<T>) >= 0);
829 /* The casts below are important as if T is smaller than int,
830 * the subtract results will become a signed int! */
831 return (T)(u - lo) <= (T)(hi - lo);
834 template <typename T> static inline bool
835 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2)
837 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2);
840 template <typename T> static inline bool
841 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
843 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3);
847 /* Useful for set-operations on small enums.
848 * For example, for testing "x ∈ {x1, x2, x3}" use:
849 * (FLAG_SAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
851 #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((x) < 32) + (1U << (x)))
852 #define FLAG_SAFE(x) (1U << (x))
853 #define FLAG_UNSAFE(x) ((x) < 32 ? FLAG_SAFE(x) : 0)
854 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
857 template <typename T, typename T2> static inline void
858 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
860 if (unlikely (!len))
861 return;
863 unsigned int k = len - 1;
864 do {
865 unsigned int new_k = 0;
867 for (unsigned int j = 0; j < k; j++)
868 if (compar (&array[j], &array[j+1]) > 0)
871 T t;
872 t = array[j];
873 array[j] = array[j + 1];
874 array[j + 1] = t;
876 if (array2)
878 T2 t;
879 t = array2[j];
880 array2[j] = array2[j + 1];
881 array2[j + 1] = t;
884 new_k = j;
886 k = new_k;
887 } while (k);
890 template <typename T> static inline void
891 hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
893 hb_bubble_sort (array, len, compar, (int *) NULL);
896 static inline hb_bool_t
897 hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
899 /* Pain because we don't know whether s is nul-terminated. */
900 char buf[64];
901 len = MIN (ARRAY_LENGTH (buf) - 1, len);
902 strncpy (buf, s, len);
903 buf[len] = '\0';
905 char *end;
906 errno = 0;
907 unsigned long v = strtoul (buf, &end, base);
908 if (errno) return false;
909 if (*end) return false;
910 *out = v;
911 return true;
915 /* Global runtime options. */
917 struct hb_options_t
919 unsigned int initialized : 1;
920 unsigned int uniscribe_bug_compatible : 1;
923 union hb_options_union_t {
924 unsigned int i;
925 hb_options_t opts;
927 ASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t));
929 HB_INTERNAL void
930 _hb_options_init (void);
932 extern HB_INTERNAL hb_options_union_t _hb_options;
934 static inline hb_options_t
935 hb_options (void)
937 if (unlikely (!_hb_options.i))
938 _hb_options_init ();
940 return _hb_options.opts;
944 #endif /* HB_PRIVATE_HH */