Obtaining glyph width in Python

Andreas Lobinger andreas.lobinger at netsurf.de
Mon Jul 4 09:33:38 EDT 2005


Aloha,

Charlie wrote:
> 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. 

So you need both: metrics for single characters/glyphs and con-
catenated glyphs and words.

> 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?

For the glyph metrics and informations there is the ttx/fonttools
project on sourceforge available. Afiar fonttools only need a
Numeric installation.

> 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.

It looks like a little bit that you're redeveloping TeX (in python)...

> 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?

Actually there is not only meta but real data included in the font,
speaking of Type1, TrueType and OpenType scalable outline fonts.

Wishing a happy day
		LOBI





More information about the Python-list mailing list