Re: What replaces gdk_text_extents() ?

On 2003.08.15 07:31, Peter Krueger wrote:
I'm wondering what functions I should use in GTK 2.2.2 to calculate
the size of some text.

I just did this using a PangoFontMetric.

Here's what I did:

PangoLayout *layout;
PangoContext *context;
PangoFontMetrics *metrics;
gchar *text_string;
gint textx, texty, char_width, string_width;

  layout = gtk_widget_create_pango_layout(widget, NULL);

  text_string = g_strdup_printf(some_text);

  pango_layout_set_text(layout, text_string, -1);

  /* want this text in the center of x-dimension and
     7/8 down the y dimension of the widget */
  textx = widget->allocation.width / 2;
  texty = 7 * widget->allocation.height / 8;

/* find out how wide the characters are to center on textx */ context = gtk_widget_get_pango_context(widget);
  metrics = pango_context_get_metrics(context,
  char_width = pango_font_metrics_get_approximate_char_width (metrics);
  string_width = strlen(text_string) * PANGO_PIXELS(char_width);

  gtk_paint_layout(widget->style, widget->window,
      GTK_WIDGET_STATE(widget), FALSE,
      NULL, widget, "widget_name", textx - (string_width/2), texty,

I don't know if this is a good way to do this or not, but it worked for me.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]