[font/var] Always allocate coords vector the length of fvar axes

In prep for black magic.
This commit is contained in:
Behdad Esfahbod committed 2025-03-02 16:59:23 -07:00
1 parent 74b0455e34
commit 04be808886
2 files changed
+29 -11

No files matched your search

+22 -8
View File
@@ -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 */
+7 -3
View File
@@ -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