reportlab/platypus/doctemplate.py
author rgbecker
Thu, 06 Jul 2000 12:40:38 +0000
changeset 325 d3e82931bd56
parent 322 f2e1df84eb51
child 332 6e57f2c4ae97
permissions -rw-r--r--
Push canvas into flowables during wrap/split
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     1
###############################################################################
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     2
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     3
#	ReportLab Public License Version 1.0
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     4
#
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
     5
#	Except for the change of names the spirit and intention of this
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
     6
#	license is the same as that of Python
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     7
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     8
#	(C) Copyright ReportLab Inc. 1998-2000.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     9
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    10
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    11
# All Rights Reserved
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    12
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    13
# Permission to use, copy, modify, and distribute this software and its
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    14
# documentation for any purpose and without fee is hereby granted, provided
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    15
# that the above copyright notice appear in all copies and that both that
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    16
# copyright notice and this permission notice appear in supporting
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    17
# documentation, and that the name of ReportLab not be used
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    18
# in advertising or publicity pertaining to distribution of the software
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    19
# without specific, written prior permission. 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    20
# 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    21
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    22
# Disclaimer
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    23
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    24
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    25
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    26
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    27
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    28
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    29
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    30
# PERFORMANCE OF THIS SOFTWARE. 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    31
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    32
###############################################################################
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    33
#	$Log: doctemplate.py,v $
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 322
diff changeset
    34
#	Revision 1.25  2000/07/06 12:40:37  rgbecker
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 322
diff changeset
    35
#	Push canvas into flowables during wrap/split
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 322
diff changeset
    36
#
322
f2e1df84eb51 Force _calc in SimpleDocTemplae.build
rgbecker
parents: 316
diff changeset
    37
#	Revision 1.24  2000/07/05 12:22:21  rgbecker
f2e1df84eb51 Force _calc in SimpleDocTemplae.build
rgbecker
parents: 316
diff changeset
    38
#	Force _calc in SimpleDocTemplae.build
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 322
diff changeset
    39
#	
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    40
#	Revision 1.23  2000/07/03 15:39:51  rgbecker
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    41
#	Documentation fixes
322
f2e1df84eb51 Force _calc in SimpleDocTemplae.build
rgbecker
parents: 316
diff changeset
    42
#	
310
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
    43
#	Revision 1.22  2000/06/28 14:52:43  rgbecker
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
    44
#	Documentation changes
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
    45
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    46
#	Revision 1.21  2000/06/26 15:58:22  rgbecker
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    47
#	Simple fix to widths problem
310
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
    48
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    49
#	Revision 1.20  2000/06/21 12:27:42	rgbecker
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
    50
#	remove UserDocTemplate, but add Andy's hook methods
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    51
#	
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    52
#	Revision 1.19  2000/06/20 21:56:17	andy_robinson
294
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
    53
#	re-synching after sourceforge went weird
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
    54
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    55
#	Revision 1.18  2000/06/19 23:51:23	andy_robinson
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
    56
#	Added UserDocTemplate class, and paragraph.getPlainText()
294
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
    57
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    58
#	Revision 1.17  2000/06/19 11:14:03	andy_robinson
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    59
#	Global sequencer put in the 'story builder'.
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
    60
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    61
#	Revision 1.16  2000/06/16 13:49:20	aaron_watters
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    62
#	new build parameters to allow alternate filename and canvas implementation
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    63
#	(in order to support slideshow summary mode, for example, or embedding one
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    64
#	document in another).
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    65
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    66
#	Revision 1.15  2000/06/13 13:03:31	aaron_watters
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    67
#	more documentation changes
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
    68
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    69
#	Revision 1.14  2000/06/01 16:27:56	rgbecker
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    70
#	pageSize is wrong at present
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    71
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    72
#	Revision 1.13  2000/06/01 15:23:06	rgbecker
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    73
#	Platypus re-organisation
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
    74
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    75
#	Revision 1.12  2000/05/26 10:27:37	rgbecker
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
    76
#	Fixed infinite recursion bug
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    77
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    78
#	Revision 1.11  2000/05/17 22:17:38	rgbecker
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    79
#	Renamed BasicFrame to Frame
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
    80
#	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
    81
#	Revision 1.10  2000/05/17 16:29:40	rgbecker
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
    82
#	Removal of SimpleFrame
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
    83
#	
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    84
#	Revision 1.9  2000/05/17 15:37:33  rgbecker
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    85
#	Changes related to removal of SimpleFlowDocument
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
    86
#	
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    87
#	Revision 1.8  2000/05/16 16:15:16  rgbecker
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    88
#	Changes related to removal of SimpleFlowDocument
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
    89
#	
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    90
#	Revision 1.7  2000/05/16 14:28:55  rgbecker
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    91
#	Fixes/Changes to get testplatypus to work with new framework
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
    92
#	
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    93
#	Revision 1.6  2000/05/15 15:07:32  rgbecker
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    94
#	Added drawPage
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
    95
#	
206
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
    96
#	Revision 1.5  2000/05/13 08:33:53  rgbecker
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
    97
#	fix typo in import
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
    98
#	
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
    99
#	Revision 1.4  2000/05/12 16:21:02  rgbecker
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   100
#	_donothing explicit import
206
8c0649978a61 fix typo in import
rgbecker
parents: 204
diff changeset
   101
#	
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   102
#	Revision 1.3  2000/05/12 14:53:38  rgbecker
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   103
#	Handle splitting error in build
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   104
#	
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   105
#	Revision 1.2  2000/05/12 14:45:31  rgbecker
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   106
#	Single actions only in ActionFlowables
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   107
#	
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   108
#	Revision 1.1  2000/05/12 12:53:33  rgbecker
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   109
#	Initial try at a document template class
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   110
#	
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 322
diff changeset
   111
__version__=''' $Id: doctemplate.py,v 1.25 2000/07/06 12:40:37 rgbecker Exp $ '''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   112
__doc__="""
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   113
This module contains the core structure of platypus.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   114
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   115
Platypus constructs documents.	Document styles are determined by DocumentTemplates.
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   116
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   117
Each DocumentTemplate contains one or more PageTemplates which defines the look of the
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   118
pages of the document.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   119
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   120
Each PageTemplate has a procedure for drawing the "non-flowing" part of the page
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   121
(for example the header, footer, page number, fixed logo graphic, watermark, etcetera) and
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   122
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
   123
tables, or non-fixed diagrams of the text).
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   124
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   125
A document is built when a DocumentTemplate is fed a sequence of Flowables.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   126
The action of the build consumes the flowables in order and places them onto
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   127
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
   128
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
   129
can also be created if a page break is forced.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   130
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   131
The special invisible flowable NextPageTemplate can be used to specify
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   132
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
   133
for the current frame).
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   134
"""
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   135
from reportlab.platypus.flowables import *
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   136
from reportlab.platypus.paragraph import Paragraph
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   137
from reportlab.platypus.frames import Frame
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   138
import reportlab.lib.sequencer
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   139
from types import *
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   140
import sys
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   141
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   142
def _doNothing(canvas, doc):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   143
	"Dummy callback for onPage"
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   144
	pass
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   145
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   146
class ActionFlowable(Flowable):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   147
	'''This Flowable is never drawn, it can be used for data driven controls
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   148
	   For example to change a page template (from one column to two, for example)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   149
	   use NextPageTemplate which creates an ActionFlowable.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   150
	'''
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   151
	def __init__(self,action=[]):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   152
		if type(action) not in (ListType, TupleType):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   153
			action = (action,)
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   154
		self.action = action
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   155
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   156
	def wrap(self, availWidth, availHeight):
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   157
		'''Should never be called.'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   158
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   159
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   160
	def draw(self):
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   161
		'''Should never be called.'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   162
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   163
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   164
	def apply(self,doc):
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   165
		'''
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   166
		This is called by the doc.build processing to allow the instance to
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   167
		implement its behaviour
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   168
		'''
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   169
		action = self.action[0]
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   170
		args = tuple(self.action[1:])
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   171
		arn = 'handle_'+action
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   172
		try:
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   173
			apply(getattr(doc,arn), args)
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   174
		except AttributeError, aerr:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   175
			if aerr.args[0]==arn:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   176
				raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   177
			else:
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   178
				raise
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   179
		except "bogus":
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   180
			t, v, None = sys.exc_info()
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   181
			raise t, "%s\n	 handle_%s args=%s"%(v,action,args)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   182
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   183
	def __call__(self):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   184
		return self
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   185
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   186
FrameBreak = ActionFlowable('frameEnd')
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   187
PageBegin = ActionFlowable('pageBegin')
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   188
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   189
class NextPageTemplate(ActionFlowable):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   190
	"""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
   191
	def __init__(self,pt):
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
   192
		ActionFlowable.__init__(self,('nextPageTemplate',pt))
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   193
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   194
class PageTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   195
	"""
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   196
	essentially a list of Frames and an onPage routine to call at the start
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   197
	of a page when this is selected. onPageEnd gets calld at the end.
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   198
	derived classes can also implement beforeDrawPage and afterDrawPage if they want
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   199
	"""
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   200
	def __init__(self,id=None,frames=[],onPage=_doNothing, onPageEnd=_doNothing):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   201
		if type(frames) not in (ListType,TupleType): frames = [frames]
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   202
		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
   203
		self.id = id
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   204
		self.frames = frames
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   205
		self.onPage = onPage
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   206
		self.onPageEnd = onPageEnd
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   207
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   208
	def beforeDrawPage(self,canv,doc):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   209
		"""Override this if you want additional functionality or prefer
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   210
		a class based page routine.  Called before any flowables for
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   211
		this page are processed."""
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   212
		pass
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   213
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   214
	def afterDrawPage(self, canv, doc):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   215
		"""This is called after the last flowable for the page has
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   216
		been processed.  You might use this if the page header or
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   217
		footer needed knowledge of what flowables were drawn on
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   218
		this page."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   219
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   220
		
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   221
class BaseDocTemplate:
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
	First attempt at defining a document template class.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   224
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   225
	The basic idea is simple.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   226
	0)	The document has a list of data associated with it
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   227
		this data should derive from flowables. We'll have
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   228
		special classes like PageBreak, FrameBreak to do things
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   229
		like forcing a page end etc.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   230
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   231
	1)	The document has one or more page templates.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   232
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   233
	2)	Each page template has one or more frames.
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
	3)	The document class provides base methods for handling the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   236
		story events and some reasonable methods for getting the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   237
		story flowables into the frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   238
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   239
	4)	The document instances can override the base handler routines.
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   240
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   241
	Most of the methods for this class are not called directly by the user,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   242
	but in some advanced usages they may need to be overridden via subclassing.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   243
	
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   244
	EXCEPTION: doctemplate.build(...) must be called for most reasonable uses
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   245
	since it builds a document using the page template.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   246
	
310
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   247
	Each document template builds exactly one document into a file specified
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   248
	by the filename argument on initialization.
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   249
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   250
	Possible keyword arguments for the initialization:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   251
	
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   252
	pageTemplates: A list of templates.  Must be nonempty.	Names
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   253
	  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
   254
	  templates should have the same name.	For example you might want one template
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   255
	  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
   256
	  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
   257
	  If this argument is omitted then at least one pageTemplate should be provided
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   258
	  using the addPageTemplates method before the document is built.
310
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   259
	showBoundary: if set draw a box around the frame boundaries.
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   260
	leftMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   261
	rightMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   262
	topMargin:
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   263
	bottomMargin:  Margin sizes in points (default 1 inch)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   264
	  These margins may be overridden by the pageTemplates.  They are primarily of interest
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   265
	  for the SimpleDocumentTemplate subclass.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   266
	allowSplitting:  If set flowables (eg, paragraphs) may be split across frames or pages
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   267
	  (default: 1)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   268
	title: Internal title for document (does not automatically display on any page)
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   269
	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
   270
	"""
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   271
	_initArgs = {	'pagesize':DEFAULT_PAGE_SIZE,
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   272
					'pageTemplates':[],
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   273
					'showBoundary':0,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   274
					'leftMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   275
					'rightMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   276
					'topMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   277
					'bottomMargin':inch,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   278
					'allowSplitting':1,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   279
					'title':None,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   280
					'author':None,
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   281
					'_pageBreakQuick':1}
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   282
	_invalidInitArgs = ()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   283
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   284
	def __init__(self, filename, **kw):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   285
		"""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
   286
		self.filename = filename
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   287
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   288
		for k in self._initArgs.keys():
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   289
			if not kw.has_key(k):
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   290
				v = self._initArgs[k]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   291
			else:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   292
				if k in self._invalidInitArgs:
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   293
					raise ValueError, "Invalid argument %s" % k
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   294
				v = kw[k]
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   295
			setattr(self,k,v)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   296
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   297
		p = self.pageTemplates
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   298
		self.pageTemplates = []
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   299
		self.addPageTemplates(p)
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   300
		self._calc()
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   301
		self.afterInit()
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
   302
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   303
	def _calc(self):
255
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   304
		self._rightMargin = self.pagesize[0] - self.rightMargin
ee9e321e747d pageSize is wrong at present
rgbecker
parents: 253
diff changeset
   305
		self._topMargin = self.pagesize[1] - self.topMargin
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   306
		self.width = self._rightMargin - self.leftMargin
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   307
		self.height = self._topMargin - self.bottomMargin
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   308
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   309
	def clean_hanging(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   310
		'handle internal postponed actions'
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   311
		while len(self._hanging):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   312
			self.handle_flowable(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   313
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   314
	def addPageTemplates(self,pageTemplates):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   315
		'add one or a sequence of pageTemplates'
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   316
		if type(pageTemplates) not in (ListType,TupleType):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   317
			pageTemplates = [pageTemplates]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   318
		assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   319
		for t in pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   320
			self.pageTemplates.append(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   321
			
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   322
	def handle_documentBegin(self):
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   323
		'''implement actions at beginning of document'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   324
		self._hanging = [PageBegin]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   325
		self.pageTemplate = self.pageTemplates[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   326
		self.page = 0
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   327
		self.beforeDocument()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   328
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   329
	def handle_pageBegin(self):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   330
		'''Perform actions required at beginning of page.
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   331
		shouldn't normally be called directly'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   332
		self.page = self.page + 1
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   333
		self.pageTemplate.beforeDrawPage(self.canv,self)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   334
		self.pageTemplate.onPage(self.canv,self)
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   335
		self.beforePage()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   336
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   337
			del self._nextFrameIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   338
		self.frame = self.pageTemplate.frames[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   339
		self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   340
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   341
	def handle_pageEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   342
		'''	show the current page
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   343
			check the next page template
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   344
			hang a page begin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   345
		'''
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   346
		self.pageTemplate.afterDrawPage(self.canv, self)
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   347
		self.pageTemplate.onPageEnd(self.canv, self)
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   348
		self.afterPage()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   349
		self.canv.showPage()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   350
		if hasattr(self,'_nextPageTemplateIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   351
			self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   352
			del self._nextPageTemplateIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   353
		self._hanging.append(PageBegin)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   354
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   355
	def handle_pageBreak(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   356
		'''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
   357
		if self._pageBreakQuick:
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   358
			self.handle_pageEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   359
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   360
			n = len(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   361
			while len(self._hanging)==n:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   362
				self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   363
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   364
	def handle_frameBegin(self,*args):
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   365
		'''What to do at the beginning of a page'''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   366
		self.frame._reset()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   367
		if self.showBoundary or self.frame.showBoundary:
226
a9acb67cdef7 Removal of SimpleFrame
rgbecker
parents: 225
diff changeset
   368
			self.frame.drawBoundary(self.canv)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   369
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   370
	def handle_frameEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   371
		'''	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
   372
			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
   373
		'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   374
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   375
			frame = self.pageTemplate.frames[self._nextFrameIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   376
			del self._nextFrameIndex
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   377
			self.handle_frameBegin()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   378
		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
   379
			self.handle_pageEnd()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   380
			self.frame = None
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   381
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   382
			f = self.frame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   383
			self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   384
			self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   385
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   386
	def handle_nextPageTemplate(self,pt):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   387
		'''On endPage chenge to the page template with name or index pt'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   388
		if type(pt) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   389
			for t in self.pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   390
				if t.id == pt:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   391
					self._nextPageTemplateIndex = self.pageTemplates.index(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   392
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   393
			raise ValueError, "can't find template('%s')"%pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   394
		elif type(pt) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   395
			self._nextPageTemplateIndex = pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   396
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   397
			raise TypeError, "argument pt should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   398
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   399
	def handle_nextFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   400
		'''On endFrame chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   401
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   402
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   403
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   404
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   405
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   406
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   407
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   408
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   409
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   410
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   411
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   412
	def handle_currentFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   413
		'''chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   414
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   415
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   416
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   417
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   418
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   419
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   420
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   421
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   422
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   423
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   424
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   425
	def handle_flowable(self,flowables):
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   426
		'''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
   427
		
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   428
		#allow document a chance to look at, modify or ignore
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   429
		#the object(s) about to be processed
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   430
		self.filterFlowables(flowables)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   431
		f = flowables[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   432
		del flowables[0]
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   433
		if f is None:
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   434
			return
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   435
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   436
		if isinstance(f,PageBreak):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   437
			self.handle_pageBreak()
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   438
			self.afterFlowable(f)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   439
		elif isinstance(f,ActionFlowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   440
			f.apply(self)
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   441
			self.afterFlowable(f)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   442
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   443
			#general case we have to do something
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   444
			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
   445
				self.afterFlowable(f)
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   446
			else:
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   447
				if self.allowSplitting:
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   448
					# see if this is a splittable thing
325
d3e82931bd56 Push canvas into flowables during wrap/split
rgbecker
parents: 322
diff changeset
   449
					S = self.frame.split(f,self.canv)
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   450
					n = len(S)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   451
				else:
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   452
					n = 0
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   453
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   454
				if n:
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   455
					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
   456
						self.afterFlowable(f)
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   457
					else:
200
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   458
						raise "LayoutError", "splitting error"
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   459
					del S[0]
74da573e661f Handle splitting error in build
rgbecker
parents: 199
diff changeset
   460
					for f in xrange(n-1):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   461
						flowables.insert(f,S[f])	# put split flowables back on the list
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   462
				else:
249
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   463
					if hasattr(f,'postponed'):
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   464
						raise "LayoutError", "Flowable too large"
1d9ea4f00348 Fixed infinite recursion bug
rgbecker
parents: 227
diff changeset
   465
					f.postponed = 1
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   466
					flowables.insert(0,f)			# put the flowable back
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   467
					self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   468
204
b19c3cae893a _donothing explicit import
rgbecker
parents: 200
diff changeset
   469
	#these are provided so that deriving classes can refer to them
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   470
	_handle_documentBegin = handle_documentBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   471
	_handle_pageBegin = handle_pageBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   472
	_handle_pageEnd = handle_pageEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   473
	_handle_frameBegin = handle_frameBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   474
	_handle_frameEnd = handle_frameEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   475
	_handle_flowable = handle_flowable
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   476
	_handle_nextPageTemplate = handle_nextPageTemplate
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   477
	_handle_currentFrame = handle_currentFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   478
	_handle_nextFrame = handle_nextFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   479
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   480
	def _startBuild(self, filename=None, canvasmaker=canvas.Canvas):
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   481
		self._calc()
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   482
		self.canv = canvasmaker(filename or self.filename)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   483
		self.handle_documentBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   484
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   485
	def _endBuild(self):
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   486
		if self._hanging!=[] and self._hanging[-1] is PageBegin:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   487
			del self._hanging[-1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   488
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   489
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   490
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   491
			self.handle_pageBreak()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   492
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   493
		self.canv.save()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   494
		del self.frame, self.pageTemplate
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   495
272
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   496
	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
   497
		"""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
   498
		   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
   499
		   rather than the one provided upon initialization.
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   500
		   If the canvasmaker argument is provided then it will be used
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   501
		   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
   502
		   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
   503
		   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
   504
		   operations).
80107aaffa7e new build parameters to allow alternate filename and canvas implementation
aaron_watters
parents: 268
diff changeset
   505
		"""
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   506
		#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
   507
		self._startBuild(filename,canvasmaker)
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   508
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   509
		while len(flowables):
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   510
			self.clean_hanging()
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   511
			self.handle_flowable(flowables)
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   512
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   513
		self._endBuild()
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   514
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   515
	#these are pure virtuals override in derived classes
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   516
	#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
   517
	#so if you derive and override the handle_xxx methods
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   518
	#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
   519
	def afterInit(self):
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   520
		"""This is called after initialisation of the base class."""
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   521
		pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   522
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   523
	def beforeDocument(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   524
		"""This is called before any processing is
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   525
		done on the document."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   526
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   527
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   528
	def beforePage(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   529
		"""This is called at the beginning of page
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   530
		processing, and immediately before the
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   531
		beforeDrawPage method of the current page
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   532
		template."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   533
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   534
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   535
	def afterPage(self):
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   536
		"""This is called after page processing, and
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   537
		immediately after the afterDrawPage method
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   538
		of the current page template."""
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   539
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   540
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   541
	def filterFlowables(self,flowables):
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   542
		'''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
   543
		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
   544
		method returns.
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   545
		'''
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   546
		pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   547
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   548
	def afterFlowable(self, flowable):
295
1f00bd112cb0 remove UserDocTemplate, but add Andy's hook methods
rgbecker
parents: 294
diff changeset
   549
		'''called after a flowable has been rendered'''
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   550
		pass
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   551
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   552
class SimpleDocTemplate(BaseDocTemplate):
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   553
	"""A special case document template that will handle many simple documents.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   554
	   See documentation for BaseDocTemplate.  No pageTemplates are required 
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   555
	   for this special case.	A page templates are inferred from the
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   556
	   margin information and the onFirstPage, onLaterPages arguments to the build method.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   557
	   
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   558
	   A document which has all pages with the same look except for the first
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
   559
	   page may can be built using this special approach.
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   560
	"""
322
f2e1df84eb51 Force _calc in SimpleDocTemplae.build
rgbecker
parents: 316
diff changeset
   561
	_invalidInitArgs = ('pageTemplates',)
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   562
	def handle_pageBegin(self):
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   563
		'''override base method to add a change of page template after the firstpage.
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   564
		'''
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   565
		self._handle_pageBegin()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   566
		self._handle_nextPageTemplate('Later')
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   567
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   568
	def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing):
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   569
		"""build the document using the flowables.	Annotate the first page using the onFirstPage
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   570
			   function and later pages using the onLaterPages function.  The onXXX pages should follow
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   571
			   the signature
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   572
			   
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   573
				  def myOnFirstPage(canvas, document):
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   574
					  # do annotations and modify the document
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   575
					  ...
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   576
					  
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 310
diff changeset
   577
			   The functions can do things like draw logos, page numbers,
305
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   578
			   footers, etcetera. They can use external variables to vary
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   579
			   the look (for example providing page numbering or section names).
f93b4c94a91e Simple fix to widths problem
rgbecker
parents: 295
diff changeset
   580
		"""
322
f2e1df84eb51 Force _calc in SimpleDocTemplae.build
rgbecker
parents: 316
diff changeset
   581
		self._calc()	#in case we changed margins sizes etc
227
fa5b4d81f471 Renamed BasicFrame to Frame
rgbecker
parents: 226
diff changeset
   582
		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
   583
		self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage),
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   584
						PageTemplate(id='Later',frames=frameT, onPage=onLaterPages)])
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   585
		if onFirstPage is _doNothing and hasattr(self,'onFirstPage'):
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   586
			self.pageTemplates[0].beforeDrawPage = self.onFirstPage
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   587
		if onLaterPages is _doNothing and hasattr(self,'onLaterPages'):
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   588
			self.pageTemplates[1].beforeDrawPage = self.onLaterPages
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   589
		BaseDocTemplate.build(self,flowables)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   590
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   591
if __name__ == '__main__':
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   592
	##########################################################
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   593
	##
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   594
	##	 testing
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   595
	##
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   596
	##########################################################
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   597
	def randomText():
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   598
		#this may or may not be appropriate in your company
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   599
		from random import randint, choice
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   600
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   601
		RANDOMWORDS = ['strategic','direction','proactive',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   602
		'reengineering','forecast','resources',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   603
		'forward-thinking','profit','growth','doubletalk',
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   604
		'venture capital','IPO']
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   605
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   606
		sentences = 5
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   607
		output = ""
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   608
		for sentenceno in range(randint(1,5)):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   609
			output = output + 'Blah'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   610
			for wordno in range(randint(10,25)):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   611
				if randint(0,4)==0:
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   612
					word = choice(RANDOMWORDS)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   613
				else:
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   614
					word = 'blah'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   615
				output = output + ' ' +word
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   616
			output = output+'.'
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   617
		return output
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   618
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   619
	def myFirstPage(canvas, doc):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   620
		canvas.saveState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   621
		canvas.setStrokeColor(red)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   622
		canvas.setLineWidth(5)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   623
		canvas.line(66,72,66,PAGE_HEIGHT-72)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   624
		canvas.setFont('Times-Bold',24)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   625
		canvas.drawString(108, PAGE_HEIGHT-108, "PLATYPUS")
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   626
		canvas.setFont('Times-Roman',12)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   627
		canvas.drawString(4 * inch, 0.75 * inch, "First Page")
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   628
		canvas.restoreState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   629
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   630
	def myLaterPages(canvas, doc):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   631
		canvas.saveState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   632
		canvas.setStrokeColor(red)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   633
		canvas.setLineWidth(5)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   634
		canvas.line(66,72,66,PAGE_HEIGHT-72)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   635
		canvas.setFont('Times-Roman',12)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   636
		canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   637
		canvas.restoreState()
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   638
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   639
	def run():
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   640
		objects_to_draw = []
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   641
		from reportlab.lib.styles import ParagraphStyle
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   642
		#from paragraph import Paragraph
225
1f58b00cf30e Changes related to removal of SimpleFlowDocument
rgbecker
parents: 221
diff changeset
   643
		from doctemplate import SimpleDocTemplate
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   644
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   645
		#need a style
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   646
		normal = ParagraphStyle('normal')
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   647
		normal.firstLineIndent = 18
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   648
		normal.spaceBefore = 6
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   649
		import random
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   650
		for i in range(15):
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   651
			height = 0.5 + (2*random.random())
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   652
			box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   653
			objects_to_draw.append(box)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   654
			para = Paragraph(randomText(), normal)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   655
			objects_to_draw.append(para)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   656
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   657
		SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw,
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   658
			onFirstPage=myFirstPage,onLaterPages=myLaterPages)
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   659
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   660
	run()
294
25d6a8334e99 re-synching after sourceforge went weird
andy_robinson
parents: 284
diff changeset
   661