Re: determining character width



On Thu, 11 Jul 2002, Evan Martin wrote:

On Thu, Jul 11, 2002 at 03:12:18PM -0400, Allin Cottrell wrote:
I want to set the width of a text window in characters rather than
fixing it in pixels.  The idea is to supply the function that creates
the window with a width parameter ("hsize") in characters, then the
function computes on the fly the corresponding width in pixels.

pango_layout_get_pixel_size(pl, &width, NULL);

Thank you, this is certainly heading in the right direction, but I'd
really like to get the fixed width of a single monospaced glyph as a
unit (so as to be able to multiply up by the desired with in
characters).  Comparing gtk-1.2 and gtk-2.0,

    gdk_char_width(fixed_font, 'X');

returns 7, while

    pango_layout_set_text(pl, "X", 1);
    pango_layout_get_pixel_size(pl, &width, NULL);

returns 9 (with the same font, Lucida Typewriter, in both cases).  If
I multiply up 9 times the max number of characters on a row (e.g. 78),
I get too wide a window.

Here's my current attempt:

gint get_char_width (GtkWidget *widget, PangoFontDescription *pfd)
{
    PangoFont *pf;
    PangoFontMetrics *pfm;
    int width;

    pf = pango_context_load_font(gtk_widget_get_pango_context(widget),
                                 pfd);
    pfm = pango_font_get_metrics(pf, NULL);
    width = pango_font_metrics_get_approximate_char_width(pfm);
    pango_font_metrics_unref(pfm);

    g_object_unref(G_OBJECT(pf));

    return PANGO_PIXELS(width);
}

It returns 8.  ;-)

Allin Cottrell.







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