Re: Finding width of a piece of text



On 04/26/2009 09:09 PM, Ian Puleston wrote:
I may well be wrong, but it seems to me that in this case it should not matter. Should 10-point courier font not be the same size no matter what it is printed on since a point is fixed at 1/72 inch? The "device unit" of a Pango layout is a point so it would seem logical that the text width being a certain number of points is the always going to be the same size. What am I missing here?

Font hinting.  The same font can have different widths when hinted vs not hinted.


To put it another way, the font metrics from the Pango context can tell me the width of the characters (see below) so Pango should be able to tell me the width of the string.

The character widths change depending on the hinting setting.

I can't use pango_cairo_create_layout() at this point because I don't yet know what print device it will be printed on.

Then at least 1) make sure your context is set to 72 dpi, and 2) set font options to disable metrics hinting.

behdad


Note that the following code gets the width of a character from the
font and
reports it as 6074. Converting that from Pango units to points and
then to
inches works out at 0.988" for the above 12-digit strings, which
looks
exactly right. So why would this yield the correct width when
pango_layout_get_size apparently does not?

      ctx = pango_layout_get_context(layout);
      g_assert(ctx);
      fontDesc = pango_context_get_font_description(ctx);
      g_assert(fontDesc);
      lang = pango_context_get_language(ctx);
      g_assert(lang);
      metrics = pango_context_get_metrics(ctx, fontDesc, lang);
      g_assert(metrics);
      g_print("char width %d\n",
pango_font_metrics_get_approximate_char_width(metrics));

Ian







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