From 1fc2c6f4a86ad8f330452c64b0335e9fd03954ab Mon Sep 17 00:00:00 2001 From: Phil Race Date: Thu, 21 Jun 2018 12:54:30 -0700 Subject: [PATCH] 8205441: Upgrade to harfbuzz 1.8.1 Reviewed-by: serb --- make/lib/Awt2dLibraries.gmk | 6 +- src/java.desktop/share/legal/harfbuzz.md | 2 +- .../harfbuzz/hb-atomic-private.hh | 9 +- .../harfbuzz/hb-blob-private.hh | 88 + .../native/libfontmanager/harfbuzz/hb-blob.cc | 232 +- .../native/libfontmanager/harfbuzz/hb-blob.h | 2 + .../harfbuzz/hb-buffer-deserialize-json.hh | 14 +- .../harfbuzz/hb-buffer-deserialize-text.hh | 20 +- .../harfbuzz/hb-buffer-private.hh | 2 +- .../libfontmanager/harfbuzz/hb-buffer.cc | 28 +- .../libfontmanager/harfbuzz/hb-common.cc | 46 +- .../libfontmanager/harfbuzz/hb-common.h | 27 +- .../libfontmanager/harfbuzz/hb-coretext.cc | 67 +- .../libfontmanager/harfbuzz/hb-dsalgs.hh | 2 +- .../native/libfontmanager/harfbuzz/hb-face.cc | 32 +- .../native/libfontmanager/harfbuzz/hb-face.h | 4 + .../harfbuzz/hb-font-private.hh | 6 +- .../native/libfontmanager/harfbuzz/hb-font.cc | 4 +- .../native/libfontmanager/harfbuzz/hb-ft.cc | 32 +- .../libfontmanager/harfbuzz/hb-map-private.hh | 255 ++ .../native/libfontmanager/harfbuzz/hb-map.cc | 261 ++ .../native/libfontmanager/harfbuzz/hb-map.h | 104 + .../harfbuzz/hb-mutex-private.hh | 2 +- .../harfbuzz/hb-object-private.hh | 53 +- .../harfbuzz/hb-open-file-private.hh | 20 +- .../harfbuzz/hb-open-type-private.hh | 263 +- .../harfbuzz/hb-ot-cmap-table.hh | 461 ++- .../harfbuzz/hb-ot-color-cbdt-table.hh | 211 +- .../harfbuzz/hb-ot-color-colr-table.hh | 110 +- .../harfbuzz/hb-ot-color-cpal-table.hh | 84 +- .../harfbuzz/hb-ot-color-sbix-table.hh | 153 + .../harfbuzz/hb-ot-color-svg-table.hh | 145 + .../libfontmanager/harfbuzz/hb-ot-font.cc | 13 +- .../harfbuzz/hb-ot-glyf-table.hh | 103 +- .../harfbuzz/hb-ot-head-table.hh | 12 +- .../harfbuzz/hb-ot-hhea-table.hh | 17 +- .../harfbuzz/hb-ot-hmtx-table.hh | 44 +- .../harfbuzz/hb-ot-kern-table.hh | 16 +- .../harfbuzz/hb-ot-layout-base-table.hh | 3 +- .../harfbuzz/hb-ot-layout-common-private.hh | 48 +- .../harfbuzz/hb-ot-layout-gdef-table.hh | 3 +- .../harfbuzz/hb-ot-layout-gpos-table.hh | 5 +- .../harfbuzz/hb-ot-layout-gsub-table.hh | 33 +- .../harfbuzz/hb-ot-layout-gsubgpos-private.hh | 90 +- .../harfbuzz/hb-ot-layout-jstf-table.hh | 3 +- .../harfbuzz/hb-ot-layout-private.hh | 20 +- .../libfontmanager/harfbuzz/hb-ot-layout.cc | 111 +- .../libfontmanager/harfbuzz/hb-ot-layout.h | 8 +- .../harfbuzz/hb-ot-map-private.hh | 49 +- .../libfontmanager/harfbuzz/hb-ot-map.cc | 31 +- .../harfbuzz/hb-ot-maxp-table.hh | 7 +- .../harfbuzz/hb-ot-name-table.hh | 4 +- .../harfbuzz/hb-ot-os2-table.hh | 53 +- .../harfbuzz/hb-ot-os2-unicode-ranges.hh | 2 +- .../harfbuzz/hb-ot-post-table.hh | 69 +- .../hb-ot-shape-complex-arabic-table.hh | 43 +- .../harfbuzz/hb-ot-shape-complex-arabic.cc | 4 +- .../harfbuzz/hb-ot-shape-complex-hangul.cc | 12 +- .../hb-ot-shape-complex-indic-machine.hh | 2 +- .../hb-ot-shape-complex-indic-private.hh | 2 +- .../hb-ot-shape-complex-indic-table.cc | 79 +- .../harfbuzz/hb-ot-shape-complex-indic.cc | 13 +- .../hb-ot-shape-complex-khmer-machine.hh | 2 +- .../harfbuzz/hb-ot-shape-complex-khmer.cc | 12 +- .../hb-ot-shape-complex-myanmar-private.hh | 2 +- .../harfbuzz/hb-ot-shape-complex-myanmar.cc | 4 +- .../harfbuzz/hb-ot-shape-complex-private.hh | 9 + .../harfbuzz/hb-ot-shape-complex-thai.cc | 6 +- .../hb-ot-shape-complex-use-machine.hh | 2 +- .../hb-ot-shape-complex-use-private.hh | 2 +- .../harfbuzz/hb-ot-shape-complex-use-table.cc | 92 +- .../harfbuzz/hb-ot-shape-complex-use.cc | 6 +- .../harfbuzz/hb-ot-shape-fallback.cc | 2 +- .../harfbuzz/hb-ot-shape-normalize.cc | 16 +- .../harfbuzz/hb-ot-shape-private.hh | 14 +- .../libfontmanager/harfbuzz/hb-ot-shape.cc | 25 +- .../libfontmanager/harfbuzz/hb-ot-tag.cc | 7 +- .../harfbuzz/hb-ot-var-avar-table.hh | 46 +- .../harfbuzz/hb-ot-var-fvar-table.hh | 21 +- .../harfbuzz/hb-ot-var-hvar-table.hh | 7 +- .../harfbuzz/hb-ot-var-mvar-table.hh | 4 +- .../libfontmanager/harfbuzz/hb-ot-var.cc | 6 +- .../libfontmanager/harfbuzz/hb-private.hh | 359 ++- .../libfontmanager/harfbuzz/hb-set-private.hh | 112 +- .../native/libfontmanager/harfbuzz/hb-set.cc | 39 +- .../native/libfontmanager/harfbuzz/hb-set.h | 6 +- .../libfontmanager/harfbuzz/hb-shape.cc | 7 +- .../libfontmanager/harfbuzz/hb-shaper.cc | 9 +- .../libfontmanager/harfbuzz/hb-static.cc | 32 + .../harfbuzz/hb-string-array.hh | 8 +- .../libfontmanager/harfbuzz/hb-subset-plan.hh | 72 +- .../harfbuzz/hb-subset-private.hh | 1 + .../libfontmanager/harfbuzz/hb-subset.h | 8 +- .../native/libfontmanager/harfbuzz/hb-ucdn.cc | 14 +- .../libfontmanager/harfbuzz/hb-ucdn/ucdn.h | 10 + .../libfontmanager/harfbuzz/hb-ucdn/ucdn_db.h | 2780 +++++++++-------- .../harfbuzz/hb-unicode-private.hh | 12 +- .../libfontmanager/harfbuzz/hb-unicode.cc | 2 +- .../libfontmanager/harfbuzz/hb-version.h | 6 +- .../share/native/libfontmanager/harfbuzz/hb.h | 1 + 100 files changed, 4887 insertions(+), 2520 deletions(-) create mode 100644 src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob-private.hh create mode 100644 src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map-private.hh create mode 100644 src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map.cc create mode 100644 src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map.h create mode 100644 src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-sbix-table.hh create mode 100644 src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-svg-table.hh create mode 100644 src/java.desktop/share/native/libfontmanager/harfbuzz/hb-static.cc diff --git a/make/lib/Awt2dLibraries.gmk b/make/lib/Awt2dLibraries.gmk index 8a138b2e788..973cd16523e 100644 --- a/make/lib/Awt2dLibraries.gmk +++ b/make/lib/Awt2dLibraries.gmk @@ -529,7 +529,7 @@ endif #### Begin harfbuzz configuration -HARFBUZZ_CFLAGS := -DHAVE_OT -DHAVE_FALLBACK -DHAVE_UCDN +HARFBUZZ_CFLAGS := -DHAVE_OT -DHAVE_FALLBACK -DHAVE_UCDN -DHAVE_ROUND ifneq ($(OPENJDK_TARGET_OS), windows) HARFBUZZ_CFLAGS += -DGETPAGESIZE -DHAVE_MPROTECT -DHAVE_PTHREAD \ @@ -589,8 +589,6 @@ ifeq ($(TOOLCHAIN_TYPE), gcc) # Turn off all warnings for sunFont.c. This is needed because the specific warning # about discarding 'const' qualifier cannot be turned off individually. BUILD_LIBFONTMANAGER_sunFont.c_CFLAGS := -w - # Turn off aliasing with GCC for ExtensionSubtables.cpp - BUILD_LIBFONTMANAGER_ExtensionSubtables.cpp_CXXFLAGS := -fno-strict-aliasing endif # LDFLAGS clarification: @@ -609,7 +607,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBFONTMANAGER, \ EXTRA_HEADER_DIRS := $(LIBFONTMANAGER_EXTRA_HEADER_DIRS), \ WARNINGS_AS_ERRORS_xlc := false, \ DISABLED_WARNINGS_gcc := sign-compare int-to-pointer-cast \ - type-limits missing-field-initializers implicit-fallthrough, \ + type-limits missing-field-initializers implicit-fallthrough strict-aliasing, \ DISABLED_WARNINGS_CXX_gcc := reorder delete-non-virtual-dtor strict-overflow \ maybe-uninitialized, \ DISABLED_WARNINGS_clang := unused-value incompatible-pointer-types \ diff --git a/src/java.desktop/share/legal/harfbuzz.md b/src/java.desktop/share/legal/harfbuzz.md index 5ca0c787131..a99f5f686e1 100644 --- a/src/java.desktop/share/legal/harfbuzz.md +++ b/src/java.desktop/share/legal/harfbuzz.md @@ -1,4 +1,4 @@ -## Harfbuzz v1.7.6 +## Harfbuzz v1.8.1 ### Harfbuzz License diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-atomic-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-atomic-private.hh index 53c37a016c3..73d7f005e8c 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-atomic-private.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-atomic-private.hh @@ -63,7 +63,6 @@ static inline void _HBMemoryBarrier (void) { } typedef LONG hb_atomic_int_impl_t; -#define HB_ATOMIC_INT_IMPL_INIT(V) (V) #define hb_atomic_int_impl_add(AI, V) InterlockedExchangeAdd (&(AI), (V)) #define hb_atomic_ptr_impl_get(P) (_HBMemoryBarrier (), (void *) *(P)) @@ -73,7 +72,6 @@ typedef LONG hb_atomic_int_impl_t; #elif !defined(HB_NO_MT) && defined(HAVE_INTEL_ATOMIC_PRIMITIVES) typedef int hb_atomic_int_impl_t; -#define HB_ATOMIC_INT_IMPL_INIT(V) (V) #define hb_atomic_int_impl_add(AI, V) __sync_fetch_and_add (&(AI), (V)) #define hb_atomic_ptr_impl_get(P) (void *) (__sync_synchronize (), *(P)) @@ -86,7 +84,6 @@ typedef int hb_atomic_int_impl_t; #include typedef unsigned int hb_atomic_int_impl_t; -#define HB_ATOMIC_INT_IMPL_INIT(V) (V) #define hb_atomic_int_impl_add(AI, V) ( ({__machine_rw_barrier ();}), atomic_add_int_nv (&(AI), (V)) - (V)) #define hb_atomic_ptr_impl_get(P) ( ({__machine_rw_barrier ();}), (void *) *(P)) @@ -104,7 +101,6 @@ typedef unsigned int hb_atomic_int_impl_t; typedef int32_t hb_atomic_int_impl_t; -#define HB_ATOMIC_INT_IMPL_INIT(V) (V) #define hb_atomic_int_impl_add(AI, V) (OSAtomicAdd32Barrier ((V), &(AI)) - (V)) #define hb_atomic_ptr_impl_get(P) (OSMemoryBarrier (), (void *) *(P)) @@ -138,7 +134,6 @@ static inline int _hb_compare_and_swaplp(volatile long* P, long O, long N) { } typedef int hb_atomic_int_impl_t; -#define HB_ATOMIC_INT_IMPL_INIT(V) (V) #define hb_atomic_int_impl_add(AI, V) _hb_fetch_and_add (&(AI), (V)) #define hb_atomic_ptr_impl_get(P) (__sync(), (void *) *(P)) @@ -149,7 +144,6 @@ typedef int hb_atomic_int_impl_t; #define HB_ATOMIC_INT_NIL 1 /* Warn that fallback implementation is in use. */ typedef volatile int hb_atomic_int_impl_t; -#define HB_ATOMIC_INT_IMPL_INIT(V) (V) #define hb_atomic_int_impl_add(AI, V) (((AI) += (V)) - (V)) #define hb_atomic_ptr_impl_get(P) ((void *) *(P)) @@ -159,7 +153,6 @@ typedef volatile int hb_atomic_int_impl_t; #else /* HB_NO_MT */ typedef int hb_atomic_int_impl_t; -#define HB_ATOMIC_INT_IMPL_INIT(V) (V) #define hb_atomic_int_impl_add(AI, V) (((AI) += (V)) - (V)) #define hb_atomic_ptr_impl_get(P) ((void *) *(P)) @@ -169,7 +162,7 @@ typedef int hb_atomic_int_impl_t; #endif -#define HB_ATOMIC_INT_INIT(V) {HB_ATOMIC_INT_IMPL_INIT(V)} +#define HB_ATOMIC_INT_INIT(V) {V} struct hb_atomic_int_t { diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob-private.hh new file mode 100644 index 00000000000..b72fa721b89 --- /dev/null +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob-private.hh @@ -0,0 +1,88 @@ +/* + * Copyright © 2009 Red Hat, Inc. + * Copyright © 2018 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Red Hat Author(s): Behdad Esfahbod + * Google Author(s): Behdad Esfahbod + */ + +#ifndef HB_BLOB_PRIVATE_HH +#define HB_BLOB_PRIVATE_HH + +#include "hb-private.hh" + +#include "hb-object-private.hh" + + +/* + * hb_blob_t + */ + +struct hb_blob_t +{ + inline void fini_shallow (void) + { + destroy_user_data (); + } + + inline void destroy_user_data (void) + { + if (destroy) + { + destroy (user_data); + user_data = nullptr; + destroy = nullptr; + } + } + + HB_INTERNAL bool try_make_writable (void); + HB_INTERNAL bool try_make_writable_inplace (void); + HB_INTERNAL bool try_make_writable_inplace_unix (void); + + inline void lock (void) + { + hb_blob_make_immutable (this); + } + + template + inline const Type* as (void) const + { + return unlikely (!data) ? &Null(Type) : reinterpret_cast (data); + } + + public: + hb_object_header_t header; + ASSERT_POD (); + + bool immutable; + + const char *data; + unsigned int length; + hb_memory_mode_t mode; + + void *user_data; + hb_destroy_func_t destroy; +}; + + +#endif /* HB_BLOB_PRIVATE_HH */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob.cc index c8a2f57028e..7b143d2fee3 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob.cc +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob.cc @@ -1,5 +1,6 @@ /* * Copyright © 2009 Red Hat, Inc. + * Copyright © 2018 Ebrahim Byagowi * * This is part of HarfBuzz, a text shaping library. * @@ -31,8 +32,7 @@ #include "hb-private.hh" #include "hb-debug.hh" - -#include "hb-object-private.hh" +#include "hb-blob-private.hh" #ifdef HAVE_SYS_MMAN_H #ifdef HAVE_UNISTD_H @@ -43,35 +43,9 @@ #include #include +#include -struct hb_blob_t { - hb_object_header_t header; - ASSERT_POD (); - - bool immutable; - - const char *data; - unsigned int length; - hb_memory_mode_t mode; - - void *user_data; - hb_destroy_func_t destroy; -}; - - -static bool _try_writable (hb_blob_t *blob); - -static void -_hb_blob_destroy_user_data (hb_blob_t *blob) -{ - if (blob->destroy) { - blob->destroy (blob->user_data); - blob->user_data = nullptr; - blob->destroy = nullptr; - } -} - /** * hb_blob_create: (skip) * @data: Pointer to blob data. @@ -114,7 +88,7 @@ hb_blob_create (const char *data, if (blob->mode == HB_MEMORY_MODE_DUPLICATE) { blob->mode = HB_MEMORY_MODE_READONLY; - if (!_try_writable (blob)) { + if (!blob->try_make_writable ()) { hb_blob_destroy (blob); return hb_blob_get_empty (); } @@ -260,7 +234,7 @@ hb_blob_destroy (hb_blob_t *blob) { if (!hb_object_destroy (blob)) return; - _hb_blob_destroy_user_data (blob); + blob->fini_shallow (); free (blob); } @@ -395,7 +369,7 @@ hb_blob_get_data (hb_blob_t *blob, unsigned int *length) char * hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length) { - if (!_try_writable (blob)) { + if (!blob->try_make_writable ()) { if (length) *length = 0; @@ -409,8 +383,8 @@ hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length) } -static hb_bool_t -_try_make_writable_inplace_unix (hb_blob_t *blob) +bool +hb_blob_t::try_make_writable_inplace_unix (void) { #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT) uintptr_t pagesize = -1, mask, length; @@ -425,25 +399,25 @@ _try_make_writable_inplace_unix (hb_blob_t *blob) #endif if ((uintptr_t) -1L == pagesize) { - DEBUG_MSG_FUNC (BLOB, blob, "failed to get pagesize: %s", strerror (errno)); + DEBUG_MSG_FUNC (BLOB, this, "failed to get pagesize: %s", strerror (errno)); return false; } - DEBUG_MSG_FUNC (BLOB, blob, "pagesize is %lu", (unsigned long) pagesize); + DEBUG_MSG_FUNC (BLOB, this, "pagesize is %lu", (unsigned long) pagesize); mask = ~(pagesize-1); - addr = (const char *) (((uintptr_t) blob->data) & mask); - length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask) - addr; - DEBUG_MSG_FUNC (BLOB, blob, + addr = (const char *) (((uintptr_t) this->data) & mask); + length = (const char *) (((uintptr_t) this->data + this->length + pagesize-1) & mask) - addr; + DEBUG_MSG_FUNC (BLOB, this, "calling mprotect on [%p..%p] (%lu bytes)", addr, addr+length, (unsigned long) length); if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) { - DEBUG_MSG_FUNC (BLOB, blob, "mprotect failed: %s", strerror (errno)); + DEBUG_MSG_FUNC (BLOB, this, "mprotect failed: %s", strerror (errno)); return false; } - blob->mode = HB_MEMORY_MODE_WRITABLE; + this->mode = HB_MEMORY_MODE_WRITABLE; - DEBUG_MSG_FUNC (BLOB, blob, + DEBUG_MSG_FUNC (BLOB, this, "successfully made [%p..%p] (%lu bytes) writable\n", addr, addr+length, (unsigned long) length); return true; @@ -452,53 +426,185 @@ _try_make_writable_inplace_unix (hb_blob_t *blob) #endif } -static bool -_try_writable_inplace (hb_blob_t *blob) +bool +hb_blob_t::try_make_writable_inplace (void) { - DEBUG_MSG_FUNC (BLOB, blob, "making writable inplace\n"); + DEBUG_MSG_FUNC (BLOB, this, "making writable inplace\n"); - if (_try_make_writable_inplace_unix (blob)) + if (this->try_make_writable_inplace_unix ()) return true; - DEBUG_MSG_FUNC (BLOB, blob, "making writable -> FAILED\n"); + DEBUG_MSG_FUNC (BLOB, this, "making writable -> FAILED\n"); /* Failed to make writable inplace, mark that */ - blob->mode = HB_MEMORY_MODE_READONLY; + this->mode = HB_MEMORY_MODE_READONLY; return false; } -static bool -_try_writable (hb_blob_t *blob) +bool +hb_blob_t::try_make_writable (void) { - if (blob->immutable) + if (this->immutable) return false; - if (blob->mode == HB_MEMORY_MODE_WRITABLE) + if (this->mode == HB_MEMORY_MODE_WRITABLE) return true; - if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && _try_writable_inplace (blob)) + if (this->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE && this->try_make_writable_inplace ()) return true; - if (blob->mode == HB_MEMORY_MODE_WRITABLE) + if (this->mode == HB_MEMORY_MODE_WRITABLE) return true; - DEBUG_MSG_FUNC (BLOB, blob, "current data is -> %p\n", blob->data); + DEBUG_MSG_FUNC (BLOB, this, "current data is -> %p\n", this->data); char *new_data; - new_data = (char *) malloc (blob->length); + new_data = (char *) malloc (this->length); if (unlikely (!new_data)) return false; - DEBUG_MSG_FUNC (BLOB, blob, "dupped successfully -> %p\n", blob->data); + DEBUG_MSG_FUNC (BLOB, this, "dupped successfully -> %p\n", this->data); - memcpy (new_data, blob->data, blob->length); - _hb_blob_destroy_user_data (blob); - blob->mode = HB_MEMORY_MODE_WRITABLE; - blob->data = new_data; - blob->user_data = new_data; - blob->destroy = free; + memcpy (new_data, this->data, this->length); + this->destroy_user_data (); + this->mode = HB_MEMORY_MODE_WRITABLE; + this->data = new_data; + this->user_data = new_data; + this->destroy = free; return true; } + +/* + * Mmap + */ + +#ifdef HAVE_MMAP +# include +# include +# include +#endif + +#if defined(_WIN32) || defined(__CYGWIN__) +# include +#endif + +#ifndef _O_BINARY +# define _O_BINARY 0 +#endif + +#ifndef MAP_NORESERVE +# define MAP_NORESERVE 0 +#endif + +struct hb_mapped_file_t +{ + char *contents; + unsigned long length; +#if defined(_WIN32) || defined(__CYGWIN__) + HANDLE mapping; +#endif +}; + +static void +_hb_mapped_file_destroy (hb_mapped_file_t *file) +{ +#ifdef HAVE_MMAP + munmap (file->contents, file->length); +#elif defined(_WIN32) || defined(__CYGWIN__) + UnmapViewOfFile (file->contents); + CloseHandle (file->mapping); +#else + free (file->contents); +#endif + + free (file); +} + +/** + * hb_blob_create_from_file: + * @file_name: font filename. + * + * Returns: A hb_blob_t pointer with the content of the file + * + * Since: 1.7.7 + **/ +hb_blob_t * +hb_blob_create_from_file (const char *file_name) +{ + // Adopted from glib's gmappedfile.c with Matthias Clasen and + // Allison Lortie permission but changed a lot to suit our need. + bool writable = false; + hb_memory_mode_t mm = HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE; + hb_mapped_file_t *file = (hb_mapped_file_t *) calloc (1, sizeof (hb_mapped_file_t)); + if (unlikely (!file)) return hb_blob_get_empty (); + +#ifdef HAVE_MMAP + int fd = open (file_name, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0); +# define CLOSE close + if (unlikely (fd == -1)) goto fail_without_close; + + struct stat st; + if (unlikely (fstat (fd, &st) == -1)) goto fail; + + // See https://github.com/GNOME/glib/blob/f9faac7/glib/gmappedfile.c#L139-L142 + if (unlikely (st.st_size == 0 && S_ISREG (st.st_mode))) goto fail; + + file->length = (unsigned long) st.st_size; + file->contents = (char *) mmap (nullptr, file->length, + writable ? PROT_READ|PROT_WRITE : PROT_READ, + MAP_PRIVATE | MAP_NORESERVE, fd, 0); + + if (unlikely (file->contents == MAP_FAILED)) goto fail; + +#elif defined(_WIN32) || defined(__CYGWIN__) + HANDLE fd = CreateFile (file_name, + writable ? GENERIC_READ|GENERIC_WRITE : GENERIC_READ, + FILE_SHARE_READ, nullptr, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, nullptr); +# define CLOSE CloseHandle + + if (unlikely (fd == INVALID_HANDLE_VALUE)) goto fail_without_close; + + file->length = (unsigned long) GetFileSize (fd, nullptr); + file->mapping = CreateFileMapping (fd, nullptr, + writable ? PAGE_WRITECOPY : PAGE_READONLY, + 0, 0, nullptr); + if (unlikely (file->mapping == nullptr)) goto fail; + + file->contents = (char *) MapViewOfFile (file->mapping, + writable ? FILE_MAP_COPY : FILE_MAP_READ, + 0, 0, 0); + if (unlikely (file->contents == nullptr)) goto fail; + +#else + mm = HB_MEMORY_MODE_WRITABLE; + + FILE *fd = fopen (file_name, "rb"); +# define CLOSE fclose + if (unlikely (!fd)) goto fail_without_close; + + fseek (fd, 0, SEEK_END); + file->length = ftell (fd); + rewind (fd); + file->contents = (char *) malloc (file->length); + if (unlikely (!file->contents)) goto fail; + + if (unlikely (fread (file->contents, 1, file->length, fd) != file->length)) + goto fail; + +#endif + + CLOSE (fd); + return hb_blob_create (file->contents, file->length, mm, (void *) file, + (hb_destroy_func_t) _hb_mapped_file_destroy); + +fail: + CLOSE (fd); +#undef CLOSE +fail_without_close: + free (file); + return hb_blob_get_empty (); +} diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob.h b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob.h index dadaccc3583..a714fb2057b 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob.h +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-blob.h @@ -123,6 +123,8 @@ hb_blob_get_data (hb_blob_t *blob, unsigned int *length); HB_EXTERN char * hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length); +HB_EXTERN hb_blob_t * +hb_blob_create_from_file (const char *file_name); HB_END_DECLS diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-deserialize-json.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-deserialize-json.hh index 067c594e4ad..3850ede36ed 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-deserialize-json.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-deserialize-json.hh @@ -503,7 +503,7 @@ _resume: #line 43 "hb-buffer-deserialize-json.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -554,7 +554,7 @@ _resume: #line 43 "hb-buffer-deserialize-json.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -566,7 +566,7 @@ _resume: #line 43 "hb-buffer-deserialize-json.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -578,7 +578,7 @@ _resume: #line 43 "hb-buffer-deserialize-json.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -590,7 +590,7 @@ _resume: #line 43 "hb-buffer-deserialize-json.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -602,7 +602,7 @@ _resume: #line 43 "hb-buffer-deserialize-json.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -614,7 +614,7 @@ _resume: #line 43 "hb-buffer-deserialize-json.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-deserialize-text.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-deserialize-text.hh index 198827491da..9f3ef732361 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-deserialize-text.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-deserialize-text.hh @@ -422,7 +422,7 @@ _resume: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -434,7 +434,7 @@ _resume: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -446,7 +446,7 @@ _resume: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -458,7 +458,7 @@ _resume: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -470,7 +470,7 @@ _resume: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -499,7 +499,7 @@ _again: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -511,7 +511,7 @@ _again: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -523,7 +523,7 @@ _again: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -535,7 +535,7 @@ _again: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; @@ -547,7 +547,7 @@ _again: #line 43 "hb-buffer-deserialize-text.rl" { buffer->add_info (info); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return false; buffer->pos[buffer->len - 1] = pos; *end_ptr = p; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-private.hh index bddfd200cb3..42fffa230a9 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-private.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer-private.hh @@ -101,7 +101,7 @@ struct hb_buffer_t { hb_buffer_content_type_t content_type; hb_segment_properties_t props; /* Script, language, direction */ - bool in_error; /* Allocation failed */ + bool successful; /* Allocations successful */ bool have_output; /* Whether we have an output buffer going on */ bool have_positions; /* Whether we have positions */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer.cc index c010d9810cf..b6a20a86f08 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer.cc +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-buffer.cc @@ -111,11 +111,11 @@ hb_segment_properties_hash (const hb_segment_properties_t *p) bool hb_buffer_t::enlarge (unsigned int size) { - if (unlikely (in_error)) + if (unlikely (!successful)) return false; if (unlikely (size > max_len)) { - in_error = true; + successful = false; return false; } @@ -139,7 +139,7 @@ hb_buffer_t::enlarge (unsigned int size) done: if (unlikely (!new_pos || !new_info)) - in_error = true; + successful = false; if (likely (new_pos)) pos = new_pos; @@ -148,10 +148,10 @@ done: info = new_info; out_info = separate_out ? (hb_glyph_info_t *) pos : info; - if (likely (!in_error)) + if (likely (successful)) allocated = new_allocated; - return likely (!in_error); + return likely (successful); } bool @@ -234,7 +234,7 @@ hb_buffer_t::clear (void) scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT; content_type = HB_BUFFER_CONTENT_TYPE_INVALID; - in_error = false; + successful = true; have_output = false; have_positions = false; @@ -324,7 +324,7 @@ hb_buffer_t::clear_positions (void) void hb_buffer_t::swap_buffers (void) { - if (unlikely (in_error)) return; + if (unlikely (!successful)) return; assert (have_output); have_output = false; @@ -409,7 +409,7 @@ hb_buffer_t::move_to (unsigned int i) idx = i; return true; } - if (unlikely (in_error)) + if (unlikely (!successful)) return false; assert (i <= out_len + (len - idx)); @@ -687,6 +687,8 @@ hb_buffer_t::guess_segment_properties (void) /* If direction is set to INVALID, guess from script */ if (props.direction == HB_DIRECTION_INVALID) { props.direction = hb_script_get_horizontal_direction (props.script); + if (props.direction == HB_DIRECTION_INVALID) + props.direction = HB_DIRECTION_LTR; } /* If language is not set, use default language from locale */ @@ -754,7 +756,7 @@ hb_buffer_get_empty (void) HB_BUFFER_CONTENT_TYPE_INVALID, HB_SEGMENT_PROPERTIES_DEFAULT, - true, /* in_error */ + false, /* successful */ true, /* have_output */ true /* have_positions */ @@ -1269,7 +1271,7 @@ hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size) hb_bool_t hb_buffer_allocation_successful (hb_buffer_t *buffer) { - return !buffer->in_error; + return buffer->successful; } /** @@ -1489,6 +1491,8 @@ hb_buffer_reverse_clusters (hb_buffer_t *buffer) * Next, if buffer direction is not set (ie. is %HB_DIRECTION_INVALID), * it will be set to the natural horizontal direction of the * buffer script as returned by hb_script_get_horizontal_direction(). + * If hb_script_get_horizontal_direction() returns %HB_DIRECTION_INVALID, + * then %HB_DIRECTION_LTR is used. * * Finally, if buffer language is not set (ie. is %HB_LANGUAGE_INVALID), * it will be set to the process's default language as returned by @@ -1750,13 +1754,13 @@ hb_buffer_append (hb_buffer_t *buffer, if (buffer->len + (end - start) < buffer->len) /* Overflows. */ { - buffer->in_error = true; + buffer->successful = false; return; } unsigned int orig_len = buffer->len; hb_buffer_set_length (buffer, buffer->len + (end - start)); - if (buffer->in_error) + if (unlikely (!buffer->successful)) return; memcpy (buffer->info + orig_len, source->info + start, (end - start) * sizeof (buffer->info[0])); diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-common.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-common.cc index 21e00d6409a..4451ce9f8d8 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-common.cc +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-common.cc @@ -225,7 +225,7 @@ struct hb_language_item_t { inline hb_language_item_t & operator = (const char *s) { /* If a custom allocated is used calling strdup() pairs - badly with a call to the custom free() in finish() below. + badly with a call to the custom free() in fini() below. Therefore don't call strdup(), implement its behavior. */ size_t len = strlen(s) + 1; @@ -240,7 +240,7 @@ struct hb_language_item_t { return *this; } - void finish (void) { free ((void *) lang); } + void fini (void) { free ((void *) lang); } }; @@ -252,11 +252,16 @@ static hb_language_item_t *langs; static void free_langs (void) { - while (langs) { - hb_language_item_t *next = langs->next; - langs->finish (); - free (langs); - langs = next; +retry: + hb_language_item_t *first_lang = (hb_language_item_t *) hb_atomic_ptr_get (&langs); + if (!hb_atomic_ptr_cmpexch (&langs, first_lang, nullptr)) + goto retry; + + while (first_lang) { + hb_language_item_t *next = first_lang->next; + first_lang->fini (); + free (first_lang); + first_lang = next; } } #endif @@ -284,7 +289,7 @@ retry: } if (!hb_atomic_ptr_cmpexch (&langs, first_lang, lang)) { - lang->finish (); + lang->fini (); free (lang); goto retry; } @@ -407,7 +412,7 @@ hb_script_from_iso15924_tag (hb_tag_t tag) case HB_TAG('Q','a','a','i'): return HB_SCRIPT_INHERITED; case HB_TAG('Q','a','a','c'): return HB_SCRIPT_COPTIC; - /* Script variants from http://unicode.org/iso15924/ */ + /* Script variants from https://unicode.org/iso15924/ */ case HB_TAG('C','y','r','s'): return HB_SCRIPT_CYRILLIC; case HB_TAG('L','a','t','f'): return HB_SCRIPT_LATIN; case HB_TAG('L','a','t','g'): return HB_SCRIPT_LATIN; @@ -475,7 +480,7 @@ hb_script_to_iso15924_tag (hb_script_t script) hb_direction_t hb_script_get_horizontal_direction (hb_script_t script) { - /* http://goo.gl/x9ilM */ + /* https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o */ switch ((hb_tag_t) script) { /* Unicode-1.1 additions */ @@ -530,7 +535,18 @@ hb_script_get_horizontal_direction (hb_script_t script) /* Unicode-9.0 additions */ case HB_SCRIPT_ADLAM: + /* Unicode-11.0 additions */ + case HB_SCRIPT_HANIFI_ROHINGYA: + case HB_SCRIPT_OLD_SOGDIAN: + case HB_SCRIPT_SOGDIAN: + return HB_DIRECTION_RTL; + + + /* https://github.com/harfbuzz/harfbuzz/issues/1000 */ + case HB_SCRIPT_OLD_ITALIC: + + return HB_DIRECTION_INVALID; } return HB_DIRECTION_LTR; @@ -719,8 +735,14 @@ static HB_LOCALE_T C_locale; static void free_C_locale (void) { - if (C_locale) - HB_FREE_LOCALE (C_locale); +retry: + HB_LOCALE_T locale = (HB_LOCALE_T) hb_atomic_ptr_get (&C_locale); + + if (!hb_atomic_ptr_cmpexch (&C_locale, locale, nullptr)) + goto retry; + + if (locale) + HB_FREE_LOCALE (locale); } #endif diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-common.h b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-common.h index c6fa84f534a..8f44db67b92 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-common.h +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-common.h @@ -49,6 +49,16 @@ # include #elif defined (_AIX) # include +#elif defined (_MSC_VER) && _MSC_VER < 1600 +/* VS 2010 (_MSC_VER 1600) has stdint.h */ +typedef __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; #else # include #endif @@ -142,8 +152,8 @@ hb_language_get_default (void); /* hb_script_t */ -/* http://unicode.org/iso15924/ */ -/* http://goo.gl/x9ilM */ +/* https://unicode.org/iso15924/ */ +/* https://docs.google.com/spreadsheets/d/1Y90M0Ie3MUJ6UVCRDOypOtijlMDLNNyyLk36T6iMu0o */ /* Unicode Character Database property: Script (sc) */ typedef enum { @@ -315,6 +325,17 @@ typedef enum /*10.0*/HB_SCRIPT_SOYOMBO = HB_TAG ('S','o','y','o'), /*10.0*/HB_SCRIPT_ZANABAZAR_SQUARE = HB_TAG ('Z','a','n','b'), + /* + * Since 1.8.0 + */ + /*11.0*/HB_SCRIPT_DOGRA = HB_TAG ('D','o','g','r'), + /*11.0*/HB_SCRIPT_GUNJALA_GONDI = HB_TAG ('G','o','n','g'), + /*11.0*/HB_SCRIPT_HANIFI_ROHINGYA = HB_TAG ('R','o','h','g'), + /*11.0*/HB_SCRIPT_MAKASAR = HB_TAG ('M','a','k','a'), + /*11.0*/HB_SCRIPT_MEDEFAIDRIN = HB_TAG ('M','e','d','f'), + /*11.0*/HB_SCRIPT_OLD_SOGDIAN = HB_TAG ('S','o','g','o'), + /*11.0*/HB_SCRIPT_SOGDIAN = HB_TAG ('S','o','g','d'), + /* No script set. */ HB_SCRIPT_INVALID = HB_TAG_NONE, @@ -323,7 +344,7 @@ typedef enum * since technically enums are int, and indeed, hb_script_t ends up being signed. * See this thread for technicalities: * - * http://lists.freedesktop.org/archives/harfbuzz/2014-March/004150.html + * https://lists.freedesktop.org/archives/harfbuzz/2014-March/004150.html */ _HB_SCRIPT_MAX_VALUE = HB_TAG_MAX, /*< skip >*/ _HB_SCRIPT_MAX_VALUE_SIGNED = HB_TAG_MAX_SIGNED /*< skip >*/ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-coretext.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-coretext.cc index fd832dc4dab..c7520ea0d77 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-coretext.cc +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-coretext.cc @@ -168,6 +168,10 @@ create_ct_font (CGFontRef cg_font, CGFloat font_size) if (CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSText")) || CFStringHasPrefix (cg_postscript_name, CFSTR (".SFNSDisplay"))) { +#if MAC_OS_X_VERSION_MIN_REQUIRED < 1080 +# define kCTFontUIFontSystem kCTFontSystemFontType +# define kCTFontUIFontEmphasizedSystem kCTFontEmphasizedSystemFontType +#endif CTFontUIFontType font_type = kCTFontUIFontSystem; if (CFStringHasSuffix (cg_postscript_name, CFSTR ("-Bold"))) font_type = kCTFontUIFontEmphasizedSystem; @@ -206,7 +210,18 @@ create_ct_font (CGFontRef cg_font, CGFloat font_size) return ct_font; } - CFURLRef original_url = (CFURLRef)CTFontCopyAttribute(ct_font, kCTFontURLAttribute); + CFURLRef original_url = nullptr; +#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 + ATSFontRef atsFont; + FSRef fsref; + OSStatus status; + atsFont = CTFontGetPlatformFont (ct_font, NULL); + status = ATSFontGetFileReference (atsFont, &fsref); + if (status == noErr) + original_url = CFURLCreateFromFSRef (NULL, &fsref); +#else + original_url = (CFURLRef) CTFontCopyAttribute (ct_font, kCTFontURLAttribute); +#endif /* Create font copy with cascade list that has LastResort first; this speeds up CoreText * font fallback which we don't need anyway. */ @@ -225,7 +240,15 @@ create_ct_font (CGFontRef cg_font, CGFloat font_size) * system locations that we cannot access from the sandboxed renderer * process in Blink. This can be detected by the new file URL location * that the newly found font points to. */ - CFURLRef new_url = (CFURLRef) CTFontCopyAttribute (new_ct_font, kCTFontURLAttribute); + CFURLRef new_url = nullptr; +#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 + atsFont = CTFontGetPlatformFont (new_ct_font, NULL); + status = ATSFontGetFileReference (atsFont, &fsref); + if (status == noErr) + new_url = CFURLCreateFromFSRef (NULL, &fsref); +#else + new_url = (CFURLRef) CTFontCopyAttribute (new_ct_font, kCTFontURLAttribute); +#endif // Keep reconfigured font if URL cannot be retrieved (seems to be the case // on Mac OS 10.12 Sierra), speculative fix for crbug.com/625606 if (!original_url || !new_url || CFEqual (original_url, new_url)) { @@ -618,8 +641,8 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, buffer->merge_clusters (i - 1, i + 1); } - hb_auto_array_t feature_records; - hb_auto_array_t range_records; + hb_auto_t > feature_records; + hb_auto_t > range_records; /* * Set up features. @@ -628,7 +651,7 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, if (num_features) { /* Sort features by start/end events. */ - hb_auto_array_t feature_events; + hb_auto_t > feature_events; for (unsigned int i = 0; i < num_features; i++) { const feature_mapping_t * mapping = (const feature_mapping_t *) bsearch (&features[i].tag, @@ -647,15 +670,11 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, feature_event_t *event; event = feature_events.push (); - if (unlikely (!event)) - goto fail_features; event->index = features[i].start; event->start = true; event->feature = feature; event = feature_events.push (); - if (unlikely (!event)) - goto fail_features; event->index = features[i].end; event->start = false; event->feature = feature; @@ -669,15 +688,13 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, feature.order = num_features + 1; feature_event_t *event = feature_events.push (); - if (unlikely (!event)) - goto fail_features; event->index = 0; /* This value does magic. */ event->start = false; event->feature = feature; } /* Scan events and save features for each range. */ - hb_auto_array_t active_features; + hb_auto_t > active_features; unsigned int last_index = 0; for (unsigned int i = 0; i < feature_events.len; i++) { @@ -687,8 +704,6 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, { /* Save a snapshot of active features and the range. */ range_record_t *range = range_records.push (); - if (unlikely (!range)) - goto fail_features; if (active_features.len) { @@ -746,23 +761,16 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, last_index = event->index; } - if (event->start) { - active_feature_t *feature = active_features.push (); - if (unlikely (!feature)) - goto fail_features; - *feature = event->feature; + if (event->start) + { + active_features.push (event->feature); } else { active_feature_t *feature = active_features.find (&event->feature); if (feature) - active_features.remove (feature - active_features.array); + active_features.remove (feature - active_features.arrayZ); } } } - else - { - fail_features: - num_features = 0; - } unsigned int scratch_size; hb_buffer_t::scratch_buffer_t *scratch = buffer->get_scratch_buffer (&scratch_size); @@ -944,6 +952,9 @@ resize_and_retry: int level = HB_DIRECTION_IS_FORWARD (buffer->props.direction) ? 0 : 1; CFNumberRef level_number = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &level); +#if MAC_OS_X_VERSION_MIN_REQUIRED < 1060 + extern const CFStringRef kCTTypesetterOptionForcedEmbeddingLevel; +#endif CFDictionaryRef options = CFDictionaryCreate (kCFAllocatorDefault, (const void **) &kCTTypesetterOptionForcedEmbeddingLevel, (const void **) &level_number, @@ -979,7 +990,7 @@ resize_and_retry: /* For right-to-left runs, CoreText returns the glyphs positioned such that * any trailing whitespace is to the left of (0,0). Adjust coordinate system * to fix for that. Test with any RTL string with trailing spaces. - * https://code.google.com/p/chromium/issues/detail?id=469028 + * https://crbug.com/469028 */ if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction)) { @@ -1032,7 +1043,7 @@ resize_and_retry: * However, even that wouldn't work if we were passed in the CGFont to * construct a hb_face to begin with. * - * See: http://github.com/harfbuzz/harfbuzz/pull/36 + * See: https://github.com/harfbuzz/harfbuzz/pull/36 * * Also see: https://bugs.chromium.org/p/chromium/issues/detail?id=597098 */ @@ -1222,7 +1233,7 @@ resize_and_retry: * directions. As such, disable the assert... It wouldn't crash, but * cursoring will be off... * - * http://crbug.com/419769 + * https://crbug.com/419769 */ if (0) { diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-dsalgs.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-dsalgs.hh index 2e37b9d87b6..0e043f2b324 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-dsalgs.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-dsalgs.hh @@ -49,7 +49,7 @@ hb_bsearch_r (const void *key, const void *base, else return (void *) p; } - return NULL; + return nullptr; } diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-face.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-face.cc index f69c8fc6412..445246bd0d6 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-face.cc +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-face.cc @@ -29,11 +29,35 @@ #include "hb-private.hh" #include "hb-face-private.hh" +#include "hb-blob-private.hh" #include "hb-open-file-private.hh" #include "hb-ot-head-table.hh" #include "hb-ot-maxp-table.hh" + +/** + * hb_face_count: Get number of faces on the blob + * @blob: + * + * + * + * Return value: Number of faces on the blob + * + * Since: 1.7.7 + **/ +unsigned int +hb_face_count (hb_blob_t *blob) +{ + if (unlikely (!blob)) + return 0; + + hb_blob_t *sanitized = OT::Sanitizer ().sanitize (blob); + const OT::OpenTypeFontFile& ot = *sanitized->as (); + + return ot.get_face_count (); +} + /* * hb_face_t */ @@ -134,7 +158,7 @@ _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void if (tag == HB_TAG_NONE) return hb_blob_reference (data->blob); - const OT::OpenTypeFontFile &ot_file = *OT::Sanitizer::lock_instance (data->blob); + const OT::OpenTypeFontFile &ot_file = *data->blob->as (); const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index); const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag); @@ -425,7 +449,7 @@ void hb_face_t::load_upem (void) const { hb_blob_t *head_blob = OT::Sanitizer().sanitize (reference_table (HB_OT_TAG_head)); - const OT::head *head_table = OT::Sanitizer::lock_instance (head_blob); + const OT::head *head_table = head_blob->as (); upem = head_table->get_upem (); hb_blob_destroy (head_blob); } @@ -469,7 +493,7 @@ void hb_face_t::load_num_glyphs (void) const { hb_blob_t *maxp_blob = OT::Sanitizer().sanitize (reference_table (HB_OT_TAG_maxp)); - const OT::maxp *maxp_table = OT::Sanitizer::lock_instance (maxp_blob); + const OT::maxp *maxp_table = maxp_blob->as (); num_glyphs = maxp_table->get_num_glyphs (); hb_blob_destroy (maxp_blob); } @@ -499,7 +523,7 @@ hb_face_get_table_tags (hb_face_t *face, hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data; - const OT::OpenTypeFontFile &ot_file = *OT::Sanitizer::lock_instance (data->blob); + const OT::OpenTypeFontFile &ot_file = *data->blob->as (); const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index); return ot_face.get_table_tags (start_offset, table_count, table_tags); diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-face.h b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-face.h index 332195902ea..15555bf4d22 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-face.h +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-face.h @@ -37,6 +37,10 @@ HB_BEGIN_DECLS +HB_EXTERN unsigned int +hb_face_count (hb_blob_t *blob); + + /* * hb_face_t */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-font-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-font-private.hh index 6bca15142a3..aaf2d06c4cc 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-font-private.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-font-private.hh @@ -83,7 +83,11 @@ struct hb_font_funcs_t { HB_FONT_FUNCS_IMPLEMENT_CALLBACKS #undef HB_FONT_FUNC_IMPLEMENT } f; - void (*array[VAR]) (void); + void (*array[0 +#define HB_FONT_FUNC_IMPLEMENT(name) +1 + HB_FONT_FUNCS_IMPLEMENT_CALLBACKS +#undef HB_FONT_FUNC_IMPLEMENT + ]) (void); } get; }; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-font.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-font.cc index e7b2d7b9464..7553f239029 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-font.cc +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-font.cc @@ -127,7 +127,7 @@ hb_font_get_variation_glyph_parent (hb_font_t *font, static hb_position_t -hb_font_get_glyph_h_advance_nil (hb_font_t *font HB_UNUSED, +hb_font_get_glyph_h_advance_nil (hb_font_t *font, void *font_data HB_UNUSED, hb_codepoint_t glyph, void *user_data HB_UNUSED) @@ -144,7 +144,7 @@ hb_font_get_glyph_h_advance_parent (hb_font_t *font, } static hb_position_t -hb_font_get_glyph_v_advance_nil (hb_font_t *font HB_UNUSED, +hb_font_get_glyph_v_advance_nil (hb_font_t *font, void *font_data HB_UNUSED, hb_codepoint_t glyph, void *user_data HB_UNUSED) diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ft.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ft.cc index 994d68ac77f..8d3865bad5a 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ft.cc +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ft.cc @@ -119,7 +119,7 @@ hb_ft_font_set_load_flags (hb_font_t *font, int load_flags) if (font->immutable) return; - if (font->destroy != _hb_ft_font_destroy) + if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy) return; hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data; @@ -139,7 +139,7 @@ hb_ft_font_set_load_flags (hb_font_t *font, int load_flags) int hb_ft_font_get_load_flags (hb_font_t *font) { - if (font->destroy != _hb_ft_font_destroy) + if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy) return 0; const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data; @@ -150,7 +150,7 @@ hb_ft_font_get_load_flags (hb_font_t *font) FT_Face hb_ft_font_get_face (hb_font_t *font) { - if (font->destroy != _hb_ft_font_destroy) + if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy) return nullptr; const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font->user_data; @@ -177,7 +177,7 @@ hb_ft_get_nominal_glyph (hb_font_t *font HB_UNUSED, /* For symbol-encoded OpenType fonts, we duplicate the * U+F000..F0FF range at U+0000..U+00FF. That's what * Windows seems to do, and that's hinted about at: - * http://www.microsoft.com/typography/otspec/recom.htm + * https://docs.microsoft.com/en-us/typography/opentype/spec/recom * under "Non-Standard (Symbol) Fonts". */ g = FT_Get_Char_Index (ft_font->ft_face, 0xF000u + unicode); if (!g) @@ -210,7 +210,7 @@ hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED, } static hb_position_t -hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED, +hb_ft_get_glyph_h_advance (hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data HB_UNUSED) @@ -228,7 +228,7 @@ hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED, } static hb_position_t -hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED, +hb_ft_get_glyph_v_advance (hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data HB_UNUSED) @@ -248,7 +248,7 @@ hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED, } static hb_bool_t -hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED, +hb_ft_get_glyph_v_origin (hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_position_t *x, @@ -292,7 +292,7 @@ hb_ft_get_glyph_h_kerning (hb_font_t *font, } static hb_bool_t -hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED, +hb_ft_get_glyph_extents (hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents, @@ -423,7 +423,12 @@ static hb_font_funcs_t *static_ft_funcs = nullptr; static void free_static_ft_funcs (void) { - hb_font_funcs_destroy (static_ft_funcs); +retry: + hb_font_funcs_t *ft_funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ft_funcs); + if (!hb_atomic_ptr_cmpexch (&static_ft_funcs, ft_funcs, nullptr)) + goto retry; + + hb_font_funcs_destroy (ft_funcs); } #endif @@ -610,7 +615,7 @@ hb_ft_font_create (FT_Face ft_face, void hb_ft_font_changed (hb_font_t *font) { - if (font->destroy != _hb_ft_font_destroy) + if (font->destroy != (hb_destroy_func_t) _hb_ft_font_destroy) return; hb_ft_font_t *ft_font = (hb_ft_font_t *) font->user_data; @@ -685,7 +690,12 @@ static FT_Library ft_library; static void free_ft_library (void) { - FT_Done_FreeType (ft_library); +retry: + FT_Library library = (FT_Library) hb_atomic_ptr_get (&ft_library); + if (!hb_atomic_ptr_cmpexch (&ft_library, library, nullptr)) + goto retry; + + FT_Done_FreeType (library); } #endif diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map-private.hh new file mode 100644 index 00000000000..3cbbce59efc --- /dev/null +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map-private.hh @@ -0,0 +1,255 @@ +/* + * Copyright © 2018 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#ifndef HB_MAP_PRIVATE_HH +#define HB_MAP_PRIVATE_HH + +#include "hb-private.hh" +#include "hb-object-private.hh" + + +template +inline uint32_t Hash (const T &v) +{ + /* Knuth's multiplicative method: */ + return (uint32_t) v * 2654435761u; +} + + +/* + * hb_map_t + */ + +struct hb_map_t +{ + struct item_t + { + hb_codepoint_t key; + hb_codepoint_t value; + + inline bool is_unused (void) const { return key == INVALID; } + inline bool is_tombstone (void) const { return key != INVALID && value == INVALID; } + }; + + hb_object_header_t header; + bool successful; /* Allocations successful */ + unsigned int population; /* Not including tombstones. */ + unsigned int occupancy; /* Including tombstones. */ + unsigned int mask; + unsigned int prime; + item_t *items; + + inline void init_shallow (void) + { + successful = true; + population = occupancy = 0; + mask = 0; + prime = 0; + items = nullptr; + } + inline void init (void) + { + hb_object_init (this); + init_shallow (); + } + inline void fini_shallow (void) + { + free (items); + } + inline void fini (void) + { + hb_object_fini (this); + fini_shallow (); + } + + inline bool resize (void) + { + if (unlikely (!successful)) return false; + + unsigned int power = _hb_bit_storage (population * 2 + 8); + unsigned int new_size = 1u << power; + item_t *new_items = (item_t *) malloc ((size_t) new_size * sizeof (item_t)); + if (unlikely (!new_items)) + { + successful = false; + return false; + } + memset (new_items, 0xFF, (size_t) new_size * sizeof (item_t)); + + unsigned int old_size = mask + 1; + item_t *old_items = items; + + /* Switch to new, empty, array. */ + population = occupancy = 0; + mask = new_size - 1; + prime = prime_for (power); + items = new_items; + + /* Insert back old items. */ + if (old_items) + for (unsigned int i = 0; i < old_size; i++) + if (old_items[i].key != INVALID && old_items[i].value != INVALID) + set (old_items[i].key, old_items[i].value); + + free (old_items); + + return true; + } + + inline void set (hb_codepoint_t key, hb_codepoint_t value) + { + if (unlikely (!successful)) return; + if (unlikely (key == INVALID)) return; + if ((occupancy + occupancy / 2) >= mask && !resize ()) return; + unsigned int i = bucket_for (key); + + if (value == INVALID && items[i].key != key) + return; /* Trying to delete non-existent key. */ + + if (!items[i].is_unused ()) + { + occupancy--; + if (items[i].is_tombstone ()) + population--; + } + + items[i].key = key; + items[i].value = value; + + occupancy++; + if (!items[i].is_tombstone ()) + population++; + + } + inline hb_codepoint_t get (hb_codepoint_t key) const + { + if (unlikely (!items)) return INVALID; + unsigned int i = bucket_for (key); + return items[i].key == key ? items[i].value : INVALID; + } + + inline void del (hb_codepoint_t key) + { + set (key, INVALID); + } + inline bool has (hb_codepoint_t key) const + { + return get (key) != INVALID; + } + + inline hb_codepoint_t operator [] (unsigned int key) const + { return get (key); } + + static const hb_codepoint_t INVALID = HB_MAP_VALUE_INVALID; + + inline void clear (void) + { + memset (items, 0xFF, ((size_t) mask + 1) * sizeof (item_t)); + population = occupancy = 0; + } + + inline bool is_empty (void) const + { + return population != 0; + } + + inline unsigned int get_population () const + { + return population; + } + + protected: + + inline unsigned int bucket_for (hb_codepoint_t key) const + { + unsigned int i = Hash (key) % prime; + unsigned int step = 0; + unsigned int tombstone = INVALID; + while (!items[i].is_unused ()) + { + if (items[i].key == key) + return i; + if (tombstone == INVALID && items[i].is_tombstone ()) + tombstone = i; + i = (i + ++step) & mask; + } + return tombstone == INVALID ? i : tombstone; + } + + static inline unsigned int prime_for (unsigned int shift) + { + /* Following comment and table copied from glib. */ + /* Each table size has an associated prime modulo (the first prime + * lower than the table size) used to find the initial bucket. Probing + * then works modulo 2^n. The prime modulo is necessary to get a + * good distribution with poor hash functions. + */ + /* Not declaring static to make all kinds of compilers happy... */ + /*static*/ const unsigned int prime_mod [32] = + { + 1, /* For 1 << 0 */ + 2, + 3, + 7, + 13, + 31, + 61, + 127, + 251, + 509, + 1021, + 2039, + 4093, + 8191, + 16381, + 32749, + 65521, /* For 1 << 16 */ + 131071, + 262139, + 524287, + 1048573, + 2097143, + 4194301, + 8388593, + 16777213, + 33554393, + 67108859, + 134217689, + 268435399, + 536870909, + 1073741789, + 2147483647 /* For 1 << 31 */ + }; + + if (unlikely (shift >= ARRAY_LENGTH (prime_mod))) + return prime_mod[ARRAY_LENGTH (prime_mod) - 1]; + + return prime_mod[shift]; + } +}; + + +#endif /* HB_MAP_PRIVATE_HH */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map.cc new file mode 100644 index 00000000000..270d5796a9b --- /dev/null +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map.cc @@ -0,0 +1,261 @@ +/* + * Copyright © 2018 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#include "hb-map-private.hh" + + +/* Public API */ + + +/** + * hb_map_create: (Xconstructor) + * + * Return value: (transfer full): + * + * Since: 1.7.7 + **/ +hb_map_t * +hb_map_create (void) +{ + hb_map_t *map; + + if (!(map = hb_object_create ())) + return hb_map_get_empty (); + + map->init_shallow (); + + return map; +} + +/** + * hb_map_get_empty: + * + * Return value: (transfer full): + * + * Since: 1.7.7 + **/ +hb_map_t * +hb_map_get_empty (void) +{ + return const_cast (&Null(hb_map_t)); +} + +/** + * hb_map_reference: (skip) + * @map: a map. + * + * Return value: (transfer full): + * + * Since: 1.7.7 + **/ +hb_map_t * +hb_map_reference (hb_map_t *map) +{ + return hb_object_reference (map); +} + +/** + * hb_map_destroy: (skip) + * @map: a map. + * + * Since: 1.7.7 + **/ +void +hb_map_destroy (hb_map_t *map) +{ + if (!hb_object_destroy (map)) return; + + map->fini_shallow (); + + free (map); +} + +/** + * hb_map_set_user_data: (skip) + * @map: a map. + * @key: + * @data: + * @destroy: + * @replace: + * + * Return value: + * + * Since: 1.7.7 + **/ +hb_bool_t +hb_map_set_user_data (hb_map_t *map, + hb_user_data_key_t *key, + void * data, + hb_destroy_func_t destroy, + hb_bool_t replace) +{ + return hb_object_set_user_data (map, key, data, destroy, replace); +} + +/** + * hb_map_get_user_data: (skip) + * @map: a map. + * @key: + * + * Return value: (transfer none): + * + * Since: 1.7.7 + **/ +void * +hb_map_get_user_data (hb_map_t *map, + hb_user_data_key_t *key) +{ + return hb_object_get_user_data (map, key); +} + + +/** + * hb_map_allocation_successful: + * @map: a map. + * + * + * + * Return value: + * + * Since: 1.7.7 + **/ +hb_bool_t +hb_map_allocation_successful (const hb_map_t *map) +{ + return map->successful; +} + + +/** + * hb_map_set: + * @map: a map. + * @key: + * @value: + * + * + * + * Return value: + * + * Since: 1.7.7 + **/ +void +hb_map_set (hb_map_t *map, + hb_codepoint_t key, + hb_codepoint_t value) +{ + map->set (key, value); +} + +/** + * hb_map_get: + * @map: a map. + * @key: + * + * + * + * Since: 1.7.7 + **/ +hb_codepoint_t +hb_map_get (const hb_map_t *map, + hb_codepoint_t key) +{ + return map->get (key); +} + +/** + * hb_map_del: + * @map: a map. + * @codepoint: + * + * + * + * Since: 1.7.7 + **/ +void +hb_map_del (hb_map_t *map, + hb_codepoint_t key) +{ + map->del (key); +} + +/** + * hb_map_has: + * @map: a map. + * @codepoint: + * + * + * + * Since: 1.7.7 + **/ +hb_bool_t +hb_map_has (const hb_map_t *map, + hb_codepoint_t key) +{ + return map->has (key); +} + + +/** + * hb_map_clear: + * @map: a map. + * + * + * + * Since: 1.7.7 + **/ +void +hb_map_clear (hb_map_t *map) +{ + return map->clear (); +} + +/** + * hb_map_is_empty: + * @map: a map. + * + * + * + * Since: 1.7.7 + **/ +hb_bool_t +hb_map_is_empty (const hb_map_t *map) +{ + return map->is_empty (); +} + +/** + * hb_map_get_population: + * @map: a map. + * + * + * + * Since: 1.7.7 + **/ +unsigned int +hb_map_get_population (const hb_map_t *map) +{ + return map->get_population (); +} diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map.h b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map.h new file mode 100644 index 00000000000..d829e58c217 --- /dev/null +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-map.h @@ -0,0 +1,104 @@ +/* + * Copyright © 2018 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#ifndef HB_H_IN +#error "Include instead." +#endif + +#ifndef HB_MAP_H +#define HB_MAP_H + +#include "hb-common.h" + +HB_BEGIN_DECLS + + +/* + * Since: 1.7.7 + */ +#define HB_MAP_VALUE_INVALID ((hb_codepoint_t) -1) + +typedef struct hb_map_t hb_map_t; + + +HB_EXTERN hb_map_t * +hb_map_create (void); + +HB_EXTERN hb_map_t * +hb_map_get_empty (void); + +HB_EXTERN hb_map_t * +hb_map_reference (hb_map_t *map); + +HB_EXTERN void +hb_map_destroy (hb_map_t *map); + +HB_EXTERN hb_bool_t +hb_map_set_user_data (hb_map_t *map, + hb_user_data_key_t *key, + void * data, + hb_destroy_func_t destroy, + hb_bool_t replace); + +HB_EXTERN void * +hb_map_get_user_data (hb_map_t *map, + hb_user_data_key_t *key); + + +/* Returns false if allocation has failed before */ +HB_EXTERN hb_bool_t +hb_map_allocation_successful (const hb_map_t *map); + +HB_EXTERN void +hb_map_clear (hb_map_t *map); + +HB_EXTERN hb_bool_t +hb_map_is_empty (const hb_map_t *map); + +HB_EXTERN unsigned int +hb_map_get_population (const hb_map_t *map); + +HB_EXTERN void +hb_map_set (hb_map_t *map, + hb_codepoint_t key, + hb_codepoint_t value); + +HB_EXTERN hb_codepoint_t +hb_map_get (const hb_map_t *map, + hb_codepoint_t key); + +HB_EXTERN void +hb_map_del (hb_map_t *map, + hb_codepoint_t key); + +HB_EXTERN hb_bool_t +hb_map_has (const hb_map_t *map, + hb_codepoint_t key); + + +HB_END_DECLS + +#endif /* HB_MAP_H */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-mutex-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-mutex-private.hh index 36bbc38e171..e33d02c8eb9 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-mutex-private.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-mutex-private.hh @@ -134,7 +134,7 @@ struct hb_mutex_t inline void init (void) { hb_mutex_impl_init (&m); } inline void lock (void) { hb_mutex_impl_lock (&m); } inline void unlock (void) { hb_mutex_impl_unlock (&m); } - inline void finish (void) { hb_mutex_impl_finish (&m); } + inline void fini (void) { hb_mutex_impl_finish (&m); } }; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-object-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-object-private.hh index 48d717e5644..a21185353a7 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-object-private.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-object-private.hh @@ -41,9 +41,9 @@ /* reference_count */ -#define HB_REFERENCE_COUNT_INERT_VALUE -1 +#define HB_REFERENCE_COUNT_INERT_VALUE 0 #define HB_REFERENCE_COUNT_POISON_VALUE -0x0000DEAD -#define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT(HB_REFERENCE_COUNT_INERT_VALUE)} +#define HB_REFERENCE_COUNT_INIT {HB_ATOMIC_INT_INIT (HB_REFERENCE_COUNT_INERT_VALUE)} struct hb_reference_count_t { @@ -53,7 +53,7 @@ struct hb_reference_count_t inline int get_unsafe (void) const { return ref_count.get_unsafe (); } inline int inc (void) { return ref_count.inc (); } inline int dec (void) { return ref_count.dec (); } - inline void finish (void) { ref_count.set_unsafe (HB_REFERENCE_COUNT_POISON_VALUE); } + inline void fini (void) { ref_count.set_unsafe (HB_REFERENCE_COUNT_POISON_VALUE); } inline bool is_inert (void) const { return ref_count.get_unsafe () == HB_REFERENCE_COUNT_INERT_VALUE; } inline bool is_valid (void) const { return ref_count.get_unsafe () > 0; } @@ -62,7 +62,6 @@ struct hb_reference_count_t /* user_data */ -#define HB_USER_DATA_ARRAY_INIT {HB_MUTEX_INIT, HB_LOCKABLE_SET_INIT} struct hb_user_data_array_t { struct hb_user_data_item_t { @@ -73,7 +72,7 @@ struct hb_user_data_array_t inline bool operator == (hb_user_data_key_t *other_key) const { return key == other_key; } inline bool operator == (hb_user_data_item_t &other) const { return key == other.key; } - void finish (void) { if (destroy) destroy (data); } + void fini (void) { if (destroy) destroy (data); } }; hb_mutex_t lock; @@ -88,7 +87,7 @@ struct hb_user_data_array_t HB_INTERNAL void *get (hb_user_data_key_t *key); - inline void finish (void) { items.finish (lock); lock.finish (); } + inline void fini (void) { items.fini (lock); lock.fini (); } }; @@ -97,9 +96,9 @@ struct hb_user_data_array_t struct hb_object_header_t { hb_reference_count_t ref_count; - hb_user_data_array_t user_data; + hb_user_data_array_t *user_data; -#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INIT, HB_USER_DATA_ARRAY_INIT} +#define HB_OBJECT_HEADER_STATIC {HB_REFERENCE_COUNT_INIT, nullptr} private: ASSERT_POD (); @@ -133,7 +132,7 @@ template static inline void hb_object_init (Type *obj) { obj->header.ref_count.init (1); - obj->header.user_data.init (); + obj->header.user_data = nullptr; } template static inline bool hb_object_is_inert (const Type *obj) @@ -165,11 +164,20 @@ static inline bool hb_object_destroy (Type *obj) if (obj->header.ref_count.dec () != 1) return false; - obj->header.ref_count.finish (); /* Do this before user_data */ - obj->header.user_data.finish (); + hb_object_fini (obj); return true; } template +static inline void hb_object_fini (Type *obj) +{ + obj->header.ref_count.fini (); /* Do this before user_data */ + if (obj->header.user_data) + { + obj->header.user_data->fini (); + free (obj->header.user_data); + } +} +template static inline bool hb_object_set_user_data (Type *obj, hb_user_data_key_t *key, void * data, @@ -179,17 +187,34 @@ static inline bool hb_object_set_user_data (Type *obj, if (unlikely (!obj || hb_object_is_inert (obj))) return false; assert (hb_object_is_valid (obj)); - return obj->header.user_data.set (key, data, destroy, replace); + +retry: + hb_user_data_array_t *user_data = (hb_user_data_array_t *) hb_atomic_ptr_get (&obj->header.user_data); + if (unlikely (!user_data)) + { + user_data = (hb_user_data_array_t *) calloc (sizeof (hb_user_data_array_t), 1); + if (unlikely (!user_data)) + return false; + user_data->init (); + if (unlikely (!hb_atomic_ptr_cmpexch (&obj->header.user_data, nullptr, user_data))) + { + user_data->fini (); + free (user_data); + goto retry; + } + } + + return user_data->set (key, data, destroy, replace); } template static inline void *hb_object_get_user_data (Type *obj, hb_user_data_key_t *key) { - if (unlikely (!obj || hb_object_is_inert (obj))) + if (unlikely (!obj || hb_object_is_inert (obj) || !obj->header.user_data)) return nullptr; assert (hb_object_is_valid (obj)); - return obj->header.user_data.get (key); + return obj->header.user_data->get (key); } diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-open-file-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-open-file-private.hh index 81686b176ad..bcf53757244 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-open-file-private.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-open-file-private.hh @@ -100,7 +100,7 @@ typedef struct OffsetTable else *table_count = MIN (*table_count, tables.len - start_offset); - const TableRecord *sub_tables = tables.array + start_offset; + const TableRecord *sub_tables = tables.arrayZ + start_offset; unsigned int count = *table_count; for (unsigned int i = 0; i < count; i++) table_tags[i] = sub_tables[i].tag; @@ -148,7 +148,7 @@ typedef struct OffsetTable /* Write OffsetTables, alloc for and write actual table blobs. */ for (unsigned int i = 0; i < table_count; i++) { - TableRecord &rec = tables.array[i]; + TableRecord &rec = tables.arrayZ[i]; hb_blob_t *blob = blobs[i]; rec.tag.set (tags[i]); rec.length.set (hb_blob_get_length (blob)); @@ -188,7 +188,7 @@ typedef struct OffsetTable checksum.set_for_data (this, dir_end - (const char *) this); for (unsigned int i = 0; i < table_count; i++) { - TableRecord &rec = tables.array[i]; + TableRecord &rec = tables.arrayZ[i]; checksum.set (checksum + rec.checkSum); } @@ -234,7 +234,7 @@ struct TTCHeaderVersion1 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ FixedVersion<>version; /* Version of the TTC Header (1.0), * 0x00010000u */ - ArrayOf, HBUINT32> + LArrayOf > table; /* Array of offsets to the OffsetTable for each font * from the beginning of the file */ public: @@ -295,11 +295,13 @@ struct OpenTypeFontFile { static const hb_tag_t tableTag = HB_TAG ('_','_','_','_'); /* Sanitizer needs this. */ - static const hb_tag_t CFFTag = HB_TAG ('O','T','T','O'); /* OpenType with Postscript outlines */ - static const hb_tag_t TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ); /* OpenType with TrueType outlines */ - static const hb_tag_t TTCTag = HB_TAG ('t','t','c','f'); /* TrueType Collection */ - static const hb_tag_t TrueTag = HB_TAG ('t','r','u','e'); /* Obsolete Apple TrueType */ - static const hb_tag_t Typ1Tag = HB_TAG ('t','y','p','1'); /* Obsolete Apple Type1 font in SFNT container */ + enum { + CFFTag = HB_TAG ('O','T','T','O'), /* OpenType with Postscript outlines */ + TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ), /* OpenType with TrueType outlines */ + TTCTag = HB_TAG ('t','t','c','f'), /* TrueType Collection */ + TrueTag = HB_TAG ('t','r','u','e'), /* Obsolete Apple TrueType */ + Typ1Tag = HB_TAG ('t','y','p','1') /* Obsolete Apple Type1 font in SFNT container */ + }; inline hb_tag_t get_tag (void) const { return u.tag; } diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-open-type-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-open-type-private.hh index e441f5793ce..07b39066405 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-open-type-private.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-open-type-private.hh @@ -31,6 +31,7 @@ #include "hb-private.hh" #include "hb-debug.hh" +#include "hb-blob-private.hh" #include "hb-face-private.hh" @@ -126,46 +127,6 @@ static inline Type& StructAfter(TObject &X) -/* - * Null objects - */ - -/* Global nul-content Null pool. Enlarge as necessary. */ - -#define HB_NULL_POOL_SIZE 264 -static_assert (HB_NULL_POOL_SIZE % sizeof (void *) == 0, "Align HB_NULL_POOL_SIZE."); - -#ifdef HB_NO_VISIBILITY -static -#else -extern HB_INTERNAL -#endif -const void * const _hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)] -#ifdef HB_NO_VISIBILITY -= {} -#endif -; - -/* Generic nul-content Null objects. */ -template -static inline const Type& Null (void) { - static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE."); - return *CastP (_hb_NullPool); -} - -/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ -#define DEFINE_NULL_DATA(Type, data) \ -static const char _Null##Type[sizeof (Type) + 1] = data; /* +1 is for nul-termination in data */ \ -template <> \ -/*static*/ inline const Type& Null (void) { \ - return *CastP (_Null##Type); \ -} /* The following line really exists such that we end in a place needing semicolon */ \ -static_assert (Type::min_size + 1 <= sizeof (_Null##Type), "Null pool too small. Enlarge.") - -/* Accessor macro. */ -#define Null(Type) Null() - - /* * Dispatch */ @@ -225,7 +186,7 @@ struct hb_sanitize_context_t : inline void start_processing (void) { this->start = hb_blob_get_data (this->blob, nullptr); - this->end = this->start + hb_blob_get_length (this->blob); + this->end = this->start + this->blob->length; assert (this->start <= this->end); /* Must not overflow. */ this->max_ops = MAX ((unsigned int) (this->end - this->start) * HB_SANITIZE_MAX_OPS_FACTOR, (unsigned) HB_SANITIZE_MAX_OPS_MIN); @@ -288,7 +249,7 @@ struct hb_sanitize_context_t : return likely (this->check_range (obj, obj->min_size)); } - inline bool may_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED) + inline bool may_edit (const void *base, unsigned int len) { if (this->edit_count >= HB_SANITIZE_MAX_EDITS) return false; @@ -368,7 +329,7 @@ struct Sanitizer unsigned int edit_count = c->edit_count; if (edit_count && !c->writable) { c->start = hb_blob_get_data_writable (blob, nullptr); - c->end = c->start + hb_blob_get_length (blob); + c->end = c->start + blob->length; if (c->start) { c->writable = true; @@ -383,19 +344,17 @@ struct Sanitizer DEBUG_MSG_FUNC (SANITIZE, c->start, sane ? "PASSED" : "FAILED"); if (sane) + { + blob->lock (); return blob; - else { + } + else + { hb_blob_destroy (blob); return hb_blob_get_empty (); } } - static const Type* lock_instance (hb_blob_t *blob) { - hb_blob_make_immutable (blob); - const char *base = hb_blob_get_data (blob, nullptr); - return unlikely (!base) ? &Null(Type) : CastP (base); - } - inline void set_num_glyphs (unsigned int num_glyphs) { c->num_glyphs = num_glyphs; } private: @@ -672,7 +631,7 @@ typedef IntType HBUINT16; /* 16-bit unsigned integer. */ typedef IntType HBINT16; /* 16-bit signed integer. */ typedef IntType HBUINT32; /* 32-bit unsigned integer. */ typedef IntType HBINT32; /* 32-bit signed integer. */ -typedef IntType UINT24; /* 24-bit unsigned integer. */ +typedef IntType HBUINT24; /* 24-bit unsigned integer. */ /* 16-bit signed integer (HBINT16) that describes a quantity in FUnits. */ typedef HBINT16 FWORD; @@ -683,17 +642,19 @@ typedef HBUINT16 UFWORD; /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */ struct F2DOT14 : HBINT16 { - //inline float to_float (void) const { return ???; } - //inline void set_float (float f) { v.set (f * ???); } + // 16384 means 1<<14 + inline float to_float (void) const { return ((int32_t) v) / 16384.f; } + inline void set_float (float f) { v.set (round (f * 16384.f)); } public: DEFINE_SIZE_STATIC (2); }; /* 32-bit signed fixed-point number (16.16). */ -struct Fixed: HBINT32 +struct Fixed : HBINT32 { - inline float to_float (void) const { return ((int32_t) v) / 65536.0; } - inline void set_float (float f) { v.set (round (f * 65536.0)); } + // 65536 means 1<<16 + inline float to_float (void) const { return ((int32_t) v) / 65536.f; } + inline void set_float (float f) { v.set (round (f * 65536.f)); } public: DEFINE_SIZE_STATIC (4); }; @@ -724,16 +685,19 @@ struct Tag : HBUINT32 public: DEFINE_SIZE_STATIC (4); }; -DEFINE_NULL_DATA (Tag, " "); +DEFINE_NULL_DATA (OT, Tag, " "); /* Glyph index number, same as uint16 (length = 16 bits) */ typedef HBUINT16 GlyphID; +/* Name-table index, same as uint16 (length = 16 bits) */ +typedef HBUINT16 NameID; + /* Script/language-system/feature index */ struct Index : HBUINT16 { static const unsigned int NOT_FOUND_INDEX = 0xFFFFu; }; -DEFINE_NULL_DATA (Index, "\xff\xff"); +DEFINE_NULL_DATA (OT, Index, "\xff\xff"); /* Offset, Null offset = 0 */ template @@ -815,6 +779,12 @@ struct OffsetTo : Offset { unsigned int offset = *this; if (unlikely (!offset)) return Null(Type); + return StructAtOffset (base, offset); + } + inline Type& operator () (void *base) const + { + unsigned int offset = *this; + if (unlikely (!offset)) return Crap(Type); return StructAtOffset (base, offset); } @@ -862,6 +832,89 @@ static inline Type& operator + (Base &base, OffsetTo &offset) * Array Types */ + +/* TODO Use it in ArrayOf, HeadlessArrayOf, and other places around the code base?? */ +template +struct UnsizedArrayOf +{ + inline const Type& operator [] (unsigned int i) const { return arrayZ[i]; } + inline Type& operator [] (unsigned int i) { return arrayZ[i]; } + + inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const + { + TRACE_SANITIZE (this); + if (unlikely (!sanitize_shallow (c, count))) return_trace (false); + + /* Note: for structs that do not reference other structs, + * we do not need to call their sanitize() as we already did + * a bound check on the aggregate array size. We just include + * a small unreachable expression to make sure the structs + * pointed to do have a simple sanitize(), ie. they do not + * reference other structs via offsets. + */ + (void) (false && arrayZ[0].sanitize (c)); + + return_trace (true); + } + inline bool sanitize (hb_sanitize_context_t *c, unsigned int count, const void *base) const + { + TRACE_SANITIZE (this); + if (unlikely (!sanitize_shallow (c, count))) return_trace (false); + for (unsigned int i = 0; i < count; i++) + if (unlikely (!arrayZ[i].sanitize (c, base))) + return_trace (false); + return_trace (true); + } + template + inline bool sanitize (hb_sanitize_context_t *c, unsigned int count, const void *base, T user_data) const + { + TRACE_SANITIZE (this); + if (unlikely (!sanitize_shallow (c, count))) return_trace (false); + for (unsigned int i = 0; i < count; i++) + if (unlikely (!arrayZ[i].sanitize (c, base, user_data))) + return_trace (false); + return_trace (true); + } + + inline bool sanitize_shallow (hb_sanitize_context_t *c, unsigned int count) const + { + TRACE_SANITIZE (this); + return_trace (c->check_array (arrayZ, arrayZ[0].static_size, count)); + } + + public: + Type arrayZ[VAR]; + public: + DEFINE_SIZE_ARRAY (0, arrayZ); +}; + +/* Unsized array of offset's */ +template +struct UnsizedOffsetArrayOf : UnsizedArrayOf > {}; + +/* Unsized array of offsets relative to the beginning of the array itself. */ +template +struct UnsizedOffsetListOf : UnsizedOffsetArrayOf +{ + inline const Type& operator [] (unsigned int i) const + { + return this+this->arrayZ[i]; + } + + inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const + { + TRACE_SANITIZE (this); + return_trace ((UnsizedOffsetArrayOf::sanitize (c, count, this))); + } + template + inline bool sanitize (hb_sanitize_context_t *c, unsigned int count, T user_data) const + { + TRACE_SANITIZE (this); + return_trace ((UnsizedOffsetArrayOf::sanitize (c, count, this, user_data))); + } +}; + + /* An array with a number of elements. */ template struct ArrayOf @@ -875,17 +928,18 @@ struct ArrayOf count -= start_offset; count = MIN (count, *pcount); *pcount = count; - return array + start_offset; + return arrayZ + start_offset; } inline const Type& operator [] (unsigned int i) const { if (unlikely (i >= len)) return Null(Type); - return array[i]; + return arrayZ[i]; } inline Type& operator [] (unsigned int i) { - return array[i]; + if (unlikely (i >= len)) return Crap(Type); + return arrayZ[i]; } inline unsigned int get_size (void) const { return len.static_size + len * Type::static_size; } @@ -907,7 +961,7 @@ struct ArrayOf TRACE_SERIALIZE (this); if (unlikely (!serialize (c, items_len))) return_trace (false); for (unsigned int i = 0; i < items_len; i++) - array[i] = items[i]; + arrayZ[i] = items[i]; items += items_len; return_trace (true); } @@ -924,7 +978,7 @@ struct ArrayOf * pointed to do have a simple sanitize(), ie. they do not * reference other structs via offsets. */ - (void) (false && array[0].sanitize (c)); + (void) (false && arrayZ[0].sanitize (c)); return_trace (true); } @@ -934,7 +988,7 @@ struct ArrayOf if (unlikely (!sanitize_shallow (c))) return_trace (false); unsigned int count = len; for (unsigned int i = 0; i < count; i++) - if (unlikely (!array[i].sanitize (c, base))) + if (unlikely (!arrayZ[i].sanitize (c, base))) return_trace (false); return_trace (true); } @@ -945,7 +999,7 @@ struct ArrayOf if (unlikely (!sanitize_shallow (c))) return_trace (false); unsigned int count = len; for (unsigned int i = 0; i < count; i++) - if (unlikely (!array[i].sanitize (c, base, user_data))) + if (unlikely (!arrayZ[i].sanitize (c, base, user_data))) return_trace (false); return_trace (true); } @@ -955,28 +1009,28 @@ struct ArrayOf { unsigned int count = len; for (unsigned int i = 0; i < count; i++) - if (!this->array[i].cmp (x)) + if (!this->arrayZ[i].cmp (x)) return i; return -1; } inline void qsort (void) { - ::qsort (array, len, sizeof (Type), Type::cmp); + ::qsort (arrayZ, len, sizeof (Type), Type::cmp); } private: inline bool sanitize_shallow (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); - return_trace (len.sanitize (c) && c->check_array (array, Type::static_size, len)); + return_trace (len.sanitize (c) && c->check_array (arrayZ, Type::static_size, len)); } public: LenType len; - Type array[VAR]; + Type arrayZ[VAR]; public: - DEFINE_SIZE_ARRAY (sizeof (LenType), array); + DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ); }; template struct LArrayOf : ArrayOf {}; @@ -991,7 +1045,12 @@ struct OffsetListOf : OffsetArrayOf inline const Type& operator [] (unsigned int i) const { if (unlikely (i >= this->len)) return Null(Type); - return this+this->array[i]; + return this+this->arrayZ[i]; + } + inline const Type& operator [] (unsigned int i) + { + if (unlikely (i >= this->len)) return Crap(Type); + return this+this->arrayZ[i]; } inline bool sanitize (hb_sanitize_context_t *c) const @@ -1015,7 +1074,12 @@ struct HeadlessArrayOf inline const Type& operator [] (unsigned int i) const { if (unlikely (i >= len || !i)) return Null(Type); - return array[i-1]; + return arrayZ[i-1]; + } + inline Type& operator [] (unsigned int i) + { + if (unlikely (i >= len || !i)) return Crap(Type); + return arrayZ[i-1]; } inline unsigned int get_size (void) const { return len.static_size + (len ? len - 1 : 0) * Type::static_size; } @@ -1030,7 +1094,7 @@ struct HeadlessArrayOf if (unlikely (!items_len)) return_trace (true); if (unlikely (!c->extend (*this))) return_trace (false); for (unsigned int i = 0; i < items_len - 1; i++) - array[i] = items[i]; + arrayZ[i] = items[i]; items += items_len - 1; return_trace (true); } @@ -1047,7 +1111,7 @@ struct HeadlessArrayOf * pointed to do have a simple sanitize(), ie. they do not * reference other structs via offsets. */ - (void) (false && array[0].sanitize (c)); + (void) (false && arrayZ[0].sanitize (c)); return_trace (true); } @@ -1057,14 +1121,14 @@ struct HeadlessArrayOf { TRACE_SANITIZE (this); return_trace (len.sanitize (c) && - (!len || c->check_array (array, Type::static_size, len - 1))); + (!len || c->check_array (arrayZ, Type::static_size, len - 1))); } public: LenType len; - Type array[VAR]; + Type arrayZ[VAR]; public: - DEFINE_SIZE_ARRAY (sizeof (LenType), array); + DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ); }; @@ -1078,7 +1142,7 @@ struct SortedArrayOf : ArrayOf inline int bsearch (const SearchType &x) const { /* Hand-coded bsearch here since this is in the hot inner loop. */ - const Type *arr = this->array; + const Type *arr = this->arrayZ; int min = 0, max = (int) this->len - 1; while (min <= max) { @@ -1113,18 +1177,18 @@ struct BinSearchHeader { len.set (v); assert (len == v); - entrySelectorZ.set (MAX (1u, _hb_bit_storage (v)) - 1); - searchRangeZ.set (16 * (1u << entrySelectorZ)); - rangeShiftZ.set (v * 16 > searchRangeZ - ? 16 * v - searchRangeZ - : 0); + entrySelector.set (MAX (1u, _hb_bit_storage (v)) - 1); + searchRange.set (16 * (1u << entrySelector)); + rangeShift.set (v * 16 > searchRange + ? 16 * v - searchRange + : 0); } protected: HBUINT16 len; - HBUINT16 searchRangeZ; - HBUINT16 entrySelectorZ; - HBUINT16 rangeShiftZ; + HBUINT16 searchRange; + HBUINT16 entrySelector; + HBUINT16 rangeShift; public: DEFINE_SIZE_STATIC (8); @@ -1136,7 +1200,7 @@ struct BinSearchArrayOf : SortedArrayOf {}; /* Lazy struct and blob loaders. */ -/* Logic is shared between hb_lazy_loader_t and hb_lazy_table_loader_t */ +/* Logic is shared between hb_lazy_loader_t and hb_table_lazy_loader_t */ template struct hb_lazy_loader_t { @@ -1148,7 +1212,7 @@ struct hb_lazy_loader_t inline void fini (void) { - if (instance && instance != &OT::Null(T)) + if (instance && instance != &Null(T)) { instance->fini(); free (instance); @@ -1163,12 +1227,12 @@ struct hb_lazy_loader_t { p = (T *) calloc (1, sizeof (T)); if (unlikely (!p)) - p = const_cast (&OT::Null(T)); + p = const_cast (&Null(T)); else p->init (face); if (unlikely (!hb_atomic_ptr_cmpexch (const_cast(&instance), nullptr, p))) { - if (p != &OT::Null(T)) + if (p != &Null(T)) p->fini (); goto retry; } @@ -1186,15 +1250,14 @@ struct hb_lazy_loader_t T *instance; }; -/* Logic is shared between hb_lazy_loader_t and hb_lazy_table_loader_t */ +/* Logic is shared between hb_lazy_loader_t and hb_table_lazy_loader_t */ template -struct hb_lazy_table_loader_t +struct hb_table_lazy_loader_t { inline void init (hb_face_t *face_) { face = face_; blob = nullptr; - instance = nullptr; } inline void fini (void) @@ -1205,19 +1268,18 @@ struct hb_lazy_table_loader_t inline const T* get (void) const { retry: - T *p = (T *) hb_atomic_ptr_get (&instance); - if (unlikely (!p)) + hb_blob_t *blob_ = (hb_blob_t *) hb_atomic_ptr_get (&blob); + if (unlikely (!blob_)) { - hb_blob_t *blob_ = OT::Sanitizer().sanitize (face->reference_table (T::tableTag)); - p = const_cast(OT::Sanitizer::lock_instance (blob_)); - if (!hb_atomic_ptr_cmpexch (const_cast(&instance), nullptr, p)) + blob_ = OT::Sanitizer().sanitize (face->reference_table (T::tableTag)); + if (!hb_atomic_ptr_cmpexch (&blob, nullptr, blob_)) { hb_blob_destroy (blob_); goto retry; } blob = blob_; } - return p; + return blob_->as (); } inline const T* operator-> (void) const @@ -1225,10 +1287,9 @@ struct hb_lazy_table_loader_t return get(); } + private: hb_face_t *face; mutable hb_blob_t *blob; - private: - mutable T *instance; }; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-cmap-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-cmap-table.hh index 75db76d8ffc..c3aeae21880 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-cmap-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-cmap-table.hh @@ -28,18 +28,19 @@ #define HB_OT_CMAP_TABLE_HH #include "hb-open-type-private.hh" +#include "hb-set-private.hh" #include "hb-subset-plan.hh" -namespace OT { - - /* - * cmap -- Character To Glyph Index Mapping Table + * cmap -- Character to Glyph Index Mapping + * https://docs.microsoft.com/en-us/typography/opentype/spec/cmap */ - #define HB_OT_TAG_cmap HB_TAG('c','m','a','p') +namespace OT { + + struct CmapSubtableFormat0 { inline bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const @@ -59,8 +60,8 @@ struct CmapSubtableFormat0 protected: HBUINT16 format; /* Format number is set to 0. */ - HBUINT16 lengthZ; /* Byte length of this subtable. */ - HBUINT16 languageZ; /* Ignore. */ + HBUINT16 length; /* Byte length of this subtable. */ + HBUINT16 language; /* Ignore. */ HBUINT8 glyphIdArray[256];/* An array that maps character * code to glyph index values. */ public: @@ -69,6 +70,158 @@ struct CmapSubtableFormat0 struct CmapSubtableFormat4 { + struct segment_plan + { + HBUINT16 start_code; + HBUINT16 end_code; + bool use_delta; + }; + + bool serialize (hb_serialize_context_t *c, + const hb_subset_plan_t *plan, + const hb_vector_t &segments) + { + TRACE_SERIALIZE (this); + + if (unlikely (!c->extend_min (*this))) return_trace (false); + + this->format.set (4); + this->length.set (get_sub_table_size (segments)); + + this->segCountX2.set (segments.len * 2); + this->entrySelector.set (MAX (1u, _hb_bit_storage (segments.len)) - 1); + this->searchRange.set (2 * (1u << this->entrySelector)); + this->rangeShift.set (segments.len * 2 > this->searchRange + ? 2 * segments.len - this->searchRange + : 0); + + HBUINT16 *end_count = c->allocate_size (HBUINT16::static_size * segments.len); + c->allocate_size (HBUINT16::static_size); // 2 bytes of padding. + HBUINT16 *start_count = c->allocate_size (HBUINT16::static_size * segments.len); + HBINT16 *id_delta = c->allocate_size (HBUINT16::static_size * segments.len); + HBUINT16 *id_range_offset = c->allocate_size (HBUINT16::static_size * segments.len); + + if (id_range_offset == nullptr) + return_trace (false); + + for (unsigned int i = 0; i < segments.len; i++) + { + end_count[i].set (segments[i].end_code); + start_count[i].set (segments[i].start_code); + if (segments[i].use_delta) + { + hb_codepoint_t cp = segments[i].start_code; + hb_codepoint_t start_gid = 0; + if (unlikely (!plan->new_gid_for_codepoint (cp, &start_gid) && cp != 0xFFFF)) + return_trace (false); + id_delta[i].set (start_gid - segments[i].start_code); + } else { + id_delta[i].set (0); + unsigned int num_codepoints = segments[i].end_code - segments[i].start_code + 1; + HBUINT16 *glyph_id_array = c->allocate_size (HBUINT16::static_size * num_codepoints); + if (glyph_id_array == nullptr) + return_trace (false); + // From the cmap spec: + // + // id_range_offset[i]/2 + // + (cp - segments[i].start_code) + // + (id_range_offset + i) + // = + // glyph_id_array + (cp - segments[i].start_code) + // + // So, solve for id_range_offset[i]: + // + // id_range_offset[i] + // = + // 2 * (glyph_id_array - id_range_offset - i) + id_range_offset[i].set (2 * ( + glyph_id_array - id_range_offset - i)); + for (unsigned int j = 0; j < num_codepoints; j++) + { + hb_codepoint_t cp = segments[i].start_code + j; + hb_codepoint_t new_gid; + if (unlikely (!plan->new_gid_for_codepoint (cp, &new_gid))) + return_trace (false); + glyph_id_array[j].set (new_gid); + } + } + } + + return_trace (true); + } + + static inline size_t get_sub_table_size (const hb_vector_t &segments) + { + size_t segment_size = 0; + for (unsigned int i = 0; i < segments.len; i++) + { + // Parallel array entries + segment_size += + 2 // end count + + 2 // start count + + 2 // delta + + 2; // range offset + + if (!segments[i].use_delta) + // Add bytes for the glyph index array entries for this segment. + segment_size += (segments[i].end_code - segments[i].start_code + 1) * 2; + } + + return min_size + + 2 // Padding + + segment_size; + } + + static inline bool create_sub_table_plan (const hb_subset_plan_t *plan, + hb_vector_t *segments) + { + segment_plan *segment = nullptr; + hb_codepoint_t last_gid = 0; + + hb_codepoint_t cp = HB_SET_VALUE_INVALID; + while (plan->unicodes->next (&cp)) { + hb_codepoint_t new_gid; + if (unlikely (!plan->new_gid_for_codepoint (cp, &new_gid))) + { + DEBUG_MSG(SUBSET, nullptr, "Unable to find new gid for %04x", cp); + return false; + } + + if (cp > 0xFFFF) { + // We are now outside of unicode BMP, stop adding to this cmap. + break; + } + + if (!segment + || cp != segment->end_code + 1u) + { + segment = segments->push (); + segment->start_code.set (cp); + segment->end_code.set (cp); + segment->use_delta = true; + } else { + segment->end_code.set (cp); + if (last_gid + 1u != new_gid) + // gid's are not consecutive in this segment so delta + // cannot be used. + segment->use_delta = false; + } + + last_gid = new_gid; + } + + // There must be a final entry with end_code == 0xFFFF. Check if we need to add one. + if (segment == nullptr || segment->end_code != 0xFFFF) + { + segment = segments->push (); + segment->start_code.set (0xFFFF); + segment->end_code.set (0xFFFF); + segment->use_delta = true; + } + + return true; + } + struct accelerator_t { inline void init (const CmapSubtableFormat4 *subtable) @@ -127,6 +280,17 @@ struct CmapSubtableFormat4 return true; } + static inline void get_all_codepoints_func (const void *obj, hb_set_t *out) + { + const accelerator_t *thiz = (const accelerator_t *) obj; + for (unsigned int i = 0; i < thiz->segCount; i++) + { + if (thiz->startCount[i] != 0xFFFFu + || thiz->endCount[i] != 0xFFFFu) // Skip the last segment (0xFFFF) + hb_set_add_range (out, thiz->startCount[i], thiz->endCount[i]); + } + } + const HBUINT16 *endCount; const HBUINT16 *startCount; const HBUINT16 *idDelta; @@ -164,15 +328,17 @@ struct CmapSubtableFormat4 return_trace (16 + 4 * (unsigned int) segCountX2 <= length); } + + protected: HBUINT16 format; /* Format number is set to 4. */ HBUINT16 length; /* This is the length in bytes of the * subtable. */ - HBUINT16 languageZ; /* Ignore. */ + HBUINT16 language; /* Ignore. */ HBUINT16 segCountX2; /* 2 x segCount. */ - HBUINT16 searchRangeZ; /* 2 * (2**floor(log2(segCount))) */ - HBUINT16 entrySelectorZ; /* log2(searchRange/2) */ - HBUINT16 rangeShiftZ; /* 2 x segCount - searchRange */ + HBUINT16 searchRange; /* 2 * (2**floor(log2(segCount))) */ + HBUINT16 entrySelector; /* log2(searchRange/2) */ + HBUINT16 rangeShift; /* 2 x segCount - searchRange */ HBUINT16 values[VAR]; #if 0 @@ -193,6 +359,8 @@ struct CmapSubtableLongGroup { friend struct CmapSubtableFormat12; friend struct CmapSubtableFormat13; + template + friend struct CmapSubtableLongSegmented; friend struct cmap; int cmp (hb_codepoint_t codepoint) const @@ -238,8 +406,8 @@ struct CmapSubtableTrimmed protected: UINT formatReserved; /* Subtable format and (maybe) padding. */ - UINT lengthZ; /* Byte length of this subtable. */ - UINT languageZ; /* Ignore. */ + UINT length; /* Byte length of this subtable. */ + UINT language; /* Ignore. */ UINT startCharCode; /* First character code covered. */ ArrayOf glyphIdArray; /* Array of glyph index values for character @@ -265,6 +433,15 @@ struct CmapSubtableLongSegmented return true; } + inline void get_all_codepoints (hb_set_t *out) const + { + for (unsigned int i = 0; i < this->groups.len; i++) { + hb_set_add_range (out, + this->groups[i].startCharCode, + this->groups[i].endCharCode); + } + } + inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -272,20 +449,20 @@ struct CmapSubtableLongSegmented } inline bool serialize (hb_serialize_context_t *c, - hb_prealloced_array_t &group_data) + const hb_vector_t &group_data) { TRACE_SERIALIZE (this); if (unlikely (!c->extend_min (*this))) return_trace (false); - Supplier supplier (group_data.array, group_data.len); + Supplier supplier (group_data.arrayZ, group_data.len); if (unlikely (!groups.serialize (c, supplier, group_data.len))) return_trace (false); return true; } protected: HBUINT16 format; /* Subtable format; set to 12. */ - HBUINT16 reservedZ; /* Reserved; set to 0. */ - HBUINT32 lengthZ; /* Byte length of this subtable. */ - HBUINT32 languageZ; /* Ignore. */ + HBUINT16 reserved; /* Reserved; set to 0. */ + HBUINT32 length; /* Byte length of this subtable. */ + HBUINT32 language; /* Ignore. */ SortedArrayOf groups; /* Groupings. */ public: @@ -297,6 +474,69 @@ struct CmapSubtableFormat12 : CmapSubtableLongSegmented static inline hb_codepoint_t group_get_glyph (const CmapSubtableLongGroup &group, hb_codepoint_t u) { return group.glyphID + (u - group.startCharCode); } + + + bool serialize (hb_serialize_context_t *c, + const hb_vector_t &groups) + { + if (unlikely (!c->extend_min (*this))) return false; + + this->format.set (12); + this->reserved.set (0); + this->length.set (get_sub_table_size (groups)); + + return CmapSubtableLongSegmented::serialize (c, groups); + } + + static inline size_t get_sub_table_size (const hb_vector_t &groups) + { + return 16 + 12 * groups.len; + } + + static inline bool create_sub_table_plan (const hb_subset_plan_t *plan, + hb_vector_t *groups) + { + CmapSubtableLongGroup *group = nullptr; + + hb_codepoint_t cp = HB_SET_VALUE_INVALID; + while (plan->unicodes->next (&cp)) { + hb_codepoint_t new_gid; + if (unlikely (!plan->new_gid_for_codepoint (cp, &new_gid))) + { + DEBUG_MSG(SUBSET, nullptr, "Unable to find new gid for %04x", cp); + return false; + } + + if (!group || !_is_gid_consecutive (group, cp, new_gid)) + { + group = groups->push (); + group->startCharCode.set (cp); + group->endCharCode.set (cp); + group->glyphID.set (new_gid); + } else + { + group->endCharCode.set (cp); + } + } + + DEBUG_MSG(SUBSET, nullptr, "cmap"); + for (unsigned int i = 0; i < groups->len; i++) { + CmapSubtableLongGroup& group = (*groups)[i]; + DEBUG_MSG(SUBSET, nullptr, " %d: U+%04X-U+%04X, gid %d-%d", i, (uint32_t) group.startCharCode, (uint32_t) group.endCharCode, (uint32_t) group.glyphID, (uint32_t) group.glyphID + ((uint32_t) group.endCharCode - (uint32_t) group.startCharCode)); + } + + return true; + } + + private: + static inline bool _is_gid_consecutive (CmapSubtableLongGroup *group, + hb_codepoint_t cp, + hb_codepoint_t new_gid) + { + return (cp - 1 == group->endCharCode) && + new_gid == group->glyphID + (cp - group->startCharCode); + } + }; struct CmapSubtableFormat13 : CmapSubtableLongSegmented @@ -328,7 +568,7 @@ struct UnicodeValueRange return_trace (c->check_struct (this)); } - UINT24 startUnicodeValue; /* First value in this range. */ + HBUINT24 startUnicodeValue; /* First value in this range. */ HBUINT8 additionalCount; /* Number of additional values in this * range. */ public: @@ -350,7 +590,7 @@ struct UVSMapping return_trace (c->check_struct (this)); } - UINT24 unicodeValue; /* Base Unicode value of the UVS */ + HBUINT24 unicodeValue; /* Base Unicode value of the UVS */ GlyphID glyphID; /* Glyph ID of the UVS */ public: DEFINE_SIZE_STATIC (5); @@ -392,7 +632,7 @@ struct VariationSelectorRecord nonDefaultUVS.sanitize (c, base)); } - UINT24 varSelector; /* Variation selector. */ + HBUINT24 varSelector; /* Variation selector. */ LOffsetTo defaultUVS; /* Offset to Default UVS Table. May be 0. */ LOffsetTo @@ -419,7 +659,7 @@ struct CmapSubtableFormat14 protected: HBUINT16 format; /* Format number is set to 14. */ - HBUINT32 lengthZ; /* Byte length of this subtable. */ + HBUINT32 length; /* Byte length of this subtable. */ SortedArrayOf record; /* Variation selector records; sorted * in increasing order of `varSelector'. */ @@ -509,6 +749,33 @@ struct cmap { static const hb_tag_t tableTag = HB_OT_TAG_cmap; + struct subset_plan { + subset_plan(void) + { + format4_segments.init(); + format12_groups.init(); + } + + ~subset_plan(void) + { + format4_segments.fini(); + format12_groups.fini(); + } + + inline size_t final_size() const + { + return 4 // header + + 8 * 3 // 3 EncodingRecord + + CmapSubtableFormat4::get_sub_table_size (this->format4_segments) + + CmapSubtableFormat12::get_sub_table_size (this->format12_groups); + } + + // Format 4 + hb_vector_t format4_segments; + // Format 12 + hb_vector_t format12_groups; + }; + inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -517,41 +784,17 @@ struct cmap encodingRecord.sanitize (c, this)); } - inline bool populate_groups (hb_subset_plan_t *plan, - hb_prealloced_array_t *groups) const + inline bool _create_plan (const hb_subset_plan_t *plan, + subset_plan *cmap_plan) const { - CmapSubtableLongGroup *group = nullptr; - for (unsigned int i = 0; i < plan->codepoints.len; i++) { + if (unlikely( !CmapSubtableFormat4::create_sub_table_plan (plan, &cmap_plan->format4_segments))) + return false; - hb_codepoint_t cp = plan->codepoints[i]; - if (!group || cp - 1 != group->endCharCode) - { - group = groups->push (); - group->startCharCode.set (cp); - group->endCharCode.set (cp); - hb_codepoint_t new_gid; - if (unlikely (!hb_subset_plan_new_gid_for_codepoint (plan, cp, &new_gid))) - { - DEBUG_MSG(SUBSET, nullptr, "Unable to find new gid for %04x", cp); - return false; - } - group->glyphID.set (new_gid); - } else - { - group->endCharCode.set (cp); - } - } - - DEBUG_MSG(SUBSET, nullptr, "cmap"); - for (unsigned int i = 0; i < groups->len; i++) { - CmapSubtableLongGroup& group = (*groups)[i]; - DEBUG_MSG(SUBSET, nullptr, " %d: U+%04X-U+%04X, gid %d-%d", i, (uint32_t) group.startCharCode, (uint32_t) group.endCharCode, (uint32_t) group.glyphID, (uint32_t) group.glyphID + ((uint32_t) group.endCharCode - (uint32_t) group.startCharCode)); - } - - return true; + return CmapSubtableFormat12::create_sub_table_plan (plan, &cmap_plan->format12_groups); } - inline bool _subset (hb_prealloced_array_t &groups, + inline bool _subset (const hb_subset_plan_t *plan, + const subset_plan &cmap_subset_plan, size_t dest_sz, void *dest) const { @@ -565,25 +808,46 @@ struct cmap cmap->version.set (0); - if (unlikely (!cmap->encodingRecord.serialize (&c, /* numTables */ 1))) return false; + if (unlikely (!cmap->encodingRecord.serialize (&c, /* numTables */ 3))) + return false; - EncodingRecord &rec = cmap->encodingRecord[0]; - rec.platformID.set (3); // Windows - rec.encodingID.set (10); // Unicode UCS-4 + // TODO(grieger): Convert the below to a for loop - /* capture offset to subtable */ - CmapSubtable &subtable = rec.subtable.serialize (&c, cmap); + // Format 4, Plat 0 Encoding Record + EncodingRecord &format4_plat0_rec = cmap->encodingRecord[0]; + format4_plat0_rec.platformID.set (0); // Unicode + format4_plat0_rec.encodingID.set (3); - subtable.u.format.set (12); + // Format 4, Plat 3 Encoding Record + EncodingRecord &format4_plat3_rec = cmap->encodingRecord[1]; + format4_plat3_rec.platformID.set (3); // Windows + format4_plat3_rec.encodingID.set (1); // Unicode BMP - CmapSubtableFormat12 &format12 = subtable.u.format12; - if (unlikely (!c.extend_min (format12))) return false; + // Format 12 Encoding Record + EncodingRecord &format12_rec = cmap->encodingRecord[2]; + format12_rec.platformID.set (3); // Windows + format12_rec.encodingID.set (10); // Unicode UCS-4 - format12.format.set (12); - format12.reservedZ.set (0); - format12.lengthZ.set (16 + 12 * groups.len); + // Write out format 4 sub table + { + CmapSubtable &subtable = format4_plat0_rec.subtable.serialize (&c, cmap); + format4_plat3_rec.subtable.set (format4_plat0_rec.subtable); + subtable.u.format.set (4); - if (unlikely (!format12.serialize (&c, groups))) return false; + CmapSubtableFormat4 &format4 = subtable.u.format4; + if (unlikely (!format4.serialize (&c, plan, cmap_subset_plan.format4_segments))) + return false; + } + + // Write out format 12 sub table. + { + CmapSubtable &subtable = format12_rec.subtable.serialize (&c, cmap); + subtable.u.format.set (12); + + CmapSubtableFormat12 &format12 = subtable.u.format12; + if (unlikely (!format12.serialize (&c, cmap_subset_plan.format12_groups))) + return false; + } c.end_serialize (); @@ -592,24 +856,25 @@ struct cmap inline bool subset (hb_subset_plan_t *plan) const { - hb_auto_array_t groups; + subset_plan cmap_subset_plan; - if (unlikely (!populate_groups (plan, &groups))) return false; + if (unlikely (!_create_plan (plan, &cmap_subset_plan))) + { + DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cmap subsetting plan."); + return false; + } // We now know how big our blob needs to be - // TODO use APIs from the structs to get size? - size_t dest_sz = 4 // header - + 8 // 1 EncodingRecord - + 16 // Format 12 header - + 12 * groups.len; // SequentialMapGroup records + size_t dest_sz = cmap_subset_plan.final_size(); void *dest = malloc (dest_sz); if (unlikely (!dest)) { DEBUG_MSG(SUBSET, nullptr, "Unable to alloc %lu for cmap subset output", (unsigned long) dest_sz); return false; } - if (unlikely (!_subset (groups, dest_sz, dest))) + if (unlikely (!_subset (plan, cmap_subset_plan, dest_sz, dest))) { + DEBUG_MSG(SUBSET, nullptr, "Failed to perform subsetting of cmap."); free (dest); return false; } @@ -620,7 +885,7 @@ struct cmap HB_MEMORY_MODE_READONLY, dest, free); - bool result = hb_subset_plan_add_table (plan, HB_OT_TAG_cmap, cmap_prime); + bool result = plan->add_table (HB_OT_TAG_cmap, cmap_prime); hb_blob_destroy (cmap_prime); return result; } @@ -630,7 +895,7 @@ struct cmap inline void init (hb_face_t *face) { this->blob = OT::Sanitizer().sanitize (face->reference_table (HB_OT_TAG_cmap)); - const OT::cmap *cmap = OT::Sanitizer::lock_instance (this->blob); + const OT::cmap *cmap = this->blob->as (); const OT::CmapSubtable *subtable = nullptr; const OT::CmapSubtableFormat14 *subtable_uvs = nullptr; @@ -651,7 +916,7 @@ struct cmap if (subtable) symbol = true; } /* Meh. */ - if (!subtable) subtable = &OT::Null(OT::CmapSubtable); + if (!subtable) subtable = &Null(OT::CmapSubtable); /* UVS subtable. */ if (!subtable_uvs) @@ -661,26 +926,36 @@ struct cmap subtable_uvs = &st->u.format14; } /* Meh. */ - if (!subtable_uvs) subtable_uvs = &OT::Null(OT::CmapSubtableFormat14); + if (!subtable_uvs) subtable_uvs = &Null(OT::CmapSubtableFormat14); this->uvs_table = subtable_uvs; this->get_glyph_data = subtable; if (unlikely (symbol)) + { this->get_glyph_func = get_glyph_from_symbol; - else + this->get_all_codepoints_func = null_get_all_codepoints_func; + } else { switch (subtable->u.format) { /* Accelerate format 4 and format 12. */ - default: this->get_glyph_func = get_glyph_from; break; - case 12: this->get_glyph_func = get_glyph_from; break; + default: + this->get_glyph_func = get_glyph_from; + this->get_all_codepoints_func = null_get_all_codepoints_func; + break; + case 12: + this->get_glyph_func = get_glyph_from; + this->get_all_codepoints_func = get_all_codepoints_from; + break; case 4: { this->format4_accel.init (&subtable->u.format4); this->get_glyph_data = &this->format4_accel; this->get_glyph_func = this->format4_accel.get_glyph_func; + this->get_all_codepoints_func = this->format4_accel.get_all_codepoints_func; } break; } + } } inline void fini (void) @@ -710,10 +985,22 @@ struct cmap return get_nominal_glyph (unicode, glyph); } + inline void get_all_codepoints (hb_set_t *out) const + { + this->get_all_codepoints_func (get_glyph_data, out); + } + protected: typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj, hb_codepoint_t codepoint, hb_codepoint_t *glyph); + typedef void (*hb_cmap_get_all_codepoints_func_t) (const void *obj, + hb_set_t *out); + + static inline void null_get_all_codepoints_func (const void *obj, hb_set_t *out) + { + // NOOP + } template static inline bool get_glyph_from (const void *obj, @@ -724,6 +1011,14 @@ struct cmap return typed_obj->get_glyph (codepoint, glyph); } + template + static inline void get_all_codepoints_from (const void *obj, + hb_set_t *out) + { + const Type *typed_obj = (const Type *) obj; + typed_obj->get_all_codepoints (out); + } + template static inline bool get_glyph_from_symbol (const void *obj, hb_codepoint_t codepoint, @@ -738,7 +1033,7 @@ struct cmap /* For symbol-encoded OpenType fonts, we duplicate the * U+F000..F0FF range at U+0000..U+00FF. That's what * Windows seems to do, and that's hinted about at: - * http://www.microsoft.com/typography/otspec/recom.htm + * https://docs.microsoft.com/en-us/typography/opentype/spec/recom * under "Non-Standard (Symbol) Fonts". */ return typed_obj->get_glyph (0xF000u + codepoint, glyph); } @@ -749,6 +1044,8 @@ struct cmap private: hb_cmap_get_glyph_func_t get_glyph_func; const void *get_glyph_data; + hb_cmap_get_all_codepoints_func_t get_all_codepoints_func; + OT::CmapSubtableFormat4::accelerator_t format4_accel; const OT::CmapSubtableFormat14 *uvs_table; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-cbdt-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-cbdt-table.hh index 39826692bbd..c4bfa3ddf96 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-cbdt-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-cbdt-table.hh @@ -29,6 +29,18 @@ #include "hb-open-type-private.hh" +/* + * CBLC -- Color Bitmap Location + * https://docs.microsoft.com/en-us/typography/opentype/spec/cblc + * https://docs.microsoft.com/en-us/typography/opentype/spec/eblc + * CBDT -- Color Bitmap Data + * https://docs.microsoft.com/en-us/typography/opentype/spec/cbdt + * https://docs.microsoft.com/en-us/typography/opentype/spec/ebdt + */ +#define HB_OT_TAG_CBLC HB_TAG('C','B','L','C') +#define HB_OT_TAG_CBDT HB_TAG('C','B','D','T') + + namespace OT { struct SmallGlyphMetrics @@ -47,21 +59,21 @@ struct SmallGlyphMetrics extents->height = -height; } - HBUINT8 height; - HBUINT8 width; - HBINT8 bearingX; - HBINT8 bearingY; - HBUINT8 advance; - + HBUINT8 height; + HBUINT8 width; + HBINT8 bearingX; + HBINT8 bearingY; + HBUINT8 advance; + public: DEFINE_SIZE_STATIC(5); }; struct BigGlyphMetrics : SmallGlyphMetrics { - HBINT8 vertBearingX; - HBINT8 vertBearingY; - HBUINT8 vertAdvance; - + HBINT8 vertBearingX; + HBINT8 vertBearingY; + HBUINT8 vertAdvance; + public: DEFINE_SIZE_STATIC(8); }; @@ -73,19 +85,19 @@ struct SBitLineMetrics return_trace (c->check_struct (this)); } - HBINT8 ascender; - HBINT8 decender; - HBUINT8 widthMax; - HBINT8 caretSlopeNumerator; - HBINT8 caretSlopeDenominator; - HBINT8 caretOffset; - HBINT8 minOriginSB; - HBINT8 minAdvanceSB; - HBINT8 maxBeforeBL; - HBINT8 minAfterBL; - HBINT8 padding1; - HBINT8 padding2; - + HBINT8 ascender; + HBINT8 decender; + HBUINT8 widthMax; + HBINT8 caretSlopeNumerator; + HBINT8 caretSlopeDenominator; + HBINT8 caretOffset; + HBINT8 minOriginSB; + HBINT8 minAdvanceSB; + HBINT8 maxBeforeBL; + HBINT8 minAfterBL; + HBINT8 padding1; + HBINT8 padding2; + public: DEFINE_SIZE_STATIC(12); }; @@ -102,10 +114,10 @@ struct IndexSubtableHeader return_trace (c->check_struct (this)); } - HBUINT16 indexFormat; - HBUINT16 imageFormat; - HBUINT32 imageDataOffset; - + HBUINT16 indexFormat; + HBUINT16 imageFormat; + HBUINT32 imageDataOffset; + public: DEFINE_SIZE_STATIC(8); }; @@ -131,9 +143,9 @@ struct IndexSubtableFormat1Or3 return true; } - IndexSubtableHeader header; - Offset offsetArrayZ[VAR]; - + IndexSubtableHeader header; + Offset offsetArrayZ[VAR]; + public: DEFINE_SIZE_ARRAY(8, offsetArrayZ); }; @@ -214,15 +226,17 @@ struct IndexSubtableRecord offset, length, format); } - HBUINT16 firstGlyphIndex; - HBUINT16 lastGlyphIndex; - LOffsetTo offsetToSubtable; - + GlyphID firstGlyphIndex; + GlyphID lastGlyphIndex; + LOffsetTo offsetToSubtable; + public: DEFINE_SIZE_STATIC(8); }; struct IndexSubtableArray { + friend struct CBDT; + inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const { TRACE_SANITIZE (this); @@ -249,8 +263,7 @@ struct IndexSubtableArray } protected: - IndexSubtableRecord indexSubtablesZ[VAR]; - + IndexSubtableRecord indexSubtablesZ[VAR]; public: DEFINE_SIZE_ARRAY(0, indexSubtablesZ); }; @@ -258,6 +271,7 @@ struct IndexSubtableArray struct BitmapSizeTable { friend struct CBLC; + friend struct CBDT; inline bool sanitize (hb_sanitize_context_t *c, const void *base) const { @@ -275,19 +289,19 @@ struct BitmapSizeTable } protected: - LOffsetTo indexSubtableArrayOffset; - HBUINT32 indexTablesSize; - HBUINT32 numberOfIndexSubtables; - HBUINT32 colorRef; - SBitLineMetrics horizontal; - SBitLineMetrics vertical; - HBUINT16 startGlyphIndex; - HBUINT16 endGlyphIndex; - HBUINT8 ppemX; - HBUINT8 ppemY; - HBUINT8 bitDepth; - HBINT8 flags; - + LOffsetTo + indexSubtableArrayOffset; + HBUINT32 indexTablesSize; + HBUINT32 numberOfIndexSubtables; + HBUINT32 colorRef; + SBitLineMetrics horizontal; + SBitLineMetrics vertical; + GlyphID startGlyphIndex; + GlyphID endGlyphIndex; + HBUINT8 ppemX; + HBUINT8 ppemY; + HBUINT8 bitDepth; + HBINT8 flags; public: DEFINE_SIZE_STATIC(48); }; @@ -299,19 +313,26 @@ struct BitmapSizeTable struct GlyphBitmapDataFormat17 { - SmallGlyphMetrics glyphMetrics; - HBUINT32 dataLen; - HBUINT8 dataZ[VAR]; - - DEFINE_SIZE_ARRAY(9, dataZ); + SmallGlyphMetrics glyphMetrics; + LArrayOf data; + public: + DEFINE_SIZE_ARRAY(9, data); }; +struct GlyphBitmapDataFormat18 +{ + BigGlyphMetrics glyphMetrics; + LArrayOf data; + public: + DEFINE_SIZE_ARRAY(12, data); +}; -/* - * CBLC -- Color Bitmap Location Table - */ - -#define HB_OT_TAG_CBLC HB_TAG('C','B','L','C') +struct GlyphBitmapDataFormat19 +{ + LArrayOf data; + public: + DEFINE_SIZE_ARRAY(4, data); +}; struct CBLC { @@ -336,8 +357,8 @@ struct CBLC unsigned int count = sizeTables.len; for (uint32_t i = 0; i < count; ++i) { - unsigned int startGlyphIndex = sizeTables.array[i].startGlyphIndex; - unsigned int endGlyphIndex = sizeTables.array[i].endGlyphIndex; + unsigned int startGlyphIndex = sizeTables.arrayZ[i].startGlyphIndex; + unsigned int endGlyphIndex = sizeTables.arrayZ[i].endGlyphIndex; if (startGlyphIndex <= glyph && glyph <= endGlyphIndex) { *x_ppem = sizeTables[i].ppemX; @@ -352,16 +373,10 @@ struct CBLC protected: FixedVersion<> version; LArrayOf sizeTables; - public: DEFINE_SIZE_ARRAY(8, sizeTables); }; -/* - * CBDT -- Color Bitmap Data Table - */ -#define HB_OT_TAG_CBDT HB_TAG('C','B','D','T') - struct CBDT { static const hb_tag_t tableTag = HB_OT_TAG_CBDT; @@ -388,8 +403,8 @@ struct CBDT cbdt = nullptr; return; /* Not a bitmap font. */ } - cblc = Sanitizer::lock_instance (cblc_blob); - cbdt = Sanitizer::lock_instance (cbdt_blob); + cblc = cblc_blob->as (); + cbdt = cbdt_blob->as (); } @@ -447,6 +462,59 @@ struct CBDT return true; } + inline void dump (void (*callback) (const uint8_t* data, unsigned int length, + unsigned int group, unsigned int gid)) const + { + if (!cblc) + return; // Not a color bitmap font. + + for (unsigned int i = 0; i < cblc->sizeTables.len; ++i) + { + const BitmapSizeTable &sizeTable = cblc->sizeTables[i]; + const IndexSubtableArray &subtable_array = cblc+sizeTable.indexSubtableArrayOffset; + for (unsigned int j = 0; j < sizeTable.numberOfIndexSubtables; ++j) + { + const IndexSubtableRecord &subtable_record = subtable_array.indexSubtablesZ[j]; + for (unsigned int gid = subtable_record.firstGlyphIndex; + gid <= subtable_record.lastGlyphIndex; ++gid) + { + unsigned int image_offset = 0, image_length = 0, image_format = 0; + + if (!subtable_record.get_image_data (gid, + &image_offset, &image_length, &image_format)) + continue; + + switch (image_format) + { + case 17: { + const GlyphBitmapDataFormat17& glyphFormat17 = + StructAtOffset (this->cbdt, image_offset); + callback ((const uint8_t *) &glyphFormat17.data.arrayZ, + glyphFormat17.data.len, i, gid); + } + break; + case 18: { + const GlyphBitmapDataFormat18& glyphFormat18 = + StructAtOffset (this->cbdt, image_offset); + callback ((const uint8_t *) &glyphFormat18.data.arrayZ, + glyphFormat18.data.len, i, gid); + } + break; + case 19: { + const GlyphBitmapDataFormat19& glyphFormat19 = + StructAtOffset (this->cbdt, image_offset); + callback ((const uint8_t *) &glyphFormat19.data.arrayZ, + glyphFormat19.data.len, i, gid); + } + break; + default: + continue; + } + } + } + } + } + private: hb_blob_t *cblc_blob; hb_blob_t *cbdt_blob; @@ -459,9 +527,8 @@ struct CBDT protected: - FixedVersion<>version; - HBUINT8 dataZ[VAR]; - + FixedVersion<> version; + HBUINT8 dataZ[VAR]; public: DEFINE_SIZE_ARRAY(4, dataZ); }; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-colr-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-colr-table.hh index d924fd0e37f..9b1bc3f2f36 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-colr-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-colr-table.hh @@ -28,12 +28,12 @@ #include "hb-open-type-private.hh" /* - * Color Palette - * http://www.microsoft.com/typography/otspec/colr.htm + * COLR -- Color + * https://docs.microsoft.com/en-us/typography/opentype/spec/colr */ - #define HB_OT_TAG_COLR HB_TAG('C','O','L','R') + namespace OT { @@ -48,8 +48,8 @@ struct LayerRecord } protected: - GlyphID gID; /* Glyph ID of layer glyph */ - HBUINT16 paletteIndex; /* Index value to use with a selected color palette */ + GlyphID glyphid; /* Glyph ID of layer glyph */ + HBUINT16 colorIdx; /* Index value to use with a selected color palette */ public: DEFINE_SIZE_STATIC (4); }; @@ -61,17 +61,28 @@ struct BaseGlyphRecord inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); - return_trace (c->check_struct (this)); + return_trace (likely (c->check_struct (this))); + } + + inline int cmp (hb_codepoint_t g) const { + return g < glyphid ? -1 : g > glyphid ? 1 : 0; } protected: - GlyphID gID; /* Glyph ID of reference glyph */ - HBUINT16 firstLayerIndex; /* Index to the layer record */ - HBUINT16 numLayers; /* Number of color layers associated with this glyph */ + GlyphID glyphid; /* Glyph ID of reference glyph */ + HBUINT16 firstLayerIdx; /* Index to the layer record */ + HBUINT16 numLayers; /* Number of color layers associated with this glyph */ public: DEFINE_SIZE_STATIC (6); }; +static int compare_bgr (const void *pa, const void *pb) +{ + const hb_codepoint_t *a = (const hb_codepoint_t *) pa; + const BaseGlyphRecord *b = (const BaseGlyphRecord *) pb; + return b->cmp (*a); +} + struct COLR { static const hb_tag_t tableTag = HB_OT_TAG_COLR; @@ -79,59 +90,50 @@ struct COLR inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); - if (!(c->check_struct (this) && - c->check_array ((const void*) &layerRecordsOffsetZ, sizeof (LayerRecord), numLayerRecords) && - c->check_array ((const void*) &baseGlyphRecordsZ, sizeof (BaseGlyphRecord), numBaseGlyphRecords))) - return_trace (false); - - const BaseGlyphRecord* base_glyph_records = &baseGlyphRecordsZ (this); - for (unsigned int i = 0; i < numBaseGlyphRecords; ++i) - if (base_glyph_records[i].firstLayerIndex + - base_glyph_records[i].numLayers > numLayerRecords) - return_trace (false); - - return_trace (true); + return_trace (likely (c->check_struct (this) && + (this+baseGlyphsZ).sanitize (c, numBaseGlyphs) && + (this+layersZ).sanitize (c, numLayers))); } - inline bool get_base_glyph_record ( - hb_codepoint_t glyph_id, unsigned int &first_layer, unsigned int &num_layers) const + inline bool get_base_glyph_record (hb_codepoint_t glyph_id, + unsigned int *first_layer /* OUT */, + unsigned int *num_layers /* OUT */) const { - const BaseGlyphRecord* base_glyph_records = &baseGlyphRecordsZ (this); - unsigned int min = 0, max = numBaseGlyphRecords - 1; - while (min <= max) + const BaseGlyphRecord* record; + record = (BaseGlyphRecord *) bsearch (&glyph_id, &(this+baseGlyphsZ), numBaseGlyphs, + sizeof (BaseGlyphRecord), compare_bgr); + if (unlikely (!record)) + return false; + + *first_layer = record->firstLayerIdx; + *num_layers = record->numLayers; + return true; + } + + inline bool get_layer_record (unsigned int record, + hb_codepoint_t *glyph_id /* OUT */, + unsigned int *palette_index /* OUT */) const + { + if (unlikely (record >= numLayers)) { - unsigned int mid = (min + max) / 2; - hb_codepoint_t gID = base_glyph_records[mid].gID; - if (gID > glyph_id) - max = mid - 1; - else if (gID < glyph_id) - min = mid + 1; - else - { - first_layer = base_glyph_records[mid].firstLayerIndex; - num_layers = base_glyph_records[mid].numLayers; - return true; - } + *glyph_id = 0; + *palette_index = 0xFFFF; + return false; } - return false; - } - - inline void get_layer_record (int layer, - hb_codepoint_t &glyph_id, unsigned int &palette_index) const - { - const LayerRecord* records = &layerRecordsOffsetZ (this); - glyph_id = records[layer].gID; - palette_index = records[layer].paletteIndex; + const LayerRecord &layer = (this+layersZ)[record]; + *glyph_id = layer.glyphid; + *palette_index = layer.colorIdx; + return true; } protected: - HBUINT16 version; /* Table version number */ - HBUINT16 numBaseGlyphRecords; /* Number of Base Glyph Records */ - LOffsetTo - baseGlyphRecordsZ; /* Offset to Base Glyph records. */ - LOffsetTo - layerRecordsOffsetZ; /* Offset to Layer Records */ - HBUINT16 numLayerRecords; /* Number of Layer Records */ + HBUINT16 version; /* Table version number */ + HBUINT16 numBaseGlyphs; /* Number of Base Glyph Records */ + LOffsetTo > + baseGlyphsZ; /* Offset to Base Glyph records. */ + LOffsetTo > + layersZ; /* Offset to Layer Records */ + HBUINT16 numLayers; /* Number of Layer Records */ public: DEFINE_SIZE_STATIC (14); }; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-cpal-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-cpal-table.hh index 46b956435af..ac7a2c65466 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-cpal-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-cpal-table.hh @@ -79,12 +79,12 @@ typedef enum { /*< flags >*/ /* - * Color Palette - * http://www.microsoft.com/typography/otspec/cpal.htm + * CPAL -- Color Palette + * https://docs.microsoft.com/en-us/typography/opentype/spec/cpal */ - #define HB_OT_TAG_CPAL HB_TAG('C','P','A','L') + namespace OT { @@ -92,35 +92,44 @@ struct CPALV1Tail { friend struct CPAL; - inline bool sanitize (hb_sanitize_context_t *c, unsigned int palettes) const + inline bool + sanitize (hb_sanitize_context_t *c, const void *base, unsigned int palettes) const { TRACE_SANITIZE (this); - return_trace ( - c->check_struct (this) && - c->check_array ((const void*) &paletteFlags, sizeof (HBUINT32), palettes) && - c->check_array ((const void*) &paletteLabel, sizeof (HBUINT16), palettes) && - c->check_array ((const void*) &paletteEntryLabel, sizeof (HBUINT16), palettes)); + return_trace (c->check_struct (this) && + (base+paletteFlagsZ).sanitize (c, palettes) && + (base+paletteLabelZ).sanitize (c, palettes) && + (base+paletteEntryLabelZ).sanitize (c, palettes)); } private: inline hb_ot_color_palette_flags_t get_palette_flags (const void *base, unsigned int palette) const { - const HBUINT32* flags = &paletteFlags (base); - return (hb_ot_color_palette_flags_t) (uint32_t) flags[palette]; + // range checked at the CPAL caller + return (hb_ot_color_palette_flags_t) (uint32_t) (base+paletteFlagsZ)[palette]; } inline unsigned int get_palette_name_id (const void *base, unsigned int palette) const { - const HBUINT16* name_ids = &paletteLabel (base); - return name_ids[palette]; + // range checked at the CPAL caller + return (base+paletteLabelZ)[palette]; } protected: - LOffsetTo paletteFlags; - LOffsetTo paletteLabel; - LOffsetTo paletteEntryLabel; + LOffsetTo > + paletteFlagsZ; /* Offset from the beginning of CPAL table to + * the Palette Type Array. Set to 0 if no array + * is provided. */ + LOffsetTo > + paletteLabelZ; /* Offset from the beginning of CPAL table to + * the Palette Labels Array. Set to 0 if no + * array is provided. */ + LOffsetTo > + paletteEntryLabelZ; /* Offset from the beginning of CPAL table to + * the Palette Entry Label Array. Set to 0 + * if no array is provided. */ public: DEFINE_SIZE_STATIC (12); }; @@ -134,13 +143,14 @@ struct CPAL inline bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); - if (!(c->check_struct (this) && // This checks colorRecordIndicesX sanity also, see #get_size - c->check_array ((const void*) &colorRecordsZ, sizeof (BGRAColor), numColorRecords))) + if (unlikely (!(c->check_struct (this) && // it checks colorRecordIndices also + // see #get_size + (this+colorRecordsZ).sanitize (c, numColorRecords)))) return_trace (false); // Check for indices sanity so no need for doing it runtime for (unsigned int i = 0; i < numPalettes; ++i) - if (colorRecordIndicesX[i] + numPaletteEntries > numColorRecords) + if (unlikely (colorRecordIndicesZ[i] + numPaletteEntries > numColorRecords)) return_trace (false); // If version is zero, we are done here; otherwise we need to check tail also @@ -148,7 +158,7 @@ struct CPAL return_trace (true); const CPALV1Tail &v1 = StructAfter (*this); - return_trace (v1.sanitize (c, numPalettes)); + return_trace (likely (v1.sanitize (c, this, numPalettes))); } inline unsigned int get_size (void) const @@ -158,7 +168,7 @@ struct CPAL inline hb_ot_color_palette_flags_t get_palette_flags (unsigned int palette) const { - if (version == 0 || palette >= numPalettes) + if (unlikely (version == 0 || palette >= numPalettes)) return HB_OT_COLOR_PALETTE_FLAG_DEFAULT; const CPALV1Tail& cpal1 = StructAfter (*this); @@ -167,7 +177,7 @@ struct CPAL inline unsigned int get_palette_name_id (unsigned int palette) const { - if (version == 0 || palette >= numPalettes) + if (unlikely (version == 0 || palette >= numPalettes)) return 0xFFFF; const CPALV1Tail& cpal1 = StructAfter (*this); @@ -179,27 +189,33 @@ struct CPAL return numPalettes; } - inline hb_ot_color_t get_color_record_argb (unsigned int color_index, unsigned int palette) const + inline hb_ot_color_t + get_color_record_argb (unsigned int color_index, unsigned int palette) const { - if (color_index >= numPaletteEntries || palette >= numPalettes) + if (unlikely (color_index >= numPaletteEntries || palette >= numPalettes)) return 0; - const BGRAColor* records = &colorRecordsZ(this); // No need for more range check as it is already done on #sanitize - return records[colorRecordIndicesX[palette] + color_index]; + const UnsizedArrayOf& color_records = this+colorRecordsZ; + return color_records[colorRecordIndicesZ[palette] + color_index]; } protected: - HBUINT16 version; + HBUINT16 version; /* Table version number */ /* Version 0 */ - HBUINT16 numPaletteEntries; - HBUINT16 numPalettes; - HBUINT16 numColorRecords; - LOffsetTo colorRecordsZ; - HBUINT16 colorRecordIndicesX[VAR]; // VAR=numPalettes -/*CPALV1Tail v1[VAR];*/ + HBUINT16 numPaletteEntries; /* Number of palette entries in each palette. */ + HBUINT16 numPalettes; /* Number of palettes in the table. */ + HBUINT16 numColorRecords; /* Total number of color records, combined for + * all palettes. */ + LOffsetTo > + colorRecordsZ; /* Offset from the beginning of CPAL table to + * the first ColorRecord. */ + UnsizedArrayOf + colorRecordIndicesZ; /* Index of each palette’s first color record in + * the combined color record array. */ +/*CPALV1Tail v1;*/ public: - DEFINE_SIZE_ARRAY (12, colorRecordIndicesX); + DEFINE_SIZE_ARRAY (12, colorRecordIndicesZ); }; } /* namespace OT */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-sbix-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-sbix-table.hh new file mode 100644 index 00000000000..ed25b5e93b3 --- /dev/null +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-sbix-table.hh @@ -0,0 +1,153 @@ +/* + * Copyright © 2018 Ebrahim Byagowi + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_OT_COLOR_SBIX_TABLE_HH +#define HB_OT_COLOR_SBIX_TABLE_HH + +#include "hb-open-type-private.hh" + +/* + * sbix -- Standard Bitmap Graphics + * https://docs.microsoft.com/en-us/typography/opentype/spec/sbix + * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6sbix.html + */ +#define HB_OT_TAG_sbix HB_TAG('s','b','i','x') + + +namespace OT { + + +struct SBIXGlyph +{ + HBINT16 xOffset; /* The horizontal (x-axis) offset from the left + * edge of the graphic to the glyph’s origin. + * That is, the x-coordinate of the point on the + * baseline at the left edge of the glyph. */ + HBINT16 yOffset; /* The vertical (y-axis) offset from the bottom + * edge of the graphic to the glyph’s origin. + * That is, the y-coordinate of the point on the + * baseline at the left edge of the glyph. */ + Tag graphicType; /* Indicates the format of the embedded graphic + * data: one of 'jpg ', 'png ' or 'tiff', or the + * special format 'dupe'. */ + UnsizedArrayOf + data; /* The actual embedded graphic data. The total + * length is inferred from sequential entries in + * the glyphDataOffsets array and the fixed size + * (8 bytes) of the preceding fields. */ + public: + DEFINE_SIZE_ARRAY (8, data); +}; + +struct SBIXStrike +{ + friend struct sbix; + + inline bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (c->check_struct (this) && + imageOffsetsZ.sanitize_shallow (c, c->num_glyphs + 1)); + } + + protected: + HBUINT16 ppem; /* The PPEM size for which this strike was designed. */ + HBUINT16 resolution; /* The device pixel density (in PPI) for which this + * strike was designed. (E.g., 96 PPI, 192 PPI.) */ + UnsizedArrayOf > + imageOffsetsZ; /* Offset from the beginning of the strike data header + * to bitmap data for an individual glyph ID. */ + public: + DEFINE_SIZE_STATIC (8); +}; + +struct sbix +{ + static const hb_tag_t tableTag = HB_OT_TAG_sbix; + + inline bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (likely (c->check_struct (this) && strikes.sanitize (c, this))); + } + + struct accelerator_t + { + inline void init (hb_face_t *face) + { + num_glyphs = hb_face_get_glyph_count (face); + + OT::Sanitizer sanitizer; + sanitizer.set_num_glyphs (num_glyphs); + sbix_blob = sanitizer.sanitize (face->reference_table (HB_OT_TAG_sbix)); + sbix_len = hb_blob_get_length (sbix_blob); + sbix_table = sbix_blob->as (); + + } + + inline void fini (void) + { + hb_blob_destroy (sbix_blob); + } + + inline void dump (void (*callback) (const uint8_t* data, unsigned int length, + unsigned int group, unsigned int gid)) const + { + for (unsigned group = 0; group < sbix_table->strikes.len; ++group) + { + const SBIXStrike &strike = sbix_table->strikes[group](sbix_table); + for (unsigned int glyph = 0; glyph < num_glyphs; ++glyph) + if (strike.imageOffsetsZ[glyph + 1] - strike.imageOffsetsZ[glyph] > 0) + { + const SBIXGlyph &sbixGlyph = strike.imageOffsetsZ[glyph]((const void *) &strike); + callback ((const uint8_t*) &sbixGlyph.data, + strike.imageOffsetsZ[glyph + 1] - strike.imageOffsetsZ[glyph] - 8, + group, glyph); + } + } + } + + private: + hb_blob_t *sbix_blob; + const sbix *sbix_table; + + unsigned int sbix_len; + unsigned int num_glyphs; + + }; + + protected: + HBUINT16 version; /* Table version number — set to 1 */ + HBUINT16 flags; /* Bit 0: Set to 1. Bit 1: Draw outlines. + * Bits 2 to 15: reserved (set to 0). */ + LArrayOf > + strikes; /* Offsets from the beginning of the 'sbix' + * table to data for each individual bitmap strike. */ + public: + DEFINE_SIZE_ARRAY (8, strikes); +}; + +} /* namespace OT */ + +#endif /* HB_OT_COLOR_SBIX_TABLE_HH */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-svg-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-svg-table.hh new file mode 100644 index 00000000000..5c0ade330ba --- /dev/null +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-color-svg-table.hh @@ -0,0 +1,145 @@ +/* + * Copyright © 2018 Ebrahim Byagowi + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#ifndef HB_OT_COLOR_SVG_TABLE_HH +#define HB_OT_COLOR_SVG_TABLE_HH + +#include "hb-open-type-private.hh" + +/* + * SVG -- SVG (Scalable Vector Graphics) + * https://docs.microsoft.com/en-us/typography/opentype/spec/svg + */ + +#define HB_OT_TAG_SVG HB_TAG('S','V','G',' ') + + +namespace OT { + + +struct SVGDocumentIndexEntry +{ + friend struct SVG; + + inline bool sanitize (hb_sanitize_context_t *c, const void* base) const + { + TRACE_SANITIZE (this); + return_trace (c->check_struct (this) && + (base+svgDoc).sanitize (c, svgDocLength)); + } + + protected: + HBUINT16 startGlyphID; /* The first glyph ID in the range described by + * this index entry. */ + HBUINT16 endGlyphID; /* The last glyph ID in the range described by + * this index entry. Must be >= startGlyphID. */ + LOffsetTo > + svgDoc; /* Offset from the beginning of the SVG Document Index + * to an SVG document. Must be non-zero. */ + HBUINT32 svgDocLength; /* Length of the SVG document. + * Must be non-zero. */ + public: + DEFINE_SIZE_STATIC (12); +}; + +struct SVGDocumentIndex +{ + friend struct SVG; + + inline bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (c->check_struct (this) && + entries.sanitize (c, this)); + } + + protected: + ArrayOf + entries; /* Array of SVG Document Index Entries. */ + public: + DEFINE_SIZE_ARRAY (2, entries); +}; + +struct SVG +{ + static const hb_tag_t tableTag = HB_OT_TAG_SVG; + + inline bool sanitize (hb_sanitize_context_t *c) const + { + TRACE_SANITIZE (this); + return_trace (likely (c->check_struct (this) && + (this+svgDocIndex).sanitize (c))); + } + + struct accelerator_t + { + inline void init (hb_face_t *face) + { + OT::Sanitizer sanitizer; + svg_blob = sanitizer.sanitize (face->reference_table (HB_OT_TAG_SVG)); + svg_len = hb_blob_get_length (svg_blob); + svg = svg_blob->as (); + + } + + inline void fini (void) + { + hb_blob_destroy (svg_blob); + } + + inline void + dump (void (*callback) (const uint8_t* data, unsigned int length, + unsigned int start_glyph, unsigned int end_glyph)) const + { + const SVGDocumentIndex &index = svg+svg->svgDocIndex; + const ArrayOf &entries = index.entries; + for (unsigned int i = 0; i < entries.len; ++i) + { + const SVGDocumentIndexEntry &entry = entries[i]; + callback ((const uint8_t*) &entry.svgDoc (&index), entry.svgDocLength, + entry.startGlyphID, entry.endGlyphID); + } + } + + private: + hb_blob_t *svg_blob; + const SVG *svg; + + unsigned int svg_len; + }; + + protected: + HBUINT16 version; /* Table version (starting at 0). */ + LOffsetTo + svgDocIndex; /* Offset (relative to the start of the SVG table) to the + * SVG Documents Index. Must be non-zero. */ + HBUINT32 reserved; /* Set to 0. */ + public: + DEFINE_SIZE_STATIC (10); +}; + +} /* namespace OT */ + + +#endif /* HB_OT_COLOR_SVG_TABLE_HH */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-font.cc b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-font.cc index 93aa2399791..941f668ee4d 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-font.cc +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-font.cc @@ -143,7 +143,7 @@ hb_ot_get_glyph_h_kerning (hb_font_t *font, } static hb_bool_t -hb_ot_get_glyph_extents (hb_font_t *font HB_UNUSED, +hb_ot_get_glyph_extents (hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents, @@ -184,7 +184,7 @@ hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED, } static hb_bool_t -hb_ot_get_font_h_extents (hb_font_t *font HB_UNUSED, +hb_ot_get_font_h_extents (hb_font_t *font, void *font_data, hb_font_extents_t *metrics, void *user_data HB_UNUSED) @@ -198,7 +198,7 @@ hb_ot_get_font_h_extents (hb_font_t *font HB_UNUSED, } static hb_bool_t -hb_ot_get_font_v_extents (hb_font_t *font HB_UNUSED, +hb_ot_get_font_v_extents (hb_font_t *font, void *font_data, hb_font_extents_t *metrics, void *user_data HB_UNUSED) @@ -217,7 +217,12 @@ static hb_font_funcs_t *static_ot_funcs = nullptr; static void free_static_ot_funcs (void) { - hb_font_funcs_destroy (static_ot_funcs); +retry: + hb_font_funcs_t *ot_funcs = (hb_font_funcs_t *) hb_atomic_ptr_get (&static_ot_funcs); + if (!hb_atomic_ptr_cmpexch (&static_ot_funcs, ot_funcs, nullptr)) + goto retry; + + hb_font_funcs_destroy (ot_funcs); } #endif diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-glyf-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-glyf-table.hh index 57b9949ad19..ea349cb217b 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-glyf-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-glyf-table.hh @@ -38,8 +38,8 @@ namespace OT { /* * loca -- Index to Location + * https://docs.microsoft.com/en-us/typography/opentype/spec/loca */ - #define HB_OT_TAG_loca HB_TAG('l','o','c','a') @@ -56,15 +56,15 @@ struct loca } protected: - HBUINT8 dataX[VAR]; /* Location data. */ - DEFINE_SIZE_ARRAY (0, dataX); + HBUINT8 dataZ[VAR]; /* Location data. */ + DEFINE_SIZE_ARRAY (0, dataZ); }; /* * glyf -- TrueType Glyph Data + * https://docs.microsoft.com/en-us/typography/opentype/spec/glyf */ - #define HB_OT_TAG_glyf HB_TAG('g','l','y','f') @@ -88,9 +88,9 @@ struct glyf bool success = true; bool use_short_loca = false; if (hb_subset_glyf_and_loca (plan, &use_short_loca, &glyf_prime, &loca_prime)) { - success = success && hb_subset_plan_add_table (plan, HB_OT_TAG_glyf, glyf_prime); - success = success && hb_subset_plan_add_table (plan, HB_OT_TAG_loca, loca_prime); - success = success && _add_head_and_set_loca_version (plan->source, use_short_loca, plan->dest); + success = success && plan->add_table (HB_OT_TAG_glyf, glyf_prime); + success = success && plan->add_table (HB_OT_TAG_loca, loca_prime); + success = success && _add_head_and_set_loca_version (plan, use_short_loca); } else { success = false; } @@ -101,9 +101,9 @@ struct glyf } static bool - _add_head_and_set_loca_version (hb_face_t *source, bool use_short_loca, hb_face_t *dest) + _add_head_and_set_loca_version (hb_subset_plan_t *plan, bool use_short_loca) { - hb_blob_t *head_blob = OT::Sanitizer().sanitize (hb_face_reference_table (source, HB_OT_TAG_head)); + hb_blob_t *head_blob = OT::Sanitizer().sanitize (hb_face_reference_table (plan->source, HB_OT_TAG_head)); hb_blob_t *head_prime_blob = hb_blob_copy_writable_or_fail (head_blob); hb_blob_destroy (head_blob); @@ -112,7 +112,7 @@ struct glyf OT::head *head_prime = (OT::head *) hb_blob_get_data_writable (head_prime_blob, nullptr); head_prime->indexToLocFormat.set (use_short_loca ? 0 : 1); - bool success = hb_subset_face_add_table (dest, HB_OT_TAG_head, head_prime_blob); + bool success = plan->add_table (HB_OT_TAG_head, head_prime_blob); hb_blob_destroy (head_prime_blob); return success; @@ -134,18 +134,20 @@ struct glyf struct CompositeGlyphHeader { - static const uint16_t ARG_1_AND_2_ARE_WORDS = 0x0001; - static const uint16_t ARGS_ARE_XY_VALUES = 0x0002; - static const uint16_t ROUND_XY_TO_GRID = 0x0004; - static const uint16_t WE_HAVE_A_SCALE = 0x0008; - static const uint16_t MORE_COMPONENTS = 0x0020; - static const uint16_t WE_HAVE_AN_X_AND_Y_SCALE = 0x0040; - static const uint16_t WE_HAVE_A_TWO_BY_TWO = 0x0080; - static const uint16_t WE_HAVE_INSTRUCTIONS = 0x0100; - static const uint16_t USE_MY_METRICS = 0x0200; - static const uint16_t OVERLAP_COMPOUND = 0x0400; - static const uint16_t SCALED_COMPONENT_OFFSET = 0x0800; - static const uint16_t UNSCALED_COMPONENT_OFFSET = 0x1000; + enum composite_glyph_flag_t { + ARG_1_AND_2_ARE_WORDS = 0x0001, + ARGS_ARE_XY_VALUES = 0x0002, + ROUND_XY_TO_GRID = 0x0004, + WE_HAVE_A_SCALE = 0x0008, + MORE_COMPONENTS = 0x0020, + WE_HAVE_AN_X_AND_Y_SCALE = 0x0040, + WE_HAVE_A_TWO_BY_TWO = 0x0080, + WE_HAVE_INSTRUCTIONS = 0x0100, + USE_MY_METRICS = 0x0200, + OVERLAP_COMPOUND = 0x0400, + SCALED_COMPONENT_OFFSET = 0x0800, + UNSCALED_COMPONENT_OFFSET = 0x1000 + }; HBUINT16 flags; HBUINT16 glyphIndex; @@ -232,11 +234,13 @@ struct glyf { inline void init (hb_face_t *face) { + memset (this, 0, sizeof (accelerator_t)); + hb_blob_t *head_blob = Sanitizer().sanitize (face->reference_table (HB_OT_TAG_head)); - const head *head_table = Sanitizer::lock_instance (head_blob); - if ((unsigned int) head_table->indexToLocFormat > 1 || head_table->glyphDataFormat != 0) + const head *head_table = head_blob->as (); + if (head_table == &Null(head) || (unsigned int) head_table->indexToLocFormat > 1 || head_table->glyphDataFormat != 0) { - /* Unknown format. Leave num_glyphs=0, that takes care of disabling us. */ + /* head table is not present, or in an unknown format. Leave num_glyphs=0, that takes care of disabling us. */ hb_blob_destroy (head_blob); return; } @@ -244,9 +248,9 @@ struct glyf hb_blob_destroy (head_blob); loca_blob = Sanitizer().sanitize (face->reference_table (HB_OT_TAG_loca)); - loca_table = Sanitizer::lock_instance (loca_blob); + loca_table = loca_blob->as (); glyf_blob = Sanitizer().sanitize (face->reference_table (HB_OT_TAG_glyf)); - glyf_table = Sanitizer::lock_instance (glyf_blob); + glyf_table = glyf_blob->as (); num_glyphs = MAX (1u, hb_blob_get_length (loca_blob) / (short_offset ? 2 : 4)) - 1; glyf_len = hb_blob_get_length (glyf_blob); @@ -266,6 +270,9 @@ struct glyf inline bool get_composite (hb_codepoint_t glyph, CompositeGlyphHeader::Iterator *composite /* OUT */) const { + if (this->glyf_table == &Null(glyf) || !num_glyphs) + return false; + unsigned int start_offset, end_offset; if (!get_offsets (glyph, &start_offset, &end_offset)) return false; /* glyph not found */ @@ -275,16 +282,18 @@ struct glyf composite); } + enum simple_glyph_flag_t { + FLAG_X_SHORT = 0x02, + FLAG_Y_SHORT = 0x04, + FLAG_REPEAT = 0x08, + FLAG_X_SAME = 0x10, + FLAG_Y_SAME = 0x20 + }; + /* based on FontTools _g_l_y_f.py::trim */ inline bool remove_padding(unsigned int start_offset, unsigned int *end_offset) const { - static const int FLAG_X_SHORT = 0x02; - static const int FLAG_Y_SHORT = 0x04; - static const int FLAG_REPEAT = 0x08; - static const int FLAG_X_SAME = 0x10; - static const int FLAG_Y_SAME = 0x20; - if (*end_offset - start_offset < GlyphHeader::static_size) return true; @@ -368,13 +377,13 @@ struct glyf if (short_offset) { - const HBUINT16 *offsets = (const HBUINT16 *) loca_table->dataX; + const HBUINT16 *offsets = (const HBUINT16 *) loca_table->dataZ; *start_offset = 2 * offsets[glyph]; *end_offset = 2 * offsets[glyph + 1]; } else { - const HBUINT32 *offsets = (const HBUINT32 *) loca_table->dataX; + const HBUINT32 *offsets = (const HBUINT32 *) loca_table->dataZ; *start_offset = offsets[glyph]; *end_offset = offsets[glyph + 1]; @@ -411,7 +420,7 @@ struct glyf } while (composite_it.move_to_next()); if ( (uint16_t) last->flags & CompositeGlyphHeader::WE_HAVE_INSTRUCTIONS) - *instruction_start = ((char *) last - (char *) glyf_table->dataX) + last->get_size(); + *instruction_start = ((char *) last - (char *) glyf_table->dataZ) + last->get_size(); else *instruction_start = end_offset; *instruction_end = end_offset; @@ -424,9 +433,23 @@ struct glyf else { unsigned int instruction_length_offset = start_offset + GlyphHeader::static_size + 2 * num_contours; + if (unlikely (instruction_length_offset + 2 > end_offset)) + { + DEBUG_MSG(SUBSET, nullptr, "Glyph size is too short, missing field instructionLength."); + return false; + } + const HBUINT16 &instruction_length = StructAtOffset (glyf_table, instruction_length_offset); - *instruction_start = instruction_length_offset + 2; - *instruction_end = *instruction_start + (uint16_t) instruction_length; + unsigned int start = instruction_length_offset + 2; + unsigned int end = start + (uint16_t) instruction_length; + if (unlikely (end > end_offset)) // Out of bounds of the current glyph + { + DEBUG_MSG(SUBSET, nullptr, "The instructions array overruns the glyph's boundaries."); + return false; + } + + *instruction_start = start; + *instruction_end = end; } return true; } @@ -462,9 +485,9 @@ struct glyf }; protected: - HBUINT8 dataX[VAR]; /* Glyphs data. */ + HBUINT8 dataZ[VAR]; /* Glyphs data. */ - DEFINE_SIZE_ARRAY (0, dataX); + DEFINE_SIZE_ARRAY (0, dataZ); }; } /* namespace OT */ diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-head-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-head-table.hh index b64c2a362f9..34d45fe4761 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-head-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-head-table.hh @@ -31,16 +31,16 @@ #include "hb-open-type-private.hh" +/* + * head -- Font Header + * https://docs.microsoft.com/en-us/typography/opentype/spec/head + */ +#define HB_OT_TAG_head HB_TAG('h','e','a','d') + namespace OT { -/* - * head -- Font Header - */ - -#define HB_OT_TAG_head HB_TAG('h','e','a','d') - struct head { friend struct OffsetTable; diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-hhea-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-hhea-table.hh index aa3d406f6d5..4b7a668192c 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-hhea-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-hhea-table.hh @@ -29,18 +29,19 @@ #include "hb-open-type-private.hh" +/* + * hhea -- Horizontal Header + * https://docs.microsoft.com/en-us/typography/opentype/spec/hhea + * vhea -- Vertical Header + * https://docs.microsoft.com/en-us/typography/opentype/spec/vhea + */ +#define HB_OT_TAG_hhea HB_TAG('h','h','e','a') +#define HB_OT_TAG_vhea HB_TAG('v','h','e','a') + namespace OT { -/* - * hhea -- The Horizontal Header Table - * vhea -- The Vertical Header Table - */ - -#define HB_OT_TAG_hhea HB_TAG('h','h','e','a') -#define HB_OT_TAG_vhea HB_TAG('v','h','e','a') - template struct _hea { diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-hmtx-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-hmtx-table.hh index 305faa94fb8..cb17755a8aa 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-hmtx-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-hmtx-table.hh @@ -31,20 +31,21 @@ #include "hb-ot-hhea-table.hh" #include "hb-ot-os2-table.hh" #include "hb-ot-var-hvar-table.hh" +#include "hb-subset-plan.hh" + +/* + * hmtx -- Horizontal Metrics + * https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx + * vmtx -- Vertical Metrics + * https://docs.microsoft.com/en-us/typography/opentype/spec/vmtx + */ +#define HB_OT_TAG_hmtx HB_TAG('h','m','t','x') +#define HB_OT_TAG_vmtx HB_TAG('v','m','t','x') namespace OT { -/* - * hmtx -- The Horizontal Metrics Table - * vmtx -- The Vertical Metrics Table - */ - -#define HB_OT_TAG_hmtx HB_TAG('h','m','t','x') -#define HB_OT_TAG_vmtx HB_TAG('v','m','t','x') - - struct LongMetric { UFWORD advance; /* Advance width/height. */ @@ -80,7 +81,7 @@ struct hmtxvmtx H *table = (H *) hb_blob_get_data (dest_blob, &length); table->numberOfLongMetrics.set (num_hmetrics); - bool result = hb_subset_plan_add_table (plan, H::tableTag, dest_blob); + bool result = plan->add_table (H::tableTag, dest_blob); hb_blob_destroy (dest_blob); return result; @@ -93,7 +94,7 @@ struct hmtxvmtx /* All the trailing glyphs with the same advance can use one LongMetric * and just keep LSB */ - hb_prealloced_array_t &gids = plan->gids_to_retain_sorted; + hb_vector_t &gids = plan->glyphs; unsigned int num_advances = gids.len; unsigned int last_advance = _mtx.get_advance (gids[num_advances - 1]); while (num_advances > 1 @@ -118,6 +119,8 @@ struct hmtxvmtx LongMetric * old_metrics = (LongMetric *) source_table; FWORD *lsbs = (FWORD *) (old_metrics + _mtx.num_advances); char * dest_pos = (char *) dest; + + bool failed = false; for (unsigned int i = 0; i < gids.len; i++) { /* the last metric or the one for gids[i] */ @@ -138,6 +141,13 @@ struct hmtxvmtx } else { + if (gids[i] >= _mtx.num_metrics) + { + DEBUG_MSG(SUBSET, nullptr, "gid %d is >= number of source metrics %d", + gids[i], _mtx.num_metrics); + failed = true; + break; + } FWORD src_lsb = *(lsbs + gids[i] - _mtx.num_advances); if (i < num_advances) { @@ -157,7 +167,7 @@ struct hmtxvmtx _mtx.fini (); // Amend header num hmetrics - if (unlikely (!subset_update_header (plan, num_advances))) + if (failed || unlikely (!subset_update_header (plan, num_advances))) { free (dest); return false; @@ -168,7 +178,7 @@ struct hmtxvmtx HB_MEMORY_MODE_READONLY, dest, free); - bool success = hb_subset_plan_add_table (plan, T::tableTag, result); + bool success = plan->add_table (T::tableTag, result); hb_blob_destroy (result); return success; } @@ -186,7 +196,7 @@ struct hmtxvmtx if (T::os2Tag) { hb_blob_t *os2_blob = Sanitizer ().sanitize (face->reference_table (T::os2Tag)); - const os2 *os2_table = Sanitizer::lock_instance (os2_blob); + const os2 *os2_table = os2_blob->as (); #define USE_TYPO_METRICS (1u<<7) if (0 != (os2_table->fsSelection & USE_TYPO_METRICS)) { @@ -199,7 +209,7 @@ struct hmtxvmtx } hb_blob_t *_hea_blob = Sanitizer ().sanitize (face->reference_table (H::tableTag)); - const H *_hea_table = Sanitizer::lock_instance (_hea_blob); + const H *_hea_table = _hea_blob->as (); num_advances = _hea_table->numberOfLongMetrics; if (!got_font_extents) { @@ -228,10 +238,10 @@ struct hmtxvmtx hb_blob_destroy (blob); blob = hb_blob_get_empty (); } - table = Sanitizer::lock_instance (blob); + table = blob->as (); var_blob = Sanitizer ().sanitize (face->reference_table (T::variationsTag)); - var_table = Sanitizer::lock_instance (var_blob); + var_table = var_blob->as (); } inline void fini (void) diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-kern-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-kern-table.hh index a98ebf6ec37..df9c05182b0 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-kern-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-kern-table.hh @@ -29,15 +29,17 @@ #include "hb-open-type-private.hh" -namespace OT { - - /* * kern -- Kerning + * https://docs.microsoft.com/en-us/typography/opentype/spec/kern + * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kern.html */ - #define HB_OT_TAG_kern HB_TAG('k','e','r','n') + +namespace OT { + + struct hb_glyph_pair_t { hb_codepoint_t left; @@ -205,7 +207,7 @@ struct KernSubTableWrapper { TRACE_SANITIZE (this); return_trace (c->check_struct (thiz()) && - thiz()->length >= thiz()->min_size && + thiz()->length >= T::min_size && c->check_array (thiz(), 1, thiz()->length) && thiz()->subtable.sanitize (c, thiz()->format)); } @@ -361,8 +363,8 @@ struct kern inline void init (hb_face_t *face) { blob = Sanitizer().sanitize (face->reference_table (HB_OT_TAG_kern)); - table = Sanitizer::lock_instance (blob); - table_length = hb_blob_get_length (blob); + table = blob->as (); + table_length = blob->length; } inline void fini (void) { diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-layout-base-table.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-layout-base-table.hh index bbc215cf3c8..c4aafe7d2a2 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-layout-base-table.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-layout-base-table.hh @@ -36,7 +36,8 @@ namespace OT { #define NOT_INDEXED ((unsigned int) -1) /* - * BASE -- The BASE Table + * BASE -- Baseline + * https://docs.microsoft.com/en-us/typography/opentype/spec/base */ struct BaseCoordFormat1 diff --git a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-layout-common-private.hh b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-layout-common-private.hh index a5a33db34e2..85148450873 100644 --- a/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-layout-common-private.hh +++ b/src/java.desktop/share/native/libfontmanager/harfbuzz/hb-ot-layout-common-private.hh @@ -165,7 +165,7 @@ struct RangeRecord public: DEFINE_SIZE_STATIC (6); }; -DEFINE_NULL_DATA (RangeRecord, "\000\001"); +DEFINE_NULL_DATA (OT, RangeRecord, "\000\001"); struct IndexArray : ArrayOf @@ -225,7 +225,7 @@ struct LangSys public: DEFINE_SIZE_ARRAY (6, featureIndex); }; -DEFINE_NULL_DATA (LangSys, "\0\0\xFF\xFF"); +DEFINE_NULL_DATA (OT, LangSys, "\0\0\xFF\xFF"); struct Script @@ -270,7 +270,7 @@ struct Script typedef RecordListOf