reportlab/platypus/figures.py
author andy_robinson
Mon, 26 Nov 2001 21:49:01 +0000
changeset 1440 243d35446390
parent 1420 bd1f0adf63d3
child 1559 2bb75ab0d0aa
permissions -rw-r--r--
Removed 0 from multiBuild stuff prior to further changes; ensured test suite really does run silently when making manuals.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     1
#copyright ReportLab Inc. 2000
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     2
#see license.txt for license details
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/docs/tools/platdemos.py?cvsroot=reportlab
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/platypus/figures.py,v 1.1 2001/11/13 17:29:56 dinu_gherman Exp $
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     5
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     6
"""This includes some demos of platypus for use in the API proposal"""
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     7
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     8
from reportlab.lib import colors
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     9
from reportlab.pdfgen.canvas import Canvas
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    10
from reportlab.lib.styles import ParagraphStyle
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    11
from reportlab.platypus import Frame
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    12
from reportlab.platypus import Flowable
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    13
from reportlab.platypus import Paragraph
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    14
from reportlab.lib.units import inch
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    15
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    16
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    17
captionStyle = ParagraphStyle('Caption', fontName='Times-Italic', fontSize=10, alignment=TA_CENTER)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    18
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    19
class Figure(Flowable):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    20
	def __init__(self, width, height, caption="",
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    21
				 captionFont="Times-Italic", captionSize=12):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    22
		Flowable.__init__(self)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    23
		self.width = width
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    24
		self.figureHeight = height
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    25
		self.captionHeight = 0	# work out later
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    26
		self.caption = caption
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    27
		self.captionStyle = ParagraphStyle(
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    28
			'Caption',
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    29
			fontName=captionFont,
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    30
			fontSize=captionSize,
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    31
			leading=1.2*captionSize,
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    32
			spaceBefore=captionSize * 0.5,
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    33
			alignment=TA_CENTER)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    34
		#must build paragraph now to get sequencing in synch
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    35
		#with rest of story
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    36
		self.captionPara = Paragraph(self.caption, self.captionStyle)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    37
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    38
		self.spaceBefore = 12
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    39
		self.spaceAfter = 12
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    40
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    41
	def wrap(self, availWidth, availHeight):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    42
		# try to get the caption aligned
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    43
		(w, h) = self.captionPara.wrap(self.width, availHeight - self.figureHeight)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    44
		self.captionHeight = h
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    45
		self.height = self.captionHeight + self.figureHeight
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    46
		self.dx = 0.5 * (availWidth - self.width)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    47
		return (self.width, self.height)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    48
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    49
	def draw(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    50
		self.canv.translate(self.dx, 0)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    51
		self.drawCaption()
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    52
		self.canv.translate(0, self.captionHeight)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    53
		self.drawBorder()
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    54
		self.drawFigure()
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    55
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    56
	def drawBorder(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    57
		self.canv.rect(0, 0, self.width, self.figureHeight)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    58
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    59
	def drawCaption(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    60
		self.captionPara.drawOn(self.canv, 0, 0)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    61
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    62
	def drawFigure(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    63
		pass
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    64
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    65
def drawPage(canvas,x, y, width, height):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    66
	#draws something which looks like a page
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    67
	pth = canvas.beginPath()
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    68
	corner = 0.05*width
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    69
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    70
	# shaded backdrop offset a little
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    71
	canvas.setFillColorRGB(0.5,0.5,0.5)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    72
	canvas.rect(x + corner, y - corner, width, height, stroke=0, fill=1)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    73
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    74
	#'sheet of paper' in light yellow
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    75
	canvas.setFillColorRGB(1,1,0.9)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    76
	canvas.setLineWidth(0)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    77
	canvas.rect(x, y, width, height, stroke=1, fill=1)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    78
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    79
	#reset
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    80
	canvas.setFillColorRGB(0,0,0)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    81
	canvas.setStrokeColorRGB(0,0,0)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    82
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    83
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    84
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    85
class PageFigure(Figure):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    86
	"""Shows a blank page in a frame, and draws on that.  Used in
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    87
	illustrations of how PLATYPUS works."""
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    88
	def __init__(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    89
		Figure.__init__(self, 3*inch, 3*inch)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    90
		self.caption = 'Figure 1 - a blank page'
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    91
		self.captionStyle = captionStyle
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    92
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    93
	def drawVirtualPage(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    94
		pass
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    95
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    96
	def drawFigure(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    97
		drawPage(self.canv, 0.625*inch, 0.25*inch, 1.75*inch, 2.5*inch)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    98
		self.canv.translate(0.625*inch, 0.25*inch)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    99
		self.canv.scale(1.75/8.27, 2.5/11.69)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   100
		self.drawVirtualPage()
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   101
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   102
class PlatPropFigure1(PageFigure):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   103
	"""This shows a page with a frame on it"""
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   104
	def __init__(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   105
		PageFigure.__init__(self)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   106
		self.caption = "Figure 1 - a page with a simple frame"
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   107
	def drawVirtualPage(self):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   108
		demo1(self.canv)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   109
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   110
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   111
def demo1(canvas):
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   112
	frame = Frame(
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   113
					2*inch,		# x
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   114
					4*inch,		# y at bottom
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   115
					4*inch,		# width
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   116
					5*inch,		# height
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   117
					showBoundary = 1  # helps us see what's going on
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   118
					)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   119
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   120
	bodyStyle = ParagraphStyle('Body', fontName='Times-Roman', fontSize=24, leading=28, spaceBefore=6)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   121
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   122
	para1 = Paragraph('Spam spam spam spam. ' * 5, bodyStyle)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   123
	para2 = Paragraph('Eggs eggs eggs. ' * 5, bodyStyle)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   124
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   125
	mydata = [para1, para2]
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   126
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   127
	#this does the packing and drawing.  The frame will consume
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   128
	#items from the front of the list as it prints them
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   129
	frame.addFromList(mydata,canvas)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   130
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   131
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   132
def test1():
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   133
	c  = Canvas('figures.pdf')
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   134
	f = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   135
	v = PlatPropFigure1()
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   136
	f.addFromList([v],c)
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   137
	c.save()
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   138
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   139
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   140
if __name__ == '__main__':
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   141
	test1()