Obtaining glyph width in Python

Charlie charles.gunyon at gmail.com
Mon Jul 4 08:07:41 EDT 2005


Hi, I'm looking for a way to obtain the width of a string, either in actual
inches/centimeters, or pixels will also work.  Unfortunately this seems
difficult as I'd like to keep things as close to the stock Python install as
possible, and I'm not working with Graphics or X at all.  Here are the options
I've found, and why they're less than optimal for my use:

PIL = Huge for only using one function.  I'm not working with any graphics.
PyFT = Everyone uses FreeType2 now, and PyFT seems dead anyhow.
PyFT2 = Does not exist.
tkinter.text() = Works with X, creates windows no matter what you do.
t1lib = Separate package, no TTF support.
t1python = Same thing as t1lib?

Ultimately, I'm looking to take a stream of text, and break it up into lines
based on page width... and I need to know how wide (and ultimately how tall,
for page breaks) the individual glyphs are so I can break properly.  If there's
an easier way to do this than calculating individual glyph width, I'm open to
that too.

I was really just looking to see if there was anything out there that wasn't
too large or too obscure/dated.  Maybe there's something lower level that could
be done to achieve this?  Is there metadata in the font that holds this
information that could be extracted?

Thanks in advance,
-Charlie



More information about the Python-list mailing list