Imported Upstream version 2.4.0
[platform/upstream/harfbuzz.git] / src / hb.hh
1 /*
2  * Copyright © 2007,2008,2009  Red Hat, Inc.
3  * Copyright © 2011,2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
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.
12  *
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.
18  *
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.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #ifndef HB_HH
30 #define HB_HH
31
32 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC
33 #if defined(_MSC_VER)
34 #pragma warning( disable: 4068 ) /* Unknown pragma */
35 #endif
36 #if defined(__GNUC__) || defined(__clang__)
37 /* Rules:
38  *
39  * - All pragmas are declared GCC even if they are clang ones.  Otherwise GCC
40  *   nags, even though we instruct it to ignore -Wunknown-pragmas. ¯\_(ツ)_/¯
41  *
42  * - Within each category, keep sorted.
43  *
44  * - Warnings whose scope can be expanded in future compiler versions shall
45  *   be declared as "warning".  Otherwise, either ignored or error.
46  */
47
48 /* Setup.  Don't sort order within this category. */
49 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
50 #pragma GCC diagnostic warning "-Wall"
51 #pragma GCC diagnostic warning "-Wextra"
52 #endif
53 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
54 #pragma GCC diagnostic ignored "-Wpragmas"
55 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
56 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
57 #endif
58 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
59 //#pragma GCC diagnostic warning "-Weverything"
60 #endif
61
62 /* Error.  Should never happen. */
63 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_ERROR
64 #pragma GCC diagnostic error   "-Wc++11-narrowing"
65 #pragma GCC diagnostic error   "-Wcast-align"
66 #pragma GCC diagnostic error   "-Wcast-function-type"
67 #pragma GCC diagnostic error   "-Wdelete-non-virtual-dtor"
68 #pragma GCC diagnostic error   "-Wformat-security"
69 #pragma GCC diagnostic error   "-Wimplicit-function-declaration"
70 #pragma GCC diagnostic error   "-Winit-self"
71 #pragma GCC diagnostic error   "-Wmissing-braces"
72 #pragma GCC diagnostic error   "-Wmissing-declarations"
73 #pragma GCC diagnostic error   "-Wmissing-prototypes"
74 #pragma GCC diagnostic error   "-Wnested-externs"
75 #pragma GCC diagnostic error   "-Wold-style-definition"
76 #pragma GCC diagnostic error   "-Wpointer-arith"
77 #pragma GCC diagnostic error   "-Wredundant-decls"
78 #pragma GCC diagnostic error   "-Wreorder"
79 #pragma GCC diagnostic error   "-Wsign-compare"
80 #pragma GCC diagnostic error   "-Wstrict-prototypes"
81 #pragma GCC diagnostic error   "-Wstring-conversion"
82 #pragma GCC diagnostic error   "-Wswitch-enum"
83 #pragma GCC diagnostic error   "-Wtautological-overlap-compare"
84 #pragma GCC diagnostic error   "-Wunneeded-internal-declaration"
85 #pragma GCC diagnostic error   "-Wunused"
86 #pragma GCC diagnostic error   "-Wunused-local-typedefs"
87 #pragma GCC diagnostic error   "-Wunused-value"
88 #pragma GCC diagnostic error   "-Wunused-variable"
89 #pragma GCC diagnostic error   "-Wvla"
90 #pragma GCC diagnostic error   "-Wwrite-strings"
91 #endif
92
93 /* Warning.  To be investigated if happens. */
94 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
95 #pragma GCC diagnostic warning "-Wbuiltin-macro-redefined"
96 #pragma GCC diagnostic warning "-Wdisabled-optimization"
97 #pragma GCC diagnostic warning "-Wformat=2"
98 #pragma GCC diagnostic warning "-Wignored-pragma-optimize"
99 #pragma GCC diagnostic warning "-Wlogical-op"
100 #pragma GCC diagnostic warning "-Wmaybe-uninitialized"
101 #pragma GCC diagnostic warning "-Wmissing-format-attribute"
102 #pragma GCC diagnostic warning "-Wundef"
103 #endif
104
105 /* Ignored currently, but should be fixed at some point. */
106 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
107 #pragma GCC diagnostic ignored "-Wconversion"                   // TODO fix
108 #pragma GCC diagnostic ignored "-Wformat-signedness"            // TODO fix
109 #pragma GCC diagnostic ignored "-Wshadow"                       // TODO fix
110 #pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations"    // TODO fix
111 #pragma GCC diagnostic ignored "-Wunused-parameter"             // TODO fix
112 #endif
113
114 /* Ignored intentionally. */
115 #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
116 #pragma GCC diagnostic ignored "-Wclass-memaccess"
117 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
118 #pragma GCC diagnostic ignored "-Wformat-zero-length"
119 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
120 #pragma GCC diagnostic ignored "-Wpacked" // Erratic impl in clang
121 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
122 #pragma GCC diagnostic ignored "-Wtype-limits"
123 #endif
124
125 #endif
126 #endif
127
128 #ifdef HAVE_CONFIG_H
129 #include "config.h"
130 #endif
131
132 /*
133  * Following added based on what AC_USE_SYSTEM_EXTENSIONS adds to
134  * config.h.in.  Copied here for the convenience of those embedding
135  * HarfBuzz and not using our build system.
136  */
137 /* Enable extensions on AIX 3, Interix.  */
138 #ifndef _ALL_SOURCE
139 # define _ALL_SOURCE 1
140 #endif
141 /* Enable GNU extensions on systems that have them.  */
142 #ifndef _GNU_SOURCE
143 # define _GNU_SOURCE 1
144 #endif
145 /* Enable threading extensions on Solaris.  */
146 #ifndef _POSIX_PTHREAD_SEMANTICS
147 # define _POSIX_PTHREAD_SEMANTICS 1
148 #endif
149 /* Enable extensions on HP NonStop.  */
150 #ifndef _TANDEM_SOURCE
151 # define _TANDEM_SOURCE 1
152 #endif
153 /* Enable general extensions on Solaris.  */
154 #ifndef __EXTENSIONS__
155 # define __EXTENSIONS__ 1
156 #endif
157
158 #if defined (_MSC_VER) && defined (HB_DLL_EXPORT)
159 #define HB_EXTERN __declspec (dllexport) extern
160 #endif
161
162 #include "hb.h"
163 #define HB_H_IN
164 #include "hb-ot.h"
165 #define HB_OT_H_IN
166 #include "hb-aat.h"
167 #define HB_AAT_H_IN
168
169 #include "hb-aat.h"
170
171 #include <math.h>
172 #include <stdlib.h>
173 #include <stddef.h>
174 #include <string.h>
175 #include <assert.h>
176 #include <errno.h>
177 #include <stdio.h>
178 #include <stdarg.h>
179
180 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
181 #include <intrin.h>
182 #endif
183
184 #define HB_PASTE1(a,b) a##b
185 #define HB_PASTE(a,b) HB_PASTE1(a,b)
186
187
188 /* Compile-time custom allocator support. */
189
190 #if defined(hb_malloc_impl) \
191  && defined(hb_calloc_impl) \
192  && defined(hb_realloc_impl) \
193  && defined(hb_free_impl)
194 extern "C" void* hb_malloc_impl(size_t size);
195 extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
196 extern "C" void* hb_realloc_impl(void *ptr, size_t size);
197 extern "C" void  hb_free_impl(void *ptr);
198 #define malloc hb_malloc_impl
199 #define calloc hb_calloc_impl
200 #define realloc hb_realloc_impl
201 #define free hb_free_impl
202
203 #if defined(hb_memalign_impl)
204 extern "C" int hb_memalign_impl(void **memptr, size_t alignment, size_t size);
205 #define posix_memalign hb_memalign_impl
206 #else
207 #undef HAVE_POSIX_MEMALIGN
208 #endif
209
210 #endif
211
212
213 /*
214  * Compiler attributes
215  */
216
217 /* https://github.com/harfbuzz/harfbuzz/issues/1634 */
218 #if __cplusplus < 201103L && !defined(_MSC_VER)
219
220 #ifndef nullptr
221 #define nullptr NULL
222 #endif
223
224 #ifndef constexpr
225 #define constexpr const
226 #endif
227
228 #ifndef static_assert
229 #define static_assert(e, msg) \
230         HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
231 #endif // static_assert
232
233 #if defined(__GNUC__)
234 #if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
235 #define thread_local __thread
236 #endif
237 #else
238 #define thread_local
239 #endif
240
241 template <typename T>
242 struct _hb_alignof
243 {
244   struct s
245   {
246     char c;
247     T t;
248   };
249   static constexpr size_t value = offsetof (s, t);
250 };
251 #ifndef alignof
252 #define alignof(x) (_hb_alignof<x>::value)
253 #endif
254
255 /* https://github.com/harfbuzz/harfbuzz/issues/1127 */
256 #ifndef explicit_operator
257 #define explicit_operator operator
258 #endif
259
260 #else /* __cplusplus >= 201103L */
261
262 /* https://github.com/harfbuzz/harfbuzz/issues/1127 */
263 #ifndef explicit_operator
264 #define explicit_operator explicit operator
265 #endif
266
267 #endif /* __cplusplus < 201103L */
268
269
270 #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
271 #define likely(expr) (__builtin_expect (!!(expr), 1))
272 #define unlikely(expr) (__builtin_expect (!!(expr), 0))
273 #else
274 #define likely(expr) (expr)
275 #define unlikely(expr) (expr)
276 #endif
277
278 #if !defined(__GNUC__) && !defined(__clang__)
279 #undef __attribute__
280 #define __attribute__(x)
281 #endif
282
283 #if defined(__GNUC__) && (__GNUC__ >= 3)
284 #define HB_PURE_FUNC    __attribute__((pure))
285 #define HB_CONST_FUNC   __attribute__((const))
286 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
287 #else
288 #define HB_PURE_FUNC
289 #define HB_CONST_FUNC
290 #define HB_PRINTF_FUNC(format_idx, arg_idx)
291 #endif
292 #if defined(__GNUC__) && (__GNUC__ >= 4)
293 #define HB_UNUSED       __attribute__((unused))
294 #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
295 #define HB_UNUSED __pragma(warning(suppress: 4100 4101))
296 #else
297 #define HB_UNUSED
298 #endif
299
300 #ifndef HB_INTERNAL
301 # if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
302 #  define HB_INTERNAL __attribute__((__visibility__("hidden")))
303 # elif defined(__MINGW32__)
304    /* We use -export-symbols on mingw32, since it does not support visibility attributes. */
305 #  define HB_INTERNAL
306 # elif defined (_MSC_VER) && defined (HB_DLL_EXPORT)
307    /* We do not try to export internal symbols on Visual Studio */
308 #  define HB_INTERNAL
309 #else
310 #  define HB_INTERNAL
311 #  define HB_NO_VISIBILITY 1
312 # endif
313 #endif
314
315 #if defined(__GNUC__) && (__GNUC__ >= 3)
316 #define HB_FUNC __PRETTY_FUNCTION__
317 #elif defined(_MSC_VER)
318 #define HB_FUNC __FUNCSIG__
319 #else
320 #define HB_FUNC __func__
321 #endif
322
323 #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
324 /* https://github.com/harfbuzz/harfbuzz/issues/630 */
325 #define __restrict
326 #endif
327
328 /*
329  * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
330  * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
331  * cases that fall through without a break or return statement. HB_FALLTHROUGH
332  * is only needed on cases that have code:
333  *
334  * switch (foo) {
335  *   case 1: // These cases have no code. No fallthrough annotations are needed.
336  *   case 2:
337  *   case 3:
338  *     foo = 4; // This case has code, so a fallthrough annotation is needed:
339  *     HB_FALLTHROUGH;
340  *   default:
341  *     return foo;
342  * }
343  */
344 #if defined(__clang__) && __cplusplus >= 201103L
345    /* clang's fallthrough annotations are only available starting in C++11. */
346 #  define HB_FALLTHROUGH [[clang::fallthrough]]
347 #elif defined(__GNUC__) && (__GNUC__ >= 7)
348    /* GNU fallthrough attribute is available from GCC7 */
349 #  define HB_FALLTHROUGH __attribute__((fallthrough))
350 #elif defined(_MSC_VER)
351    /*
352     * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
353     * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
354     */
355 #  include <sal.h>
356 #  define HB_FALLTHROUGH __fallthrough
357 #else
358 #  define HB_FALLTHROUGH /* FALLTHROUGH */
359 #endif
360
361 #if defined(__clang__)
362 /* Disable certain sanitizer errors. */
363 /* https://github.com/harfbuzz/harfbuzz/issues/1247 */
364 #define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow")))
365 #else
366 #define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW
367 #endif
368
369
370 #ifdef _WIN32
371    /* We need Windows Vista for both Uniscribe backend and for
372     * MemoryBarrier.  We don't support compiling on Windows XP,
373     * though we run on it fine. */
374 #  if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
375 #    undef _WIN32_WINNT
376 #  endif
377 #  ifndef _WIN32_WINNT
378 #    if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
379 #      define _WIN32_WINNT 0x0600
380 #    endif
381 #  endif
382 #  ifndef WIN32_LEAN_AND_MEAN
383 #    define WIN32_LEAN_AND_MEAN 1
384 #  endif
385 #  ifndef STRICT
386 #    define STRICT 1
387 #  endif
388
389 #  if defined(_WIN32_WCE)
390      /* Some things not defined on Windows CE. */
391 #    define vsnprintf _vsnprintf
392 #    define getenv(Name) nullptr
393 #    if _WIN32_WCE < 0x800
394 #      define setlocale(Category, Locale) "C"
395 static int errno = 0; /* Use something better? */
396 #    endif
397 #  elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
398 #    define getenv(Name) nullptr
399 #  endif
400 #  if defined(_MSC_VER) && _MSC_VER < 1900
401 #    define snprintf _snprintf
402 #  endif
403 #endif
404
405 #if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
406 /* atexit() is only safe to be called from shared libraries on certain
407  * platforms.  Whitelist.
408  * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
409 #  if defined(__linux) && defined(__GLIBC_PREREQ)
410 #    if __GLIBC_PREREQ(2,3)
411 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
412 #      define HB_USE_ATEXIT 1
413 #    endif
414 #  elif defined(_MSC_VER) || defined(__MINGW32__)
415 /* For MSVC:
416  * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
417  * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
418  * mingw32 headers say atexit is safe to use in shared libraries.
419  */
420 #    define HB_USE_ATEXIT 1
421 #  elif defined(__ANDROID__)
422 /* This is available since Android NKD r8 or r8b:
423  * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
424  */
425 #    define HB_USE_ATEXIT 1
426 #  elif defined(__APPLE__)
427 /* For macOS and related platforms, the atexit man page indicates
428  * that it will be invoked when the library is unloaded, not only
429  * at application exit.
430  */
431 #    define HB_USE_ATEXIT 1
432 #  endif
433 #endif
434 #ifdef HB_NO_ATEXIT
435 #  undef HB_USE_ATEXIT
436 #endif
437 #ifndef HB_USE_ATEXIT
438 #  define HB_USE_ATEXIT 0
439 #endif
440
441 #define HB_STMT_START do
442 #define HB_STMT_END   while (0)
443
444 /* Static-assert as expression. */
445 template <unsigned int cond> class hb_assert_constant_t;
446 template <> class hb_assert_constant_t<1> {};
447 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
448
449 /* Lets assert int types.  Saves trouble down the road. */
450 static_assert ((sizeof (int8_t) == 1), "");
451 static_assert ((sizeof (uint8_t) == 1), "");
452 static_assert ((sizeof (int16_t) == 2), "");
453 static_assert ((sizeof (uint16_t) == 2), "");
454 static_assert ((sizeof (int32_t) == 4), "");
455 static_assert ((sizeof (uint32_t) == 4), "");
456 static_assert ((sizeof (int64_t) == 8), "");
457 static_assert ((sizeof (uint64_t) == 8), "");
458 static_assert ((sizeof (hb_codepoint_t) == 4), "");
459 static_assert ((sizeof (hb_position_t) == 4), "");
460 static_assert ((sizeof (hb_mask_t) == 4), "");
461 static_assert ((sizeof (hb_var_int_t) == 4), "");
462
463
464 #if __cplusplus >= 201103L
465
466 /* We only enable these with C++11 or later, since earlier language
467  * does not allow structs with constructors in unions, and we need
468  * those. */
469
470 #define HB_NO_COPY_ASSIGN(TypeName) \
471   TypeName(const TypeName&); \
472   void operator=(const TypeName&)
473 #define HB_NO_COPY_ASSIGN_TEMPLATE(TypeName, T) \
474   TypeName(const TypeName<T>&); \
475   void operator=(const TypeName<T>&)
476 #define HB_NO_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \
477   TypeName(const TypeName<T1, T2>&); \
478   void operator=(const TypeName<T1, T2>&)
479 #define HB_NO_CREATE_COPY_ASSIGN(TypeName) \
480   TypeName(); \
481   TypeName(const TypeName&); \
482   void operator=(const TypeName&)
483 #define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) \
484   TypeName(); \
485   TypeName(const TypeName<T>&); \
486   void operator=(const TypeName<T>&)
487 #define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \
488   TypeName(); \
489   TypeName(const TypeName<T1, T2>&); \
490   void operator=(const TypeName<T1, T2>&)
491
492 #else /* __cpluspplus >= 201103L */
493
494 #define HB_NO_COPY_ASSIGN(TypeName) static_assert (true, "")
495 #define HB_NO_COPY_ASSIGN_TEMPLATE(TypeName, T) static_assert (true, "")
496 #define HB_NO_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) static_assert (true, "")
497 #define HB_NO_CREATE_COPY_ASSIGN(TypeName) static_assert (true, "")
498 #define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) static_assert (true, "")
499 #define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) static_assert (true, "")
500
501 #endif /* __cpluspplus >= 201103L */
502
503
504 /*
505  * Compiler-assisted vectorization parameters.
506  */
507
508 /*
509  * Disable vectorization for now.  To correctly use them, we should
510  * use posix_memalign() to allocate in hb_vector_t.  Otherwise, can
511  * cause misaligned access.
512  *
513  * https://bugs.chromium.org/p/chromium/issues/detail?id=860184
514  */
515 #if !defined(HB_VECTOR_SIZE)
516 #  define HB_VECTOR_SIZE 0
517 #endif
518
519 /* The `vector_size' attribute was introduced in gcc 3.1. */
520 #if !defined(HB_VECTOR_SIZE)
521 #  if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
522 #    define HB_VECTOR_SIZE 128
523 #  else
524 #    define HB_VECTOR_SIZE 0
525 #  endif
526 #endif
527 static_assert (0 == (HB_VECTOR_SIZE & (HB_VECTOR_SIZE - 1)), "HB_VECTOR_SIZE is not power of 2.");
528 static_assert (0 == (HB_VECTOR_SIZE % 64), "HB_VECTOR_SIZE is not multiple of 64.");
529 #if HB_VECTOR_SIZE
530 typedef uint64_t hb_vector_size_impl_t __attribute__((vector_size (HB_VECTOR_SIZE / 8)));
531 #else
532 typedef uint64_t hb_vector_size_impl_t;
533 #endif
534
535
536 /* HB_NDEBUG disables some sanity checks that are very safe to disable and
537  * should be disabled in production systems.  If NDEBUG is defined, enable
538  * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
539  * light-weight) to be enabled, then HB_DEBUG can be defined to disable
540  * the costlier checks. */
541 #ifdef NDEBUG
542 #define HB_NDEBUG 1
543 #endif
544
545
546 /* Flags */
547
548 /* Enable bitwise ops on enums marked as flags_t */
549 /* To my surprise, looks like the function resolver is happy to silently cast
550  * one enum to another...  So this doesn't provide the type-checking that I
551  * originally had in mind... :(.
552  *
553  * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
554  */
555 #ifdef _MSC_VER
556 # pragma warning(disable:4200)
557 # pragma warning(disable:4800)
558 #endif
559 #define HB_MARK_AS_FLAG_T(T) \
560         extern "C++" { \
561           static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
562           static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
563           static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
564           static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
565           static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
566           static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
567           static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
568         } \
569         static_assert (true, "")
570
571 /* Useful for set-operations on small enums.
572  * For example, for testing "x ∈ {x1, x2, x3}" use:
573  * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
574  */
575 #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x)))
576 #define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0)
577 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
578 #define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x)))
579 #define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0)
580
581
582 /* Size signifying variable-sized array */
583 #define VAR 1
584
585
586 /* fallback for round() */
587 static inline double
588 _hb_round (double x)
589 {
590   if (x >= 0)
591     return floor (x + 0.5);
592   else
593     return ceil (x - 0.5);
594 }
595 #if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND)
596 #define round(x) _hb_round(x)
597 #endif
598
599
600 /* fallback for posix_memalign() */
601 static inline int
602 _hb_memalign(void **memptr, size_t alignment, size_t size)
603 {
604   if (unlikely (0 != (alignment & (alignment - 1)) ||
605                 !alignment ||
606                 0 != (alignment & (sizeof (void *) - 1))))
607     return EINVAL;
608
609   char *p = (char *) malloc (size + alignment - 1);
610   if (unlikely (!p))
611     return ENOMEM;
612
613   size_t off = (size_t) p & (alignment - 1);
614   if (off)
615     p += alignment - off;
616
617   *memptr = (void *) p;
618
619   return 0;
620 }
621 #if !defined(posix_memalign) && !defined(HAVE_POSIX_MEMALIGN)
622 #define posix_memalign _hb_memalign
623 #endif
624
625
626 /*
627  * For lack of a better place, put Zawgyi script hack here.
628  * https://github.com/harfbuzz/harfbuzz/issues/1162
629  */
630
631 #define HB_SCRIPT_MYANMAR_ZAWGYI        ((hb_script_t) HB_TAG ('Q','a','a','g'))
632
633
634 /* Some really basic things everyone wants. */
635 template <typename T> struct hb_remove_const { typedef T value; };
636 template <typename T> struct hb_remove_const<const T> { typedef T value; };
637 #define hb_remove_const(T) hb_remove_const<T>::value
638 template <typename T> struct hb_remove_reference { typedef T value; };
639 template <typename T> struct hb_remove_reference<T &> { typedef T value; };
640 #define hb_remove_reference(T) hb_remove_reference<T>::value
641 template <typename T> struct hb_remove_pointer { typedef T value; };
642 template <typename T> struct hb_remove_pointer<T *> { typedef T value; };
643 #define hb_remove_pointer(T) hb_remove_pointer<T>::value
644
645
646 /* Headers we include for everyone.  Keep topologically sorted by dependency.
647  * They express dependency amongst themselves, but no other file should include
648  * them directly.*/
649 #include "hb-atomic.hh"
650 #include "hb-mutex.hh"
651 #include "hb-null.hh"
652 #include "hb-dsalgs.hh" // Requires: hb-null
653 #include "hb-iter.hh"   // Requires: hb-null
654 #include "hb-debug.hh"  // Requires: hb-atomic hb-dsalgs
655 #include "hb-array.hh"  // Requires: hb-dsalgs hb-iter hb-null
656 #include "hb-vector.hh" // Requires: hb-array hb-null
657 #include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector
658
659 #endif /* HB_HH */