[reportlab-users] True Type fonts - newbie question

0 views
Skip to first unread message

Glenn Linderman

unread,
May 16, 2011, 5:08:27 AM5/16/11
to reportl...@lists2.reportlab.com
Trying to access other fonts in ReportLab, because Times doesn't have
them all...

Used the following, sort of from user guide section 3.5, but the new
font doesn't show up in the list, and although this sample doesn't
produce an error from the setFont, when I embed in a larger program,
sometimes it does... after the print !?

But the issue is how can I add a TT font and get it registered, and show
up in the list?

#!c:\python26\python.exe
# -*- coding: utf-8 -*-

import sys, os, re, getopt
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import LETTER, A4, LEGAL, ELEVENSEVENTEEN
from reportlab.lib.units import cm, mm, inch, pica
def list_available_fonts():
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Times-New-Roman',
'c:\\windows\\fonts\\times.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanBd',
'c:\\windows\\fonts\\timesBd.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanIt',
'c:\\windows\\fonts\\timesI.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanBI',
'c:\\windows\\fonts\\timesBI.ttf'))
pdf = Canvas(sys.stdout)
pdf.setFont('Times-New-Roman', 16)
all_fonts = pdf.getAvailableFonts()
print all_fonts

list_available_fonts()
_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
http://two.pairlist.net/mailman/listinfo/reportlab-users

Robin Becker

unread,
May 16, 2011, 5:33:51 AM5/16/11
to reportlab-users
On 16/05/2011 10:08, Glenn Linderman wrote:
........

The getAvailableFonts is actually wrong and is being stupid. It turns out that
it defers to PdfDoc.getAvailableFonts which is also stupid and wrong. Both of
these ought to be named something like getUsedFonts or getActualizedFonts or
somesuch. Turns out that except for the standard 14 we don't internalize the
fonts until they're actually used.

Try the script below and you should actually see the font end up in the list. I
think availableFonts should refer to those that have been registered even if not
yet used.


import sys, os, re, getopt
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import LETTER, A4, LEGAL, ELEVENSEVENTEEN
from reportlab.lib.units import cm, mm, inch, pica
def list_available_fonts():
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Times-New-Roman', 'c:\\windows\\fonts\\times.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanBd',
'c:\\windows\\fonts\\timesBd.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanIt',
'c:\\windows\\fonts\\timesI.ttf'))
pdfmetrics.registerFont(TTFont('Times-New-RomanBI',
'c:\\windows\\fonts\\timesBI.ttf'))
pdf = Canvas(sys.stdout)
pdf.setFont('Times-New-Roman', 16)

pdf.drawString(72,72,'Hello World')


all_fonts = pdf.getAvailableFonts()
print all_fonts

list_available_fonts()


--
Robin Becker

Glenn Linderman

unread,
May 16, 2011, 4:26:12 PM5/16/11
to Robin Becker, reportlab-users
On 5/16/2011 2:33 AM, Robin Becker wrote:
The getAvailableFonts is actually wrong and is being stupid. It turns out that it defers to PdfDoc.getAvailableFonts which is also stupid and wrong. Both of these ought to be named something like getUsedFonts or getActualizedFonts or somesuch. Turns out that except for the standard 14 we don't internalize the fonts until they're actually used.

Try the script below and you should actually see the font end up in the list. I think availableFonts should refer to those that have been registered even if not yet used.

OK, with this information, I whacked at the program and got it working.

Any clues when there'll be a Python 3 version of reportlab?
Reply all
Reply to author
Forward
0 new messages