blob: 86dccd6ef1c29f528592055ff256e532a9570445 [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 Esfahbodf60282c2019-01-24 16:29:09 +010032#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC
Behdad Esfahbod51b58482019-01-28 14:09:45 -050033#if defined(_MSC_VER)
34#pragma warning( disable: 4068 ) /* Unknown pragma */
35#endif
Behdad Esfahbodf13b6782019-01-18 14:53:54 -050036#if defined(__GNUC__) || defined(__clang__)
Behdad Esfahbod1077e402019-01-17 18:36:10 -050037/* Rules:
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050038 *
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 *
Behdad Esfahbod1077e402019-01-17 18:36:10 -050042 * - Within each category, keep sorted.
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050043 *
Behdad Esfahbod2bd530d2019-01-18 09:28:22 -050044 * - Warnings whose scope can be expanded in future compiler versions shall
45 * be declared as "warning". Otherwise, either ignored or error.
Behdad Esfahbod1077e402019-01-17 18:36:10 -050046 */
Behdad Esfahbod16e5ba82019-01-18 09:46:17 -050047
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050048/* Setup. Don't sort order within this category. */
Behdad Esfahbodf60282c2019-01-24 16:29:09 +010049#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050050#pragma GCC diagnostic warning "-Wall"
51#pragma GCC diagnostic warning "-Wextra"
Behdad Esfahbodf60282c2019-01-24 16:29:09 +010052#endif
53#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050054#pragma GCC diagnostic ignored "-Wpragmas"
55#pragma GCC diagnostic ignored "-Wunknown-pragmas"
56#pragma GCC diagnostic ignored "-Wunknown-warning-option"
Behdad Esfahbodf60282c2019-01-24 16:29:09 +010057#endif
58#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050059//#pragma GCC diagnostic warning "-Weverything"
Behdad Esfahbodf60282c2019-01-24 16:29:09 +010060#endif
Behdad Esfahbod16e5ba82019-01-18 09:46:17 -050061
Behdad Esfahbod2bd530d2019-01-18 09:28:22 -050062/* Error. Should never happen. */
Behdad Esfahbodf60282c2019-01-24 16:29:09 +010063#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_ERROR
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050064#pragma GCC diagnostic error "-Wc++11-narrowing"
65#pragma GCC diagnostic error "-Wcast-align"
Behdad Esfahbod60022ec2019-01-26 14:04:51 +010066#pragma GCC diagnostic error "-Wcast-function-type"
Behdad Esfahbod9b4e51b2019-01-18 12:23:02 -050067#pragma GCC diagnostic error "-Wdelete-non-virtual-dtor"
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050068#pragma GCC diagnostic error "-Wformat-security"
69#pragma GCC diagnostic error "-Wimplicit-function-declaration"
70#pragma GCC diagnostic error "-Winit-self"
Behdad Esfahbodf35e7ea2019-01-26 22:50:00 +010071#pragma GCC diagnostic error "-Winjected-class-name"
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050072#pragma GCC diagnostic error "-Wmissing-braces"
73#pragma GCC diagnostic error "-Wmissing-declarations"
74#pragma GCC diagnostic error "-Wmissing-prototypes"
75#pragma GCC diagnostic error "-Wnested-externs"
76#pragma GCC diagnostic error "-Wold-style-definition"
77#pragma GCC diagnostic error "-Wpointer-arith"
78#pragma GCC diagnostic error "-Wredundant-decls"
Behdad Esfahboda25174a2019-01-18 09:59:36 -050079#pragma GCC diagnostic error "-Wreorder"
Behdad Esfahbod90772722019-01-22 12:40:18 +010080#pragma GCC diagnostic error "-Wsign-compare"
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050081#pragma GCC diagnostic error "-Wstrict-prototypes"
82#pragma GCC diagnostic error "-Wstring-conversion"
83#pragma GCC diagnostic error "-Wswitch-enum"
84#pragma GCC diagnostic error "-Wtautological-overlap-compare"
85#pragma GCC diagnostic error "-Wunneeded-internal-declaration"
86#pragma GCC diagnostic error "-Wunused"
87#pragma GCC diagnostic error "-Wunused-local-typedefs"
88#pragma GCC diagnostic error "-Wunused-value"
89#pragma GCC diagnostic error "-Wunused-variable"
Behdad Esfahbodc81f02f2019-01-22 12:00:44 +010090#pragma GCC diagnostic error "-Wvla"
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050091#pragma GCC diagnostic error "-Wwrite-strings"
Behdad Esfahbodf60282c2019-01-24 16:29:09 +010092#endif
Behdad Esfahbod16e5ba82019-01-18 09:46:17 -050093
Behdad Esfahbod2bd530d2019-01-18 09:28:22 -050094/* Warning. To be investigated if happens. */
Behdad Esfahbodf60282c2019-01-24 16:29:09 +010095#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING
Behdad Esfahbod7a634d72019-01-18 09:49:14 -050096#pragma GCC diagnostic warning "-Wbuiltin-macro-redefined"
97#pragma GCC diagnostic warning "-Wdisabled-optimization"
98#pragma GCC diagnostic warning "-Wformat=2"
99#pragma GCC diagnostic warning "-Wignored-pragma-optimize"
100#pragma GCC diagnostic warning "-Wlogical-op"
Behdad Esfahbod46600902019-01-18 09:53:16 -0500101#pragma GCC diagnostic warning "-Wmaybe-uninitialized"
Behdad Esfahbod7a634d72019-01-18 09:49:14 -0500102#pragma GCC diagnostic warning "-Wmissing-format-attribute"
Behdad Esfahbod7a634d72019-01-18 09:49:14 -0500103#pragma GCC diagnostic warning "-Wundef"
Behdad Esfahbodf60282c2019-01-24 16:29:09 +0100104#endif
Behdad Esfahbod16e5ba82019-01-18 09:46:17 -0500105
Behdad Esfahbod2bd530d2019-01-18 09:28:22 -0500106/* Ignored currently, but should be fixed at some point. */
Behdad Esfahbodf60282c2019-01-24 16:29:09 +0100107#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
Behdad Esfahbod7a634d72019-01-18 09:49:14 -0500108#pragma GCC diagnostic ignored "-Wconversion" // TODO fix
109#pragma GCC diagnostic ignored "-Wformat-signedness" // TODO fix
110#pragma GCC diagnostic ignored "-Wshadow" // TODO fix
111#pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations" // TODO fix
112#pragma GCC diagnostic ignored "-Wunused-parameter" // TODO fix
Behdad Esfahbodf60282c2019-01-24 16:29:09 +0100113#endif
Behdad Esfahbod16e5ba82019-01-18 09:46:17 -0500114
Behdad Esfahbod2bd530d2019-01-18 09:28:22 -0500115/* Ignored intentionally. */
Behdad Esfahbodf60282c2019-01-24 16:29:09 +0100116#ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED
Behdad Esfahbod7a634d72019-01-18 09:49:14 -0500117#pragma GCC diagnostic ignored "-Wclass-memaccess"
118#pragma GCC diagnostic ignored "-Wformat-nonliteral"
119#pragma GCC diagnostic ignored "-Wformat-zero-length"
120#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
121#pragma GCC diagnostic ignored "-Wpacked" // Erratic impl in clang
122#pragma GCC diagnostic ignored "-Wstrict-aliasing"
123#pragma GCC diagnostic ignored "-Wtype-limits"
Ebrahim Byagowie261dc32019-05-07 01:24:55 +0430124#pragma GCC diagnostic ignored "-Wc++11-compat" // only gcc raises it
Behdad Esfahbodf60282c2019-01-24 16:29:09 +0100125#endif
Behdad Esfahbodf13b6782019-01-18 14:53:54 -0500126
127#endif
Behdad Esfahbod868b0c02019-01-17 15:08:36 -0500128#endif
129
Behdad Esfahbod5ddd9cc2011-09-16 16:40:44 -0400130#ifdef HAVE_CONFIG_H
Behdad Esfahboddf660282009-08-01 20:46:02 -0400131#include "config.h"
132#endif
Behdad Esfahbod12360f72008-01-23 15:50:38 -0500133
Behdad Esfahbod49334f92018-12-17 18:27:36 -0500134/*
135 * Following added based on what AC_USE_SYSTEM_EXTENSIONS adds to
136 * config.h.in. Copied here for the convenience of those embedding
137 * HarfBuzz and not using our build system.
138 */
139/* Enable extensions on AIX 3, Interix. */
140#ifndef _ALL_SOURCE
141# define _ALL_SOURCE 1
142#endif
143/* Enable GNU extensions on systems that have them. */
144#ifndef _GNU_SOURCE
145# define _GNU_SOURCE 1
146#endif
147/* Enable threading extensions on Solaris. */
148#ifndef _POSIX_PTHREAD_SEMANTICS
149# define _POSIX_PTHREAD_SEMANTICS 1
150#endif
151/* Enable extensions on HP NonStop. */
152#ifndef _TANDEM_SOURCE
153# define _TANDEM_SOURCE 1
154#endif
155/* Enable general extensions on Solaris. */
156#ifndef __EXTENSIONS__
157# define __EXTENSIONS__ 1
158#endif
159
Chun-wei Fan8e4ad1d2018-07-25 18:12:34 +0800160#if defined (_MSC_VER) && defined (HB_DLL_EXPORT)
161#define HB_EXTERN __declspec (dllexport) extern
162#endif
163
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -0400164#include "hb.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -0400165#define HB_H_IN
Behdad Esfahbod3e32cd92012-04-23 13:20:52 -0400166#include "hb-ot.h"
Behdad Esfahbodd1c9eb42012-04-12 13:17:44 -0400167#define HB_OT_H_IN
Behdad Esfahbodb2061332018-11-23 14:05:11 -0500168#include "hb-aat.h"
169#define HB_AAT_H_IN
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400170
Behdad Esfahbod22da1232019-04-24 10:53:16 -0400171#include <limits.h>
Behdad Esfahbod7cdd6ab2018-01-10 04:33:08 +0100172#include <math.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400173#include <stdlib.h>
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400174#include <stddef.h>
Behdad Esfahbodf0954d12009-07-30 15:33:57 -0400175#include <string.h>
176#include <assert.h>
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400177#include <errno.h>
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400178#include <stdio.h>
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400179#include <stdarg.h>
Behdad Esfahbodc7d457a2009-05-21 12:46:29 -0400180
Tor Andersson8f4c1232018-04-13 23:01:54 +0430181#if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
Arkady Shapkin6c103282018-02-15 23:17:46 +0300182#include <intrin.h>
183#endif
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400184
Behdad Esfahbod2a749682017-10-26 19:48:33 -0600185#define HB_PASTE1(a,b) a##b
186#define HB_PASTE(a,b) HB_PASTE1(a,b)
187
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200188
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100189/* Compile-time custom allocator support. */
190
191#if defined(hb_malloc_impl) \
192 && defined(hb_calloc_impl) \
193 && defined(hb_realloc_impl) \
194 && defined(hb_free_impl)
Behdad Esfahbodcc6ea302015-10-12 17:21:52 -0400195extern "C" void* hb_malloc_impl(size_t size);
196extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
197extern "C" void* hb_realloc_impl(void *ptr, size_t size);
198extern "C" void hb_free_impl(void *ptr);
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100199#define malloc hb_malloc_impl
200#define calloc hb_calloc_impl
201#define realloc hb_realloc_impl
202#define free hb_free_impl
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200203
Behdad Esfahbod763f8782018-07-10 13:47:41 +0200204#if defined(hb_memalign_impl)
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200205extern "C" int hb_memalign_impl(void **memptr, size_t alignment, size_t size);
206#define posix_memalign hb_memalign_impl
207#else
208#undef HAVE_POSIX_MEMALIGN
209#endif
210
Behdad Esfahbod52b41852015-10-03 13:20:55 +0100211#endif
212
213
Behdad Esfahbod2aae6172018-07-11 17:27:23 +0200214/*
215 * Compiler attributes
Behdad Esfahbod37be7742018-08-06 04:51:38 -0700216 */
Behdad Esfahbod292c1002018-07-10 13:16:52 +0200217
Ebrahim Byagowi76c48732017-06-02 21:53:10 +0430218#if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
219#define likely(expr) (__builtin_expect (!!(expr), 1))
220#define unlikely(expr) (__builtin_expect (!!(expr), 0))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400221#else
222#define likely(expr) (expr)
223#define unlikely(expr) (expr)
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400224#endif
225
Steve Lhomme0b8f3ab2016-07-11 21:57:26 +0200226#if !defined(__GNUC__) && !defined(__clang__)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400227#undef __attribute__
228#define __attribute__(x)
229#endif
Behdad Esfahboda794ebf2009-08-06 12:32:35 -0400230
Behdad Esfahbodd9f6be32019-01-18 12:48:01 -0500231#if defined(__GNUC__) && (__GNUC__ >= 3)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400232#define HB_PURE_FUNC __attribute__((pure))
233#define HB_CONST_FUNC __attribute__((const))
234#define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
235#else
236#define HB_PURE_FUNC
237#define HB_CONST_FUNC
238#define HB_PRINTF_FUNC(format_idx, arg_idx)
239#endif
Behdad Esfahbodd9f6be32019-01-18 12:48:01 -0500240#if defined(__GNUC__) && (__GNUC__ >= 4)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400241#define HB_UNUSED __attribute__((unused))
Behdad Esfahbodbe59f3c2017-12-05 09:01:28 -0800242#elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
243#define HB_UNUSED __pragma(warning(suppress: 4100 4101))
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400244#else
245#define HB_UNUSED
246#endif
247
248#ifndef HB_INTERNAL
Behdad Esfahbod90d1c742018-02-11 19:08:59 -0800249# if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400250# define HB_INTERNAL __attribute__((__visibility__("hidden")))
Behdad Esfahbod94759d42018-07-23 15:01:39 -0700251# elif defined(__MINGW32__)
Chun-wei Fan8e4ad1d2018-07-25 18:12:34 +0800252 /* We use -export-symbols on mingw32, since it does not support visibility attributes. */
253# define HB_INTERNAL
254# elif defined (_MSC_VER) && defined (HB_DLL_EXPORT)
255 /* We do not try to export internal symbols on Visual Studio */
Behdad Esfahbod94759d42018-07-23 15:01:39 -0700256# define HB_INTERNAL
257#else
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400258# define HB_INTERNAL
Behdad Esfahbodb87cbe22018-02-07 14:11:26 -0500259# define HB_NO_VISIBILITY 1
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400260# endif
261#endif
262
Behdad Esfahbod02d864a2019-04-15 15:39:03 -0400263/* https://github.com/harfbuzz/harfbuzz/issues/1651 */
264#if defined(__clang__) && __clang_major__ < 10
265#define static_const static
266#else
267#define static_const static const
268#endif
269
Behdad Esfahbodd9f6be32019-01-18 12:48:01 -0500270#if defined(__GNUC__) && (__GNUC__ >= 3)
Behdad Esfahbodae2b8542014-06-03 16:59:09 -0400271#define HB_FUNC __PRETTY_FUNCTION__
272#elif defined(_MSC_VER)
273#define HB_FUNC __FUNCSIG__
274#else
275#define HB_FUNC __func__
276#endif
277
prrace498e4372018-06-09 16:04:28 -0700278#if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
Behdad Esfahbod8cef3a62018-02-09 16:04:23 -0600279/* https://github.com/harfbuzz/harfbuzz/issues/630 */
280#define __restrict
281#endif
282
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200283/*
284 * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
285 * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
286 * cases that fall through without a break or return statement. HB_FALLTHROUGH
287 * is only needed on cases that have code:
288 *
289 * switch (foo) {
290 * case 1: // These cases have no code. No fallthrough annotations are needed.
291 * case 2:
292 * case 3:
293 * foo = 4; // This case has code, so a fallthrough annotation is needed:
294 * HB_FALLTHROUGH;
295 * default:
296 * return foo;
297 * }
298 */
299#if defined(__clang__) && __cplusplus >= 201103L
300 /* clang's fallthrough annotations are only available starting in C++11. */
301# define HB_FALLTHROUGH [[clang::fallthrough]]
Behdad Esfahbodd9f6be32019-01-18 12:48:01 -0500302#elif defined(__GNUC__) && (__GNUC__ >= 7)
Behdad Esfahbodf41b9212018-02-05 19:51:09 -0500303 /* GNU fallthrough attribute is available from GCC7 */
304# define HB_FALLTHROUGH __attribute__((fallthrough))
Behdad Esfahbod305d2fb2015-10-21 11:04:28 -0200305#elif defined(_MSC_VER)
306 /*
307 * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
308 * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
309 */
310# include <sal.h>
311# define HB_FALLTHROUGH __fallthrough
312#else
313# define HB_FALLTHROUGH /* FALLTHROUGH */
314#endif
315
Behdad Esfahbod62376a72018-10-14 15:20:50 -0700316#if defined(__clang__)
317/* Disable certain sanitizer errors. */
318/* https://github.com/harfbuzz/harfbuzz/issues/1247 */
319#define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow")))
320#else
321#define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW
322#endif
323
324
Ken Browneee5b5e2018-11-12 21:05:39 -0500325#ifdef _WIN32
Behdad Esfahboddb308282014-07-19 16:32:04 -0400326 /* We need Windows Vista for both Uniscribe backend and for
327 * MemoryBarrier. We don't support compiling on Windows XP,
328 * though we run on it fine. */
329# if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
330# undef _WIN32_WINNT
331# endif
332# ifndef _WIN32_WINNT
Matt Oliver24dd6c12018-09-23 18:08:30 +1000333# if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
334# define _WIN32_WINNT 0x0600
335# endif
Behdad Esfahboddb308282014-07-19 16:32:04 -0400336# endif
Behdad Esfahbod270971a2014-08-15 14:28:04 -0400337# ifndef WIN32_LEAN_AND_MEAN
338# define WIN32_LEAN_AND_MEAN 1
339# endif
340# ifndef STRICT
341# define STRICT 1
342# endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500343
Konstantin Rittf3537b62015-01-25 09:50:51 +0400344# if defined(_WIN32_WCE)
345 /* Some things not defined on Windows CE. */
Konstantin Ritt50690622016-04-26 12:02:26 +0400346# define vsnprintf _vsnprintf
Cody Planteen72e3eba2019-05-02 13:03:15 -0600347# ifndef HB_NO_GETENV
348# define HB_NO_GETENV
349# endif
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400350# if _WIN32_WCE < 0x800
351# define setlocale(Category, Locale) "C"
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400352static int errno = 0; /* Use something better? */
Konstantin Ritt855a5d72015-04-10 17:18:01 +0400353# endif
Konstantin Rittf3537b62015-01-25 09:50:51 +0400354# elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
Cody Planteen72e3eba2019-05-02 13:03:15 -0600355# ifndef HB_NO_GETENV
356# define HB_NO_GETENV
357# endif
Konstantin Rittf3537b62015-01-25 09:50:51 +0400358# endif
Behdad Esfahbodce01ad72015-04-01 11:05:59 -0700359# if defined(_MSC_VER) && _MSC_VER < 1900
Konstantin Rittf3537b62015-01-25 09:50:51 +0400360# define snprintf _snprintf
Konstantin Rittf3537b62015-01-25 09:50:51 +0400361# endif
Behdad Esfahbod26a963b2014-08-10 18:04:50 -0400362#endif
363
Cody Planteen72e3eba2019-05-02 13:03:15 -0600364#ifdef HB_NO_GETENV
365#define getenv(Name) nullptr
366#endif
367
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500368#if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400369/* atexit() is only safe to be called from shared libraries on certain
370 * platforms. Whitelist.
371 * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
372# if defined(__linux) && defined(__GLIBC_PREREQ)
373# if __GLIBC_PREREQ(2,3)
374/* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
375# define HB_USE_ATEXIT 1
376# endif
377# elif defined(_MSC_VER) || defined(__MINGW32__)
378/* For MSVC:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +0430379 * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
380 * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400381 * mingw32 headers say atexit is safe to use in shared libraries.
382 */
383# define HB_USE_ATEXIT 1
Ebrahim Byagowi632713b2018-04-12 14:17:03 +0430384# elif defined(__ANDROID__)
385/* This is available since Android NKD r8 or r8b:
386 * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400387 */
388# define HB_USE_ATEXIT 1
Bruce Mitchenere8859fc2018-02-04 01:26:57 +0700389# elif defined(__APPLE__)
390/* For macOS and related platforms, the atexit man page indicates
391 * that it will be invoked when the library is unloaded, not only
392 * at application exit.
393 */
394# define HB_USE_ATEXIT 1
Behdad Esfahbod38fb30d2014-08-06 13:34:49 -0400395# endif
396#endif
Behdad Esfahbodc14b24f2018-03-26 10:44:54 -0700397#ifdef HB_NO_ATEXIT
398# undef HB_USE_ATEXIT
399#endif
Behdad Esfahbodb89c7fd2018-11-21 12:32:48 -0500400#ifndef HB_USE_ATEXIT
401# define HB_USE_ATEXIT 0
402#endif
Behdad Esfahboddabe6982012-11-23 14:21:35 -0500403
Behdad Esfahbod35a73832009-08-01 19:30:31 -0400404#define HB_STMT_START do
405#define HB_STMT_END while (0)
Behdad Esfahbod5b3f7702006-12-28 06:42:37 -0500406
Behdad Esfahbod19e00912018-08-06 04:54:31 -0700407/* Static-assert as expression. */
Behdad Esfahbod76dcbf82017-10-15 11:24:35 +0200408template <unsigned int cond> class hb_assert_constant_t;
409template <> class hb_assert_constant_t<1> {};
Behdad Esfahbod672ca3b2015-10-26 14:05:05 -0700410#define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
Behdad Esfahboddcb70262011-04-21 16:34:22 -0400411
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400412/* Lets assert int types. Saves trouble down the road. */
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200413static_assert ((sizeof (int8_t) == 1), "");
414static_assert ((sizeof (uint8_t) == 1), "");
415static_assert ((sizeof (int16_t) == 2), "");
416static_assert ((sizeof (uint16_t) == 2), "");
417static_assert ((sizeof (int32_t) == 4), "");
418static_assert ((sizeof (uint32_t) == 4), "");
419static_assert ((sizeof (int64_t) == 8), "");
420static_assert ((sizeof (uint64_t) == 8), "");
421static_assert ((sizeof (hb_codepoint_t) == 4), "");
422static_assert ((sizeof (hb_position_t) == 4), "");
423static_assert ((sizeof (hb_mask_t) == 4), "");
424static_assert ((sizeof (hb_var_int_t) == 4), "");
Behdad Esfahbod6fd53642011-04-11 11:47:14 -0400425
Behdad Esfahbod824fd342019-04-11 11:16:01 -0400426#define HB_DELETE_COPY_ASSIGN(TypeName) \
Behdad Esfahbodbaf1e792019-04-11 11:18:04 -0400427 TypeName(const TypeName&) = delete; \
428 void operator=(const TypeName&) = delete
Behdad Esfahbod824fd342019-04-11 11:16:01 -0400429#define HB_DELETE_CREATE_COPY_ASSIGN(TypeName) \
Behdad Esfahbodbaf1e792019-04-11 11:18:04 -0400430 TypeName() = delete; \
431 TypeName(const TypeName&) = delete; \
432 void operator=(const TypeName&) = delete
Behdad Esfahbodc7c5df92018-10-29 15:16:52 -0700433
Behdad Esfahbod44999f82018-07-11 17:00:59 +0200434
435/*
436 * Compiler-assisted vectorization parameters.
437 */
438
439/*
440 * Disable vectorization for now. To correctly use them, we should
441 * use posix_memalign() to allocate in hb_vector_t. Otherwise, can
442 * cause misaligned access.
443 *
444 * https://bugs.chromium.org/p/chromium/issues/detail?id=860184
445 */
446#if !defined(HB_VECTOR_SIZE)
447# define HB_VECTOR_SIZE 0
448#endif
449
450/* The `vector_size' attribute was introduced in gcc 3.1. */
451#if !defined(HB_VECTOR_SIZE)
452# if defined( __GNUC__ ) && ( __GNUC__ >= 4 )
453# define HB_VECTOR_SIZE 128
454# else
455# define HB_VECTOR_SIZE 0
456# endif
457#endif
Behdad Esfahbod606bf572018-09-16 19:33:48 +0200458static_assert (0 == (HB_VECTOR_SIZE & (HB_VECTOR_SIZE - 1)), "HB_VECTOR_SIZE is not power of 2.");
459static_assert (0 == (HB_VECTOR_SIZE % 64), "HB_VECTOR_SIZE is not multiple of 64.");
Behdad Esfahbod44999f82018-07-11 17:00:59 +0200460#if HB_VECTOR_SIZE
461typedef uint64_t hb_vector_size_impl_t __attribute__((vector_size (HB_VECTOR_SIZE / 8)));
462#else
463typedef uint64_t hb_vector_size_impl_t;
464#endif
465
466
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900467/* HB_NDEBUG disables some sanity checks that are very safe to disable and
468 * should be disabled in production systems. If NDEBUG is defined, enable
469 * HB_NDEBUG; but if it's desirable that normal assert()s (which are very
470 * light-weight) to be enabled, then HB_DEBUG can be defined to disable
471 * the costlier checks. */
472#ifdef NDEBUG
Behdad Esfahbodbd021a62018-05-08 01:50:30 -0700473#define HB_NDEBUG 1
Behdad Esfahbod91dd1152016-02-25 13:56:47 +0900474#endif
475
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400476
Behdad Esfahbodbe336da2018-08-06 04:32:51 -0700477/* Flags */
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400478
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800479/* Enable bitwise ops on enums marked as flags_t */
Behdad Esfahbod69862082015-11-04 18:46:22 -0800480/* To my surprise, looks like the function resolver is happy to silently cast
481 * one enum to another... So this doesn't provide the type-checking that I
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800482 * originally had in mind... :(.
483 *
ebraminio7c6937e2017-11-20 14:49:22 -0500484 * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
Behdad Esfahbode0082ae2015-11-17 18:42:13 -0800485 */
486#ifdef _MSC_VER
487# pragma warning(disable:4200)
488# pragma warning(disable:4800)
Chun-wei Fan167c3272015-11-09 17:17:56 +0800489#endif
Behdad Esfahbod1dc32ea2015-11-20 13:24:19 -0800490#define HB_MARK_AS_FLAG_T(T) \
491 extern "C++" { \
492 static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
493 static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
494 static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
495 static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
496 static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
497 static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
498 static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
Behdad Esfahbodd25a2f12018-12-23 20:19:52 -0500499 } \
500 static_assert (true, "")
Behdad Esfahbodaa7044d2015-11-04 16:25:57 -0800501
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400502/* Useful for set-operations on small enums.
503 * For example, for testing "x ∈ {x1, x2, x3}" use:
Behdad Esfahbod6058f982017-10-19 11:39:52 -0700504 * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
Behdad Esfahbod45d6f292011-07-30 14:44:30 -0400505 */
Behdad Esfahbod3d2b98e2018-11-23 10:45:44 -0500506#define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x)))
507#define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0)
Behdad Esfahbod2c372b82012-07-20 13:37:48 -0400508#define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
Behdad Esfahbod3d2b98e2018-11-23 10:45:44 -0500509#define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x)))
510#define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0)
Behdad Esfahbod7b08b0a2011-07-20 23:59:07 -0400511
Behdad Esfahbod8f0b64f2011-07-29 17:02:48 -0400512
Steven R. Loomisa13b0232015-12-11 10:21:27 -0800513/* Size signifying variable-sized array */
514#define VAR 1
Behdad Esfahbodbab02d32013-02-12 15:26:45 -0500515
Behdad Esfahbode1a37f32017-10-30 11:42:28 -0600516
Chun-wei Fan19256be2018-03-12 13:33:03 +0800517/* fallback for round() */
Chun-wei Fan19256be2018-03-12 13:33:03 +0800518static inline double
Behdad Esfahbod01dff1e2018-06-26 18:00:58 -0400519_hb_round (double x)
Chun-wei Fan19256be2018-03-12 13:33:03 +0800520{
521 if (x >= 0)
522 return floor (x + 0.5);
523 else
524 return ceil (x - 0.5);
525}
Behdad Esfahbod292c1002018-07-10 13:16:52 +0200526#if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND)
Behdad Esfahbod01dff1e2018-06-26 18:00:58 -0400527#define round(x) _hb_round(x)
Chun-wei Fan19256be2018-03-12 13:33:03 +0800528#endif
529
530
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200531/* fallback for posix_memalign() */
532static inline int
533_hb_memalign(void **memptr, size_t alignment, size_t size)
534{
Behdad Esfahbod491d93b2018-07-10 16:03:31 +0200535 if (unlikely (0 != (alignment & (alignment - 1)) ||
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200536 !alignment ||
537 0 != (alignment & (sizeof (void *) - 1))))
538 return EINVAL;
539
540 char *p = (char *) malloc (size + alignment - 1);
541 if (unlikely (!p))
542 return ENOMEM;
543
544 size_t off = (size_t) p & (alignment - 1);
545 if (off)
546 p += alignment - off;
547
548 *memptr = (void *) p;
549
550 return 0;
551}
552#if !defined(posix_memalign) && !defined(HAVE_POSIX_MEMALIGN)
553#define posix_memalign _hb_memalign
554#endif
555
556
Behdad Esfahbodbdb53ca2018-10-11 20:20:00 -0400557/*
Behdad Esfahbod17f0cfa2019-03-31 21:34:19 -0700558 * Big-endian integers. Here because fundamental.
559 */
560
561template <typename Type, int Bytes> struct BEInt;
562
563template <typename Type>
564struct BEInt<Type, 1>
565{
566 public:
567 BEInt<Type, 1>& operator = (Type V)
568 {
569 v = V;
570 return *this;
571 }
572 operator Type () const { return v; }
573 private: uint8_t v;
574};
575template <typename Type>
576struct BEInt<Type, 2>
577{
578 public:
579 BEInt<Type, 2>& operator = (Type V)
580 {
581 v[0] = (V >> 8) & 0xFF;
582 v[1] = (V ) & 0xFF;
583 return *this;
584 }
585 operator Type () const
586 {
587#if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \
588 defined(__BYTE_ORDER) && \
589 (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN)
590 /* Spoon-feed the compiler a big-endian integer with alignment 1.
591 * https://github.com/harfbuzz/harfbuzz/pull/1398 */
592 struct __attribute__((packed)) packed_uint16_t { uint16_t v; };
593#if __BYTE_ORDER == __LITTLE_ENDIAN
594 return __builtin_bswap16 (((packed_uint16_t *) this)->v);
595#else /* __BYTE_ORDER == __BIG_ENDIAN */
596 return ((packed_uint16_t *) this)->v;
597#endif
598#endif
599 return (v[0] << 8)
600 + (v[1] );
601 }
602 private: uint8_t v[2];
603};
604template <typename Type>
605struct BEInt<Type, 3>
606{
607 public:
608 BEInt<Type, 3>& operator = (Type V)
609 {
610 v[0] = (V >> 16) & 0xFF;
611 v[1] = (V >> 8) & 0xFF;
612 v[2] = (V ) & 0xFF;
613 return *this;
614 }
615 operator Type () const
616 {
617 return (v[0] << 16)
618 + (v[1] << 8)
619 + (v[2] );
620 }
621 private: uint8_t v[3];
622};
623template <typename Type>
624struct BEInt<Type, 4>
625{
626 public:
627 BEInt<Type, 4>& operator = (Type V)
628 {
629 v[0] = (V >> 24) & 0xFF;
630 v[1] = (V >> 16) & 0xFF;
631 v[2] = (V >> 8) & 0xFF;
632 v[3] = (V ) & 0xFF;
633 return *this;
634 }
635 operator Type () const
636 {
637 return (v[0] << 24)
638 + (v[1] << 16)
639 + (v[2] << 8)
640 + (v[3] );
641 }
642 private: uint8_t v[4];
643};
644
645
646/*
Behdad Esfahbodbdb53ca2018-10-11 20:20:00 -0400647 * For lack of a better place, put Zawgyi script hack here.
648 * https://github.com/harfbuzz/harfbuzz/issues/1162
649 */
650
651#define HB_SCRIPT_MYANMAR_ZAWGYI ((hb_script_t) HB_TAG ('Q','a','a','g'))
652
653
Behdad Esfahbod865deeb2018-12-21 17:35:58 -0500654/* Headers we include for everyone. Keep topologically sorted by dependency.
655 * They express dependency amongst themselves, but no other file should include
656 * them directly.*/
Behdad Esfahbode76a3ca2018-12-27 17:23:12 -0500657#include "hb-meta.hh"
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700658#include "hb-mutex.hh"
Behdad Esfahbodf64ea8f2018-12-30 18:49:34 -0500659#include "hb-atomic.hh" // Requires: hb-meta
Behdad Esfahbod8c6cbbd2018-12-28 14:29:09 -0500660#include "hb-null.hh" // Requires: hb-meta
Behdad Esfahbodbdd5a9c2019-03-28 21:58:07 -0700661#include "hb-algs.hh" // Requires: hb-meta hb-null
Behdad Esfahboda3fcb9a2019-04-16 10:45:20 -0400662#include "hb-iter.hh" // Requires: hb-algs hb-meta
Behdad Esfahbod6e3ad652019-01-09 09:05:01 -0800663#include "hb-debug.hh" // Requires: hb-algs hb-atomic
664#include "hb-array.hh" // Requires: hb-algs hb-iter hb-null
Behdad Esfahbod865deeb2018-12-21 17:35:58 -0500665#include "hb-vector.hh" // Requires: hb-array hb-null
666#include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector
Behdad Esfahbod83ea2772018-07-10 13:17:27 +0200667
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700668#endif /* HB_HH */