[reportlab-users] Font Helvetica always used?

Robin Becker robin at reportlab.com
Thu Jun 18 08:08:29 EDT 2015


On 17/06/2015 20:55, Glenn Linderman wrote:
> On 6/17/2015 5:09 AM, Robin Becker wrote:
>> If you don't want to mess with the settings overrides then this code at the
>> start of your initial script should do the trick
.........
>
> So what is the real restriction?  This has to be done before what? Before first
> canvas instantiation, or before other imports, or before what, specifically?
.......
The Canvas class needs a default fontname at instantiation. It can be passed in, 
but if the default None is present we use

reportlab.rl_config.canvas_basefontname

however, it's fairly obvious that we need to ensure the font exists at the time 
of first use. With our existing simple rl_config module that's hard to do in one 
place.

I did some experiments with the technique blessed by GvR here

http://stackoverflow.com/questions/2447353/getattr-on-a-module

and it seems if we adopted this technique we could then use a more simple 
mechanism like this in our settings file overrides so  in one of

>
>    i: reportlab/rl_local_settings.py
>   ii: reportlab_settings.py  somewhere on the PYTHONPATH.
>  iii: ~/.reportlab_settings.py for unix like systems.

you could do this

def canvas_basefontname():
     from reportlab.pdfbase.ttfonts import TTFont
     from reportlab.pdfbase.pdfmetrics import registerFont, registerFontFamily
     registerFont(TTFont('arial','arial.ttf'),)
     registerFont(TTFont('arial-bold','arialbd.ttf'),)
     registerFont(TTFont('arial-italic','ariali.ttf'),)
     registerFont(TTFont('arial-bolditalic','arialbi.ttf'),)
     registerFontFamily('arial',
             normal='arial',
             bold='arial-bold',
             italic='arial-italic',
             boldItalic='arial-bolditalic',
             )
     return 'arial'

and the rl_config object could check for and defer evaluation until the first 
import/use of rl_config.canvas_basefontname. This avoids circular importing 
which happens otherwise.
-- 
Robin Becker


More information about the reportlab-users mailing list