"""This card creates a 'Document' class for rendering a number of identical cards of specified dimensions and intercard gaps. The Document is inherited from piddlePDF.PDFCanvas, and cards are added to it using 'addcard'. All cards drawn are centered on the page (vertically and horizontally). Andrew Williams """ import string import card from piddlePDF import PDFCanvas import pagesizes A4=pagesizes.A4 #An example card, used during code testing. test=card.Card(edition='Test cards vol1', author='Andrew Williams', type='Attack', value='The hotel burns down due to a lack of working fire '+ 'extinguishers.', comment='They were used up during the live roleplaying...') class Document(PDFCanvas): """Class used to store a number of cards, and render them on a page as a PDF file. Cards can be laid out horizontally (landscape) or vertically. """ def __init__(self, cardheight=255, cardwidth=156, hgap=20, vgap=20, minborder=10, portrait=1): """Create a document object with the specified card dimensions and intercard gaps, in points (1/72 inch). The 'minborder' field is the _minimum_ value for the page margin. If portrait=1 then the cards will be drawn vertically, otherwise they will be in landscape mode. """ PDFCanvas.__init__(self) self.pdf.setPageSize(A4) self.orientation(portrait) self.cardheight=cardheight self.cardwidth=cardwidth self.hgap=hgap self.vgap=vgap self.minborder=minborder self.Nacross=int((self.pagewidth-(minborder*2)+hgap) / (cardwidth+hgap)) self.Ndown=int((self.pageheight-(minborder*2)+vgap) / (cardheight+vgap)) drawnwidth=self.Nacross*(self.cardwidth+self.hgap)-self.hgap self.xp=(self.pagewidth - drawnwidth)/2 drawnheight=self.Ndown*(self.cardheight+self.vgap)-self.vgap self.yp=(self.pageheight - drawnheight)/2 self.cardnum=0 def wrapString(self, s='', x=0, y=0, width=100, font=None, color=None): """Draw a string word-wrapped to a given width, in points. Note that no check for string _length_ is made. """ if not font: font=self.defaultFont if not color: color=self.defaultFillColor xp=x yp=y wsp=self.stringWidth(' ', font) for word in string.split(s): ww=self.stringWidth(word, font) if ww+wsp > width: return #There's a word too long for the given width if xp+wsp+ww > x+width: yp=yp+self.fontHeight(font)+self.fontDescent(font) xp=x self.drawString(word, xp, yp, font, color) xp=xp+ww else: self.drawString(word, xp+wsp, yp, font, color) xp=xp+wsp+ww def orientation(self, portrait=1): "Sets up for portrait or landscape mode" self.portrait=portrait if portrait: self.pagewidth=A4[0] self.pageheight=A4[1] else: # Define coordinate transforms to be used for future drawing self.pdf.rotate(-90) self.pdf.translate(-A4[1],0) self.pagewidth=A4[1] self.pageheight=A4[0] def newpage(self): "Issues a PDF showpage, and resets the card number and drawing options" self.clear() #Issue a showpage self.orientation(self.portrait) drawnwidth=self.Nacross*(self.cardwidth+self.hgap)-self.hgap self.xp=(self.pagewidth - drawnwidth)/2 drawnheight=self.Ndown*(self.cardheight+self.vgap)-self.vgap self.yp=(self.pageheight - drawnheight)/2 self.cardnum=0 def addcard(self, card): """Add a card to the document, and render it as PDF. Keep track of the card number and calculate the position for the next card. """ if self.cardnum == self.Nacross*self.Ndown: self.newpage() card.draw(self, x=self.xp, y=self.yp, height=self.cardheight, width=self.cardwidth) self.cardnum=self.cardnum+1 row,col=divmod(self.cardnum, self.Nacross) if col==0: self.yp=self.yp+self.cardheight+self.vgap drawnwidth=self.Nacross*(self.cardwidth+self.hgap)-self.hgap self.xp=(self.pagewidth - drawnwidth)/2 else: self.xp=self.xp+self.cardwidth+self.hgap