[coretext] Fix loading named instances
Needs tests...
TTC indices > 0 can't be loaded with CoreText API it seems.
Fixes https://github.com/harfbuzz/harfbuzz/issues/5158
diff --git a/src/hb-coretext.cc b/src/hb-coretext.cc
index deb3878..0280528 100644
--- a/src/hb-coretext.cc
+++ b/src/hb-coretext.cc
@@ -158,10 +158,17 @@
}
CGFontRef
-create_cg_font (CFArrayRef ct_font_desc_array, unsigned int index)
+create_cg_font (CFArrayRef ct_font_desc_array, unsigned int named_instance_index)
{
- auto ct_font_desc = (CFArrayGetCount (ct_font_desc_array) > index) ?
- (CTFontDescriptorRef) CFArrayGetValueAtIndex (ct_font_desc_array, index) : nullptr;
+ if (named_instance_index == 0)
+ {
+ // Default instance. We don't know which one is it. Return the first one.
+ // We will set the correct variations on it later.
+ }
+ else
+ named_instance_index--;
+ auto ct_font_desc = (CFArrayGetCount (ct_font_desc_array) > named_instance_index) ?
+ (CTFontDescriptorRef) CFArrayGetValueAtIndex (ct_font_desc_array, named_instance_index) : nullptr;
if (unlikely (!ct_font_desc))
{
CFRelease (ct_font_desc_array);
@@ -187,12 +194,21 @@
if (unlikely (!blob_length))
DEBUG_MSG (CORETEXT, blob, "Empty blob");
- if (unlikely (index != 0))
+ unsigned ttc_index = index & 0xFFFF;
+ unsigned named_instance_index = index >> 16;
+
+ if (ttc_index != 0)
+ {
+ DEBUG_MSG (CORETEXT, blob, "TTC index %d not supported", ttc_index);
+ return nullptr; // CoreText does not support TTCs
+ }
+
+ if (unlikely (named_instance_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);
+ return create_cg_font (ct_font_desc_array, named_instance_index);
}
hb_blob_reference (blob);
@@ -393,7 +409,16 @@
return nullptr;
}
- auto cg_font = create_cg_font (ct_font_desc_array, index);
+ unsigned ttc_index = index & 0xFFFF;
+ unsigned named_instance_index = index >> 16;
+
+ if (ttc_index != 0)
+ {
+ DEBUG_MSG (CORETEXT, nullptr, "TTC index %d not supported", ttc_index);
+ return nullptr; // CoreText does not support TTCs
+ }
+
+ auto cg_font = create_cg_font (ct_font_desc_array, named_instance_index);
CFRelease (url);
hb_face_t *face = hb_coretext_face_create (cg_font);
@@ -401,6 +426,8 @@
if (unlikely (hb_face_is_immutable (face)))
return nullptr;
+ hb_face_set_index (face, index);
+
return face;
}
@@ -433,6 +460,8 @@
if (unlikely (hb_face_is_immutable (face)))
return nullptr;
+ hb_face_set_index (face, index);
+
return face;
}
diff --git a/util/font-options.hh b/util/font-options.hh
index bacd7bc..8c7ce1c 100644
--- a/util/font-options.hh
+++ b/util/font-options.hh
@@ -101,7 +101,8 @@
hb_font_set_scale (font, scale_x, scale_y);
#ifndef HB_NO_VAR
- hb_font_set_var_named_instance (font, named_instance);
+ if (named_instance != HB_FONT_NO_VAR_NAMED_INSTANCE)
+ hb_font_set_var_named_instance (font, named_instance);
hb_font_set_variations (font, variations, num_variations);
#endif