From 2d42fc9fbb9ddb7c9bd53b06a7ef70ef6f459399 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 2 Jan 2022 07:45:10 -0700 Subject: [PATCH] [font] Load named-instance if face index top bits are set This matches FreeType behavior. Fixes https://github.com/harfbuzz/harfbuzz/issues/3348 --- src/hb-face.cc | 4 ++++ src/hb-font.cc | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/hb-face.cc b/src/hb-face.cc index 2f6f29121..536559863 100644 --- a/src/hb-face.cc +++ b/src/hb-face.cc @@ -205,6 +205,10 @@ _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void * is ignored. Otherwise, only the lower 16-bits of @index are used. * The unmodified @index can be accessed via hb_face_get_index(). * + * Note: The high 16-bits of @index, if non-zero, are used by + * hb_font_create() to load named-instances in variable fonts. See + * hb_font_create() for details. + * * Return value: (transfer full): The new face object * * Since: 0.9.2 diff --git a/src/hb-font.cc b/src/hb-font.cc index fa8da9639..d1cb3491e 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -1521,6 +1521,13 @@ _hb_font_create (hb_face_t *face) * * Constructs a new font object from the specified face. * + * Note: If @face's index value (as passed to hb_face_create() + * has non-zero top 16-bits, those bits minus one are passed to + * hb_font_set_var_named_instance(), effectively loading a named-instance + * of a variable font, instead of the default-instance. This allows + * specifying which named-instance to load by default when creating the + * face. + * * Return value: (transfer full): The new font object * * Since: 0.9.2 @@ -1535,6 +1542,9 @@ hb_font_create (hb_face_t *face) hb_ot_font_set_funcs (font); #endif + if (face && face->index >> 16) + hb_font_set_var_named_instance (font, (face->index >> 16) - 1); + return font; }