[reportlab-users] Built-in versus embedded fonts

Glenn Linderman v+python at g.nevcal.com
Wed Jun 24 13:09:35 EDT 2015


On 6/24/2015 3:14 AM, Robin Becker wrote:
> On 24/06/2015 07:17, Glenn Linderman wrote:
> .............
>> The rest is experimentation based on this discussion thread.
>>
>> I don't have the latest version of reportlab, but I think it is the 
>> latest one
>> released.... there is a reportlab-3.1.8.dist-info in my 
>> site-packages, so I'll
>> assume that is the version number.
>>
>> Glenn
> ........
>
> Glenn, I'm not sure exactly where your PDFu class is going wrong, but
> in latest code this works as expected using the new Canvas argument

Me neither.  I guess 3.1.8 isn't the latest, not even the latest 
released.  I should try the latest released, I guess, before doing 
anything else. But I suppose you tried it on the latest? And it still 
failed there?

If you did, and it still fails, then something is going wrong.  PDFu is 
just trying to be a flatter name space than reportlab offerts, exposing 
the APIs I need in my application via PDFu, yet still encapsulated 
rather than cluttering the global name space by importing lots of 
reportlab symbols there... and, it avoids the reportlab imports 
completely if reportlab functionality is not needed, and PDFu is not 
instantiated.

Unless reportlab plays games with altering previously imported symbols 
with different functionality, and only checks module-level and 
global-level symbols when doing such alterations, then there should be 
no problem with encapsulating the APIs in that manner.

I must say that I didn't understand the code I found after looking for 
the symbol Laura suggested, "instanceStringWidthTTF"... in rl_accel.py 
the actual function seems to be defined... but only if the name of the 
function it is already defined in some _py_funcs thing?

Then in ttfonts.py, it is imported from rl_accel, and used by the 
stringWidth defined there... which is apparently the one I am using 
successfully.

So it does appear there are games being played with where and when 
things are defined and used. Maybe it is simply that I have some of the 
imports listed in the wrong order?

I haven't tried to discover how pdfmetrics.stringWidth gets populated 
with other sources of stringWidth functionality... the theory being that 
somehow it learns to call TTFont.stringWidth, which calls 
instanceStringWidthTTF.... there are 33 references to stringWidth, so 
that would be a much bigger job for someone that isn't familiar with the 
code.

I did look at pdfmetrics.py, where the stringWidth that is documented 
lives, and which is the one that doesn't work for me, and found it 
interesting that it imports and uses instanceStringWidthT1, but 
instanceStringWidthTTF is only referenced in a comment.

I also find it interesting that your code below imports some things from 
pdfmetrics early, and stringWidth later. So that makes me wonder even 
more strongly if it is an import order issue.  I never found any import 
order documented in the reportlab documentation, but maybe I didn't look 
hard enough.  I also see that I don't explicitly import pdfmetrics, only 
pdfbase, but then references to pdfbase.pdfmetrics do not give errors, 
the symbols do seem to be defined... but maybe not defined correctly?

>
>
> ############################################################
> from reportlab.pdfgen.canvas import Canvas
> from reportlab.pdfbase.ttfonts import TTFont
> from reportlab.pdfbase.pdfmetrics import registerFont, registerFontFamily
> registerFont(TTFont('times','times.ttf'),)
> registerFont(TTFont('times-bold','timesbd.ttf'),)
> registerFont(TTFont('times-italic','timesi.ttf'),)
> registerFont(TTFont('times-bolditalic','timesbi.ttf'),)
> registerFontFamily('times',
>         normal='times',
>         bold='times-bold',
>         italic='times-italic',
>         boldItalic='times-bolditalic',)
> sample = 'Hello World'
> from reportlab.pdfbase.pdfmetrics import stringWidth
> print "stringWidth(%r,'times',12) = %s" % (sample,stringWidth(sample, 
> 'times',12))
>
> canv = Canvas('thello.pdf',initialFontName='times')
> print "canv.stringWidth(%r) = %s" % (sample,canv.stringWidth(sample))
>
> canv.drawString(72,72,sample)
> canv.save()
> ############################################################
>
>
> this also works exactly as expected using the hacky rl_config stuff
>
> ############################################################
> from reportlab import rl_config
> from reportlab.pdfgen.canvas import Canvas
> from reportlab.pdfbase.ttfonts import TTFont
> from reportlab.pdfbase.pdfmetrics import registerFont, registerFontFamily
> rl_config._SAVED['canvas_basefontname'] = 'times'
> rl_config._startUp()
> registerFont(TTFont('times','times.ttf'))
> registerFont(TTFont('times-bold','timesbd.ttf'))
> registerFont(TTFont('times-italic','timesi.ttf'))
> registerFont(TTFont('times-bolditalic','timesbi.ttf'))
> registerFontFamily('times',
>         normal='times',
>         bold='times-bold',
>         italic='times-italic',
>         boldItalic='times-bolditalic')
>
> sample = 'Hello World'
> from reportlab.pdfbase.pdfmetrics import stringWidth
> print "stringWidth(%r,'times',12) = %s" % (sample,stringWidth(sample, 
> 'times',12))
>
> canv = Canvas('thello1.pdf')
> print "canv.stringWidth(%r) = %s" % (sample,canv.stringWidth(sample))
>
> canv.drawString(72,72,sample)
> canv.save()
> ############################################################
>
> If I add these lines immediately prior to the final for loop in your 
> example
>
> myPDFinit()
> myPDF.register_fonts()
> print myPDF.canv._fontname
> print myPDF.rlfonts.keys()
> for txt in ('012', 'abc', 'ABC'):
>
> then I see this in the output
>
>> C:\Users\rptlab\Downloads>rl-test.py
>> Times-New-Roman
>> ['Times-BoldItalic', 'Times-Italic', 'Times-Bold', 'Times']
>> ('thiswid:', 30.0)
>> Traceback (most recent call last):
>>   File "C:\Users\rptlab\Downloads\rl-test.py", line 82, in <module>
>
>
> so perhaps you should consider making the font set up code a bit more 
> transparent

Not sure what is not transparent... it is all there, just divided into 
functions.


> -- 
> Robin Becker
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at lists2.reportlab.com
> https://pairlist2.pair.net/mailman/listinfo/reportlab-users
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20150624/f295a1ed/attachment-0001.html>


More information about the reportlab-users mailing list