fcbd330588e17f42f1dd64f7c2e32d86291f7994
[platform/upstream/harfbuzz.git] / src / hb.hh
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 <stdlib.h>
180 #include <stddef.h>
181 #include <string.h>
182 #include <assert.h>
183 #include <stdio.h>
184 #include <stdarg.h>
185
186 #if (defined(_MSC_VER) && _MSC_VER >= 1500) || defined(__MINGW32__)
187 #ifdef __MINGW32_VERSION
188 #ifndef WIN32_LEAN_AND_MEAN
189 #define WIN32_LEAN_AND_MEAN 1
190 #endif
191 #include <windows.h>
192 #else
193 #include <intrin.h>
194 #endif
195 #endif
196
197 #define HB_PASTE1(a,b) a##b
198 #define HB_PASTE(a,b) HB_PASTE1(a,b)
199
200
201 /* Compile-time custom allocator support. */
202
203 #if defined(hb_malloc_impl) \
204  && defined(hb_calloc_impl) \
205  && defined(hb_realloc_impl) \
206  && defined(hb_free_impl)
207 extern "C" void* hb_malloc_impl(size_t size);
208 extern "C" void* hb_calloc_impl(size_t nmemb, size_t size);
209 extern "C" void* hb_realloc_impl(void *ptr, size_t size);
210 extern "C" void  hb_free_impl(void *ptr);
211 #define malloc hb_malloc_impl
212 #define calloc hb_calloc_impl
213 #define realloc hb_realloc_impl
214 #define free hb_free_impl
215 #endif
216
217
218 /*
219  * Compiler attributes
220  */
221
222 #if (defined(__GNUC__) || defined(__clang__)) && defined(__OPTIMIZE__)
223 #define likely(expr) (__builtin_expect (!!(expr), 1))
224 #define unlikely(expr) (__builtin_expect (!!(expr), 0))
225 #else
226 #define likely(expr) (expr)
227 #define unlikely(expr) (expr)
228 #endif
229
230 #if !defined(__GNUC__) && !defined(__clang__)
231 #undef __attribute__
232 #define __attribute__(x)
233 #endif
234
235 #if defined(__GNUC__) && (__GNUC__ >= 3)
236 #define HB_PURE_FUNC    __attribute__((pure))
237 #define HB_CONST_FUNC   __attribute__((const))
238 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
239 #else
240 #define HB_PURE_FUNC
241 #define HB_CONST_FUNC
242 #define HB_PRINTF_FUNC(format_idx, arg_idx)
243 #endif
244 #if defined(__GNUC__) && (__GNUC__ >= 4) || (__clang__)
245 #define HB_UNUSED       __attribute__((unused))
246 #elif defined(_MSC_VER) /* https://github.com/harfbuzz/harfbuzz/issues/635 */
247 #define HB_UNUSED __pragma(warning(suppress: 4100 4101))
248 #else
249 #define HB_UNUSED
250 #endif
251
252 #ifndef HB_INTERNAL
253 # if !defined(HB_NO_VISIBILITY) && !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_MSC_VER) && !defined(__SUNPRO_CC)
254 #  define HB_INTERNAL __attribute__((__visibility__("hidden")))
255 # elif defined(__MINGW32__)
256    /* We use -export-symbols on mingw32, since it does not support visibility attributes. */
257 #  define HB_INTERNAL
258 # elif defined (_MSC_VER) && defined (HB_DLL_EXPORT)
259    /* We do not try to export internal symbols on Visual Studio */
260 #  define HB_INTERNAL
261 #else
262 #  define HB_INTERNAL
263 #  define HB_NO_VISIBILITY 1
264 # endif
265 #endif
266
267 /* https://github.com/harfbuzz/harfbuzz/issues/1651 */
268 #if defined(__clang__) && __clang_major__ < 10
269 #define static_const static
270 #else
271 #define static_const static const
272 #endif
273
274 #if defined(__GNUC__) && (__GNUC__ >= 3)
275 #define HB_FUNC __PRETTY_FUNCTION__
276 #elif defined(_MSC_VER)
277 #define HB_FUNC __FUNCSIG__
278 #else
279 #define HB_FUNC __func__
280 #endif
281
282 #if defined(__SUNPRO_CC) && (__SUNPRO_CC < 0x5140)
283 /* https://github.com/harfbuzz/harfbuzz/issues/630 */
284 #define __restrict
285 #endif
286
287 /*
288  * Borrowed from https://bugzilla.mozilla.org/show_bug.cgi?id=1215411
289  * HB_FALLTHROUGH is an annotation to suppress compiler warnings about switch
290  * cases that fall through without a break or return statement. HB_FALLTHROUGH
291  * is only needed on cases that have code:
292  *
293  * switch (foo) {
294  *   case 1: // These cases have no code. No fallthrough annotations are needed.
295  *   case 2:
296  *   case 3:
297  *     foo = 4; // This case has code, so a fallthrough annotation is needed:
298  *     HB_FALLTHROUGH;
299  *   default:
300  *     return foo;
301  * }
302  */
303 #if defined(__clang__) && __cplusplus >= 201103L
304    /* clang's fallthrough annotations are only available starting in C++11. */
305 #  define HB_FALLTHROUGH [[clang::fallthrough]]
306 #elif defined(__GNUC__) && (__GNUC__ >= 7)
307    /* GNU fallthrough attribute is available from GCC7 */
308 #  define HB_FALLTHROUGH __attribute__((fallthrough))
309 #elif defined(_MSC_VER)
310    /*
311     * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
312     * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
313     */
314 #  include <sal.h>
315 #  define HB_FALLTHROUGH __fallthrough
316 #else
317 #  define HB_FALLTHROUGH /* FALLTHROUGH */
318 #endif
319
320 /* https://github.com/harfbuzz/harfbuzz/issues/1852 */
321 #if defined(__clang__) && !(defined(_AIX) && (defined(__IBMCPP__) || defined(__ibmxl__)))
322 /* Disable certain sanitizer errors. */
323 /* https://github.com/harfbuzz/harfbuzz/issues/1247 */
324 #define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW __attribute__((no_sanitize("signed-integer-overflow")))
325 #else
326 #define HB_NO_SANITIZE_SIGNED_INTEGER_OVERFLOW
327 #endif
328
329
330 #ifdef _WIN32
331    /* We need Windows Vista for both Uniscribe backend and for
332     * MemoryBarrier.  We don't support compiling on Windows XP,
333     * though we run on it fine. */
334 #  if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0600
335 #    undef _WIN32_WINNT
336 #  endif
337 #  ifndef _WIN32_WINNT
338 #    if !defined(WINAPI_FAMILY) || !(WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
339 #      define _WIN32_WINNT 0x0600
340 #    endif
341 #  endif
342 #  ifndef WIN32_LEAN_AND_MEAN
343 #    define WIN32_LEAN_AND_MEAN 1
344 #  endif
345 #  ifndef STRICT
346 #    define STRICT 1
347 #  endif
348
349 #  if defined(_WIN32_WCE)
350      /* Some things not defined on Windows CE. */
351 #    define vsnprintf _vsnprintf
352 #    ifndef HB_NO_GETENV
353 #      define HB_NO_GETENV
354 #    endif
355 #    if _WIN32_WCE < 0x800
356 #      define HB_NO_SETLOCALE
357 #      define HB_NO_ERRNO
358 #    endif
359 #  elif defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_PC_APP || WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
360 #    ifndef HB_NO_GETENV
361 #      define HB_NO_GETENV
362 #    endif
363 #  endif
364 #  if defined(_MSC_VER) && _MSC_VER < 1900
365 #    define snprintf _snprintf
366 #  endif
367 #endif
368
369 #ifdef HB_NO_GETENV
370 #define getenv(Name) nullptr
371 #endif
372
373 #ifndef HB_NO_ERRNO
374 #  include <errno.h>
375 #else
376 static int HB_UNUSED _hb_errno = 0;
377 #  undef errno
378 #  define errno _hb_errno
379 #endif
380
381 #if defined(HAVE_ATEXIT) && !defined(HB_USE_ATEXIT)
382 /* atexit() is only safe to be called from shared libraries on certain
383  * platforms.  Whitelist.
384  * https://bugs.freedesktop.org/show_bug.cgi?id=82246 */
385 #  if defined(__linux) && defined(__GLIBC_PREREQ)
386 #    if __GLIBC_PREREQ(2,3)
387 /* From atexit() manpage, it's safe with glibc 2.2.3 on Linux. */
388 #      define HB_USE_ATEXIT 1
389 #    endif
390 #  elif defined(_MSC_VER) || defined(__MINGW32__)
391 /* For MSVC:
392  * https://msdn.microsoft.com/en-us/library/tze57ck3.aspx
393  * https://msdn.microsoft.com/en-us/library/zk17ww08.aspx
394  * mingw32 headers say atexit is safe to use in shared libraries.
395  */
396 #    define HB_USE_ATEXIT 1
397 #  elif defined(__ANDROID__)
398 /* This is available since Android NKD r8 or r8b:
399  * https://issuetracker.google.com/code/p/android/issues/detail?id=6455
400  */
401 #    define HB_USE_ATEXIT 1
402 #  elif defined(__APPLE__)
403 /* For macOS and related platforms, the atexit man page indicates
404  * that it will be invoked when the library is unloaded, not only
405  * at application exit.
406  */
407 #    define HB_USE_ATEXIT 1
408 #  endif
409 #endif
410 #ifdef HB_NO_ATEXIT
411 #  undef HB_USE_ATEXIT
412 #endif
413 #ifndef HB_USE_ATEXIT
414 #  define HB_USE_ATEXIT 0
415 #endif
416
417 #define HB_STMT_START do
418 #define HB_STMT_END   while (0)
419
420 /* Static-assert as expression. */
421 template <unsigned int cond> class hb_assert_constant_t;
422 template <> class hb_assert_constant_t<1> {};
423 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * (unsigned int) sizeof (hb_assert_constant_t<_cond>))
424
425 /* Lets assert int types.  Saves trouble down the road. */
426 static_assert ((sizeof (int8_t) == 1), "");
427 static_assert ((sizeof (uint8_t) == 1), "");
428 static_assert ((sizeof (int16_t) == 2), "");
429 static_assert ((sizeof (uint16_t) == 2), "");
430 static_assert ((sizeof (int32_t) == 4), "");
431 static_assert ((sizeof (uint32_t) == 4), "");
432 static_assert ((sizeof (int64_t) == 8), "");
433 static_assert ((sizeof (uint64_t) == 8), "");
434 static_assert ((sizeof (hb_codepoint_t) == 4), "");
435 static_assert ((sizeof (hb_position_t) == 4), "");
436 static_assert ((sizeof (hb_mask_t) == 4), "");
437 static_assert ((sizeof (hb_var_int_t) == 4), "");
438
439 #define HB_DELETE_COPY_ASSIGN(TypeName) \
440   TypeName(const TypeName&) = delete; \
441   void operator=(const TypeName&) = delete
442 #define HB_DELETE_CREATE_COPY_ASSIGN(TypeName) \
443   TypeName() = delete; \
444   TypeName(const TypeName&) = delete; \
445   void operator=(const TypeName&) = delete
446
447
448 /* Flags */
449
450 /* Enable bitwise ops on enums marked as flags_t */
451 /* To my surprise, looks like the function resolver is happy to silently cast
452  * one enum to another...  So this doesn't provide the type-checking that I
453  * originally had in mind... :(.
454  *
455  * For MSVC warnings, see: https://github.com/harfbuzz/harfbuzz/pull/163
456  */
457 #ifdef _MSC_VER
458 # pragma warning(disable:4200)
459 # pragma warning(disable:4800)
460 #endif
461 #define HB_MARK_AS_FLAG_T(T) \
462         extern "C++" { \
463           static inline T operator | (T l, T r) { return T ((unsigned) l | (unsigned) r); } \
464           static inline T operator & (T l, T r) { return T ((unsigned) l & (unsigned) r); } \
465           static inline T operator ^ (T l, T r) { return T ((unsigned) l ^ (unsigned) r); } \
466           static inline T operator ~ (T r) { return T (~(unsigned int) r); } \
467           static inline T& operator |= (T &l, T r) { l = l | r; return l; } \
468           static inline T& operator &= (T& l, T r) { l = l & r; return l; } \
469           static inline T& operator ^= (T& l, T r) { l = l ^ r; return l; } \
470         } \
471         static_assert (true, "")
472
473 /* Useful for set-operations on small enums.
474  * For example, for testing "x ∈ {x1, x2, x3}" use:
475  * (FLAG_UNSAFE(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
476  */
477 #define FLAG(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 32) + (((uint32_t) 1U) << (unsigned)(x)))
478 #define FLAG_UNSAFE(x) ((unsigned)(x) < 32 ? (((uint32_t) 1U) << (unsigned)(x)) : 0)
479 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
480 #define FLAG64(x) (ASSERT_STATIC_EXPR_ZERO ((unsigned)(x) < 64) + (((uint64_t) 1ULL) << (unsigned)(x)))
481 #define FLAG64_UNSAFE(x) ((unsigned)(x) < 64 ? (((uint64_t) 1ULL) << (unsigned)(x)) : 0)
482
483
484 /* Size signifying variable-sized array */
485 #ifndef HB_VAR_ARRAY
486 #define HB_VAR_ARRAY 1
487 #endif
488
489 static inline double
490 _hb_roundf (float x)
491 {
492   return x >= 0 ? floor ((double) x + .5) : ceil ((double) x - .5);
493 }
494 #ifndef HAVE_ROUNDF
495 #define roundf(x) _hb_roundf(x)
496 #endif
497
498 /* Endian swap, used in Windows related backends */
499 static inline uint16_t hb_uint16_swap (const uint16_t v)
500 { return (v >> 8) | (v << 8); }
501 static inline uint32_t hb_uint32_swap (const uint32_t v)
502 { return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
503
504 /*
505  * Big-endian integers.  Here because fundamental.
506  */
507
508 template <typename Type, int Bytes> struct BEInt;
509
510 template <typename Type>
511 struct BEInt<Type, 1>
512 {
513   public:
514   BEInt<Type, 1>& operator = (Type V)
515   {
516     v = V;
517     return *this;
518   }
519   operator Type () const { return v; }
520   private: uint8_t v;
521 };
522 template <typename Type>
523 struct BEInt<Type, 2>
524 {
525   public:
526   BEInt<Type, 2>& operator = (Type V)
527   {
528     v[0] = (V >>  8) & 0xFF;
529     v[1] = (V      ) & 0xFF;
530     return *this;
531   }
532   operator Type () const
533   {
534 #if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \
535     defined(__BYTE_ORDER) && \
536     (__BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __BIG_ENDIAN)
537     /* Spoon-feed the compiler a big-endian integer with alignment 1.
538      * https://github.com/harfbuzz/harfbuzz/pull/1398 */
539     struct __attribute__((packed)) packed_uint16_t { uint16_t v; };
540 #if __BYTE_ORDER == __LITTLE_ENDIAN
541     return __builtin_bswap16 (((packed_uint16_t *) this)->v);
542 #else /* __BYTE_ORDER == __BIG_ENDIAN */
543     return ((packed_uint16_t *) this)->v;
544 #endif
545 #endif
546     return (v[0] <<  8)
547          + (v[1]      );
548   }
549   private: uint8_t v[2];
550 };
551 template <typename Type>
552 struct BEInt<Type, 3>
553 {
554   public:
555   BEInt<Type, 3>& operator = (Type V)
556   {
557     v[0] = (V >> 16) & 0xFF;
558     v[1] = (V >>  8) & 0xFF;
559     v[2] = (V      ) & 0xFF;
560     return *this;
561   }
562   operator Type () const
563   {
564     return (v[0] << 16)
565          + (v[1] <<  8)
566          + (v[2]      );
567   }
568   private: uint8_t v[3];
569 };
570 template <typename Type>
571 struct BEInt<Type, 4>
572 {
573   public:
574   BEInt<Type, 4>& operator = (Type V)
575   {
576     v[0] = (V >> 24) & 0xFF;
577     v[1] = (V >> 16) & 0xFF;
578     v[2] = (V >>  8) & 0xFF;
579     v[3] = (V      ) & 0xFF;
580     return *this;
581   }
582   operator Type () const
583   {
584     return (v[0] << 24)
585          + (v[1] << 16)
586          + (v[2] <<  8)
587          + (v[3]      );
588   }
589   private: uint8_t v[4];
590 };
591
592
593 /*
594  * For lack of a better place, put Zawgyi script hack here.
595  * https://github.com/harfbuzz/harfbuzz/issues/1162
596  */
597
598 #define HB_SCRIPT_MYANMAR_ZAWGYI        ((hb_script_t) HB_TAG ('Q','a','a','g'))
599
600
601 /* Headers we include for everyone.  Keep topologically sorted by dependency.
602  * They express dependency amongst themselves, but no other file should include
603  * them directly.*/
604 #include "hb-meta.hh"
605 #include "hb-mutex.hh"
606 #include "hb-number.hh"
607 #include "hb-atomic.hh" // Requires: hb-meta
608 #include "hb-null.hh"   // Requires: hb-meta
609 #include "hb-algs.hh"   // Requires: hb-meta hb-null hb-number
610 #include "hb-iter.hh"   // Requires: hb-algs hb-meta
611 #include "hb-debug.hh"  // Requires: hb-algs hb-atomic
612 #include "hb-array.hh"  // Requires: hb-algs hb-iter hb-null
613 #include "hb-vector.hh" // Requires: hb-array hb-null
614 #include "hb-object.hh" // Requires: hb-atomic hb-mutex hb-vector
615
616 #endif /* HB_HH */