| 1 | /* |
| 2 | * Copyright © 2007,2008,2009 Red Hat, Inc. |
| 3 | * Copyright © 2011,2012 Google, Inc. |
| 4 | * |
| 5 | * This is part of HarfBuzz, a text shaping library. |
| 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 |
| 26 | * Google Author(s): Behdad Esfahbod |
| 27 | */ |
| 28 | |
| 29 | #ifndef HB_HH |
| 30 | #define HB_HH |
| 31 | |
| 32 | |
| 33 | #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC |
| 34 | #ifdef _MSC_VER |
| 35 | #pragma warning( disable: 4068 ) /* Unknown pragma */ |
| 36 | #endif |
| 37 | #if defined(__GNUC__) || defined(__clang__) |
| 38 | /* Rules: |
| 39 | * |
| 40 | * - All pragmas are declared GCC even if they are clang ones. Otherwise GCC |
| 41 | * nags, even though we instruct it to ignore -Wunknown-pragmas. ¯\_(ツ)_/¯ |
| 42 | * |
| 43 | * - Within each category, keep sorted. |
| 44 | * |
| 45 | * - Warnings whose scope can be expanded in future compiler versions shall |
| 46 | * be declared as "warning". Otherwise, either ignored or error. |
| 47 | */ |
| 48 | |
| 49 | /* Setup. Don't sort order within this category. */ |
| 50 | #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING |
| 51 | #pragma GCC diagnostic warning "-Wall" |
| 52 | #pragma GCC diagnostic warning "-Wextra" |
| 53 | #endif |
| 54 | #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED |
| 55 | #pragma GCC diagnostic ignored "-Wpragmas" |
| 56 | #pragma GCC diagnostic ignored "-Wunknown-pragmas" |
| 57 | #pragma GCC diagnostic ignored "-Wunknown-warning-option" |
| 58 | #endif |
| 59 | #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING |
| 60 | //#pragma GCC diagnostic warning "-Weverything" |
| 61 | #endif |
| 62 | |
| 63 | /* Error. Should never happen. */ |
| 64 | #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_ERROR |
| 65 | #pragma GCC diagnostic error "-Wc++11-narrowing" |
| 66 | #pragma GCC diagnostic error "-Wcast-align" |
| 67 | #pragma GCC diagnostic error "-Wcast-function-type" |
| 68 | #pragma GCC diagnostic error "-Wdelete-non-virtual-dtor" |
| 69 | #pragma GCC diagnostic error "-Wembedded-directive" |
| 70 | #pragma GCC diagnostic error "-Wextra-semi-stmt" |
| 71 | #pragma GCC diagnostic error "-Wformat-security" |
| 72 | #pragma GCC diagnostic error "-Wimplicit-function-declaration" |
| 73 | #pragma GCC diagnostic error "-Winit-self" |
| 74 | #pragma GCC diagnostic error "-Winjected-class-name" |
| 75 | #pragma GCC diagnostic error "-Wmissing-braces" |
| 76 | #pragma GCC diagnostic error "-Wmissing-declarations" |
| 77 | #pragma GCC diagnostic error "-Wmissing-prototypes" |
| 78 | #pragma GCC diagnostic error "-Wnested-externs" |
| 79 | #pragma GCC diagnostic error "-Wold-style-definition" |
| 80 | #pragma GCC diagnostic error "-Wpointer-arith" |
| 81 | #pragma GCC diagnostic error "-Wredundant-decls" |
| 82 | #pragma GCC diagnostic error "-Wreorder" |
| 83 | #pragma GCC diagnostic error "-Wsign-compare" |
| 84 | #pragma GCC diagnostic error "-Wstrict-prototypes" |
| 85 | #pragma GCC diagnostic error "-Wstring-conversion" |
| 86 | #pragma GCC diagnostic error "-Wswitch-enum" |
| 87 | #pragma GCC diagnostic error "-Wtautological-overlap-compare" |
| 88 | #pragma GCC diagnostic error "-Wunneeded-internal-declaration" |
| 89 | #pragma GCC diagnostic error "-Wunused" |
| 90 | #pragma GCC diagnostic error "-Wunused-local-typedefs" |
| 91 | #pragma GCC diagnostic error "-Wunused-value" |
| 92 | #pragma GCC diagnostic error "-Wunused-variable" |
| 93 | #pragma GCC diagnostic error "-Wvla" |
| 94 | #pragma GCC diagnostic error "-Wwrite-strings" |
| 95 | #endif |
| 96 | |
| 97 | /* Warning. To be investigated if happens. */ |
| 98 | #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_WARNING |
| 99 | #pragma GCC diagnostic warning "-Wbuiltin-macro-redefined" |
| 100 | #pragma GCC diagnostic warning "-Wdeprecated" |
| 101 | #pragma GCC diagnostic warning "-Wdeprecated-declarations" |
| 102 | #pragma GCC diagnostic warning "-Wdisabled-optimization" |
| 103 | #pragma GCC diagnostic warning "-Wdouble-promotion" |
| 104 | #pragma GCC diagnostic warning "-Wformat=2" |
| 105 | #pragma GCC diagnostic warning "-Wignored-pragma-optimize" |
| 106 | #pragma GCC diagnostic warning "-Wlogical-op" |
| 107 | #pragma GCC diagnostic warning "-Wmaybe-uninitialized" |
| 108 | #pragma GCC diagnostic warning "-Wmissing-format-attribute" |
| 109 | #pragma GCC diagnostic warning "-Wundef" |
| 110 | #endif |
| 111 | |
| 112 | /* Ignored currently, but should be fixed at some point. */ |
| 113 | #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED |
| 114 | #pragma GCC diagnostic ignored "-Wconversion" // TODO fix |
| 115 | #pragma GCC diagnostic ignored "-Wformat-signedness" // TODO fix |
| 116 | #pragma GCC diagnostic ignored "-Wshadow" // TODO fix |
| 117 | #pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations" // TODO fix |
| 118 | #pragma GCC diagnostic ignored "-Wunused-parameter" // TODO fix |
| 119 | #endif |
| 120 | |
| 121 | /* Ignored intentionally. */ |
| 122 | #ifndef HB_NO_PRAGMA_GCC_DIAGNOSTIC_IGNORED |
| 123 | #pragma GCC diagnostic ignored "-Wclass-memaccess" |
| 124 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
| 125 | #pragma GCC diagnostic ignored "-Wformat-zero-length" |
| 126 | #pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
| 127 | #pragma GCC diagnostic ignored "-Wpacked" // Erratic impl in clang |
| 128 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
| 129 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 130 | #pragma GCC diagnostic ignored "-Wc++11-compat" // only gcc raises it |
| 131 | #endif |
| 132 | |
| 133 | #endif |
| 134 | #endif |
| 135 | |
| 136 | |
| 137 | #include "hb-config.hh" |
| 138 | |
| 139 | |
| 140 | /* |
| 141 | * Following added based on what AC_USE_SYSTEM_EXTENSIONS adds to |
| 142 | * config.h.in. Copied here for the convenience of those embedding |
| 143 | * HarfBuzz and not using our build system. |
| 144 | */ |
| 145 | /* Enable extensions on AIX 3, Interix. */ |
| 146 | #ifndef _ALL_SOURCE |
| 147 | # define _ALL_SOURCE 1 |
| 148 | #endif |
| 149 | /* Enable GNU extensions on systems that have them. */ |
| 150 | #ifndef _GNU_SOURCE |
| 151 | # define _GNU_SOURCE 1 |
| 152 | #endif |
| 153 | /* Enable threading extensions on Solaris. */ |
| 154 | #ifndef _POSIX_PTHREAD_SEMANTICS |
| 155 | # define _POSIX_PTHREAD_SEMANTICS 1 |
| 156 | #endif |
| 157 | /* Enable extensions on HP NonStop. */ |
| 158 | #ifndef _TANDEM_SOURCE |
| 159 | # define _TANDEM_SOURCE 1 |
| 160 | #endif |
| 161 | /* Enable general extensions on Solaris. */ |
| 162 | #ifndef __EXTENSIONS__ |
| 163 | # define __EXTENSIONS__ 1 |
| 164 | #endif |
| 165 | |
| 166 | #if defined (_MSC_VER) && defined (HB_DLL_EXPORT) |
| 167 | #define HB_EXTERN __declspec (dllexport) extern |
| 168 | #endif |
| 169 | |
| 170 | #include "hb.h" |
| 171 | #define HB_H_IN |
| 172 | #include "hb-ot.h" |
| 173 | #define HB_OT_H_IN |
| 174 | #include "hb-aat.h" |
| 175 | #define HB_AAT_H_IN |
| 176 | |
| 177 | #include <limits.h> |
| 178 | #include <math.h> |
| 179 | #include <float.h> |
| 180 | #include <stdlib.h> |
| 181 | #include <stddef.h> |
| 182 | #include <string.h> |
| 183 | #include <assert.h> |
| 184 | #include <stdio.h> |
| 185 | #include <stdarg.h> |
| 186 | |
| 187 | #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__) |
| 188 | #ifdef __MINGW32_VERSION |
| 189 | #ifndef WIN32_LEAN_AND_MEAN |
| 190 | #define WIN32_LEAN_AND_MEAN 1 |
| 191 | #endif |
| 192 | #else |
| 193 | #include <intrin.h> |
| 194 | #endif |
| 195 | #endif |
| 196 | |
| 197 | #ifdef _WIN32 |
| 198 | #include <windows.h> |
| 199 | #include <winapifamily.h> |
| 200 | #endif |
| 201 | |
| 202 | #define HB_PASTE1(a,b) a##b |
| 203 | #define HB_PASTE(a,b) HB_PASTE1(a,b) |
| 204 | |
| 205 | |
| 206 | /* Compile-time custom allocator support. */ |
| 207 | |
| 208 | #if !defined(HB_CUSTOM_MALLOC) \ |
| 209 | && defined(hb_malloc_impl) \ |
| 210 | && defined(hb_calloc_impl) \ |
| 211 | && defined(hb_realloc_impl) \ |
| 212 | && defined(hb_free_impl) |
| 213 | #define HB_CUSTOM_MALLOC |
| 214 | #endif |
| 215 | |
| 216 | #ifdef HB_CUSTOM_MALLOC |
| 217 | extern "C" void* hb_malloc_impl(size_t size); |
| 218 | extern "C" void* hb_calloc_impl(size_t nmemb, size_t size); |
| 219 | extern "C" void* hb_realloc_impl(void *ptr, size_t size); |
| 220 | extern "C" void hb_free_impl(void *ptr); |
| 221 | #define malloc hb_malloc_impl |
| 222 | #define calloc hb_calloc_impl |
| 223 | #define realloc hb_realloc_impl |
| 224 | #define free hb_free_impl |
| 225 | #endif |
| 226 | |
| 227 | |
| 228 | /* |
| 229 | * Compiler attributes |
| 230 | */ |
| 231 | |
| 232 | #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__) |
| 233 | #define likely(expr) (__builtin_expect (!!(expr), 1)) |
| 234 | #define unlikely(expr) (__builtin_expect (!!(expr), 0)) |
| 235 | #else |
| 236 | #define likely(expr) (expr) |
| 237 | #define unlikely(expr) (expr) |
| 238 | #endif |
| 239 | |
| 240 | #if !defined(__GNUC__) && !defined(__clang__) |
| 241 | #undef __attribute__ |
| 242 | #define __attribute__(x) |
| 243 | #endif |
| 244 | |
| 245 | #if defined(__GNUC__) && (__GNUC__ >= 3) |
| 246 | #define HB_PURE_FUNC __attribute__((pure)) |
| 247 | #define HB_CONST_FUNC __attribute__((const)) |
| 248 | #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) |
| 249 | #else |
| 250 | #define HB_PURE_FUNC |
| 251 | #define HB_CONST_FUNC |
| 252 | #define HB_PRINTF_FUNC(format_idx, arg_idx) |
| 253 | #endif |
| 254 | #if defined(__GNUC__) && (__GNUC__ >= 4) || (__clang__) |
| 255 | #define HB_UNUSED __attribute__((unused)) |
| 256 | #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */ |
| 257 | #define HB_UNUSED __pragma(warning(suppress: 4100 4101)) |
| 258 | #else |
| 259 | #define HB_UNUSED |
| 260 | #endif |
| 261 | |
| 262 | #ifndef HB_INTERNAL |
| 263 | # if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC) |
| 264 | # define HB_INTERNAL __attribute__((__visibility__("hidden"))) |
| 265 | # elif defined(__MINGW32__) |
| 266 | /* We use -export-symbols on mingw32, since it does not support visibility attributes. */ |
| 267 | # define HB_INTERNAL |
| 268 | # elif defined (_MSC_VER) && defined (HB_DLL_EXPORT) |
| 269 | /* We do not try to export internal symbols on Visual Studio */ |
| 270 | # define HB_INTERNAL |
| 271 | #else |
| 272 | # define HB_INTERNAL |
| 273 | # define HB_NO_VISIBILITY 1 |
| 274 | # endif |
| 275 | #endif |
| 276 | |
| 277 | /* https://github.com/harfbuzz/harfbuzz/issues/1651 */ |
| 278 | #if defined(__clang__) && __clang_major__ < 10 |
| 279 | #define static_const static |
| 280 | #else |
| 281 | #define static_const static const |
| 282 | #endif |
| 283 | |
| 284 | #if defined(__GNUC__) && (__GNUC__ >= 3) |
| 285 | #define HB_FUNC __PRETTY_FUNCTION__ |
| 286 | #elif defined(_MSC_VER) |
| 287 | #define HB_FUNC __FUNCSIG__ |
| 288 | #else |
| 289 | #define HB_FUNC __func__ |
| 290 | #endif |
| 291 | |
| 292 | #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140) |
| 293 | /* https://github.com/harfbuzz/harfbuzz/issues/630 */ |
| 294 | #define __restrict |
| 295 | #endif |
| 296 | |
| 297 | /* |
| 298 | * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411 |
| 299 | * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch |
| 300 | * cases that fall through without a break or return statement. HB_FALLTHROUGH |
| 301 | * is only needed on cases that have code: |
| 302 | * |
| 303 | * switch (foo) { |
| 304 | * case 1: // These cases have no code. No fallthrough annotations are needed. |
| 305 | * case 2: |
| 306 | * case 3: |
| 307 | * foo = 4; // This case has code, so a fallthrough annotation is needed: |
| 308 | * HB_FALLTHROUGH; |
| 309 | * default: |
| 310 | * return foo; |
| 311 | * } |
| 312 | */ |
| 313 | #if defined(__clang__) && __cplusplus >= 201103L |
| 314 | /* clang's fallthrough annotations are only available starting in C++11. */ |
| 315 | # define HB_FALLTHROUGH [[clang::fallthrough]] |
| 316 | #elif defined(__GNUC__) && (__GNUC__ >= 7) |
| 317 | /* GNU fallthrough attribute is available from GCC7 */ |
| 318 | # define HB_FALLTHROUGH __attribute__((fallthrough)) |
| 319 | #elif defined(_MSC_VER) |
| 320 | /* |
| 321 | * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis): |
| 322 | * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx |
| 323 | */ |
| 324 | # include <sal.h> |
| 325 | # define HB_FALLTHROUGH __fallthrough |
| 326 | #else |
| 327 | # define HB_FALLTHROUGH /* FALLTHROUGH */ |
| 328 | #endif |
| 329 | |
| 330 | /* A tag to enforce use of return value for a function */ |
| 331 | #if __cplusplus >= 201703L |
| 332 | # define HB_NODISCARD [[nodiscard]] |
| 333 | #elif defined(__GNUC__) || defined(__clang__) |
| 334 | # define HB_NODISCARD __attribute__((warn_unused_result)) |
| 335 | #elif defined(_MSC_VER) |
| 336 | # define HB_NODISCARD _Check_return_ |
| 337 | #else |
| 338 | # define HB_NODISCARD |
| 339 | #endif |
| 340 | #define hb_success_t HB_NODISCARD bool |
| 341 | |
| 342 | /* https://github.com/harfbuzz/harfbuzz/issues/1852 */ |
| 343 | #if defined(__clang__) && !(defined(_AIX) && (defined(__IBMCPP__) || defined(__ibmxl__))) |
| 344 | /* Disable certain sanitizer errors. */ |
| 345 | /* https://github.com/harfbuzz/harfbuzz/issues/1247 */ |
| 346 | #define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow"))) |
| 347 | #else |
| 348 | #define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW |
| 349 | #endif |
| 350 | |
| 351 | |
| 352 | #ifdef _WIN32 |
| 353 | /* We need Windows Vista for both Uniscribe backend and for |
| 354 | * MemoryBarrier. We don't support compiling on Windows XP, |
| 355 | * though we run on it fine. */ |
| 356 | # if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600 |
| 357 | # undef _WIN32_WINNT |
| 358 | # endif |
| 359 | # ifndef _WIN32_WINNT |
| 360 | # if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 361 | # define _WIN32_WINNT 0x0600 |
| 362 | # endif |
| 363 | # endif |
| 364 | # ifndef WIN32_LEAN_AND_MEAN |
| 365 | # define WIN32_LEAN_AND_MEAN 1 |
| 366 | # endif |
| 367 | # ifndef STRICT |
| 368 | # define STRICT 1 |
| 369 | # endif |
| 370 | |
| 371 | # if defined(_WIN32_WCE) |
| 372 | /* Some things not defined on Windows CE. */ |
| 373 | # define vsnprintf _vsnprintf |
| 374 | # ifndef HB_NO_GETENV |
| 375 | # define HB_NO_GETENV |
| 376 | # endif |
| 377 | # if _WIN32_WCE < 0x800 |
| 378 | # define HB_NO_SETLOCALE |
| 379 | # define HB_NO_ERRNO |
| 380 | # endif |
| 381 | # elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 382 | # ifndef HB_NO_GETENV |
| 383 | # define HB_NO_GETENV |
| 384 | # endif |
| 385 | # endif |
| 386 | # if defined(_MSC_VER) && _MSC_VER < 1900 |
| 387 | # define snprintf _snprintf |
| 388 | # endif |
| 389 | #endif |
| 390 | |
| 391 | #ifdef HB_NO_GETENV |
| 392 | #define getenv(Name) nullptr |
| 393 | #endif |
| 394 | |
| 395 | #ifndef HB_NO_ERRNO |
| 396 | # include <errno.h> |
| 397 | #else |
| 398 | static int HB_UNUSED _hb_errno = 0; |
| 399 | # undef errno |
| 400 | # define errno _hb_errno |
| 401 | #endif |
| 402 | |
| 403 | #if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT) |
| 404 | /* atexit() is only safe to be called from shared libraries on certain |
| 405 | * platforms. Whitelist. |
| 406 | * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */ |
| 407 | # if defined(__linux) && defined(__GLIBC_PREREQ) |
| 408 | # if __GLIBC_PREREQ(2,3) |
| 409 | /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */ |
| 410 | # define HB_USE_ATEXIT 1 |
| 411 | # endif |
| 412 | # elif defined(_MSC_VER) || defined(__MINGW32__) |
| 413 | /* For MSVC: |
| 414 | * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx |
| 415 | * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx |
| 416 | * mingw32 headers say atexit is safe to use in shared libraries. |
| 417 | */ |
| 418 | # define HB_USE_ATEXIT 1 |
| 419 | # elif defined(__ANDROID__) |
| 420 | /* This is available since Android NKD r8 or r8b: |
| 421 | * https://issuetracker.google.com/code/p/android/issues/detail?id=6455 |
| 422 | */ |
| 423 | # define HB_USE_ATEXIT 1 |
| 424 | # elif defined(__APPLE__) |
| 425 | /* For macOS and related platforms, the atexit man page indicates |
| 426 | * that it will be invoked when the library is unloaded, not only |
| 427 | * at application exit. |
| 428 | */ |
| 429 | # define HB_USE_ATEXIT 1 |
| 430 | # endif |
| 431 | #endif |
| 432 | #ifdef HB_NO_ATEXIT |
| 433 | # undef HB_USE_ATEXIT |
| 434 | #endif |
| 435 | #ifndef HB_USE_ATEXIT |
| 436 | # define HB_USE_ATEXIT 0 |
| 437 | #endif |
| 438 | |
| 439 | #define HB_STMT_START do |
| 440 | #define HB_STMT_END while (0) |
| 441 | |
| 442 | /* Static-assert as expression. */ |
| 443 | template <unsigned int cond> class hb_assert_constant_t; |
| 444 | template <> class hb_assert_constant_t<1> {}; |
| 445 | #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>)) |
| 446 | |
| 447 | /* Lets assert int types. Saves trouble down the road. */ |
| 448 | static_assert ((sizeof (int8_t) == 1), "" ); |
| 449 | static_assert ((sizeof (uint8_t) == 1), "" ); |
| 450 | static_assert ((sizeof (int16_t) == 2), "" ); |
| 451 | static_assert ((sizeof (uint16_t) == 2), "" ); |
| 452 | static_assert ((sizeof (int32_t) == 4), "" ); |
| 453 | static_assert ((sizeof (uint32_t) == 4), "" ); |
| 454 | static_assert ((sizeof (int64_t) == 8), "" ); |
| 455 | static_assert ((sizeof (uint64_t) == 8), "" ); |
| 456 | static_assert ((sizeof (hb_codepoint_t) == 4), "" ); |
| 457 | static_assert ((sizeof (hb_position_t) == 4), "" ); |
| 458 | static_assert ((sizeof (hb_mask_t) == 4), "" ); |
| 459 | static_assert ((sizeof (hb_var_int_t) == 4), "" ); |
| 460 | |
| 461 | #define HB_DELETE_COPY_ASSIGN(TypeName) \ |
| 462 | TypeName(const TypeName&) = delete; \ |
| 463 | void operator=(const TypeName&) = delete |
| 464 | #define HB_DELETE_CREATE_COPY_ASSIGN(TypeName) \ |
| 465 | TypeName() = delete; \ |
| 466 | TypeName(const TypeName&) = delete; \ |
| 467 | void operator=(const TypeName&) = delete |
| 468 | |
| 469 | |
| 470 | /* Flags */ |
| 471 | |
| 472 | /* Enable bitwise ops on enums marked as flags_t */ |
| 473 | /* To my surprise, looks like the function resolver is happy to silently cast |
| 474 | * one enum to another... So this doesn't provide the type-checking that I |
| 475 | * originally had in mind... :(. |
| 476 | * |
| 477 | * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163 |
| 478 | */ |
| 479 | #ifdef _MSC_VER |
| 480 | # pragma warning(disable:4200) |
| 481 | # pragma warning(disable:4800) |
| 482 | #endif |
| 483 | #define HB_MARK_AS_FLAG_T(T) \ |
| 484 | extern "C++" { \ |
| 485 | static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \ |
| 486 | static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \ |
| 487 | static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \ |
| 488 | static inline T operator ~ (T r) { return T (~(unsigned int) r); } \ |
| 489 | static inline T& operator |= (T &l, T r) { l = l | r; return l; } \ |
| 490 | static inline T& operator &= (T& l, T r) { l = l & r; return l; } \ |
| 491 | static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \ |
| 492 | } \ |
| 493 | static_assert (true, "") |
| 494 | |
| 495 | /* Useful for set-operations on small enums. |
| 496 | * For example, for testing "x ∈ {x1, x2, x3}" use: |
| 497 | * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) |
| 498 | */ |
| 499 | #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x))) |
| 500 | #define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0) |
| 501 | #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x)) |
| 502 | #define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x))) |
| 503 | #define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0) |
| 504 | |
| 505 | |
| 506 | /* Size signifying variable-sized array */ |
| 507 | #ifndef HB_VAR_ARRAY |
| 508 | #define HB_VAR_ARRAY 1 |
| 509 | #endif |
| 510 | |
| 511 | static inline float |
| 512 | _hb_roundf (float x) { return floorf (x + .5f); } |
| 513 | #define roundf(x) _hb_roundf(x) |
| 514 | |
| 515 | /* Endian swap, used in Windows related backends */ |
| 516 | static inline uint16_t hb_uint16_swap (const uint16_t v) |
| 517 | { return (v >> 8) | (v << 8); } |
| 518 | static inline uint32_t hb_uint32_swap (const uint32_t v) |
| 519 | { return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); } |
| 520 | |
| 521 | /* |
| 522 | * Big-endian integers. Here because fundamental. |
| 523 | */ |
| 524 | |
| 525 | template <typename Type, int Bytes> struct BEInt; |
| 526 | |
| 527 | template <typename Type> |
| 528 | struct BEInt<Type, 1> |
| 529 | { |
| 530 | public: |
| 531 | BEInt<Type, 1>& operator = (Type V) |
| 532 | { |
| 533 | v = V; |
| 534 | return *this; |
| 535 | } |
| 536 | operator Type () const { return v; } |
| 537 | private: uint8_t v; |
| 538 | }; |
| 539 | template <typename Type> |
| 540 | struct BEInt<Type, 2> |
| 541 | { |
| 542 | public: |
| 543 | BEInt<Type, 2>& operator = (Type V) |
| 544 | { |
| 545 | v[0] = (V >> 8) & 0xFF; |
| 546 | v[1] = (V ) & 0xFF; |
| 547 | return *this; |
| 548 | } |
| 549 | operator Type () const |
| 550 | { |
| 551 | #if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \ |
| 552 | defined(__BYTE_ORDER) && \ |
| 553 | (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN) |
| 554 | /* Spoon-feed the compiler a big-endian integer with alignment 1. |
| 555 | * https://github.com/harfbuzz/harfbuzz/pull/1398 */ |
| 556 | struct __attribute__((packed)) packed_uint16_t { uint16_t v; }; |
| 557 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 558 | return __builtin_bswap16 (((packed_uint16_t *) this)->v); |
| 559 | #else /* __BYTE_ORDER == __BIG_ENDIAN */ |
| 560 | return ((packed_uint16_t *) this)->v; |
| 561 | #endif |
| 562 | #endif |
| 563 | return (v[0] << 8) |
| 564 | + (v[1] ); |
| 565 | } |
| 566 | private: uint8_t v[2]; |
| 567 | }; |
| 568 | template <typename Type> |
| 569 | struct BEInt<Type, 3> |
| 570 | { |
| 571 | public: |
| 572 | BEInt<Type, 3>& operator = (Type V) |
| 573 | { |
| 574 | v[0] = (V >> 16) & 0xFF; |
| 575 | v[1] = (V >> 8) & 0xFF; |
| 576 | v[2] = (V ) & 0xFF; |
| 577 | return *this; |
| 578 | } |
| 579 | operator Type () const |
| 580 | { |
| 581 | return (v[0] << 16) |
| 582 | + (v[1] << 8) |
| 583 | + (v[2] ); |
| 584 | } |
| 585 | private: uint8_t v[3]; |
| 586 | }; |
| 587 | template <typename Type> |
| 588 | struct BEInt<Type, 4> |
| 589 | { |
| 590 | public: |
| 591 | BEInt<Type, 4>& operator = (Type V) |
| 592 | { |
| 593 | v[0] = (V >> 24) & 0xFF; |
| 594 | v[1] = (V >> 16) & 0xFF; |
| 595 | v[2] = (V >> 8) & 0xFF; |
| 596 | v[3] = (V ) & 0xFF; |
| 597 | return *this; |
| 598 | } |
| 599 | operator Type () const |
| 600 | { |
| 601 | return (v[0] << 24) |
| 602 | + (v[1] << 16) |
| 603 | + (v[2] << 8) |
| 604 | + (v[3] ); |
| 605 | } |
| 606 | private: uint8_t v[4]; |
| 607 | }; |
| 608 | |
| 609 | |
| 610 | /* |
| 611 | * For lack of a better place, put Zawgyi script hack here. |
| 612 | * https://github.com/harfbuzz/harfbuzz/issues/1162 |
| 613 | */ |
| 614 | |
| 615 | #define HB_SCRIPT_MYANMAR_ZAWGYI ((hb_script_t) HB_TAG ('Q','a','a','g')) |
| 616 | |
| 617 | |
| 618 | /* Headers we include for everyone. Keep topologically sorted by dependency. |
| 619 | * They express dependency amongst themselves, but no other file should include |
| 620 | * them directly.*/ |
| 621 | #include "hb-meta.hh" |
| 622 | #include "hb-mutex.hh" |
| 623 | #include "hb-number.hh" |
| 624 | #include "hb-atomic.hh" // Requires: hb-meta |
| 625 | #include "hb-null.hh" // Requires: hb-meta |
| 626 | #include "hb-algs.hh" // Requires: hb-meta hb-null hb-number |
| 627 | #include "hb-iter.hh" // Requires: hb-algs hb-meta |
| 628 | #include "hb-debug.hh" // Requires: hb-algs hb-atomic |
| 629 | #include "hb-array.hh" // Requires: hb-algs hb-iter hb-null |
| 630 | #include "hb-vector.hh" // Requires: hb-array hb-null |
| 631 | #include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector |
| 632 | |
| 633 | #endif /* HB_HH */ |
| 634 | |