class Figure(Flowable): def __init__(self, imgFile, captionTxt, captionStyle, imgWidth=None, imgHeight=None, margin=(0, 0, 0, 0), padding=(0, 0, 0, 0), align=None, borderColor=(0.75, 0.75, 0.75), no_mask=False, url=None): imgFile = imgFile self.imgPath = imgFile # workaround for http://code.pediapress.com/wiki/ticket/324 # see http://two.pairlist.net/pipermail/reportlab-users/2008-October/007526.html if no_mask: self.i = Image(imgFile, width=imgWidth, height=imgHeight, mask=None) else: self.i = Image(imgFile, width=imgWidth, height=imgHeight) self.imgWidth = imgWidth self.imgHeight = imgHeight self.c = Paragraph(captionTxt, style=captionStyle) self.margin = margin # 4-tuple. margins in order: top, right, bottom, left self.padding = padding # same as above self.borderColor = borderColor self.align = align self.cs = captionStyle self.captionTxt = captionTxt self.availWidth = None self.availHeight = None self.url = url def draw(self): canv = self.canv if self.align == "center": canv.translate((self.availWidth - self.width) / 2, 0) canv.saveState() canv.setStrokeColor(Color(self.borderColor[0], self.borderColor[1], self.borderColor[2])) canv.rect(self.margin[3], self.margin[2], self.boxWidth, self.boxHeight) canv.restoreState() canv.translate(self.margin[3] + self.padding[3], self.margin[2] + self.padding[2] - 2) self.c.canv = canv self.c.draw() canv.translate((self.boxWidth - self.padding[1] - self.padding[3] - self.i.drawWidth) / 2, self.captionHeight + 2) self.i.canv = canv self.i.draw() if self.url: frags = urlparse.urlsplit(self.url.encode('utf-8')) clean_url = urlparse.urlunsplit((frags.scheme, frags.netloc, urllib.quote(frags.path, safe='/'), urllib.quote(frags.query, safe='=&'), frags.fragment,)).decode('utf-8') canv.linkURL(clean_url, (0, 0, self.imgWidth, self.imgHeight), relative=1, thickness=0) def wrap(self, availWidth, availHeight): self.availWidth = availWidth self.availHeight = availHeight contentWidth = max(self.i.drawWidth, self.c.wrap(self.i.drawWidth, availHeight)[0]) self.boxWidth = contentWidth + self.padding[1] + self.padding[3] (self.captionWidth, self.captionHeight) = self.c.wrap(contentWidth, availHeight) self.captionHeight += self.cs.spaceBefore + self.cs.spaceAfter self.boxHeight = self.i.drawHeight + self.captionHeight + self.padding[0] + self.padding[2] self.width = self.boxWidth + self.margin[1] + self.margin[3] self.height = self.boxHeight + self.margin[0] + self.margin[2] return (self.width, self.height)
def draw(self): self.canv.rotate(90) Image.draw(self)