Skip to content

Commit

Permalink
[font] Remember named_instance index
Browse files Browse the repository at this point in the history
Specially, in hb_font_set_variations() default to the named_instance
for unspecified axes.

Fixes #1883
  • Loading branch information
behdad committed Jan 15, 2023
1 parent 14a83d6 commit d195e07
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/hb-font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ DEFINE_NULL_INSTANCE (hb_font_t) =
0, /* y_ppem */
0, /* ptem */

(unsigned) -1, /* instance_index */
0, /* num_coords */
nullptr, /* coords */
nullptr, /* design_coords */
Expand Down Expand Up @@ -1805,6 +1806,7 @@ _hb_font_create (hb_face_t *face)
font->x_scale = font->y_scale = face->get_upem ();
font->x_multf = font->y_multf = 1.f;
font->x_mult = font->y_mult = 1 << 16;
font->instance_index = (unsigned) -1;

return font;
}
Expand Down Expand Up @@ -2513,7 +2515,7 @@ hb_font_set_variations (hb_font_t *font,

font->serial_coords = ++font->serial;

if (!variations_length)
if (!variations_length && font->instance_index == (unsigned) -1)
{
hb_font_set_var_coords_normalized (font, nullptr, 0);
return;
Expand All @@ -2533,23 +2535,29 @@ hb_font_set_variations (hb_font_t *font,
return;
}

/* Initialize design coords to default from fvar. */
for (unsigned int i = 0; i < coords_length; i++)
design_coords[i] = axes[i].get_default ();
/* Initialize design coords. */
if (font->instance_index == (unsigned) -1)
for (unsigned int i = 0; i < coords_length; i++)
design_coords[i] = axes[i].get_default ();
else
{
unsigned count = coords_length;
hb_ot_var_named_instance_get_design_coords (font->face, font->instance_index,
&count, design_coords);
assert (count == coords_length);
}

for (unsigned int i = 0; i < variations_length; i++)
{
const auto tag = variations[i].tag;
const auto v = variations[i].value;
for (unsigned axis_index = 0; axis_index < coords_length; axis_index++)
if (axes[axis_index].axisTag == tag)
{
design_coords[axis_index] = v;
normalized[axis_index] = fvar.normalize_axis_value (axis_index, v);
}
}
font->face->table.avar->map_coords (normalized, coords_length);

hb_ot_var_normalize_coords (font->face, coords_length, design_coords, normalized);
_hb_font_adopt_var_coords (font, normalized, design_coords, coords_length);
}

Expand Down Expand Up @@ -2611,17 +2619,13 @@ hb_font_set_var_named_instance (hb_font_t *font,
if (hb_object_is_immutable (font))
return;

font->serial_coords = ++font->serial;

unsigned int coords_length = hb_ot_var_named_instance_get_design_coords (font->face, instance_index, nullptr, nullptr);

float *coords = coords_length ? (float *) hb_calloc (coords_length, sizeof (float)) : nullptr;
if (unlikely (coords_length && !coords))
if (font->instance_index == instance_index)
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);
hb_free (coords);
font->serial_coords = ++font->serial;

font->instance_index = instance_index;
hb_font_set_variations (font, nullptr, 0);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/hb-font.hh
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ struct hb_font_t
float ptem;

/* Font variation coordinates. */
unsigned int instance_index;
unsigned int num_coords;
int *coords;
float *design_coords;
Expand Down

0 comments on commit d195e07

Please sign in to comment.