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
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
54 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
58 #define HB_PASTE1(a,b) a##b
59 #define HB_PASTE(a,b) HB_PASTE1(a,b)
61 /* Compile-time custom allocator support. */
63 #if defined(hb_malloc_impl) \
64 && defined(hb_calloc_impl) \
65 && defined(hb_realloc_impl) \
66 && defined(hb_free_impl)
67 extern "C" void* hb_malloc_impl(size_t size);
68 extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
69 extern "C" void* hb_realloc_impl(void *ptr, size_t size);
70 extern "C" void hb_free_impl(void *ptr);
71 #define malloc hb_malloc_impl
72 #define calloc hb_calloc_impl
73 #define realloc hb_realloc_impl
74 #define free hb_free_impl
78 /* Compiler attributes */
81 #if __cplusplus < 201103L
89 #define static_assert(e, msg) \
90 HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
91 #endif // static_assert
94 #if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
95 #define thread_local __thread
101 #endif // __cplusplus < 201103L
103 #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
104 #define likely(expr) (__builtin_expect (!!(expr), 1))
105 #define unlikely(expr) (__builtin_expect (!!(expr), 0))
107 #define likely(expr) (expr)
108 #define unlikely(expr) (expr)
111 #if !defined(__GNUC__) && !defined(__clang__)
113 #define __attribute__(x)
117 #define HB_PURE_FUNC __attribute__((pure))
118 #define HB_CONST_FUNC __attribute__((const))
119 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
122 #define HB_CONST_FUNC
123 #define HB_PRINTF_FUNC(format_idx, arg_idx)
126 #define HB_UNUSED __attribute__((unused))
127 #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
128 #define HB_UNUSED __pragma(warning(suppress: 4100 4101))
134 # if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
135 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
138 # define HB_NO_VISIBILITY 1
143 #define HB_FUNC __PRETTY_FUNCTION__
144 #elif defined(_MSC_VER)
145 #define HB_FUNC __FUNCSIG__
147 #define HB_FUNC __func__
150 #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
151 /* https://github.com/harfbuzz/harfbuzz/issues/630 */
156 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
157 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
158 * cases that fall through without a break or return statement. HB_FALLTHROUGH
159 * is only needed on cases that have code:
162 * case 1: // These cases have no code. No fallthrough annotations are needed.
165 * foo = 4; // This case has code, so a fallthrough annotation is needed:
171 #if defined(__clang__) && __cplusplus >= 201103L
172 /* clang's fallthrough annotations are only available starting in C++11. */
173 # define HB_FALLTHROUGH [[clang::fallthrough]]
175 /* GNU fallthrough attribute is available from GCC7 */
176 # define HB_FALLTHROUGH __attribute__((fallthrough))
177 #elif defined(_MSC_VER)
179 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
180 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
183 # define HB_FALLTHROUGH __fallthrough
185 # define HB_FALLTHROUGH /* FALLTHROUGH */
188 #if defined(_WIN32) || defined(__CYGWIN__)
189 /* We need Windows Vista for both Uniscribe backend and for
190 * MemoryBarrier. We don't support compiling on Windows XP,
191 * though we run on it fine. */
192 # if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
195 # ifndef _WIN32_WINNT
196 # define _WIN32_WINNT 0x0600
198 # ifndef WIN32_LEAN_AND_MEAN
199 # define WIN32_LEAN_AND_MEAN 1
205 # if defined(_WIN32_WCE)
206 /* Some things not defined on Windows CE. */
207 # define vsnprintf _vsnprintf
208 # define getenv(Name) nullptr
209 # if _WIN32_WCE < 0x800
210 # define setlocale(Category, Locale) "C"
211 static int errno = 0; /* Use something better? */
213 # elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
214 # define getenv(Name) nullptr
216 # if defined(_MSC_VER) && _MSC_VER < 1900
217 # define snprintf _snprintf
222 /* atexit() is only safe to be called from shared libraries on certain
223 * platforms. Whitelist.
224 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
225 # if defined(__linux) && defined(__GLIBC_PREREQ)
226 # if __GLIBC_PREREQ(2,3)
227 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
228 # define HB_USE_ATEXIT 1
230 # elif defined(_MSC_VER) || defined(__MINGW32__)
232 * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
233 * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
234 * mingw32 headers say atexit is safe to use in shared libraries.
236 # define HB_USE_ATEXIT 1
237 # elif defined(__ANDROID__)
238 /* This is available since Android NKD r8 or r8b:
239 * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
241 # define HB_USE_ATEXIT 1
242 # elif defined(__APPLE__)
243 /* For macOS and related platforms, the atexit man page indicates
244 * that it will be invoked when the library is unloaded, not only
245 * at application exit.
247 # define HB_USE_ATEXIT 1
251 # undef HB_USE_ATEXIT
257 template <typename Type>
258 static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; }
261 template <typename Type>
262 static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; }
264 static inline unsigned int DIV_CEIL (const unsigned int a, unsigned int b)
265 { return (a + (b - 1)) / b; }
269 template <typename Type, unsigned int n>
270 static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; }
271 /* A const version, but does not detect erratically being called on pointers. */
272 #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
274 #define HB_STMT_START do
275 #define HB_STMT_END while (0)
277 template <unsigned int cond> class hb_assert_constant_t;
278 template <> class hb_assert_constant_t<1> {};
280 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
282 /* Lets assert int types. Saves trouble down the road. */
284 static_assert ((sizeof (int8_t) == 1), "");
285 static_assert ((sizeof (uint8_t) == 1), "");
286 static_assert ((sizeof (int16_t) == 2), "");
287 static_assert ((sizeof (uint16_t) == 2), "");
288 static_assert ((sizeof (int32_t) == 4), "");
289 static_assert ((sizeof (uint32_t) == 4), "");
290 static_assert ((sizeof (int64_t) == 8), "");
291 static_assert ((sizeof (uint64_t) == 8), "");
293 static_assert ((sizeof (hb_codepoint_t) == 4), "");
294 static_assert ((sizeof (hb_position_t) == 4), "");
295 static_assert ((sizeof (hb_mask_t) == 4), "");
296 static_assert ((sizeof (hb_var_int_t) == 4), "");
299 /* We like our types POD */
301 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
302 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
303 #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
306 # define _ASSERT_INSTANCE_POD1(_line, _instance) \
308 typedef __typeof__(_instance) _type_##_line; \
309 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
312 # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
314 # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
315 # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
317 /* Check _assertion in a method environment */
318 #define _ASSERT_POD1(_line) \
319 HB_UNUSED inline void _static_assertion_on_line_##_line (void) const \
320 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
321 # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
322 # define ASSERT_POD() _ASSERT_POD0 (__LINE__)
331 typedef const struct _hb_void_t *hb_void_t;
332 #define HB_VOID ((const _hb_void_t *) nullptr)
334 /* Return the number of 1 bits in v. */
335 template <typename T>
336 static inline HB_CONST_FUNC unsigned int
339 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) && defined(__OPTIMIZE__)
340 if (sizeof (T) <= sizeof (unsigned int))
341 return __builtin_popcount (v);
343 if (sizeof (T) <= sizeof (unsigned long))
344 return __builtin_popcountl (v);
346 if (sizeof (T) <= sizeof (unsigned long long))
347 return __builtin_popcountll (v);
354 y = (v >> 1) &033333333333;
355 y = v - y - ((y >>1) & 033333333333);
356 return (((y + (y >> 3)) & 030707070707) % 077);
361 unsigned int shift = 32;
362 return _hb_popcount<uint32_t> ((uint32_t) v) + _hb_popcount ((uint32_t) (v >> shift));
365 if (sizeof (T) == 16)
367 unsigned int shift = 64;
368 return _hb_popcount<uint64_t> ((uint64_t) v) + _hb_popcount ((uint64_t) (v >> shift));
372 return 0; /* Shut up stupid compiler. */
375 /* Returns the number of bits needed to store number */
376 template <typename T>
377 static inline HB_CONST_FUNC unsigned int
378 _hb_bit_storage (T v)
380 if (unlikely (!v)) return 0;
382 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
383 if (sizeof (T) <= sizeof (unsigned int))
384 return sizeof (unsigned int) * 8 - __builtin_clz (v);
386 if (sizeof (T) <= sizeof (unsigned long))
387 return sizeof (unsigned long) * 8 - __builtin_clzl (v);
389 if (sizeof (T) <= sizeof (unsigned long long))
390 return sizeof (unsigned long long) * 8 - __builtin_clzll (v);
393 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
394 if (sizeof (T) <= sizeof (unsigned int))
397 _BitScanReverse (&where, v);
404 _BitScanReverse64 (&where, v);
413 const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
414 const unsigned int S[] = {1, 2, 4, 8, 16};
416 for (int i = 4; i >= 0; i--)
427 const uint64_t b[] = {0x2ULL, 0xCULL, 0xF0ULL, 0xFF00ULL, 0xFFFF0000ULL, 0xFFFFFFFF00000000ULL};
428 const unsigned int S[] = {1, 2, 4, 8, 16, 32};
430 for (int i = 5; i >= 0; i--)
438 if (sizeof (T) == 16)
440 unsigned int shift = 64;
441 return (v >> shift) ? _hb_bit_storage<uint64_t> ((uint64_t) (v >> shift)) + shift :
442 _hb_bit_storage<uint64_t> ((uint64_t) v);
446 return 0; /* Shut up stupid compiler. */
449 /* Returns the number of zero bits in the least significant side of v */
450 template <typename T>
451 static inline HB_CONST_FUNC unsigned int
454 if (unlikely (!v)) return 0;
456 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
457 if (sizeof (T) <= sizeof (unsigned int))
458 return __builtin_ctz (v);
460 if (sizeof (T) <= sizeof (unsigned long))
461 return __builtin_ctzl (v);
463 if (sizeof (T) <= sizeof (unsigned long long))
464 return __builtin_ctzll (v);
467 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
468 if (sizeof (T) <= sizeof (unsigned int))
471 _BitScanForward (&where, v);
478 _BitScanForward64 (&where, v);
490 if (v & 0x0000FFFF) c -= 16;
491 if (v & 0x00FF00FF) c -= 8;
492 if (v & 0x0F0F0F0F) c -= 4;
493 if (v & 0x33333333) c -= 2;
494 if (v & 0x55555555) c -= 1;
501 v &= - (int64_t) (v);
503 if (v & 0x00000000FFFFFFFFULL) c -= 32;
504 if (v & 0x0000FFFF0000FFFFULL) c -= 16;
505 if (v & 0x00FF00FF00FF00FFULL) c -= 8;
506 if (v & 0x0F0F0F0F0F0F0F0FULL) c -= 4;
507 if (v & 0x3333333333333333ULL) c -= 2;
508 if (v & 0x5555555555555555ULL) c -= 1;
511 if (sizeof (T) == 16)
513 unsigned int shift = 64;
514 return (uint64_t) v ? _hb_bit_storage<uint64_t> ((uint64_t) v) :
515 _hb_bit_storage<uint64_t> ((uint64_t) v >> shift) + shift;
519 return 0; /* Shut up stupid compiler. */
523 _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size)
525 return (size > 0) && (count >= ((unsigned int) -1) / size);
528 static inline unsigned int
529 _hb_ceil_to_4 (unsigned int v)
531 return ((v - 1) | 3) + 1;
542 #define HB_DISALLOW_COPY_AND_ASSIGN(TypeName) \
543 TypeName(const TypeName&); \
544 void operator=(const TypeName&)
550 /* Global nul-content Null pool. Enlarge as necessary. */
552 #define HB_NULL_POOL_SIZE 264
553 static_assert (HB_NULL_POOL_SIZE % sizeof (void *) == 0, "Align HB_NULL_POOL_SIZE.");
555 #ifdef HB_NO_VISIBILITY
560 void * const _hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)]
561 #ifdef HB_NO_VISIBILITY
565 /* Generic nul-content Null objects. */
566 template <typename Type>
567 static inline Type const & Null (void) {
568 static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
569 return *reinterpret_cast<Type const *> (_hb_NullPool);
571 #define Null(Type) Null<Type>()
573 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
574 #define DEFINE_NULL_DATA(Namespace, Type, data) \
575 } /* Close namespace. */ \
576 static const char _Null##Type[sizeof (Namespace::Type) + 1] = data; /* +1 is for nul-termination in data */ \
578 /*static*/ inline const Namespace::Type& Null<Namespace::Type> (void) { \
579 return *reinterpret_cast<const Namespace::Type *> (_Null##Type); \
581 namespace Namespace { \
582 /* The following line really exists such that we end in a place needing semicolon */ \
583 static_assert (Namespace::Type::min_size + 1 <= sizeof (_Null##Type), "Null pool too small. Enlarge.")
586 /* Global writable pool. Enlarge as necessary. */
588 /* To be fully correct, CrapPool must be thread_local. However, we do not rely on CrapPool
589 * for correct operation. It only exist to catch and divert program logic bugs instead of
590 * causing bad memory access. So, races there are not actually introducing incorrectness
591 * in the code. Has ~12kb binary size overhead to have it, also clang build fails with it. */
592 #ifdef HB_NO_VISIBILITY
597 /*thread_local*/ void * _hb_CrapPool[HB_NULL_POOL_SIZE / sizeof (void *)]
598 #ifdef HB_NO_VISIBILITY
602 /* CRAP pool: Common Region for Access Protection. */
603 template <typename Type>
604 static inline Type& Crap (void) {
605 static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
606 Type *obj = reinterpret_cast<Type *> (_hb_CrapPool);
610 #define Crap(Type) Crap<Type>()
612 template <typename Type>
614 static inline Type & get (void) { return Crap(Type); }
616 template <typename Type>
617 struct CrapOrNull<const Type> {
618 static inline Type const & get (void) { return Null(Type); }
620 #define CrapOrNull(Type) CrapOrNull<Type>::get ()
624 /* arrays and maps */
627 #define HB_PREALLOCED_ARRAY_INIT {0, 0, nullptr}
628 template <typename Type, unsigned int StaticSize=8>
632 unsigned int allocated;
635 Type static_array[StaticSize];
640 allocated = ARRAY_LENGTH (static_array);
642 arrayZ = static_array;
645 inline Type& operator [] (unsigned int i)
647 if (unlikely (i >= len))
651 inline const Type& operator [] (unsigned int i) const
653 if (unlikely (i >= len))
658 inline Type *push (void)
660 if (unlikely (!resize (len + 1)))
662 return &arrayZ[len - 1];
664 inline Type *push (const Type& v)
671 /* Allocate for size but don't adjust len. */
672 inline bool alloc (unsigned int size)
674 if (unlikely (!successful))
677 if (likely (size <= allocated))
682 unsigned int new_allocated = allocated;
683 while (size >= new_allocated)
684 new_allocated += (new_allocated >> 1) + 8;
686 Type *new_array = nullptr;
688 if (arrayZ == static_array)
690 new_array = (Type *) calloc (new_allocated, sizeof (Type));
692 memcpy (new_array, arrayZ, len * sizeof (Type));
696 bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type));
697 if (likely (!overflows))
698 new_array = (Type *) realloc (arrayZ, new_allocated * sizeof (Type));
701 if (unlikely (!new_array))
708 allocated = new_allocated;
713 inline bool resize (int size_)
715 unsigned int size = size_ < 0 ? 0u : (unsigned int) size_;
720 memset (arrayZ + len, 0, (size - len) * sizeof (*arrayZ));
726 inline void pop (void)
732 inline void remove (unsigned int i)
734 if (unlikely (i >= len))
736 memmove (static_cast<void *> (&arrayZ[i]),
737 static_cast<void *> (&arrayZ[i + 1]),
738 (len - i - 1) * sizeof (Type));
742 inline void shrink (int size_)
744 unsigned int size = size_ < 0 ? 0u : (unsigned int) size_;
749 template <typename T>
750 inline Type *find (T v) {
751 for (unsigned int i = 0; i < len; i++)
756 template <typename T>
757 inline const Type *find (T v) const {
758 for (unsigned int i = 0; i < len; i++)
764 inline void qsort (int (*cmp)(const void*, const void*))
766 ::qsort (arrayZ, len, sizeof (Type), cmp);
769 inline void qsort (void)
771 ::qsort (arrayZ, len, sizeof (Type), Type::cmp);
774 inline void qsort (unsigned int start, unsigned int end)
776 ::qsort (arrayZ + start, end - start, sizeof (Type), Type::cmp);
779 template <typename T>
780 inline Type *lsearch (const T &x)
782 for (unsigned int i = 0; i < len; i++)
783 if (0 == this->arrayZ[i].cmp (&x))
788 template <typename T>
789 inline Type *bsearch (const T &x)
792 return bfind (x, &i) ? &arrayZ[i] : nullptr;
794 template <typename T>
795 inline const Type *bsearch (const T &x) const
798 return bfind (x, &i) ? &arrayZ[i] : nullptr;
800 template <typename T>
801 inline bool bfind (const T &x, unsigned int *i) const
803 int min = 0, max = (int) this->len - 1;
806 int mid = (min + max) / 2;
807 int c = this->arrayZ[mid].cmp (&x);
818 if (max < 0 || (max < (int) this->len && this->arrayZ[max].cmp (&x) > 0))
824 inline void fini (void)
826 if (arrayZ != static_array)
833 template <typename Type>
834 struct hb_auto_t : Type
836 hb_auto_t (void) { Type::init (); }
837 ~hb_auto_t (void) { Type::fini (); }
842 template <typename Type>
843 struct hb_auto_array_t : hb_auto_t <hb_vector_t <Type> > {};
846 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
847 template <typename item_t, typename lock_t>
848 struct hb_lockable_set_t
850 hb_vector_t <item_t, 1> items;
852 inline void init (void) { items.init (); }
854 template <typename T>
855 inline item_t *replace_or_insert (T v, lock_t &l, bool replace)
858 item_t *item = items.find (v);
871 item = items.push (v);
877 template <typename T>
878 inline void remove (T v, lock_t &l)
881 item_t *item = items.find (v);
884 *item = items[items.len - 1];
893 template <typename T>
894 inline bool find (T v, item_t *i, lock_t &l)
897 item_t *item = items.find (v);
904 template <typename T>
905 inline item_t *find_or_insert (T v, lock_t &l)
908 item_t *item = items.find (v);
910 item = items.push (v);
916 inline void fini (lock_t &l)
919 /* No need for locking. */
925 item_t old = items[items.len - 1];
938 /* ASCII tag/character handling */
940 static inline bool ISALPHA (unsigned char c)
941 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
942 static inline bool ISALNUM (unsigned char c)
943 { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }
944 static inline bool ISSPACE (unsigned char c)
945 { return c == ' ' || c =='\f'|| c =='\n'|| c =='\r'|| c =='\t'|| c =='\v'; }
946 static inline unsigned char TOUPPER (unsigned char c)
947 { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; }
948 static inline unsigned char TOLOWER (unsigned char c)
949 { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; }
952 /* HB_NDEBUG disables some sanity checks that are very safe to disable and
953 * should be disabled in production systems. If NDEBUG is defined, enable
954 * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
955 * light-weight) to be enabled, then HB_DEBUG can be defined to disable
956 * the costlier checks. */
964 template <typename T> class hb_assert_unsigned_t;
965 template <> class hb_assert_unsigned_t<unsigned char> {};
966 template <> class hb_assert_unsigned_t<unsigned short> {};
967 template <> class hb_assert_unsigned_t<unsigned int> {};
968 template <> class hb_assert_unsigned_t<unsigned long> {};
970 template <typename T> static inline bool
971 hb_in_range (T u, T lo, T hi)
973 /* The sizeof() is here to force template instantiation.
974 * I'm sure there are better ways to do this but can't think of
975 * one right now. Declaring a variable won't work as HB_UNUSED
976 * is unusable on some platforms and unused types are less likely
977 * to generate a warning than unused variables. */
978 static_assert ((sizeof (hb_assert_unsigned_t<T>) >= 0), "");
980 /* The casts below are important as if T is smaller than int,
981 * the subtract results will become a signed int! */
982 return (T)(u - lo) <= (T)(hi - lo);
985 template <typename T> static inline bool
986 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2)
988 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2);
991 template <typename T> static inline bool
992 hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3)
994 return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3);
998 /* Enable bitwise ops on enums marked as flags_t */
999 /* To my surprise, looks like the function resolver is happy to silently cast
1000 * one enum to another... So this doesn't provide the type-checking that I
1001 * originally had in mind... :(.
1003 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
1006 # pragma warning(disable:4200)
1007 # pragma warning(disable:4800)
1009 #define HB_MARK_AS_FLAG_T(T) \
1011 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
1012 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
1013 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
1014 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
1015 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
1016 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
1017 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
1021 /* Useful for set-operations on small enums.
1022 * For example, for testing "x ∈ {x1, x2, x3}" use:
1023 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
1025 #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned int)(x) < 32) + (1U << (unsigned int)(x)))
1026 #define FLAG_UNSAFE(x) ((unsigned int)(x) < 32 ? (1U << (unsigned int)(x)) : 0)
1027 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
1030 template <typename T, typename T2> static inline void
1031 hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2)
1033 for (unsigned int i = 1; i < len; i++)
1036 while (j && compar (&array[j - 1], &array[i]) > 0)
1040 /* Move item i to occupy place for item j, shift what's in between. */
1043 memmove (&array[j + 1], &array[j], (i - j) * sizeof (T));
1049 memmove (&array2[j + 1], &array2[j], (i - j) * sizeof (T2));
1055 template <typename T> static inline void
1056 hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *))
1058 hb_stable_sort (array, len, compar, (int *) nullptr);
1061 static inline hb_bool_t
1062 hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out)
1064 /* Pain because we don't know whether s is nul-terminated. */
1066 len = MIN (ARRAY_LENGTH (buf) - 1, len);
1067 strncpy (buf, s, len);
1072 unsigned long v = strtoul (buf, &end, base);
1073 if (errno) return false;
1074 if (*end) return false;
1084 static const bool passthru_left = true;
1085 static const bool passthru_right = true;
1086 template <typename T> static void process (T &o, const T &a, const T &b) { o = a | b; }
1090 static const bool passthru_left = false;
1091 static const bool passthru_right = false;
1092 template <typename T> static void process (T &o, const T &a, const T &b) { o = a & b; }
1096 static const bool passthru_left = true;
1097 static const bool passthru_right = false;
1098 template <typename T> static void process (T &o, const T &a, const T &b) { o = a & ~b; }
1102 static const bool passthru_left = true;
1103 static const bool passthru_right = true;
1104 template <typename T> static void process (T &o, const T &a, const T &b) { o = a ^ b; }
1108 /* Compiler-assisted vectorization. */
1110 /* The `vector_size' attribute was introduced in gcc 3.1. */
1111 #if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
1112 #define HB_VECTOR_SIZE 128
1113 #elif !defined(HB_VECTOR_SIZE)
1114 #define HB_VECTOR_SIZE 0
1117 /* Type behaving similar to vectorized vars defined using __attribute__((vector_size(...))). */
1118 template <typename elt_t, unsigned int byte_size>
1119 struct hb_vector_size_t
1121 elt_t& operator [] (unsigned int i) { return u.v[i]; }
1122 const elt_t& operator [] (unsigned int i) const { return u.v[i]; }
1125 inline hb_vector_size_t process (const hb_vector_size_t &o) const
1129 if (HB_VECTOR_SIZE && 0 == (byte_size * 8) % HB_VECTOR_SIZE)
1130 for (unsigned int i = 0; i < ARRAY_LENGTH (u.vec); i++)
1131 Op::process (r.u.vec[i], u.vec[i], o.u.vec[i]);
1134 for (unsigned int i = 0; i < ARRAY_LENGTH (u.v); i++)
1135 Op::process (r.u.v[i], u.v[i], o.u.v[i]);
1138 inline hb_vector_size_t operator | (const hb_vector_size_t &o) const
1139 { return process<HbOpOr> (o); }
1140 inline hb_vector_size_t operator & (const hb_vector_size_t &o) const
1141 { return process<HbOpAnd> (o); }
1142 inline hb_vector_size_t operator ^ (const hb_vector_size_t &o) const
1143 { return process<HbOpXor> (o); }
1144 inline hb_vector_size_t operator ~ () const
1147 #if HB_VECTOR_SIZE && 0
1148 if (HB_VECTOR_SIZE && 0 == (byte_size * 8) % HB_VECTOR_SIZE)
1149 for (unsigned int i = 0; i < ARRAY_LENGTH (u.vec); i++)
1150 r.u.vec[i] = ~u.vec[i];
1153 for (unsigned int i = 0; i < ARRAY_LENGTH (u.v); i++)
1159 static_assert (byte_size / sizeof (elt_t) * sizeof (elt_t) == byte_size, "");
1161 elt_t v[byte_size / sizeof (elt_t)];
1163 typedef unsigned long vec_t __attribute__((vector_size (HB_VECTOR_SIZE / 8)));
1164 vec_t vec[byte_size / sizeof (vec_t)];
1170 /* Global runtime options. */
1174 unsigned int initialized : 1;
1175 unsigned int uniscribe_bug_compatible : 1;
1178 union hb_options_union_t {
1182 static_assert ((sizeof (int) == sizeof (hb_options_union_t)), "");
1185 _hb_options_init (void);
1187 extern HB_INTERNAL hb_options_union_t _hb_options;
1189 static inline hb_options_t
1192 if (unlikely (!_hb_options.i))
1193 _hb_options_init ();
1195 return _hb_options.opts;
1198 /* Size signifying variable-sized array */
1206 inline hb_bytes_t (void) : bytes (nullptr), len (0) {}
1207 inline hb_bytes_t (const char *bytes_, unsigned int len_) : bytes (bytes_), len (len_) {}
1209 inline int cmp (const hb_bytes_t &a) const
1212 return (int) a.len - (int) len;
1214 return memcmp (a.bytes, bytes, len);
1216 static inline int cmp (const void *pa, const void *pb)
1218 hb_bytes_t *a = (hb_bytes_t *) pa;
1219 hb_bytes_t *b = (hb_bytes_t *) pb;
1228 /* fallback for round() */
1229 #if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND)
1230 static inline double
1234 return floor (x + 0.5);
1236 return ceil (x - 0.5);
1241 #endif /* HB_PRIVATE_HH */