[coretext] Fix loading TTC files

This commit is contained in:
Behdad Esfahbod committed 2025-03-11 12:46:27 -06:00
1 parent 5e6da54166
commit 759e1881e8
3 files changed
+76 -62

No files matched your search

+31 -22
View File
@@ -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.
*
+15 -5
View File
@@ -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
+30 -35
View File
@@ -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();