Re: width of string rendered by pango font?



On Wed, 2005-07-13 at 00:40 -0400, Allin Cottrell wrote:
On Mon, 11 Jul 2005, David Morse wrote:

How does one figure out the width of a certain string in a certain 
pango font? (Other than abandoning pango fonts and reverting to 
gdkfonts, of course. ; )

It's a bit of a contortion.

gint get_string_width (const gchar *str)
{
     gint width;
     GtkWidget *w;
     PangoLayout *pl;
     PangoContext *pc;

     w = gtk_label_new(NULL);
     pc = gtk_widget_get_pango_context(w);

     pl = pango_layout_new(pc);
     pango_layout_set_text(pl, str, -1);
     pango_layout_get_pixel_size(pl, &width, NULL);

     gtk_widget_destroy(w);
     g_object_unref(G_OBJECT(pl));

     return width;

Well, 
 
 PangoLayout  *gtk_widget_create_pango_layout  (GtkWidget   *widget,
                                               const gchar *text);

Would simplify the above. And considering that asking about the
size of a string before you have a widget to draw it on is meaningless,
you wouldn't have the creation of the dummy label either.

(Finding the size of a string is documented in 
http://developer.gnome.org/doc/API/2.0/gtk/gtk-question-index.html
BTW)

Regards,
                                                Owen



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