Best way to draw text for a widget in Gtk+-2.0

Hi all,

How to obtain font information from a GtkWidget?

Currently, I do the following:

  cairo_t *cr;
  PangoLayout *layout;
  PangoFontDescription *desc;

  gchar *str;

  cr = gdk_cairo_create(GTK_WIDGET(level)->window);

  if(cr == NULL){
    return;
  }

  str = g_strdup_printf("%u [Hz]", level->samplerate);

  layout = pango_cairo_create_layout(cr);
  pango_layout_set_text(layout, str, -1);
  desc = pango_font_description_from_string("Sans Slant");
  pango_font_description_set_size(desc,
                                  level->font_size * PANGO_SCALE);
  pango_layout_set_font_description(layout, desc);
  pango_font_description_free(desc);

  pango_cairo_update_layout(cr, layout);
  pango_cairo_show_layout(cr, layout);

  g_object_unref(layout);

What is actually quiet bad because themed font isn’t applied. Would the correct way be to use GtkSettings?

In order to change to following:

  g_object_get(gtk_settings_get_default(),
               "gtk-font-name", &font_name,
               NULL);
  desc = pango_font_description_from_string(font_name);

But what about GtkWidget::size-request() and GtkWidget::size-allocate(), you can’t create a cairo context until the widget got GtkWidget::realized()

Is there a different way to calculate text extents than using cairo context?

cairo_text_extents (cr, str, &te);


by Joël

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