blob: 6714bbb8bc46a2ac3ebc8164958b8d0cf1eaaa13 [file] [log] [blame]
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2007,2008,2009 Red Hat, Inc.
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02003 * Copyright © 2011,2012 Google, Inc.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05006 *
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
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -040026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -050027 */
28
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070029#ifndef HB_HH
30#define HB_HH
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -050031
Behdad Esfahbodd6c54732019-01-17 15:22:46 -050032#ifndef HB_NO_DIAGNOSTIC_PRAGMAS
Behdad Esfahboda9946b72019-01-17 15:22:28 -050033// Enable
34#pragma GCC diagnostic warning "-Wall"
35#pragma GCC diagnostic warning "-Wextra"
Behdad Esfahbod3f2daae2019-01-17 15:27:31 -050036#pragma GCC diagnostic warning "-Wunused"
Behdad Esfahbod868b0c02019-01-17 15:08:36 -050037#pragma GCC diagnostic warning "-Wcast-align"
Behdad Esfahboda9946b72019-01-17 15:22:28 -050038// Disable
39#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
Behdad Esfahbod14a560a2019-01-17 17:42:44 -050040#pragma GCC diagnostic ignored "-Wtype-limits"
Behdad Esfahboda9946b72019-01-17 15:22:28 -050041#pragma GCC diagnostic ignored "-Wunused-parameter" // TODO Fix
Behdad Esfahbod3f2daae2019-01-17 15:27:31 -050042#pragma GCC diagnostic ignored "-Wshadow" // TODO fix
Behdad Esfahbod868b0c02019-01-17 15:08:36 -050043#endif
44
Behdad Esfahbod5ddd9cc2011-09-16 16:40:44 -040045#ifdef HAVE_CONFIG_H
Behdad Esfahboddf660282009-08-01 20:46:02 -040046#include "config.h"
47#endif
Behdad Esfahbod12360f72008-01-23 15:50:38 -050048
Behdad Esfahbod49334f92018-12-17 18:27:36 -050049/*
50 * Following added based on what AC_USE_SYSTEM_EXTENSIONS adds to
51 * config.h.in. Copied here for the convenience of those embedding
52 * HarfBuzz and not using our build system.
53 */
54/* Enable extensions on AIX 3, Interix. */
55#ifndef _ALL_SOURCE
56# define _ALL_SOURCE 1
57#endif
58/* Enable GNU extensions on systems that have them. */
59#ifndef _GNU_SOURCE
60# define _GNU_SOURCE 1
61#endif
62/* Enable threading extensions on Solaris. */
63#ifndef _POSIX_PTHREAD_SEMANTICS
64# define _POSIX_PTHREAD_SEMANTICS 1
65#endif
66/* Enable extensions on HP NonStop. */
67#ifndef _TANDEM_SOURCE
68# define _TANDEM_SOURCE 1
69#endif
70/* Enable general extensions on Solaris. */
71#ifndef __EXTENSIONS__
72# define __EXTENSIONS__ 1
73#endif
74
Chun-wei Fan8e4ad1d2018-07-25 18:12:34 +080075#if defined (_MSC_VER) && defined (HB_DLL_EXPORT)
76#define HB_EXTERN __declspec (dllexport) extern
77#endif
78
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040079#include "hb.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040080#define HB_H_IN
Behdad Esfahbod3e32cd92012-04-23 13:20:52 -040081#include "hb-ot.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040082#define HB_OT_H_IN
Behdad Esfahbodb2061332018-11-23 14:05:11 -050083#include "hb-aat.h"
84#define HB_AAT_H_IN
Behdad Esfahbodb28815c2009-08-04 22:35:36 -040085
Ebrahim Byagowia8726cb2018-11-17 14:23:11 +033086#include "hb-aat.h"
87
Behdad Esfahbod7cdd6ab2018-01-10 04:33:08 +010088#include <math.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040089#include <stdlib.h>
Behdad Esfahbodb65c0602011-07-28 16:48:43 -040090#include <stddef.h>
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040091#include <string.h>
92#include <assert.h>
Behdad Esfahbod7acb3892009-08-05 15:20:34 -040093#include <errno.h>
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040094#include <stdio.h>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -040095#include <stdarg.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040096
Tor Andersson8f4c1232018-04-13 23:01:54 +043097#if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
Arkady Shapkin6c103282018-02-15 23:17:46 +030098#include <intrin.h>
99#endif
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400100
Behdad Esfahbod2a749682017-10-26 19:48:33 -0600101#define HB_PASTE1(a,b) a##b
102#define HB_PASTE(a,b) HB_PASTE1(a,b)
103
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200104
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100105/* Compile-time custom allocator support. */
106
107#if defined(hb_malloc_impl) \
108 && defined(hb_calloc_impl) \
109 && defined(hb_realloc_impl) \
110 && defined(hb_free_impl)
Behdad Esfahbodcc6ea302015-10-12 17:21:52 -0400111extern "C" void* hb_malloc_impl(size_t size);
112extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
113extern "C" void* hb_realloc_impl(void *ptr, size_t size);
114extern "C" void hb_free_impl(void *ptr);
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100115#define malloc hb_malloc_impl
116#define calloc hb_calloc_impl
117#define realloc hb_realloc_impl
118#define free hb_free_impl
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200119
Behdad Esfahbod763f8782018-07-10 13:47:41 +0200120#if defined(hb_memalign_impl)
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200121extern "C" int hb_memalign_impl(void **memptr, size_t alignment, size_t size);
122#define posix_memalign hb_memalign_impl
123#else
124#undef HAVE_POSIX_MEMALIGN
125#endif
126
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100127#endif
128
129
Behdad Esfahbod2aae6172018-07-11 17:27:23 +0200130/*
131 * Compiler attributes
Behdad Esfahbod37be7742018-08-06 04:51:38 -0700132 */
Behdad Esfahbod292c1002018-07-10 13:16:52 +0200133
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200134#if __cplusplus < 201103L
135
Behdad Esfahbod62e312e2017-10-27 09:29:20 -0600136#ifndef nullptr
137#define nullptr NULL
138#endif
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200139
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200140#ifndef constexpr
141#define constexpr const
142#endif
143
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200144#ifndef static_assert
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200145#define static_assert(e, msg) \
Behdad Esfahbod6f08b122017-10-26 18:23:03 -0600146 HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200147#endif // static_assert
148
Behdad Esfahbodf83e9922018-05-24 11:46:57 -0700149#ifdef __GNUC__
150#if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
151#define thread_local __thread
152#endif
153#else
154#define thread_local
155#endif
156
Behdad Esfahbod2aae6172018-07-11 17:27:23 +0200157template <typename T>
158struct _hb_alignof
159{
160 struct s
161 {
162 char c;
163 T t;
164 };
165 static constexpr size_t value = offsetof (s, t);
166};
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200167#ifndef alignof
Behdad Esfahbod27c53172018-07-10 13:03:42 +0200168#define alignof(x) (_hb_alignof<x>::value)
Behdad Esfahbod37be7742018-08-06 04:51:38 -0700169#endif
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200170
Behdad Esfahbodf9a3eab2018-08-12 12:21:56 -0700171/* https://github.com/harfbuzz/harfbuzz/issues/1127 */
172#ifndef explicit_operator
Behdad Esfahbod01d06e32018-12-16 14:27:43 -0500173#define explicit_operator operator
Behdad Esfahbodf9a3eab2018-08-12 12:21:56 -0700174#endif
175
176#else /* __cplusplus >= 201103L */
177
178/* https://github.com/harfbuzz/harfbuzz/issues/1127 */
179#ifndef explicit_operator
Behdad Esfahbod01d06e32018-12-16 14:27:43 -0500180#define explicit_operator explicit operator
Behdad Esfahbodf9a3eab2018-08-12 12:21:56 -0700181#endif
182
183#endif /* __cplusplus < 201103L */
184
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200185
Ebrahim Byagowi76c48732017-06-02 21:53:10 +0430186#if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
187#define likely(expr) (__builtin_expect (!!(expr), 1))
188#define unlikely(expr) (__builtin_expect (!!(expr), 0))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400189#else
190#define likely(expr) (expr)
191#define unlikely(expr) (expr)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400192#endif
193
Steve Lhomme0b8f3ab2016-07-11 21:57:26 +0200194#if !defined(__GNUC__) && !defined(__clang__)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400195#undef __attribute__
196#define __attribute__(x)
197#endif
Behdad Esfahboda794ebf2009-08-06 12:32:35 -0400198
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400199#if __GNUC__ >= 3
200#define HB_PURE_FUNC __attribute__((pure))
201#define HB_CONST_FUNC __attribute__((const))
202#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
203#else
204#define HB_PURE_FUNC
205#define HB_CONST_FUNC
206#define HB_PRINTF_FUNC(format_idx, arg_idx)
207#endif
208#if __GNUC__ >= 4
209#define HB_UNUSED __attribute__((unused))
Behdad Esfahbodbe59f3c2017-12-05 09:01:28 -0800210#elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
211#define HB_UNUSED __pragma(warning(suppress: 4100 4101))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400212#else
213#define HB_UNUSED
214#endif
215
216#ifndef HB_INTERNAL
Behdad Esfahbod90d1c742018-02-11 19:08:59 -0800217# if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400218# define HB_INTERNAL __attribute__((__visibility__("hidden")))
Behdad Esfahbod94759d42018-07-23 15:01:39 -0700219# elif defined(__MINGW32__)
Chun-wei Fan8e4ad1d2018-07-25 18:12:34 +0800220 /* We use -export-symbols on mingw32, since it does not support visibility attributes. */
221# define HB_INTERNAL
222# elif defined (_MSC_VER) && defined (HB_DLL_EXPORT)
223 /* We do not try to export internal symbols on Visual Studio */
Behdad Esfahbod94759d42018-07-23 15:01:39 -0700224# define HB_INTERNAL
225#else
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400226# define HB_INTERNAL
Behdad Esfahbodb87cbe22018-02-07 14:11:26 -0500227# define HB_NO_VISIBILITY 1
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400228# endif
229#endif
230
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400231#if __GNUC__ >= 3
232#define HB_FUNC __PRETTY_FUNCTION__
233#elif defined(_MSC_VER)
234#define HB_FUNC __FUNCSIG__
235#else
236#define HB_FUNC __func__
237#endif
238
prrace498e4372018-06-09 16:04:28 -0700239#if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
Behdad Esfahbod8cef3a62018-02-09 16:04:23 -0600240/* https://github.com/harfbuzz/harfbuzz/issues/630 */
241#define __restrict
242#endif
243
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200244/*
245 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
246 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
247 * cases that fall through without a break or return statement. HB_FALLTHROUGH
248 * is only needed on cases that have code:
249 *
250 * switch (foo) {
251 * case 1: // These cases have no code. No fallthrough annotations are needed.
252 * case 2:
253 * case 3:
254 * foo = 4; // This case has code, so a fallthrough annotation is needed:
255 * HB_FALLTHROUGH;
256 * default:
257 * return foo;
258 * }
259 */
260#if defined(__clang__) && __cplusplus >= 201103L
261 /* clang's fallthrough annotations are only available starting in C++11. */
262# define HB_FALLTHROUGH [[clang::fallthrough]]
Behdad Esfahbodf41b9212018-02-05 19:51:09 -0500263#elif __GNUC__ >= 7
264 /* GNU fallthrough attribute is available from GCC7 */
265# define HB_FALLTHROUGH __attribute__((fallthrough))
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200266#elif defined(_MSC_VER)
267 /*
268 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
269 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
270 */
271# include <sal.h>
272# define HB_FALLTHROUGH __fallthrough
273#else
274# define HB_FALLTHROUGH /* FALLTHROUGH */
275#endif
276
Behdad Esfahbod62376a72018-10-14 15:20:50 -0700277#if defined(__clang__)
278/* Disable certain sanitizer errors. */
279/* https://github.com/harfbuzz/harfbuzz/issues/1247 */
280#define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow")))
281#else
282#define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW
283#endif
284
285
Ken Browneee5b5e2018-11-12 21:05:39 -0500286#ifdef _WIN32
Behdad Esfahboddb308282014-07-19 16:32:04 -0400287 /* We need Windows Vista for both Uniscribe backend and for
288 * MemoryBarrier. We don't support compiling on Windows XP,
289 * though we run on it fine. */
290# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
291# undef _WIN32_WINNT
292# endif
293# ifndef _WIN32_WINNT
Matt Oliver24dd6c12018-09-23 18:08:30 +1000294# if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
295# define _WIN32_WINNT 0x0600
296# endif
Behdad Esfahboddb308282014-07-19 16:32:04 -0400297# endif
Behdad Esfahbod270971a2014-08-15 14:28:04 -0400298# ifndef WIN32_LEAN_AND_MEAN
299# define WIN32_LEAN_AND_MEAN 1
300# endif
301# ifndef STRICT
302# define STRICT 1
303# endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500304
Konstantin Rittf3537b62015-01-25 09:50:51 +0400305# if defined(_WIN32_WCE)
306 /* Some things not defined on Windows CE. */
Konstantin Ritt50690622016-04-26 12:02:26 +0400307# define vsnprintf _vsnprintf
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200308# define getenv(Name) nullptr
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400309# if _WIN32_WCE < 0x800
310# define setlocale(Category, Locale) "C"
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400311static int errno = 0; /* Use something better? */
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400312# endif
Konstantin Rittf3537b62015-01-25 09:50:51 +0400313# elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200314# define getenv(Name) nullptr
Konstantin Rittf3537b62015-01-25 09:50:51 +0400315# endif
Behdad Esfahbodce01ad72015-04-01 11:05:59 -0700316# if defined(_MSC_VER) && _MSC_VER < 1900
Konstantin Rittf3537b62015-01-25 09:50:51 +0400317# define snprintf _snprintf
Konstantin Rittf3537b62015-01-25 09:50:51 +0400318# endif
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400319#endif
320
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500321#if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400322/* atexit() is only safe to be called from shared libraries on certain
323 * platforms. Whitelist.
324 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
325# if defined(__linux) && defined(__GLIBC_PREREQ)
326# if __GLIBC_PREREQ(2,3)
327/* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
328# define HB_USE_ATEXIT 1
329# endif
330# elif defined(_MSC_VER) || defined(__MINGW32__)
331/* For MSVC:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430332 * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
333 * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400334 * mingw32 headers say atexit is safe to use in shared libraries.
335 */
336# define HB_USE_ATEXIT 1
Ebrahim Byagowi632713b2018-04-12 14:17:03 +0430337# elif defined(__ANDROID__)
338/* This is available since Android NKD r8 or r8b:
339 * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400340 */
341# define HB_USE_ATEXIT 1
Bruce Mitchenere8859fc2018-02-04 01:26:57 +0700342# elif defined(__APPLE__)
343/* For macOS and related platforms, the atexit man page indicates
344 * that it will be invoked when the library is unloaded, not only
345 * at application exit.
346 */
347# define HB_USE_ATEXIT 1
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400348# endif
349#endif
Behdad Esfahbodc14b24f2018-03-26 10:44:54 -0700350#ifdef HB_NO_ATEXIT
351# undef HB_USE_ATEXIT
352#endif
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500353#ifndef HB_USE_ATEXIT
354# define HB_USE_ATEXIT 0
355#endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500356
Behdad Esfahbod35a73832009-08-01 19:30:31 -0400357#define HB_STMT_START do
358#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -0500359
Behdad Esfahbod19e00912018-08-06 04:54:31 -0700360/* Static-assert as expression. */
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200361template <unsigned int cond> class hb_assert_constant_t;
362template <> class hb_assert_constant_t<1> {};
Behdad Esfahbod672ca3b2015-10-26 14:05:05 -0700363#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
Behdad Esfahboddcb70262011-04-21 16:34:22 -0400364
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400365/* Lets assert int types. Saves trouble down the road. */
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200366static_assert ((sizeof (int8_t) == 1), "");
367static_assert ((sizeof (uint8_t) == 1), "");
368static_assert ((sizeof (int16_t) == 2), "");
369static_assert ((sizeof (uint16_t) == 2), "");
370static_assert ((sizeof (int32_t) == 4), "");
371static_assert ((sizeof (uint32_t) == 4), "");
372static_assert ((sizeof (int64_t) == 8), "");
373static_assert ((sizeof (uint64_t) == 8), "");
374static_assert ((sizeof (hb_codepoint_t) == 4), "");
375static_assert ((sizeof (hb_position_t) == 4), "");
376static_assert ((sizeof (hb_mask_t) == 4), "");
377static_assert ((sizeof (hb_var_int_t) == 4), "");
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400378
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400379
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700380#if __cplusplus >= 201103L
381
Behdad Esfahbod67a22f32018-10-29 17:37:41 -0700382/* We only enable these with C++11 or later, since earlier language
383 * does not allow structs with constructors in unions, and we need
384 * those. */
385
386#define HB_NO_COPY_ASSIGN(TypeName) \
387 TypeName(const TypeName&); \
388 void operator=(const TypeName&)
Behdad Esfahbodfa333e32018-12-27 17:56:22 -0500389#define HB_NO_COPY_ASSIGN_TEMPLATE(TypeName, T) \
390 TypeName(const TypeName<T>&); \
391 void operator=(const TypeName<T>&)
Behdad Esfahbod67a22f32018-10-29 17:37:41 -0700392#define HB_NO_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \
393 TypeName(const TypeName<T1, T2>&); \
394 void operator=(const TypeName<T1, T2>&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700395#define HB_NO_CREATE_COPY_ASSIGN(TypeName) \
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330396 TypeName(); \
Behdad Esfahbodfb07d1a2018-06-01 17:32:07 -0700397 TypeName(const TypeName&); \
398 void operator=(const TypeName&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700399#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) \
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330400 TypeName(); \
Behdad Esfahboda256a922018-10-29 11:25:35 -0700401 TypeName(const TypeName<T>&); \
402 void operator=(const TypeName<T>&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700403#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330404 TypeName(); \
Behdad Esfahboda256a922018-10-29 11:25:35 -0700405 TypeName(const TypeName<T1, T2>&); \
Ebrahim Byagowif7a08cd2018-10-30 11:29:09 +0330406 void operator=(const TypeName<T1, T2>&)
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700407
408#else /* __cpluspplus >= 201103L */
409
Behdad Esfahbod87ff65a2018-12-17 17:19:42 -0500410#define HB_NO_COPY_ASSIGN(TypeName) static_assert (true, "")
Behdad Esfahbodd092fb22018-12-30 01:53:03 -0500411#define HB_NO_COPY_ASSIGN_TEMPLATE(TypeName, T) static_assert (true, "")
Behdad Esfahbodae9ad102018-11-03 21:41:50 -0400412#define HB_NO_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) static_assert (true, "")
413#define HB_NO_CREATE_COPY_ASSIGN(TypeName) static_assert (true, "")
414#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) static_assert (true, "")
415#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) static_assert (true, "")
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700416
417#endif /* __cpluspplus >= 201103L */
Behdad Esfahbod6c222762018-05-25 16:21:27 -0700418
Behdad Esfahbod44999f82018-07-11 17:00:59 +0200419
420/*
421 * Compiler-assisted vectorization parameters.
422 */
423
424/*
425 * Disable vectorization for now. To correctly use them, we should
426 * use posix_memalign() to allocate in hb_vector_t. Otherwise, can
427 * cause misaligned access.
428 *
429 * https://bugs.chromium.org/p/chromium/issues/detail?id=860184
430 */
431#if !defined(HB_VECTOR_SIZE)
432# define HB_VECTOR_SIZE 0
433#endif
434
435/* The `vector_size' attribute was introduced in gcc 3.1. */
436#if !defined(HB_VECTOR_SIZE)
437# if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
438# define HB_VECTOR_SIZE 128
439# else
440# define HB_VECTOR_SIZE 0
441# endif
442#endif
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200443static_assert (0 == (HB_VECTOR_SIZE & (HB_VECTOR_SIZE - 1)), "HB_VECTOR_SIZE is not power of 2.");
444static_assert (0 == (HB_VECTOR_SIZE % 64), "HB_VECTOR_SIZE is not multiple of 64.");
Behdad Esfahbod44999f82018-07-11 17:00:59 +0200445#if HB_VECTOR_SIZE
446typedef uint64_t hb_vector_size_impl_t __attribute__((vector_size (HB_VECTOR_SIZE / 8)));
447#else
448typedef uint64_t hb_vector_size_impl_t;
449#endif
450
451
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900452/* HB_NDEBUG disables some sanity checks that are very safe to disable and
453 * should be disabled in production systems. If NDEBUG is defined, enable
454 * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
455 * light-weight) to be enabled, then HB_DEBUG can be defined to disable
456 * the costlier checks. */
457#ifdef NDEBUG
Behdad Esfahbodbd021a62018-05-08 01:50:30 -0700458#define HB_NDEBUG 1
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900459#endif
460
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400461
Behdad Esfahbodbe336da2018-08-06 04:32:51 -0700462/* Flags */
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400463
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800464/* Enable bitwise ops on enums marked as flags_t */
Behdad Esfahbod69862082015-11-04 18:46:22 -0800465/* To my surprise, looks like the function resolver is happy to silently cast
466 * one enum to another... So this doesn't provide the type-checking that I
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800467 * originally had in mind... :(.
468 *
ebraminio7c6937e2017-11-20 14:49:22 -0500469 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800470 */
471#ifdef _MSC_VER
472# pragma warning(disable:4200)
473# pragma warning(disable:4800)
Chun-wei Fan167c3272015-11-09 17:17:56 +0800474#endif
Behdad Esfahbod1dc32ea2015-11-20 13:24:19 -0800475#define HB_MARK_AS_FLAG_T(T) \
476 extern "C++" { \
477 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
478 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
479 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
480 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
481 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
482 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
483 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
Behdad Esfahbodd25a2f12018-12-23 20:19:52 -0500484 } \
485 static_assert (true, "")
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800486
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400487/* Useful for set-operations on small enums.
488 * For example, for testing "x ∈ {x1, x2, x3}" use:
Behdad Esfahbod6058f982017-10-19 11:39:52 -0700489 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400490 */
Behdad Esfahbod3d2b98e2018-11-23 10:45:44 -0500491#define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x)))
492#define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0)
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400493#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod3d2b98e2018-11-23 10:45:44 -0500494#define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x)))
495#define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400496
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400497
Steven R. Loomisa13b0232015-12-11 10:21:27 -0800498/* Size signifying variable-sized array */
499#define VAR 1
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500500
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600501
Chun-wei Fan19256be2018-03-12 13:33:03 +0800502/* fallback for round() */
Chun-wei Fan19256be2018-03-12 13:33:03 +0800503static inline double
Behdad Esfahbod01dff1e2018-06-26 18:00:58 -0400504_hb_round (double x)
Chun-wei Fan19256be2018-03-12 13:33:03 +0800505{
506 if (x >= 0)
507 return floor (x + 0.5);
508 else
509 return ceil (x - 0.5);
510}
Behdad Esfahbod292c1002018-07-10 13:16:52 +0200511#if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND)
Behdad Esfahbod01dff1e2018-06-26 18:00:58 -0400512#define round(x) _hb_round(x)
Chun-wei Fan19256be2018-03-12 13:33:03 +0800513#endif
514
515
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200516/* fallback for posix_memalign() */
517static inline int
518_hb_memalign(void **memptr, size_t alignment, size_t size)
519{
Behdad Esfahbod491d93b2018-07-10 16:03:31 +0200520 if (unlikely (0 != (alignment & (alignment - 1)) ||
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200521 !alignment ||
522 0 != (alignment & (sizeof (void *) - 1))))
523 return EINVAL;
524
525 char *p = (char *) malloc (size + alignment - 1);
526 if (unlikely (!p))
527 return ENOMEM;
528
529 size_t off = (size_t) p & (alignment - 1);
530 if (off)
531 p += alignment - off;
532
533 *memptr = (void *) p;
534
535 return 0;
536}
537#if !defined(posix_memalign) && !defined(HAVE_POSIX_MEMALIGN)
538#define posix_memalign _hb_memalign
539#endif
540
541
Behdad Esfahbodbdb53ca2018-10-11 20:20:00 -0400542/*
543 * For lack of a better place, put Zawgyi script hack here.
544 * https://github.com/harfbuzz/harfbuzz/issues/1162
545 */
546
547#define HB_SCRIPT_MYANMAR_ZAWGYI ((hb_script_t) HB_TAG ('Q','a','a','g'))
548
549
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700550/* Some really basic things everyone wants. */
551template <typename T> struct hb_remove_const { typedef T value; };
552template <typename T> struct hb_remove_const<const T> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500553#define hb_remove_const(T) hb_remove_const<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700554template <typename T> struct hb_remove_reference { typedef T value; };
555template <typename T> struct hb_remove_reference<T &> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500556#define hb_remove_reference(T) hb_remove_reference<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700557template <typename T> struct hb_remove_pointer { typedef T value; };
558template <typename T> struct hb_remove_pointer<T *> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500559#define hb_remove_pointer(T) hb_remove_pointer<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700560
561
Behdad Esfahbod865deeb2018-12-21 17:35:58 -0500562/* Headers we include for everyone. Keep topologically sorted by dependency.
563 * They express dependency amongst themselves, but no other file should include
564 * them directly.*/
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700565#include "hb-atomic.hh"
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700566#include "hb-mutex.hh"
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700567#include "hb-null.hh"
Behdad Esfahbod865deeb2018-12-21 17:35:58 -0500568#include "hb-dsalgs.hh" // Requires: hb-null
569#include "hb-iter.hh" // Requires: hb-null
570#include "hb-debug.hh" // Requires: hb-atomic hb-dsalgs
571#include "hb-array.hh" // Requires: hb-dsalgs hb-iter hb-null
572#include "hb-vector.hh" // Requires: hb-array hb-null
573#include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200574
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700575#endif /* HB_HH */