[reportlab-users] UTF8 Encoded Strings

Tim Roberts timr at probo.com
Wed Oct 31 16:48:28 EDT 2007


Rich Shepard wrote:

>

> Are these available for reuse by others ... particularly me? :-)


Here's one example, completely devoid of comments. If it was hard to
write, it should be hard to read. ;)

textOut writes something at the current caret, and updates the caret.
textOutNo writes something at the current caret, but does not update the
caret. textLine writes at the caret and does a carriage return and
linefeed. drawUnderline draws an underline starting at the caret.
drawUnderlineTo draws an underline from the caret to a fixed position.
drawBox draws a checkbox at the caret and advances beyond it. gap
inserts a fixed-size gap.

class TextBox:
def __init__(self, canvas, x=0, y=0 ):
self.canvas = canvas
self.setXY( x, y )
self.x0 = x

def setXY( self, x, y ):
self.x = x
self.y = y

def getXY( self ):
return (self.x,self.y)

def linefeed( self ):
self.y -= self.canvas._leading

def textOut( self, txt ):
c = self.canvas
c.drawString( self.x, self.y, txt )
self.x += c.stringWidth(txt, c._fontname, c._fontsize)

def textOutNo( self, txt, adjustx=0 ):
c = self.canvas
c.drawString( self.x+adjustx, self.y, txt )

def textLine( self, txt=None ):
if txt:
self.canvas.drawString( self.x, self.y, txt )
self.x = self.x0
self.linefeed()

def drawUnderline( self, length, adjustx=1 ):
self.canvas.line( self.x, self.y-2, self.x+length, self.y-2 )
if adjustx:
self.x += length

def drawUnderlineTo( self, x ):
self.drawUnderline( x - self.x )

def drawBox( self, w, h, gap=0 ):
self.canvas.rect( self.x, self.y-2, w, h )
self.x += w + gap

def gap( self, w ):
self.x += w

def fillWith( self, ch, tabstop ):
c = self.canvas
s = ''
target = tabstop - self.x
while c.stringWidth( s, c._fontname, c._fontsize ) < target:
s += ch
c.drawRightString( tabstop, self.y, s[:-1] )
self.x = tabstop

So, a usage:

c = self.canvas.
...
tb = TextBox( c, left, y )
self.fontNormal()
tb.textOut( 'Tax ID#' ) # draw and advance
tb.gap( 6 ) # leave 6 point gap
self.fontField()
# Fill in a data field but do not advance caret. Leave
# a 9 pt left margin.
tb.textOutNo( self.data['ein'], 9 )
tb.drawUnderline( 2*inch ) # draw a 2 inch underline
tb.textLine() # start new line

if not self.isCouncil:
self.fontNormal()
tb.textOut( 'Club Name:' )
tb.gap( 6 )
self.fontField()
tb.textOutNo( self.data['name'], 9 )
tb.drawUnderline( 6*inch )
tb.textLine()

self.fontNormal()
tb.textOut( 'Council:' )
tb.gap( 6 )
self.fontField()
tb.textOutNo( self.data['council'], 9 )
tb.drawUnderline( 3*inch )
tb.textLine()
return tb.getXY()[1] # return the new bottom Y

These all draw immediately. They don't use the deferred textbox feature.

--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list