[reportlab-users] Built-in versus embedded fonts

Glenn Linderman v+python at g.nevcal.com
Wed Jun 24 15:05:39 EDT 2015

On 6/24/2015 3:14 AM, Robin Becker wrote:
> 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 

OK, something clicked, but something is _VERY_ unclear.

I changed my code to pass "Times-New-Roman" to Canvas.stringWidth, and 
that works.

Before, I had only tried "Times", "times", and "Times New Roman".

Of course the font name is actually "Times New Roman" in the font file.  
Somewhere along the line I had felt a need to register it as 
"Times-New-Roman".    And when doing the font-family to and  -Bold etc.  
So by the time I needed stringWidth, I had forgotton about the hyphens.  
Interestingly, taking all the hyphens out, also works, then passing 
"Times New Roman" to the stringWidth.

So either something changed and example code I found is old, or hyphens 
were never needed but allowed, but should be used or not used consistently.

So, either consistently using or consistently not using hyphens instead 
of spaces in the font names works fine with pdfmetrics.stringWidth.  But 
mixing them causes the problem.

I guess that is a user code error, but it is _VERY_ unclear, why both 
forms work.... and if both forms work, why they must be used so 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20150624/010ba80c/attachment.html>

More information about the reportlab-users mailing list