From 04be808886398dca8322d20a67340e33aa670a16 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 8 Feb 2025 01:29:14 +0000 Subject: [PATCH] [font/var] Always allocate coords vector the length of fvar axes In prep for black magic. --- src/hb-font.cc | 30 ++++++++++++++++++++++-------- src/hb-ot-var.cc | 10 +++++++--- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/hb-font.cc b/src/hb-font.cc index 96b79ecca..e6778bf85 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -2723,7 +2723,6 @@ hb_font_set_variation (hb_font_t *font, hb_ot_var_normalize_coords (font->face, coords_length, design_coords, normalized); _hb_font_adopt_var_coords (font, normalized, design_coords, coords_length); - } /** @@ -2744,13 +2743,19 @@ hb_font_set_variation (hb_font_t *font, void hb_font_set_var_coords_design (hb_font_t *font, const float *coords, - unsigned int coords_length) + unsigned int input_coords_length) { if (hb_object_is_immutable (font)) return; font->serial_coords = ++font->serial; + const OT::fvar &fvar = *font->face->table.fvar; + auto axes = fvar.get_axes (); + const unsigned coords_length = axes.length; + + input_coords_length = hb_min (input_coords_length, coords_length); + int *normalized = coords_length ? (int *) hb_calloc (coords_length, sizeof (int)) : nullptr; float *design_coords = coords_length ? (float *) hb_calloc (coords_length, sizeof (float)) : nullptr; @@ -2761,8 +2766,11 @@ hb_font_set_var_coords_design (hb_font_t *font, return; } - if (coords_length) - hb_memcpy (design_coords, coords, coords_length * sizeof (font->design_coords[0])); + if (input_coords_length) + hb_memcpy (design_coords, coords, input_coords_length * sizeof (font->design_coords[0])); + // Fill in the rest with default values + for (unsigned int i = input_coords_length; i < coords_length; i++) + design_coords[i] = axes[i].get_default (); hb_ot_var_normalize_coords (font->face, coords_length, coords, normalized); _hb_font_adopt_var_coords (font, normalized, design_coords, coords_length); @@ -2829,11 +2837,17 @@ hb_font_get_var_named_instance (hb_font_t *font) void hb_font_set_var_coords_normalized (hb_font_t *font, const int *coords, /* 2.14 normalized */ - unsigned int coords_length) + unsigned int input_coords_length) { if (hb_object_is_immutable (font)) return; + const OT::fvar &fvar = *font->face->table.fvar; + auto axes = fvar.get_axes (); + unsigned coords_length = axes.length; + + input_coords_length = hb_min (input_coords_length, coords_length); + font->serial_coords = ++font->serial; int *copy = coords_length ? (int *) hb_calloc (coords_length, sizeof (coords[0])) : nullptr; @@ -2848,10 +2862,10 @@ hb_font_set_var_coords_normalized (hb_font_t *font, return; } - if (coords_length) + if (input_coords_length) { - hb_memcpy (copy, coords, coords_length * sizeof (coords[0])); - hb_memcpy (unmapped, coords, coords_length * sizeof (coords[0])); + hb_memcpy (copy, coords, input_coords_length * sizeof (coords[0])); + hb_memcpy (unmapped, coords, input_coords_length * sizeof (coords[0])); } /* Best effort design coords simulation */ diff --git a/src/hb-ot-var.cc b/src/hb-ot-var.cc index f000f2726..6d696eadc 100644 --- a/src/hb-ot-var.cc +++ b/src/hb-ot-var.cc @@ -70,7 +70,7 @@ hb_ot_var_has_data (hb_face_t *face) * hb_ot_var_get_axis_count: * @face: The #hb_face_t to work on * - * Fetches the number of OpenType variation axes included in the face. + * Fetches the number of OpenType variation axes included in the face. * * Return value: the number of variation axes defined * @@ -183,7 +183,7 @@ hb_ot_var_find_axis_info (hb_face_t *face, * hb_ot_var_get_named_instance_count: * @face: The #hb_face_t to work on * - * Fetches the number of named instances included in the face. + * Fetches the number of named instances included in the face. * * Return value: the number of named instances defined * @@ -263,7 +263,7 @@ hb_ot_var_named_instance_get_design_coords (hb_face_t *face, * @face: The #hb_face_t to work on * @variations: The array of variations to normalize * @variations_length: The number of variations to normalize - * @coords: (out) (array length=coords_length): The array of normalized coordinates + * @coords: (out) (array length=coords_length): The array of normalized coordinates * @coords_length: The length of the coordinate array * * Normalizes all of the coordinates in the given list of variation axes. @@ -309,6 +309,10 @@ hb_ot_var_normalize_variations (hb_face_t *face, * Any additional scaling defined in the face's `avar` table is also * applied, as described at https://docs.microsoft.com/en-us/typography/opentype/spec/avar * + * Note: @coords_length must be the same as the number of axes in the face, as + * for example returned by hb_ot_var_get_axis_count(). + * Otherwise, the behavior is undefined. + * * Since: 1.4.2 **/ void