[reportlab-users] Font Helvetica always used?

Robin Becker robin at reportlab.com
Thu Jun 18 15:59:58 EDT 2015

On 18 June 2015 at 20:32, Glenn Linderman <v+python at g.nevcal.com> wrote:
> On 6/18/2015 5:08 AM, Robin Becker wrote:
> But do I understand correctly that simply providing the fontname='arial'
> parameter when instantiating the Canvas class could bypass all the settings
> weirdness, and/or the initial script hacks?
the token 'arial' is just a string. Support has to be provided by
registering the font (or by it being one of the standard 14).

> Except, in looking at Canvas, it doesn't seem to have a fontname parameter,
> nor a catchall keyword parameter.  So I don't understand your comment "It
> can be passed in"... is that just a possibility in a parallel universe?
It can't be passed in directly, but can be via the default in
rl_config. Alternatively before a canvas is used you can call setFont
and that font will be the one used. However, unless it is one of the
standard 14 you still need to register a font before use.

> I did some experiments with the technique blessed by GvR here
> http://stackoverflow.com/questions/2447353/getattr-on-a-module
> The technique you referenced is mind-blowing!
> 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'
the GvR technique effectively makes a module into a class instance so
that the module object can do all the standard getattr tricks etc etc.

> but canvas.basefontname is a property or variable, not a function. Wouldn't

well as we set it up we can check for callable things and hide them
away. That makes the attribute invisible and subject to __getattr__
magic. In the instance __getattr__ we can see all unknown. attributes.

> this result in self._fontname being a reference to a function, instead of
> the string 'arial'? Or is that one of the side-effects of the __getattr__
> stuff, that the function is called when the property is referenced? (maybe
> my mind didn't get blown far enough apart)

yes exactly; we can check in the __getattr__ for squirrelled away
callables and call them just once when first referenced. The returned
value can then be used from then on.
> This avoids circular importing which happens otherwise.
> I don't understand this comment.

the settings modules/files are used in defining rl_config; most of the
reportlab code will try to import rl_config so if a settings file
contains complex rl code then we have eg

canvas --> rl_config --> settings --> ttfont --> rl_config

Robin Becker

More information about the reportlab-users mailing list