From e4fe8bf95e4e7080abfdb005e48563b56b1c8fc6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 10 Mar 2025 20:49:14 -0600 Subject: [PATCH] [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. */