Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2007,2008,2009 Red Hat, Inc. |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 3 | * Copyright © 2011,2012 Google, Inc. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 4 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 5 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 6 | * |
| 7 | * Permission is hereby granted, without written agreement and without |
| 8 | * license or royalty fees, to use, copy, modify, and distribute this |
| 9 | * software and its documentation for any purpose, provided that the |
| 10 | * above copyright notice and the following two paragraphs appear in |
| 11 | * all copies of this software. |
| 12 | * |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | * |
| 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | * |
| 25 | * Red Hat Author(s): Behdad Esfahbod |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 29 | #ifndef HB_HH |
| 30 | #define HB_HH |
Behdad Esfahbod | 5b3f770 | 2006-12-28 06:42:37 -0500 | [diff] [blame] | 31 | |
Ebrahim Byagowi | 5dbbd0f | 2018-01-11 12:33:22 +0330 | [diff] [blame] | 32 | #define _GNU_SOURCE 1 |
| 33 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 34 | #ifdef HAVE_CONFIG_H |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 35 | #include "config.h" |
| 36 | #endif |
Behdad Esfahbod | 12360f7 | 2008-01-23 15:50:38 -0500 | [diff] [blame] | 37 | |
Behdad Esfahbod | 3f08750 | 2018-09-30 18:23:34 +0200 | [diff] [blame] | 38 | #ifndef _POSIX_C_SOURCE |
| 39 | #define _POSIX_C_SOURCE 200809L |
| 40 | #endif |
| 41 | |
Chun-wei Fan | 8e4ad1d | 2018-07-25 18:12:34 +0800 | [diff] [blame] | 42 | #if defined (_MSC_VER) && defined (HB_DLL_EXPORT) |
| 43 | #define HB_EXTERN __declspec (dllexport) extern |
| 44 | #endif |
| 45 | |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 46 | #include "hb.h" |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 47 | #define HB_H_IN |
Behdad Esfahbod | 3e32cd9 | 2012-04-23 13:20:52 -0400 | [diff] [blame] | 48 | #include "hb-ot.h" |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 49 | #define HB_OT_H_IN |
Behdad Esfahbod | b28815c | 2009-08-04 22:35:36 -0400 | [diff] [blame] | 50 | |
Behdad Esfahbod | 7cdd6ab | 2018-01-10 04:33:08 +0100 | [diff] [blame] | 51 | #include <math.h> |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 52 | #include <stdlib.h> |
Behdad Esfahbod | b65c060 | 2011-07-28 16:48:43 -0400 | [diff] [blame] | 53 | #include <stddef.h> |
Behdad Esfahbod | f0954d1 | 2009-07-30 15:33:57 -0400 | [diff] [blame] | 54 | #include <string.h> |
| 55 | #include <assert.h> |
Behdad Esfahbod | 7acb389 | 2009-08-05 15:20:34 -0400 | [diff] [blame] | 56 | #include <errno.h> |
Behdad Esfahbod | 40ec3bb | 2017-11-03 16:57:30 -0400 | [diff] [blame] | 57 | #include <stdio.h> |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 58 | #include <stdarg.h> |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 59 | |
Tor Andersson | 8f4c123 | 2018-04-13 23:01:54 +0430 | [diff] [blame] | 60 | #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__) |
Arkady Shapkin | 6c10328 | 2018-02-15 23:17:46 +0300 | [diff] [blame] | 61 | #include <intrin.h> |
| 62 | #endif |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 63 | |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 64 | #define HB_PASTE1(a,b) a##b |
| 65 | #define HB_PASTE(a,b) HB_PASTE1(a,b) |
| 66 | |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 67 | |
Behdad Esfahbod | 52b4185 | 2015-10-03 13:20:55 +0100 | [diff] [blame] | 68 | /* 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 Esfahbod | cc6ea30 | 2015-10-12 17:21:52 -0400 | [diff] [blame] | 74 | extern "C" void* hb_malloc_impl(size_t size); |
| 75 | extern "C" void* hb_calloc_impl(size_t nmemb, size_t size); |
| 76 | extern "C" void* hb_realloc_impl(void *ptr, size_t size); |
| 77 | extern "C" void hb_free_impl(void *ptr); |
Behdad Esfahbod | 52b4185 | 2015-10-03 13:20:55 +0100 | [diff] [blame] | 78 | #define malloc hb_malloc_impl |
| 79 | #define calloc hb_calloc_impl |
| 80 | #define realloc hb_realloc_impl |
| 81 | #define free hb_free_impl |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 82 | |
Behdad Esfahbod | 763f878 | 2018-07-10 13:47:41 +0200 | [diff] [blame] | 83 | #if defined(hb_memalign_impl) |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 84 | extern "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 Esfahbod | 52b4185 | 2015-10-03 13:20:55 +0100 | [diff] [blame] | 90 | #endif |
| 91 | |
| 92 | |
Behdad Esfahbod | 2aae617 | 2018-07-11 17:27:23 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Compiler attributes |
Behdad Esfahbod | 37be774 | 2018-08-06 04:51:38 -0700 | [diff] [blame] | 95 | */ |
Behdad Esfahbod | 292c100 | 2018-07-10 13:16:52 +0200 | [diff] [blame] | 96 | |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 97 | #if __cplusplus < 201103L |
| 98 | |
Behdad Esfahbod | 62e312e | 2017-10-27 09:29:20 -0600 | [diff] [blame] | 99 | #ifndef nullptr |
| 100 | #define nullptr NULL |
| 101 | #endif |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 102 | |
Behdad Esfahbod | bca8361 | 2018-07-10 12:58:13 +0200 | [diff] [blame] | 103 | #ifndef constexpr |
| 104 | #define constexpr const |
| 105 | #endif |
| 106 | |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 107 | #ifndef static_assert |
Behdad Esfahbod | 606bf57 | 2018-09-16 19:33:48 +0200 | [diff] [blame] | 108 | #define static_assert(e, msg) \ |
Behdad Esfahbod | 6f08b12 | 2017-10-26 18:23:03 -0600 | [diff] [blame] | 109 | HB_UNUSED typedef int HB_PASTE(static_assertion_failed_at_line_, __LINE__) [(e) ? 1 : -1] |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 110 | #endif // static_assert |
| 111 | |
Behdad Esfahbod | f83e992 | 2018-05-24 11:46:57 -0700 | [diff] [blame] | 112 | #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 Esfahbod | 2aae617 | 2018-07-11 17:27:23 +0200 | [diff] [blame] | 120 | template <typename T> |
| 121 | struct _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 Esfahbod | bca8361 | 2018-07-10 12:58:13 +0200 | [diff] [blame] | 130 | #ifndef alignof |
Behdad Esfahbod | 27c5317 | 2018-07-10 13:03:42 +0200 | [diff] [blame] | 131 | #define alignof(x) (_hb_alignof<x>::value) |
Behdad Esfahbod | 37be774 | 2018-08-06 04:51:38 -0700 | [diff] [blame] | 132 | #endif |
Behdad Esfahbod | bca8361 | 2018-07-10 12:58:13 +0200 | [diff] [blame] | 133 | |
Behdad Esfahbod | f9a3eab | 2018-08-12 12:21:56 -0700 | [diff] [blame] | 134 | /* 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 Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 148 | |
Ebrahim Byagowi | 76c4873 | 2017-06-02 21:53:10 +0430 | [diff] [blame] | 149 | #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__) |
| 150 | #define likely(expr) (__builtin_expect (!!(expr), 1)) |
| 151 | #define unlikely(expr) (__builtin_expect (!!(expr), 0)) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 152 | #else |
| 153 | #define likely(expr) (expr) |
| 154 | #define unlikely(expr) (expr) |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 155 | #endif |
| 156 | |
Steve Lhomme | 0b8f3ab | 2016-07-11 21:57:26 +0200 | [diff] [blame] | 157 | #if !defined(__GNUC__) && !defined(__clang__) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 158 | #undef __attribute__ |
| 159 | #define __attribute__(x) |
| 160 | #endif |
Behdad Esfahbod | a794ebf | 2009-08-06 12:32:35 -0400 | [diff] [blame] | 161 | |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 162 | #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 Esfahbod | be59f3c | 2017-12-05 09:01:28 -0800 | [diff] [blame] | 173 | #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */ |
| 174 | #define HB_UNUSED __pragma(warning(suppress: 4100 4101)) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 175 | #else |
| 176 | #define HB_UNUSED |
| 177 | #endif |
| 178 | |
| 179 | #ifndef HB_INTERNAL |
Behdad Esfahbod | 90d1c74 | 2018-02-11 19:08:59 -0800 | [diff] [blame] | 180 | # if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC) |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 181 | # define HB_INTERNAL __attribute__((__visibility__("hidden"))) |
Behdad Esfahbod | 94759d4 | 2018-07-23 15:01:39 -0700 | [diff] [blame] | 182 | # elif defined(__MINGW32__) |
Chun-wei Fan | 8e4ad1d | 2018-07-25 18:12:34 +0800 | [diff] [blame] | 183 | /* 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 Esfahbod | 94759d4 | 2018-07-23 15:01:39 -0700 | [diff] [blame] | 187 | # define HB_INTERNAL |
| 188 | #else |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 189 | # define HB_INTERNAL |
Behdad Esfahbod | b87cbe2 | 2018-02-07 14:11:26 -0500 | [diff] [blame] | 190 | # define HB_NO_VISIBILITY 1 |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 191 | # endif |
| 192 | #endif |
| 193 | |
Behdad Esfahbod | ae2b854 | 2014-06-03 16:59:09 -0400 | [diff] [blame] | 194 | #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 | |
prrace | 498e437 | 2018-06-09 16:04:28 -0700 | [diff] [blame] | 202 | #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140) |
Behdad Esfahbod | 8cef3a6 | 2018-02-09 16:04:23 -0600 | [diff] [blame] | 203 | /* https://github.com/harfbuzz/harfbuzz/issues/630 */ |
| 204 | #define __restrict |
| 205 | #endif |
| 206 | |
Behdad Esfahbod | 305d2fb | 2015-10-21 11:04:28 -0200 | [diff] [blame] | 207 | /* |
| 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 Esfahbod | f41b921 | 2018-02-05 19:51:09 -0500 | [diff] [blame] | 226 | #elif __GNUC__ >= 7 |
| 227 | /* GNU fallthrough attribute is available from GCC7 */ |
| 228 | # define HB_FALLTHROUGH __attribute__((fallthrough)) |
Behdad Esfahbod | 305d2fb | 2015-10-21 11:04:28 -0200 | [diff] [blame] | 229 | #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 Esfahbod | 62376a7 | 2018-10-14 15:20:50 -0700 | [diff] [blame] | 240 | #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 Brown | eee5b5e | 2018-11-12 21:05:39 -0500 | [diff] [blame] | 249 | #ifdef _WIN32 |
Behdad Esfahbod | db30828 | 2014-07-19 16:32:04 -0400 | [diff] [blame] | 250 | /* 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 Oliver | 24dd6c1 | 2018-09-23 18:08:30 +1000 | [diff] [blame] | 257 | # if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP) |
| 258 | # define _WIN32_WINNT 0x0600 |
| 259 | # endif |
Behdad Esfahbod | db30828 | 2014-07-19 16:32:04 -0400 | [diff] [blame] | 260 | # endif |
Behdad Esfahbod | 270971a | 2014-08-15 14:28:04 -0400 | [diff] [blame] | 261 | # ifndef WIN32_LEAN_AND_MEAN |
| 262 | # define WIN32_LEAN_AND_MEAN 1 |
| 263 | # endif |
| 264 | # ifndef STRICT |
| 265 | # define STRICT 1 |
| 266 | # endif |
Behdad Esfahbod | dabe698 | 2012-11-23 14:21:35 -0500 | [diff] [blame] | 267 | |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 268 | # if defined(_WIN32_WCE) |
| 269 | /* Some things not defined on Windows CE. */ |
Konstantin Ritt | 5069062 | 2016-04-26 12:02:26 +0400 | [diff] [blame] | 270 | # define vsnprintf _vsnprintf |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 271 | # define getenv(Name) nullptr |
Konstantin Ritt | 855a5d7 | 2015-04-10 17:18:01 +0400 | [diff] [blame] | 272 | # if _WIN32_WCE < 0x800 |
| 273 | # define setlocale(Category, Locale) "C" |
Behdad Esfahbod | 26a963b | 2014-08-10 18:04:50 -0400 | [diff] [blame] | 274 | static int errno = 0; /* Use something better? */ |
Konstantin Ritt | 855a5d7 | 2015-04-10 17:18:01 +0400 | [diff] [blame] | 275 | # endif |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 276 | # elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP) |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 277 | # define getenv(Name) nullptr |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 278 | # endif |
Behdad Esfahbod | ce01ad7 | 2015-04-01 11:05:59 -0700 | [diff] [blame] | 279 | # if defined(_MSC_VER) && _MSC_VER < 1900 |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 280 | # define snprintf _snprintf |
Konstantin Ritt | f3537b6 | 2015-01-25 09:50:51 +0400 | [diff] [blame] | 281 | # endif |
Behdad Esfahbod | 26a963b | 2014-08-10 18:04:50 -0400 | [diff] [blame] | 282 | #endif |
| 283 | |
Behdad Esfahbod | b89c7fd | 2018-11-21 12:32:48 -0500 | [diff] [blame] | 284 | #if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT) |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 285 | /* 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 Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 295 | * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx |
| 296 | * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx |
Behdad Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 297 | * mingw32 headers say atexit is safe to use in shared libraries. |
| 298 | */ |
| 299 | # define HB_USE_ATEXIT 1 |
Ebrahim Byagowi | 632713b | 2018-04-12 14:17:03 +0430 | [diff] [blame] | 300 | # 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 Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 303 | */ |
| 304 | # define HB_USE_ATEXIT 1 |
Bruce Mitchener | e8859fc | 2018-02-04 01:26:57 +0700 | [diff] [blame] | 305 | # 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 Esfahbod | 38fb30d | 2014-08-06 13:34:49 -0400 | [diff] [blame] | 311 | # endif |
| 312 | #endif |
Behdad Esfahbod | c14b24f | 2018-03-26 10:44:54 -0700 | [diff] [blame] | 313 | #ifdef HB_NO_ATEXIT |
| 314 | # undef HB_USE_ATEXIT |
| 315 | #endif |
Behdad Esfahbod | b89c7fd | 2018-11-21 12:32:48 -0500 | [diff] [blame] | 316 | #ifndef HB_USE_ATEXIT |
| 317 | # define HB_USE_ATEXIT 0 |
| 318 | #endif |
Behdad Esfahbod | dabe698 | 2012-11-23 14:21:35 -0500 | [diff] [blame] | 319 | |
Behdad Esfahbod | 35a7383 | 2009-08-01 19:30:31 -0400 | [diff] [blame] | 320 | #define HB_STMT_START do |
| 321 | #define HB_STMT_END while (0) |
Behdad Esfahbod | 5b3f770 | 2006-12-28 06:42:37 -0500 | [diff] [blame] | 322 | |
Behdad Esfahbod | 19e0091 | 2018-08-06 04:54:31 -0700 | [diff] [blame] | 323 | /* Static-assert as expression. */ |
Behdad Esfahbod | 76dcbf8 | 2017-10-15 11:24:35 +0200 | [diff] [blame] | 324 | template <unsigned int cond> class hb_assert_constant_t; |
| 325 | template <> class hb_assert_constant_t<1> {}; |
Behdad Esfahbod | 672ca3b | 2015-10-26 14:05:05 -0700 | [diff] [blame] | 326 | #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>)) |
Behdad Esfahbod | dcb7026 | 2011-04-21 16:34:22 -0400 | [diff] [blame] | 327 | |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 328 | /* Lets assert int types. Saves trouble down the road. */ |
Behdad Esfahbod | 606bf57 | 2018-09-16 19:33:48 +0200 | [diff] [blame] | 329 | static_assert ((sizeof (int8_t) == 1), ""); |
| 330 | static_assert ((sizeof (uint8_t) == 1), ""); |
| 331 | static_assert ((sizeof (int16_t) == 2), ""); |
| 332 | static_assert ((sizeof (uint16_t) == 2), ""); |
| 333 | static_assert ((sizeof (int32_t) == 4), ""); |
| 334 | static_assert ((sizeof (uint32_t) == 4), ""); |
| 335 | static_assert ((sizeof (int64_t) == 8), ""); |
| 336 | static_assert ((sizeof (uint64_t) == 8), ""); |
| 337 | static_assert ((sizeof (hb_codepoint_t) == 4), ""); |
| 338 | static_assert ((sizeof (hb_position_t) == 4), ""); |
| 339 | static_assert ((sizeof (hb_mask_t) == 4), ""); |
| 340 | static_assert ((sizeof (hb_var_int_t) == 4), ""); |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 341 | |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 342 | |
Behdad Esfahbod | c7c5df9 | 2018-10-29 15:16:52 -0700 | [diff] [blame] | 343 | #if __cplusplus >= 201103L |
| 344 | |
Behdad Esfahbod | 67a22f3 | 2018-10-29 17:37:41 -0700 | [diff] [blame] | 345 | /* 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 Esfahbod | ea0e51d | 2018-10-29 16:00:23 -0700 | [diff] [blame] | 355 | #define HB_NO_CREATE_COPY_ASSIGN(TypeName) \ |
| 356 | TypeName(void); \ |
Behdad Esfahbod | fb07d1a | 2018-06-01 17:32:07 -0700 | [diff] [blame] | 357 | TypeName(const TypeName&); \ |
| 358 | void operator=(const TypeName&) |
Behdad Esfahbod | ea0e51d | 2018-10-29 16:00:23 -0700 | [diff] [blame] | 359 | #define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE(TypeName, T) \ |
| 360 | TypeName(void); \ |
Behdad Esfahbod | a256a92 | 2018-10-29 11:25:35 -0700 | [diff] [blame] | 361 | TypeName(const TypeName<T>&); \ |
| 362 | void operator=(const TypeName<T>&) |
Behdad Esfahbod | ea0e51d | 2018-10-29 16:00:23 -0700 | [diff] [blame] | 363 | #define HB_NO_CREATE_COPY_ASSIGN_TEMPLATE2(TypeName, T1, T2) \ |
| 364 | TypeName(void); \ |
Behdad Esfahbod | a256a92 | 2018-10-29 11:25:35 -0700 | [diff] [blame] | 365 | TypeName(const TypeName<T1, T2>&); \ |
Ebrahim Byagowi | f7a08cd | 2018-10-30 11:29:09 +0330 | [diff] [blame] | 366 | void operator=(const TypeName<T1, T2>&) |
Behdad Esfahbod | c7c5df9 | 2018-10-29 15:16:52 -0700 | [diff] [blame] | 367 | |
| 368 | #else /* __cpluspplus >= 201103L */ |
| 369 | |
Behdad Esfahbod | 67a22f3 | 2018-10-29 17:37:41 -0700 | [diff] [blame] | 370 | #define HB_NO_COPY_ASSIGN(TypeName) |
Behdad Esfahbod | ae9ad10 | 2018-11-03 21:41:50 -0400 | [diff] [blame] | 371 | #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 Esfahbod | c7c5df9 | 2018-10-29 15:16:52 -0700 | [diff] [blame] | 375 | |
| 376 | #endif /* __cpluspplus >= 201103L */ |
Behdad Esfahbod | 6c22276 | 2018-05-25 16:21:27 -0700 | [diff] [blame] | 377 | |
Behdad Esfahbod | 44999f8 | 2018-07-11 17:00:59 +0200 | [diff] [blame] | 378 | |
| 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 Esfahbod | 606bf57 | 2018-09-16 19:33:48 +0200 | [diff] [blame] | 402 | static_assert (0 == (HB_VECTOR_SIZE & (HB_VECTOR_SIZE - 1)), "HB_VECTOR_SIZE is not power of 2."); |
| 403 | static_assert (0 == (HB_VECTOR_SIZE % 64), "HB_VECTOR_SIZE is not multiple of 64."); |
Behdad Esfahbod | 44999f8 | 2018-07-11 17:00:59 +0200 | [diff] [blame] | 404 | #if HB_VECTOR_SIZE |
| 405 | typedef uint64_t hb_vector_size_impl_t __attribute__((vector_size (HB_VECTOR_SIZE / 8))); |
| 406 | #else |
| 407 | typedef uint64_t hb_vector_size_impl_t; |
| 408 | #endif |
| 409 | |
| 410 | |
Behdad Esfahbod | 91dd115 | 2016-02-25 13:56:47 +0900 | [diff] [blame] | 411 | /* 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 Esfahbod | bd021a6 | 2018-05-08 01:50:30 -0700 | [diff] [blame] | 417 | #define HB_NDEBUG 1 |
Behdad Esfahbod | 91dd115 | 2016-02-25 13:56:47 +0900 | [diff] [blame] | 418 | #endif |
| 419 | |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 420 | |
Behdad Esfahbod | be336da | 2018-08-06 04:32:51 -0700 | [diff] [blame] | 421 | /* Flags */ |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 422 | |
Behdad Esfahbod | aa7044d | 2015-11-04 16:25:57 -0800 | [diff] [blame] | 423 | /* Enable bitwise ops on enums marked as flags_t */ |
Behdad Esfahbod | 6986208 | 2015-11-04 18:46:22 -0800 | [diff] [blame] | 424 | /* 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 Esfahbod | e0082ae | 2015-11-17 18:42:13 -0800 | [diff] [blame] | 426 | * originally had in mind... :(. |
| 427 | * |
ebraminio | 7c6937e | 2017-11-20 14:49:22 -0500 | [diff] [blame] | 428 | * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163 |
Behdad Esfahbod | e0082ae | 2015-11-17 18:42:13 -0800 | [diff] [blame] | 429 | */ |
| 430 | #ifdef _MSC_VER |
| 431 | # pragma warning(disable:4200) |
| 432 | # pragma warning(disable:4800) |
Chun-wei Fan | 167c327 | 2015-11-09 17:17:56 +0800 | [diff] [blame] | 433 | #endif |
Behdad Esfahbod | 1dc32ea | 2015-11-20 13:24:19 -0800 | [diff] [blame] | 434 | #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 Esfahbod | aa7044d | 2015-11-04 16:25:57 -0800 | [diff] [blame] | 444 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 445 | /* Useful for set-operations on small enums. |
| 446 | * For example, for testing "x ∈ {x1, x2, x3}" use: |
Behdad Esfahbod | 6058f98 | 2017-10-19 11:39:52 -0700 | [diff] [blame] | 447 | * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 448 | */ |
Behdad Esfahbod | 3d2b98e | 2018-11-23 10:45:44 -0500 | [diff] [blame] | 449 | #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 Esfahbod | 2c372b8 | 2012-07-20 13:37:48 -0400 | [diff] [blame] | 451 | #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x)) |
Behdad Esfahbod | 3d2b98e | 2018-11-23 10:45:44 -0500 | [diff] [blame] | 452 | #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 Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 454 | |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 455 | |
Steven R. Loomis | a13b023 | 2015-12-11 10:21:27 -0800 | [diff] [blame] | 456 | /* Size signifying variable-sized array */ |
| 457 | #define VAR 1 |
Behdad Esfahbod | bab02d3 | 2013-02-12 15:26:45 -0500 | [diff] [blame] | 458 | |
Behdad Esfahbod | e1a37f3 | 2017-10-30 11:42:28 -0600 | [diff] [blame] | 459 | |
Chun-wei Fan | 19256be | 2018-03-12 13:33:03 +0800 | [diff] [blame] | 460 | /* fallback for round() */ |
Chun-wei Fan | 19256be | 2018-03-12 13:33:03 +0800 | [diff] [blame] | 461 | static inline double |
Behdad Esfahbod | 01dff1e | 2018-06-26 18:00:58 -0400 | [diff] [blame] | 462 | _hb_round (double x) |
Chun-wei Fan | 19256be | 2018-03-12 13:33:03 +0800 | [diff] [blame] | 463 | { |
| 464 | if (x >= 0) |
| 465 | return floor (x + 0.5); |
| 466 | else |
| 467 | return ceil (x - 0.5); |
| 468 | } |
Behdad Esfahbod | 292c100 | 2018-07-10 13:16:52 +0200 | [diff] [blame] | 469 | #if !defined (HAVE_ROUND) && !defined (HAVE_DECL_ROUND) |
Behdad Esfahbod | 01dff1e | 2018-06-26 18:00:58 -0400 | [diff] [blame] | 470 | #define round(x) _hb_round(x) |
Chun-wei Fan | 19256be | 2018-03-12 13:33:03 +0800 | [diff] [blame] | 471 | #endif |
| 472 | |
| 473 | |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 474 | /* fallback for posix_memalign() */ |
| 475 | static inline int |
| 476 | _hb_memalign(void **memptr, size_t alignment, size_t size) |
| 477 | { |
Behdad Esfahbod | 491d93b | 2018-07-10 16:03:31 +0200 | [diff] [blame] | 478 | if (unlikely (0 != (alignment & (alignment - 1)) || |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 479 | !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 Esfahbod | bdb53ca | 2018-10-11 20:20:00 -0400 | [diff] [blame] | 500 | /* |
| 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 Esfahbod | 07386ea | 2018-10-22 21:18:27 -0700 | [diff] [blame] | 508 | /* Some really basic things everyone wants. */ |
| 509 | template <typename T> struct hb_remove_const { typedef T value; }; |
| 510 | template <typename T> struct hb_remove_const<const T> { typedef T value; }; |
Behdad Esfahbod | f99abcc | 2018-11-24 00:22:21 -0500 | [diff] [blame^] | 511 | #define hb_remove_const(T) hb_remove_const<T>::value |
Behdad Esfahbod | 07386ea | 2018-10-22 21:18:27 -0700 | [diff] [blame] | 512 | template <typename T> struct hb_remove_reference { typedef T value; }; |
| 513 | template <typename T> struct hb_remove_reference<T &> { typedef T value; }; |
Behdad Esfahbod | f99abcc | 2018-11-24 00:22:21 -0500 | [diff] [blame^] | 514 | #define hb_remove_reference(T) hb_remove_reference<T>::value |
Behdad Esfahbod | 07386ea | 2018-10-22 21:18:27 -0700 | [diff] [blame] | 515 | template <typename T> struct hb_remove_pointer { typedef T value; }; |
| 516 | template <typename T> struct hb_remove_pointer<T *> { typedef T value; }; |
Behdad Esfahbod | f99abcc | 2018-11-24 00:22:21 -0500 | [diff] [blame^] | 517 | #define hb_remove_pointer(T) hb_remove_pointer<T>::value |
Behdad Esfahbod | 07386ea | 2018-10-22 21:18:27 -0700 | [diff] [blame] | 518 | |
| 519 | |
Behdad Esfahbod | 25aa411 | 2018-07-10 16:05:03 +0200 | [diff] [blame] | 520 | /* Headers we include for everyone. Keep sorted. They express dependency amongst |
| 521 | * themselves, but no other file should include them.*/ |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 522 | #include "hb-atomic.hh" |
Behdad Esfahbod | 491d93b | 2018-07-10 16:03:31 +0200 | [diff] [blame] | 523 | #include "hb-debug.hh" |
Behdad Esfahbod | 25aa411 | 2018-07-10 16:05:03 +0200 | [diff] [blame] | 524 | #include "hb-dsalgs.hh" |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 525 | #include "hb-mutex.hh" |
Behdad Esfahbod | 25147ff | 2018-08-06 05:01:52 -0700 | [diff] [blame] | 526 | #include "hb-null.hh" |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 527 | #include "hb-object.hh" |
Behdad Esfahbod | 83ea277 | 2018-07-10 13:17:27 +0200 | [diff] [blame] | 528 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 529 | #endif /* HB_HH */ |