[pygtk] Text handling within PyGTK questions

Gustavo J. A. M. Carneiro gjc at inescporto.pt
Thu Aug 26 19:09:58 WST 2004

A Qua, 2004-08-25 às 23:09, Art Haas escreveu:
> Hi.
> My PythonCAD work has lately been in dealing with text, and I'm turning
> to the list in an effort to get some suggestions and help.
> The root of my difficulties is the way I'm trying to define and
> handle text sizes. I want the text size in a drawing to be defined
> as a floating point value, say 2.5 inches high, instead of defining the
> text size in points. As you zoom in and zoom out of the drawing,
> the size of the text should change. If you are zoomed into a region
> with a text blob, the text lettering should look larger, and as you
> zoom out the lettering gets smaller.
> Part of the problem I'm having is that PyGTK is missing a few Pango
> methods that allow for a more detailed inspection of the pango.Layout
> instances. The get_line(), get_lines(), and get_iter() methods are
> not currently available, and each would be immensely useful when
> determining the positioning of text. My C skills are lacking in the
> ability to submit a patch to provide these missing routines, but even if
> they were to appear in the next release of PyGTK, I'm still stuck in
> that the routines are not in the prior releases. BTW, these routines
> are used when the pango.Layout instance contains multiple lines of text.

  The API for PyGTK 2.4 is frozen, but if you open a bug report we might
add those methods for PyGTK 2.6.

  In the mean time, though, you could try to split text yourself in
multiple lines, and put each line on a separate pango layout.  Not an
ideal solution, but at least compatible with current pygtk.  Otherwise
you'd have to build your own extension module with those functions,
independent of pygtk, and ship with your application.  Or file a bug
report and wait 6 months. :-)

> The pango module does currently provied get_line_count(), get_size(),
> and get_pixel_size() methods for pango.Layout instances, and these
> methods are indespensible for determining the size of some blob of text.
> I've found, though, that the ratio of the width and height values of
> a Layout change as the font size changes. That is, if you call
> get_size() on a layout defined for a small font size, their ratio will
> not be the same as if the font size was larger, and this behavior
> is causing me problems. What I really want to do is to calculate the
> width of some text given a particular font family, font weight, and
> font style. What I'd been trying to do is calculate the width and
> height of some text at a given font size, say 18 pts, and then
> use those values to calculate the width. I'd calculate the width
> of the text by assuming the ratio of width/height in pango units
> is the same as it would be in the units I'm interested in, such
> as millimeters or inches, and via basic algebra determine the
> text width. This approach is not working, as the ratio is
> variable, and I'm a bit stumped on what else to try. Suggestions on 
> other approaches to calculating text width welcomed.

  Don't ever[1] try to guess the text width given font description and
text string.  As you found out, the results are not accurate.  That is
because of hinting, which adjusts both size and position of text on
  Why can't you just create a pango layout and ask for its size, even
without rendering it?


[1] except for speed/accuracy tradeoff in complex layout algorithms

Gustavo J. A. M. Carneiro
<gjc at inescporto.pt> <gustavo at users.sourceforge.net>
The universe is always one step beyond logic

More information about the pygtk mailing list