"""This module defines the code to represent a card, and render it on a Piddle PDF Canvas. The canvas must provide the non-standard 'wrapString' method. Andrew Williams """ import piddle import string PIL=0 try: import Image PIL=1 except: print "Python Imaging Library not found, disabling image support" class Card: "Represents a card, with methods to create and render on a canvas" def __init__(self, edition='', author='', type='', value='', comment='', back='', background='', panel=''): """Initialise a card. """ self.edition=edition self.author=author self.type=type self.value=value self.comment=comment self.back=back self.background=background self.panel=panel def draw(self, canvas=None, x=0, y=0, height=100, width=40, border=1): """Render (the front of) a card to the canvas, with the specified position and dimensions, in points (1/72 inch). If border=1, draw a border around the card. """ saver=piddle.StateSaver(canvas) #Restore defaults automagically later canvas.defaultLineWidth=height/120 x1,y1,x2,y2 = x, y, x+width, y+height tfont=piddle.Font(face='times', size=height/15) vfont=piddle.Font(face='times', size=height/24) cfont=piddle.Font(face='times', size=height/24) afont=piddle.Font(face='times', size=height/24) edfont=piddle.Font(face='times', size=height/24) if PIL: if self.background: canvas.drawImage(Image.open(self.background), x1, y1, x2, y2) if self.panel: p=Image.open(self.panel) canvas.drawImage(p, x1+width/10, y1+5*height/16.0, x2-width/10, y1+12*height/16.0) canvas.drawRect(x1, y1, x2, y2, piddle.black) canvas.drawLine(x1, y1+height/8.0, x2, y1+height/8.0, piddle.black) canvas.drawLine(x1, y1+5*height/16.0, x2, y1+5*height/16.0, piddle.black) canvas.drawLine(x1, y1+12*height/16.0, x2, y1+12*height/16.0, piddle.black) canvas.drawLine(x1, y1+15*height/16.0, x2, y1+15*height/16.0, piddle.black) canvas.drawString(s=self.type, x=x1+width/2.0-canvas.stringWidth(self.type, tfont)/2.0, y=y1+height/16.0+tfont.size/2, font=tfont, color=piddle.black) canvas.drawString(s=self.edition, x=x1+edfont.size/4, y=y1+15*height/16.0+edfont.size, font=edfont, color=piddle.black) canvas.drawString(s=self.author, x=x2-afont.size/4-canvas.stringWidth(self.author, edfont), y=y1+15*height/16.0+afont.size, font=afont, color=piddle.black) canvas.wrapString(s=self.value, x=x1+vfont.size/4, y=y1+2*height/16.0+vfont.size, width=width-vfont.size/2, font=vfont, color=piddle.black) canvas.wrapString(s=self.comment, x=x1+cfont.size/4, y=y1+12*height/16.0+cfont.size, width=width-cfont.size/2, font=cfont, color=piddle.black)