reportlab/platypus/doctemplate.py
author rgbecker
Wed, 25 Oct 2000 08:57:46 +0000
changeset 494 54257447cfe9
parent 405 8ee871d827a2
child 500 58d712fef651
permissions -rw-r--r--
Changed to indirect copyright
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 405
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 405
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 405
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/doctemplate.py?cvsroot=reportlab
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 405
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/platypus/doctemplate.py,v 1.29 2000/10/25 08:57:45 rgbecker Exp $
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 405
diff changeset
     5
__version__=''' $Id: doctemplate.py,v 1.29 2000/10/25 08:57:45 rgbecker Exp $ '''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     6
__doc__="""
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
     7
This module contains the core structure of platypus.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
     8
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
     9
Platypus constructs documents.	Document styles are determined by DocumentTemplates.
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    10
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    11
Each DocumentTemplate contains one or more PageTemplates which defines the look of the
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    12
pages of the document.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    13
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    14
Each PageTemplate has a procedure for drawing the "non-flowing" part of the page
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    15
(for example the header, footer, page number, fixed logo graphic, watermark, etcetera) and
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    16
a set of Frames which enclose the flowing part of the page (for example the paragraphs,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    17
tables, or non-fixed diagrams of the text).
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    18
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    19
A document is built when a DocumentTemplate is fed a sequence of Flowables.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    20
The action of the build consumes the flowables in order and places them onto
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    21
frames on pages as space allows.  When a frame runs out of space the next frame
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    22
of the page is used.  If no frame remains a new page is created.  A new page
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    23
can also be created if a page break is forced.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    24
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    25
The special invisible flowable NextPageTemplate can be used to specify
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    26
the page template for the next page (which by default is the one being used
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    27
for the current frame).
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    28
"""
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    29
from reportlab.platypus.flowables import *
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    30
from reportlab.platypus.paragraph import Paragraph
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    31
from reportlab.platypus.frames import Frame
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    32
import reportlab.lib.sequencer
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    33
from types import *
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    34
import sys
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    35
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    36
def _doNothing(canvas, doc):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    37
	"Dummy callback for onPage"
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    38
	pass
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    39
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    40
class ActionFlowable(Flowable):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    41
	'''This Flowable is never drawn, it can be used for data driven controls
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    42
	   For example to change a page template (from one column to two, for example)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    43
	   use NextPageTemplate which creates an ActionFlowable.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    44
	'''
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    45
	def __init__(self,action=[]):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    46
		if type(action) not in (ListType, TupleType):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    47
			action = (action,)
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    48
		self.action = action
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    49
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    50
	def wrap(self, availWidth, availHeight):
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    51
		'''Should never be called.'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    52
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    53
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    54
	def draw(self):
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    55
		'''Should never be called.'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    56
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    57
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    58
	def apply(self,doc):
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    59
		'''
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    60
		This is called by the doc.build processing to allow the instance to
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    61
		implement its behaviour
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    62
		'''
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    63
		action = self.action[0]
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    64
		args = tuple(self.action[1:])
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    65
		arn = 'handle_'+action
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    66
		try:
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    67
			apply(getattr(doc,arn), args)
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    68
		except AttributeError, aerr:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    69
			if aerr.args[0]==arn:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    70
				raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    71
			else:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    72
				raise
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    73
		except "bogus":
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    74
			t, v, None = sys.exc_info()
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    75
			raise t, "%s\n	 handle_%s args=%s"%(v,action,args)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    76
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    77
	def __call__(self):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    78
		return self
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    79
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    80
FrameBreak = ActionFlowable('frameEnd')
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    81
PageBegin = ActionFlowable('pageBegin')
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    82
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    83
class NextPageTemplate(ActionFlowable):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    84
	"""When you get to the next page, use the template specified (change to two column, for example)  """
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    85
	def __init__(self,pt):
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    86
		ActionFlowable.__init__(self,('nextPageTemplate',pt))
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    87
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    88
class PageTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    89
	"""
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    90
	essentially a list of Frames and an onPage routine to call at the start
332
6e57f2c4ae97 Cosmetics
rgbecker
parents: 325
diff changeset
    91
	of a page when this is selected. onPageEnd gets called at the end.
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
    92
	derived classes can also implement beforeDrawPage and afterDrawPage if they want
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    93
	"""
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
    94
	def __init__(self,id=None,frames=[],onPage=_doNothing, onPageEnd=_doNothing):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    95
		if type(frames) not in (ListType,TupleType): frames = [frames]
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    96
		assert filter(lambda x: not isinstance(x,Frame), frames)==[], "frames argument error"
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    97
		self.id = id
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    98
		self.frames = frames
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
    99
		self.onPage = onPage
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   100
		self.onPageEnd = onPageEnd
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   101
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   102
	def beforeDrawPage(self,canv,doc):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   103
		"""Override this if you want additional functionality or prefer
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   104
		a class based page routine.  Called before any flowables for
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   105
		this page are processed."""
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   106
		pass
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   107
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   108
	def afterDrawPage(self, canv, doc):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   109
		"""This is called after the last flowable for the page has
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   110
		been processed.  You might use this if the page header or
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   111
		footer needed knowledge of what flowables were drawn on
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   112
		this page."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   113
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   114
		
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   115
class BaseDocTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   116
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   117
	First attempt at defining a document template class.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   118
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   119
	The basic idea is simple.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   120
	0)	The document has a list of data associated with it
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   121
		this data should derive from flowables. We'll have
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   122
		special classes like PageBreak, FrameBreak to do things
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   123
		like forcing a page end etc.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   124
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   125
	1)	The document has one or more page templates.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   126
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   127
	2)	Each page template has one or more frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   128
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   129
	3)	The document class provides base methods for handling the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   130
		story events and some reasonable methods for getting the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   131
		story flowables into the frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   132
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   133
	4)	The document instances can override the base handler routines.
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   134
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   135
	Most of the methods for this class are not called directly by the user,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   136
	but in some advanced usages they may need to be overridden via subclassing.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   137
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   138
	EXCEPTION: doctemplate.build(...) must be called for most reasonable uses
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   139
	since it builds a document using the page template.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   140
	
310
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   141
	Each document template builds exactly one document into a file specified
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   142
	by the filename argument on initialization.
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   143
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   144
	Possible keyword arguments for the initialization:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   145
	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   146
	pageTemplates: A list of templates.  Must be nonempty.	Names
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   147
	  assigned to the templates are used for referring to them so no two used
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   148
	  templates should have the same name.	For example you might want one template
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   149
	  for a title page, one for a section first page, one for a first page of
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   150
	  a chapter and two more for the interior of a chapter on odd and even pages.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   151
	  If this argument is omitted then at least one pageTemplate should be provided
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   152
	  using the addPageTemplates method before the document is built.
310
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   153
	showBoundary: if set draw a box around the frame boundaries.
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   154
	leftMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   155
	rightMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   156
	topMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   157
	bottomMargin:  Margin sizes in points (default 1 inch)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   158
	  These margins may be overridden by the pageTemplates.  They are primarily of interest
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   159
	  for the SimpleDocumentTemplate subclass.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   160
	allowSplitting:  If set flowables (eg, paragraphs) may be split across frames or pages
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   161
	  (default: 1)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   162
	title: Internal title for document (does not automatically display on any page)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   163
	author: Internal author for document (does not automatically display on any page)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   164
	"""
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   165
	_initArgs = {	'pagesize':DEFAULT_PAGE_SIZE,
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   166
					'pageTemplates':[],
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   167
					'showBoundary':0,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   168
					'leftMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   169
					'rightMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   170
					'topMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   171
					'bottomMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   172
					'allowSplitting':1,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   173
					'title':None,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   174
					'author':None,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   175
					'_pageBreakQuick':1}
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   176
	_invalidInitArgs = ()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   177
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   178
	def __init__(self, filename, **kw):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   179
		"""create a document template bound to a filename (see class documentation for keyword arguments)"""
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   180
		self.filename = filename
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   181
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   182
		for k in self._initArgs.keys():
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   183
			if not kw.has_key(k):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   184
				v = self._initArgs[k]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   185
			else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   186
				if k in self._invalidInitArgs:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   187
					raise ValueError, "Invalid argument %s" % k
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   188
				v = kw[k]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   189
			setattr(self,k,v)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   190
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   191
		p = self.pageTemplates
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   192
		self.pageTemplates = []
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   193
		self.addPageTemplates(p)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   194
		self._calc()
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   195
		self.afterInit()
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   196
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   197
	def _calc(self):
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   198
		self._rightMargin = self.pagesize[0] - self.rightMargin
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   199
		self._topMargin = self.pagesize[1] - self.topMargin
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   200
		self.width = self._rightMargin - self.leftMargin
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   201
		self.height = self._topMargin - self.bottomMargin
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   202
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   203
	def clean_hanging(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   204
		'handle internal postponed actions'
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   205
		while len(self._hanging):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   206
			self.handle_flowable(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   207
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   208
	def addPageTemplates(self,pageTemplates):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   209
		'add one or a sequence of pageTemplates'
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   210
		if type(pageTemplates) not in (ListType,TupleType):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   211
			pageTemplates = [pageTemplates]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   212
		assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   213
		for t in pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   214
			self.pageTemplates.append(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   215
			
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   216
	def handle_documentBegin(self):
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   217
		'''implement actions at beginning of document'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   218
		self._hanging = [PageBegin]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   219
		self.pageTemplate = self.pageTemplates[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   220
		self.page = 0
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   221
		self.beforeDocument()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   222
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   223
	def handle_pageBegin(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   224
		'''Perform actions required at beginning of page.
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   225
		shouldn't normally be called directly'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   226
		self.page = self.page + 1
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   227
		self.pageTemplate.beforeDrawPage(self.canv,self)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   228
		self.pageTemplate.onPage(self.canv,self)
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   229
		self.beforePage()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   230
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   231
			del self._nextFrameIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   232
		self.frame = self.pageTemplate.frames[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   233
		self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   234
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   235
	def handle_pageEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   236
		'''	show the current page
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   237
			check the next page template
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   238
			hang a page begin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   239
		'''
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   240
		self.pageTemplate.afterDrawPage(self.canv, self)
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   241
		self.pageTemplate.onPageEnd(self.canv, self)
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   242
		self.afterPage()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   243
		self.canv.showPage()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   244
		if hasattr(self,'_nextPageTemplateIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   245
			self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   246
			del self._nextPageTemplateIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   247
		self._hanging.append(PageBegin)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   248
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   249
	def handle_pageBreak(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   250
		'''some might choose not to end all the frames'''
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   251
		if self._pageBreakQuick:
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   252
			self.handle_pageEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   253
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   254
			n = len(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   255
			while len(self._hanging)==n:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   256
				self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   257
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   258
	def handle_frameBegin(self,*args):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   259
		'''What to do at the beginning of a page'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   260
		self.frame._reset()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   261
		if self.showBoundary or self.frame.showBoundary:
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
   262
			self.frame.drawBoundary(self.canv)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   263
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   264
	def handle_frameEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   265
		'''	Handles the semantics of the end of a frame. This includes the selection of
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   266
			the next frame or if this is the last frame then invoke pageEnd.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   267
		'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   268
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   269
			frame = self.pageTemplate.frames[self._nextFrameIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   270
			del self._nextFrameIndex
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   271
			self.handle_frameBegin()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   272
		elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   273
			self.handle_pageEnd()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   274
			self.frame = None
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   275
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   276
			f = self.frame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   277
			self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   278
			self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   279
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   280
	def handle_nextPageTemplate(self,pt):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   281
		'''On endPage chenge to the page template with name or index pt'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   282
		if type(pt) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   283
			for t in self.pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   284
				if t.id == pt:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   285
					self._nextPageTemplateIndex = self.pageTemplates.index(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   286
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   287
			raise ValueError, "can't find template('%s')"%pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   288
		elif type(pt) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   289
			self._nextPageTemplateIndex = pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   290
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   291
			raise TypeError, "argument pt should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   292
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   293
	def handle_nextFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   294
		'''On endFrame chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   295
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   296
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   297
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   298
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   299
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   300
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   301
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   302
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   303
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   304
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   305
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   306
	def handle_currentFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   307
		'''chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   308
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   309
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   310
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   311
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   312
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   313
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   314
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   315
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   316
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   317
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   318
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   319
	def handle_flowable(self,flowables):
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   320
		'''try to handle one flowable from the front of list flowables.'''
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   321
		
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   322
		#allow document a chance to look at, modify or ignore
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   323
		#the object(s) about to be processed
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   324
		self.filterFlowables(flowables)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   325
		f = flowables[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   326
		del flowables[0]
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   327
		if f is None:
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   328
			return
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   329
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   330
		if isinstance(f,PageBreak):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   331
			self.handle_pageBreak()
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   332
			self.afterFlowable(f)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   333
		elif isinstance(f,ActionFlowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   334
			f.apply(self)
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   335
			self.afterFlowable(f)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   336
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   337
			#general case we have to do something
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   338
			if self.frame.add(f, self.canv, trySplit=self.allowSplitting):
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   339
				self.afterFlowable(f)
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   340
			else:
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   341
				if self.allowSplitting:
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   342
					# see if this is a splittable thing
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 322
diff changeset
   343
					S = self.frame.split(f,self.canv)
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   344
					n = len(S)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   345
				else:
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   346
					n = 0
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   347
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   348
				if n:
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   349
					if self.frame.add(S[0], self.canv, trySplit=0):
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   350
						self.afterFlowable(f)
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   351
					else:
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   352
						raise "LayoutError", "splitting error"
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   353
					del S[0]
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   354
					for f in xrange(n-1):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   355
						flowables.insert(f,S[f])	# put split flowables back on the list
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   356
				else:
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   357
					if hasattr(f,'postponed'):
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   358
						raise "LayoutError", "Flowable too large"
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   359
					f.postponed = 1
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   360
					flowables.insert(0,f)			# put the flowable back
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   361
					self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   362
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   363
	#these are provided so that deriving classes can refer to them
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   364
	_handle_documentBegin = handle_documentBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   365
	_handle_pageBegin = handle_pageBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   366
	_handle_pageEnd = handle_pageEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   367
	_handle_frameBegin = handle_frameBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   368
	_handle_frameEnd = handle_frameEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   369
	_handle_flowable = handle_flowable
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   370
	_handle_nextPageTemplate = handle_nextPageTemplate
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   371
	_handle_currentFrame = handle_currentFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   372
	_handle_nextFrame = handle_nextFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   373
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   374
	def _startBuild(self, filename=None, canvasmaker=canvas.Canvas):
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   375
		self._calc()
405
8ee871d827a2 Added pagesize to canvasmaker call
rgbecker
parents: 339
diff changeset
   376
		self.canv = canvasmaker(filename or self.filename,pagesize=self.pagesize)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   377
		self.handle_documentBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   378
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   379
	def _endBuild(self):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   380
		if self._hanging!=[] and self._hanging[-1] is PageBegin:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   381
			del self._hanging[-1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   382
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   383
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   384
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   385
			self.handle_pageBreak()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   386
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   387
		self.canv.save()
339
e6ccd2f83f6c Pre-incrementing bug fixed
andy_robinson
parents: 332
diff changeset
   388
		#AR - hack - for some reason a document did not
e6ccd2f83f6c Pre-incrementing bug fixed
andy_robinson
parents: 332
diff changeset
   389
		#have these:
e6ccd2f83f6c Pre-incrementing bug fixed
andy_robinson
parents: 332
diff changeset
   390
		#if hasattr(self, 'frame'): del self.frame
e6ccd2f83f6c Pre-incrementing bug fixed
andy_robinson
parents: 332
diff changeset
   391
		#if hasattr(self, 'pageTemplate'): del self.pageTemplate
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   392
		del self.frame, self.pageTemplate
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   393
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   394
	def build(self, flowables, filename=None, canvasmaker=canvas.Canvas):
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   395
		"""Build the document from a list of flowables.
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   396
		   If the filename argument is provided then that filename is used
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   397
		   rather than the one provided upon initialization.
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   398
		   If the canvasmaker argument is provided then it will be used
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   399
		   instead of the default.	For example a slideshow might use
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   400
		   an alternate canvas which places 6 slides on a page (by
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   401
		   doing translations, scalings and redefining the page break
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   402
		   operations).
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   403
		"""
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   404
		#assert filter(lambda x: not isinstance(x,Flowable), flowables)==[], "flowables argument error"
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   405
		self._startBuild(filename,canvasmaker)
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   406
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   407
		while len(flowables):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   408
			self.clean_hanging()
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   409
			self.handle_flowable(flowables)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   410
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   411
		self._endBuild()
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   412
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   413
	#these are pure virtuals override in derived classes
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   414
	#NB these get called at suitable places by the base class
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   415
	#so if you derive and override the handle_xxx methods
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   416
	#it's up to you to ensure that they maintain the needed consistency
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   417
	def afterInit(self):
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   418
		"""This is called after initialisation of the base class."""
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   419
		pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   420
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   421
	def beforeDocument(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   422
		"""This is called before any processing is
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   423
		done on the document."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   424
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   425
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   426
	def beforePage(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   427
		"""This is called at the beginning of page
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   428
		processing, and immediately before the
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   429
		beforeDrawPage method of the current page
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   430
		template."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   431
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   432
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   433
	def afterPage(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   434
		"""This is called after page processing, and
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   435
		immediately after the afterDrawPage method
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   436
		of the current page template."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   437
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   438
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   439
	def filterFlowables(self,flowables):
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   440
		'''called to filter flowables at the start of the main handle_flowable method.
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   441
		Upon return if flowables[0] has been set to None it is discarded and the main
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   442
		method returns.
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   443
		'''
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   444
		pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   445
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   446
	def afterFlowable(self, flowable):
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   447
		'''called after a flowable has been rendered'''
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   448
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   449
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   450
class SimpleDocTemplate(BaseDocTemplate):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   451
	"""A special case document template that will handle many simple documents.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   452
	   See documentation for BaseDocTemplate.  No pageTemplates are required 
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   453
	   for this special case.	A page templates are inferred from the
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   454
	   margin information and the onFirstPage, onLaterPages arguments to the build method.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   455
	   
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   456
	   A document which has all pages with the same look except for the first
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   457
	   page may can be built using this special approach.
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   458
	"""
322
f2e1df84eb51 Force _calc in SimpleDocTemplae.build
rgbecker
parents: 316
diff changeset
   459
	_invalidInitArgs = ('pageTemplates',)
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   460
	def handle_pageBegin(self):
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   461
		'''override base method to add a change of page template after the firstpage.
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   462
		'''
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   463
		self._handle_pageBegin()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   464
		self._handle_nextPageTemplate('Later')
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   465
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   466
	def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing):
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   467
		"""build the document using the flowables.	Annotate the first page using the onFirstPage
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   468
			   function and later pages using the onLaterPages function.  The onXXX pages should follow
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   469
			   the signature
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   470
			   
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   471
				  def myOnFirstPage(canvas, document):
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   472
					  # do annotations and modify the document
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   473
					  ...
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   474
					  
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   475
			   The functions can do things like draw logos, page numbers,
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   476
			   footers, etcetera. They can use external variables to vary
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   477
			   the look (for example providing page numbering or section names).
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   478
		"""
322
f2e1df84eb51 Force _calc in SimpleDocTemplae.build
rgbecker
parents: 316
diff changeset
   479
		self._calc()	#in case we changed margins sizes etc
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   480
		frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal')
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   481
		self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage),
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   482
						PageTemplate(id='Later',frames=frameT, onPage=onLaterPages)])
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   483
		if onFirstPage is _doNothing and hasattr(self,'onFirstPage'):
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   484
			self.pageTemplates[0].beforeDrawPage = self.onFirstPage
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   485
		if onLaterPages is _doNothing and hasattr(self,'onLaterPages'):
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   486
			self.pageTemplates[1].beforeDrawPage = self.onLaterPages
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   487
		BaseDocTemplate.build(self,flowables)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   488
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   489
if __name__ == '__main__':
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   490
	##########################################################
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   491
	##
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   492
	##	 testing
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   493
	##
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   494
	##########################################################
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   495
	def randomText():
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   496
		#this may or may not be appropriate in your company
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   497
		from random import randint, choice
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   498
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   499
		RANDOMWORDS = ['strategic','direction','proactive',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   500
		'reengineering','forecast','resources',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   501
		'forward-thinking','profit','growth','doubletalk',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   502
		'venture capital','IPO']
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   503
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   504
		sentences = 5
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   505
		output = ""
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   506
		for sentenceno in range(randint(1,5)):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   507
			output = output + 'Blah'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   508
			for wordno in range(randint(10,25)):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   509
				if randint(0,4)==0:
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   510
					word = choice(RANDOMWORDS)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   511
				else:
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   512
					word = 'blah'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   513
				output = output + ' ' +word
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   514
			output = output+'.'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   515
		return output
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   516
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   517
	def myFirstPage(canvas, doc):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   518
		canvas.saveState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   519
		canvas.setStrokeColor(red)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   520
		canvas.setLineWidth(5)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   521
		canvas.line(66,72,66,PAGE_HEIGHT-72)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   522
		canvas.setFont('Times-Bold',24)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   523
		canvas.drawString(108, PAGE_HEIGHT-108, "PLATYPUS")
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   524
		canvas.setFont('Times-Roman',12)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   525
		canvas.drawString(4 * inch, 0.75 * inch, "First Page")
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   526
		canvas.restoreState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   527
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   528
	def myLaterPages(canvas, doc):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   529
		canvas.saveState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   530
		canvas.setStrokeColor(red)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   531
		canvas.setLineWidth(5)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   532
		canvas.line(66,72,66,PAGE_HEIGHT-72)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   533
		canvas.setFont('Times-Roman',12)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   534
		canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   535
		canvas.restoreState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   536
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   537
	def run():
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   538
		objects_to_draw = []
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   539
		from reportlab.lib.styles import ParagraphStyle
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   540
		#from paragraph import Paragraph
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   541
		from doctemplate import SimpleDocTemplate
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   542
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   543
		#need a style
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   544
		normal = ParagraphStyle('normal')
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   545
		normal.firstLineIndent = 18
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   546
		normal.spaceBefore = 6
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   547
		import random
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   548
		for i in range(15):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   549
			height = 0.5 + (2*random.random())
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   550
			box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   551
			objects_to_draw.append(box)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   552
			para = Paragraph(randomText(), normal)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   553
			objects_to_draw.append(para)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   554
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   555
		SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw,
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   556
			onFirstPage=myFirstPage,onLaterPages=myLaterPages)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   557
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   558
	run()
294
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
   559