Finding width of a piece of text
- From: "Ian Puleston" <ian underpressuredivers com>
- To: "'gtk-list'" <gtk-list gnome org>
- Subject: Finding width of a piece of text
- Date: Sun, 26 Apr 2009 16:27:25 -0700
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.
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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]