Is font attributes available

I think we pretty much covered that. Use pango_font_face_list_sizes(), with a fallback array for when that returns NULL. Are you asking how to take an array of numbers and make a DropDown?

Hi,

Does this mean that different widgets have different context objects and so have different FontMap’s?

Thank you.

It’s possible to set a custom font map for a widget (gtk_widget_set_font_map()), but as long as that’s not done, widgets chain up to the toplevel window which uses pango_cairo_font_map_get_default(). So, they’ll normally be the same.

@chrisaw,
I’m sorry - one more question…

The page at PangoCairo.FontMap.get_default does not indicate whiich header the function is declared in.

Can you help?

Thank you.

That information is available on the main page for the namespace. Click where it says PangoCairo.

C headers pango/pangocairo.h
pkg-config files pangocairo

But if you’re using GTK, you don’t need to include that separately.

@chrisaw ,

So what I do is following:

pango_context_list_families();
const gchar *name = pango_font_family_get_name();

then those names will be added into the font name selection combo box.

I already have a FontMap pointer. And so when I select the name X, how do I get the PangoFontFace from the FontMap for a specific font name?

Because I need PangoFontFace to get the sizes array…

Thank you.

You can get the list of faces from the family (pango_font_family_list_faces()).

But in GTK4, all of these objects implement GListModel, allowing you to use the modern list model APIs. Look at how this is implemented in GTK’s font chooser:

It’s actually remarkably simple, boiling down to essentially:

PangoFontMap *fontmap = pango_cairo_font_map_get_default ();
GListModel *model = G_LIST_MODEL (gtk_flatten_list_model_new (G_LIST_MODEL (g_object_ref (fontmap))));

That gives you a GListModel of PangoFontFaces, which you can use directly as the model for a GtkDropDown. You just need to provide an expression to get the name from the face object:

expression = gtk_cclosure_expression_new (G_TYPE_STRING, NULL,
                                          0, NULL,
                                          (GCallback)get_font_name,
                                          NULL, NULL);
gtk_drop_down_set_expression (GTK_DROP_DOWN (button), expression);

Where get_font_name is something like:

static char *
get_family_name (gpointer item)
{
  return g_strconcat (pango_font_family_get_name (pango_font_face_get_family (item)),
                      " ",
                      pango_font_face_get_face_name (item),
                      NULL);

}

This is all taken from the above link and the Pickers demo in gtk4-demo, which has a font family dropdown.

@chrisaw ,

This function will give me all available faces that makes up family.

Not what I am lookijng for…

I need a solution for GTK3 still…

Thank you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.