[reportlab-users] TrueType Fonts

Marek Pêtlicki reportlab-users@reportlab.com
Thu, 13 Mar 2003 17:27:37 +0100


Hi!

I have this problem with true type fonts.

When trying to use them in a code like this:

from reportlab.lib.fonts import addMapping
from reportlab.pdfbase import pdfmetrics

# Adding TT font Arial + mappings
pdfmetrics.registerFont(TTFont('Arial', 'arial.ttf'))
pdfmetrics.registerFont(TTFont('Arial-Bold', 'arialbd.ttf'))
pdfmetrics.registerFont(TTFont('Arial-Italic', 'ariali.ttf'))
pdfmetrics.registerFont(TTFont('Arial-BoldItalic', 'arialbi.ttf'))
addMapping('Arial', 0, 0, 'Arial')
addMapping('Arial', 1, 0, 'Arial-Bold')
addMapping('Arial', 0, 1, 'Arial-Italic')
addMapping('Arial', 1, 1, 'Arial-BoldItalic')


there come 2 kinds of problems.

First is that the name Arial is taken, so I can not use it.

The file in reportlab responsible for this is libs/font.py, where are 
predefined some font names. Is there any reason why I should not 
override them?

The other problem (with the same core) is that I can't add font variants 
to the same font name (bold, italics, bold-italics and regular). it ends 
up with ValueError exception from addMapping() function run internally 
by the registerFont() function:

         fonts.addMapping(ttname, 0, 0, font.fontName)
         fonts.addMapping(ttname, 1, 0, font.fontName)
         fonts.addMapping(ttname, 0, 1, font.fontName)
         fonts.addMapping(ttname, 1, 1, font.fontName)

so I can't register font variants from many ttf-files for the same font 
name, because registerFont() adds all the mappings for me with the first 
registered ttf file! A single font file will work for all of the 
variants, so using font modifications (like in platypus' Paragraph class 
with XML-markup) simply will not work.

My above code will fail and addMapping() function will rise an 
ValueError exception... So the both problems are linked with 
addMapping() function.

Why is it done this way?

For now I've hashed-out the mentioned code in pdfmetrics.py (as well as 
the Arial mapping in fonts.py, but I am not sure if I'm not creating 
some bug in here) and my code works flawlessly (using some Platypus 
functionality and some 'core' pdfgen).

I would eventually like to make my code clean, not needing to 'patch' 
every new release of RportLab. Or, in the worst event, to know, why I 
should not use it the way I want (although I did not do anything in 
conflict with the documentation! Which is still 1.15-version based by 
the way).


My example code used to work in 1.15, so the changes happened somewhere 
in between...


best regards

Marek Pêtlicki