Re: Finding width of a piece of text



> -----Original Message-----
> From: Behdad Esfahbod 
> Sent: Sunday, April 26, 2009 4:54 PM
> 
> On 04/26/2009 07:27 PM, Ian Puleston wrote:
> > Hi,
> >
> > I'm having (yet another) problem with Pango. I have a need to find
> the width
> > that a piece of text will be displayed. I thought this piece of code
> would
> > do it, but the result is not quite right. The Pango attributes have
> been set
> > for "Courier" font and point size 10:
> >
> >      lbl = gtk_label_new(pText);
> >      gtk_label_set_attributes(GTK_LABEL(lbl), attrList);
> >      layout = gtk_label_get_layout(GTK_LABEL(lbl));
> >      g_assert(layout);
> >      pango_layout_get_size(layout,&width,&height);
> >
> >      gdouble wid = (gdouble)width / (72 * PANGO_SCALE);
> >      g_print("'%s' width %d units (%.2g\")\n", pText, width, wid);
> >
> > An example of what it prints is:
> >
> > 'Ken Hanlon' width 81920 units (1.1")
> > 'Ian Puleston' width 98304 units (1.3")
> >
> > But those don't match the widths it prints on paper with the same
> font and
> > point size - these names are both about 1" wide.
> 
> Why are you measuring a GtkLabel layout, but talk about print?  A
> GtkLabel is
> a widget to be shown on the screen.  Text metrics depend on the target
> surface
> (by way of surface font options).  Create a layout using
> pango_cairo_create_layout() directly for your target PDF/PS/etc
> surface.
> 
> behdad

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?

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.

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.

> > 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]