Discussion:
Cross platform TTF font render from Python [was: Load TTF from pycairo under Windows]
Laszlo Nagy
2009-09-18 06:54:13 UTC
Permalink
Hi All,

I need to render antialiased PNG images using TTF font files and UTF-8
text. It needs to be available at least on Linux and Windows. This is
what I have tried:

#1. PIL - it has some problems and I cannot use it. Specifically, the
ImageFont.getsize() returns bad value for some fonts, and there are some
TTF fonts that are rendered incorrectly (UTF-8)

#2. pycairo - very sophisticated, nice features, knows and does
everything correctly. However, has no native support for libfreetype. I
could write an extension in C and use it from Linux. Not available on
windows.

#3. gdmodule - I tried to install in under windows without success. No
binary installer available.

#4. pygame - documentation looks great, it is cross platform. But the
first example program I had tried has been terminated, printing out
memory dump and complaining about double freeing some memory location.

What other options do we have?

Thanks,

Laszlo
Robin Becker
2009-09-18 10:05:59 UTC
Permalink
Post by Laszlo Nagy
Hi All,
I need to render antialiased PNG images using TTF font files and UTF-8
text. It needs to be available at least on Linux and Windows. This is
#1. PIL - it has some problems and I cannot use it. Specifically, the
ImageFont.getsize() returns bad value for some fonts, and there are some
TTF fonts that are rendered incorrectly (UTF-8)
#2. pycairo - very sophisticated, nice features, knows and does
everything correctly. However, has no native support for libfreetype. I
could write an extension in C and use it from Linux. Not available on
windows.
#3. gdmodule - I tried to install in under windows without success. No
binary installer available.
#4. pygame - documentation looks great, it is cross platform. But the
first example program I had tried has been terminated, printing out
memory dump and complaining about double freeing some memory location.
.......
the reportlab graphics renderPM(_renderPM) module does most things with T1 and
TTF and works linux/win32. We use freetype2 internally to extract the curves
from ttf and then draw them with libart_lgpl which does anti-aliasing.

However, we don't render the fonts using freetype so hinting etc etc don't get done.

I think something similar could be done directly with PIL and the antigrain
extension.

I'm surprised when you say that libfreetype isn't available on windows. It's a
fairly hard road, but it can be travelled; certainly we built the parts of
freetype that we needed into our extension. That required only a static library
from freetype. I haven't needed to do this on windows since 2.1.5 so perhaps
it's harder now.
--
Robin Becker
Laszlo Nagy
2009-09-18 13:20:15 UTC
Permalink
Post by Robin Becker
.......
the reportlab graphics renderPM(_renderPM) module does most things
with T1 and TTF and works linux/win32. We use freetype2 internally to
extract the curves from ttf and then draw them with libart_lgpl which
does anti-aliasing.
I just tried reportlab and RenderPM. I got the same error others got: it
looks impossible to use it for creating raster images. Details here:

http://osdir.com/ml/python.reportlab.user/2005-06/msg00015.html
Post by Robin Becker
I think something similar could be done directly with PIL and the
antigrain extension.
PIL would be ideal because it is lightweight and works on Windows and
Linux too. But it is buggy. It doesn't render some east european
(iso8859-2) characters, which we deperately need. Some TTF fonts are
rendered incorrecly in PIL. Simply it doesn't work.
Post by Robin Becker
I'm surprised when you say that libfreetype isn't available on
windows. It's a fairly hard road, but it can be travelled; certainly
we built the parts of freetype that we needed into our extension. That
required only a static library from freetype. I haven't needed to do
this on windows since 2.1.5 so perhaps it's harder now.
I'll try anything that might work. In fact I have already seen articles
about windows + libfreetype on the internet, but I could not find
statically linked libraries built against Python 2.6. If you could send
me a few hints where to start, I would greatly appreciate it. The only
one requirement that I do not want to start writting C code and glue
together libraries by hand. Not because I'm lazy but because I would
like to have something that can be installed easily on new windows
systems, and have no dependency problems "out of the box".

Thank you for your efforts.

Laszlo
Robin Becker
2009-09-18 13:50:14 UTC
Permalink
Post by Laszlo Nagy
Post by Robin Becker
.......
http://osdir.com/ml/python.reportlab.user/2005-06/msg00015.html
OK your error occurs because we need to set up the renderPM canvas with an
initial font (for compatibility with the PDF canvas). You can down load a zip
file containing suitable files from http://www.reportlab.org/ftp/pfbfer.zip.

Just unzip the pfb/afm files into reportlab/fonts and stuff should work there after.
Post by Laszlo Nagy
.........
Post by Robin Becker
I'm surprised when you say that libfreetype isn't available on
windows. It's a fairly hard road, but it can be travelled; certainly
we built the parts of freetype that we needed into our extension. That
required only a static library from freetype. I haven't needed to do
this on windows since 2.1.5 so perhaps it's harder now.
I'll try anything that might work. In fact I have already seen articles
about windows + libfreetype on the internet, but I could not find
statically linked libraries built against Python 2.6. If you could send
me a few hints where to start, I would greatly appreciate it. The only
one requirement that I do not want to start writting C code and glue
together libraries by hand. Not because I'm lazy but because I would
like to have something that can be installed easily on new windows
systems, and have no dependency problems "out of the box".
Thank you for your efforts.
Laszlo
I could send you the statically linked library that I use, but it's a
relocatable static library ie you need to combine it with some other extension.
I'm using it to combine with the _renderPM.c file to create a standalone pyd. I
suspect that's not what you want.
--
Robin Becker
David Boddie
2009-09-19 02:43:55 UTC
Permalink
Post by Laszlo Nagy
I need to render antialiased PNG images using TTF font files and UTF-8
text. It needs to be available at least on Linux and Windows. This is
[...]
Post by Laszlo Nagy
#4. pygame - documentation looks great, it is cross platform. But the
first example program I had tried has been terminated, printing out
memory dump and complaining about double freeing some memory location.
I'm surprised this doesn't work. I've seen games that are distributed
with TrueType fonts, so I would expect them to work, or people would
probably say something. :-)
Post by Laszlo Nagy
What other options do we have?
PyQt4 can render Unicode text to all sorts of paint devices, and it supports
TrueType fonts, too. You basically do all the painting via a single API:

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpainter.html

Here's a quick example of how it could be done:

http://www.diotavelli.net/PyQtWiki/Paint%20on%20an%20image

All the relevant stuff happens in the updateImage() method.

David

Loading...