Re: width of string rendered by pango font?



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;
}

Allin Cottrell



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