[reportlab-users] Using Mac and TrueType fonts with renderPM

Ka-Ping Yee reportlab at zesty.ca
Sat Jan 6 22:01:17 EST 2007


Hello,

I just went through many steps and some sleuthing to figure out
how to get renderPM to properly produce a PNG image containing
strings drawn in a Macintosh font, and I'd like to contribute
my notes here. I hope they help someone.

1. I installed ReportLab 2.0, renderPM, and PIL.

2. I wanted to use Lucida Grande, which is available on all
Mac OS X systems at /System/Library/Fonts/LucidaGrande.dfont.
But to use it with ReportLab, I needed a TTF file, and I also
discovered that to use it with renderPM, I needed a Type 1
font file as well.

3. fondu, available at fondu.sf.net, will convert the .dfont
file to a .ttf. (Use fondu <filename>.)

4. ttf2pt1, available at ttf2pt1.sf.net, will convert the .ttf
file to a .pfb. (Use ttf2pt1 -b <filename>.)

5. Now you can create a font object and register it as usual:

from reportlab.pdfbase import pdfmetrics, ttfonts, _fontdata
font = ttfonts.TTFont('Lucida Grande', 'LucidaGrande.ttf')
pdfmetrics.registerFont(font)

6. However, this font object has no encoding vector. To make
it usable by renderPM, I had to stuff in an encoding vector:

font.encoding.vector = _fontdata.encodings['StandardEncoding']

7. renderPM will always look for the Times-Roman font, even if
you don't use it, and will fail because you don't have the
Type 1 files for Times-Roman. To fix this, do:

from reportlab.graphics import shapes, renderPM
shapes.STATE_DEFAULTS['fontName'] = 'Lucida Grande'

8. Now you can finally draw!

d = shapes.Drawing()
d.add(shapes.String(100, 100, 'Hello.', fontSize=16))
renderPM.drawToFile(d, 'hello.png', 'PNG')

I had to look through the source of pdfmetrics, ttfonts, _fontdata,
and renderPM to figure out how to get around these problems, but
I haven't studied the code enough to submit a patch. It looks like
steps 6 and 7 could/should be fixed in ReportLab. Presumably the
right thing to do for 6 is to extract the encoding from the TTF,
and the right thing to do for 7 is to not attempt to look up the
Type 1 file until it's actually time to draw a string.

Hope this helps!


-- ?!ng


More information about the reportlab-users mailing list