gtk: cache font face descriptions while sorting

This commit is contained in:
Daemonratte 2026-05-15 18:27:36 +02:00
commit b74c87f355

View file

@ -952,18 +952,26 @@ compare_font_descriptions (const PangoFontDescription *a, const PangoFontDescrip
return 0;
}
static int
faces_sort_func (const void *a, const void *b)
typedef struct
{
PangoFontDescription *desc_a = pango_font_face_describe (*(PangoFontFace **)a);
PangoFontDescription *desc_b = pango_font_face_describe (*(PangoFontFace **)b);
PangoFontFace *face;
PangoFontDescription *desc;
const gchar *name;
} FontFaceSortInfo;
int ord = compare_font_descriptions (desc_a, desc_b);
static int
faces_sort_func (const void *a,
const void *b)
{
const FontFaceSortInfo *face_a = a;
const FontFaceSortInfo *face_b = b;
gint result;
pango_font_description_free (desc_a);
pango_font_description_free (desc_b);
result = compare_font_descriptions (face_a->desc, face_b->desc);
if (result != 0)
return result;
return ord;
return g_strcmp0 (face_a->name, face_b->name);
}
static gboolean
@ -983,6 +991,7 @@ gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
{
gint n_faces, i;
PangoFontFace **faces;
FontFaceSortInfo *sorted_faces;
PangoFontDescription *old_desc;
GtkListStore *model;
GtkTreeIter match_row;
@ -996,37 +1005,40 @@ gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
old_desc= NULL;
pango_font_family_list_faces (fontsel->family, &faces, &n_faces);
qsort (faces, n_faces, sizeof (PangoFontFace *), faces_sort_func);
sorted_faces = g_new (FontFaceSortInfo, n_faces);
for (i = 0; i < n_faces; i++)
{
sorted_faces[i].face = faces[i];
sorted_faces[i].desc = pango_font_face_describe (faces[i]);
sorted_faces[i].name = pango_font_face_get_face_name (faces[i]);
}
qsort (sorted_faces, n_faces, sizeof (FontFaceSortInfo), faces_sort_func);
gtk_list_store_clear (model);
for (i=0; i < n_faces; i++)
for (i = 0; i < n_faces; i++)
{
GtkTreeIter iter;
const gchar *str = pango_font_face_get_face_name (faces[i]);
const gchar *str = sorted_faces[i].name;
gtk_list_store_insert_with_values (model, &iter, -1,
FACE_COLUMN, faces[i],
FACE_COLUMN, sorted_faces[i].face,
FACE_NAME_COLUMN, _(str),
-1);
if (i == 0)
{
match_row = iter;
match_face = faces[i];
}
else if (old_desc)
{
PangoFontDescription *tmp_desc = pango_font_face_describe (faces[i]);
if (font_description_style_equal (tmp_desc, old_desc))
{
match_row = iter;
match_face = faces[i];
}
pango_font_description_free (tmp_desc);
}
{
match_row = iter;
match_face = sorted_faces[i].face;
}
else if (old_desc &&
font_description_style_equal (sorted_faces[i].desc, old_desc))
{
match_row = iter;
match_face = sorted_faces[i].face;
}
}
if (old_desc)
@ -1043,6 +1055,10 @@ gtk_font_selection_show_available_styles (GtkFontSelection *fontsel)
set_cursor_to_iter (GTK_TREE_VIEW (fontsel->face_list), &match_row);
}
for (i = 0; i < n_faces; i++)
pango_font_description_free (sorted_faces[i].desc);
g_free (sorted_faces);
g_free (faces);
}