[reportlab-users] accentuated chars w/ TTF and textLabels

Webmaster webmaster at expertancy.com
Fri Mar 18 09:49:05 EST 2005


Hello,

I just compiled _renderPM :-) see previous messages
now my objective is to use TT fonts in text labels, that would support 
Stroke outline, and é à û ù ... (latin-1) char in them

code, trace and debug below

here is a summary :

             outline     latin char    outline + latin char
TTF        Y               E1                  E2
RLF        Y               Y                   E3

Y : produced a correct gif file
RL : reportlab font (pfb files)
TTF : TrueType (.ttf files)

error E1 :
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
line 170, in drawString
    self._canvas.drawString(x,y,text)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 6-8: 
invalid data

error E2 :
 line 93, in _text2PathDescription
    for g in _gs._stringPath(text,x,y):
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 6-8: 
invalid data

error E3
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/shapes.py", 
line 813, in _renderPath
    apply(func, points[i:j])
_renderPM.Error: path must begin with a moveTo

===================================
CODE

# -*- coding: latin-1 -*-
import os, sys

from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas

from reportlab.pdfbase.pdfmetrics import stringWidth
from reportlab.pdfbase.pdfmetrics import getFont
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfbase.ttfonts import TTFont

from reportlab.lib.utils import getStringIO, open_and_read
from reportlab.graphics.shapes import *

#from reportlab.graphics.renderPDF import _PDFRenderer
from reportlab.graphics.renderPM import drawToFile

from reportlab import rl_config

from reportlab.graphics.charts.textlabels import Label

registerFont(TTFont('Rina', 'rina.ttf'))


def MakeLabel():
    D = Drawing(400, 200)
    lab = Label()
    lab.setOrigin(50,150)
    lab.boxAnchor = 'nw'
    lab.angle = 5
    lab.dx = 0
    lab.dy = -20
    lab.fontName = 'Rina'
    lab.fontSize = 46
    lab.fillColor = colors.black
    #lab.strokeColor = colors.yellow
    #lab.strokeWidth = 1
    lab.setText('Hello é ï î ö ô\n ç à ù û ü World')

    D.add(lab)
    return D


def test():
    drawing=MakeLabel()
    for k in ['png','gif','pct']:
        filename = '%s.%s' % ('sortie', k)
        fullpath = os.path.join('.', filename)
        if os.path.isfile(fullpath):
            os.remove(fullpath)
        from reportlab.lib.colors import white
        drawToFile(drawing,fullpath,fmt=k,bg=white, 
configPIL={'transparent':0xFFFFFF})

if __name__=='__main__':
    test()


ERROR TRACE

Warn: Font Rina: _not an encoding_  not found - using Courier: _not an 
encoding_  for widths
Traceback (most recent call last):
  File "rl_text_usage.py", line 107, in ?
    test()
  File "rl_text_usage.py", line 104, in test
    drawToFile(drawing,fullpath,fmt=k,bg=white, 
configPIL={'transparent':0xFFFFFF})
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
line 562, in drawToFile
    c = drawToPMCanvas(d, dpi=dpi, bg=bg, configPIL=configPIL, 
showBoundary=showBoundary)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
line 548, in drawToPMCanvas
    draw(d, c, 0, 0, showBoundary=showBoundary)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
line 51, in draw
    R.draw(drawing, canvas, x, y, showBoundary=showBoundary)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderbase.py", 
line 189, in draw
    self.drawNode(drawing)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
line 98, in drawNode
    self.drawNodeDispatcher(node)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderbase.py", 
line 245, in drawNodeDispatcher
    self.drawGroup(node)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderbase.py", 
line 270, in drawGroup
    self.drawNode(node)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
line 98, in drawNode
    self.drawNodeDispatcher(node)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderbase.py", 
line 245, in drawNodeDispatcher
    self.drawGroup(node)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderbase.py", 
line 270, in drawGroup
    self.drawNode(node)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
line 98, in drawNode
    self.drawNodeDispatcher(node)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderbase.py", 
line 243, in drawNodeDispatcher
    self.drawString(node)
  File 
"/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py", 
line 170, in drawString
    self._canvas.drawString(x,y,text)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 6-8: 
invalid data

DEBUG TRACE
(Pdb) print stringObj.text
Hello é ï î ö ô
(Pdb) s
 > 
/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py(161)drawString()
-> if not text_anchor in ['start','inherited']:
(Pdb) print text_anchor
start
(Pdb) n
 > 
/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py(170)drawString()
-> self._canvas.drawString(x,y,text)
(Pdb) s
--Call--
 > 
/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py(335)__getattr__()
-> def __getattr__(self,name):
(Pdb) s
 > 
/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py(336)__getattr__()
-> return getattr(self._gs,name)
(Pdb) s
--Return--
 > 
/home/zope/zope/py24/lib/python2.4/site-packages/reportlab/graphics/renderPM.py(336)__getattr__()-><built-i...40546410>
-> return getattr(self._gs,name)
(Pdb) s
UnicodeDecodeError: <exceptions.UnicodeDecodeError instance at 0x405ae8ac>




More information about the reportlab-users mailing list