Re: determining character width



On Thu, 11 Jul 2002, Evan Martin wrote:

(re. determining the pixel width of a given piece of text in a
monospaced font under pango)

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

I'm reaching here, but it seems possible that the extra space is because
the character is in isolation.  Perhaps try:

char *x_times_78 = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
pango_layout_set_text(pl, x_times_78, 1);

After some more experimentation, I can say that this doesn't work.
With the help of xmag, I can see that the unambiguously correct width
for a single glyph in the chosen fixed-width font (plus its
side-bearings if any) is 7 pixels, as reported by gdk_char_width().

I can't get pango to reproduce this answer.  I tried using a string of
32 x's and dividing the width given by pango_layout_get_pixel_size()
by 32.  If the x's are lower case, the unit width is reported as 6,
while if they are uppercase it is reported as 9.  That is, pango is
not using the correct (monospaced) font in its calculations. This
seems weird, since prior to doing pango_layout_set_text() I have
initialized the layout using gtk_widget_get_pango_context(widget),
where the widget in question has already been processed with
gtk_widget_modify_font() to apply the monospaced font (and the text
comes out in the right font on screen).

Allin Cottrell.




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