From e4fe8bf95e4e7080abfdb005e48563b56b1c8fc6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Mar 2025 20:49:14 -0600 Subject: [PATCH 1/9] [ft] Add hb_ft_face_create_from_blob_or_fail() Part of https://github.com/harfbuzz/harfbuzz/issues/5117 Untested. New API: +hb_ft_face_create_from_blob_or_fail() --- docs/harfbuzz-sections.txt | 1 + src/hb-ft.cc | 72 +++++++++++++++++++++++++++++++++++++- src/hb-ft.h | 4 +++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index f09c85128..736f66179 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -536,6 +536,7 @@ hb_ft_face_create hb_ft_face_create_cached hb_ft_face_create_referenced hb_ft_face_create_from_file_or_fail +hb_ft_face_create_from_blob_or_fail hb_ft_font_create hb_ft_font_create_referenced hb_ft_font_changed diff --git a/src/hb-ft.cc b/src/hb-ft.cc index 3159a9ec3..8e83a3deb 100644 --- a/src/hb-ft.cc +++ b/src/hb-ft.cc @@ -1610,7 +1610,8 @@ destroy_ft_library (void *arg) * font file and face index. * * This is similar in functionality to hb_face_create_from_file_or_fail(), - * but uses the FreeType library for loading the font file. + * but uses the FreeType library for loading the font file. This can + * be useful, for example, to load WOFF and WOFF2 font data. * * Return value: (transfer full): The new face object, or `NULL` if * no face is found at the specified index or the file cannot be read. @@ -1647,6 +1648,75 @@ hb_ft_face_create_from_file_or_fail (const char *file_name, return face; } +static hb_user_data_key_t ft_blob_key = {0}; + +static void +_destroy_blob (void *p) +{ + hb_blob_destroy ((hb_blob_t *) p); +} + +/** + * hb_ft_face_create_from_blob_or_fail: + * @blob: A blob + * @index: The index of the face within the blob + * + * Creates an #hb_face_t face object from the specified + * font blob and face index. + * + * This is similar in functionality to hb_face_create_from_blob_or_fail(), + * but uses the FreeType library for loading the font blob. This can + * be useful, for example, to load WOFF and WOFF2 font data. + * + * Return value: (transfer full): The new face object, or `NULL` if + * loading fails (eg. blob does not contain valid font data). + * + * XSince: REPLACEME + */ +hb_face_t * +hb_ft_face_create_from_blob_or_fail (hb_blob_t *blob, + unsigned int index) +{ + FT_Library ft_library = reference_ft_library (); + if (unlikely (!ft_library)) + { + DEBUG_MSG (FT, ft_library, "reference_ft_library failed"); + return nullptr; + } + + hb_blob_make_immutable (blob); + unsigned blob_size; + const char *blob_data = hb_blob_get_data (blob, &blob_size); + + FT_Face ft_face; + if (unlikely (FT_New_Memory_Face (ft_library, + (const FT_Byte *) blob_data, + blob_size, + index, + &ft_face))) + return nullptr; + + hb_face_t *face = hb_ft_face_create_referenced (ft_face); + FT_Done_Face (ft_face); + + ft_face->generic.data = ft_library; + ft_face->generic.finalizer = finalize_ft_library; + + if (hb_face_is_immutable (face)) + return nullptr; + + // Hook the blob to the hb_face_t, since FT_Face still needs it. + hb_blob_reference (blob); + if (!hb_face_set_user_data (face, &ft_blob_key, blob, _destroy_blob, true)) + { + hb_blob_destroy (blob); + hb_face_destroy (face); + return nullptr; + } + + return face; +} + static void _release_blob (void *arg) { diff --git a/src/hb-ft.h b/src/hb-ft.h index 42bfb4f46..227b4f221 100644 --- a/src/hb-ft.h +++ b/src/hb-ft.h @@ -88,6 +88,10 @@ HB_EXTERN hb_face_t * hb_ft_face_create_from_file_or_fail (const char *file_name, unsigned int index); +HB_EXTERN hb_face_t * +hb_ft_face_create_from_blob_or_fail (hb_blob_t *blob, + unsigned int index); + /* * hb-font from ft-face. */ From 8ca9fe7617ba087e8b4937666642458d7adaf6d4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Mar 2025 21:13:07 -0600 Subject: [PATCH 2/9] [face] Avoid edit-sanitizing in hb_face_count() --- src/hb-face.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/hb-face.cc b/src/hb-face.cc index e35aa1caf..4db8c7a92 100644 --- a/src/hb-face.cc +++ b/src/hb-face.cc @@ -79,14 +79,14 @@ hb_face_count (hb_blob_t *blob) if (unlikely (!blob)) return 0; - /* TODO We shouldn't be sanitizing blob. Port to run sanitizer and return if not sane. */ - /* Make API signature const after. */ - hb_blob_t *sanitized = hb_sanitize_context_t ().sanitize_blob (hb_blob_reference (blob)); - const OT::OpenTypeFontFile& ot = *sanitized->as (); - unsigned int ret = ot.get_face_count (); - hb_blob_destroy (sanitized); + hb_sanitize_context_t c (blob); - return ret; + const char *start = hb_blob_get_data (blob, nullptr); + auto *ot = reinterpret_cast (const_cast (start)); + if (unlikely (!ot->sanitize (&c))) + return 0; + + return ot->get_face_count (); } /* From f98c203e7a3e3a18433f830f7e633005362397c6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Mar 2025 21:30:28 -0600 Subject: [PATCH 3/9] [coretext] Add hb_coretext_face_create_from_blob_or_fail() Part of https://github.com/harfbuzz/harfbuzz/issues/5117 Untested. New API: +hb_coretext_face_create_from_blob_or_fail() --- docs/harfbuzz-sections.txt | 1 + src/hb-coretext-shape.cc | 76 ++++++++++++++++++++++++++++++-------- src/hb-coretext.h | 4 ++ 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 736f66179..6592277c3 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -347,6 +347,7 @@ HB_CORETEXT_TAG_MORT HB_CORETEXT_TAG_MORX hb_coretext_face_create hb_coretext_face_create_from_file_or_fail +hb_coretext_face_create_from_blob_or_fail hb_coretext_font_create hb_coretext_face_get_cg_font hb_coretext_font_get_ct_font diff --git a/src/hb-coretext-shape.cc b/src/hb-coretext-shape.cc index 2e029fe2b..33296c77c 100644 --- a/src/hb-coretext-shape.cc +++ b/src/hb-coretext-shape.cc @@ -160,30 +160,42 @@ release_data (void *info, const void *data, size_t size) hb_blob_destroy ((hb_blob_t *) info); } +static CGFontRef +create_cg_font (hb_blob_t *blob, unsigned int index) +{ + if (unlikely (index != 0)) + return nullptr; // How? + + hb_blob_make_immutable (blob); + unsigned int blob_length; + const char *blob_data = hb_blob_get_data (blob, &blob_length); + if (unlikely (!blob_length)) + DEBUG_MSG (CORETEXT, blob, "Empty blob"); + + hb_blob_reference (blob); + CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data); + CGFontRef cg_font = nullptr; + if (likely (provider)) + { + cg_font = CGFontCreateWithDataProvider (provider); + if (unlikely (!cg_font)) + DEBUG_MSG (CORETEXT, blob, "CGFontCreateWithDataProvider() failed"); + CGDataProviderRelease (provider); + } + return cg_font; +} + static CGFontRef create_cg_font (hb_face_t *face) { CGFontRef cg_font = nullptr; if (face->destroy == _hb_cg_font_release) - { cg_font = CGFontRetain ((CGFontRef) face->user_data); - } else { hb_blob_t *blob = hb_face_reference_blob (face); - unsigned int blob_length; - const char *blob_data = hb_blob_get_data (blob, &blob_length); - if (unlikely (!blob_length)) - DEBUG_MSG (CORETEXT, face, "Face has empty blob"); - - CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data); - if (likely (provider)) - { - cg_font = CGFontCreateWithDataProvider (provider); - if (unlikely (!cg_font)) - DEBUG_MSG (CORETEXT, face, "Face CGFontCreateWithDataProvider() failed"); - CGDataProviderRelease (provider); - } + cg_font = create_cg_font (blob, face->index); + hb_blob_destroy (blob); } return cg_font; } @@ -404,6 +416,40 @@ hb_coretext_face_create_from_file_or_fail (const char *file_name, return face; } +/** + * hb_coretext_face_create_from_blob_or_fail: + * @blob: A blob containing the font data + * @index: The index of the face within the blob + * + * Creates an #hb_face_t face object from the specified + * blob and face index. + * + * This is similar in functionality to hb_face_create_from_blob_or_fail(), + * but uses the CoreText library for loading the font data. + * + * Note that currently only @index equal to 0 is supported. + * + * Return value: (transfer full): The new face object, or `NULL` if + * no face is found at the specified index or the blob cannot be read. + * + * XSince: REPLACEME + */ +hb_face_t * +hb_coretext_face_create_from_blob_or_fail (hb_blob_t *blob, + unsigned int index) +{ + auto cg_font = create_cg_font (blob, index); + if (unlikely (!cg_font)) + return nullptr; + + hb_face_t *face = hb_coretext_face_create (cg_font); + CFRelease (cg_font); + if (unlikely (hb_face_is_immutable (face))) + return nullptr; + + return face; +} + /** * hb_coretext_face_get_cg_font: * @face: The #hb_face_t to work upon diff --git a/src/hb-coretext.h b/src/hb-coretext.h index 3626f1c12..a7c7fee76 100644 --- a/src/hb-coretext.h +++ b/src/hb-coretext.h @@ -84,6 +84,10 @@ HB_EXTERN hb_face_t * hb_coretext_face_create_from_file_or_fail (const char *file_name, unsigned int index); +HB_EXTERN hb_face_t * +hb_coretext_face_create_from_blob_or_fail (hb_blob_t *blob, + unsigned int index); + HB_EXTERN hb_font_t * hb_coretext_font_create (CTFontRef ct_font); From b1c50eb938d5a6d7fdf09bf8490b1b54c1c6aedf Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Mar 2025 21:35:46 -0600 Subject: [PATCH 4/9] [face] Rename a variable --- src/hb-face.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hb-face.cc b/src/hb-face.cc index 4db8c7a92..b6c7d80c1 100644 --- a/src/hb-face.cc +++ b/src/hb-face.cc @@ -370,15 +370,15 @@ hb_face_create_from_file_or_fail_using (const char *file_name, if (!loader_name || !*loader_name) { - static hb_atomic_ptr_t static_funcs_name; - loader_name = static_funcs_name.get_acquire (); + static hb_atomic_ptr_t static_loader_name; + loader_name = static_loader_name.get_acquire (); if (!loader_name) { loader_name = getenv ("HB_FACE_LOADER"); if (!loader_name) loader_name = ""; - if (!static_funcs_name.cmpexch (nullptr, loader_name)) - loader_name = static_funcs_name.get_acquire (); + if (!static_loader_name.cmpexch (nullptr, loader_name)) + loader_name = static_loader_name.get_acquire (); } retry = true; } From cbc205c2f025d1980d24737fbbb87be1eb73b9aa Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Mar 2025 21:41:54 -0600 Subject: [PATCH 5/9] [font/face] Refactor some code --- src/hb-face.cc | 27 ++++++++++++++++----------- src/hb-font.cc | 26 ++++++++++++++++---------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/hb-face.cc b/src/hb-face.cc index b6c7d80c1..56ff340ff 100644 --- a/src/hb-face.cc +++ b/src/hb-face.cc @@ -342,6 +342,21 @@ static struct supported_face_loaders_t { #endif }; +static const char *get_default_loader_name () +{ + static hb_atomic_ptr_t static_loader_name; + const char *loader_name = static_loader_name.get_acquire (); + if (!loader_name) + { + loader_name = getenv ("HB_FACE_LOADER"); + if (!loader_name) + loader_name = ""; + if (!static_loader_name.cmpexch (nullptr, loader_name)) + loader_name = static_loader_name.get_acquire (); + } + return loader_name; +} + /** * hb_face_create_from_file_or_fail_using: * @file_name: A font filename @@ -367,19 +382,9 @@ hb_face_create_from_file_or_fail_using (const char *file_name, const char *loader_name) { bool retry = false; - if (!loader_name || !*loader_name) { - static hb_atomic_ptr_t static_loader_name; - loader_name = static_loader_name.get_acquire (); - if (!loader_name) - { - loader_name = getenv ("HB_FACE_LOADER"); - if (!loader_name) - loader_name = ""; - if (!static_loader_name.cmpexch (nullptr, loader_name)) - loader_name = static_loader_name.get_acquire (); - } + loader_name = get_default_loader_name (); retry = true; } if (loader_name && !*loader_name) loader_name = nullptr; diff --git a/src/hb-font.cc b/src/hb-font.cc index 33d71c86d..28d9ff2f5 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -2321,6 +2321,21 @@ static struct supported_font_funcs_t { #endif }; +static const char *get_default_funcs_name () +{ + static hb_atomic_ptr_t static_funcs_name; + const char *name = static_funcs_name.get_acquire (); + if (!name) + { + name = getenv ("HB_FONT_FUNCS"); + if (!name) + name = ""; + if (!static_funcs_name.cmpexch (nullptr, name)) + name = static_funcs_name.get_acquire (); + } + return name; +} + /** * hb_font_set_funcs_using: * @font: #hb_font_t to work upon @@ -2345,16 +2360,7 @@ hb_font_set_funcs_using (hb_font_t *font, if (!name || !*name) { - static hb_atomic_ptr_t static_funcs_name; - name = static_funcs_name.get_acquire (); - if (!name) - { - name = getenv ("HB_FONT_FUNCS"); - if (!name) - name = ""; - if (!static_funcs_name.cmpexch (nullptr, name)) - name = static_funcs_name.get_acquire (); - } + name = get_default_funcs_name (); retry = true; } if (name && !*name) name = nullptr; From 8e8a9f6f40554fc716c7d6657fb48ca431648205 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Mar 2025 21:48:36 -0600 Subject: [PATCH 6/9] [face] Add hb_face_create_or_fail_using() Fixes https://github.com/harfbuzz/harfbuzz/issues/5117 Untested. New API: +hb_face_create_or_fail_using --- docs/harfbuzz-sections.txt | 1 + src/hb-face.cc | 77 ++++++++++++++++++++++++++++++++++---- src/hb-face.h | 5 +++ 3 files changed, 76 insertions(+), 7 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 6592277c3..fb29dba9b 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -368,6 +368,7 @@ hb_face_count hb_face_t hb_face_create hb_face_create_or_fail +hb_face_create_or_fail_using hb_face_create_from_file_or_fail hb_face_create_from_file_or_fail_using hb_face_list_loaders diff --git a/src/hb-face.cc b/src/hb-face.cc index 56ff340ff..0399e720e 100644 --- a/src/hb-face.cc +++ b/src/hb-face.cc @@ -328,17 +328,29 @@ hb_face_create_from_file_or_fail (const char *file_name, static struct supported_face_loaders_t { char name[9]; - hb_face_t * (*func) (const char *font_file, unsigned face_index); + hb_face_t * (*from_file) (const char *font_file, unsigned face_index); + hb_face_t * (*from_blob) (hb_blob_t *blob, unsigned face_index); } supported_face_loaders[] = { + {"ot", #ifndef HB_NO_OPEN - {"ot", hb_face_create_from_file_or_fail}, + hb_face_create_from_file_or_fail, +#else + nullptr, #endif + hb_face_create_or_fail + }, #ifdef HAVE_FREETYPE - {"ft", hb_ft_face_create_from_file_or_fail}, + {"ft", + hb_ft_face_create_from_file_or_fail, + hb_ft_face_create_from_blob_or_fail + }, #endif #ifdef HAVE_CORETEXT - {"coretext", hb_coretext_face_create_from_file_or_fail}, + {"coretext", + hb_coretext_face_create_from_file_or_fail, + hb_coretext_face_create_from_blob_or_fail + }, #endif }; @@ -368,7 +380,7 @@ static const char *get_default_loader_name () * is used. * * For example, the FreeType ("ft") loader might be able to load - * .woff and .woff2 files if FreeType is built with those features, + * WOFF and WOFF2 files if FreeType is built with those features, * whereas the OpenType ("ot") loader will not. * * Return value: (transfer full): The new face object, or `NULL` if @@ -381,6 +393,7 @@ hb_face_create_from_file_or_fail_using (const char *file_name, unsigned int index, const char *loader_name) { + // Duplicated in hb_face_create_or_fail_using bool retry = false; if (!loader_name || !*loader_name) { @@ -392,8 +405,58 @@ hb_face_create_from_file_or_fail_using (const char *file_name, retry: for (unsigned i = 0; i < ARRAY_LENGTH (supported_face_loaders); i++) { - if (!loader_name || !strcmp (supported_face_loaders[i].name, loader_name)) - return supported_face_loaders[i].func (file_name, index); + if (!loader_name || (supported_face_loaders[i].from_file && !strcmp (supported_face_loaders[i].name, loader_name))) + return supported_face_loaders[i].from_file (file_name, index); + } + + if (retry) + { + retry = false; + loader_name = nullptr; + goto retry; + } + + return nullptr; +} + +/** + * hb_face_create_or_fail_using: + * @blob: #hb_blob_t to work upon + * @index: The index of the face within @blob + * @loader_name: (nullable): The name of the loader to use, or `NULL` + * + * A thin wrapper around the face loader functions registered with HarfBuzz. + * If @loader_name is `NULL` or the empty string, the first available loader + * is used. + * + * For example, the FreeType ("ft") loader might be able to load + * WOFF and WOFF2 files if FreeType is built with those features, + * whereas the OpenType ("ot") loader will not. + * + * Return value: (transfer full): The new face object, or `NULL` if + * the loader fails to load the face. + * + * XSince: REPLACEME + **/ +hb_face_t * +hb_face_create_or_fail_using (hb_blob_t *blob, + unsigned int index, + const char *loader_name) +{ + // Duplicated in hb_face_create_from_file_or_fail_using + bool retry = false; + if (!loader_name || !*loader_name) + { + loader_name = get_default_loader_name (); + retry = true; + } + if (loader_name && !*loader_name) loader_name = nullptr; + +retry: + for (unsigned i = 0; i < ARRAY_LENGTH (supported_face_loaders); i++) + { + if (!loader_name || (supported_face_loaders[i].from_blob && !strcmp (supported_face_loaders[i].name, loader_name))) + return supported_face_loaders[i].from_blob (blob, index); } if (retry) diff --git a/src/hb-face.h b/src/hb-face.h index c59ec0c4b..5d39a48cc 100644 --- a/src/hb-face.h +++ b/src/hb-face.h @@ -63,6 +63,11 @@ HB_EXTERN hb_face_t * hb_face_create_or_fail (hb_blob_t *blob, unsigned int index); +HB_EXTERN hb_face_t * +hb_face_create_or_fail_using (hb_blob_t *blob, + unsigned int index, + const char *loader_name); + HB_EXTERN hb_face_t * hb_face_create_from_file_or_fail (const char *file_name, unsigned int index); From 8687f5c38ef27da72279a15cfb07c8f2739eb78a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Mar 2025 23:11:27 -0600 Subject: [PATCH 7/9] [face] Add tests for new constructors --- test/api/hb-test.h | 16 +++++-- test/api/meson.build | 1 + test/api/test-face.c | 111 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 test/api/test-face.c diff --git a/test/api/hb-test.h b/test/api/hb-test.h index cdf355580..82006364d 100644 --- a/test/api/hb-test.h +++ b/test/api/hb-test.h @@ -305,14 +305,20 @@ G_STMT_START { \ } G_STMT_END +static inline char * +hb_test_resolve_path (const char *path) +{ +#if GLIB_CHECK_VERSION(2,37,2) + return g_test_build_filename (G_TEST_DIST, path, NULL); +#else + return path = g_strdup (path); +#endif +} + static inline hb_face_t * hb_test_open_font_file (const char *font_path) { -#if GLIB_CHECK_VERSION(2,37,2) - char *path = g_test_build_filename (G_TEST_DIST, font_path, NULL); -#else - char *path = g_strdup (font_path); -#endif + char *path = hb_test_resolve_path (font_path); hb_blob_t *blob = hb_blob_create_from_file_or_fail (path); hb_face_t *face; diff --git a/test/api/meson.build b/test/api/meson.build index 24d0d854f..bc4e0c5b8 100644 --- a/test/api/meson.build +++ b/test/api/meson.build @@ -18,6 +18,7 @@ tests = [ 'test-draw.c', 'test-draw-varc.c', 'test-extents.c', + 'test-face.c', 'test-font.c', 'test-font-scale.c', 'test-get-table-tags.c', diff --git a/test/api/test-face.c b/test/api/test-face.c new file mode 100644 index 000000000..7f9cda165 --- /dev/null +++ b/test/api/test-face.c @@ -0,0 +1,111 @@ +/* + * 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. + * + * Author(s): Behdad Esfahbod + */ + +#include "hb-test.h" + +/* Unit tests for hb-face.h */ + +#define FONT_FILE "fonts/Roboto-Regular.ac.ttf" +static const char *font_file; + +hb_face_t *master_face = NULL; + +#define HEAD_TAG HB_TAG ('h', 'e', 'a', 'd') +hb_blob_t *master_head = NULL; + +static void +test_face (hb_face_t *face) +{ + g_assert (face); + + hb_blob_t *head = hb_face_reference_table (face, HEAD_TAG); + + unsigned int length = hb_blob_get_length (head); + unsigned int master_length = hb_blob_get_length (master_head); + g_assert_cmpuint (length, ==, master_length); + + const char *data = hb_blob_get_data (head, NULL); + const char *master_data = hb_blob_get_data (master_head, NULL); + g_assert (memcmp (data, master_data, length) == 0); + + hb_blob_destroy (head); +} + +static void +test_create_from_file_using (const char *loader) +{ + hb_face_t *face = hb_face_create_from_file_or_fail_using (font_file, 0, loader); + test_face (face); + hb_face_destroy (face); +} + +static void +test_create_from_file (void) +{ + test_create_from_file_using (NULL); + const char **loaders = hb_face_list_loaders (); + for (unsigned i = 0; loaders[i]; i++) + test_create_from_file_using (loaders[i]); +} + +static void +test_create_from_blob_using (const char *loader) +{ + hb_blob_t *blob = hb_blob_create_from_file_or_fail (font_file); + hb_face_t *face = hb_face_create_or_fail_using (blob, 0, loader); + hb_blob_destroy (blob); + test_face (face); + hb_face_destroy (face); +} + +static void +test_create_from_blob (void) +{ + test_create_from_file_using (NULL); + const char **loaders = hb_face_list_loaders (); + for (unsigned i = 0; loaders[i]; i++) + test_create_from_blob_using (loaders[i]); +} + +int +main (int argc, char **argv) +{ + hb_test_init (&argc, &argv); + + font_file = hb_test_resolve_path (FONT_FILE); + master_face = hb_test_open_font_file (FONT_FILE); + master_head = hb_face_reference_table (master_face, HEAD_TAG); + g_assert (hb_blob_get_length (master_head) > 0); + + hb_test_add (test_create_from_file); + hb_test_add (test_create_from_blob); + + int ret = hb_test_run(); + + hb_blob_destroy (master_head); + hb_face_destroy (master_face); + g_free ((char *) font_file); + + return ret; +} From 5e6da541665822fba4137c5bfcaabe6a60fdca4f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 11 Mar 2025 12:00:18 -0600 Subject: [PATCH 8/9] [test] Minor, accept absolute font paths --- test/api/hb-test.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/api/hb-test.h b/test/api/hb-test.h index 82006364d..537a2df82 100644 --- a/test/api/hb-test.h +++ b/test/api/hb-test.h @@ -309,10 +309,10 @@ static inline char * hb_test_resolve_path (const char *path) { #if GLIB_CHECK_VERSION(2,37,2) - return g_test_build_filename (G_TEST_DIST, path, NULL); -#else - return path = g_strdup (path); + if (path[0] != '/') + return g_test_build_filename (G_TEST_DIST, path, NULL); #endif + return g_strdup (path); } static inline hb_face_t * From 759e1881e81a5ceb7280ddffe5ddd1fd6d4da484 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 11 Mar 2025 12:12:08 -0600 Subject: [PATCH 9/9] [coretext] Fix loading TTC files --- src/hb-coretext-shape.cc | 53 ++++++++++++++++++-------------- test/api/hb-test.h | 20 +++++++++---- test/api/test-face.c | 65 +++++++++++++++++++--------------------- 3 files changed, 76 insertions(+), 62 deletions(-) diff --git a/src/hb-coretext-shape.cc b/src/hb-coretext-shape.cc index 33296c77c..58a1fb28e 100644 --- a/src/hb-coretext-shape.cc +++ b/src/hb-coretext-shape.cc @@ -160,18 +160,44 @@ release_data (void *info, const void *data, size_t size) hb_blob_destroy ((hb_blob_t *) info); } +static CGFontRef +create_cg_font (CFArrayRef ct_font_desc_array, unsigned int index) +{ + auto ct_font_desc = (CFArrayGetCount (ct_font_desc_array) > index) ? + (CTFontDescriptorRef) CFArrayGetValueAtIndex (ct_font_desc_array, index) : nullptr; + if (unlikely (!ct_font_desc)) + { + CFRelease (ct_font_desc_array); + return nullptr; + } + auto ct_font = ct_font_desc ? CTFontCreateWithFontDescriptor (ct_font_desc, 0, nullptr) : nullptr; + CFRelease (ct_font_desc_array); + if (unlikely (!ct_font)) + return nullptr; + + auto cg_font = ct_font ? CTFontCopyGraphicsFont (ct_font, nullptr) : nullptr; + CFRelease (ct_font); + + return cg_font; +} + static CGFontRef create_cg_font (hb_blob_t *blob, unsigned int index) { - if (unlikely (index != 0)) - return nullptr; // How? - hb_blob_make_immutable (blob); unsigned int blob_length; const char *blob_data = hb_blob_get_data (blob, &blob_length); if (unlikely (!blob_length)) DEBUG_MSG (CORETEXT, blob, "Empty blob"); + if (unlikely (index != 0)) + { + auto ct_font_desc_array = CTFontManagerCreateFontDescriptorsFromData (CFDataCreate (kCFAllocatorDefault, (const UInt8 *) blob_data, blob_length)); + if (unlikely (!ct_font_desc_array)) + return nullptr; + return create_cg_font (ct_font_desc_array, index); + } + hb_blob_reference (blob); CGDataProviderRef provider = CGDataProviderCreateWithData (blob, blob_data, blob_length, &release_data); CGFontRef cg_font = nullptr; @@ -389,24 +415,9 @@ hb_coretext_face_create_from_file_or_fail (const char *file_name, CFRelease (url); return nullptr; } - auto ct_font_desc = (CFArrayGetCount (ct_font_desc_array) > index) ? - (CTFontDescriptorRef) CFArrayGetValueAtIndex (ct_font_desc_array, index) : nullptr; - if (unlikely (!ct_font_desc)) - { - CFRelease (ct_font_desc_array); - CFRelease (url); - return nullptr; - } - CFRelease (url); - auto ct_font = ct_font_desc ? CTFontCreateWithFontDescriptor (ct_font_desc, 0, nullptr) : nullptr; - CFRelease (ct_font_desc_array); - if (unlikely (!ct_font)) - return nullptr; - auto cg_font = ct_font ? CTFontCopyGraphicsFont (ct_font, nullptr) : nullptr; - CFRelease (ct_font); - if (unlikely (!cg_font)) - return nullptr; + auto cg_font = create_cg_font (ct_font_desc_array, index); + CFRelease (url); hb_face_t *face = hb_coretext_face_create (cg_font); CFRelease (cg_font); @@ -427,8 +438,6 @@ hb_coretext_face_create_from_file_or_fail (const char *file_name, * This is similar in functionality to hb_face_create_from_blob_or_fail(), * but uses the CoreText library for loading the font data. * - * Note that currently only @index equal to 0 is supported. - * * Return value: (transfer full): The new face object, or `NULL` if * no face is found at the specified index or the blob cannot be read. * diff --git a/test/api/hb-test.h b/test/api/hb-test.h index 537a2df82..648f452e4 100644 --- a/test/api/hb-test.h +++ b/test/api/hb-test.h @@ -241,9 +241,14 @@ hb_test_add_data_func_flavor (const char *test_path, gconstpointer test_data, hb_test_data_func_t test_func) { - char *path = g_strdup_printf ("%s/%s", test_path, flavor); - hb_test_add_data_func (path, test_data, test_func); - g_free (path); + if (flavor && *flavor) + { + char *path = g_strdup_printf ("%s/%s", test_path, flavor); + hb_test_add_data_func (path, test_data, test_func); + g_free (path); + } + else + hb_test_add_data_func (test_path, test_data, test_func); } #define hb_test_add_data_flavor(UserData, Flavor, Func) hb_test_add_data_func_flavor (#Func, Flavor, UserData, Func) @@ -316,7 +321,7 @@ hb_test_resolve_path (const char *path) } static inline hb_face_t * -hb_test_open_font_file (const char *font_path) +hb_test_open_font_file_with_index (const char *font_path, unsigned face_index) { char *path = hb_test_resolve_path (font_path); @@ -325,13 +330,18 @@ hb_test_open_font_file (const char *font_path) if (!blob) g_error ("Font %s not found.", path); - face = hb_face_create (blob, 0); + face = hb_face_create (blob, face_index); hb_blob_destroy (blob); g_free (path); return face; } +static inline hb_face_t * +hb_test_open_font_file (const char *font_path) +{ + return hb_test_open_font_file_with_index (font_path, 0); +} HB_END_DECLS diff --git a/test/api/test-face.c b/test/api/test-face.c index 7f9cda165..9d45eca29 100644 --- a/test/api/test-face.c +++ b/test/api/test-face.c @@ -27,7 +27,8 @@ /* Unit tests for hb-face.h */ #define FONT_FILE "fonts/Roboto-Regular.ac.ttf" -static const char *font_file; +static const char *font_file = NULL; +static unsigned int face_index = 0; hb_face_t *master_face = NULL; @@ -37,69 +38,63 @@ hb_blob_t *master_head = NULL; static void test_face (hb_face_t *face) { - g_assert (face); + g_assert_nonnull (face); hb_blob_t *head = hb_face_reference_table (face, HEAD_TAG); - unsigned int length = hb_blob_get_length (head); - unsigned int master_length = hb_blob_get_length (master_head); - g_assert_cmpuint (length, ==, master_length); - - const char *data = hb_blob_get_data (head, NULL); - const char *master_data = hb_blob_get_data (master_head, NULL); - g_assert (memcmp (data, master_data, length) == 0); + unsigned int length; + unsigned int master_length; + const char *data = hb_blob_get_data (head, &length); + const char *master_data = hb_blob_get_data (master_head, &master_length); + g_assert_cmpmem (data, length, master_data, master_length); hb_blob_destroy (head); } static void -test_create_from_file_using (const char *loader) +test_create_from_file_using (const void *user_data) { - hb_face_t *face = hb_face_create_from_file_or_fail_using (font_file, 0, loader); + const char *loader = user_data; + hb_face_t *face = hb_face_create_from_file_or_fail_using (font_file, face_index, loader); test_face (face); hb_face_destroy (face); } static void -test_create_from_file (void) -{ - test_create_from_file_using (NULL); - const char **loaders = hb_face_list_loaders (); - for (unsigned i = 0; loaders[i]; i++) - test_create_from_file_using (loaders[i]); -} - -static void -test_create_from_blob_using (const char *loader) +test_create_from_blob_using (const void *user_data) { + const char *loader = user_data; hb_blob_t *blob = hb_blob_create_from_file_or_fail (font_file); - hb_face_t *face = hb_face_create_or_fail_using (blob, 0, loader); + hb_face_t *face = hb_face_create_or_fail_using (blob, face_index, loader); hb_blob_destroy (blob); test_face (face); hb_face_destroy (face); } -static void -test_create_from_blob (void) -{ - test_create_from_file_using (NULL); - const char **loaders = hb_face_list_loaders (); - for (unsigned i = 0; loaders[i]; i++) - test_create_from_blob_using (loaders[i]); -} - int main (int argc, char **argv) { hb_test_init (&argc, &argv); - font_file = hb_test_resolve_path (FONT_FILE); - master_face = hb_test_open_font_file (FONT_FILE); + font_file = FONT_FILE; + if (argc > 1) + font_file = argv[1]; + if (argc > 2) + face_index = atoi (argv[2]); + + master_face = hb_test_open_font_file_with_index (font_file, face_index); master_head = hb_face_reference_table (master_face, HEAD_TAG); g_assert (hb_blob_get_length (master_head) > 0); - hb_test_add (test_create_from_file); - hb_test_add (test_create_from_blob); + font_file = hb_test_resolve_path (font_file); + + hb_test_add_flavor ("", test_create_from_file_using); + hb_test_add_flavor ("", test_create_from_blob_using); + for (const char **loaders = hb_face_list_loaders (); *loaders; loaders++) + { + hb_test_add_flavor (*loaders, test_create_from_file_using); + hb_test_add_flavor (*loaders, test_create_from_blob_using); + } int ret = hb_test_run();