blob: d20f3b873ba68c55b39b61fcac7904311ce523ef [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 Esfahbod1077e402019-01-17 18:36:10 -050033/* Rules:
34 * - We set all pragmas under GCC, even clang-specific ones. Clang
35 * still reads them.
36 * - Within each category, keep sorted.
37 */
38/* Setup */
39#pragma GCC diagnostic ignored "-Wpragmas" // Must be first
Behdad Esfahboda9946b72019-01-17 15:22:28 -050040#pragma GCC diagnostic warning "-Wall"
41#pragma GCC diagnostic warning "-Wextra"
Behdad Esfahbod1077e402019-01-17 18:36:10 -050042//#pragma GCC diagnostic warning "-Weverything"
43/* Error */
44#pragma GCC diagnostic error "-Wcast-align"
45#pragma GCC diagnostic error "-Wunused"
46/* Warning */
47/* Ignored */
48#pragma GCC diagnostic ignored "-Wconversion" // TODO fix
Behdad Esfahboda9946b72019-01-17 15:22:28 -050049#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
Behdad Esfahbod1077e402019-01-17 18:36:10 -050050#pragma GCC diagnostic ignored "-Wpacked" // Erratic impl in clang
51#pragma GCC diagnostic ignored "-Wshadow" // TODO fix
52#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Behdad Esfahbod14a560a2019-01-17 17:42:44 -050053#pragma GCC diagnostic ignored "-Wtype-limits"
Behdad Esfahboda9946b72019-01-17 15:22:28 -050054#pragma GCC diagnostic ignored "-Wunused-parameter" // TODO Fix
Behdad Esfahbod868b0c02019-01-17 15:08:36 -050055#endif
56
Behdad Esfahbod5ddd9cc2011-09-16 16:40:44 -040057#ifdef HAVE_CONFIG_H
Behdad Esfahboddf660282009-08-01 20:46:02 -040058#include "config.h"
59#endif
Behdad Esfahbod12360f72008-01-23 15:50:38 -050060
Behdad Esfahbod49334f92018-12-17 18:27:36 -050061/*
62 * Following added based on what AC_USE_SYSTEM_EXTENSIONS adds to
63 * config.h.in. Copied here for the convenience of those embedding
64 * HarfBuzz and not using our build system.
65 */
66/* Enable extensions on AIX 3, Interix. */
67#ifndef _ALL_SOURCE
68# define _ALL_SOURCE 1
69#endif
70/* Enable GNU extensions on systems that have them. */
71#ifndef _GNU_SOURCE
72# define _GNU_SOURCE 1
73#endif
74/* Enable threading extensions on Solaris. */
75#ifndef _POSIX_PTHREAD_SEMANTICS
76# define _POSIX_PTHREAD_SEMANTICS 1
77#endif
78/* Enable extensions on HP NonStop. */
79#ifndef _TANDEM_SOURCE
80# define _TANDEM_SOURCE 1
81#endif
82/* Enable general extensions on Solaris. */
83#ifndef __EXTENSIONS__
84# define __EXTENSIONS__ 1
85#endif
86
Chun-wei Fan8e4ad1d2018-07-25 18:12:34 +080087#if defined (_MSC_VER) && defined (HB_DLL_EXPORT)
88#define HB_EXTERN __declspec (dllexport) extern
89#endif
90
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040091#include "hb.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040092#define HB_H_IN
Behdad Esfahbod3e32cd92012-04-23 13:20:52 -040093#include "hb-ot.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040094#define HB_OT_H_IN
Behdad Esfahbodb2061332018-11-23 14:05:11 -050095#include "hb-aat.h"
96#define HB_AAT_H_IN
Behdad Esfahbodb28815c2009-08-04 22:35:36 -040097
Ebrahim Byagowia8726cb2018-11-17 14:23:11 +033098#include "hb-aat.h"
99
Behdad Esfahbod7cdd6ab2018-01-10 04:33:08 +0100100#include <math.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400101#include <stdlib.h>
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400102#include <stddef.h>
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400103#include <string.h>
104#include <assert.h>
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400105#include <errno.h>
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400106#include <stdio.h>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400107#include <stdarg.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400108
Tor Andersson8f4c1232018-04-13 23:01:54 +0430109#if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
Arkady Shapkin6c103282018-02-15 23:17:46 +0300110#include <intrin.h>
111#endif
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400112
Behdad Esfahbod2a749682017-10-26 19:48:33 -0600113#define HB_PASTE1(a,b) a##b
114#define HB_PASTE(a,b) HB_PASTE1(a,b)
115
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200116
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100117/* Compile-time custom allocator support. */
118
119#if defined(hb_malloc_impl) \
120 && defined(hb_calloc_impl) \
121 && defined(hb_realloc_impl) \
122 && defined(hb_free_impl)
Behdad Esfahbodcc6ea302015-10-12 17:21:52 -0400123extern "C" void* hb_malloc_impl(size_t size);
124extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
125extern "C" void* hb_realloc_impl(void *ptr, size_t size);
126extern "C" void hb_free_impl(void *ptr);
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100127#define malloc hb_malloc_impl
128#define calloc hb_calloc_impl
129#define realloc hb_realloc_impl
130#define free hb_free_impl
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200131
Behdad Esfahbod763f8782018-07-10 13:47:41 +0200132#if defined(hb_memalign_impl)
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200133extern "C" int hb_memalign_impl(void **memptr, size_t alignment, size_t size);
134#define posix_memalign hb_memalign_impl
135#else
136#undef HAVE_POSIX_MEMALIGN
137#endif
138
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100139#endif
140
141
Behdad Esfahbod2aae6172018-07-11 17:27:23 +0200142/*
143 * Compiler attributes
Behdad Esfahbod37be7742018-08-06 04:51:38 -0700144 */
Behdad Esfahbod292c1002018-07-10 13:16:52 +0200145
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200146#if __cplusplus < 201103L
147
Behdad Esfahbod62e312e2017-10-27 09:29:20 -0600148#ifndef nullptr
149#define nullptr NULL
150#endif
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200151
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200152#ifndef constexpr
153#define constexpr const
154#endif
155
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200156#ifndef static_assert
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200157#define static_assert(e, msg) \
Behdad Esfahbod6f08b122017-10-26 18:23:03 -0600158 HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200159#endif // static_assert
160
Behdad Esfahbodf83e9922018-05-24 11:46:57 -0700161#ifdef __GNUC__
162#if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
163#define thread_local __thread
164#endif
165#else
166#define thread_local
167#endif
168
Behdad Esfahbod2aae6172018-07-11 17:27:23 +0200169template <typename T>
170struct _hb_alignof
171{
172 struct s
173 {
174 char c;
175 T t;
176 };
177 static constexpr size_t value = offsetof (s, t);
178};
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200179#ifndef alignof
Behdad Esfahbod27c53172018-07-10 13:03:42 +0200180#define alignof(x) (_hb_alignof<x>::value)
Behdad Esfahbod37be7742018-08-06 04:51:38 -0700181#endif
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200182
Behdad Esfahbodf9a3eab2018-08-12 12:21:56 -0700183/* https://github.com/harfbuzz/harfbuzz/issues/1127 */
184#ifndef explicit_operator
Behdad Esfahbod01d06e32018-12-16 14:27:43 -0500185#define explicit_operator operator
Behdad Esfahbodf9a3eab2018-08-12 12:21:56 -0700186#endif
187
188#else /* __cplusplus >= 201103L */
189
190/* https://github.com/harfbuzz/harfbuzz/issues/1127 */
191#ifndef explicit_operator
Behdad Esfahbod01d06e32018-12-16 14:27:43 -0500192#define explicit_operator explicit operator
Behdad Esfahbodf9a3eab2018-08-12 12:21:56 -0700193#endif
194
195#endif /* __cplusplus < 201103L */
196
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200197
Ebrahim Byagowi76c48732017-06-02 21:53:10 +0430198#if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
199#define likely(expr) (__builtin_expect (!!(expr), 1))
200#define unlikely(expr) (__builtin_expect (!!(expr), 0))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400201#else
202#define likely(expr) (expr)
203#define unlikely(expr) (expr)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400204#endif
205
Steve Lhomme0b8f3ab2016-07-11 21:57:26 +0200206#if !defined(__GNUC__) && !defined(__clang__)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400207#undef __attribute__
208#define __attribute__(x)
209#endif
Behdad Esfahboda794ebf2009-08-06 12:32:35 -0400210
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400211#if __GNUC__ >= 3
212#define HB_PURE_FUNC __attribute__((pure))
213#define HB_CONST_FUNC __attribute__((const))
214#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
215#else
216#define HB_PURE_FUNC
217#define HB_CONST_FUNC
218#define HB_PRINTF_FUNC(format_idx, arg_idx)
219#endif
220#if __GNUC__ >= 4
221#define HB_UNUSED __attribute__((unused))
Behdad Esfahbodbe59f3c2017-12-05 09:01:28 -0800222#elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
223#define HB_UNUSED __pragma(warning(suppress: 4100 4101))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400224#else
225#define HB_UNUSED
226#endif
227
228#ifndef HB_INTERNAL
Behdad Esfahbod90d1c742018-02-11 19:08:59 -0800229# if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400230# define HB_INTERNAL __attribute__((__visibility__("hidden")))
Behdad Esfahbod94759d42018-07-23 15:01:39 -0700231# elif defined(__MINGW32__)
Chun-wei Fan8e4ad1d2018-07-25 18:12:34 +0800232 /* We use -export-symbols on mingw32, since it does not support visibility attributes. */
233# define HB_INTERNAL
234# elif defined (_MSC_VER) && defined (HB_DLL_EXPORT)
235 /* We do not try to export internal symbols on Visual Studio */
Behdad Esfahbod94759d42018-07-23 15:01:39 -0700236# define HB_INTERNAL
237#else
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400238# define HB_INTERNAL
Behdad Esfahbodb87cbe22018-02-07 14:11:26 -0500239# define HB_NO_VISIBILITY 1
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400240# endif
241#endif
242
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400243#if __GNUC__ >= 3
244#define HB_FUNC __PRETTY_FUNCTION__
245#elif defined(_MSC_VER)
246#define HB_FUNC __FUNCSIG__
247#else
248#define HB_FUNC __func__
249#endif
250
prrace498e4372018-06-09 16:04:28 -0700251#if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
Behdad Esfahbod8cef3a62018-02-09 16:04:23 -0600252/* https://github.com/harfbuzz/harfbuzz/issues/630 */
253#define __restrict
254#endif
255
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200256/*
257 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
258 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
259 * cases that fall through without a break or return statement. HB_FALLTHROUGH
260 * is only needed on cases that have code:
261 *
262 * switch (foo) {
263 * case 1: // These cases have no code. No fallthrough annotations are needed.
264 * case 2:
265 * case 3:
266 * foo = 4; // This case has code, so a fallthrough annotation is needed:
267 * HB_FALLTHROUGH;
268 * default:
269 * return foo;
270 * }
271 */
272#if defined(__clang__) && __cplusplus >= 201103L
273 /* clang's fallthrough annotations are only available starting in C++11. */
274# define HB_FALLTHROUGH [[clang::fallthrough]]
Behdad Esfahbodf41b9212018-02-05 19:51:09 -0500275#elif __GNUC__ >= 7
276 /* GNU fallthrough attribute is available from GCC7 */
277# define HB_FALLTHROUGH __attribute__((fallthrough))
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200278#elif defined(_MSC_VER)
279 /*
280 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
281 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
282 */
283# include <sal.h>
284# define HB_FALLTHROUGH __fallthrough
285#else
286# define HB_FALLTHROUGH /* FALLTHROUGH */
287#endif
288
Behdad Esfahbod62376a72018-10-14 15:20:50 -0700289#if defined(__clang__)
290/* Disable certain sanitizer errors. */
291/* https://github.com/harfbuzz/harfbuzz/issues/1247 */
292#define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow")))
293#else
294#define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW
295#endif
296
297
Ken Browneee5b5e2018-11-12 21:05:39 -0500298#ifdef _WIN32
Behdad Esfahboddb308282014-07-19 16:32:04 -0400299 /* We need Windows Vista for both Uniscribe backend and for
300 * MemoryBarrier. We don't support compiling on Windows XP,
301 * though we run on it fine. */
302# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
303# undef _WIN32_WINNT
304# endif
305# ifndef _WIN32_WINNT
Matt Oliver24dd6c12018-09-23 18:08:30 +1000306# if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
307# define _WIN32_WINNT 0x0600
308# endif
Behdad Esfahboddb308282014-07-19 16:32:04 -0400309# endif
Behdad Esfahbod270971a2014-08-15 14:28:04 -0400310# ifndef WIN32_LEAN_AND_MEAN
311# define WIN32_LEAN_AND_MEAN 1
312# endif
313# ifndef STRICT
314# define STRICT 1
315# endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500316
Konstantin Rittf3537b62015-01-25 09:50:51 +0400317# if defined(_WIN32_WCE)
318 /* Some things not defined on Windows CE. */
Konstantin Ritt50690622016-04-26 12:02:26 +0400319# define vsnprintf _vsnprintf
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200320# define getenv(Name) nullptr
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400321# if _WIN32_WCE < 0x800
322# define setlocale(Category, Locale) "C"
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400323static int errno = 0; /* Use something better? */
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400324# endif
Konstantin Rittf3537b62015-01-25 09:50:51 +0400325# elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200326# define getenv(Name) nullptr
Konstantin Rittf3537b62015-01-25 09:50:51 +0400327# endif
Behdad Esfahbodce01ad72015-04-01 11:05:59 -0700328# if defined(_MSC_VER) && _MSC_VER < 1900
Konstantin Rittf3537b62015-01-25 09:50:51 +0400329# define snprintf _snprintf
Konstantin Rittf3537b62015-01-25 09:50:51 +0400330# endif
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400331#endif
332
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500333#if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400334/* atexit() is only safe to be called from shared libraries on certain
335 * platforms. Whitelist.
336 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
337# if defined(__linux) && defined(__GLIBC_PREREQ)
338# if __GLIBC_PREREQ(2,3)
339/* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
340# define HB_USE_ATEXIT 1
341# endif
342# elif defined(_MSC_VER) || defined(__MINGW32__)
343/* For MSVC:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430344 * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
345 * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400346 * mingw32 headers say atexit is safe to use in shared libraries.
347 */
348# define HB_USE_ATEXIT 1
Ebrahim Byagowi632713b2018-04-12 14:17:03 +0430349# elif defined(__ANDROID__)
350/* This is available since Android NKD r8 or r8b:
351 * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400352 */
353# define HB_USE_ATEXIT 1
Bruce Mitchenere8859fc2018-02-04 01:26:57 +0700354# elif defined(__APPLE__)
355/* For macOS and related platforms, the atexit man page indicates
356 * that it will be invoked when the library is unloaded, not only
357 * at application exit.
358 */
359# define HB_USE_ATEXIT 1
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400360# endif
361#endif
Behdad Esfahbodc14b24f2018-03-26 10:44:54 -0700362#ifdef HB_NO_ATEXIT
363# undef HB_USE_ATEXIT
364#endif
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500365#ifndef HB_USE_ATEXIT
366# define HB_USE_ATEXIT 0
367#endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500368
Behdad Esfahbod35a73832009-08-01 19:30:31 -0400369#define HB_STMT_START do
370#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -0500371
Behdad Esfahbod19e00912018-08-06 04:54:31 -0700372/* Static-assert as expression. */
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200373template <unsigned int cond> class hb_assert_constant_t;
374template <> class hb_assert_constant_t<1> {};
Behdad Esfahbod672ca3b2015-10-26 14:05:05 -0700375#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
Behdad Esfahboddcb70262011-04-21 16:34:22 -0400376
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400377/* Lets assert int types. Saves trouble down the road. */
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200378static_assert ((sizeof (int8_t) == 1), "");
379static_assert ((sizeof (uint8_t) == 1), "");
380static_assert ((sizeof (int16_t) == 2), "");
381static_assert ((sizeof (uint16_t) == 2), "");
382static_assert ((sizeof (int32_t) == 4), "");
383static_assert ((sizeof (uint32_t) == 4), "");
384static_assert ((sizeof (int64_t) == 8), "");
385static_assert ((sizeof (uint64_t) == 8), "");
386static_assert ((sizeof (hb_codepoint_t) == 4), "");
387static_assert ((sizeof (hb_position_t) == 4), "");
388static_assert ((sizeof (hb_mask_t) == 4), "");
389static_assert ((sizeof (hb_var_int_t) == 4), "");
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400390
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400391
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700392#if __cplusplus >= 201103L
393
Behdad Esfahbod67a22f32018-10-29 17:37:41 -0700394/* We only enable these with C++11 or later, since earlier language
395 * does not allow structs with constructors in unions, and we need
396 * those. */
397
398#define HB_NO_COPY_ASSIGN(TypeName) \
399 TypeName(const TypeName&); \
400 void operator=(const TypeName&)
Behdad Esfahbodfa333e32018-12-27 17:56:22 -0500401#define HB_NO_COPY_ASSIGN_TEMPLATE(TypeName, T) \
402 TypeName(const TypeName<T>&); \
403 void operator=(const TypeName<T>&)
Behdad Esfahbod67a22f32018-10-29 17:37:41 -0700404#define HB_NO_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \
405 TypeName(const TypeName<T1, T2>&); \
406 void operator=(const TypeName<T1, T2>&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700407#define HB_NO_CREATE_COPY_ASSIGN(TypeName) \
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330408 TypeName(); \
Behdad Esfahbodfb07d1a2018-06-01 17:32:07 -0700409 TypeName(const TypeName&); \
410 void operator=(const TypeName&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700411#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) \
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330412 TypeName(); \
Behdad Esfahboda256a922018-10-29 11:25:35 -0700413 TypeName(const TypeName<T>&); \
414 void operator=(const TypeName<T>&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700415#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330416 TypeName(); \
Behdad Esfahboda256a922018-10-29 11:25:35 -0700417 TypeName(const TypeName<T1, T2>&); \
Ebrahim Byagowif7a08cd2018-10-30 11:29:09 +0330418 void operator=(const TypeName<T1, T2>&)
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700419
420#else /* __cpluspplus >= 201103L */
421
Behdad Esfahbod87ff65a2018-12-17 17:19:42 -0500422#define HB_NO_COPY_ASSIGN(TypeName) static_assert (true, "")
Behdad Esfahbodd092fb22018-12-30 01:53:03 -0500423#define HB_NO_COPY_ASSIGN_TEMPLATE(TypeName, T) static_assert (true, "")
Behdad Esfahbodae9ad102018-11-03 21:41:50 -0400424#define HB_NO_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) static_assert (true, "")
425#define HB_NO_CREATE_COPY_ASSIGN(TypeName) static_assert (true, "")
426#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) static_assert (true, "")
427#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) static_assert (true, "")
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700428
429#endif /* __cpluspplus >= 201103L */
Behdad Esfahbod6c222762018-05-25 16:21:27 -0700430
Behdad Esfahbod44999f82018-07-11 17:00:59 +0200431
432/*
433 * Compiler-assisted vectorization parameters.
434 */
435
436/*
437 * Disable vectorization for now. To correctly use them, we should
438 * use posix_memalign() to allocate in hb_vector_t. Otherwise, can
439 * cause misaligned access.
440 *
441 * https://bugs.chromium.org/p/chromium/issues/detail?id=860184
442 */
443#if !defined(HB_VECTOR_SIZE)
444# define HB_VECTOR_SIZE 0
445#endif
446
447/* The `vector_size' attribute was introduced in gcc 3.1. */
448#if !defined(HB_VECTOR_SIZE)
449# if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
450# define HB_VECTOR_SIZE 128
451# else
452# define HB_VECTOR_SIZE 0
453# endif
454#endif
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200455static_assert (0 == (HB_VECTOR_SIZE & (HB_VECTOR_SIZE - 1)), "HB_VECTOR_SIZE is not power of 2.");
456static_assert (0 == (HB_VECTOR_SIZE % 64), "HB_VECTOR_SIZE is not multiple of 64.");
Behdad Esfahbod44999f82018-07-11 17:00:59 +0200457#if HB_VECTOR_SIZE
458typedef uint64_t hb_vector_size_impl_t __attribute__((vector_size (HB_VECTOR_SIZE / 8)));
459#else
460typedef uint64_t hb_vector_size_impl_t;
461#endif
462
463
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900464/* HB_NDEBUG disables some sanity checks that are very safe to disable and
465 * should be disabled in production systems. If NDEBUG is defined, enable
466 * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
467 * light-weight) to be enabled, then HB_DEBUG can be defined to disable
468 * the costlier checks. */
469#ifdef NDEBUG
Behdad Esfahbodbd021a62018-05-08 01:50:30 -0700470#define HB_NDEBUG 1
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900471#endif
472
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400473
Behdad Esfahbodbe336da2018-08-06 04:32:51 -0700474/* Flags */
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400475
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800476/* Enable bitwise ops on enums marked as flags_t */
Behdad Esfahbod69862082015-11-04 18:46:22 -0800477/* To my surprise, looks like the function resolver is happy to silently cast
478 * one enum to another... So this doesn't provide the type-checking that I
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800479 * originally had in mind... :(.
480 *
ebraminio7c6937e2017-11-20 14:49:22 -0500481 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800482 */
483#ifdef _MSC_VER
484# pragma warning(disable:4200)
485# pragma warning(disable:4800)
Chun-wei Fan167c3272015-11-09 17:17:56 +0800486#endif
Behdad Esfahbod1dc32ea2015-11-20 13:24:19 -0800487#define HB_MARK_AS_FLAG_T(T) \
488 extern "C++" { \
489 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
490 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
491 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
492 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
493 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
494 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
495 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
Behdad Esfahbodd25a2f12018-12-23 20:19:52 -0500496 } \
497 static_assert (true, "")
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800498
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400499/* Useful for set-operations on small enums.
500 * For example, for testing "x ∈ {x1, x2, x3}" use:
Behdad Esfahbod6058f982017-10-19 11:39:52 -0700501 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400502 */
Behdad Esfahbod3d2b98e2018-11-23 10:45:44 -0500503#define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x)))
504#define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0)
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400505#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod3d2b98e2018-11-23 10:45:44 -0500506#define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x)))
507#define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400508
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400509
Steven R. Loomisa13b0232015-12-11 10:21:27 -0800510/* Size signifying variable-sized array */
511#define VAR 1
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500512
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600513
Chun-wei Fan19256be2018-03-12 13:33:03 +0800514/* fallback for round() */
Chun-wei Fan19256be2018-03-12 13:33:03 +0800515static inline double
Behdad Esfahbod01dff1e2018-06-26 18:00:58 -0400516_hb_round (double x)
Chun-wei Fan19256be2018-03-12 13:33:03 +0800517{
518 if (x >= 0)
519 return floor (x + 0.5);
520 else
521 return ceil (x - 0.5);
522}
Behdad Esfahbod292c1002018-07-10 13:16:52 +0200523#if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND)
Behdad Esfahbod01dff1e2018-06-26 18:00:58 -0400524#define round(x) _hb_round(x)
Chun-wei Fan19256be2018-03-12 13:33:03 +0800525#endif
526
527
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200528/* fallback for posix_memalign() */
529static inline int
530_hb_memalign(void **memptr, size_t alignment, size_t size)
531{
Behdad Esfahbod491d93b2018-07-10 16:03:31 +0200532 if (unlikely (0 != (alignment & (alignment - 1)) ||
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200533 !alignment ||
534 0 != (alignment & (sizeof (void *) - 1))))
535 return EINVAL;
536
537 char *p = (char *) malloc (size + alignment - 1);
538 if (unlikely (!p))
539 return ENOMEM;
540
541 size_t off = (size_t) p & (alignment - 1);
542 if (off)
543 p += alignment - off;
544
545 *memptr = (void *) p;
546
547 return 0;
548}
549#if !defined(posix_memalign) && !defined(HAVE_POSIX_MEMALIGN)
550#define posix_memalign _hb_memalign
551#endif
552
553
Behdad Esfahbodbdb53ca2018-10-11 20:20:00 -0400554/*
555 * For lack of a better place, put Zawgyi script hack here.
556 * https://github.com/harfbuzz/harfbuzz/issues/1162
557 */
558
559#define HB_SCRIPT_MYANMAR_ZAWGYI ((hb_script_t) HB_TAG ('Q','a','a','g'))
560
561
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700562/* Some really basic things everyone wants. */
563template <typename T> struct hb_remove_const { typedef T value; };
564template <typename T> struct hb_remove_const<const T> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500565#define hb_remove_const(T) hb_remove_const<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700566template <typename T> struct hb_remove_reference { typedef T value; };
567template <typename T> struct hb_remove_reference<T &> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500568#define hb_remove_reference(T) hb_remove_reference<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700569template <typename T> struct hb_remove_pointer { typedef T value; };
570template <typename T> struct hb_remove_pointer<T *> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500571#define hb_remove_pointer(T) hb_remove_pointer<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700572
573
Behdad Esfahbod865deeb2018-12-21 17:35:58 -0500574/* Headers we include for everyone. Keep topologically sorted by dependency.
575 * They express dependency amongst themselves, but no other file should include
576 * them directly.*/
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700577#include "hb-atomic.hh"
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700578#include "hb-mutex.hh"
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700579#include "hb-null.hh"
Behdad Esfahbod865deeb2018-12-21 17:35:58 -0500580#include "hb-dsalgs.hh" // Requires: hb-null
581#include "hb-iter.hh" // Requires: hb-null
582#include "hb-debug.hh" // Requires: hb-atomic hb-dsalgs
583#include "hb-array.hh" // Requires: hb-dsalgs hb-iter hb-null
584#include "hb-vector.hh" // Requires: hb-array hb-null
585#include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200586
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700587#endif /* HB_HH */