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
33 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC
35 #pragma warning( disable: 4068 ) /* Unknown pragma */
37 #if defined(__GNUC__) || defined(__clang__)
40 * - All pragmas are declared GCC even if they are clang ones. Otherwise GCC
41 * nags, even though we instruct it to ignore -Wunknown-pragmas. ¯\_(ツ)_/¯
43 * - Within each category, keep sorted.
45 * - Warnings whose scope can be expanded in future compiler versions shall
46 * be declared as "warning". Otherwise, either ignored or error.
49 /* Setup. Don't sort order within this category. */
50 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
51 #pragma GCC diagnostic warning "-Wall"
52 #pragma GCC diagnostic warning "-Wextra"
54 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
55 #pragma GCC diagnostic ignored "-Wpragmas"
56 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
57 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
59 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
60 //#pragma GCC diagnostic warning "-Weverything"
63 /* Error. Should never happen. */
64 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_ERROR
65 #pragma GCC diagnostic error "-Wc++11-narrowing"
66 #pragma GCC diagnostic error "-Wcast-align"
67 #pragma GCC diagnostic error "-Wcast-function-type"
68 #pragma GCC diagnostic error "-Wdelete-non-virtual-dtor"
69 #pragma GCC diagnostic error "-Wembedded-directive"
70 #pragma GCC diagnostic error "-Wextra-semi-stmt"
71 #pragma GCC diagnostic error "-Wformat-security"
72 #pragma GCC diagnostic error "-Wimplicit-function-declaration"
73 #pragma GCC diagnostic error "-Winit-self"
74 #pragma GCC diagnostic error "-Winjected-class-name"
75 #pragma GCC diagnostic error "-Wmissing-braces"
76 #pragma GCC diagnostic error "-Wmissing-declarations"
77 #pragma GCC diagnostic error "-Wmissing-prototypes"
78 #pragma GCC diagnostic error "-Wnested-externs"
79 #pragma GCC diagnostic error "-Wold-style-definition"
80 #pragma GCC diagnostic error "-Wpointer-arith"
81 #pragma GCC diagnostic error "-Wredundant-decls"
82 #pragma GCC diagnostic error "-Wreorder"
83 #pragma GCC diagnostic error "-Wsign-compare"
84 #pragma GCC diagnostic error "-Wstrict-prototypes"
85 #pragma GCC diagnostic error "-Wstring-conversion"
86 #pragma GCC diagnostic error "-Wswitch-enum"
87 #pragma GCC diagnostic error "-Wtautological-overlap-compare"
88 #pragma GCC diagnostic error "-Wunneeded-internal-declaration"
89 #pragma GCC diagnostic error "-Wunused"
90 #pragma GCC diagnostic error "-Wunused-local-typedefs"
91 #pragma GCC diagnostic error "-Wunused-value"
92 #pragma GCC diagnostic error "-Wunused-variable"
93 #pragma GCC diagnostic error "-Wvla"
94 #pragma GCC diagnostic error "-Wwrite-strings"
97 /* Warning. To be investigated if happens. */
98 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
99 #pragma GCC diagnostic warning "-Wbuiltin-macro-redefined"
100 #pragma GCC diagnostic warning "-Wdeprecated"
101 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
102 #pragma GCC diagnostic warning "-Wdisabled-optimization"
103 #pragma GCC diagnostic warning "-Wdouble-promotion"
104 #pragma GCC diagnostic warning "-Wformat=2"
105 #pragma GCC diagnostic warning "-Wignored-pragma-optimize"
106 #pragma GCC diagnostic warning "-Wlogical-op"
107 #pragma GCC diagnostic warning "-Wmaybe-uninitialized"
108 #pragma GCC diagnostic warning "-Wmissing-format-attribute"
109 #pragma GCC diagnostic warning "-Wundef"
112 /* Ignored currently, but should be fixed at some point. */
113 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
114 #pragma GCC diagnostic ignored "-Wconversion" // TODO fix
115 #pragma GCC diagnostic ignored "-Wformat-signedness" // TODO fix
116 #pragma GCC diagnostic ignored "-Wshadow" // TODO fix
117 #pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations" // TODO fix
118 #pragma GCC diagnostic ignored "-Wunused-parameter" // TODO fix
121 /* Ignored intentionally. */
122 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
123 #pragma GCC diagnostic ignored "-Wclass-memaccess"
124 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
125 #pragma GCC diagnostic ignored "-Wformat-zero-length"
126 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
127 #pragma GCC diagnostic ignored "-Wpacked" // Erratic impl in clang
128 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
129 #pragma GCC diagnostic ignored "-Wtype-limits"
130 #pragma GCC diagnostic ignored "-Wc++11-compat" // only gcc raises it
137 #include "hb-config.hh"
141 * Following added based on what AC_USE_SYSTEM_EXTENSIONS adds to
142 * config.h.in. Copied here for the convenience of those embedding
143 * HarfBuzz and not using our build system.
145 /* Enable extensions on AIX 3, Interix. */
147 # define _ALL_SOURCE 1
149 /* Enable GNU extensions on systems that have them. */
151 # define _GNU_SOURCE 1
153 /* Enable threading extensions on Solaris. */
154 #ifndef _POSIX_PTHREAD_SEMANTICS
155 # define _POSIX_PTHREAD_SEMANTICS 1
157 /* Enable extensions on HP NonStop. */
158 #ifndef _TANDEM_SOURCE
159 # define _TANDEM_SOURCE 1
161 /* Enable general extensions on Solaris. */
162 #ifndef __EXTENSIONS__
163 # define __EXTENSIONS__ 1
166 #if defined (_MSC_VER) && defined (HB_DLL_EXPORT)
167 #define HB_EXTERN __declspec (dllexport) extern
186 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
187 #ifdef __MINGW32_VERSION
188 #ifndef WIN32_LEAN_AND_MEAN
189 #define WIN32_LEAN_AND_MEAN 1
197 #define HB_PASTE1(a,b) a##b
198 #define HB_PASTE(a,b) HB_PASTE1(a,b)
201 /* Compile-time custom allocator support. */
203 #if defined(hb_malloc_impl) \
204 && defined(hb_calloc_impl) \
205 && defined(hb_realloc_impl) \
206 && defined(hb_free_impl)
207 extern "C" void* hb_malloc_impl(size_t size);
208 extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
209 extern "C" void* hb_realloc_impl(void *ptr, size_t size);
210 extern "C" void hb_free_impl(void *ptr);
211 #define malloc hb_malloc_impl
212 #define calloc hb_calloc_impl
213 #define realloc hb_realloc_impl
214 #define free hb_free_impl
219 * Compiler attributes
222 #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
223 #define likely(expr) (__builtin_expect (!!(expr), 1))
224 #define unlikely(expr) (__builtin_expect (!!(expr), 0))
226 #define likely(expr) (expr)
227 #define unlikely(expr) (expr)
230 #if !defined(__GNUC__) && !defined(__clang__)
232 #define __attribute__(x)
235 #if defined(__GNUC__) && (__GNUC__ >= 3)
236 #define HB_PURE_FUNC __attribute__((pure))
237 #define HB_CONST_FUNC __attribute__((const))
238 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
241 #define HB_CONST_FUNC
242 #define HB_PRINTF_FUNC(format_idx, arg_idx)
244 #if defined(__GNUC__) && (__GNUC__ >= 4) || (__clang__)
245 #define HB_UNUSED __attribute__((unused))
246 #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
247 #define HB_UNUSED __pragma(warning(suppress: 4100 4101))
253 # if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
254 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
255 # elif defined(__MINGW32__)
256 /* We use -export-symbols on mingw32, since it does not support visibility attributes. */
258 # elif defined (_MSC_VER) && defined (HB_DLL_EXPORT)
259 /* We do not try to export internal symbols on Visual Studio */
263 # define HB_NO_VISIBILITY 1
267 /* https://github.com/harfbuzz/harfbuzz/issues/1651 */
268 #if defined(__clang__) && __clang_major__ < 10
269 #define static_const static
271 #define static_const static const
274 #if defined(__GNUC__) && (__GNUC__ >= 3)
275 #define HB_FUNC __PRETTY_FUNCTION__
276 #elif defined(_MSC_VER)
277 #define HB_FUNC __FUNCSIG__
279 #define HB_FUNC __func__
282 #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
283 /* https://github.com/harfbuzz/harfbuzz/issues/630 */
288 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
289 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
290 * cases that fall through without a break or return statement. HB_FALLTHROUGH
291 * is only needed on cases that have code:
294 * case 1: // These cases have no code. No fallthrough annotations are needed.
297 * foo = 4; // This case has code, so a fallthrough annotation is needed:
303 #if defined(__clang__) && __cplusplus >= 201103L
304 /* clang's fallthrough annotations are only available starting in C++11. */
305 # define HB_FALLTHROUGH [[clang::fallthrough]]
306 #elif defined(__GNUC__) && (__GNUC__ >= 7)
307 /* GNU fallthrough attribute is available from GCC7 */
308 # define HB_FALLTHROUGH __attribute__((fallthrough))
309 #elif defined(_MSC_VER)
311 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
312 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
315 # define HB_FALLTHROUGH __fallthrough
317 # define HB_FALLTHROUGH /* FALLTHROUGH */
320 /* https://github.com/harfbuzz/harfbuzz/issues/1852 */
321 #if defined(__clang__) && !(defined(_AIX) && (defined(__IBMCPP__) || defined(__ibmxl__)))
322 /* Disable certain sanitizer errors. */
323 /* https://github.com/harfbuzz/harfbuzz/issues/1247 */
324 #define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow")))
326 #define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW
331 /* We need Windows Vista for both Uniscribe backend and for
332 * MemoryBarrier. We don't support compiling on Windows XP,
333 * though we run on it fine. */
334 # if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
337 # ifndef _WIN32_WINNT
338 # if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
339 # define _WIN32_WINNT 0x0600
342 # ifndef WIN32_LEAN_AND_MEAN
343 # define WIN32_LEAN_AND_MEAN 1
349 # if defined(_WIN32_WCE)
350 /* Some things not defined on Windows CE. */
351 # define vsnprintf _vsnprintf
352 # ifndef HB_NO_GETENV
353 # define HB_NO_GETENV
355 # if _WIN32_WCE < 0x800
356 # define HB_NO_SETLOCALE
359 # elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
360 # ifndef HB_NO_GETENV
361 # define HB_NO_GETENV
364 # if defined(_MSC_VER) && _MSC_VER < 1900
365 # define snprintf _snprintf
370 #define getenv(Name) nullptr
376 static int HB_UNUSED _hb_errno = 0;
378 # define errno _hb_errno
381 #if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
382 /* atexit() is only safe to be called from shared libraries on certain
383 * platforms. Whitelist.
384 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
385 # if defined(__linux) && defined(__GLIBC_PREREQ)
386 # if __GLIBC_PREREQ(2,3)
387 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
388 # define HB_USE_ATEXIT 1
390 # elif defined(_MSC_VER) || defined(__MINGW32__)
392 * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
393 * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
394 * mingw32 headers say atexit is safe to use in shared libraries.
396 # define HB_USE_ATEXIT 1
397 # elif defined(__ANDROID__)
398 /* This is available since Android NKD r8 or r8b:
399 * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
401 # define HB_USE_ATEXIT 1
402 # elif defined(__APPLE__)
403 /* For macOS and related platforms, the atexit man page indicates
404 * that it will be invoked when the library is unloaded, not only
405 * at application exit.
407 # define HB_USE_ATEXIT 1
411 # undef HB_USE_ATEXIT
413 #ifndef HB_USE_ATEXIT
414 # define HB_USE_ATEXIT 0
417 #define HB_STMT_START do
418 #define HB_STMT_END while (0)
420 /* Static-assert as expression. */
421 template <unsigned int cond> class hb_assert_constant_t;
422 template <> class hb_assert_constant_t<1> {};
423 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
425 /* Lets assert int types. Saves trouble down the road. */
426 static_assert ((sizeof (int8_t) == 1), "");
427 static_assert ((sizeof (uint8_t) == 1), "");
428 static_assert ((sizeof (int16_t) == 2), "");
429 static_assert ((sizeof (uint16_t) == 2), "");
430 static_assert ((sizeof (int32_t) == 4), "");
431 static_assert ((sizeof (uint32_t) == 4), "");
432 static_assert ((sizeof (int64_t) == 8), "");
433 static_assert ((sizeof (uint64_t) == 8), "");
434 static_assert ((sizeof (hb_codepoint_t) == 4), "");
435 static_assert ((sizeof (hb_position_t) == 4), "");
436 static_assert ((sizeof (hb_mask_t) == 4), "");
437 static_assert ((sizeof (hb_var_int_t) == 4), "");
439 #define HB_DELETE_COPY_ASSIGN(TypeName) \
440 TypeName(const TypeName&) = delete; \
441 void operator=(const TypeName&) = delete
442 #define HB_DELETE_CREATE_COPY_ASSIGN(TypeName) \
443 TypeName() = delete; \
444 TypeName(const TypeName&) = delete; \
445 void operator=(const TypeName&) = delete
450 /* Enable bitwise ops on enums marked as flags_t */
451 /* To my surprise, looks like the function resolver is happy to silently cast
452 * one enum to another... So this doesn't provide the type-checking that I
453 * originally had in mind... :(.
455 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
458 # pragma warning(disable:4200)
459 # pragma warning(disable:4800)
461 #define HB_MARK_AS_FLAG_T(T) \
463 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
464 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
465 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
466 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
467 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
468 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
469 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
471 static_assert (true, "")
473 /* Useful for set-operations on small enums.
474 * For example, for testing "x ∈ {x1, x2, x3}" use:
475 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
477 #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x)))
478 #define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0)
479 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
480 #define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x)))
481 #define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0)
484 /* Size signifying variable-sized array */
486 #define HB_VAR_ARRAY 1
492 return x >= 0 ? floor ((double) x + .5) : ceil ((double) x - .5);
495 #define roundf(x) _hb_roundf(x)
498 /* Endian swap, used in Windows related backends */
499 static inline uint16_t hb_uint16_swap (const uint16_t v)
500 { return (v >> 8) | (v << 8); }
501 static inline uint32_t hb_uint32_swap (const uint32_t v)
502 { return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
505 * Big-endian integers. Here because fundamental.
508 template <typename Type, int Bytes> struct BEInt;
510 template <typename Type>
511 struct BEInt<Type, 1>
514 BEInt<Type, 1>& operator = (Type V)
519 operator Type () const { return v; }
522 template <typename Type>
523 struct BEInt<Type, 2>
526 BEInt<Type, 2>& operator = (Type V)
528 v[0] = (V >> 8) & 0xFF;
532 operator Type () const
534 #if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \
535 defined(__BYTE_ORDER) && \
536 (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN)
537 /* Spoon-feed the compiler a big-endian integer with alignment 1.
538 * https://github.com/harfbuzz/harfbuzz/pull/1398 */
539 struct __attribute__((packed)) packed_uint16_t { uint16_t v; };
540 #if __BYTE_ORDER == __LITTLE_ENDIAN
541 return __builtin_bswap16 (((packed_uint16_t *) this)->v);
542 #else /* __BYTE_ORDER == __BIG_ENDIAN */
543 return ((packed_uint16_t *) this)->v;
549 private: uint8_t v[2];
551 template <typename Type>
552 struct BEInt<Type, 3>
555 BEInt<Type, 3>& operator = (Type V)
557 v[0] = (V >> 16) & 0xFF;
558 v[1] = (V >> 8) & 0xFF;
562 operator Type () const
568 private: uint8_t v[3];
570 template <typename Type>
571 struct BEInt<Type, 4>
574 BEInt<Type, 4>& operator = (Type V)
576 v[0] = (V >> 24) & 0xFF;
577 v[1] = (V >> 16) & 0xFF;
578 v[2] = (V >> 8) & 0xFF;
582 operator Type () const
589 private: uint8_t v[4];
594 * For lack of a better place, put Zawgyi script hack here.
595 * https://github.com/harfbuzz/harfbuzz/issues/1162
598 #define HB_SCRIPT_MYANMAR_ZAWGYI ((hb_script_t) HB_TAG ('Q','a','a','g'))
601 /* Headers we include for everyone. Keep topologically sorted by dependency.
602 * They express dependency amongst themselves, but no other file should include
604 #include "hb-meta.hh"
605 #include "hb-mutex.hh"
606 #include "hb-number.hh"
607 #include "hb-atomic.hh" // Requires: hb-meta
608 #include "hb-null.hh" // Requires: hb-meta
609 #include "hb-algs.hh" // Requires: hb-meta hb-null hb-number
610 #include "hb-iter.hh" // Requires: hb-algs hb-meta
611 #include "hb-debug.hh" // Requires: hb-algs hb-atomic
612 #include "hb-array.hh" // Requires: hb-algs hb-iter hb-null
613 #include "hb-vector.hh" // Requires: hb-array hb-null
614 #include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector