Source: python-reportlab
Version: 3.0-1
Severity: important
Tags: patch

renderPM fails to find (replacements for) standard PostScript fonts:

$ python test-minimal.py
Warn: Can't find .pfb for face 'Times-Roman'
Traceback (most recent call last):
File "test.py", line 6, in <module>
  renderPM.drawToFile(d, 'test.png', 'PNG')
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 
655, in drawToFile
  c = drawToPMCanvas(d, dpi=dpi, bg=bg, configPIL=configPIL, 
showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 
641, in drawToPMCanvas
  draw(d, c, 0, 0, showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 
50, in draw
  R.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 
198, in draw
  self.initState(x,y)  #this is the push()
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 
99, in initState
  self.applyState()
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 
93, in applyState
  self._canvas.setFont(s['fontName'], s['fontSize'])
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 
374, in setFont
  _setFont(self._gs,fontName,fontSize)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 
227, in _setFont
  raise RenderPMError("Can't setFont(%s) missing the T1 files?\nOriginally %s: 
%s" % (fontName,s1,s2))
reportlab.graphics.renderPM.RenderPMError: Can't setFont(Times-Roman) missing 
the T1 files?
Originally <type 'exceptions.TypeError'>: makeT1Font() argument 2 must be 
string, not None


I've attached patch that makes renderPM use the replacement from the gsfont package. You should also add gsfonts to Recommends or Depends somewhere.

I've also attached a script (test-fonts.py) that I used to verify that after patching renderPM is able to find all the fonts correctly.

--
Jakub Wilk
--- a/src/reportlab/pdfbase/_fontdata.py
+++ b/src/reportlab/pdfbase/_fontdata.py
@@ -54,46 +54,22 @@
     }
 
 #this maps fontnames to the equivalent filename root.
-_font2fnrMapWin32 = {
-                    'symbol':                   'sy______',
-                    'zapfdingbats':             'zd______',
-                    'helvetica':                '_a______',
-                    'helvetica-bold':           '_ab_____',
-                    'helvetica-boldoblique':    '_abi____',
-                    'helvetica-oblique':        '_ai_____',
-                    'times-bold':               '_eb_____',
-                    'times-bolditalic':         '_ebi____',
-                    'times-italic':             '_ei_____',
-                    'times-roman':              '_er_____',
-                    'courier-bold':             'cob_____',
-                    'courier-boldoblique':      'cobo____',
-                    'courier':                  'com_____',
-                    'courier-oblique':          'coo_____',
-                    }
-if sys.platform in ('linux2',):
-    _font2fnrMapLinux2 ={
-                'symbol': 'Symbol',
-                'zapfdingbats': 'ZapfDingbats',
-                'helvetica': 'Arial',
-                'helvetica-bold': 'Arial-Bold',
-                'helvetica-boldoblique': 'Arial-BoldItalic',
-                'helvetica-oblique': 'Arial-Italic',
-                'times-bold': 'TimesNewRoman-Bold',
-                'times-bolditalic':'TimesNewRoman-BoldItalic',
-                'times-italic': 'TimesNewRoman-Italic',
-                'times-roman': 'TimesNewRoman',
-                'courier-bold': 'Courier-Bold',
-                'courier-boldoblique': 'Courier-BoldOblique',
-                'courier': 'Courier',
-                'courier-oblique': 'Courier-Oblique',
-                }
-    _font2fnrMap = _font2fnrMapLinux2
-    for k, v in _font2fnrMap.items():
-        if k in _font2fnrMapWin32.keys():
-            _font2fnrMapWin32[v.lower()] = _font2fnrMapWin32[k]
-    del k, v
-else:
-    _font2fnrMap = _font2fnrMapWin32
+_font2fnrMap = {
+    'symbol':                   's050000l',
+    'zapfdingbats':             'd050000l',
+    'helvetica':                'n019003l',
+    'helvetica-bold':           'n019004l',
+    'helvetica-oblique':        'n019023l',
+    'helvetica-boldoblique':    'n019024l',
+    'times-roman':              'n021003l',
+    'times-bold':               'n021004l',
+    'times-italic':             'n021023l',
+    'times-bolditalic':         'n021024l',
+    'courier':                  'n022003l',
+    'courier-bold':             'n022004l',
+    'courier-oblique':          'n022023l',
+    'courier-boldoblique':      'n022024l',
+}
 
 def _findFNR(fontName):
     return _font2fnrMap[fontName.lower()]
@@ -109,19 +85,6 @@
 del T1SearchPath, rl_isfile
 
 def findT1File(fontName,ext='.pfb'):
-    if sys.platform in ('linux2',) and ext=='.pfb':
-        try:
-            f = _searchT1Dirs(_findFNR(fontName))
-            if f: return f
-        except:
-            pass
-
-        try:
-            f = _searchT1Dirs(_font2fnrMapWin32[fontName.lower()]+ext)
-            if f: return f
-        except:
-            pass
-
     return _searchT1Dirs(_findFNR(fontName)+ext)
 
 # this lists the predefined font encodings - WinAnsi and MacRoman.  We have
from reportlab.graphics.shapes import Drawing
from reportlab.graphics import renderPM

d = Drawing(100, 100)
renderPM.drawToFile(d, 'test.png', 'PNG')
# encoding=UTF-8

from reportlab.graphics.shapes import Drawing, String
from reportlab.graphics import renderPM
from reportlab.pdfbase._fontdata import standardFonts as fonts

try:
    unichr
except NameError:
    unichr = chr

d = Drawing(800, 50 * len(fonts))
for i, font in enumerate(fonts):
    if font == 'Symbol':
        text = 'Συμβολο'
    elif font == 'ZapfDingbats':
        text = ''.join(
            unichr(0x2710 + i) for i, ch in enumerate(text)
        )
    else:
        text = font
    s = String(0, i * 50, text)
    s.fontName = font
    s.fontSize = 50
    d.add(s)
renderPM.drawToFile(d, 'test-fonts.png', 'PNG')

Reply via email to