[pygtk] Text handling within PyGTK questions
ahaas at airmail.net
Thu Aug 26 06:09:16 WST 2004
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 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.
I am also confused about the result of finding the spacing between
lines. When the pango.Layout instance is dealing with multiple lines,
it always seems to return 0 for get_spacing(). Is this the default
value, or does it depend on a particular font? Calling the
set_spacing() method and then get_spacing() will return the value
given in set_spacing(), so I know the spacing can be adjusted.
I'm certain that text scaling problems like the ones I'm dealing with
have occured before, and I'm hoping that some of the list readers can
share a little of that knowledge. Thanks in advance.
Man once surrendering his reason, has no remaining guard against absurdities
the most monstrous, and like a ship without rudder, is the sport of every wind.
-Thomas Jefferson to James Smith, 1822
More information about the pygtk