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
187 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
188 #ifdef __MINGW32_VERSION
189 #ifndef WIN32_LEAN_AND_MEAN
190 #define WIN32_LEAN_AND_MEAN 1
198 #define HB_PASTE1(a,b) a##b
199 #define HB_PASTE(a,b) HB_PASTE1(a,b)
202 /* Compile-time custom allocator support. */
204 #if defined(hb_malloc_impl) \
205 && defined(hb_calloc_impl) \
206 && defined(hb_realloc_impl) \
207 && defined(hb_free_impl)
208 extern "C" void* hb_malloc_impl(size_t size);
209 extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
210 extern "C" void* hb_realloc_impl(void *ptr, size_t size);
211 extern "C" void hb_free_impl(void *ptr);
212 #define malloc hb_malloc_impl
213 #define calloc hb_calloc_impl
214 #define realloc hb_realloc_impl
215 #define free hb_free_impl
220 * Compiler attributes
223 #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
224 #define likely(expr) (__builtin_expect (!!(expr), 1))
225 #define unlikely(expr) (__builtin_expect (!!(expr), 0))
227 #define likely(expr) (expr)
228 #define unlikely(expr) (expr)
231 #if !defined(__GNUC__) && !defined(__clang__)
233 #define __attribute__(x)
236 #if defined(__GNUC__) && (__GNUC__ >= 3)
237 #define HB_PURE_FUNC __attribute__((pure))
238 #define HB_CONST_FUNC __attribute__((const))
239 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
242 #define HB_CONST_FUNC
243 #define HB_PRINTF_FUNC(format_idx, arg_idx)
245 #if defined(__GNUC__) && (__GNUC__ >= 4)
246 #define HB_UNUSED __attribute__((unused))
247 #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
248 #define HB_UNUSED __pragma(warning(suppress: 4100 4101))
254 # if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
255 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
256 # elif defined(__MINGW32__)
257 /* We use -export-symbols on mingw32, since it does not support visibility attributes. */
259 # elif defined (_MSC_VER) && defined (HB_DLL_EXPORT)
260 /* We do not try to export internal symbols on Visual Studio */
264 # define HB_NO_VISIBILITY 1
268 /* https://github.com/harfbuzz/harfbuzz/issues/1651 */
269 #if defined(__clang__) && __clang_major__ < 10
270 #define static_const static
272 #define static_const static const
275 #if defined(__GNUC__) && (__GNUC__ >= 3)
276 #define HB_FUNC __PRETTY_FUNCTION__
277 #elif defined(_MSC_VER)
278 #define HB_FUNC __FUNCSIG__
280 #define HB_FUNC __func__
283 #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
284 /* https://github.com/harfbuzz/harfbuzz/issues/630 */
289 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
290 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
291 * cases that fall through without a break or return statement. HB_FALLTHROUGH
292 * is only needed on cases that have code:
295 * case 1: // These cases have no code. No fallthrough annotations are needed.
298 * foo = 4; // This case has code, so a fallthrough annotation is needed:
304 #if defined(__clang__) && __cplusplus >= 201103L
305 /* clang's fallthrough annotations are only available starting in C++11. */
306 # define HB_FALLTHROUGH [[clang::fallthrough]]
307 #elif defined(__GNUC__) && (__GNUC__ >= 7)
308 /* GNU fallthrough attribute is available from GCC7 */
309 # define HB_FALLTHROUGH __attribute__((fallthrough))
310 #elif defined(_MSC_VER)
312 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
313 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
316 # define HB_FALLTHROUGH __fallthrough
318 # define HB_FALLTHROUGH /* FALLTHROUGH */
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
357 static int errno = 0; /* Use something better? */
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
373 #if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
374 /* atexit() is only safe to be called from shared libraries on certain
375 * platforms. Whitelist.
376 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
377 # if defined(__linux) && defined(__GLIBC_PREREQ)
378 # if __GLIBC_PREREQ(2,3)
379 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
380 # define HB_USE_ATEXIT 1
382 # elif defined(_MSC_VER) || defined(__MINGW32__)
384 * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
385 * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
386 * mingw32 headers say atexit is safe to use in shared libraries.
388 # define HB_USE_ATEXIT 1
389 # elif defined(__ANDROID__)
390 /* This is available since Android NKD r8 or r8b:
391 * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
393 # define HB_USE_ATEXIT 1
394 # elif defined(__APPLE__)
395 /* For macOS and related platforms, the atexit man page indicates
396 * that it will be invoked when the library is unloaded, not only
397 * at application exit.
399 # define HB_USE_ATEXIT 1
403 # undef HB_USE_ATEXIT
405 #ifndef HB_USE_ATEXIT
406 # define HB_USE_ATEXIT 0
409 #define HB_STMT_START do
410 #define HB_STMT_END while (0)
412 /* Static-assert as expression. */
413 template <unsigned int cond> class hb_assert_constant_t;
414 template <> class hb_assert_constant_t<1> {};
415 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
417 /* Lets assert int types. Saves trouble down the road. */
418 static_assert ((sizeof (int8_t) == 1), "");
419 static_assert ((sizeof (uint8_t) == 1), "");
420 static_assert ((sizeof (int16_t) == 2), "");
421 static_assert ((sizeof (uint16_t) == 2), "");
422 static_assert ((sizeof (int32_t) == 4), "");
423 static_assert ((sizeof (uint32_t) == 4), "");
424 static_assert ((sizeof (int64_t) == 8), "");
425 static_assert ((sizeof (uint64_t) == 8), "");
426 static_assert ((sizeof (hb_codepoint_t) == 4), "");
427 static_assert ((sizeof (hb_position_t) == 4), "");
428 static_assert ((sizeof (hb_mask_t) == 4), "");
429 static_assert ((sizeof (hb_var_int_t) == 4), "");
431 #define HB_DELETE_COPY_ASSIGN(TypeName) \
432 TypeName(const TypeName&) = delete; \
433 void operator=(const TypeName&) = delete
434 #define HB_DELETE_CREATE_COPY_ASSIGN(TypeName) \
435 TypeName() = delete; \
436 TypeName(const TypeName&) = delete; \
437 void operator=(const TypeName&) = delete
442 /* Enable bitwise ops on enums marked as flags_t */
443 /* To my surprise, looks like the function resolver is happy to silently cast
444 * one enum to another... So this doesn't provide the type-checking that I
445 * originally had in mind... :(.
447 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
450 # pragma warning(disable:4200)
451 # pragma warning(disable:4800)
453 #define HB_MARK_AS_FLAG_T(T) \
455 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
456 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
457 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
458 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
459 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
460 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
461 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
463 static_assert (true, "")
465 /* Useful for set-operations on small enums.
466 * For example, for testing "x ∈ {x1, x2, x3}" use:
467 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
469 #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x)))
470 #define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0)
471 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
472 #define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x)))
473 #define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0)
476 /* Size signifying variable-sized array */
481 * Big-endian integers. Here because fundamental.
484 template <typename Type, int Bytes> struct BEInt;
486 template <typename Type>
487 struct BEInt<Type, 1>
490 BEInt<Type, 1>& operator = (Type V)
495 operator Type () const { return v; }
498 template <typename Type>
499 struct BEInt<Type, 2>
502 BEInt<Type, 2>& operator = (Type V)
504 v[0] = (V >> 8) & 0xFF;
508 operator Type () const
510 #if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \
511 defined(__BYTE_ORDER) && \
512 (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN)
513 /* Spoon-feed the compiler a big-endian integer with alignment 1.
514 * https://github.com/harfbuzz/harfbuzz/pull/1398 */
515 struct __attribute__((packed)) packed_uint16_t { uint16_t v; };
516 #if __BYTE_ORDER == __LITTLE_ENDIAN
517 return __builtin_bswap16 (((packed_uint16_t *) this)->v);
518 #else /* __BYTE_ORDER == __BIG_ENDIAN */
519 return ((packed_uint16_t *) this)->v;
525 private: uint8_t v[2];
527 template <typename Type>
528 struct BEInt<Type, 3>
531 BEInt<Type, 3>& operator = (Type V)
533 v[0] = (V >> 16) & 0xFF;
534 v[1] = (V >> 8) & 0xFF;
538 operator Type () const
544 private: uint8_t v[3];
546 template <typename Type>
547 struct BEInt<Type, 4>
550 BEInt<Type, 4>& operator = (Type V)
552 v[0] = (V >> 24) & 0xFF;
553 v[1] = (V >> 16) & 0xFF;
554 v[2] = (V >> 8) & 0xFF;
558 operator Type () const
565 private: uint8_t v[4];
570 * For lack of a better place, put Zawgyi script hack here.
571 * https://github.com/harfbuzz/harfbuzz/issues/1162
574 #define HB_SCRIPT_MYANMAR_ZAWGYI ((hb_script_t) HB_TAG ('Q','a','a','g'))
577 /* Headers we include for everyone. Keep topologically sorted by dependency.
578 * They express dependency amongst themselves, but no other file should include
580 #include "hb-meta.hh"
581 #include "hb-mutex.hh"
582 #include "hb-atomic.hh" // Requires: hb-meta
583 #include "hb-null.hh" // Requires: hb-meta
584 #include "hb-algs.hh" // Requires: hb-meta hb-null
585 #include "hb-iter.hh" // Requires: hb-algs hb-meta
586 #include "hb-debug.hh" // Requires: hb-algs hb-atomic
587 #include "hb-array.hh" // Requires: hb-algs hb-iter hb-null
588 #include "hb-vector.hh" // Requires: hb-array hb-null
589 #include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector