[Solved?] How to convert from Gtk::FontButton to Cairo::Context::select_font_face / set_font_size?

I can pick fonts easily with Gtk::FontButton, but fonts are returned as a string (glib::ustring).

How can this font be used, then, in a Cairo::Context ? It does not have any function to provide this string. Instead, it wants independent parameters for font name, style and size.

I tried looking for this as best as possible, but I can’t find any method to convert from one format to the other.

I have the feeling this would help but it seems broken:


Nope, I can’t seem to use pango for this, I Can’t get the font size reliably from FontDescription… how is this done properly? Nothing in these APIs makes any sense.

The documentation in fontdescription.h in pangomm is very confusing, the one in pygtk is different and better, I had to divide the size by Pango::SCALE… so not quite sure if it’s right. It works, though.

GtkFontButton implements the GtkFontChooser interface, which contains a GtkFontChooser::font-desc property. But note that font descriptions, by nature, don’t contain every aspect of a font. See pango_font_description_get_set_fields() for the bit-mask of what has been set.

Some of that data can’t be “resolved” until the font is used in context, so that a font map can be applied as well as other display specific conditions.

To use the PangoFontDescription with cairo, you can use a PangoLayout. If you are are rendering to a GTK widget, gtk_widget_create_pango_layout() will get you one. If you are using cairo only, pango_cairo_create_layout() can be used with your cairo_t.

A quick example:

PangoFontDescription *font_desc = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (button));
PangoLayout *l = gtk_widget_create_pango_layout (GTK_WIDGET (self));
pango_layout_set_font_description (l, font_desc);
pango_layout_set_text (l, "Foo", -1);
pango_cairo_show_layout (cr, l);
g_object_unref (l);
g_object_unref (font_desc);

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