Merge branch 'matthiasc/for-main' into 'main'

fontchooser: Use new hb api

See merge request GNOME/gtk!4323
This commit is contained in:
Matthias Clasen 2022-01-03 02:52:45 +00:00
commit edd57004d3
2 changed files with 87 additions and 38 deletions

View File

@ -433,6 +433,8 @@ gtk_font_button_font_chooser_set_font_map (GtkFontChooser *chooser,
context = gtk_widget_get_pango_context (font_button->font_label);
pango_context_set_font_map (context, font_map);
if (font_button->font_dialog)
gtk_font_chooser_set_font_map (GTK_FONT_CHOOSER (font_button->font_dialog), font_map);
}
}

View File

@ -1470,26 +1470,23 @@ add_font_variations (GtkFontChooserWidget *fontchooser,
{
GHashTableIter iter;
Axis *axis;
const char *sep = "";
char buf[G_ASCII_DTOSTR_BUF_SIZE];
g_hash_table_iter_init (&iter, fontchooser->axes);
while (g_hash_table_iter_next (&iter, (gpointer *)NULL, (gpointer *)&axis))
{
char tag[5];
double value;
char buf[128];
value = gtk_adjustment_get_value (axis->adjustment);
if (value == axis->default_value)
continue;
tag[0] = (axis->tag >> 24) & 0xff;
tag[1] = (axis->tag >> 16) & 0xff;
tag[2] = (axis->tag >> 8) & 0xff;
tag[3] = (axis->tag >> 0) & 0xff;
tag[4] = '\0';
g_string_append_printf (s, "%s%s=%s", sep, tag, g_ascii_dtostr (buf, sizeof(buf), value));
sep = ",";
hb_variation_to_string (&(hb_variation_t) { axis->tag, value }, buf, sizeof (buf));
if (s->len > 0)
g_string_append_c (s, ',');
g_string_append (s, buf);
}
}
@ -1518,10 +1515,7 @@ adjustment_changed (GtkAdjustment *adjustment,
static gboolean
should_show_axis (hb_ot_var_axis_info_t *ax)
{
if (ax->flags & HB_OT_VAR_AXIS_FLAG_HIDDEN)
return FALSE;
return TRUE;
return (ax->flags & HB_OT_VAR_AXIS_FLAG_HIDDEN) == 0;
}
static gboolean
@ -1535,11 +1529,11 @@ static struct {
guint32 tag;
const char *name;
} axis_names[] = {
{ HB_OT_TAG_VAR_AXIS_WIDTH, N_("Width") },
{ HB_OT_TAG_VAR_AXIS_WEIGHT, N_("Weight") },
{ HB_OT_TAG_VAR_AXIS_ITALIC, N_("Italic") },
{ HB_OT_TAG_VAR_AXIS_SLANT, N_("Slant") },
{ HB_OT_TAG_VAR_AXIS_OPTICAL_SIZE, N_("Optical Size") },
{ HB_OT_TAG_VAR_AXIS_WIDTH, NC_("Font variation axis", "Width") },
{ HB_OT_TAG_VAR_AXIS_WEIGHT, NC_("Font variation axis", "Weight") },
{ HB_OT_TAG_VAR_AXIS_ITALIC, NC_("Font variation axis", "Italic") },
{ HB_OT_TAG_VAR_AXIS_SLANT, NC_("Font variation axis", "Slant") },
{ HB_OT_TAG_VAR_AXIS_OPTICAL_SIZE, NC_("Font variation axis", "Optical Size") },
};
static gboolean
@ -1570,7 +1564,7 @@ add_axis (GtkFontChooserWidget *fontchooser,
{
if (axis_names[i].tag == ax->tag)
{
name = _(axis_names[i].name);
name = g_dpgettext2 (NULL, "Font variation axis", axis_names[i].name);
break;
}
}
@ -1612,6 +1606,33 @@ add_axis (GtkFontChooserWidget *fontchooser,
return TRUE;
}
#if HB_VERSION_ATLEAST (3, 3, 0)
static void
get_axes_and_values (hb_font_t *font,
unsigned int n_axes,
hb_ot_var_axis_info_t *axes,
float *coords)
{
const float *dcoords;
unsigned int length = n_axes;
hb_ot_var_get_axis_infos (hb_font_get_face (font), 0, &length, axes);
dcoords = hb_font_get_var_coords_design (font, &length);
if (dcoords)
memcpy (coords, dcoords, sizeof (float) * length);
else
{
for (int i = 0; i < n_axes; i++)
{
hb_ot_var_axis_info_t *axis = &axes[i];
coords[axis->axis_index] = axis->default_value;
}
}
}
#else
/* FIXME: This doesn't work if the font has an avar table */
static float
denorm_coord (hb_ot_var_axis_info_t *axis, int coord)
@ -1624,16 +1645,40 @@ denorm_coord (hb_ot_var_axis_info_t *axis, int coord)
return axis->default_value + r * (axis->max_value - axis->default_value);
}
static void
get_axes_and_values (hb_font_t *font,
unsigned int n_axes,
hb_ot_var_axis_info_t *axes,
float *coords)
{
const int *ncoords;
unsigned int length = n_axes;
hb_ot_var_get_axis_infos (hb_font_get_face (font), 0, &length, axes);
ncoords = hb_font_get_var_coords_normalized (font, &length);
for (int i = 0; i < n_axes; i++)
{
hb_ot_var_axis_info_t *axis = &axes[i];
int idx = axis->axis_index;
if (ncoords)
coords[idx] = denorm_coord (axis, ncoords[idx]);
else
coords[idx] = axis->default_value;
}
}
#endif
static gboolean
gtk_font_chooser_widget_update_font_variations (GtkFontChooserWidget *fontchooser)
{
PangoFont *pango_font;
hb_font_t *hb_font;
hb_face_t *hb_face;
const int *coords;
unsigned int n_coords;
unsigned int n_axes;
hb_ot_var_axis_info_t *axes;
float *coords;
gboolean has_axis = FALSE;
int i;
@ -1654,24 +1699,17 @@ gtk_font_chooser_widget_update_font_variations (GtkFontChooserWidget *fontchoose
if (!hb_ot_var_has_data (hb_face))
return FALSE;
coords = hb_font_get_var_coords_normalized (hb_font, &n_coords);
n_axes = hb_ot_var_get_axis_count (hb_face);
axes = g_new0 (hb_ot_var_axis_info_t, n_axes);
hb_ot_var_get_axis_infos (hb_face, 0, &n_axes, axes);
axes = g_alloca (sizeof (hb_ot_var_axis_info_t) * n_axes);
coords = g_alloca (sizeof (float) * n_axes);
get_axes_and_values (hb_font, n_axes, axes, coords);
for (i = 0; i < n_axes; i++)
{
float value;
if (coords && i < n_coords)
value = denorm_coord (&axes[i], coords[i]);
else
value = axes[i].default_value;
if (add_axis (fontchooser, hb_font, &axes[i], value, i + 4))
if (add_axis (fontchooser, hb_font, &axes[i], coords[axes[i].axis_index], i + 4))
has_axis = TRUE;
}
g_free (axes);
g_object_unref (pango_font);
return has_axis;
@ -2260,6 +2298,7 @@ update_font_features (GtkFontChooserWidget *fontchooser)
{
GString *s;
GList *l;
char buf[128];
s = g_string_new ("");
@ -2275,17 +2314,24 @@ update_font_features (GtkFontChooserWidget *fontchooser)
if (gtk_check_button_get_active (GTK_CHECK_BUTTON (item->feat)) &&
strcmp (item->name, "xxxx") != 0)
{
g_string_append_printf (s, "%s\"%s\" %d", s->len > 0 ? ", " : "", item->name, 1);
hb_feature_to_string (&(hb_feature_t) { item->tag, 1, 0, -1 }, buf, sizeof (buf));
if (s->len > 0)
g_string_append_c (s, ',');
g_string_append (s, buf);
}
}
else if (GTK_IS_CHECK_BUTTON (item->feat))
{
guint32 value;
if (gtk_check_button_get_inconsistent (GTK_CHECK_BUTTON (item->feat)))
continue;
g_string_append_printf (s, "%s\"%s\" %d",
s->len > 0 ? ", " : "", item->name,
gtk_check_button_get_active (GTK_CHECK_BUTTON (item->feat)));
value = gtk_check_button_get_active (GTK_CHECK_BUTTON (item->feat));
hb_feature_to_string (&(hb_feature_t) { item->tag, value, 0, -1 }, buf, sizeof (buf));
if (s->len > 0)
g_string_append_c (s, ',');
g_string_append (s, buf);
}
}
@ -2323,7 +2369,8 @@ gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget *fontchooser
{
double font_size = (double) pango_font_description_get_size (fontchooser->font_desc) / PANGO_SCALE;
/* XXX: This clamps, which can cause it to reloop into here, do we need
* to block its signal handler? */
* to block its signal handler?
*/
gtk_range_set_value (GTK_RANGE (fontchooser->size_slider), font_size);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (fontchooser->size_spin), font_size);
}