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){

  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");
                                  level->font_size * PANGO_SCALE);
  pango_layout_set_font_description(layout, desc);

  pango_cairo_update_layout(cr, layout);
  pango_cairo_show_layout(cr, 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:

               "gtk-font-name", &font_name,
  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.