From 40a4b6ddbdc84a25f76bd4d7ff41b1322fe95b83 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Sat, 27 Jul 2019 13:33:46 +0430 Subject: [PATCH] [var] Add a new API, hb_font_set_var_named_instance --- docs/harfbuzz-sections.txt | 1 + src/hb-font.cc | 28 ++++++++++++++++++++++++++++ src/hb-font.h | 4 ++++ test/api/test-ot-extents-cff.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 99916ebe2..0bc2a2c9a 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -342,6 +342,7 @@ hb_font_set_user_data hb_font_set_variations hb_font_set_var_coords_design hb_font_set_var_coords_normalized +hb_font_set_var_named_instance hb_font_subtract_glyph_origin_for_direction hb_font_t hb_reference_table_func_t diff --git a/src/hb-font.cc b/src/hb-font.cc index 9cd501170..44953c98b 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -1861,6 +1861,7 @@ hb_font_set_variations (hb_font_t *font, normalized, coords_length); _hb_font_adopt_var_coords_normalized (font, normalized, coords_length); } + /** * hb_font_set_var_coords_design: * @@ -1881,6 +1882,33 @@ hb_font_set_var_coords_design (hb_font_t *font, hb_ot_var_normalize_coords (font->face, coords_length, coords, normalized); _hb_font_adopt_var_coords_normalized (font, normalized, coords_length); } + +/** + * hb_font_set_var_named_instance: + * @font: a font. + * @instance_index: named instance index. + * + * Sets design coords of a font from a named instance index. + * + * Since: REPLACEME + */ +void +hb_font_set_var_named_instance (hb_font_t *font, + unsigned instance_index) +{ + if (hb_object_is_immutable (font)) + return; + + unsigned int coords_length = hb_ot_var_named_instance_get_design_coords (font->face, instance_index, nullptr, nullptr); + + float *coords = coords_length ? (float *) calloc (coords_length, sizeof (float)) : nullptr; + if (unlikely (coords_length && !coords)) + return; + + hb_ot_var_named_instance_get_design_coords (font->face, instance_index, &coords_length, coords); + hb_font_set_var_coords_design (font, coords, coords_length); + free (coords); +} #endif /** diff --git a/src/hb-font.h b/src/hb-font.h index 5bd8cbbcf..e68fd1813 100644 --- a/src/hb-font.h +++ b/src/hb-font.h @@ -705,6 +705,10 @@ HB_EXTERN const int * hb_font_get_var_coords_normalized (hb_font_t *font, unsigned int *length); +HB_EXTERN void +hb_font_set_var_named_instance (hb_font_t *font, + unsigned instance_index); + HB_END_DECLS #endif /* HB_FONT_H */ diff --git a/test/api/test-ot-extents-cff.c b/test/api/test-ot-extents-cff.c index 49b87997e..6810ea457 100644 --- a/test/api/test-ot-extents-cff.c +++ b/test/api/test-ot-extents-cff.c @@ -184,6 +184,37 @@ test_extents_cff2_vsindex (void) hb_font_destroy (font); } +static void +test_extents_cff2_vsindex_named_instance (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype_vsindex.otf"); + g_assert (face); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + g_assert (font); + hb_ot_font_set_funcs (font); + + hb_font_set_var_named_instance (font, 6); // 6 (BlackMediumContrast): 900, 50 + hb_glyph_extents_t extents; + hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents); + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 12); + g_assert_cmpint (extents.y_bearing, ==, 652); + g_assert_cmpint (extents.width, ==, 653); + g_assert_cmpint (extents.height, ==, -652); + + result = hb_font_get_glyph_extents (font, 2, &extents); + g_assert (result); + + g_assert_cmpint (extents.x_bearing, ==, 6); + g_assert_cmpint (extents.y_bearing, ==, 675); + g_assert_cmpint (extents.width, ==, 647); + g_assert_cmpint (extents.height, ==, -675); + + hb_font_destroy (font); +} + int main (int argc, char **argv) { @@ -194,6 +225,7 @@ main (int argc, char **argv) hb_test_add (test_extents_cff1_seac); hb_test_add (test_extents_cff2); hb_test_add (test_extents_cff2_vsindex); + hb_test_add (test_extents_cff2_vsindex_named_instance); return hb_test_run (); }