blob: c771d1fb9258d2d2cad2e69decd06a1c595042d0 [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
Ebrahim Byagowi5dbbd0f2018-01-11 12:33:22 +033032#define _GNU_SOURCE 1
33
Behdad Esfahbod5ddd9cc2011-09-16 16:40:44 -040034#ifdef HAVE_CONFIG_H
Behdad Esfahboddf660282009-08-01 20:46:02 -040035#include "config.h"
36#endif
Behdad Esfahbod12360f72008-01-23 15:50:38 -050037
Behdad Esfahbod3f087502018-09-30 18:23:34 +020038#ifndef _POSIX_C_SOURCE
39#define _POSIX_C_SOURCE 200809L
40#endif
41
Chun-wei Fan8e4ad1d2018-07-25 18:12:34 +080042#if defined (_MSC_VER) && defined (HB_DLL_EXPORT)
43#define HB_EXTERN __declspec (dllexport) extern
44#endif
45
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040046#include "hb.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040047#define HB_H_IN
Behdad Esfahbod3e32cd92012-04-23 13:20:52 -040048#include "hb-ot.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -040049#define HB_OT_H_IN
Behdad Esfahbodb28815c2009-08-04 22:35:36 -040050
Behdad Esfahbod7cdd6ab2018-01-10 04:33:08 +010051#include <math.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040052#include <stdlib.h>
Behdad Esfahbodb65c0602011-07-28 16:48:43 -040053#include <stddef.h>
Behdad Esfahbodf0954d12009-07-30 15:33:57 -040054#include <string.h>
55#include <assert.h>
Behdad Esfahbod7acb3892009-08-05 15:20:34 -040056#include <errno.h>
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040057#include <stdio.h>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -040058#include <stdarg.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -040059
Tor Andersson8f4c1232018-04-13 23:01:54 +043060#if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
Arkady Shapkin6c103282018-02-15 23:17:46 +030061#include <intrin.h>
62#endif
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040063
Behdad Esfahbod2a749682017-10-26 19:48:33 -060064#define HB_PASTE1(a,b) a##b
65#define HB_PASTE(a,b) HB_PASTE1(a,b)
66
Behdad Esfahbod83ea2772018-07-10 13:17:27 +020067
Behdad Esfahbod52b41852015-10-03 13:20:55 +010068/* Compile-time custom allocator support. */
69
70#if defined(hb_malloc_impl) \
71 && defined(hb_calloc_impl) \
72 && defined(hb_realloc_impl) \
73 && defined(hb_free_impl)
Behdad Esfahbodcc6ea302015-10-12 17:21:52 -040074extern "C" void* hb_malloc_impl(size_t size);
75extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
76extern "C" void* hb_realloc_impl(void *ptr, size_t size);
77extern "C" void hb_free_impl(void *ptr);
Behdad Esfahbod52b41852015-10-03 13:20:55 +010078#define malloc hb_malloc_impl
79#define calloc hb_calloc_impl
80#define realloc hb_realloc_impl
81#define free hb_free_impl
Behdad Esfahbod83ea2772018-07-10 13:17:27 +020082
Behdad Esfahbod763f8782018-07-10 13:47:41 +020083#if defined(hb_memalign_impl)
Behdad Esfahbod83ea2772018-07-10 13:17:27 +020084extern "C" int hb_memalign_impl(void **memptr, size_t alignment, size_t size);
85#define posix_memalign hb_memalign_impl
86#else
87#undef HAVE_POSIX_MEMALIGN
88#endif
89
Behdad Esfahbod52b41852015-10-03 13:20:55 +010090#endif
91
92
Behdad Esfahbod2aae6172018-07-11 17:27:23 +020093/*
94 * Compiler attributes
Behdad Esfahbod37be7742018-08-06 04:51:38 -070095 */
Behdad Esfahbod292c1002018-07-10 13:16:52 +020096
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +020097#if __cplusplus < 201103L
98
Behdad Esfahbod62e312e2017-10-27 09:29:20 -060099#ifndef nullptr
100#define nullptr NULL
101#endif
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200102
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200103#ifndef constexpr
104#define constexpr const
105#endif
106
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200107#ifndef static_assert
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200108#define static_assert(e, msg) \
Behdad Esfahbod6f08b122017-10-26 18:23:03 -0600109 HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1]
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200110#endif // static_assert
111
Behdad Esfahbodf83e9922018-05-24 11:46:57 -0700112#ifdef __GNUC__
113#if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8))
114#define thread_local __thread
115#endif
116#else
117#define thread_local
118#endif
119
Behdad Esfahbod2aae6172018-07-11 17:27:23 +0200120template <typename T>
121struct _hb_alignof
122{
123 struct s
124 {
125 char c;
126 T t;
127 };
128 static constexpr size_t value = offsetof (s, t);
129};
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200130#ifndef alignof
Behdad Esfahbod27c53172018-07-10 13:03:42 +0200131#define alignof(x) (_hb_alignof<x>::value)
Behdad Esfahbod37be7742018-08-06 04:51:38 -0700132#endif
Behdad Esfahbodbca83612018-07-10 12:58:13 +0200133
Behdad Esfahbodf9a3eab2018-08-12 12:21:56 -0700134/* https://github.com/harfbuzz/harfbuzz/issues/1127 */
135#ifndef explicit_operator
136#define explicit_operator
137#endif
138
139#else /* __cplusplus >= 201103L */
140
141/* https://github.com/harfbuzz/harfbuzz/issues/1127 */
142#ifndef explicit_operator
143#define explicit_operator explicit
144#endif
145
146#endif /* __cplusplus < 201103L */
147
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200148
Ebrahim Byagowi76c48732017-06-02 21:53:10 +0430149#if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
150#define likely(expr) (__builtin_expect (!!(expr), 1))
151#define unlikely(expr) (__builtin_expect (!!(expr), 0))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400152#else
153#define likely(expr) (expr)
154#define unlikely(expr) (expr)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400155#endif
156
Steve Lhomme0b8f3ab2016-07-11 21:57:26 +0200157#if !defined(__GNUC__) && !defined(__clang__)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400158#undef __attribute__
159#define __attribute__(x)
160#endif
Behdad Esfahboda794ebf2009-08-06 12:32:35 -0400161
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400162#if __GNUC__ >= 3
163#define HB_PURE_FUNC __attribute__((pure))
164#define HB_CONST_FUNC __attribute__((const))
165#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
166#else
167#define HB_PURE_FUNC
168#define HB_CONST_FUNC
169#define HB_PRINTF_FUNC(format_idx, arg_idx)
170#endif
171#if __GNUC__ >= 4
172#define HB_UNUSED __attribute__((unused))
Behdad Esfahbodbe59f3c2017-12-05 09:01:28 -0800173#elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
174#define HB_UNUSED __pragma(warning(suppress: 4100 4101))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400175#else
176#define HB_UNUSED
177#endif
178
179#ifndef HB_INTERNAL
Behdad Esfahbod90d1c742018-02-11 19:08:59 -0800180# if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400181# define HB_INTERNAL __attribute__((__visibility__("hidden")))
Behdad Esfahbod94759d42018-07-23 15:01:39 -0700182# elif defined(__MINGW32__)
Chun-wei Fan8e4ad1d2018-07-25 18:12:34 +0800183 /* We use -export-symbols on mingw32, since it does not support visibility attributes. */
184# define HB_INTERNAL
185# elif defined (_MSC_VER) && defined (HB_DLL_EXPORT)
186 /* We do not try to export internal symbols on Visual Studio */
Behdad Esfahbod94759d42018-07-23 15:01:39 -0700187# define HB_INTERNAL
188#else
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400189# define HB_INTERNAL
Behdad Esfahbodb87cbe22018-02-07 14:11:26 -0500190# define HB_NO_VISIBILITY 1
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400191# endif
192#endif
193
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400194#if __GNUC__ >= 3
195#define HB_FUNC __PRETTY_FUNCTION__
196#elif defined(_MSC_VER)
197#define HB_FUNC __FUNCSIG__
198#else
199#define HB_FUNC __func__
200#endif
201
prrace498e4372018-06-09 16:04:28 -0700202#if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
Behdad Esfahbod8cef3a62018-02-09 16:04:23 -0600203/* https://github.com/harfbuzz/harfbuzz/issues/630 */
204#define __restrict
205#endif
206
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200207/*
208 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
209 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
210 * cases that fall through without a break or return statement. HB_FALLTHROUGH
211 * is only needed on cases that have code:
212 *
213 * switch (foo) {
214 * case 1: // These cases have no code. No fallthrough annotations are needed.
215 * case 2:
216 * case 3:
217 * foo = 4; // This case has code, so a fallthrough annotation is needed:
218 * HB_FALLTHROUGH;
219 * default:
220 * return foo;
221 * }
222 */
223#if defined(__clang__) && __cplusplus >= 201103L
224 /* clang's fallthrough annotations are only available starting in C++11. */
225# define HB_FALLTHROUGH [[clang::fallthrough]]
Behdad Esfahbodf41b9212018-02-05 19:51:09 -0500226#elif __GNUC__ >= 7
227 /* GNU fallthrough attribute is available from GCC7 */
228# define HB_FALLTHROUGH __attribute__((fallthrough))
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200229#elif defined(_MSC_VER)
230 /*
231 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
232 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
233 */
234# include <sal.h>
235# define HB_FALLTHROUGH __fallthrough
236#else
237# define HB_FALLTHROUGH /* FALLTHROUGH */
238#endif
239
Behdad Esfahbod62376a72018-10-14 15:20:50 -0700240#if defined(__clang__)
241/* Disable certain sanitizer errors. */
242/* https://github.com/harfbuzz/harfbuzz/issues/1247 */
243#define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow")))
244#else
245#define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW
246#endif
247
248
Ken Browneee5b5e2018-11-12 21:05:39 -0500249#ifdef _WIN32
Behdad Esfahboddb308282014-07-19 16:32:04 -0400250 /* We need Windows Vista for both Uniscribe backend and for
251 * MemoryBarrier. We don't support compiling on Windows XP,
252 * though we run on it fine. */
253# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
254# undef _WIN32_WINNT
255# endif
256# ifndef _WIN32_WINNT
Matt Oliver24dd6c12018-09-23 18:08:30 +1000257# if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
258# define _WIN32_WINNT 0x0600
259# endif
Behdad Esfahboddb308282014-07-19 16:32:04 -0400260# endif
Behdad Esfahbod270971a2014-08-15 14:28:04 -0400261# ifndef WIN32_LEAN_AND_MEAN
262# define WIN32_LEAN_AND_MEAN 1
263# endif
264# ifndef STRICT
265# define STRICT 1
266# endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500267
Konstantin Rittf3537b62015-01-25 09:50:51 +0400268# if defined(_WIN32_WCE)
269 /* Some things not defined on Windows CE. */
Konstantin Ritt50690622016-04-26 12:02:26 +0400270# define vsnprintf _vsnprintf
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200271# define getenv(Name) nullptr
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400272# if _WIN32_WCE < 0x800
273# define setlocale(Category, Locale) "C"
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400274static int errno = 0; /* Use something better? */
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400275# endif
Konstantin Rittf3537b62015-01-25 09:50:51 +0400276# elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200277# define getenv(Name) nullptr
Konstantin Rittf3537b62015-01-25 09:50:51 +0400278# endif
Behdad Esfahbodce01ad72015-04-01 11:05:59 -0700279# if defined(_MSC_VER) && _MSC_VER < 1900
Konstantin Rittf3537b62015-01-25 09:50:51 +0400280# define snprintf _snprintf
Konstantin Rittf3537b62015-01-25 09:50:51 +0400281# endif
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400282#endif
283
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500284#if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400285/* atexit() is only safe to be called from shared libraries on certain
286 * platforms. Whitelist.
287 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
288# if defined(__linux) && defined(__GLIBC_PREREQ)
289# if __GLIBC_PREREQ(2,3)
290/* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
291# define HB_USE_ATEXIT 1
292# endif
293# elif defined(_MSC_VER) || defined(__MINGW32__)
294/* For MSVC:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430295 * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
296 * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400297 * mingw32 headers say atexit is safe to use in shared libraries.
298 */
299# define HB_USE_ATEXIT 1
Ebrahim Byagowi632713b2018-04-12 14:17:03 +0430300# elif defined(__ANDROID__)
301/* This is available since Android NKD r8 or r8b:
302 * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400303 */
304# define HB_USE_ATEXIT 1
Bruce Mitchenere8859fc2018-02-04 01:26:57 +0700305# elif defined(__APPLE__)
306/* For macOS and related platforms, the atexit man page indicates
307 * that it will be invoked when the library is unloaded, not only
308 * at application exit.
309 */
310# define HB_USE_ATEXIT 1
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400311# endif
312#endif
Behdad Esfahbodc14b24f2018-03-26 10:44:54 -0700313#ifdef HB_NO_ATEXIT
314# undef HB_USE_ATEXIT
315#endif
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500316#ifndef HB_USE_ATEXIT
317# define HB_USE_ATEXIT 0
318#endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500319
Behdad Esfahbod35a73832009-08-01 19:30:31 -0400320#define HB_STMT_START do
321#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -0500322
Behdad Esfahbod19e00912018-08-06 04:54:31 -0700323/* Static-assert as expression. */
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200324template <unsigned int cond> class hb_assert_constant_t;
325template <> class hb_assert_constant_t<1> {};
Behdad Esfahbod672ca3b2015-10-26 14:05:05 -0700326#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
Behdad Esfahboddcb70262011-04-21 16:34:22 -0400327
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400328/* Lets assert int types. Saves trouble down the road. */
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200329static_assert ((sizeof (int8_t) == 1), "");
330static_assert ((sizeof (uint8_t) == 1), "");
331static_assert ((sizeof (int16_t) == 2), "");
332static_assert ((sizeof (uint16_t) == 2), "");
333static_assert ((sizeof (int32_t) == 4), "");
334static_assert ((sizeof (uint32_t) == 4), "");
335static_assert ((sizeof (int64_t) == 8), "");
336static_assert ((sizeof (uint64_t) == 8), "");
337static_assert ((sizeof (hb_codepoint_t) == 4), "");
338static_assert ((sizeof (hb_position_t) == 4), "");
339static_assert ((sizeof (hb_mask_t) == 4), "");
340static_assert ((sizeof (hb_var_int_t) == 4), "");
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400341
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400342
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700343#if __cplusplus >= 201103L
344
Behdad Esfahbod67a22f32018-10-29 17:37:41 -0700345/* We only enable these with C++11 or later, since earlier language
346 * does not allow structs with constructors in unions, and we need
347 * those. */
348
349#define HB_NO_COPY_ASSIGN(TypeName) \
350 TypeName(const TypeName&); \
351 void operator=(const TypeName&)
352#define HB_NO_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \
353 TypeName(const TypeName<T1, T2>&); \
354 void operator=(const TypeName<T1, T2>&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700355#define HB_NO_CREATE_COPY_ASSIGN(TypeName) \
356 TypeName(void); \
Behdad Esfahbodfb07d1a2018-06-01 17:32:07 -0700357 TypeName(const TypeName&); \
358 void operator=(const TypeName&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700359#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) \
360 TypeName(void); \
Behdad Esfahboda256a922018-10-29 11:25:35 -0700361 TypeName(const TypeName<T>&); \
362 void operator=(const TypeName<T>&)
Behdad Esfahbodea0e51d2018-10-29 16:00:23 -0700363#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \
364 TypeName(void); \
Behdad Esfahboda256a922018-10-29 11:25:35 -0700365 TypeName(const TypeName<T1, T2>&); \
Ebrahim Byagowif7a08cd2018-10-30 11:29:09 +0330366 void operator=(const TypeName<T1, T2>&)
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700367
368#else /* __cpluspplus >= 201103L */
369
Behdad Esfahbod67a22f32018-10-29 17:37:41 -0700370#define HB_NO_COPY_ASSIGN(TypeName)
Behdad Esfahbodae9ad102018-11-03 21:41:50 -0400371#define HB_NO_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) static_assert (true, "")
372#define HB_NO_CREATE_COPY_ASSIGN(TypeName) static_assert (true, "")
373#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) static_assert (true, "")
374#define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) static_assert (true, "")
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700375
376#endif /* __cpluspplus >= 201103L */
Behdad Esfahbod6c222762018-05-25 16:21:27 -0700377
Behdad Esfahbod44999f82018-07-11 17:00:59 +0200378
379/*
380 * Compiler-assisted vectorization parameters.
381 */
382
383/*
384 * Disable vectorization for now. To correctly use them, we should
385 * use posix_memalign() to allocate in hb_vector_t. Otherwise, can
386 * cause misaligned access.
387 *
388 * https://bugs.chromium.org/p/chromium/issues/detail?id=860184
389 */
390#if !defined(HB_VECTOR_SIZE)
391# define HB_VECTOR_SIZE 0
392#endif
393
394/* The `vector_size' attribute was introduced in gcc 3.1. */
395#if !defined(HB_VECTOR_SIZE)
396# if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
397# define HB_VECTOR_SIZE 128
398# else
399# define HB_VECTOR_SIZE 0
400# endif
401#endif
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200402static_assert (0 == (HB_VECTOR_SIZE & (HB_VECTOR_SIZE - 1)), "HB_VECTOR_SIZE is not power of 2.");
403static_assert (0 == (HB_VECTOR_SIZE % 64), "HB_VECTOR_SIZE is not multiple of 64.");
Behdad Esfahbod44999f82018-07-11 17:00:59 +0200404#if HB_VECTOR_SIZE
405typedef uint64_t hb_vector_size_impl_t __attribute__((vector_size (HB_VECTOR_SIZE / 8)));
406#else
407typedef uint64_t hb_vector_size_impl_t;
408#endif
409
410
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900411/* HB_NDEBUG disables some sanity checks that are very safe to disable and
412 * should be disabled in production systems. If NDEBUG is defined, enable
413 * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
414 * light-weight) to be enabled, then HB_DEBUG can be defined to disable
415 * the costlier checks. */
416#ifdef NDEBUG
Behdad Esfahbodbd021a62018-05-08 01:50:30 -0700417#define HB_NDEBUG 1
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900418#endif
419
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400420
Behdad Esfahbodbe336da2018-08-06 04:32:51 -0700421/* Flags */
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400422
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800423/* Enable bitwise ops on enums marked as flags_t */
Behdad Esfahbod69862082015-11-04 18:46:22 -0800424/* To my surprise, looks like the function resolver is happy to silently cast
425 * one enum to another... So this doesn't provide the type-checking that I
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800426 * originally had in mind... :(.
427 *
ebraminio7c6937e2017-11-20 14:49:22 -0500428 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800429 */
430#ifdef _MSC_VER
431# pragma warning(disable:4200)
432# pragma warning(disable:4800)
Chun-wei Fan167c3272015-11-09 17:17:56 +0800433#endif
Behdad Esfahbod1dc32ea2015-11-20 13:24:19 -0800434#define HB_MARK_AS_FLAG_T(T) \
435 extern "C++" { \
436 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
437 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
438 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
439 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
440 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
441 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
442 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
443 }
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800444
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400445/* Useful for set-operations on small enums.
446 * For example, for testing "x ∈ {x1, x2, x3}" use:
Behdad Esfahbod6058f982017-10-19 11:39:52 -0700447 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400448 */
Behdad Esfahbod3d2b98e2018-11-23 10:45:44 -0500449#define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x)))
450#define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0)
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400451#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod3d2b98e2018-11-23 10:45:44 -0500452#define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x)))
453#define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400454
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400455
Steven R. Loomisa13b0232015-12-11 10:21:27 -0800456/* Size signifying variable-sized array */
457#define VAR 1
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500458
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600459
Chun-wei Fan19256be2018-03-12 13:33:03 +0800460/* fallback for round() */
Chun-wei Fan19256be2018-03-12 13:33:03 +0800461static inline double
Behdad Esfahbod01dff1e2018-06-26 18:00:58 -0400462_hb_round (double x)
Chun-wei Fan19256be2018-03-12 13:33:03 +0800463{
464 if (x >= 0)
465 return floor (x + 0.5);
466 else
467 return ceil (x - 0.5);
468}
Behdad Esfahbod292c1002018-07-10 13:16:52 +0200469#if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND)
Behdad Esfahbod01dff1e2018-06-26 18:00:58 -0400470#define round(x) _hb_round(x)
Chun-wei Fan19256be2018-03-12 13:33:03 +0800471#endif
472
473
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200474/* fallback for posix_memalign() */
475static inline int
476_hb_memalign(void **memptr, size_t alignment, size_t size)
477{
Behdad Esfahbod491d93b2018-07-10 16:03:31 +0200478 if (unlikely (0 != (alignment & (alignment - 1)) ||
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200479 !alignment ||
480 0 != (alignment & (sizeof (void *) - 1))))
481 return EINVAL;
482
483 char *p = (char *) malloc (size + alignment - 1);
484 if (unlikely (!p))
485 return ENOMEM;
486
487 size_t off = (size_t) p & (alignment - 1);
488 if (off)
489 p += alignment - off;
490
491 *memptr = (void *) p;
492
493 return 0;
494}
495#if !defined(posix_memalign) && !defined(HAVE_POSIX_MEMALIGN)
496#define posix_memalign _hb_memalign
497#endif
498
499
Behdad Esfahbodbdb53ca2018-10-11 20:20:00 -0400500/*
501 * For lack of a better place, put Zawgyi script hack here.
502 * https://github.com/harfbuzz/harfbuzz/issues/1162
503 */
504
505#define HB_SCRIPT_MYANMAR_ZAWGYI ((hb_script_t) HB_TAG ('Q','a','a','g'))
506
507
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700508/* Some really basic things everyone wants. */
509template <typename T> struct hb_remove_const { typedef T value; };
510template <typename T> struct hb_remove_const<const T> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500511#define hb_remove_const(T) hb_remove_const<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700512template <typename T> struct hb_remove_reference { typedef T value; };
513template <typename T> struct hb_remove_reference<T &> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500514#define hb_remove_reference(T) hb_remove_reference<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700515template <typename T> struct hb_remove_pointer { typedef T value; };
516template <typename T> struct hb_remove_pointer<T *> { typedef T value; };
Behdad Esfahbodf99abcc2018-11-24 00:22:21 -0500517#define hb_remove_pointer(T) hb_remove_pointer<T>::value
Behdad Esfahbod07386ea2018-10-22 21:18:27 -0700518
519
Behdad Esfahbod25aa4112018-07-10 16:05:03 +0200520/* Headers we include for everyone. Keep sorted. They express dependency amongst
521 * themselves, but no other file should include them.*/
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700522#include "hb-atomic.hh"
Behdad Esfahbod491d93b2018-07-10 16:03:31 +0200523#include "hb-debug.hh"
Behdad Esfahbod25aa4112018-07-10 16:05:03 +0200524#include "hb-dsalgs.hh"
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700525#include "hb-mutex.hh"
Behdad Esfahbod25147ff2018-08-06 05:01:52 -0700526#include "hb-null.hh"
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700527#include "hb-object.hh"
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200528
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700529#endif /* HB_HH */