Re: Finding width of a piece of text
- From: Behdad Esfahbod <behdad behdad org>
- To: Ian Puleston <ian underpressuredivers com>
- Cc: 'gtk-list' <gtk-list gnome org>
- Subject: Re: Finding width of a piece of text
- Date: Sun, 26 Apr 2009 19:54:22 -0400
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 also tried getting the PangoLayoutLine from the layout and using
pango_layout_line_get_extents but got the same result.
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
_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]