reportlab/lib/corp.py
changeset 1121 32fb78c22ae4
parent 494 54257447cfe9
child 1134 d29ad6e71bfa
--- a/reportlab/lib/corp.py	Wed Aug 08 19:16:57 2001 +0000
+++ b/reportlab/lib/corp.py	Wed Aug 08 19:17:23 2001 +0000
@@ -2,106 +2,131 @@
 #copyright ReportLab Inc. 2000
 #see license.txt for license details
 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/corp.py?cvsroot=reportlab
-#$Header: /tmp/reportlab/reportlab/lib/corp.py,v 1.2 2000/10/25 08:57:45 rgbecker Exp $
-
+#$Header: /tmp/reportlab/reportlab/lib/corp.py,v 1.3 2001/08/08 19:17:23 rgbecker Exp $
 """ This module includes some reusable routines for ReportLab's
  'Corporate Image' - the logo, standard page backdrops and
  so on - you are advised to do the same for your own company!"""
-__version__=''' $Id: corp.py,v 1.2 2000/10/25 08:57:45 rgbecker Exp $ '''
+__version__=''' $Id: corp.py,v 1.3 2001/08/08 19:17:23 rgbecker Exp $ '''
 
 from reportlab.lib.units import inch
+from reportlab.lib.validators import *
+from reportlab.lib.attrmap import *
+from reportlab.graphics import shapes
+from reportlab.graphics.widgetbase import Widget
+from reportlab.lib.colors import Color, white, ReportLabBlue
+from reportlab.pdfbase.pdfmetrics import stringWidth
+from math import sin, pi
+
+class RL_CorpLogo(Widget):
+	"""The ReportLab Logo.
+
+	New version created by John Precedo on 7-8 August 2001.
+	Based on bitmapped imaged from E-Id.
+	Possible attributes"""
+
+	_attrMap = AttrMap(
+		x = AttrMapValue(isNumber),
+		y = AttrMapValue(isNumber),
+		height = AttrMapValue(isNumberOrNone),
+		width = AttrMapValue(isNumberOrNone),
+		fillColor = AttrMapValue(isColorOrNone),
+		strokeColor = AttrMapValue( isColorOrNone)
+		)
+
+	_h = 90.5
+	_w = 136.5
+	_text='R e p o r t L a b'
+	_fontName = 'Helvetica-Bold'
+	_fontSize = 16
+
+	def __init__(self):
+		self.fillColor = ReportLabBlue
+		self.strokeColor = white
+		self.x = 0
+		self.y = 0
+		self.height = self._h
+		self.width = self._w
+
+	def demo(self):
+		D = shapes.Drawing(self.width, self.height)
+		D.add(self)
+		return D
+
+	def _getText(self, x=0, y=0, color=None):
+		return shapes.String(x,y, self._text, fontName=self._fontName, fontSize=self._fontSize, fillColor=color)
+
+	def _sw(self,f=None,l=None):
+		text = self._text
+		if f is None: f = 0
+		if l is None: l = len(text)
+		return stringWidth(text[f:l],self._fontName,self._fontSize)
+
+	def _addPage(self, g, strokeWidth=3, color=None, dx=0, dy=0):
+		x1, x2 = 31.85+dx, 80.97+dx
+		fL = 10	# fold length
+		y1, y2 = dy-34, dy+50.5
+		L = [[x1,dy-4,x1,y1, x2, y1, x2, dy-1],
+			[x1,dy+11,x1,y2,x2-fL,y2,x2,y2-fL,x2,dy+14],
+			[x2-10,y2,x2-10,y2-fL,x2,y2-fL]]
+
+		for l in L:
+			g.add(shapes.PolyLine(l, strokeWidth=strokeWidth, strokeColor=color, strokeLineJoin=0))
+
+	def draw(self):
+		sx = 0.5
+		fillColor = self.fillColor
+		strokeColor = self.strokeColor
+		shadow = Color(fillColor.red*sx,fillColor.green*sx,fillColor.blue*sx)
+		g = shapes.Group()
+		g2= shapes.Group()
+		g.add(shapes.Rect(fillColor=fillColor, strokeColor=fillColor, x=0, y=0, width=self._w, height=self._h))
+		sx = (self._w-2)/self._sw()
+		g2.scale(sx,1)
+		self._addPage(g2,strokeWidth=3,dx=2,dy=-2.5,color=shadow)
+		self._addPage(g2,strokeWidth=3,color=strokeColor)
+		g2.scale(1/sx,1)
+		g2.add(self._getText(x=1,y=0,color=shadow))
+		g2.add(self._getText(x=0,y=1,color=strokeColor))
+		g2.scale(sx,1)
+		g2.skew(kx=10, ky=0)
+		g2.shift(0,38)
+		g.add(g2)
+		g.scale(self.width/self._w,self.height/self._h)
+		g.shift(self.x,self.y)
+		return g
 
 class ReportLabLogo:
-    """vector reportlab logo centered in a 250x by 150y rectangle"""
-    
-    def __init__(self, atx=0, aty=0, width=2.5*inch, height=1.5*inch, powered_by=0):
-        self.origin = (atx, aty)
-        self.dimensions = (width, height)
-        self.powered_by = powered_by
-        
-    dogrid = 0
-    xticks = 25
-    yticks = 15
-    def draw(self, canvas):
-        canvas.saveState()
-        (atx,aty) = self.origin
-        canvas.translate(atx, aty)
-        (width, height) = self.dimensions
-        canvas.scale(width/250.0, height/150.0)
-        # do a skew
-        canvas.skew(0,20)
-        self.setup(canvas)
-        if self.powered_by:
-            canvas.setFont("Helvetica-Bold", 18)
-            canvas.drawString(-15, 135, "powered by")
-        # do a stretched "ReportLab"
-        canvas.saveState()
-        canvas.setFont("Helvetica-Bold", 30)
-        canvas.scale(1.65,1)
-        canvas.drawString(0,60,"ReportLab")
-        canvas.restoreState()
-        # draw the "page" as a bunch of rectangles
-        rects = [
-        #  x  y  h, w
-         [65,10,40,8], # below p
-         [65,53,22.5,8], # over p
-         [65,78,50,8], # above p
-         [65,123,7,75], # across top
-         [161,86,29,9], # above L
-         [161,60.5,21,9], # over L
-         [161,10,47,9], # below L
-         [65,10,7,102], # across bottom
-         [140,110,20,10], # notch vertical
-         [140,110,7,29.5],# notch horizontal
-         ]
-        fill = 1
-        for [x,y,h,w] in rects:
-            canvas.rect(x,y,w,h, stroke=1, fill=fill)
-            #break
-        path = canvas.beginPath()
-        path.moveTo(150,130)
-        for (x,y) in ((170,115), (167,112), (147,127)):
-            path.lineTo(x,y)
-        #path.close()
-        canvas.drawPath(path, stroke=1, fill=fill)
-        canvas.restoreState()
-   
-    def setup(self, canvas):
-        xticks, yticks = self.xticks, self.yticks
-        if self.dogrid:
-            canvas.saveState()
-            for y in range(yticks):
-                if y%5==0:
-                     canvas.setStrokeColorRGB(1,0,0)
-                canvas.line(0,y*10,xticks*10,y*10)
-                if y%5==0:
-                     canvas.setStrokeColorRGB(0,1,0)
-            for x in range(xticks):
-                if x%5==0:
-                     canvas.setStrokeColorRGB(1,0,0)
-                canvas.line(x*10,yticks*10,x*10,0)
-                if x%5==0:
-                     canvas.setStrokeColorRGB(0,1,0)
-            canvas.restoreState()
+	"""vector reportlab logo centered in a 250x by 150y rectangle"""
+	
+	def __init__(self, atx=0, aty=0, width=2.5*inch, height=1.5*inch, powered_by=0):
+		self.origin = (atx, aty)
+		self.dimensions = (width, height)
+		self.powered_by = powered_by
+		
+	def draw(self, canvas):
+		from reportlab.graphics import renderPDF
+		canvas.saveState()
+		(atx,aty) = self.origin
+		(width, height) = self.dimensions
+		logo = RL_CorpLogo()
+		logo.width, logo.height = width, height
+		renderPDF.draw(logo.demo(),canvas,atx,aty,0)
+		canvas.restoreState()
 
-def main(filename, logomaker):
-    from reportlab.pdfgen import canvas
-    c = canvas.Canvas(filename)
-    #c.translate(inch,inch)
-    # scale each point to a quarter inch/10
-    #tqinch = inch/40.0
-    #c.scale(tqinch, tqinch)
-    logomaker.draw(c)
-    c.save()
-    
-if __name__=="__main__":
-    logomaker = ReportLabLogo(inch, 8*inch, powered_by=1)
-    main("logo.pdf", logomaker)
-    
+def test():
+	"""This function produces a pdf with examples. """
+	from reportlab.graphics import renderPDF, renderPM
+	rl = RL_CorpLogo()
+	D = shapes.Drawing(rl.width,rl.height)
+	D.add(rl)
+	renderPDF.drawToFile(D, 'corplogo.pdf', 'corplogo.pdf')
+	print 'Wrote corplogo.pdf'
+	rl.width = 129
+	rl.height = 86
+	D = shapes.Drawing(rl.width,rl.height)
+	D.add(rl)
+	renderPM.drawToFile(D, 'corplogo.gif')
+	print 'Wrote corplogo.gif'
 
-
-
-
-
-
-
+if __name__=='__main__':
+	test()